mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
improved data frame processing
This commit is contained in:
parent
b9f2f1874f
commit
6f770d1d69
3 changed files with 165 additions and 112 deletions
223
arq.py
223
arq.py
|
@ -14,6 +14,7 @@ from random import randrange
|
||||||
import static
|
import static
|
||||||
import modem
|
import modem
|
||||||
import helpers
|
import helpers
|
||||||
|
import main
|
||||||
|
|
||||||
modem = modem.RF()
|
modem = modem.RF()
|
||||||
|
|
||||||
|
@ -25,132 +26,168 @@ def arq_ack_timeout():
|
||||||
|
|
||||||
|
|
||||||
def data_received(data_in):
|
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 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_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
|
||||||
# get total number of arq frames from data frame header
|
static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = int.from_bytes(bytes(data_in[4:6]), "big") # get get total number of frames
|
||||||
if data_in[8:10] == static.FRAME_BOF:
|
static.ARQ_BURST_PAYLOAD_CRC = data_in[6:8]
|
||||||
static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = int.from_bytes(bytes(data_in[4:6]), "big")
|
|
||||||
print("----------------------------------------------------------------")
|
|
||||||
logging.info("ARQ | RX | INCOMMING DATA FRAME ----- ARQ FRAMES: " + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME))
|
print("----------------------------------------------------------------")
|
||||||
|
logging.info("ARQ_N_FRAME: " + str(static.ARQ_N_FRAME))
|
||||||
|
logging.info("ARQ_N_RX_FRAMES_PER_BURSTS: " + str(static.ARQ_N_RX_FRAMES_PER_BURSTS))
|
||||||
|
logging.info("ARQ_RX_N_CURRENT_ARQ_FRAME: " + str(static.ARQ_RX_N_CURRENT_ARQ_FRAME))
|
||||||
|
logging.info("ARQ_N_ARQ_FRAMES_PER_DATA_FRAME: " + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME))
|
||||||
|
logging.info("static.ARQ_BURST_PAYLOAD_CRC: " + str(static.ARQ_BURST_PAYLOAD_CRC))
|
||||||
|
print("----------------------------------------------------------------")
|
||||||
|
|
||||||
|
|
||||||
arq_percent_burst = int((static.ARQ_N_FRAME / static.ARQ_N_RX_FRAMES_PER_BURSTS)*100)
|
arq_percent_burst = int((static.ARQ_N_FRAME / static.ARQ_N_RX_FRAMES_PER_BURSTS)*100)
|
||||||
arq_percent_frame = int(((static.ARQ_N_RX_ARQ_FRAMES + static.ARQ_N_FRAME)/static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME)*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_N_RX_ARQ_FRAMES + static.ARQ_N_FRAME) + "/" + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME) + "] [" + str(arq_percent_frame).zfill(3) + "%]" )
|
|
||||||
|
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) + "%]" )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#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:
|
||||||
|
for i in range(0,static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME+1):
|
||||||
|
static.ARQ_RX_FRAME_BUFFER.insert(i,None)
|
||||||
|
|
||||||
|
|
||||||
#allocate ARQ_RX_BURST_BUFFER as a list with "None" if not already done. This should be done only once per burst!
|
#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
|
# 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:
|
if static.ARQ_N_RX_FRAMES_PER_BURSTS != len(static.ARQ_RX_BURST_BUFFER) and static.ARQ_N_FRAME == 1:
|
||||||
for i in range(0,static.ARQ_N_RX_FRAMES_PER_BURSTS+1):
|
for i in range(0,static.ARQ_N_RX_FRAMES_PER_BURSTS+1):
|
||||||
static.ARQ_RX_BURST_BUFFER.insert(i,None)
|
static.ARQ_RX_BURST_BUFFER.insert(i,None)
|
||||||
#print(len(static.ARQ_RX_BURST_BUFFER))
|
|
||||||
|
|
||||||
# now we add the incoming data to the specified position in our list
|
|
||||||
static.ARQ_RX_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
|
|
||||||
|
|
||||||
|
# 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
|
# run only if we recieved all ARQ FRAMES per ARQ BURST
|
||||||
burst_total_payload = bytearray()
|
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
|
if static.ARQ_N_FRAME == static.ARQ_N_RX_FRAMES_PER_BURSTS: #if received bursts are equal to burst number in frame
|
||||||
|
|
||||||
#print("len ARQ_RX_BURST_BUFFER: " + str(len(static.ARQ_RX_BURST_BUFFER)-1))
|
#here we get the total payload for the frame to calculate the crc
|
||||||
#print("static.ARQ_N_FRAME: " + str(static.ARQ_N_FRAME))
|
|
||||||
#print("static.ARQ_N_RX_FRAMES_PER_BURSTS: " + str(static.ARQ_N_RX_FRAMES_PER_BURSTS))
|
|
||||||
|
|
||||||
|
|
||||||
#for l in range(0,len(static.ARQ_RX_BURST_BUFFER)):
|
|
||||||
# print(static.ARQ_RX_BURST_BUFFER[l])
|
|
||||||
|
|
||||||
|
|
||||||
#here we get the total payload for the frame
|
|
||||||
for n_raw_frame in range(1,len(static.ARQ_RX_BURST_BUFFER)):
|
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
|
# we need to check if we have a None or received data in list
|
||||||
if static.ARQ_RX_BURST_BUFFER[n_raw_frame] != None:
|
if static.ARQ_RX_BURST_BUFFER[n_raw_frame] != None:
|
||||||
burst_frame = static.ARQ_RX_BURST_BUFFER[n_raw_frame] #get burst frame
|
burst_frame = static.ARQ_RX_BURST_BUFFER[n_raw_frame] #get burst frame
|
||||||
burst_payload = burst_frame[4:] #remove frame type and burst CRC
|
burst_payload = burst_frame[8:] #remove frame type and burst CRC #4
|
||||||
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
|
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
|
||||||
|
|
||||||
# ------------------ calculate CRC of BURST
|
# ------------------ calculate CRC of BURST
|
||||||
burst_payload_crc = helpers.get_crc_16(burst_total_payload)
|
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 CRC IS CORRECT, APPEND BURST TO BUFFER AND SEND ACK FRAME
|
||||||
if burst_payload_crc == data_in[2:4]:
|
if burst_payload_crc == data_in[6:8]:
|
||||||
|
|
||||||
#update received frames counter
|
#BUILDING ACK FRAME FOR BURST -----------------------------------------------
|
||||||
static.ARQ_N_RX_ARQ_FRAMES = static.ARQ_N_RX_ARQ_FRAMES + static.ARQ_N_RX_FRAMES_PER_BURSTS
|
|
||||||
|
|
||||||
#logging.info("ARQ BURST CRC ARE EQUAL!")
|
|
||||||
static.ARQ_RX_FRAME_BUFFER.append(burst_total_payload) # IF CRC TRUE APPEND burst_total_payload TO ARQ_RX_FRAME_BUFFER
|
|
||||||
|
|
||||||
#BUILDING ACK FRAME -----------------------------------------------
|
|
||||||
ack_payload = bytes(burst_payload_crc)
|
ack_payload = bytes(burst_payload_crc)
|
||||||
ack_frame = b'<'+ bytes(burst_payload_crc) # < = 60
|
ack_frame = b'<'+ bytes(burst_payload_crc) # < = 60
|
||||||
ack_buffer = bytearray(static.ARQ_ACK_PAYLOAD_PER_FRAME)
|
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_buffer[:len(ack_frame)] = ack_frame # set buffersize to length of data which will be send
|
||||||
|
|
||||||
#TRANSMIT ACK FRAME -----------------------------------------------
|
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
|
||||||
logging.info("ARQ | TX | ARQ BURST ACK [" + str(data_in[1:3].hex()) +"]")
|
logging.info("ARQ | TX | ARQ BURST ACK [" + str(data_in[6:8].hex()) +"]") #1:3
|
||||||
modem.transmit_arq_ack(ack_buffer)
|
modem.transmit_arq_ack(ack_buffer)
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
static.ARQ_RX_BURST_BUFFER = [] # CLEAR RX BURST BUFFER AFTER SENDING DATA
|
static.ARQ_RX_BURST_BUFFER = [] # CLEAR RX BURST BUFFER AFTER SENDING DATA
|
||||||
|
|
||||||
else: #IF burst payload crc and input crc are NOT equal
|
else: #IF burst payload crc and input crc are NOT equal
|
||||||
logging.info("ARQ BURST CRC NOT EQUAL! [" + str(data_in[2:4]) + "]")
|
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 = [] #erase ARQ RX Burst buffer
|
||||||
|
|
||||||
|
|
||||||
# LOOP THOUGH FRAME BUFFER AND STICK EVERYTHING TOGETHER
|
#if nframes are unequal to expected frames per burst
|
||||||
# WE ALSO CHECK FOR FRAME HEADER AND LAST FRAME
|
else:
|
||||||
complete_frame = bytearray()
|
print("unequal")
|
||||||
#print(static.ARQ_RX_FRAME_BUFFER)
|
pass
|
||||||
for frame in range(len(static.ARQ_RX_FRAME_BUFFER)):
|
|
||||||
complete_frame = complete_frame + static.ARQ_RX_FRAME_BUFFER[frame]
|
# ---------------------------- FRAME MACHINE
|
||||||
|
|
||||||
# -------- DETECT IF WE ALREADY RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
|
# --------------- CHECK IF WE ARE MISSING FRAMES -------------------------------------------
|
||||||
if complete_frame[4:6].startswith(static.FRAME_BOF) or burst_total_payload[4:6].startswith(static.FRAME_BOF): #5:7
|
for frame in range(1,len(static.ARQ_RX_FRAME_BUFFER)):
|
||||||
static.FRAME_CRC = complete_frame[2:4]
|
if static.ARQ_RX_FRAME_BUFFER[frame] == None:
|
||||||
static.ARQ_FRAME_BOF_RECEIVED = True
|
print("Missing frames:" + str(frame))
|
||||||
|
|
||||||
# -------- DETECT IF WE HAVE ALREADY RECEIVED THE LAST FRAME
|
# --------------- IF LIST NOT CONTAINS "None" stick everything together
|
||||||
if burst_total_payload.rstrip(b'\x00').endswith(static.FRAME_EOF):
|
complete_data_frame = bytearray()
|
||||||
static.ARQ_FRAME_EOF_RECEIVED = True
|
if static.ARQ_RX_FRAME_BUFFER.count(None) == 1: ## 1 because position 0 of list will alaways be None in our case
|
||||||
|
print("ALL DATA RECEIVED!")
|
||||||
|
|
||||||
|
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:]
|
||||||
|
|
||||||
|
# -------- DETECT IF WE RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
|
||||||
|
if arq_frame_payload[2:4].startswith(static.FRAME_BOF):
|
||||||
|
static.FRAME_CRC = arq_frame_payload[:2]
|
||||||
|
static.ARQ_FRAME_BOF_RECEIVED = True
|
||||||
|
|
||||||
|
arq_frame_payload = arq_frame_payload.split(static.FRAME_BOF)
|
||||||
|
arq_frame_payload = arq_frame_payload[1]
|
||||||
|
|
||||||
|
# -------- DETECT IF WE RECEIVED A FRAME FOOTER THEN SAVE DATA TO GLOBALS
|
||||||
|
if arq_frame_payload.rstrip(b'\x00').endswith(static.FRAME_EOF):
|
||||||
|
static.ARQ_FRAME_EOF_RECEIVED = True
|
||||||
|
|
||||||
|
arq_frame_payload = arq_frame_payload.split(static.FRAME_EOF)
|
||||||
|
arq_frame_payload = arq_frame_payload[0]
|
||||||
|
|
||||||
|
# --------- 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
|
#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:
|
if static.ARQ_FRAME_BOF_RECEIVED == True and static.ARQ_FRAME_EOF_RECEIVED == True:
|
||||||
|
|
||||||
# NOW WE TRY TO SEPARATE THE FRAME CRC FOR A CRC CALCULATION
|
frame_payload_crc = helpers.get_crc_16(complete_data_frame)
|
||||||
frame_payload = complete_frame.rstrip(b'\x00') #REMOVE x00
|
|
||||||
frame_payload = frame_payload[6:-2] #THIS IS THE FRAME PAYLOAD
|
|
||||||
frame_payload_crc = helpers.get_crc_16(frame_payload)
|
|
||||||
|
|
||||||
#IF THE FRAME PAYLOAD CRC IS EQUAL TO THE FRAME CRC WHICH IS KNOWN FROM THE HEADER --> SUCCESS
|
#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:
|
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")
|
||||||
|
|
||||||
static.RX_BUFFER.append(frame_payload)
|
static.RX_BUFFER.append(complete_data_frame)
|
||||||
|
|
||||||
# clearing buffers and resetting counters
|
# clearing buffers and resetting counters
|
||||||
|
static.ARQ_RX_BURST_BUFFER = []
|
||||||
static.ARQ_RX_FRAME_BUFFER = []
|
static.ARQ_RX_FRAME_BUFFER = []
|
||||||
static.ARQ_FRAME_BOF_RECEIVED = False
|
static.ARQ_FRAME_BOF_RECEIVED = False
|
||||||
static.ARQ_FRAME_EOF_RECEIVED = False
|
static.ARQ_FRAME_EOF_RECEIVED = False
|
||||||
static.ARQ_N_RX_ARQ_FRAMES = 0
|
#static.ARQ_N_RX_ARQ_FRAMES = 0
|
||||||
static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0
|
static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0
|
||||||
|
|
||||||
print("----------------------------------------------------------------")
|
print("----------------------------------------------------------------")
|
||||||
print(static.RX_BUFFER[-1])
|
print(static.RX_BUFFER[-1])
|
||||||
|
|
||||||
print("----------------------------------------------------------------")
|
print("----------------------------------------------------------------")
|
||||||
# HERE: WE COULD SEND ACK FOR TOTAL FRAME
|
# HERE: WE COULD SEND ACK FOR TOTAL FRAME
|
||||||
else:
|
else:
|
||||||
logging.info("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!")
|
logging.info("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!")
|
||||||
#print("ARQ FRAME PAYLOAD CRC: " + str(frame_payload_crc))
|
|
||||||
#print("ARQ FRAME PAYLOAD: " + str(frame_payload))
|
|
||||||
#print("DATA FRAME: " + str(complete_frame))
|
|
||||||
#static.ARQ_RX_FRAME_BUFFER = [] # ---> BUFFER ERST LÖSCHEN WENN MINDESTANZAHL AN BURSTS ERHALTEN WORDEN SIND
|
|
||||||
|
|
||||||
def ack_received():
|
def ack_received():
|
||||||
|
|
||||||
|
@ -161,30 +198,24 @@ def ack_received():
|
||||||
|
|
||||||
def transmit(data_out):
|
def transmit(data_out):
|
||||||
|
|
||||||
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 4 #3 ohne ARQ_TX_N_FRAMES_PER_BURST
|
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 8 #3 ohne ARQ_TX_N_FRAMES_PER_BURST
|
||||||
frame_header_length = 8
|
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
|
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.FREEDV_DATA_PAYLOAD_PER_FRAME)
|
||||||
#print(static.ARQ_PAYLOAD_PER_FRAME)
|
#print(static.ARQ_PAYLOAD_PER_FRAME)
|
||||||
#print(n_bursts_prediction)
|
#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_N_ARQ_FRAMES_PER_DATA_FRAME = n_arq_frames_per_data_frame.to_bytes(2, byteorder='big') #65535
|
||||||
|
|
||||||
frame_payload_crc = helpers.get_crc_16(data_out)
|
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
|
# This is the total frame with frame header, which will be send
|
||||||
data_out = static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME + frame_payload_crc + static.FRAME_BOF + data_out + static.FRAME_EOF
|
data_out = frame_payload_crc + static.FRAME_BOF + data_out + static.FRAME_EOF
|
||||||
# 2 2 2 N 2
|
# 2 2 N 2
|
||||||
#print("ARQ_N_ARQ_FRAMES_PER_DATA_FRAME" + str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#print(data_out)
|
|
||||||
#print(len(data_out))
|
|
||||||
#print(static.ARQ_PAYLOAD_PER_FRAME)
|
|
||||||
|
|
||||||
# --------------------------------------------- LETS CREATE A BUFFER BY SPLITTING THE FILES INTO PEACES
|
# --------------------------------------------- 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 = [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)
|
static.TX_BUFFER_SIZE = len(static.TX_BUFFER)
|
||||||
|
@ -230,10 +261,10 @@ def transmit(data_out):
|
||||||
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
|
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
|
||||||
burst_payload[:len(burst_raw_payload)] = burst_raw_payload # get frame from TX_BUFFER
|
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
|
burst_total_payload = burst_total_payload + burst_payload # append single frame to total payload buffer
|
||||||
#print(burst_total_payload)
|
|
||||||
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES_PER_BURST
|
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES_PER_BURST
|
||||||
static.ARQ_BURST_PAYLOAD_CRC = helpers.get_crc_16(burst_total_payload)
|
static.ARQ_BURST_PAYLOAD_CRC = helpers.get_crc_16(burst_total_payload)
|
||||||
|
|
||||||
#--------------------------------------------- N ATTEMPTS TO SEND BURSTS IF ACK RECEPTION FAILS
|
#--------------------------------------------- N ATTEMPTS TO SEND BURSTS IF ACK RECEPTION FAILS
|
||||||
for static.TX_N_RETRIES in range(static.TX_N_MAX_RETRIES):
|
for static.TX_N_RETRIES in range(static.TX_N_MAX_RETRIES):
|
||||||
logging.info("ARQ | TX | ARQ FRAME --- ATTEMPT [" + str(static.TX_N_RETRIES+1) + "/" + str(static.TX_N_MAX_RETRIES) + "]")
|
logging.info("ARQ | TX | ARQ FRAME --- ATTEMPT [" + str(static.TX_N_RETRIES+1) + "/" + str(static.TX_N_MAX_RETRIES) + "]")
|
||||||
|
@ -253,28 +284,16 @@ def transmit(data_out):
|
||||||
|
|
||||||
logging.info("ARQ | RX | WAITING FOR ACK")
|
logging.info("ARQ | RX | WAITING FOR ACK")
|
||||||
static.ARQ_STATE = 'RECEIVING_ACK'
|
static.ARQ_STATE = 'RECEIVING_ACK'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
timer = threading.Timer(static.ARQ_ACK_TIMEOUT_SECONDS, arq_ack_timeout)
|
timer = threading.Timer(static.ARQ_ACK_TIMEOUT_SECONDS, arq_ack_timeout)
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
# waiting the other way...
|
|
||||||
#starttimer = time.time() + static.ARQ_ACK_TIMEOUT_SECONDS
|
|
||||||
#while starttimer > time.time() and static.ARQ_ACK_RECEIVED == 0:
|
|
||||||
# #print(str(starttimer - time.time()))
|
|
||||||
# pass
|
|
||||||
#else:
|
|
||||||
# static.ARQ_ACK_TIMEOUT = 1
|
|
||||||
|
|
||||||
#static.MODEM_RECEIVE = False
|
|
||||||
|
|
||||||
# --------------------------- WHILE TIMEOUT NOT REACHED AND NO ACK RECEIVED --> LISTEN
|
# --------------------------- 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_TIMEOUT == 0 and static.ARQ_ACK_RECEIVED == 0:
|
||||||
time.sleep(0.01) # lets reduce CPU load a little bit
|
time.sleep(0.01) # lets reduce CPU load a little bit
|
||||||
#print(static.ARQ_STATE)
|
#print(static.ARQ_STATE)
|
||||||
|
|
||||||
|
|
||||||
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED
|
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED
|
||||||
if static.ARQ_ACK_RECEIVED == 1:
|
if static.ARQ_ACK_RECEIVED == 1:
|
||||||
#-----------IF ACK RECEIVED, INCREMENT ITERATOR FOR MAIN LOOP TO PROCEED WITH NEXT FRAMES/BURST
|
#-----------IF ACK RECEIVED, INCREMENT ITERATOR FOR MAIN LOOP TO PROCEED WITH NEXT FRAMES/BURST
|
||||||
|
@ -315,6 +334,6 @@ def transmit(data_out):
|
||||||
# BURST MACHINE TO DEFINE N BURSTS PER FRAME ---> LATER WE CAN USE CHANNEL MESSUREMENT TO SET FRAMES PER BURST
|
# 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():
|
def get_n_frames_per_burst():
|
||||||
|
|
||||||
n_frames_per_burst = randrange(1,5)
|
n_frames_per_burst = randrange(1,15)
|
||||||
#n_frames_per_burst = 3
|
#n_frames_per_burst = 1
|
||||||
return n_frames_per_burst
|
return n_frames_per_burst
|
||||||
|
|
41
modem.py
41
modem.py
|
@ -90,10 +90,11 @@ class RF():
|
||||||
txbuffer += bytes(mod_out_preamble)
|
txbuffer += bytes(mod_out_preamble)
|
||||||
txbuffer += bytes(mod_out)
|
txbuffer += bytes(mod_out)
|
||||||
txbuffer = txbuffer.rstrip(b'\x00')
|
txbuffer = txbuffer.rstrip(b'\x00')
|
||||||
|
|
||||||
|
|
||||||
# -------------- transmit audio
|
# -------------- transmit audio
|
||||||
self.stream_tx.write(bytes(txbuffer))
|
self.stream_tx.write(bytes(txbuffer))
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------
|
||||||
def transmit_arq_ack(self,ack_buffer):
|
def transmit_arq_ack(self,ack_buffer):
|
||||||
|
|
||||||
|
@ -127,10 +128,10 @@ class RF():
|
||||||
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
|
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
|
||||||
txbuffer += bytes(mod_out)
|
txbuffer += bytes(mod_out)
|
||||||
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
|
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
|
||||||
|
|
||||||
# -------------- transmit audio
|
# -------------- transmit audio
|
||||||
self.stream_tx.write(bytes(txbuffer))
|
self.stream_tx.write(bytes(txbuffer))
|
||||||
|
self.stream_tx.write(bytes(txbuffer))
|
||||||
|
|
||||||
static.ARQ_STATE = 'RECEIVING_DATA'
|
static.ARQ_STATE = 'RECEIVING_DATA'
|
||||||
#--------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------
|
||||||
# GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT
|
# GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT
|
||||||
|
@ -162,8 +163,30 @@ class RF():
|
||||||
frame_type = bytes([frame_type])
|
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_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
|
||||||
|
|
||||||
|
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 + \
|
||||||
|
payload_data
|
||||||
|
|
||||||
arqframe = frame_type + bytes([static.ARQ_TX_N_FRAMES_PER_BURST]) + static.ARQ_BURST_PAYLOAD_CRC + payload_data
|
print(arqframe)
|
||||||
|
|
||||||
buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer
|
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[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
|
||||||
|
@ -201,8 +224,8 @@ class RF():
|
||||||
signalling_bytes_out = (ctypes.c_ubyte * static.FREEDV_SIGNALLING_BYTES_PER_FRAME)
|
signalling_bytes_out = (ctypes.c_ubyte * static.FREEDV_SIGNALLING_BYTES_PER_FRAME)
|
||||||
signalling_bytes_out = signalling_bytes_out() #get pointer from bytes_out
|
signalling_bytes_out = signalling_bytes_out() #get pointer from bytes_out
|
||||||
|
|
||||||
|
# with this we can interrupt receiving
|
||||||
while 1:
|
while static.FREEDV_RECEIVE == True:
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
while static.ARQ_STATE == 'RECEIVING_DATA':
|
while static.ARQ_STATE == 'RECEIVING_DATA':
|
||||||
|
@ -228,7 +251,7 @@ class RF():
|
||||||
#print(modem_stats_snr)
|
#print(modem_stats_snr)
|
||||||
|
|
||||||
if nbytes == static.FREEDV_DATA_BYTES_PER_FRAME:
|
if nbytes == static.FREEDV_DATA_BYTES_PER_FRAME:
|
||||||
|
|
||||||
# CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------
|
# CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------
|
||||||
frametype = int.from_bytes(bytes(data_bytes_out[:1]), "big")
|
frametype = int.from_bytes(bytes(data_bytes_out[:1]), "big")
|
||||||
frame = frametype - 10
|
frame = frametype - 10
|
||||||
|
@ -265,7 +288,7 @@ class RF():
|
||||||
arq.ack_received()
|
arq.ack_received()
|
||||||
|
|
||||||
rxstatus = self.c_lib.freedv_get_rx_status(freedv_signalling)
|
rxstatus = self.c_lib.freedv_get_rx_status(freedv_signalling)
|
||||||
#print(rxstatus)
|
print(rxstatus)
|
||||||
if nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME or rxstatus == 10:
|
if nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME or rxstatus == 10:
|
||||||
self.c_lib.freedv_set_sync(freedv_signalling, 0) #FORCE UNSYNC
|
self.c_lib.freedv_set_sync(freedv_signalling, 0) #FORCE UNSYNC
|
||||||
|
|
||||||
|
|
13
static.py
13
static.py
|
@ -13,6 +13,7 @@ Created on Wed Dec 23 11:13:57 2020
|
||||||
#AUDIO TIME: 14.750595331192017 #12 # 2 FRAME + PREAMBLE
|
#AUDIO TIME: 14.750595331192017 #12 # 2 FRAME + PREAMBLE
|
||||||
|
|
||||||
# FreeDV Defaults
|
# FreeDV Defaults
|
||||||
|
FREEDV_RECEIVE = True
|
||||||
|
|
||||||
FREEDV_DATA_MODE = 12
|
FREEDV_DATA_MODE = 12
|
||||||
FREEDV_SIGNALLING_MODE = 14
|
FREEDV_SIGNALLING_MODE = 14
|
||||||
|
@ -56,8 +57,18 @@ ARQ_RX_BURST_BUFFER = []
|
||||||
ARQ_RX_FRAME_BUFFER = []
|
ARQ_RX_FRAME_BUFFER = []
|
||||||
ARQ_RX_FRAME_N_BURSTS = 0
|
ARQ_RX_FRAME_N_BURSTS = 0
|
||||||
|
|
||||||
ARQ_N_RX_ARQ_FRAMES = 0 # total number of received frames
|
## TX
|
||||||
|
ARQ_TX_N_CURRENT_ARQ_FRAME = 0
|
||||||
|
ARQ_TX_N_TOTAL_ARQ_FRAMES = 0
|
||||||
|
##
|
||||||
|
|
||||||
|
## RX
|
||||||
ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0 #total number of arq frames per data frame
|
ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0 #total number of arq frames per data frame
|
||||||
|
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_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_PAYLOAD_PER_FRAME = 0 # PAYLOAD per ACK frame
|
||||||
ARQ_ACK_RECEIVED = 0 # set to 1 if ACK received
|
ARQ_ACK_RECEIVED = 0 # set to 1 if ACK received
|
||||||
|
|
Loading…
Reference in a new issue