Merge pull request #24 from DJ2LS/ARQv2

ARQv2
This commit is contained in:
DJ2LS 2021-02-16 16:49:06 +01:00 committed by GitHub
commit f789925b50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 588 additions and 391 deletions

440
arq.py
View file

@ -13,35 +13,18 @@ from random import randrange
import static
import modem
import helpers
import main
modem = modem.RF()
import helpers
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 3 #6?!
static.ARQ_ACK_PAYLOAD_PER_FRAME = 14 - 2#
def arq_ack_timeout():
static.ARQ_ACK_TIMEOUT = 1
def data_received(data_in):
# arqframe = frame_type + \ # 1 [:1] # frame type and current number of arq frame of (current) burst
# bytes([static.ARQ_TX_N_FRAMES_PER_BURST]) + \ # 1 [1:2] # total number of arq frames per (current) burst
# static.ARQ_N_CURRENT_ARQ_FRAME + \ # 2 [2:4] # current arq frame number
# static.ARQ_N_TOTAL_ARQ_FRAMES + \ # 2 [4:6] # total number arq frames
# static.ARQ_BURST_PAYLOAD_CRC + \ # 2 [6:8] # arq crc
# payload_data # N [8:N] # payload data
static.ARQ_N_FRAME = int.from_bytes(bytes(data_in[:1]), "big") - 10 #get number of burst frame
static.ARQ_N_FRAME = int.from_bytes(bytes(data_in[:1]), "big") - 10 #get number of burst frame
static.ARQ_N_RX_FRAMES_PER_BURSTS = int.from_bytes(bytes(data_in[1:2]), "big") #get number of bursts from received frame
static.ARQ_RX_N_CURRENT_ARQ_FRAME = int.from_bytes(bytes(data_in[2:4]), "big") #get current number of total frames
static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = int.from_bytes(bytes(data_in[4:6]), "big") # get get total number of frames
static.ARQ_BURST_PAYLOAD_CRC = data_in[6:8]
logging.debug("----------------------------------------------------------------")
@ -49,90 +32,96 @@ def data_received(data_in):
logging.debug("ARQ_N_RX_FRAMES_PER_BURSTS: " + str(static.ARQ_N_RX_FRAMES_PER_BURSTS))
logging.debug("ARQ_RX_N_CURRENT_ARQ_FRAME: " + str(static.ARQ_RX_N_CURRENT_ARQ_FRAME))
logging.debug("ARQ_N_ARQ_FRAMES_PER_DATA_FRAME: " + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME))
logging.debug("static.ARQ_BURST_PAYLOAD_CRC: " + str(static.ARQ_BURST_PAYLOAD_CRC))
logging.debug("----------------------------------------------------------------")
arq_percent_burst = int((static.ARQ_N_FRAME / static.ARQ_N_RX_FRAMES_PER_BURSTS)*100)
arq_percent_frame = int(((static.ARQ_RX_N_CURRENT_ARQ_FRAME)/static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME)*100)
logging.info("ARQ | RX | ARQ FRAME [" + str(static.ARQ_N_FRAME) + "/" + str(static.ARQ_N_RX_FRAMES_PER_BURSTS) + "] [" + str(arq_percent_burst).zfill(3) + "%] --- TOTAL [" + str(static.ARQ_RX_N_CURRENT_ARQ_FRAME) + "/" + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME) + "] [" + str(arq_percent_frame).zfill(3) + "%]" )
logging.log(24, "ARQ | RX | ARQ FRAME [" + str(static.ARQ_N_FRAME) + "/" + str(static.ARQ_N_RX_FRAMES_PER_BURSTS) + "] [" + str(arq_percent_burst).zfill(3) + "%] --- TOTAL [" + str(static.ARQ_RX_N_CURRENT_ARQ_FRAME) + "/" + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME) + "] [" + str(arq_percent_frame).zfill(3) + "%]" )
#allocate ARQ_RX_FRAME_BUFFER as a list with "None" if not already done. This should be done only once per burst!
# here we will save the N frame of a data frame to N list position so we can explicit search for it
if static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME != len(static.ARQ_RX_FRAME_BUFFER) and static.ARQ_RX_N_CURRENT_ARQ_FRAME == 1:
# delete frame buffer if first frame to make sure the buffer is cleared and no junks of a old frame is remaining
if static.ARQ_RX_N_CURRENT_ARQ_FRAME == 1:
static.ARQ_RX_FRAME_BUFFER = []
try:
static.ARQ_RX_FRAME_BUFFER[static.ARQ_RX_N_CURRENT_ARQ_FRAME] = bytes(data_in)
except IndexError:
static.ARQ_RX_FRAME_BUFFER = []
for i in range(0,static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME+1):
static.ARQ_RX_FRAME_BUFFER.insert(i,None)
static.ARQ_RX_FRAME_BUFFER[static.ARQ_RX_N_CURRENT_ARQ_FRAME] = bytes(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)
#allocate ARQ_RX_BURST_BUFFER as a list with "None" if not already done. This should be done only once per burst!
# here we will save the N frame of a burst to N list position so we can explicit search for it
if static.ARQ_N_RX_FRAMES_PER_BURSTS != len(static.ARQ_RX_BURST_BUFFER) and static.ARQ_N_FRAME == 1:
except IndexError:
static.ARQ_RX_BURST_BUFFER = []
for i in range(0,static.ARQ_N_RX_FRAMES_PER_BURSTS+1):
static.ARQ_RX_BURST_BUFFER.insert(i,None)
# now we add the incoming data to the specified position in our lists
static.ARQ_RX_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
static.ARQ_RX_FRAME_BUFFER[static.ARQ_RX_N_CURRENT_ARQ_FRAME] = bytes(data_in)
# -------------------------- ARQ BURST CHECKER
# run only if we recieved all ARQ FRAMES per ARQ BURST
burst_total_payload = bytearray()
if static.ARQ_N_FRAME == static.ARQ_N_RX_FRAMES_PER_BURSTS: #if received bursts are equal to burst number in frame
#here we get the total payload for the frame to calculate the crc
for n_raw_frame in range(1,len(static.ARQ_RX_BURST_BUFFER)):
# we need to check if we have a None or received data in list
if static.ARQ_RX_BURST_BUFFER[n_raw_frame] != None:
burst_frame = static.ARQ_RX_BURST_BUFFER[n_raw_frame] #get burst frame
burst_payload = burst_frame[8:] #remove frame type and burst CRC #4
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
# ------------------ calculate CRC of BURST
burst_payload_crc = helpers.get_crc_16(burst_total_payload)
# IF BURST CRC IS CORRECT, APPEND BURST TO BUFFER AND SEND ACK FRAME
if burst_payload_crc == data_in[6:8]:
#BUILDING ACK FRAME FOR BURST -----------------------------------------------
#ack_payload = bytes(burst_payload_crc)
ack_frame = b'<'+ bytes(burst_payload_crc) # < = 60
ack_buffer = bytearray(static.ARQ_ACK_PAYLOAD_PER_FRAME)
ack_buffer[:len(ack_frame)] = ack_frame # set buffersize to length of data which will be send
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
logging.info("ARQ | TX | ARQ BURST ACK [" + str(data_in[6:8].hex()) +"]")
modem.transmit_arq_ack(ack_buffer)
# ----------------------------------------------------------------
static.ARQ_RX_BURST_BUFFER = [] # CLEAR RX BURST BUFFER AFTER SENDING DATA
else: #IF burst payload crc and input crc are NOT equal
logging.info("ARQ BURST CRC NOT EQUAL! [" + str(data_in[6:8]) + "]")
static.ARQ_RX_BURST_BUFFER = [] #erase ARQ RX Burst buffer
static.ARQ_RX_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
# - ------------------------- ARQ BURST CHECKER
# run only if we recieved all ARQ FRAMES per ARQ BURST
if static.ARQ_RX_BURST_BUFFER.count(None) == 1: #count nones
logging.info("ARQ | TX | BURST ACK")
#BUILDING ACK FRAME FOR BURST -----------------------------------------------
ack_payload = b'BURST_ACK'
ack_frame = b'<' + ack_payload # < = 60
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
modem.transmit_arq_ack(ack_frame)
#clear burst buffer
static.ARQ_RX_BURST_BUFFER = []
#if nframes are unequal to expected frames per burst
else:
#just a placeholder at this time
pass
#if decoded N frames are unequal to expected frames per burst
elif static.ARQ_N_FRAME == static.ARQ_N_RX_FRAMES_PER_BURSTS and static.ARQ_RX_BURST_BUFFER.count(None) != 1:
# --------------- CHECK WHICH BURST FRAMES WE ARE MISSING -------------------------------------------
missing_frames = b''
for burstnumber in range(1,len(static.ARQ_RX_BURST_BUFFER)):
if static.ARQ_RX_BURST_BUFFER[burstnumber] == None:
#frame_number = static.ARQ_RX_N_CURRENT_ARQ_FRAME - static.ARQ_N_RX_FRAMES_PER_BURSTS + burstnumber
#logging.debug("frame_number" + str(frame_number))
logging.debug("static.ARQ_RX_N_CURRENT_ARQ_FRAME" + str(static.ARQ_RX_N_CURRENT_ARQ_FRAME))
logging.debug("ARQ_N_RX_FRAMES_PER_BURSTS" + str(static.ARQ_N_RX_FRAMES_PER_BURSTS))
frame_number = burstnumber
frame_number = frame_number.to_bytes(2, byteorder='big')
missing_frames += frame_number
logging.warning("ARQ | TX | RPT ARQ FRAMES [" + str(missing_frames) + "]")
#BUILDING RPT FRAME FOR BURST -----------------------------------------------
rpt_payload = missing_frames
rpt_frame = b'>' + rpt_payload #> = 63
#TRANSMIT RPT FRAME FOR BURST-----------------------------------------------
modem.transmit_arq_ack(rpt_frame)
# ---------------------------- FRAME MACHINE
# --------------- CHECK IF WE ARE MISSING FRAMES -------------------------------------------
#for frame in range(1,len(static.ARQ_RX_FRAME_BUFFER)):
# if static.ARQ_RX_FRAME_BUFFER[frame] == None:
# print("Missing frames:" + str(frame))
# --------------- 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
#print("DECODING FRAME!")
for frame in range(1,len(static.ARQ_RX_FRAME_BUFFER)):
raw_arq_frame = static.ARQ_RX_FRAME_BUFFER[frame]
arq_frame_payload = raw_arq_frame[8:]
@ -153,10 +142,9 @@ def data_received(data_in):
arq_frame_payload = arq_frame_payload[0]
# --------- AFTER WE SEPARATED BOF AND EOF, STICK EVERYTHING TOGETHER
# --------- AFTER WE SEPARATED BOF AND EOF, STICK EVERYTHING TOGETHER
complete_data_frame = complete_data_frame + arq_frame_payload
#check if Begin of Frame BOF and End of Frame EOF are received, then start calculating CRC and sticking everything together
if static.ARQ_FRAME_BOF_RECEIVED == True and static.ARQ_FRAME_EOF_RECEIVED == True:
@ -165,20 +153,20 @@ def data_received(data_in):
#IF THE FRAME PAYLOAD CRC IS EQUAL TO THE FRAME CRC WHICH IS KNOWN FROM THE HEADER --> SUCCESS
if frame_payload_crc == static.FRAME_CRC:
logging.info("ARQ | RX | DATA FRAME SUCESSFULLY RECEIVED! - TIME TO PARTY")
#logging.info("ARQ | RX | DATA FRAME SUCESSFULLY RECEIVED! - TIME TO PARTY")
logging.log(25,"ARQ | RX | DATA FRAME SUCESSFULLY RECEIVED! - TIME TO PARTY")
#append received frame to RX_BUFFER
static.RX_BUFFER.append(complete_data_frame)
#BUILDING ACK FRAME FOR DATA FRAME -----------------------------------------------
ack_frame = b'='+ bytes(static.FRAME_CRC) # < = 61
ack_buffer = bytearray(static.ARQ_ACK_PAYLOAD_PER_FRAME)
ack_buffer[:len(ack_frame)] = ack_frame # set buffersize to length of data which will be send
ack_payload = b'FRAME_ACK'
ack_frame = b'='+ ack_payload + bytes(static.FRAME_CRC) # < = 61
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
time.sleep(1) #0.5
logging.info("ARQ | TX | ARQ DATA FRAME ACK [" + str(static.FRAME_CRC.hex()) +"]")
modem.transmit_arq_ack(ack_buffer)
modem.transmit_arq_ack(ack_frame)
# clearing buffers and resetting counters
static.ARQ_RX_BURST_BUFFER = []
@ -188,51 +176,32 @@ def data_received(data_in):
static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0
#print("----------------------------------------------------------------")
#print(static.RX_BUFFER[-1])
#print(static.RX_BUFFER[-1])
#print("----------------------------------------------------------------")
else:
logging.info("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!")
def burst_ack_received():
logging.debug("ARQ | RX | BURST ACK RCVD!")
static.ARQ_ACK_TIMEOUT = 1 #Force timer to stop waiting
static.ARQ_ACK_RECEIVED = 1 #Force data loops of TNC to stop and continue with next frame
def frame_ack_received():
logging.debug("ARQ | RX | FRAME ACK RCVD!")
static.ARQ_ACK_TIMEOUT = 1 #Force timer to stop waiting
static.ARQ_FRAME_ACK_RECEIVED = 1 #Force data loops of TNC to stop and continue with next frame
logging.error("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!")
def transmit(data_out):
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 8 #3 ohne ARQ_TX_N_FRAMES_PER_BURST
frame_header_length = 4
n_arq_frames_per_data_frame = (len(data_out)+frame_header_length) // static.ARQ_PAYLOAD_PER_FRAME + ((len(data_out)+frame_header_length) % static.ARQ_PAYLOAD_PER_FRAME > 0) # aufrunden 3.2 = 4
#print(static.FREEDV_DATA_PAYLOAD_PER_FRAME)
#print(static.ARQ_PAYLOAD_PER_FRAME)
#print(n_bursts_prediction)
####static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = n_arq_frames_per_data_frame.to_bytes(2, byteorder='big') #65535
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 8
frame_header_length = 6 #4
n_arq_frames_per_data_frame = (len(data_out)+frame_header_length) // static.ARQ_PAYLOAD_PER_FRAME + ((len(data_out)+frame_header_length) % static.ARQ_PAYLOAD_PER_FRAME > 0)
frame_payload_crc = helpers.get_crc_16(data_out)
#print(frame_payload_crc)
# This is the total frame with frame header, which will be send
data_out = frame_payload_crc + static.FRAME_BOF + data_out + static.FRAME_EOF
# 2 2 N 2
# --------------------------------------------- LETS CREATE A BUFFER BY SPLITTING THE FILES INTO PEACES
static.TX_BUFFER = [data_out[i:i+static.ARQ_PAYLOAD_PER_FRAME] for i in range(0, len(data_out), static.ARQ_PAYLOAD_PER_FRAME)]
static.TX_BUFFER_SIZE = len(static.TX_BUFFER)
#print(static.TX_BUFFER)
logging.info("ARQ | TX | DATA FRAME --- BYTES: " + str(len(data_out)) + " ARQ FRAMES: " + str(static.TX_BUFFER_SIZE))
@ -241,120 +210,213 @@ def transmit(data_out):
static.ARQ_N_SENT_FRAMES = 0 # SET N SENT FRAMES TO 0 FOR A NEW SENDING CYCLE
while static.ARQ_N_SENT_FRAMES <= static.TX_BUFFER_SIZE:
#print("static.ARQ_N_SENT_FRAMES: " + str(static.ARQ_N_SENT_FRAMES))
static.ARQ_TX_N_FRAMES_PER_BURST = get_n_frames_per_burst()
# ----------- CREATE FRAME TOTAL PAYLOAD TO BE ABLE TO CREATE CRC FOR IT
burst_total_payload = bytearray()
try: # DETECT IF LAST BURST TO PREVENT INDEX ERROR OF BUFFER
for i in range(static.ARQ_TX_N_FRAMES_PER_BURST): # Loop through TX_BUFFER LIST
# make sure we have always a filled buffer with the length of payload per frame
burst_raw_payload = static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + i]
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
burst_payload[:len(burst_raw_payload)] = burst_raw_payload # get frame from TX_BUFFER
burst_total_payload = burst_total_payload + burst_payload # append single frame to total payload buffer
for i in range(static.ARQ_TX_N_FRAMES_PER_BURST): # Loop through TX_BUFFER LIST
len(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + i]) #we calculate the length to trigger a list index error
except IndexError: # IF LAST BURST DETECTED BUILD CRC WITH LESS FRAMES AND SET static.ARQ_TX_N_FRAMES_PER_BURST TO VALUE OF REST!
burst_total_payload = bytearray() # reset burst_total_payload because of possible input remaining of detecting loop one step above
if static.ARQ_N_SENT_FRAMES == 0 and (static.ARQ_TX_N_FRAMES_PER_BURST > static.TX_BUFFER_SIZE): #WE CANT DO MODULO 0 --> CHECK IF FIRST FRAME == LAST FRAME
static.ARQ_TX_N_FRAMES_PER_BURST = static.TX_BUFFER_SIZE
elif static.ARQ_N_SENT_FRAMES == 1 and (static.ARQ_TX_N_FRAMES_PER_BURST > static.TX_BUFFER_SIZE): # MODULO 1 WILL ALWAYS BE 0 --> THIS FIXES IT
static.ARQ_TX_N_FRAMES_PER_BURST = static.TX_BUFFER_SIZE - static.ARQ_N_SENT_FRAMES
else:
static.ARQ_TX_N_FRAMES_PER_BURST = (static.TX_BUFFER_SIZE % static.ARQ_N_SENT_FRAMES)
#print("ARQ_TX_N_FRAMES_PER_BURST OF LAST BURST: " + str(static.ARQ_TX_N_FRAMES_PER_BURST))
for i in range(static.ARQ_TX_N_FRAMES_PER_BURST): #bytearray(b'111111111111111111111111222222222222222222222222')
# make sure we have always a filled buffer with the length of payload per frame
burst_raw_payload = static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + i]
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
burst_payload[:len(burst_raw_payload)] = burst_raw_payload # get frame from TX_BUFFER
burst_total_payload = burst_total_payload + burst_payload # append single frame to total payload buffer
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES_PER_BURST
static.ARQ_BURST_PAYLOAD_CRC = helpers.get_crc_16(burst_total_payload)
#--------------------------------------------- N ATTEMPTS TO SEND BURSTS IF ACK RECEPTION FAILS
for static.TX_N_RETRIES in range(static.TX_N_MAX_RETRIES):
if static.ARQ_N_SENT_FRAMES+1 <= static.TX_BUFFER_SIZE:
logging.info("ARQ | TX | B:[" + str(static.ARQ_BURST_PAYLOAD_CRC.hex()) + "] 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) + "]")
if static.ARQ_N_SENT_FRAMES + 1 <= static.TX_BUFFER_SIZE:
logging.log(24, "ARQ | TX | 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) + "]")
# 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()
# lets wait during sending. After sending is finished we will continue
while static.ARQ_STATE == 'SENDING_DATA':
time.sleep(0.05)
time.sleep(0.01)
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
#reset timer and ack state
static.ARQ_FRAME_ACK_RECEIVED = 0
static.ARQ_ACK_RECEIVED = 0
static.ARQ_ACK_TIMEOUT = 0
logging.debug("ARQ | RX | WAITING FOR BURST ACK")
static.ARQ_STATE = 'RECEIVING_ACK'
timer = threading.Timer(static.ARQ_ACK_TIMEOUT_SECONDS, arq_ack_timeout)
timer.start()
static.ARQ_STATE = 'RECEIVING_SIGNALLING'
helpers.arq_reset_timeout(False)
helpers.arq_reset_ack(False)
acktimer = threading.Timer(static.ARQ_RX_ACK_TIMEOUT_SECONDS, helpers.arq_ack_timeout)
acktimer.start()
logging.debug(".............................")
logging.debug("static.ARQ_STATE " + str(static.ARQ_STATE))
logging.debug("static.ARQ_FRAME_ACK_RECEIVED " + str(static.ARQ_FRAME_ACK_RECEIVED))
logging.debug("static.ARQ_ACK_RECEIVED " + str(static.ARQ_ACK_RECEIVED))
logging.debug("static.ARQ_RX_ACK_TIMEOUT " + str(static.ARQ_RX_ACK_TIMEOUT))
logging.debug("static.ARQ_RPT_RECEIVED " + str(static.ARQ_RPT_RECEIVED))
logging.debug(".............................")
# --------------------------- WHILE TIMEOUT NOT REACHED AND NO ACK RECEIVED --> LISTEN
while static.ARQ_ACK_TIMEOUT == 0 and static.ARQ_ACK_RECEIVED == 0:
while static.ARQ_ACK_RECEIVED != True and static.ARQ_RPT_RECEIVED != True and static.ARQ_FRAME_ACK_RECEIVED != True and static.ARQ_RX_FRAME_TIMEOUT != True and static.ARQ_RX_ACK_TIMEOUT != True:
time.sleep(0.01) # lets reduce CPU load a little bit
#print(static.ARQ_STATE)
logging.debug(static.ARQ_STATE)
if static.ARQ_RPT_RECEIVED == True:
logging.warning("ARQ | RX | REQUEST FOR REPEATING FRAMES: " + str(static.ARQ_RPT_FRAMES))
logging.warning("ARQ | TX | SENDING REQUESTED FRAMES: " + str(static.ARQ_RPT_FRAMES))
TRANSMIT_ARQ_BURST_THREAD = threading.Thread(target=modem.transmit_arq_burst, name="TRANSMIT_ARQ_BURST")
TRANSMIT_ARQ_BURST_THREAD.start()
# lets wait during sending. After sending is finished we will continue
while static.ARQ_STATE == 'SENDING_DATA':
time.sleep(0.01)
static.ARQ_STATE = 'RECEIVING_SIGNALLING'
helpers.arq_reset_timeout(False)
helpers.arq_reset_ack(False)
rpttimer = threading.Timer(static.ARQ_RX_RPT_TIMEOUT_SECONDS, helpers.arq_rpt_timeout)
rpttimer.start()
while static.ARQ_ACK_RECEIVED == False and static.ARQ_FRAME_ACK_RECEIVED == False and static.ARQ_RX_RPT_TIMEOUT == False:
time.sleep(0.01) # lets reduce CPU load a little bit
logging.debug(static.ARQ_STATE)
if static.ARQ_ACK_RECEIVED == True:
logging.info("ARQ | RX | ACK AFTER RPT")
rpttimer.cancel()
helpers.arq_reset_ack(True)
static.ARQ_RPT_FRAMES = []
if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
#logging.info("ARQ | RX | ACK TIMEOUT | SENDING ARQ BURST AGAIN")
pass
if static.ARQ_RX_RPT_TIMEOUT == True and static.ARQ_ACK_RECEIVED == False:
logging.error("ARQ | Burst lost....")
helpers.arq_reset_ack(False)
static.ARQ_RPT_FRAMES = []
#--------------------------------------------------------------------------------------------------------------
elif static.ARQ_ACK_RECEIVED == 0 and static.ARQ_RX_ACK_TIMEOUT == 1:
logging.warning("ARQ | RX | ACK TIMEOUT!")
pass #no break here so we can continue with the next try of repeating the burst
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED OR FRAME ACK HAS BEEN RECEIVED
if static.ARQ_ACK_RECEIVED == 1:
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED
elif static.ARQ_ACK_RECEIVED == True:
logging.info("ARQ | RX | ACK")
acktimer.cancel()
#-----------IF ACK RECEIVED, INCREMENT ITERATOR FOR MAIN LOOP TO PROCEED WITH NEXT FRAMES/BURST
static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST
break
#--------------- BREAK LOOP IF FRAME ACK HAS BEEN RECEIVED EARLIER AS EXPECTED
if static.ARQ_FRAME_ACK_RECEIVED == 1:
logging.info("ARQ | RX | EARLY FRAME ACK RECEIVED - STOPPING TX")
elif static.ARQ_FRAME_ACK_RECEIVED == True:
logging.info("----------------------------------------------------------")
logging.info("ARQ | RX | EARLY FRAME ACK RECEIVED")
#static.ARQ_N_SENT_FRAMES = #static.TX_BUFFER_SIZE
static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST
break
else:
logging.debug("------------------------------->NO RULE MATCHED!")
break
#--------------------------------WAITING AREA FOR FRAME ACKs
logging.debug("static.ARQ_N_SENT_FRAMES " + str(static.ARQ_N_SENT_FRAMES))
logging.debug("static.TX_BUFFER_SIZE " + str(static.TX_BUFFER_SIZE))
logging.debug("static.TX_N_RETRIES " + str(static.TX_N_RETRIES))
logging.debug("static.TX_N_MAX_RETRIES " + str(static.TX_N_MAX_RETRIES))
logging.debug("static.ARQ_STATE " + str(static.ARQ_STATE))
logging.debug("static.ARQ_FRAME_ACK_RECEIVED " + str(static.ARQ_FRAME_ACK_RECEIVED))
logging.debug("static.ARQ_RX_FRAME_TIMEOUT " + str(static.ARQ_RX_FRAME_TIMEOUT))
logging.debug("static.ARQ_ACK_RECEIVED " + str(static.ARQ_ACK_RECEIVED))
logging.debug("static.ARQ_RX_ACK_TIMEOUT " + str(static.ARQ_RX_ACK_TIMEOUT))
logging.debug("static.ARQ_RPT_RECEIVED " + str(static.ARQ_RPT_RECEIVED))
logging.debug("static.ARQ_TX_N_FRAMES_PER_BURST " + str(static.ARQ_TX_N_FRAMES_PER_BURST))
frametimer = threading.Timer(static.ARQ_RX_FRAME_TIMEOUT_SECONDS, helpers.arq_frame_timeout)
frametimer.start()
static.ARQ_STATE = 'RECEIVING_SIGNALLING'
# wait for frame ACK if we processed the last frame/burst
while static.ARQ_FRAME_ACK_RECEIVED == False and static.ARQ_RX_FRAME_TIMEOUT == False and static.ARQ_N_SENT_FRAMES == static.TX_BUFFER_SIZE:
time.sleep(0.01) # lets reduce CPU load a little bit
#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 == 0 and static.ARQ_FRAME_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
logging.info("ARQ | TX | NO BURST OR FRAME ACK RECEIVED | DATA SHOULD BE RESEND!")
print(bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES]))
break
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 BURST OR FRAME ACK RECEIVED | DATA SHOULD BE RESEND!")
break
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT AND WE GOT A FRAME ACK
if static.ARQ_N_SENT_FRAMES == static.TX_BUFFER_SIZE and static.ARQ_FRAME_ACK_RECEIVED == 1:
logging.info("ARQ | RX | REGULAR FRAME ACK RECEIVED - DATA TRANSMITTED!")
elif static.ARQ_N_SENT_FRAMES == static.TX_BUFFER_SIZE and static.ARQ_FRAME_ACK_RECEIVED == True:
logging.log(25,"ARQ | RX | FRAME ACK RECEIVED - DATA TRANSMITTED! :-)")
break
else:
logging.debug("NO MATCHING RULE AT THE END")
# stop all timers
try:
frametimer.cancel()
except Exception:
pass
try:
acktimer.cancel()
except Exception:
pass
# IF TX BUFFER IS EMPTY / ALL FRAMES HAVE BEEN SENT --> HERE WE COULD ADD AN static.VAR for IDLE STATE
logging.info("ARQ | TX | BUFFER EMPTY")
# - RESET COUNTERS
static.ARQ_N_SENT_FRAMES = 0
static.ARQ_TX_N_FRAMES_PER_BURST = 0
static.ARQ_ACK_RECEIVED = 0
static.ARQ_FRAME_ACK_RECEIVED = 0
helpers.arq_reset_frame_machine()
# BURST MACHINE TO DEFINE N BURSTS PER FRAME ---> LATER WE CAN USE CHANNEL MESSUREMENT TO SET FRAMES PER BURST
def get_n_frames_per_burst():
n_frames_per_burst = randrange(1,10)
#n_frames_per_burst = 1
#n_frames_per_burst = randrange(1,10)
n_frames_per_burst = 2
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 = []
missing_area = bytes(data_in[1:9])
for i in range(0,6,2):
if not missing_area[i:i+2].endswith(b'\x00\x00'):
missing = missing_area[i:i+2]
static.ARQ_RPT_FRAMES.insert(0,missing)

View file

@ -10,11 +10,13 @@ import time
import threading
import logging
import crcengine
import pyaudio
import static
def get_crc_8(data):
crc_algorithm = crcengine.new('crc8-ccitt') #load crc8 library
@ -26,4 +28,70 @@ 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
return crc_data
def arq_ack_timeout():
if static.ARQ_STATE == 'RECEIVING_SIGNALLING':
static.ARQ_RX_ACK_TIMEOUT = True
logging.debug("ARQ_RX_ACK_TIMEOUT")
def arq_rpt_timeout():
if static.ARQ_STATE == 'RECEIVING_SIGNALLING':
static.ARQ_RX_RPT_TIMEOUT = True
logging.debug("ARQ_RX_RPT_TIMEOUT")
def arq_frame_timeout():
if static.ARQ_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)
static.TX_N_RETRIES = 0
static.ARQ_N_SENT_FRAMES = 0
static.ARQ_TX_N_FRAMES_PER_BURST = 0
def setup_logging():
logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)s:\t%(message)s', datefmt='%H:%M:%S', level=logging.INFO)
logging.addLevelName( logging.DEBUG, "\033[1;36m%s\033[1;0m" % logging.getLevelName(logging.DEBUG))
logging.addLevelName( logging.INFO, "\033[1;37m%s\033[1;0m" % logging.getLevelName(logging.INFO))
logging.addLevelName( logging.WARNING, "\033[1;33m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName( logging.ERROR, "\033[1;31m%s\033[1;0m" % "FAILED")
#logging.addLevelName( logging.ERROR, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
logging.addLevelName( logging.CRITICAL, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL))
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
def list_audio_devices():
p = pyaudio.PyAudio()
devices = []
for x in range(0, p.get_device_count()):
devices.append(f"{x} - {p.get_device_info_by_index(x)['name']}")
for line in devices:
print(line)

64
main.py
View file

@ -10,43 +10,59 @@ Created on Tue Dec 22 16:58:45 2020
import socketserver
import argparse
import logging
import threading
#import tnc
import static
import helpers
if __name__ == '__main__':
# config logging
helpers.setup_logging()
# list audio devices
helpers.list_audio_devices()
static.MYCALLSIGN = b'DJ2LS'
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
static.DXCALLSIGN = b'DH3WO'
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
print("MYCALLSIGN " + str(static.MYCALLSIGN))
print("MYCALLSIGN_CRC8 " + str(static.MYCALLSIGN_CRC8))
print("DXCALLSIGN " + str(static.DXCALLSIGN))
print("DXCALLSIGN_CRC8 " + str(static.DXCALLSIGN_CRC8))
#--------------------------------------------GET PARAMETER INPUTS
parser = argparse.ArgumentParser(description='Simons TEST TNC')
parser.add_argument('--rx', dest="audio_input_device", default=0, help="sound card for listening.", type=int)
parser.add_argument('--tx', dest="audio_output_device", default=0, help="sound card for transmitting.", type=int)
parser.add_argument('--port', dest="socket_port", default=3000, help="Set the port, the socket is listening on.", type=int)
parser.add_argument('--mode', dest="freedv_data_mode", default=12, help="Set the mode.", type=int)
args = parser.parse_args()
#--------------------------------------------START CMD & DATA SERVER
static.FREEDV_DATA_MODE = args.freedv_data_mode
static.AUDIO_INPUT_DEVICE = args.audio_input_device
static.AUDIO_OUTPUT_DEVICE = args.audio_output_device
static.PORT = args.socket_port
import tnc # we need to wait until we got all parameters from argparse
import sock # we need to wait until we got all parameters from argparse
cmd_server_thread = threading.Thread(target=sock.start_cmd_socket, name="cmd server")
cmd_server_thread.start()
data_server_thread = threading.Thread(target=sock.start_data_socket, name="data server")
data_server_thread.start()
#-------------------------------------------- DEFINE LOGGER
logger = logging.getLogger()
logger.setLevel("INFO") #DEBUG>INFO>WARNING>ERROR>CRITICAL
#--------------------------------------------START SERVER
logging.info("STARTING TCP/IP SOCKET ON PORT " + str(static.PORT))
try:
socketserver.TCPServer.allow_reuse_address = True #https://stackoverflow.com/a/16641793
server = socketserver.TCPServer((static.HOST, static.PORT), tnc.TCPRequestHandler)
server.serve_forever()
finally:
server.server_close()

263
modem.py
View file

@ -10,7 +10,8 @@ import ctypes
from ctypes import *
import pathlib
import pyaudio
import sys
import audioop
#import sys
import logging
import time
import threading
@ -19,6 +20,13 @@ import helpers
import static
import arq
class RF():
def __init__(self):
@ -51,53 +59,10 @@ class RF():
#--------------------------------------------START DECODER THREAD
FREEDV_DECODER_THREAD = threading.Thread(target=self.receive, args=[static.FREEDV_DATA_MODE,static.FREEDV_SIGNALLING_MODE], name="FREEDV_DECODER_THREAD")
FREEDV_DECODER_THREAD.start()
#--------------------------------------------------------------------------------------------------------
# GET DATA AND MODULATE IT
def transmit(self,mode,data_out):
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
freedv = self.c_lib.freedv_open(mode)
bytes_per_frame = int(self.c_lib.freedv_get_bits_per_modem_frame(freedv)/8)
payload_per_frame = bytes_per_frame -2
n_nom_modem_samples = self.c_lib.freedv_get_n_nom_modem_samples(freedv)
n_tx_modem_samples = self.c_lib.freedv_get_n_tx_modem_samples(freedv)*2 #get n_tx_modem_samples which defines the size of the modulation object
mod_out = ctypes.c_short * n_tx_modem_samples
mod_out = mod_out()
mod_out_preamble = ctypes.c_short * n_tx_modem_samples #1760 for mode 10,11,12 #4000 for mode 9
mod_out_preamble = mod_out_preamble()
data_list = [data_out[i:i+payload_per_frame] for i in range(0, len(data_out), payload_per_frame)] # split incomming bytes to size of 30bytes, create a list and loop through it
data_list_length = len(data_list)
for i in range(data_list_length): # LOOP THROUGH DATA LIST
buffer = bytearray(payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3)
buffer[:len(data_list[i])] = data_list[i] # set buffersize to length of data which will be send
crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), payload_per_frame)) # generate CRC16
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
buffer += crc # append crc16 to buffer
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
# -------------- preamble area
# WE NEED TO ADJUST IT FOR SINGLE TRANSMISSION
txbuffer = bytearray()
txbuffer += bytes(mod_out_preamble)
txbuffer += bytes(mod_out)
txbuffer = txbuffer.rstrip(b'\x00')
# -------------- transmit audio
self.stream_tx.write(bytes(txbuffer))
#--------------------------------------------------------------------------------------------------------
def transmit_arq_ack(self,ack_buffer):
#print(ack_buffer)
static.ARQ_STATE = 'SENDING_ACK'
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
@ -118,7 +83,6 @@ class RF():
crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), payload_per_frame)) # generate CRC16
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
buffer += crc # append crc16 to buffer
#print(bytes(buffer))
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
preamble_bytes = self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
@ -128,16 +92,20 @@ class RF():
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
txbuffer += bytes(mod_out)
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
# -------------- transmit audio
self.stream_tx.write(bytes(txbuffer))
self.stream_tx.write(bytes(txbuffer))
# -------------- transmit audio twice
logging.debug("SEND SIGNALLING FRAME " + str(ack_buffer))
self.stream_tx.write(bytes(txbuffer))
self.stream_tx.write(bytes(txbuffer))
static.ARQ_STATE = 'RECEIVING_DATA'
#--------------------------------------------------------------------------------------------------------
# GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT
def transmit_arq_burst(self):
static.ARQ_STATE = 'SENDING_DATA'
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
freedv = self.c_lib.freedv_open(static.FREEDV_DATA_MODE)
static.FREEDV_DATA_BYTES_PER_FRAME = int(self.c_lib.freedv_get_bits_per_modem_frame(freedv)/8)
@ -156,58 +124,94 @@ class RF():
txbuffer += bytes(mod_out_preamble)
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
for n in range(0,static.ARQ_TX_N_FRAMES_PER_BURST):
if static.ARQ_RPT_RECEIVED == False:
for n in range(0,static.ARQ_TX_N_FRAMES_PER_BURST):
#---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
frame_type = 10 + n + 1 #static.ARQ_TX_N_FRAMES_PER_BURST
frame_type = bytes([frame_type])
frame_type = 10 + n + 1 #static.ARQ_TX_N_FRAMES_PER_BURST
frame_type = bytes([frame_type])
payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + n])
payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + n])
n_current_arq_frame = static.ARQ_N_SENT_FRAMES + n + 1
static.ARQ_TX_N_CURRENT_ARQ_FRAME = n_current_arq_frame.to_bytes(2, byteorder='big')
n_current_arq_frame = static.ARQ_N_SENT_FRAMES + n + 1
static.ARQ_TX_N_CURRENT_ARQ_FRAME = n_current_arq_frame.to_bytes(2, byteorder='big')
n_total_arq_frame = len(static.TX_BUFFER)
static.ARQ_TX_N_TOTAL_ARQ_FRAMES = n_total_arq_frame.to_bytes(2, byteorder='big')
# 1 # frame type and current number of arq frame of burst
# 1 # total number of arq frames per (current) burst
# 2 # current arq frame number
# 2 # total number arq frames
# 2 # arq crc
# N # payload data
n_total_arq_frame = len(static.TX_BUFFER)
static.ARQ_TX_N_TOTAL_ARQ_FRAMES = n_total_arq_frame.to_bytes(2, byteorder='big')
arqframe = frame_type + \
arqframe = frame_type + \
bytes([static.ARQ_TX_N_FRAMES_PER_BURST]) + \
static.ARQ_TX_N_CURRENT_ARQ_FRAME + \
static.ARQ_TX_N_TOTAL_ARQ_FRAMES + \
static.ARQ_BURST_PAYLOAD_CRC + \
static.DXCALLSIGN_CRC8 + \
static.MYCALLSIGN_CRC8 + \
payload_data
#print(arqframe)
#print(arqframe)
buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), static.FREEDV_DATA_PAYLOAD_PER_FRAME)) # generate CRC16
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
buffer += crc # append crc16 to buffer
crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), static.FREEDV_DATA_PAYLOAD_PER_FRAME)) # generate CRC16
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
buffer += crc # append crc16 to buffer
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
txbuffer += bytes(mod_out)
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
txbuffer += bytes(mod_out)
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
# -------------- transmit audio
elif static.ARQ_RPT_RECEIVED == True:
for n in range(0,len(static.ARQ_RPT_FRAMES)):
missing_frame = int.from_bytes(static.ARQ_RPT_FRAMES[n], "big")
#---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
frame_type = 10 + missing_frame #static.ARQ_TX_N_FRAMES_PER_BURST
frame_type = bytes([frame_type])
payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + missing_frame - 1])
n_current_arq_frame = static.ARQ_N_SENT_FRAMES + missing_frame
static.ARQ_TX_N_CURRENT_ARQ_FRAME = n_current_arq_frame.to_bytes(2, byteorder='big')
n_total_arq_frame = len(static.TX_BUFFER)
static.ARQ_TX_N_TOTAL_ARQ_FRAMES = n_total_arq_frame.to_bytes(2, byteorder='big')
arqframe = frame_type + \
bytes([static.ARQ_TX_N_FRAMES_PER_BURST]) + \
static.ARQ_TX_N_CURRENT_ARQ_FRAME + \
static.ARQ_TX_N_TOTAL_ARQ_FRAMES + \
static.DXCALLSIGN_CRC8 + \
static.MYCALLSIGN_CRC8 + \
payload_data
#print(arqframe)
buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), static.FREEDV_DATA_PAYLOAD_PER_FRAME)) # generate CRC16
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
buffer += crc # append crc16 to buffer
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
txbuffer += bytes(mod_out)
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
# -------------- transmit audio
self.stream_tx.write(bytes(txbuffer))
static.ARQ_STATE = 'RECEIVING_ACK'
#time.sleep(0.5)
static.ARQ_STATE = 'IDLE'
#static.ARQ_STATE = 'RECEIVING_SIGNALLING'
#--------------------------------------------------------------------------------------------------------
def receive(self,data_mode,signalling_mode):
force = False
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
freedv_data = self.c_lib.freedv_open(data_mode)
@ -232,24 +236,22 @@ class RF():
stuck_in_sync_counter = 0
stuck_in_sync_10_counter = 0
#
while static.ARQ_STATE == 'RECEIVING_DATA':
time.sleep(0.01)
nin = self.c_lib.freedv_nin(freedv_data)
nin = int(nin*(static.AUDIO_SAMPLE_RATE_RX/static.MODEM_SAMPLE_RATE))
#nin = int(nin*(static.AUDIO_SAMPLE_RATE_RX/static.MODEM_SAMPLE_RATE))
data_in = self.stream_rx.read(nin, exception_on_overflow = False)
#print(audioop.rms(data_in, 2))
data_in = data_in.rstrip(b'\x00')
#print(data_in)
self.c_lib.freedv_rawdatarx.argtype = [ctypes.POINTER(ctypes.c_ubyte), data_bytes_out, data_in] # check if really neccessary
#self.c_lib.freedv_rawdatarx.argtype = [ctypes.POINTER(ctypes.c_ubyte), data_bytes_out, data_in] # check if really neccessary
nbytes = self.c_lib.freedv_rawdatarx(freedv_data, data_bytes_out, data_in) # demodulate audio
#print(self.c_lib.freedv_get_rx_status(freedv_data))
logging.debug(self.c_lib.freedv_get_rx_status(freedv_data))
#-------------STUCK IN SYNC DETECTOR
stuck_in_sync_counter += 1
@ -257,9 +259,8 @@ class RF():
stuck_in_sync_10_counter += 1
#print(stuck_in_sync_counter)
if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv_data) == 10:
print("stuck in sync #1 --> DOING UNSYNC")
logging.critical("stuck in sync #1")
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
stuck_in_sync_counter = 0
stuck_in_sync_10_counter = 0
@ -267,22 +268,16 @@ class RF():
if stuck_in_sync_counter >= 66 and stuck_in_sync_10_counter >= 2:
print("stuck in sync #2 --> DOING UNSYNC")
logging.critical("stuck in sync #2")
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
stuck_in_sync_counter = 0
stuck_in_sync_10_counter = 0
data_in = None
#-----------------------------------
#modem_stats_snr = c_float()
#modem_stats_sync = c_int()
#self.c_lib.freedv_get_modem_stats(freedv_data,byref(modem_stats_sync), byref(modem_stats_snr))
#modem_stats_snr = modem_stats_snr.value
#print(modem_stats_snr)
if nbytes == static.FREEDV_DATA_BYTES_PER_FRAME:
# counter reset for stuck in sync counter
stuck_in_sync_counter = 0
stuck_in_sync_10_counter = 0
@ -292,45 +287,69 @@ class RF():
frametype = int.from_bytes(bytes(data_bytes_out[:1]), "big")
frame = frametype - 10
n_frames_per_burst = int.from_bytes(bytes(data_bytes_out[1:2]), "big")
if 50 >= frametype >= 10 and len(data_bytes_out) > 30: # --> The length check filters out random strings without CRC
arq.data_received(bytes(data_bytes_out[:-2])) #send payload data to arq checker without CRC16
if 50 >= frametype >= 10:
if frame != 3 or force == True:
arq.data_received(bytes(data_bytes_out[:-2])) #send payload data to arq checker without CRC16
#print("static.ARQ_RX_BURST_BUFFER.count(None) " + str(static.ARQ_RX_BURST_BUFFER.count(None)))
if static.ARQ_RX_BURST_BUFFER.count(None) <= 1:
logging.debug("FULL BURST BUFFER ---> UNSYNC")
self.c_lib.freedv_set_sync(freedv_data, 0)
else:
logging.critical("---------------------------SIMULATED MISSING FRAME")
force = True
else:
print("MODE: " + str(data_mode) + " DATA: " + str(bytes(data_bytes_out)))
logging.info("MODE: " + str(data_mode) + " DATA: " + str(bytes(data_bytes_out)))
# NEEDS TO BE OPTIMIZED
# DO UNSYNC AFTER LAST BURST by checking the frame numbers agains the total frames per burst
# DO UNSYNC AFTER LAST BURST by checking the frame numbers agains the total frames per burst
if frame == n_frames_per_burst:
logging.debug("LAST FRAME ---> UNSYNC")
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
# DETECT STUCK IN SYNC
# count rx status 10
# if 10 greater 2 after 6 then unsync
while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_ACK':
while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_SIGNALLING':
time.sleep(0.01)
nin = self.c_lib.freedv_nin(freedv_signalling)
nin = int(nin*(static.AUDIO_SAMPLE_RATE_RX/static.MODEM_SAMPLE_RATE))
#nin = int(nin*(static.AUDIO_SAMPLE_RATE_RX/static.MODEM_SAMPLE_RATE))
data_in = self.stream_rx.read(nin, exception_on_overflow = False)
data_in = data_in.rstrip(b'\x00')
self.c_lib.freedv_rawdatarx.argtype = [ctypes.POINTER(ctypes.c_ubyte), signalling_bytes_out, data_in] # check if really neccessary
#self.c_lib.freedv_rawdatarx.argtype = [ctypes.POINTER(ctypes.c_ubyte), signalling_bytes_out, data_in] # check if really neccessary
nbytes = self.c_lib.freedv_rawdatarx(freedv_signalling, signalling_bytes_out, data_in) # demodulate audio
# CHECK IF FRAME CONTAINS ACK------------------------
frametype = int.from_bytes(bytes(signalling_bytes_out[:1]), "big")
# BURST ACK
if frametype == 60 and nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME:
if nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME:
self.c_lib.freedv_set_sync(freedv_signalling, 0)
frametype = int.from_bytes(bytes(signalling_bytes_out[:1]), "big")
# BURST ACK
if frametype == 60:
logging.debug("ACK RECEIVED....")
arq.burst_ack_received()
# FRAME ACK
if frametype == 61 and nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME:
# FRAME ACK
elif frametype == 61:
logging.debug("FRAME ACK RECEIVED....")
arq.frame_ack_received()
# FRAME RPT
elif frametype == 62:
logging.debug("REPEAT REQUEST RECEIVED....")
arq.burst_rpt_received(signalling_bytes_out[:-2])
else:
logging.debug("OTHER FRAME: " + str(signalling_bytes_out[:-2]))
rxstatus = self.c_lib.freedv_get_rx_status(freedv_signalling)
#print(rxstatus)
logging.debug("ACK-" + str(rxstatus))
if nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME or rxstatus == 10:
self.c_lib.freedv_set_sync(freedv_signalling, 0) #FORCE UNSYNC

View file

@ -14,20 +14,40 @@ import logging
import static
import arq
class TCPRequestHandler(socketserver.BaseRequestHandler):
class DATATCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = bytes()
while True:
chunk = self.request.recv(8192)#.strip()
chunk = self.request.recv(8192)#.strip()
self.data += chunk
if chunk.endswith(b'\n'):
break
self.data += chunk
# SEND AN ARQ FRAME -------------------------
if self.data.startswith(b'ARQ:'):
data = self.data.split(b'ARQ:')
data_out = data[1]
TRANSMIT_ARQ = threading.Thread(target=arq.transmit, args=[data_out], name="TRANSMIT_ARQ")
TRANSMIT_ARQ.start()
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = bytes()
while True:
chunk = self.request.recv(8192)#.strip()
self.data += chunk
if chunk.endswith(b'\n'):
break
# self.request is the TCP socket connected to the client
#self.data = self.request.recv(1024).strip()
@ -49,28 +69,29 @@ class TCPRequestHandler(socketserver.BaseRequestHandler):
if self.data.startswith(b'SHOWBUFFERSIZE'):
self.request.sendall(bytes(static.RX_BUFFER[-1]))
print(static.RX_BUFFER_SIZE)
# BROADCAST PARSER -----------------------------------------------------------
if self.data.startswith(b'BC:'):
#import modem
#modem = modem.RF()
data = self.data.split(b'BC:')
#modem.Transmit(data[1])
# SEND AN ARQ FRAME -----------------------------------------------------------
if self.data.startswith(b'ARQ:'):
data = self.data.split(b'ARQ:')
data_out = data[1]
#arq.transmit(data_out)
TRANSMIT_ARQ = threading.Thread(target=arq.transmit, args=[data_out], name="TRANSMIT_ARQ")
TRANSMIT_ARQ.start()
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()
def start_data_socket():
try:
logging.info("SRV | STARTING TCP/IP SOCKET FOR DATA ON PORT: " + str(static.PORT + 1))
socketserver.TCPServer.allow_reuse_address = True #https://stackoverflow.com/a/16641793
dataserver = socketserver.TCPServer((static.HOST, static.PORT + 1), DATATCPRequestHandler)
dataserver.serve_forever()
finally:
dataserver.server_close()

View file

@ -5,30 +5,35 @@ Created on Wed Dec 23 11:13:57 2020
@author: DJ2LS
"""
# ADDITION MESSUREMENT:
#AUDIO TIME: 7.451462268829346 #12 # 1 FRAME + PREAMBLE
#MODULATION TIME: 0.002051115036010742 #12 # 1 FRAME + PREAMBLE
#MODULATION TIME: 0.004580974578857422 #12 # 2 FRAME + PREAMBLE
#AUDIO TIME: 14.750595331192017 #12 # 2 FRAME + PREAMBLE
# Operator Defaults
MYCALLSIGN = b''
MYCALLSIGN_CRC8 = b''
DXCALLSIGN = b''
DXCALLSIGN_CRC8 = b''
MYGRID = b''
#---------------------------------
# Server Defaults
HOST = "localhost"
PORT = 3000
#---------------------------------
# FreeDV Defaults
FREEDV_RECEIVE = True
FREEDV_DATA_MODE = 12
FREEDV_DATA_MODE = 10
FREEDV_SIGNALLING_MODE = 14
FREEDV_DATA_BYTES_PER_FRAME = 0
FREEDV_DATA_PAYLOAD_PER_FRAME = 0
FREEDV_SIGNALLING_BYTES_PER_FRAME = 0
FREEDV_SIGNALLING_PAYLOAD_PER_FRAME = 0
#---------------------------------
# Server Defaults
HOST = "localhost"
PORT = 3000
#AUdio Defaults
#Audio Defaults
AUDIO_INPUT_DEVICE = 1
AUDIO_OUTPUT_DEVICE = 1
#TX_SAMPLE_STATE = None
@ -39,20 +44,17 @@ AUDIO_OUTPUT_DEVICE = 1
MODEM_SAMPLE_RATE = 8000 #8000
AUDIO_FRAMES_PER_BUFFER = 2048
AUDIO_CHANNELS = 1
#---------------------------------
#TNC DEFAULTS
# ARQ
TX_N_MAX_RETRIES = 3
#ARQ DEFAULTS
TX_N_MAX_RETRIES = 10
TX_N_RETRIES = 0
ARQ_TX_N_FRAMES_PER_BURST = 0
ARQ_TX_N_BURSTS = 0
ARQ_PAYLOAD_PER_FRAME = 0
ARQ_ACK_WAITING_FOR_ID = 0
ARQ_RX_BURST_BUFFER = []
ARQ_RX_FRAME_BUFFER = []
ARQ_RX_FRAME_N_BURSTS = 0
@ -68,13 +70,22 @@ ARQ_RX_N_CURRENT_ARQ_FRAME = 0
##
ARQ_N_RX_ARQ_FRAMES = 0 # total number of received frames
ARQ_N_RX_FRAMES_PER_BURSTS = 0 # NUMBER OF FRAMES WE ARE WAITING FOR --> GOT DATA FROM RECEIVED FRAME
ARQ_ACK_PAYLOAD_PER_FRAME = 0 # PAYLOAD per ACK frame
ARQ_ACK_RECEIVED = 0 # set to 1 if ACK received
ARQ_FRAME_ACK_RECEIVED = 0 # set to 1 if FRAME ACK received
ARQ_ACK_TIMEOUT = 0 # set to 1 if timeut reached
ARQ_ACK_TIMEOUT_SECONDS = 4.0 #timeout for waiting for ACK frames
ARQ_ACK_RECEIVED = False # set to 1 if ACK received
ARQ_RX_ACK_TIMEOUT = False # set to 1 if timeut reached
ARQ_RX_ACK_TIMEOUT_SECONDS = 10.0 #timeout for waiting for ACK frames
ARQ_FRAME_ACK_RECEIVED = False # set to 1 if FRAME ACK received
ARQ_RX_FRAME_TIMEOUT = False
ARQ_RX_FRAME_TIMEOUT_SECONDS = 10.0
ARQ_RX_RPT_TIMEOUT = False
ARQ_RX_RPT_TIMEOUT_SECONDS = 10.0
ARQ_RPT_RECEIVED = False #indicate if RPT frame has been received
ARQ_RPT_FRAMES = [] #buffer for frames which are requested to repeat
FRAME_CRC = b''
FRAME_BOF = b'\xAA\xAA' #here we define 2 bytes for the BOF
@ -89,7 +100,7 @@ ARQ_N_SENT_FRAMES = 0 #counter for already sent frames
# IDLE
# RECEIVING_DATA
# SENDING_DATA
# RECEIVING_ACK
# RECEIVING_SIGNALLING
# SENDING_ACK
# ACK_RECEIVED
#