mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
ARQ Go-Back-N
ARQ Go-Back-N now working even if frames are odd to frames per burst.
This commit is contained in:
parent
fde5754607
commit
e43970ad50
165
arq.py
165
arq.py
|
@ -18,59 +18,62 @@ import modem
|
||||||
import other
|
import other
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
modem = modem.RF()
|
modem = modem.RF()
|
||||||
|
|
||||||
crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
|
crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
|
||||||
|
|
||||||
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_PAYLOAD_PER_FRAME - 6
|
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_PAYLOAD_PER_FRAME - 6
|
||||||
|
|
||||||
#class ARQ():
|
|
||||||
#
|
|
||||||
# def receive(bytes_in):
|
|
||||||
#
|
|
||||||
# print("TEST")
|
|
||||||
|
|
||||||
|
|
||||||
def data_received(data_in):
|
def data_received(data_in):
|
||||||
|
|
||||||
ARQ_N_RX_BURSTS = int.from_bytes(bytes(data_in[:1]), "big") - 10
|
ARQ_N_RX_BURSTS = int.from_bytes(bytes(data_in[:1]), "big") - 10
|
||||||
static.ARQ_RX_BUFFER.append(data_in) #append data to RX BUFFER
|
static.ARQ_RX_BUFFER.append(data_in) #append data to RX BUFFER
|
||||||
|
|
||||||
if len(static.ARQ_RX_BUFFER) == ARQ_N_RX_BURSTS: #if received bursts are equal to burst number in frame
|
print(ARQ_N_RX_BURSTS)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#while static.ACK_RX_TIMEOUT == 0: #define timeout where data has to be received untl error occurs
|
||||||
|
|
||||||
|
if len(static.ARQ_RX_BUFFER) == ARQ_N_RX_BURSTS: #if received bursts are equal to burst number in frame
|
||||||
|
|
||||||
burst_total_payload = bytearray()
|
burst_total_payload = bytearray()
|
||||||
for n_raw_frame in range(0,len(static.ARQ_RX_BUFFER)):
|
for n_raw_frame in range(0,len(static.ARQ_RX_BUFFER)):
|
||||||
|
|
||||||
burst_frame = static.ARQ_RX_BUFFER[n_raw_frame] #get burst frame
|
burst_frame = static.ARQ_RX_BUFFER[n_raw_frame] #get burst frame
|
||||||
burst_payload = burst_frame[3:] #remove frame type and burst CRC
|
burst_payload = burst_frame[3:] #remove frame type and burst CRC
|
||||||
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
|
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
|
||||||
|
|
||||||
print(burst_total_payload)
|
print(burst_total_payload)
|
||||||
burst_payload_crc = crc_algorithm(burst_total_payload)
|
burst_payload_crc = crc_algorithm(burst_total_payload)
|
||||||
burst_payload_crc = burst_payload_crc.to_bytes(2, byteorder='big')
|
burst_payload_crc = burst_payload_crc.to_bytes(2, byteorder='big')
|
||||||
print(burst_payload_crc)
|
print(burst_payload_crc)
|
||||||
|
|
||||||
|
|
||||||
if burst_payload_crc == data_in[1:3]: #IF burst payload crc and input crc are equal
|
if burst_payload_crc == data_in[1:3]: #IF burst payload crc and input crc are equal
|
||||||
print(data_in[1:3])
|
|
||||||
print("CRC EQUAL")
|
print(data_in[1:3])
|
||||||
logging.info("TX | SENDING ACK [" + str(data_in[1:3]) +"]")
|
print("CRC EQUAL")
|
||||||
|
logging.info("TX | SENDING ACK [" + str(data_in[1:3]) +"]")
|
||||||
|
|
||||||
#BUILDING ACK FRAME -----------------------------------------------
|
#BUILDING ACK FRAME -----------------------------------------------
|
||||||
|
|
||||||
ack_frame = b'\7' + bytes(burst_payload_crc)
|
ack_frame = b'\7' + bytes(burst_payload_crc)
|
||||||
ack_buffer = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
|
ack_buffer = bytearray(static.ARQ_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 -----------------------------------------------
|
||||||
|
time.sleep(2)
|
||||||
|
modem.Transmit(ack_buffer)
|
||||||
|
static.ARQ_RX_BUFFER = []
|
||||||
|
|
||||||
modem.Transmit(ack_buffer)
|
else: #IF burst payload crc and input crc are NOT equal
|
||||||
static.ARQ_RX_BUFFER = []
|
print("CRC NOT EQUAL!!!!!")
|
||||||
else: #IF burst payload crc and input crc are NOT equal
|
print(data_in[1:3])
|
||||||
print("CRC NOT EQUAL!!!!!")
|
static.ARQ_RX_BUFFER = []
|
||||||
print(data_in[1:3])
|
|
||||||
static.ARQ_RX_BUFFER = []
|
|
||||||
|
|
||||||
|
|
||||||
def ack_received():
|
def ack_received():
|
||||||
|
@ -91,44 +94,84 @@ def transmit(data_out):
|
||||||
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)] # split incomming bytes to size of 30bytes - arq payload
|
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)] # split incomming bytes to size of 30bytes - arq payload
|
||||||
static.TX_BUFFER_SIZE = len(static.TX_BUFFER)
|
static.TX_BUFFER_SIZE = len(static.TX_BUFFER)
|
||||||
|
|
||||||
|
static.ARQ_TX_N_FRAMES = n_frames_per_burst(len(data_out)) # DEFINE NUMBER OF FRAMES PER BURSTS
|
||||||
|
|
||||||
logging.info("TX | TOTAL PAYLOAD BYTES/FRAMES TO SEND: " + str(len(data_out)) + " / " + str(static.TX_BUFFER_SIZE))
|
logging.info("TX | TOTAL PAYLOAD BYTES/FRAMES TO SEND: " + str(len(data_out)) + " / " + str(static.TX_BUFFER_SIZE))
|
||||||
|
|
||||||
|
#print(static.TX_BUFFER[2])
|
||||||
|
|
||||||
for n_raw_frame in range(0, static.TX_BUFFER_SIZE, static.ARQ_TX_N_FRAMES): # LOOP THROUGH DATA LIST with steps = ARQ_TX_N_FRAMES
|
for n_raw_frame in range(0, static.TX_BUFFER_SIZE, static.ARQ_TX_N_FRAMES): # LOOP THROUGH DATA LIST with steps = ARQ_TX_N_FRAMES
|
||||||
print("N_RAW_FRAME: " + str(n_raw_frame))
|
print("N_RAW_FRAME: " + str(n_raw_frame))
|
||||||
|
|
||||||
|
## check, if we have to adjust burst frames
|
||||||
|
#if (n_raw_frame % static.TX_BUFFER_SIZE) != 0:
|
||||||
|
# static.ARQ_TX_N_FRAMES = (n_raw_frame % static.TX_BUFFER_SIZE)
|
||||||
|
# print(static.ARQ_TX_N_FRAMES)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES
|
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES
|
||||||
burst_total_payload = bytearray()
|
burst_total_payload = bytearray()
|
||||||
for i in range(static.ARQ_TX_N_FRAMES): #bytearray(b'111111111111111111111111222222222222222222222222')
|
|
||||||
# we need to make sure, payload data is always as long as static.ARQ_PAYLOAD_PER_FRAME beacuse of CRC!
|
|
||||||
burst_raw_payload = static.TX_BUFFER[n_raw_frame + i]
|
#----------------------------------------------------------------------------------------------------------
|
||||||
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
|
try: # DETECT IF LAST BURST
|
||||||
burst_payload[:len(burst_raw_payload)] = burst_raw_payload # set buffersize to length of data which will be send
|
for i in range(static.ARQ_TX_N_FRAMES): #bytearray(b'111111111111111111111111222222222222222222222222')
|
||||||
burst_total_payload = burst_total_payload + burst_payload
|
|
||||||
|
# we need to make sure, payload data is always as long as static.ARQ_PAYLOAD_PER_FRAME beacuse of CRC!
|
||||||
|
|
||||||
|
burst_raw_payload = static.TX_BUFFER[n_raw_frame + i]
|
||||||
|
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
|
||||||
|
burst_payload[:len(burst_raw_payload)] = burst_raw_payload # set buffersize to length of data which will be send
|
||||||
|
burst_total_payload = burst_total_payload + burst_payload
|
||||||
|
|
||||||
|
except IndexError: # IF LAST BURST DETECTED BUILD CRC WITH LESS FRAMES AND SET static.ARQ_TX_N_FRAMES TO VALUE OF REST!
|
||||||
|
print("LAST BURST!!!")
|
||||||
|
burst_total_payload = bytearray() # reset burst_total_payload because of possible input remaining of detecting loop one step above
|
||||||
|
n_last_burst = (static.TX_BUFFER_SIZE % n_raw_frame)
|
||||||
|
print(n_last_burst)
|
||||||
|
static.ARQ_TX_N_FRAMES = n_last_burst
|
||||||
|
|
||||||
|
for i in range(n_last_burst): #bytearray(b'111111111111111111111111222222222222222222222222')
|
||||||
|
|
||||||
|
burst_raw_payload = static.TX_BUFFER[n_raw_frame + i]
|
||||||
|
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
|
||||||
|
burst_payload[:len(burst_raw_payload)] = burst_raw_payload # set buffersize to length of data which will be send
|
||||||
|
burst_total_payload = burst_total_payload + burst_payload
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
print(burst_total_payload)
|
||||||
|
|
||||||
burst_payload_crc = crc_algorithm(burst_total_payload)
|
burst_payload_crc = crc_algorithm(burst_total_payload)
|
||||||
burst_payload_crc = burst_payload_crc.to_bytes(2, byteorder='big')
|
burst_payload_crc = burst_payload_crc.to_bytes(2, byteorder='big')
|
||||||
|
|
||||||
print(burst_payload_crc)
|
print(burst_payload_crc)
|
||||||
static.ARQ_ACK_WAITING_FOR_ID = burst_payload_crc #set the global variable so we know for which ACK we are waiting for
|
static.ARQ_ACK_WAITING_FOR_ID = burst_payload_crc #set the global variable so we know for which ACK we are waiting for
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------------------------------------
|
||||||
#-------------------- BUILD ARQBURSTS
|
#-------------------- BUILD ARQBURSTS
|
||||||
|
|
||||||
arqburst = []
|
arqburst = []
|
||||||
for i in range(static.ARQ_TX_N_FRAMES):
|
for i in range(static.ARQ_TX_N_FRAMES):
|
||||||
|
|
||||||
frame_type = 10 + static.ARQ_TX_N_FRAMES
|
|
||||||
frame_type = bytes([frame_type])
|
|
||||||
|
|
||||||
payload_data = bytes(static.TX_BUFFER[n_raw_frame + i])
|
|
||||||
|
frame_type = 10 + static.ARQ_TX_N_FRAMES
|
||||||
arqframe = frame_type + burst_payload_crc + payload_data
|
frame_type = bytes([frame_type])
|
||||||
|
|
||||||
buffer = bytearray(static.FREEDV_PAYLOAD_PER_FRAME) # create TX buffer
|
payload_data = bytes(static.TX_BUFFER[n_raw_frame + i])
|
||||||
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
|
|
||||||
|
arqframe = frame_type + burst_payload_crc + payload_data
|
||||||
|
|
||||||
|
buffer = bytearray(static.FREEDV_PAYLOAD_PER_FRAME) # create TX buffer
|
||||||
|
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
|
||||||
|
|
||||||
arqburst.append(buffer)
|
arqburst.append(buffer)
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
#--------------------------------------------- N ATTEMPTS TO SEND BURSTS IF ACK FAILS
|
#--------------------------------------------- N ATTEMPTS TO SEND BURSTS IF ACK FAILS
|
||||||
|
@ -137,6 +180,7 @@ def transmit(data_out):
|
||||||
static.ACK_RECEIVED = 0
|
static.ACK_RECEIVED = 0
|
||||||
|
|
||||||
# ----------------------- Loop through ARQ FRAMES BUFFER with N = Numbers of frames which will be send at once
|
# ----------------------- Loop through ARQ FRAMES BUFFER with N = Numbers of frames which will be send at once
|
||||||
|
|
||||||
for n in range(static.ARQ_TX_N_FRAMES):
|
for n in range(static.ARQ_TX_N_FRAMES):
|
||||||
logging.info("TX | SENDING BURST " + str(n+1) + " / " + str(static.ARQ_TX_N_FRAMES))
|
logging.info("TX | SENDING BURST " + str(n+1) + " / " + str(static.ARQ_TX_N_FRAMES))
|
||||||
modem.Transmit(arqburst[n])
|
modem.Transmit(arqburst[n])
|
||||||
|
@ -171,5 +215,22 @@ def transmit(data_out):
|
||||||
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT
|
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT
|
||||||
if n_raw_frame == static.TX_BUFFER_SIZE:
|
if n_raw_frame == static.TX_BUFFER_SIZE:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# ------------ TIMER TO WAIT UNTIL NEXT PACKAGE WILL BE SEND TO PREVENT TIME ISSEUS
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
logging.info("TX | BUFFER EMPTY")
|
logging.info("TX | BUFFER EMPTY")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# BURST MACHINE TO DEFINE N BURSTS PER FRAME
|
||||||
|
def n_frames_per_burst(len_data):
|
||||||
|
|
||||||
|
if len_data <= static.ARQ_PAYLOAD_PER_FRAME:
|
||||||
|
n_frames_per_burst = 1
|
||||||
|
else:
|
||||||
|
n_frames_per_burst = 3
|
||||||
|
|
||||||
|
|
||||||
|
return n_frames_per_burst
|
Loading…
Reference in a new issue