smaller PEP8 cleanup

pull/26/head
DJ2LS 2021-03-12 14:14:36 +01:00 committed by GitHub
parent 0dccb0a3d4
commit 36b7ac91f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 939 additions and 959 deletions

View File

@ -6,6 +6,7 @@ Created on Sun Dec 27 20:43:40 2020
@author: DJ2LS
"""
import logging
import threading
import time
@ -15,10 +16,8 @@ import sys
import static
import modem
modem = modem.RF()
import helpers
modem = modem.RF()
# ############################################################################################################
@ -66,7 +65,6 @@ def arq_data_received(data_in):
static.ARQ_FRAME_BOF_RECEIVED = False
static.ARQ_FRAME_EOF_RECEIVED = False
try:
static.ARQ_RX_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
@ -101,7 +99,6 @@ def arq_data_received(data_in):
# TRANSMIT_ARQ_ACK_THREAD.start()
# while static.ARQ_STATE == 'SENDING_ACK':
# pass
# clear burst buffer
static.ARQ_RX_BURST_BUFFER = []
@ -143,7 +140,7 @@ def arq_data_received(data_in):
# --------------- IF LIST NOT CONTAINS "None" stick everything together
complete_data_frame = bytearray()
# print("static.ARQ_RX_FRAME_BUFFER.count(None)" + str(static.ARQ_RX_FRAME_BUFFER.count(None)))
if static.ARQ_RX_FRAME_BUFFER.count(None) == 1: ## 1 because position 0 of list will alaways be None in our case
if static.ARQ_RX_FRAME_BUFFER.count(None) == 1: # 1 because position 0 of list will alaways be None in our case
logging.debug("DECODING FRAME!")
for frame in range(1, len(static.ARQ_RX_FRAME_BUFFER)):
raw_arq_frame = static.ARQ_RX_FRAME_BUFFER[frame]
@ -290,7 +287,6 @@ def arq_transmit(data_out):
if static.ARQ_N_SENT_FRAMES + 1 <= static.TX_BUFFER_SIZE:
logging.log(24, "ARQ | TX | M:" + str(static.ARQ_DATA_CHANNEL_MODE) + " | F:[" + str(static.ARQ_N_SENT_FRAMES + 1) + "-" + str(static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST) + "] | T:[" + str(static.ARQ_N_SENT_FRAMES) + "/" + str(static.TX_BUFFER_SIZE) + "] [" + str(int(static.ARQ_N_SENT_FRAMES / (static.TX_BUFFER_SIZE) * 100)).zfill(3) + "%] | A:[" + str(static.TX_N_RETRIES + 1) + "/" + str(static.TX_N_MAX_RETRIES) + "] [BER." + str(static.BER) + "]")
# lets start a thread to transmit nonblocking
# TRANSMIT_ARQ_BURST_THREAD = threading.Thread(target=modem.transmit_arq_burst, name="TRANSMIT_ARQ_BURST")
# TRANSMIT_ARQ_BURST_THREAD.start()
@ -316,7 +312,6 @@ def arq_transmit(data_out):
# async with trio.open_nursery() as nursery:
# nursery.start_soon(helpers.set_after_timeout())
# print("TIMEOUT glaube gestartet...")
# print(task)
# print(static.ARQ_RX_ACK_TIMEOUT)
@ -403,7 +398,6 @@ def arq_transmit(data_out):
logging.debug("------------------------------->NO RULE MATCHED!")
break
# --------------------------------WAITING AREA FOR FRAME ACKs
logging.debug("static.ARQ_N_SENT_FRAMES " + str(static.ARQ_N_SENT_FRAMES))
@ -428,7 +422,6 @@ def arq_transmit(data_out):
# print(static.ARQ_STATE)
logging.debug("WAITING FOR FRAME ACK")
# ----------- if no ACK received and out of retries.....stop frame sending
if static.ARQ_ACK_RECEIVED == False and static.ARQ_FRAME_ACK_RECEIVED == False and static.ARQ_RX_ACK_TIMEOUT == True:
logging.error("ARQ | TX | NO ACK RECEIVED | DATA SHOULD BE RESEND!")
@ -482,17 +475,14 @@ def get_n_frames_per_burst():
return n_frames_per_burst
def burst_ack_received():
static.ARQ_ACK_RECEIVED = True # Force data loops of TNC to stop and continue with next frame
def frame_ack_received():
static.ARQ_FRAME_ACK_RECEIVED = True # Force data loops of TNC to stop and continue with next frame
def burst_rpt_received(data_in):
static.ARQ_RPT_RECEIVED = True
static.ARQ_RPT_FRAMES = []
@ -508,6 +498,7 @@ def burst_rpt_received(data_in):
# ARQ CONNECT HANDLER
# ############################################################################################################
async def arq_connect():
static.ARQ_STATE = 'CONNECTING'
logging.info("CONN [" + str(static.MYCALLSIGN, 'utf-8') + "]-> <-[" + str(static.DXCALLSIGN, 'utf-8') + "] [BER." + str(static.BER) + "]")
@ -526,6 +517,7 @@ async def arq_connect():
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
def arq_received_connect(data_in):
static.ARQ_STATE = 'CONNECTING'
@ -559,6 +551,7 @@ def arq_transmit_keep_alive():
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
def arq_received_connect_keep_alive(data_in):
if static.ARQ_SEND_KEEP_ALIVE == True and (static.ARQ_STATE == 'CONNECTING' or static.ARQ_STATE == 'CONNECTED'):
logging.info("CONN [" + str(static.MYCALLSIGN, 'utf-8') + "] >|< [" + str(static.DXCALLSIGN, 'utf-8') + "] [BER." + str(static.BER) + "]")
@ -604,7 +597,6 @@ async def arq_open_data_channel():
connection_frame[12:13] = bytes([static.ARQ_DATA_CHANNEL_MODE])
# connection_frame[13:14] = bytes([225])
modem.transmit_signalling(connection_frame)
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
@ -632,6 +624,7 @@ def arq_received_data_channel_opener(data_in):
# einen timeout benötigen wir auch noch....
# und ab hier geht es dann in den "RECEIVING_DATA" mode....
def arq_received_channel_is_open(data_in):
static.ARQ_SEND_KEEP_ALIVE == False
@ -672,6 +665,7 @@ async def arq_disconnect():
static.DXCALLSIGN = b''
static.DXCALLSIGN_CRC8 = b''
def arq_disconnect_received(data_in):
static.ARQ_STATE = 'DISCONNECTED'
logging.info("DISC [" + str(static.MYCALLSIGN, 'utf-8') + "]< X >[" + str(static.DXCALLSIGN, 'utf-8') + "] [BER." + str(static.BER) + "]")
@ -681,7 +675,6 @@ def arq_disconnect_received(data_in):
static.DXCALLSIGN_CRC8 = b''
# ############################################################################################################
# PING HANDLER
# ############################################################################################################
@ -697,12 +690,12 @@ async def transmit_ping(callsign):
ping_frame[2:3] = static.MYCALLSIGN_CRC8
ping_frame[3:9] = static.MYCALLSIGN
# wait while sending....
modem.transmit_signalling(ping_frame)
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
def received_ping(data_in):
static.DXCALLSIGN_CRC8 = bytes(data_in[2:3]).rstrip(b'\x00')
@ -720,6 +713,8 @@ def received_ping(data_in):
modem.transmit_signalling(ping_frame)
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
def received_ping_ack(data_in):
static.DXCALLSIGN_CRC8 = bytes(data_in[2:3]).rstrip(b'\x00')
@ -732,6 +727,7 @@ def received_ping_ack(data_in):
# BROADCAST HANDLER
# ############################################################################################################
async def transmit_cq():
logging.info("CQ CQ CQ")
@ -747,12 +743,13 @@ async def transmit_cq():
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
def received_cq(data_in):
static.DXCALLSIGN = b''
static.DXCALLSIGN_CRC8 = b''
logging.info("CQ [" + str(bytes(data_in[3:9]), 'utf-8') + "] [BER."+str(static.BER)+"]")
logging.info("CQ RCVD [" + str(bytes(data_in[3:9]), 'utf-8') + "] [BER." + str(static.BER) + "]")
## here we add the received station to the heard stations buffer
# here we add the received station to the heard stations buffer
dxcallsign = bytes(data_in[3:9]).rstrip(b'\x00')
# check if buffer empty
if len(static.HEARD_STATIONS) == 0:
@ -780,3 +777,26 @@ async def transmit_beacon():
modem.transmit_signalling(beacon_frame)
while static.CHANNEL_STATE == 'SENDING_SIGNALLING':
time.sleep(0.01)
def received_beacon():
static.DXCALLSIGN = b''
static.DXCALLSIGN_CRC8 = b''
logging.info("BEACON RCVD [" + str(bytes(data_in[3:9]), 'utf-8') + "] [BER." + str(static.BER) + "]")
# here we add the received station to the heard stations buffer
dxcallsign = bytes(data_in[3:9]).rstrip(b'\x00')
# check if buffer empty
if len(static.HEARD_STATIONS) == 0:
static.HEARD_STATIONS.append([dxcallsign, int(time.time())])
# if not, we search and update
else:
for i in range(0, len(static.HEARD_STATIONS)):
# update callsign with new timestamp
if static.HEARD_STATIONS[i].count(dxcallsign) > 0:
static.HEARD_STATIONS[i] = [dxcallsign, int(time.time())]
break
# insert if nothing found
if i == len(static.HEARD_STATIONS) - 1:
static.HEARD_STATIONS.append([dxcallsign, int(time.time())])
break

View File

@ -7,32 +7,28 @@ Created on Fri Dec 25 21:25:14 2020
"""
import time
import threading
import logging
import crcengine
import pyaudio
import asyncio
import crcengine
import data_handler
import static
def get_crc_8(data):
crc_algorithm = crcengine.new('crc8-ccitt') # load crc8 library
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(1, byteorder='big')
return crc_data
def get_crc_16(data):
crc_algorithm = crcengine.new('crc16-ccitt-false') # load crc16 library
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(2, byteorder='big')
return crc_data
async def set_after_timeout():
while True:
logging.info("HALLO?!?")
@ -44,7 +40,6 @@ async def set_after_timeout():
#vars()[variable] = value
def arq_disconnect_timeout():
static.ARQ_WAIT_FOR_DISCONNECT = True
logging.debug("ARQ_WAIT_FOR_DISCONNECT")
@ -55,26 +50,31 @@ def arq_ack_timeout():
static.ARQ_RX_ACK_TIMEOUT = True
logging.debug("ARQ_RX_ACK_TIMEOUT")
def arq_rpt_timeout():
if static.CHANNEL_STATE == 'RECEIVING_SIGNALLING':
static.ARQ_RX_RPT_TIMEOUT = True
logging.debug("ARQ_RX_RPT_TIMEOUT")
def arq_frame_timeout():
if static.CHANNEL_STATE == 'RECEIVING_SIGNALLING':
static.ARQ_RX_FRAME_TIMEOUT = True
logging.debug("ARQ_RX_FRAME_TIMEOUT")
def arq_reset_timeout(state):
static.ARQ_RX_ACK_TIMEOUT = state
static.ARQ_RX_FRAME_TIMEOUT = state
static.ARQ_RX_RPT_TIMEOUT = state
def arq_reset_ack(state):
static.ARQ_ACK_RECEIVED = state
static.ARQ_RPT_RECEIVED = state
static.ARQ_FRAME_ACK_RECEIVED = state
def arq_reset_frame_machine():
arq_reset_timeout(False)
arq_reset_ack(False)
@ -90,7 +90,6 @@ def arq_reset_frame_machine():
static.ARQ_FRAME_BOF_RECEIVED = False
static.ARQ_FRAME_EOF_RECEIVED = False
static.TNC_STATE = 'IDLE'
static.ARQ_SEND_KEEP_ALIVE = True
static.CHANNEL_STATE = 'RECEIVING_SIGNALLING'
@ -101,6 +100,8 @@ def arq_reset_frame_machine():
# acktimer.start()
# await asyncio.sleep(2)
# modem.transmit_arq_connect()
def setup_logging():
logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)s:\t%(message)s', datefmt='%H:%M:%S', level=logging.INFO)
@ -115,11 +116,9 @@ def setup_logging():
logging.addLevelName(25, "\033[1;32m%s\033[1;0m" % "SUCCESS")
logging.addLevelName(24, "\033[1;34m%s\033[1;0m" % "DATA")
# https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
# 'DEBUG' : 37, # white
# 'INFO' : 36, # cyan
# 'WARNING' : 33, # yellow
# 'ERROR' : 31, # red
# 'CRITICAL': 41, # white on red bg

View File

@ -24,7 +24,6 @@ import data_handler
import Hamlib
class RF():
def __init__(self):
@ -55,7 +54,6 @@ class RF():
output_device_index=static.AUDIO_OUTPUT_DEVICE, # static.AUDIO_OUTPUT_DEVICE
)
self.streambuffer = bytes(0)
self.audio_writing_to_stream = False
# --------------------------------------------START DECODER THREAD
@ -74,7 +72,6 @@ class RF():
FREEDV_PLAYBACK_THREAD = threading.Thread(target=self.play_audio, name="FREEDV_DECODER_THREAD_14")
FREEDV_PLAYBACK_THREAD.start()
# --------------------------------------------CONFIGURE HAMLIB
Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE)
@ -84,8 +81,6 @@ class RF():
self.my_rig.set_conf("retry", "5")
self.my_rig.open()
if static.HAMLIB_PTT_TYPE == 'RIG_PTT_RIG':
self.hamlib_ptt_type = Hamlib.RIG_PTT_RIG
elif static.HAMLIB_PTT_TYPE == 'RIG_PTT_SERIAL_DTR':
@ -120,6 +115,7 @@ class RF():
#static.CHANNEL_STATE = state_before_transmit
self.audio_writing_to_stream = False
# --------------------------------------------------------------------------------------------------------
def transmit_signalling(self, data_out):
state_before_transmit = static.CHANNEL_STATE
@ -176,6 +172,7 @@ class RF():
# time.sleep(0.5)
# --------------------------------------------------------------------------------------------------------
# GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT
def transmit_arq_burst(self):
self.my_rig.set_ptt(self.hamlib_ptt_type, 1)
@ -198,7 +195,7 @@ class RF():
mod_out_preamble = ctypes.c_short * n_tx_preamble_modem_samples # *2 #1760 for mode 10,11,12 #4000 for mode 9
mod_out_preamble = mod_out_preamble()
self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble);
self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
txbuffer = bytearray()
txbuffer += bytes(mod_out_preamble)
@ -276,10 +273,8 @@ class RF():
self.c_lib.freedv_rawdatatx(freedv, mod_out, data) # modulate DATA and safe it into mod_out pointer
txbuffer += bytes(mod_out)
# -------------- transmit audio
# self.stream_tx.write(bytes(txbuffer))
self.streambuffer = bytes()
self.streambuffer = bytes(txbuffer)
@ -309,18 +304,16 @@ class RF():
static.FREEDV_SIGNALLING_BYTES_PER_FRAME = bytes_per_frame
static.FREEDV_SIGNALLING_PAYLOAD_PER_FRAME = bytes_per_frame - 2
self.c_lib.freedv_set_frames_per_burst(freedv, 1);
self.c_lib.freedv_set_frames_per_burst(freedv, 1)
elif mode == static.ARQ_DATA_CHANNEL_MODE:
static.FREEDV_DATA_BYTES_PER_FRAME = bytes_per_frame
static.FREEDV_DATA_PAYLOAD_PER_FRAME = bytes_per_frame - 2
self.c_lib.freedv_set_frames_per_burst(freedv, 0);
self.c_lib.freedv_set_frames_per_burst(freedv, 0)
else:
pass
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
bytes_out = bytes_out() # get pointer to bytes_out
@ -464,6 +457,7 @@ class RF():
# ARQ CONNECT ACK / KEEP ALIVE
elif frametype == 230:
logging.debug("BEACON RECEIVED")
data_handler.received_beacon(bytes_out[:-2])
else:
logging.info("OTHER FRAME: " + str(bytes_out[:-2]))
@ -474,7 +468,6 @@ class RF():
logging.debug("LAST FRAME ---> UNSYNC")
self.c_lib.freedv_set_sync(freedv, 0) # FORCE UNSYNC
# clear bytes_out buffer to be ready for next frames after successfull decoding
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
@ -490,7 +483,6 @@ class RF():
pass
# print(bytes_out[:-2])
def calculate_ber(self, freedv):
Tbits = self.c_lib.freedv_get_total_bits(freedv)
Terrs = self.c_lib.freedv_get_total_bit_errors(freedv)
@ -500,4 +492,3 @@ class RF():
self.c_lib.freedv_set_total_bit_errors(freedv, 0)
self.c_lib.freedv_set_total_bits(freedv, 0)

26
sock.py
View File

@ -9,7 +9,6 @@ Created on Fri Dec 25 21:25:14 2020
import socketserver
import threading
import logging
import time
import json
import asyncio
@ -20,10 +19,6 @@ import helpers
import asyncbg
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
@ -52,7 +47,6 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
# asyncio.run(asyncbg.call(data_handler.transmit_cq))
#######self.request.sendall(b'CALLING CQ')
# PING ----------------------------------------------------------
if data.startswith('PING:'):
# send ping frame and wait for ACK
@ -60,8 +54,8 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
dxcallsign = pingcommand[1]
# data_handler.transmit_ping(dxcallsign)
##loop = asyncio.get_event_loop()
##loop.create_task(data_handler.transmit_ping(dxcallsign))
##loop.run()
# loop.create_task(data_handler.transmit_ping(dxcallsign))
# loop.run()
# asyncio.new_event_loop()
# asyncio.ensure_future(data_handler.transmit_ping(dxcallsign))
@ -100,11 +94,6 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
########self.request.sendall(bytes("DISCONNECTING", encoding))
# data_handler.arq_disconnect()
if data.startswith('ARQ:OPEN_DATA_CHANNEL') and static.ARQ_STATE == 'CONNECTED':
static.ARQ_READY_FOR_DATA = False
static.TNC_STATE = 'BUSY'
@ -128,13 +117,12 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
else:
static.MYCALLSIGN = bytes(callsign[1], encoding)
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
########self.request.sendall(static.MYCALLSIGN)
# self.request.sendall(static.MYCALLSIGN)
logging.info("CMD | MYCALLSIGN: " + str(static.MYCALLSIGN))
if data == 'GET:MYCALLSIGN':
self.request.sendall(bytes(static.MYCALLSIGN, encoding))
if data == 'GET:DXCALLSIGN':
self.request.sendall(bytes(static.DXCALLSIGN, encoding))
@ -173,11 +161,6 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
jsondata = json.dumps(output)
self.request.sendall(bytes(jsondata, encoding))
if data.startswith('GET:RX_BUFFER:'):
data = data.split('GET:RX_BUFFER:')
bufferposition = int(data[1]) - 1
@ -188,15 +171,12 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
if bufferposition <= len(static.RX_BUFFER) > 0:
self.request.sendall(bytes(static.RX_BUFFER[bufferposition]))
if data == 'DEL:RX_BUFFER':
static.RX_BUFFER = []
# self.request.close()
def start_cmd_socket():
try:

View File

@ -15,11 +15,6 @@ DXCALLSIGN_CRC8 = b'A'
MYGRID = b''
# ---------------------------------
# Server Defaults
@ -54,10 +49,6 @@ HAMLIB_PTT_TYPE = 'RIG_PTT_NONE'
PTT_STATE = False
# -------------------------
# FreeDV Defaults
FREEDV_RECEIVE = True
@ -99,12 +90,12 @@ ARQ_RX_BURST_BUFFER = []
ARQ_RX_FRAME_BUFFER = []
ARQ_RX_FRAME_N_BURSTS = 0
## TX
# TX
ARQ_TX_N_CURRENT_ARQ_FRAME = 0
ARQ_TX_N_TOTAL_ARQ_FRAMES = 0
##
## RX
# RX
ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0 # total number of arq frames per data frame
ARQ_RX_N_CURRENT_ARQ_FRAME = 0
##
@ -177,4 +168,3 @@ RX_BUFFER_SIZE = 0
# ------- HEARD STATIOS BUFFER
HEARD_STATIONS = []