mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
improved RX
This commit is contained in:
parent
95674ebca7
commit
b52081866f
186
arq.py
186
arq.py
|
@ -18,135 +18,131 @@ import helpers
|
||||||
modem = modem.RF()
|
modem = modem.RF()
|
||||||
|
|
||||||
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 3 #6?!
|
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 3 #6?!
|
||||||
static.ARQ_ACK_PAYLOAD_PER_FRAME = 14 - 2# --> 700D
|
static.ARQ_ACK_PAYLOAD_PER_FRAME = 14 - 2#
|
||||||
|
|
||||||
|
|
||||||
def arq_ack_timeout():
|
def arq_ack_timeout():
|
||||||
static.ACK_TIMEOUT = 1
|
static.ARQ_ACK_TIMEOUT = 1
|
||||||
|
|
||||||
|
|
||||||
def data_received(data_in):
|
def data_received(data_in):
|
||||||
|
|
||||||
static.ARQ_N_RX_FRAMES_PER_BURSTS = int.from_bytes(bytes(data_in[:1]), "big") - 10 #get number of bursts from received frame
|
static.ARQ_N_FRAME = int.from_bytes(bytes(data_in[:1]), "big") - 10 #get number of frame
|
||||||
static.ARQ_RX_BURST_BUFFER.append(data_in) #append data to RX BUFFER
|
static.ARQ_N_RX_FRAMES_PER_BURSTS = int.from_bytes(bytes(data_in[1:2]), "big") #get number of bursts from received frame
|
||||||
|
|
||||||
print("WAITING FOR FRAMS_PER_BURST: " + str(static.ARQ_N_RX_FRAMES_PER_BURSTS))
|
|
||||||
print("ARQ_RX_BURST_BUFFER: " + str(len(static.ARQ_RX_BURST_BUFFER)))
|
|
||||||
|
|
||||||
burst_total_payload = bytearray()
|
logging.info("ARQ | RX | FRAME [" + str(static.ARQ_N_FRAME) + "/" + str(static.ARQ_N_RX_FRAMES_PER_BURSTS) + "] [" + str((static.ARQ_N_FRAME / static.ARQ_N_RX_FRAMES_PER_BURSTS)*100) + "%]")
|
||||||
|
|
||||||
#while static.ACK_RX_TIMEOUT == 0: #define timeout where data has to be received untl error occurs
|
|
||||||
|
|
||||||
if len(static.ARQ_RX_BURST_BUFFER) == static.ARQ_N_RX_FRAMES_PER_BURSTS: #if received bursts are equal to burst number in frame
|
#allocate ARQ_RX_BURST_BUFFER list with 0 if not already done
|
||||||
|
# here we will save the N frame of a burst to N list position so we can explicit search for it
|
||||||
for n_raw_frame in range(0,len(static.ARQ_RX_BURST_BUFFER)):
|
if static.ARQ_N_RX_FRAMES_PER_BURSTS != len(static.ARQ_RX_BURST_BUFFER) and static.ARQ_N_FRAME == 1:
|
||||||
|
print("LIST ALLOCATION!")
|
||||||
burst_frame = static.ARQ_RX_BURST_BUFFER[n_raw_frame] #get burst frame
|
for i in range(0,static.ARQ_N_RX_FRAMES_PER_BURSTS+1):
|
||||||
burst_payload = burst_frame[3:] #remove frame type and burst CRC
|
static.ARQ_RX_BURST_BUFFER.insert(i,None)
|
||||||
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
|
print(len(static.ARQ_RX_BURST_BUFFER))
|
||||||
|
# now we add the incoming data to the specified position in our list
|
||||||
# ------------------ caculate CRC of BURST
|
static.ARQ_RX_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
|
||||||
burst_payload_crc = helpers.get_crc_16(burst_total_payload)
|
|
||||||
|
|
||||||
|
# run if we recieved all frames
|
||||||
|
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
|
||||||
|
print("JETZT GEHTS LOS MIT DER SCHLEIFE")
|
||||||
|
print(len(static.ARQ_RX_BURST_BUFFER)-1)
|
||||||
|
|
||||||
|
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)):
|
||||||
|
# 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[4:] #remove frame type and burst CRC
|
||||||
|
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 CRC IS CORRECT, APPEND BURST TO BUFFER AND SEND ACK FRAME
|
||||||
if burst_payload_crc == data_in[1:3]:
|
if burst_payload_crc == data_in[2:4]:
|
||||||
|
|
||||||
logging.info("BURST CRC ARE EQUAL!")
|
logging.info("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
|
static.ARQ_RX_FRAME_BUFFER.append(burst_total_payload) # IF CRC TRUE APPEND burst_total_payload TO ARQ_RX_FRAME_BUFFER
|
||||||
|
|
||||||
#BUILDING ACK FRAME -----------------------------------------------
|
#BUILDING ACK FRAME -----------------------------------------------
|
||||||
|
|
||||||
ack_payload = bytes(burst_payload_crc)
|
ack_payload = bytes(burst_payload_crc)
|
||||||
#ack_frame = b'\7'+ bytes(burst_payload_crc)
|
ack_frame = b'<'+ bytes(burst_payload_crc) # < = 60
|
||||||
ack_frame = b'<'+ bytes(burst_payload_crc) # < = 60
|
|
||||||
#ack_frame[1] = b'\7'
|
|
||||||
#ack_frame[2] = helpers.get_crc8(data)
|
|
||||||
#ack_frame[3:4] = bytes(burst_payload_crc)
|
|
||||||
|
|
||||||
frametype = int.from_bytes(bytes(ack_frame[:1]), "big")
|
|
||||||
print("ACK TYPE: " + str(frametype))
|
|
||||||
|
|
||||||
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 -----------------------------------------------
|
||||||
logging.info("TX | SENDING ARQ BURST ACK [" + str(data_in[1:3]) +"]")
|
# we need to wait until RX is finished on TX side
|
||||||
time.sleep(4.5)
|
time.sleep(3.8)
|
||||||
|
logging.info("ARQ | TX | SENDING ARQ BURST ACK [" + str(data_in[1:3]) +"]")
|
||||||
modem.transmit_arq_ack(ack_buffer)
|
modem.transmit_arq_ack(ack_buffer)
|
||||||
#print(ack_buffer)
|
|
||||||
|
|
||||||
# ------------------------------------------------------------
|
|
||||||
frametype = int.from_bytes(bytes(ack_buffer[:1]), "big")
|
|
||||||
print("ACK TYPE: " + str(frametype))
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
static.ARQ_RX_BURST_BUFFER = [] # CLEAR RX BURST BUFFER
|
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
|
||||||
print("CRC NOT EQUAL!!!!![" + str(data_in[1:3]) + "]")
|
logging.info("CRC NOT EQUAL!!!!![" + str(data_in[2:4]) + "]")
|
||||||
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
|
# LOOP THOUGH FRAME BUFFER AND STICK EVERYTHING TOGETHER
|
||||||
# WE ALSO CHECK FOR FRAME HEADER AND LAST FRAME
|
# WE ALSO CHECK FOR FRAME HEADER AND LAST FRAME
|
||||||
complete_frame = bytearray()
|
complete_frame = bytearray()
|
||||||
print(static.ARQ_RX_FRAME_BUFFER)
|
#print(static.ARQ_RX_FRAME_BUFFER)
|
||||||
for frame in range(len(static.ARQ_RX_FRAME_BUFFER)):
|
for frame in range(len(static.ARQ_RX_FRAME_BUFFER)):
|
||||||
complete_frame = complete_frame + static.ARQ_RX_FRAME_BUFFER[frame]
|
complete_frame = complete_frame + static.ARQ_RX_FRAME_BUFFER[frame]
|
||||||
#print(complete_frame)
|
|
||||||
#print(complete_frame[4:6])
|
|
||||||
# -------- DETECT IF WE ALREADY RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
|
# -------- DETECT IF WE ALREADY RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
|
||||||
#if burst_total_payload[4:6].startswith(b'\xAA\xAA'):
|
if complete_frame[4:6].startswith(static.FRAME_BOF) or burst_total_payload[4:6].startswith(static.FRAME_BOF): #5:7
|
||||||
|
#print("FRAME BOF RECEIVED")
|
||||||
if complete_frame[4:6].startswith(static.FRAME_BOF) or burst_total_payload[4:6].startswith(static.FRAME_BOF):
|
|
||||||
print("FRAME HEADER RECEIVED!")
|
|
||||||
#print("FRAME BURSTS = " + str(complete_frame[:2]))
|
|
||||||
#print("FRAME CRC = " + str(complete_frame[2:4]))
|
|
||||||
static.FRAME_CRC = complete_frame[2:4]
|
static.FRAME_CRC = complete_frame[2:4]
|
||||||
static.ARQ_RX_FRAME_N_BURSTS = int.from_bytes(bytes(complete_frame[:2]), "big")
|
|
||||||
|
|
||||||
# -------- DETECT IF WE HAVE ALREADY RECEIVED THE LAST FRAME
|
# -------- DETECT IF WE HAVE ALREADY RECEIVED THE LAST FRAME
|
||||||
if burst_total_payload.rstrip(b'\x00').endswith(static.FRAME_EOF):
|
if burst_total_payload.rstrip(b'\x00').endswith(static.FRAME_EOF):
|
||||||
print("EOF RECEIVED")
|
pass
|
||||||
|
#print("FRAME EOF RECEIVED")
|
||||||
|
|
||||||
print("LET'S CONTINUE...")
|
|
||||||
# NOW WE TRY TO SEPARATE THE FRAME CRC FOR A CRC CALCULATION
|
# NOW WE TRY TO SEPARATE THE FRAME CRC FOR A CRC CALCULATION
|
||||||
frame_payload = complete_frame.rstrip(b'\x00') #REMOVE x00
|
frame_payload = complete_frame.rstrip(b'\x00') #REMOVE x00
|
||||||
frame_payload = frame_payload[6:-2] #THIS IS THE FRAME PAYLOAD
|
frame_payload = frame_payload[6:-2] #THIS IS THE FRAME PAYLOAD
|
||||||
frame_payload_crc = helpers.get_crc_16(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("RX | FILE SUCESSFULL RECEIVED! - TIME TO PARTY")
|
logging.info("ARQ | RX | FRAME SUCESSFULLY RECEIVED! - TIME TO PARTY")
|
||||||
static.RX_BUFFER.append(frame_payload)
|
static.RX_BUFFER.append(frame_payload)
|
||||||
static.ARQ_RX_FRAME_BUFFER = []
|
static.ARQ_RX_FRAME_BUFFER = []
|
||||||
#print(static.RX_BUFFER[-1])
|
print("----------------------------------------------------------------")
|
||||||
# HERE: SEND ACK FOR TOTAL FRAME!!!
|
print(static.RX_BUFFER[-1])
|
||||||
|
print("----------------------------------------------------------------")
|
||||||
|
# HERE: WE COULD SEND ACK FOR TOTAL FRAME
|
||||||
#else:
|
#else:
|
||||||
#print("CRC FOR FRAME NOT EQUAL!")
|
logging.info("ARQ | RX | FRAME NOT SUCESSFULLY RECEIVED!")
|
||||||
# print("FRAME PAYLOAD CRC: " + str(frame_payload_crc))
|
print("FRAME PAYLOAD CRC: " + str(frame_payload_crc))
|
||||||
# print("FRAME PAYLOAD: " + str(frame_payload))
|
print("FRAME PAYLOAD: " + str(frame_payload))
|
||||||
#print("COMPLETE FRAME: " + str(complete_frame))
|
print("COMPLETE FRAME: " + str(complete_frame))
|
||||||
#static.ARQ_RX_FRAME_BUFFER = [] # ---> BUFFER ERST LÖSCHEN WENN MINDESTANZAHL AN BURSTS ERHALTEN WORDEN SIND
|
#static.ARQ_RX_FRAME_BUFFER = [] # ---> BUFFER ERST LÖSCHEN WENN MINDESTANZAHL AN BURSTS ERHALTEN WORDEN SIND
|
||||||
|
|
||||||
def ack_received():
|
def ack_received():
|
||||||
|
|
||||||
logging.info("RX | ACK RCVD!")
|
logging.info("ARQ | RX | ACK RCVD!")
|
||||||
static.ARQ_ACK_TIMEOUT = 1 #Force timer to stop waiting
|
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
|
static.ARQ_ACK_RECEIVED = 1 #Force data loops of TNC to stop and continue with next frame
|
||||||
# static.ARQ_ACK_WAITING_FOR_ID
|
|
||||||
|
|
||||||
def transmit(data_out):
|
def transmit(data_out):
|
||||||
|
|
||||||
|
|
||||||
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 3
|
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 4 #3 ohne ARQ_TX_N_FRAMES_PER_BURST
|
||||||
frame_header_length = 8
|
frame_header_length = 8
|
||||||
|
|
||||||
n_bursts_prediction = (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_bursts_prediction = (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)
|
||||||
n_bursts_prediction = n_bursts_prediction.to_bytes(2, byteorder='big') #65535
|
n_bursts_prediction = n_bursts_prediction.to_bytes(2, byteorder='big') #65535
|
||||||
|
|
||||||
frame_payload_crc = helpers.get_crc_16(data_out)
|
frame_payload_crc = helpers.get_crc_16(data_out)
|
||||||
|
@ -154,15 +150,15 @@ def transmit(data_out):
|
||||||
# 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 = n_bursts_prediction + frame_payload_crc + static.FRAME_BOF + data_out + static.FRAME_EOF
|
data_out = n_bursts_prediction + frame_payload_crc + static.FRAME_BOF + data_out + static.FRAME_EOF
|
||||||
# 2 2 2 N 2
|
# 2 2 2 N 2
|
||||||
print(data_out)
|
#print(data_out)
|
||||||
print(len(data_out))
|
#print(len(data_out))
|
||||||
print(static.ARQ_PAYLOAD_PER_FRAME)
|
#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)
|
||||||
print(static.TX_BUFFER)
|
#print(static.TX_BUFFER)
|
||||||
|
|
||||||
logging.info("TX | TOTAL PAYLOAD BYTES/FRAMES TO SEND: " + str(len(data_out)) + " / " + str(static.TX_BUFFER_SIZE))
|
logging.info("ARQ | TX | TOTAL PAYLOAD BYTES/FRAMES TO SEND: " + str(len(data_out)) + " / " + str(static.TX_BUFFER_SIZE))
|
||||||
|
|
||||||
# --------------------------------------------- THIS IS THE MAIN LOOP-----------------------------------------------------------------
|
# --------------------------------------------- THIS IS THE MAIN LOOP-----------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -204,7 +200,7 @@ 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)
|
||||||
|
@ -212,7 +208,9 @@ def transmit(data_out):
|
||||||
#--------------------------------------------- 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):
|
||||||
print("SENDING")
|
print("SENDING")
|
||||||
|
#reset timer and ack state
|
||||||
|
static.ARQ_ACK_RECEIVED = 0
|
||||||
|
static.ARQ_ACK_TIMEOUT = 0
|
||||||
# lets start a thread to transmit nonblocking
|
# 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 = threading.Thread(target=modem.transmit_arq_burst, name="TRANSMIT_ARQ_BURST")
|
||||||
TRANSMIT_ARQ_BURST_THREAD.start()
|
TRANSMIT_ARQ_BURST_THREAD.start()
|
||||||
|
@ -220,32 +218,43 @@ def transmit(data_out):
|
||||||
# lets sleep a while during sending. After this we will continue
|
# lets sleep a while during sending. After this we will continue
|
||||||
while static.ARQ_STATE == 'SENDING_DATA':
|
while static.ARQ_STATE == 'SENDING_DATA':
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
#print("WAIT WHILE SENDIN")
|
|
||||||
|
|
||||||
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
|
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
|
||||||
logging.info("TX | WAITING FOR ACK")
|
logging.info("ARQ | WAITING FOR ACK")
|
||||||
static.ARQ_ACK_TIMEOUT = 0
|
|
||||||
static.ARQ_STATE = 'RECEIVING_ACK'
|
static.ARQ_STATE = 'RECEIVING_ACK'
|
||||||
timer = threading.Timer(static.ARQ_ACK_TIMEOUT_SECONDS * static.ARQ_TX_N_FRAMES_PER_BURST, 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
|
#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
|
||||||
|
|
||||||
|
|
||||||
#--------------- 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
|
||||||
static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST
|
static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
|
||||||
|
logging.info("ARQ | ACK TIMEOUT | SENDING BURST AGAIN")
|
||||||
|
|
||||||
# ----------- if no ACK received and out of retries.....stop frame sending
|
# ----------- if no ACK received and out of retries.....stop frame sending
|
||||||
if static.ARQ_ACK_RECEIVED == 0:
|
if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
|
||||||
logging.info("TX | NO ACK RECEIVED | FRAME NEEDS TO BE RESEND!")
|
logging.info("ARQ | TX | NO ACK RECEIVED | DATA FRAME NEEDS TO BE RESEND!")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT
|
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT
|
||||||
|
@ -253,15 +262,18 @@ def transmit(data_out):
|
||||||
break
|
break
|
||||||
|
|
||||||
# ------------ TIMER TO WAIT UNTIL NEXT PACKAGE WILL BE SEND TO PREVENT TIME ISSEUS --> NEEDS TO BE IMPROVED LATER
|
# ------------ TIMER TO WAIT UNTIL NEXT PACKAGE WILL BE SEND TO PREVENT TIME ISSEUS --> NEEDS TO BE IMPROVED LATER
|
||||||
time.sleep(3)
|
#time.sleep(3)
|
||||||
|
|
||||||
# IF TX BUFFER IS EMPTY / ALL FRAMES HAVE BEEN SENT --> HERE WE COULD ADD AN static.VAR for IDLE STATE
|
# IF TX BUFFER IS EMPTY / ALL FRAMES HAVE BEEN SENT --> HERE WE COULD ADD AN static.VAR for IDLE STATE
|
||||||
logging.info("TX | BUFFER EMPTY")
|
#logging.info("ARQ | TX | FRAME SUCESSFULLY TRANSMITTED! - TIME TO PARTY")
|
||||||
|
logging.info("ARQ | TX | BUFFER EMPTY")
|
||||||
print(static.ARQ_N_SENT_FRAMES)
|
print(static.ARQ_N_SENT_FRAMES)
|
||||||
print(static.ARQ_TX_N_FRAMES_PER_BURST)
|
print(static.ARQ_TX_N_FRAMES_PER_BURST)
|
||||||
# - RESET COUNTERS
|
# - RESET COUNTERS
|
||||||
static.ARQ_N_SENT_FRAMES = 0
|
static.ARQ_N_SENT_FRAMES = 0
|
||||||
static.ARQ_TX_N_FRAMES_PER_BURST = 0
|
static.ARQ_TX_N_FRAMES_PER_BURST = 0
|
||||||
|
static.ARQ_ACK_RECEIVED = 0
|
||||||
|
|
||||||
|
|
||||||
# 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():
|
||||||
|
|
86
modem.py
86
modem.py
|
@ -31,7 +31,7 @@ class RF():
|
||||||
#static.AUDIO_SAMPLE_RATE_RX = int(self.p.get_device_info_by_index(static.AUDIO_INPUT_DEVICE)['defaultSampleRate'])
|
#static.AUDIO_SAMPLE_RATE_RX = int(self.p.get_device_info_by_index(static.AUDIO_INPUT_DEVICE)['defaultSampleRate'])
|
||||||
#static.AUDIO_SAMPLE_RATE_TX = int(self.p.get_device_info_by_index(static.AUDIO_OUTPUT_DEVICE)['defaultSampleRate'])
|
#static.AUDIO_SAMPLE_RATE_TX = int(self.p.get_device_info_by_index(static.AUDIO_OUTPUT_DEVICE)['defaultSampleRate'])
|
||||||
static.AUDIO_SAMPLE_RATE_TX = 8000
|
static.AUDIO_SAMPLE_RATE_TX = 8000
|
||||||
static.AUDIO_SAMPLE_RATE_TX = 8000
|
static.AUDIO_SAMPLE_RATE_RX = 8000
|
||||||
#--------------------------------------------OPEN AUDIO CHANNEL RX
|
#--------------------------------------------OPEN AUDIO CHANNEL RX
|
||||||
self.stream_rx = self.p.open(format=pyaudio.paInt16,
|
self.stream_rx = self.p.open(format=pyaudio.paInt16,
|
||||||
channels=static.AUDIO_CHANNELS,
|
channels=static.AUDIO_CHANNELS,
|
||||||
|
@ -89,9 +89,8 @@ class RF():
|
||||||
txbuffer = bytearray()
|
txbuffer = bytearray()
|
||||||
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')
|
||||||
# -------------- audio sample rate conversion
|
|
||||||
|
|
||||||
# -------------- transmit audio
|
# -------------- transmit audio
|
||||||
self.stream_tx.write(bytes(txbuffer))
|
self.stream_tx.write(bytes(txbuffer))
|
||||||
|
|
||||||
|
@ -109,7 +108,7 @@ class RF():
|
||||||
|
|
||||||
mod_out = ctypes.c_short * n_tx_modem_samples
|
mod_out = ctypes.c_short * n_tx_modem_samples
|
||||||
mod_out = mod_out()
|
mod_out = mod_out()
|
||||||
mod_out_preamble = ctypes.c_short * (n_tx_modem_samples*2) #1760 for mode 10,11,12 #4000 for mode 9
|
mod_out_preamble = ctypes.c_short * (1760*2) #1760 for mode 10,11,12 #4000 for mode 9
|
||||||
mod_out_preamble = mod_out_preamble()
|
mod_out_preamble = mod_out_preamble()
|
||||||
|
|
||||||
buffer = bytearray(payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3)
|
buffer = bytearray(payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3)
|
||||||
|
@ -118,18 +117,18 @@ class RF():
|
||||||
crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), payload_per_frame)) # generate CRC16
|
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
|
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
|
||||||
buffer += crc # append crc16 to buffer
|
buffer += crc # append crc16 to buffer
|
||||||
print(bytes(buffer))
|
#print(bytes(buffer))
|
||||||
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
||||||
|
|
||||||
self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
preamble_bytes = 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
|
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
|
||||||
|
|
||||||
txbuffer = bytearray()
|
txbuffer = bytearray()
|
||||||
txbuffer += bytes(mod_out_preamble)
|
txbuffer += bytes(mod_out_preamble)
|
||||||
|
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')
|
txbuffer = txbuffer.rstrip(b'\x00') #lets remove unallocated memory because of wrong buffer :-/
|
||||||
|
|
||||||
# -------------- transmit audio twice
|
# -------------- transmit audio
|
||||||
self.stream_tx.write(bytes(txbuffer))
|
self.stream_tx.write(bytes(txbuffer))
|
||||||
|
|
||||||
static.ARQ_STATE = 'RECEIVING_DATA'
|
static.ARQ_STATE = 'RECEIVING_DATA'
|
||||||
|
@ -143,34 +142,28 @@ class RF():
|
||||||
freedv = self.c_lib.freedv_open(static.FREEDV_DATA_MODE)
|
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)
|
static.FREEDV_DATA_BYTES_PER_FRAME = int(self.c_lib.freedv_get_bits_per_modem_frame(freedv)/8)
|
||||||
static.FREEDV_DATA_PAYLOAD_PER_FRAME = static.FREEDV_DATA_BYTES_PER_FRAME -2
|
static.FREEDV_DATA_PAYLOAD_PER_FRAME = static.FREEDV_DATA_BYTES_PER_FRAME -2
|
||||||
|
|
||||||
print(static.FREEDV_DATA_BYTES_PER_FRAME)
|
|
||||||
|
|
||||||
n_nom_modem_samples = self.c_lib.freedv_get_n_nom_modem_samples(freedv)
|
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
|
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 = ctypes.c_short * n_tx_modem_samples
|
||||||
mod_out = mod_out()
|
mod_out = mod_out()
|
||||||
mod_out_preamble = ctypes.c_short * (1760*2) #1760 for mode 10,11,12 #4000 for mode 9
|
mod_out_preamble = ctypes.c_short * (n_tx_modem_samples*2) #1760 for mode 10,11,12 #4000 for mode 9
|
||||||
mod_out_preamble = mod_out_preamble()
|
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 = bytearray()
|
||||||
txbuffer += bytes(mod_out_preamble)
|
txbuffer += bytes(mod_out_preamble)
|
||||||
|
|
||||||
time_start = time.time()
|
|
||||||
for n in range(0,static.ARQ_TX_N_FRAMES_PER_BURST):
|
for n in range(0,static.ARQ_TX_N_FRAMES_PER_BURST):
|
||||||
|
|
||||||
#---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
#---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
||||||
frame_type = 10 + static.ARQ_TX_N_FRAMES_PER_BURST
|
frame_type = 10 + n + 1 #static.ARQ_TX_N_FRAMES_PER_BURST
|
||||||
frame_type = bytes([frame_type])
|
frame_type = bytes([frame_type])
|
||||||
print(frame_type)
|
|
||||||
print("static.ARQ_N_SENT_FRAMES: " + str(static.ARQ_N_SENT_FRAMES))
|
|
||||||
print("static.ARQ_TX_N_FRAMES_PER_BURST: " + str(static.ARQ_TX_N_FRAMES_PER_BURST))
|
|
||||||
|
|
||||||
payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + n])
|
payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + n])
|
||||||
|
|
||||||
arqframe = frame_type + 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
|
||||||
|
|
||||||
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
|
||||||
|
@ -191,9 +184,6 @@ class RF():
|
||||||
#--------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------
|
||||||
def receive(self,data_mode,signalling_mode):
|
def receive(self,data_mode,signalling_mode):
|
||||||
|
|
||||||
print("RECEIVING FOR DATA MODE: " + str(data_mode))
|
|
||||||
print("RECEIVING FOR SIGNALLING MODE: " + str(signalling_mode))
|
|
||||||
|
|
||||||
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
||||||
|
|
||||||
freedv_data = self.c_lib.freedv_open(data_mode)
|
freedv_data = self.c_lib.freedv_open(data_mode)
|
||||||
|
@ -227,7 +217,7 @@ class RF():
|
||||||
|
|
||||||
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
|
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))
|
#print(self.c_lib.freedv_get_rx_status(freedv_data))
|
||||||
|
|
||||||
#modem_stats_snr = c_float()
|
#modem_stats_snr = c_float()
|
||||||
#modem_stats_sync = c_int()
|
#modem_stats_sync = c_int()
|
||||||
|
@ -236,30 +226,24 @@ class RF():
|
||||||
#modem_stats_snr = modem_stats_snr.value
|
#modem_stats_snr = modem_stats_snr.value
|
||||||
#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
|
||||||
|
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
|
if 50 >= frametype >= 10 and len(data_bytes_out) > 30: # --> The length check filters out random strings without CRC
|
||||||
print("MODE: " + str(data_mode) + " DATA: " + str(bytes(data_bytes_out[:-2])))
|
#print("ARQ | RX | FRAME [" + str(frame) + "/" + str(n_frames_per_burst) + "]")
|
||||||
arq.data_received(bytes(data_bytes_out[:-2])) #send payload data to arq checker without CRC16
|
arq.data_received(bytes(data_bytes_out[:-2])) #send payload data to arq checker without CRC16
|
||||||
|
|
||||||
|
|
||||||
#self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
|
||||||
else:
|
else:
|
||||||
print("MODE: " + str(data_mode) + " DATA: " + str(bytes(data_bytes_out)))
|
print("MODE: " + str(data_mode) + " DATA: " + str(bytes(data_bytes_out)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# NEEDS TO BE OPTIMIZED
|
# NEEDS TO BE OPTIMIZED
|
||||||
# DO UNSYNC IF LAST FRAME PER BURST SENT
|
# DO UNSYNC AFTER LAST BURST
|
||||||
if len(static.ARQ_RX_BURST_BUFFER) == static.ARQ_N_RX_FRAMES_PER_BURSTS and len(static.ARQ_RX_BURST_BUFFER) != 0:
|
if frame == n_frames_per_burst:
|
||||||
print("DOING UNSYNC")
|
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
||||||
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_ACK':
|
while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_ACK':
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
@ -272,22 +256,14 @@ class RF():
|
||||||
|
|
||||||
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
|
nbytes = self.c_lib.freedv_rawdatarx(freedv_signalling, signalling_bytes_out, data_in) # demodulate audio
|
||||||
|
|
||||||
|
# CHECK IF FRAME CONTAINS ACK------------------------
|
||||||
# CHECK IF FRAME CONTAINS ACK------------------------ --> 700D / 7
|
|
||||||
|
|
||||||
frametype = int.from_bytes(bytes(signalling_bytes_out[:1]), "big")
|
frametype = int.from_bytes(bytes(signalling_bytes_out[:1]), "big")
|
||||||
if frametype == 60 and len(signalling_bytes_out) == static.FREEDV_SIGNALLING_BYTES_PER_FRAME:
|
if frametype == 60 and nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME:
|
||||||
arq.ack_received()
|
arq.ack_received()
|
||||||
# print("ACK FRAME RECEIVED!!!!!!!!!!")
|
|
||||||
#if bytes(bytes_out[:1]) == b'<': #b'\7': < = 60
|
rxstatus = self.c_lib.freedv_get_rx_status(freedv_signalling)
|
||||||
# CHECK CRC 8 OF ACK FRAME
|
#print(rxstatus)
|
||||||
|
if nbytes == static.FREEDV_SIGNALLING_BYTES_PER_FRAME or rxstatus == 10:
|
||||||
# print(signalling_bytes_out[:1])
|
self.c_lib.freedv_set_sync(freedv_signalling, 0) #FORCE UNSYNC
|
||||||
# print(signalling_bytes_out[3:14])
|
|
||||||
|
|
||||||
# if bytes(signalling_bytes_out[:2]) == helpers.get_crc_8(bytes(signalling_bytes_out[3:14])):
|
|
||||||
# print("MODE: " + str(signalling_mode) + " DATA: " + str(bytes(signalling_bytes_out)))
|
|
||||||
# arq.ack_received()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue