FreeDATA/sock.py

209 lines
8 KiB
Python
Raw Normal View History

2021-02-16 13:23:57 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 25 21:25:14 2020
@author: DJ2LS
"""
import socketserver
import threading
import logging
2021-02-16 18:39:08 +00:00
import time
2021-02-16 13:23:57 +00:00
import static
2021-02-24 13:22:28 +00:00
import data_handler
2021-02-16 19:49:02 +00:00
import helpers
import fec
2021-02-28 14:24:14 +00:00
import asyncio
2021-02-16 13:23:57 +00:00
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
2021-02-16 13:36:01 +00:00
2021-02-16 18:39:08 +00:00
def handle(self):
2021-02-16 19:49:02 +00:00
2021-02-18 14:11:37 +00:00
encoding = 'utf-8'
#data = str(self.request.recv(1024), 'utf-8')
data = bytes()
while True:
chunk = self.request.recv(8192)#.strip()
data += chunk
if chunk.endswith(b'\n'):
break
data = data[:-1] # remove b'\n'
data = str(data, 'utf-8')
2021-02-16 18:39:08 +00:00
# SOCKETTEST ---------------------------------------------------
2021-02-16 18:39:08 +00:00
if data == 'SOCKETTEST':
2021-02-16 21:41:06 +00:00
cur_thread = threading.current_thread()
2021-02-18 14:11:37 +00:00
response = bytes("WELL DONE! YOU ARE ABLE TO COMMUNICATE WITH THE TNC", encoding)
2021-02-16 18:39:08 +00:00
self.request.sendall(response)
# CQ CQ CQ -----------------------------------------------------
if data == 'CQCQCQ':
for i in range(0,3):
2021-02-24 13:22:28 +00:00
data_handler.transmit_cq()
self.request.sendall(bytes("CALLING CQ"))
while static.ARQ_STATE == 'SENDING_SIGNALLING':
time.sleep(0.1)
pass
# PING ----------------------------------------------------------
if data.startswith('PING:'):
#send ping frame and wait for ACK
pingcommand = data.split('PING:')
dxcallsign = pingcommand[1]
2021-02-24 13:22:28 +00:00
data_handler.transmit_ping(dxcallsign)
# ARQ CONNECT TO CALLSIGN ----------------------------------------
if data.startswith('ARQ:CONNECT:'):
arqconnectcommand = data.split('ARQ:CONNECT:')
dxcallsign = arqconnectcommand[1]
2021-02-28 14:24:14 +00:00
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
2021-02-24 16:41:14 +00:00
if static.ARQ_STATE == 'CONNECTED':
2021-02-28 14:24:14 +00:00
# here we could disconnect
pass
2021-02-24 16:41:14 +00:00
if static.TNC_STATE == 'IDLE':
# here we send an "CONNECT FRAME
#ARQ_CONNECT_THREAD = threading.Thread(target=data_handler.arq_connect, name="ARQ_CONNECT")
#ARQ_CONNECT_THREAD.start()
asyncio.run(data_handler.arq_connect())
self.request.sendall(bytes("CONNECTING", encoding))
#data_handler.arq_connect()
# ARQ DISCONNECT FROM CALLSIGN ----------------------------------------
if data == 'ARQ:DISCONNECT':
#ARQ_DISCONNECT_THREAD = threading.Thread(target=data_handler.arq_disconnect, name="ARQ_DISCONNECT")
#ARQ_DISCONNECT_THREAD.start()
asyncio.run(data_handler.arq_disconnect())
self.request.sendall(bytes("DISCONNECTING", encoding))
#data_handler.arq_disconnect()
2021-02-16 18:39:08 +00:00
# TRANSMIT ARQ MESSAGE ------------------------------------------
# wen need to change the TNC_STATE to "CONNECTE" and need to make sure we have a valid callsign and callsign crc8 of the DX station
#print(static.ARQ_STATE)
2021-02-28 14:24:14 +00:00
if data.startswith('ARQ:DATA') and static.ARQ_STATE == 'CONNECTED':
static.ARQ_READY_FOR_DATA = False
2021-02-16 18:39:08 +00:00
logging.info("CMD | NEW ARQ DATA")
self.request.sendall(b'SENDIN ARQ DATA')
2021-02-28 14:24:14 +00:00
asyncio.run(data_handler.arq_open_data_channel())
#data_handler.arq_open_data_channel()
2021-02-28 14:24:14 +00:00
#wait until we set the data mode
# here we need a timeout as well!!!
while static.ARQ_READY_FOR_DATA == False:
time.sleep(0.01)
if static.ARQ_READY_FOR_DATA == True:
2021-02-28 16:28:07 +00:00
#logging.info("CMD | SENDING ARQ DATA")
2021-02-28 14:24:14 +00:00
static.TNC_STATE = 'BUSY'
arqdata = data.split('ARQ:')
data_out = bytes(arqdata[1], 'utf-8')
2021-02-16 13:36:01 +00:00
asyncio.run(data_handler.arq_transmit(data_out))
#data_handler.arq_transmit(data_out)
#TRANSMIT_ARQ = threading.Thread(target=data_handler.transmit, args=[data_out], name="TRANSMIT_ARQ")
#TRANSMIT_ARQ.start()
2021-02-16 19:49:02 +00:00
# SETTINGS AND STATUS ---------------------------------------------
2021-02-16 19:49:02 +00:00
if data.startswith('SET:MYCALLSIGN:'):
2021-02-28 14:24:14 +00:00
callsign = data.split('SET:MYCALLSIGN:')
if bytes(callsign[1], encoding) == b'':
2021-02-18 14:11:37 +00:00
self.request.sendall(b'INVALID CALLSIGN')
else:
2021-02-28 14:24:14 +00:00
static.MYCALLSIGN = bytes(callsign[1], encoding)
2021-02-18 14:11:37 +00:00
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
self.request.sendall(static.MYCALLSIGN)
logging.info("CMD | MYCALLSIGN: " + str(static.MYCALLSIGN))
2021-02-28 14:24:14 +00:00
2021-02-16 19:49:02 +00:00
if data == 'GET:MYCALLSIGN':
self.request.sendall(bytes(static.MYCALLSIGN, encoding))
if data == 'GET:MYCALLSIGN_CRC8':
self.request.sendall(bytes(static.MYCALLSIGN_CRC8, encoding))
if data == 'GET:DXCALLSIGN':
self.request.sendall(bytes(static.DXCALLSIGN, encoding))
2021-02-19 08:58:12 +00:00
if data == 'GET:TNC_STATE':
self.request.sendall(static.TNC_STATE)
2021-02-16 19:49:02 +00:00
2021-02-19 08:58:12 +00:00
if data == 'GET:PTT_STATE':
self.request.sendall(bytes(str(static.PTT_STATE), encoding))
2021-02-16 19:49:02 +00:00
# ARQ
if data == 'GET:ARQ_STATE':
self.request.sendall(bytes(static.ARQ_STATE, encoding))
if data == 'GET:TX_N_MAX_RETRIES':
self.request.sendall(bytes([static.TX_N_MAX_RETRIES], encoding))
if data == 'GET:TX_N_RETRIES':
self.request.sendall(bytes([static.TX_N_RETRIES], encoding))
if data == 'GET:ARQ_TX_N_FRAMES_PER_BURST':
self.request.sendall(bytes([static.ARQ_TX_N_FRAMES_PER_BURST], encoding))
if data == 'GET:ARQ_TX_N_BURSTS':
self.request.sendall(bytes([static.ARQ_TX_N_BURSTS], encoding))
if data == 'GET:ARQ_TX_N_CURRENT_ARQ_FRAME':
self.request.sendall(bytes([static.ARQ_TX_N_CURRENT_ARQ_FRAME], encoding))
if data == 'GET:ARQ_TX_N_TOTAL_ARQ_FRAMES':
self.request.sendall(bytes([static.ARQ_TX_N_TOTAL_ARQ_FRAMES], encoding))
if data == 'GET:ARQ_RX_FRAME_N_BURSTS':
self.request.sendall(bytes([static.ARQ_RX_FRAME_N_BURSTS], encoding))
if data == 'GET:ARQ_RX_N_CURRENT_ARQ_FRAME':
self.request.sendall(bytes([static.ARQ_RX_N_CURRENT_ARQ_FRAME], encoding))
if data == 'GET:ARQ_N_ARQ_FRAMES_PER_DATA_FRAME':
self.request.sendall(bytes([static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME], encoding))
if data == 'GET:RX_BUFFER_LENGTH':
self.request.sendall(bytes(str(len(static.RX_BUFFER)),encoding))
2021-02-16 13:23:57 +00:00
2021-02-16 19:49:02 +00:00
if data.startswith('GET:RX_BUFFER:'):
data = data.split('GET:RX_BUFFER:')
bufferposition = int(data[1])-1
2021-02-16 21:41:06 +00:00
if bufferposition == -1:
2021-02-16 19:49:02 +00:00
if len(static.RX_BUFFER) > 0:
self.request.sendall(static.RX_BUFFER[-1])
if bufferposition <= len(static.RX_BUFFER) > 0:
self.request.sendall(bytes(static.RX_BUFFER[bufferposition]))
2021-02-18 14:11:37 +00:00
if data == 'DEL:RX_BUFFER':
static.RX_BUFFER = []
2021-02-16 13:36:01 +00:00
def start_cmd_socket():
try:
logging.info("SRV | STARTING TCP/IP SOCKET FOR CMD ON PORT: " + str(static.PORT))
socketserver.TCPServer.allow_reuse_address = True #https://stackoverflow.com/a/16641793
cmdserver = socketserver.TCPServer((static.HOST, static.PORT), CMDTCPRequestHandler)
cmdserver.serve_forever()
finally:
cmdserver.server_close()