mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
improved RPT
Now working, but still timing issues and stuck in sync
This commit is contained in:
parent
6adf7db483
commit
0a8ea24112
3 changed files with 103 additions and 180 deletions
125
arq.py
125
arq.py
|
@ -39,13 +39,8 @@ def data_received(data_in):
|
||||||
# payload_data # N [8:N] # payload data
|
# payload_data # N [8:N] # payload data
|
||||||
|
|
||||||
|
|
||||||
# detect if we got a RPT frame
|
|
||||||
if int.from_bytes(bytes(data_in[:1]), "big") == 51:
|
|
||||||
frame_type = int.from_bytes(bytes(data_in[:1]), "big")
|
|
||||||
else:
|
|
||||||
frame_type = 10
|
|
||||||
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_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_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_N_ARQ_FRAMES_PER_DATA_FRAME = int.from_bytes(bytes(data_in[4:6]), "big") # get get total number of frames
|
||||||
|
@ -70,33 +65,32 @@ def data_received(data_in):
|
||||||
|
|
||||||
#allocate ARQ_RX_FRAME_BUFFER as a list with "None" if not already done. This should be done only once per burst!
|
#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
|
# 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:
|
|
||||||
|
try:
|
||||||
|
static.ARQ_RX_FRAME_BUFFER[static.ARQ_RX_N_CURRENT_ARQ_FRAME] = bytes(data_in)
|
||||||
|
|
||||||
|
except IndexError:
|
||||||
|
|
||||||
static.ARQ_RX_FRAME_BUFFER = []
|
static.ARQ_RX_FRAME_BUFFER = []
|
||||||
for i in range(0,static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME+1):
|
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.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!
|
static.ARQ_RX_FRAME_BUFFER[static.ARQ_RX_N_CURRENT_ARQ_FRAME] = bytes(data_in)
|
||||||
# here we will save the N frame of a burst to N list position so we can explicit search for it
|
|
||||||
print(len(static.ARQ_RX_BURST_BUFFER))
|
|
||||||
print(static.ARQ_N_FRAME)
|
|
||||||
|
|
||||||
|
|
||||||
if static.ARQ_N_RX_FRAMES_PER_BURSTS != len(static.ARQ_RX_BURST_BUFFER) and static.ARQ_N_FRAME == 1 and frame_type != 51:
|
try:
|
||||||
|
static.ARQ_RX_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
|
||||||
|
|
||||||
|
except IndexError:
|
||||||
|
|
||||||
static.ARQ_RX_BURST_BUFFER = []
|
static.ARQ_RX_BURST_BUFFER = []
|
||||||
print("ITERATOR!!!!!!!!!!!!!!")
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
# 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_BURST_BUFFER[static.ARQ_N_FRAME] = bytes(data_in)
|
||||||
static.ARQ_RX_FRAME_BUFFER[static.ARQ_RX_N_CURRENT_ARQ_FRAME] = bytes(data_in)
|
|
||||||
|
|
||||||
for i in range(len(static.ARQ_RX_BURST_BUFFER)):
|
|
||||||
print(static.ARQ_RX_BURST_BUFFER[i])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#for i in range(len(static.ARQ_RX_BURST_BUFFER)):
|
||||||
|
# print(static.ARQ_RX_BURST_BUFFER[i])
|
||||||
|
|
||||||
# - ------------------------- ARQ BURST CHECKER
|
# - ------------------------- ARQ BURST CHECKER
|
||||||
|
|
||||||
|
@ -108,60 +102,43 @@ def data_received(data_in):
|
||||||
#BUILDING ACK FRAME FOR BURST -----------------------------------------------
|
#BUILDING ACK FRAME FOR BURST -----------------------------------------------
|
||||||
ack_payload = b'ACK FRAME'
|
ack_payload = b'ACK FRAME'
|
||||||
ack_frame = b'<' + ack_payload # < = 60
|
ack_frame = b'<' + ack_payload # < = 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-----------------------------------------------
|
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
|
||||||
modem.transmit_arq_ack(ack_buffer)
|
modem.transmit_arq_ack(ack_frame)
|
||||||
|
|
||||||
#clear burst buffer
|
#clear burst buffer
|
||||||
static.ARQ_RX_BURST_BUFFER = []
|
static.ARQ_RX_BURST_BUFFER = []
|
||||||
|
|
||||||
#if decoded N frames are unequal to expected frames per burst
|
#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:
|
elif static.ARQ_N_FRAME == static.ARQ_N_RX_FRAMES_PER_BURSTS and static.ARQ_RX_BURST_BUFFER.count(None) != 1:
|
||||||
print("RPT FRAME NEEDS TO BE SEND!!!!")
|
|
||||||
# --------------- CHECK WHICH BURST FRAMES WE ARE MISSING -------------------------------------------
|
# --------------- CHECK WHICH BURST FRAMES WE ARE MISSING -------------------------------------------
|
||||||
|
|
||||||
missing_frames = b''
|
missing_frames = b''
|
||||||
for burst in range(1,len(static.ARQ_RX_BURST_BUFFER)):
|
for burst in range(1,len(static.ARQ_RX_BURST_BUFFER)):
|
||||||
|
|
||||||
if static.ARQ_RX_BURST_BUFFER[burst] == None:
|
if static.ARQ_RX_BURST_BUFFER[burst] == None:
|
||||||
print(burst)
|
|
||||||
burst = burst.to_bytes(2, byteorder='big')
|
burst = burst.to_bytes(2, byteorder='big')
|
||||||
missing_frames += burst
|
missing_frames += burst
|
||||||
|
|
||||||
|
logging.info("ARQ | TX | RPT ARQ FRAMES [" + str(missing_frames) + "]")
|
||||||
|
|
||||||
print("MISSING FRAMES: " + str(missing_frames))
|
#BUILDING RPT FRAME FOR BURST -----------------------------------------------
|
||||||
|
|
||||||
rpt_payload = missing_frames
|
rpt_payload = missing_frames
|
||||||
rpt_frame = b'>' + rpt_payload #> = 63
|
rpt_frame = b'>' + rpt_payload #> = 63
|
||||||
rpt_buffer = bytearray(static.ARQ_ACK_PAYLOAD_PER_FRAME)
|
|
||||||
rpt_buffer[:len(rpt_frame)] = rpt_frame # set buffersize to length of data which will be send
|
|
||||||
|
|
||||||
modem.transmit_arq_ack(rpt_buffer)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#TRANSMIT RPT FRAME FOR BURST-----------------------------------------------
|
||||||
|
modem.transmit_arq_ack(rpt_frame)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------- FRAME MACHINE
|
# ---------------------------- 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
|
# --------------- IF LIST NOT CONTAINS "None" stick everything together
|
||||||
complete_data_frame = bytearray()
|
complete_data_frame = bytearray()
|
||||||
print("static.ARQ_RX_FRAME_BUFFER.count(None)" + str(static.ARQ_RX_FRAME_BUFFER.count(None)))
|
#print("static.ARQ_RX_FRAME_BUFFER.count(None)" + str(static.ARQ_RX_FRAME_BUFFER.count(None)))
|
||||||
if static.ARQ_RX_FRAME_BUFFER.count(None) == 1: ## 1 because position 0 of list will alaways be None in our case
|
if static.ARQ_RX_FRAME_BUFFER.count(None) == 1: ## 1 because position 0 of list will alaways be None in our case
|
||||||
print("DECODING FRAME!")
|
#print("DECODING FRAME!")
|
||||||
for frame in range(1,len(static.ARQ_RX_FRAME_BUFFER)):
|
for frame in range(1,len(static.ARQ_RX_FRAME_BUFFER)):
|
||||||
raw_arq_frame = static.ARQ_RX_FRAME_BUFFER[frame]
|
raw_arq_frame = static.ARQ_RX_FRAME_BUFFER[frame]
|
||||||
arq_frame_payload = raw_arq_frame[8:]
|
arq_frame_payload = raw_arq_frame[8:]
|
||||||
|
@ -200,12 +177,11 @@ def data_received(data_in):
|
||||||
#BUILDING ACK FRAME FOR DATA FRAME -----------------------------------------------
|
#BUILDING ACK FRAME FOR DATA FRAME -----------------------------------------------
|
||||||
|
|
||||||
ack_frame = b'='+ bytes(static.FRAME_CRC) # < = 61
|
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
|
|
||||||
|
|
||||||
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
|
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
|
||||||
logging.info("ARQ | TX | ARQ DATA FRAME ACK [" + str(static.FRAME_CRC.hex()) +"]")
|
logging.info("ARQ | TX | ARQ DATA FRAME ACK [" + str(static.FRAME_CRC.hex()) +"]")
|
||||||
modem.transmit_arq_ack(ack_buffer)
|
time.sleep(0.5)
|
||||||
|
modem.transmit_arq_ack(ack_frame)
|
||||||
|
|
||||||
# clearing buffers and resetting counters
|
# clearing buffers and resetting counters
|
||||||
static.ARQ_RX_BURST_BUFFER = []
|
static.ARQ_RX_BURST_BUFFER = []
|
||||||
|
@ -221,27 +197,33 @@ def data_received(data_in):
|
||||||
else:
|
else:
|
||||||
logging.info("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!")
|
logging.info("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!")
|
||||||
|
|
||||||
#break
|
|
||||||
|
|
||||||
def burst_ack_received():
|
def burst_ack_received():
|
||||||
|
|
||||||
logging.info("ARQ | RX | BURST ACK RCVD!")
|
logging.info("ARQ | RX | BURST 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.RPT_ACK_RECEIVED = False
|
|
||||||
|
static.ARQ_RPT_TIMEOUT = True #Force timer to stop waiting
|
||||||
|
static.ARQ_RPT_RECEIVED = False
|
||||||
|
static.ARQ_RPT_FRAMES = []
|
||||||
|
|
||||||
def burst_rpt_received(data_in):
|
def burst_rpt_received(data_in):
|
||||||
|
|
||||||
logging.info("ARQ | RX | BURST RPT RCVD!")
|
logging.info("ARQ | RX | BURST RPT RCVD!")
|
||||||
static.ARQ_ACK_TIMEOUT = 0 #Force timer to stop waiting
|
static.ARQ_ACK_TIMEOUT = 0 #Force timer to stop waiting
|
||||||
static.ARQ_ACK_RECEIVED = 0 #Force data loops of TNC to stop and continue with next frame
|
static.ARQ_ACK_RECEIVED = 0 #Force data loops of TNC to stop and continue with next frame
|
||||||
|
|
||||||
static.ARQ_RPT_RECEIVED = True
|
static.ARQ_RPT_RECEIVED = True
|
||||||
#static.ARQ_RPT_FRAMES = b'123'
|
static.ARQ_RPT_FRAMES = []
|
||||||
missing = 3
|
|
||||||
missing = missing.to_bytes(2, byteorder='big')
|
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)
|
static.ARQ_RPT_FRAMES.insert(0,missing)
|
||||||
|
|
||||||
print(bytes(data_in))
|
|
||||||
|
|
||||||
|
|
||||||
def frame_ack_received():
|
def frame_ack_received():
|
||||||
|
@ -249,7 +231,8 @@ def frame_ack_received():
|
||||||
logging.info("ARQ | RX | FRAME ACK RCVD!")
|
logging.info("ARQ | RX | FRAME ACK RCVD!")
|
||||||
static.ARQ_ACK_TIMEOUT = 1 #Force timer to stop waiting
|
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
|
static.ARQ_FRAME_ACK_RECEIVED = 1 #Force data loops of TNC to stop and continue with next frame
|
||||||
|
static.RPT_ACK_RECEIVED = False
|
||||||
|
static.ARQ_RPT_TIMEOUT = True
|
||||||
|
|
||||||
def transmit(data_out):
|
def transmit(data_out):
|
||||||
|
|
||||||
|
@ -310,6 +293,7 @@ def transmit(data_out):
|
||||||
# lets wait during sending. After sending is finished we will continue
|
# lets wait during sending. After sending is finished we will continue
|
||||||
while static.ARQ_STATE == 'SENDING_DATA':
|
while static.ARQ_STATE == 'SENDING_DATA':
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
|
print("sending.....")
|
||||||
|
|
||||||
|
|
||||||
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
|
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
|
||||||
|
@ -325,14 +309,14 @@ def transmit(data_out):
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
# --------------------------- 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_RPT_RECEIVED == False and static.ARQ_ACK_RECEIVED == 0:
|
while (static.ARQ_ACK_RECEIVED == 0 or static.ARQ_RPT_RECEIVED == False or static.ARQ_FRAME_ACK_RECEIVED == 0) and static.ARQ_TIMEOUT_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)
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
if static.ARQ_RPT_RECEIVED == True:
|
if static.ARQ_RPT_RECEIVED == True:
|
||||||
print("lets send a rpt packet")
|
|
||||||
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()
|
||||||
# lets wait during sending. After sending is finished we will continue
|
# lets wait during sending. After sending is finished we will continue
|
||||||
|
@ -342,34 +326,32 @@ def transmit(data_out):
|
||||||
static.ARQ_FRAME_ACK_RECEIVED = 0
|
static.ARQ_FRAME_ACK_RECEIVED = 0
|
||||||
static.ARQ_ACK_RECEIVED = 0
|
static.ARQ_ACK_RECEIVED = 0
|
||||||
static.ARQ_ACK_TIMEOUT = 0
|
static.ARQ_ACK_TIMEOUT = 0
|
||||||
|
static.ARQ_RPT_TIMEOUT = False
|
||||||
|
|
||||||
static.ARQ_STATE = 'RECEIVING_ACK'
|
static.ARQ_STATE = 'RECEIVING_ACK'
|
||||||
|
|
||||||
timer = threading.Timer(static.ARQ_RPT_TIMEOUT_SECONDS, arq_rpt_timeout)
|
timer = threading.Timer(static.ARQ_RPT_TIMEOUT_SECONDS, arq_rpt_timeout)
|
||||||
timer.start()
|
timer.start()
|
||||||
print("kommen wir hier überhaupt an?")
|
|
||||||
while static.ARQ_ACK_TIMEOUT == 0 and static.ARQ_ACK_RECEIVED == 0 and static.ARQ_RPT_TIMEOUT == False:
|
while static.ARQ_ACK_RECEIVED == 0 and static.ARQ_RPT_TIMEOUT == False:
|
||||||
time.sleep(0.01) # lets reduce CPU load a little bit
|
time.sleep(0.01) # lets reduce CPU load a little bit
|
||||||
print("waiting for ack while rpt")
|
|
||||||
if static.ARQ_ACK_RECEIVED == 1:
|
if static.ARQ_ACK_RECEIVED == 1:
|
||||||
|
|
||||||
print("ACK WHILE RPT")
|
print("ACK WHILE RPT")
|
||||||
time.sleep(1)
|
|
||||||
static.ARQ_ACK_TIMEOUT = 1
|
|
||||||
static.ARQ_RPT_RECEIVED = False
|
|
||||||
static.ARQ_RPT_TIMEOUT == False
|
|
||||||
|
|
||||||
break
|
static.ARQ_ACK_TIMEOUT = 1
|
||||||
|
static.ARQ_RPT_TIMEOUT = True
|
||||||
|
#break
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1 and static.ARQ_RPT_TIMEOUT == True:
|
#if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
|
||||||
#logging.info("ARQ | RX | ACK TIMEOUT | SENDING ARQ BURST AGAIN")
|
# #logging.info("ARQ | RX | ACK TIMEOUT | SENDING ARQ BURST AGAIN")
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED OR FRAME ACK HAS BEEN RECEIVED
|
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED
|
||||||
if static.ARQ_ACK_RECEIVED == 1:
|
if static.ARQ_ACK_RECEIVED == 1:
|
||||||
print("der interator increment ist wichtig!")
|
|
||||||
#-----------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
|
||||||
|
@ -384,7 +366,6 @@ def transmit(data_out):
|
||||||
# ----------- 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 and static.ARQ_FRAME_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
|
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!")
|
logging.info("ARQ | TX | NO BURST OR FRAME ACK RECEIVED | DATA SHOULD BE RESEND!")
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT AND WE GOT A FRAME ACK
|
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT AND WE GOT A FRAME ACK
|
||||||
|
|
104
modem.py
104
modem.py
|
@ -56,53 +56,10 @@ class RF():
|
||||||
#--------------------------------------------START DECODER THREAD
|
#--------------------------------------------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 = threading.Thread(target=self.receive, args=[static.FREEDV_DATA_MODE,static.FREEDV_SIGNALLING_MODE], name="FREEDV_DECODER_THREAD")
|
||||||
FREEDV_DECODER_THREAD.start()
|
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):
|
def transmit_arq_ack(self,ack_buffer):
|
||||||
|
#print(ack_buffer)
|
||||||
static.ARQ_STATE = 'SENDING_ACK'
|
static.ARQ_STATE = 'SENDING_ACK'
|
||||||
|
|
||||||
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
||||||
|
@ -123,7 +80,6 @@ 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))
|
|
||||||
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
||||||
|
|
||||||
preamble_bytes = self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
preamble_bytes = self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
||||||
|
@ -143,7 +99,6 @@ class RF():
|
||||||
# GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT
|
# GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT
|
||||||
def transmit_arq_burst(self):
|
def transmit_arq_burst(self):
|
||||||
static.ARQ_STATE = 'SENDING_DATA'
|
static.ARQ_STATE = 'SENDING_DATA'
|
||||||
time.sleep(3)
|
|
||||||
|
|
||||||
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
||||||
freedv = self.c_lib.freedv_open(static.FREEDV_DATA_MODE)
|
freedv = self.c_lib.freedv_open(static.FREEDV_DATA_MODE)
|
||||||
|
@ -203,9 +158,8 @@ class RF():
|
||||||
elif static.ARQ_RPT_RECEIVED == True:
|
elif static.ARQ_RPT_RECEIVED == True:
|
||||||
for n in range(0,len(static.ARQ_RPT_FRAMES)):
|
for n in range(0,len(static.ARQ_RPT_FRAMES)):
|
||||||
|
|
||||||
|
|
||||||
missing_frame = int.from_bytes(static.ARQ_RPT_FRAMES[n], "big")
|
missing_frame = int.from_bytes(static.ARQ_RPT_FRAMES[n], "big")
|
||||||
print("MISSING_FRAME: " + str(missing_frame))
|
print("MISSING ARQ FRAME: " + str(missing_frame))
|
||||||
#---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
#---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
||||||
frame_type = 10 + missing_frame #static.ARQ_TX_N_FRAMES_PER_BURST
|
frame_type = 10 + missing_frame #static.ARQ_TX_N_FRAMES_PER_BURST
|
||||||
frame_type = bytes([frame_type])
|
frame_type = bytes([frame_type])
|
||||||
|
@ -226,8 +180,6 @@ class RF():
|
||||||
static.MYCALLSIGN_CRC8 + \
|
static.MYCALLSIGN_CRC8 + \
|
||||||
payload_data
|
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
|
||||||
|
|
||||||
|
@ -241,12 +193,6 @@ 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 :-/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# -------------- transmit audio
|
# -------------- transmit audio
|
||||||
self.stream_tx.write(bytes(txbuffer))
|
self.stream_tx.write(bytes(txbuffer))
|
||||||
|
|
||||||
|
@ -281,8 +227,6 @@ class RF():
|
||||||
stuck_in_sync_10_counter = 0
|
stuck_in_sync_10_counter = 0
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while static.ARQ_STATE == 'RECEIVING_DATA':
|
while static.ARQ_STATE == 'RECEIVING_DATA':
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
@ -296,7 +240,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))
|
||||||
|
|
||||||
|
|
||||||
#-------------STUCK IN SYNC DETECTOR
|
#-------------STUCK IN SYNC DETECTOR
|
||||||
|
@ -306,12 +250,12 @@ class RF():
|
||||||
|
|
||||||
|
|
||||||
#print(stuck_in_sync_counter)
|
#print(stuck_in_sync_counter)
|
||||||
if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv_data) == 10:
|
#if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv_data) == 10:
|
||||||
print("stuck in sync #1 --> DOING UNSYNC")
|
# print("stuck in sync #1 --> DOING UNSYNC")
|
||||||
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
# self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
||||||
stuck_in_sync_counter = 0
|
# stuck_in_sync_counter = 0
|
||||||
stuck_in_sync_10_counter = 0
|
# stuck_in_sync_10_counter = 0
|
||||||
data_in = None
|
# data_in = None
|
||||||
|
|
||||||
|
|
||||||
if stuck_in_sync_counter >= 66 and stuck_in_sync_10_counter >= 2:
|
if stuck_in_sync_counter >= 66 and stuck_in_sync_10_counter >= 2:
|
||||||
|
@ -323,14 +267,8 @@ class RF():
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
#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:
|
if nbytes == static.FREEDV_DATA_BYTES_PER_FRAME:
|
||||||
|
|
||||||
# counter reset for stuck in sync counter
|
# counter reset for stuck in sync counter
|
||||||
stuck_in_sync_counter = 0
|
stuck_in_sync_counter = 0
|
||||||
stuck_in_sync_10_counter = 0
|
stuck_in_sync_10_counter = 0
|
||||||
|
@ -344,19 +282,23 @@ class RF():
|
||||||
if 50 >= frametype >= 10:
|
if 50 >= frametype >= 10:
|
||||||
if frame != 3 or force == True:
|
if frame != 3 or force == True:
|
||||||
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
|
||||||
|
#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:
|
||||||
|
print("FULL BURST BUFFER ---> UNSYNC")
|
||||||
|
self.c_lib.freedv_set_sync(freedv_data, 0)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("---------------------------3er FRAME")
|
print("---------------------------SIMULATED MISSING FRAME")
|
||||||
force = True
|
force = True
|
||||||
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
|
|
||||||
# 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:
|
if frame == n_frames_per_burst:
|
||||||
|
print("LAST FRAME ---> UNSYNC")
|
||||||
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
self.c_lib.freedv_set_sync(freedv_data, 0) #FORCE UNSYNC
|
||||||
|
|
||||||
if static.ARQ_RX_BURST_BUFFER.count(None) == 1:
|
|
||||||
self.c_lib.freedv_set_sync(freedv_data, 0)
|
|
||||||
|
|
||||||
|
|
||||||
while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_ACK':
|
while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_ACK':
|
||||||
|
@ -374,8 +316,9 @@ class RF():
|
||||||
# CHECK IF FRAME CONTAINS ACK------------------------
|
# CHECK IF FRAME CONTAINS ACK------------------------
|
||||||
|
|
||||||
if 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")
|
frametype = int.from_bytes(bytes(signalling_bytes_out[:1]), "big")
|
||||||
|
print("SIGNALLING RECEIVED")
|
||||||
|
|
||||||
# BURST ACK
|
# BURST ACK
|
||||||
if frametype == 60:
|
if frametype == 60:
|
||||||
|
@ -390,10 +333,9 @@ class RF():
|
||||||
arq.burst_rpt_received(signalling_bytes_out[:-2])
|
arq.burst_rpt_received(signalling_bytes_out[:-2])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
rxstatus = self.c_lib.freedv_get_rx_status(freedv_signalling)
|
rxstatus = self.c_lib.freedv_get_rx_status(freedv_signalling)
|
||||||
|
print("ACK")
|
||||||
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
|
||||||
|
|
||||||
|
|
|
@ -85,10 +85,10 @@ 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
|
||||||
ARQ_FRAME_ACK_RECEIVED = 0 # set to 1 if FRAME 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 = 0 # set to 1 if timeut reached
|
||||||
ARQ_ACK_TIMEOUT_SECONDS = 4.0 #timeout for waiting for ACK frames
|
ARQ_ACK_TIMEOUT_SECONDS = 6.0 #timeout for waiting for ACK frames
|
||||||
|
|
||||||
ARQ_RPT_TIMEOUT = False
|
ARQ_RPT_TIMEOUT = False
|
||||||
ARQ_RPT_TIMEOUT_SECONDS = 4.0
|
ARQ_RPT_TIMEOUT_SECONDS = 6.0
|
||||||
ARQ_RPT_RECEIVED = False #indicate if RPT frame has been received
|
ARQ_RPT_RECEIVED = False #indicate if RPT frame has been received
|
||||||
ARQ_RPT_FRAMES = []
|
ARQ_RPT_FRAMES = []
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue