FreeDATA/arq.py

303 lines
17 KiB
Python
Raw Normal View History

2020-12-27 21:38:49 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 27 20:43:40 2020
@author: DJ2LS
"""
import logging
import threading
import time
2021-01-16 14:04:24 +00:00
from random import randrange
2020-12-27 21:38:49 +00:00
import static
import modem
import helpers
2020-12-27 21:38:49 +00:00
modem = modem.RF()
2021-01-20 21:51:14 +00:00
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 3 #6?!
2021-02-01 20:46:33 +00:00
static.ARQ_ACK_PAYLOAD_PER_FRAME = 14 - 2#
def arq_ack_timeout():
2021-02-01 20:46:33 +00:00
static.ARQ_ACK_TIMEOUT = 1
2020-12-27 21:38:49 +00:00
2021-01-16 14:04:24 +00:00
def data_received(data_in):
2021-02-01 20:46:33 +00:00
static.ARQ_N_FRAME = int.from_bytes(bytes(data_in[:1]), "big") - 10 #get number of frame
static.ARQ_N_RX_FRAMES_PER_BURSTS = int.from_bytes(bytes(data_in[1:2]), "big") #get number of bursts from received frame
2021-01-16 14:04:24 +00:00
2021-02-01 20:46:33 +00:00
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) + "%]")
2021-02-04 14:25:15 +00:00
#allocate ARQ_RX_BURST_BUFFER as a list with "None" if not already done. This should be done only once per burst!
2021-02-01 20:46:33 +00:00
# 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:
for i in range(0,static.ARQ_N_RX_FRAMES_PER_BURSTS+1):
static.ARQ_RX_BURST_BUFFER.insert(i,None)
2021-02-04 14:25:15 +00:00
#print(len(static.ARQ_RX_BURST_BUFFER))
2021-02-01 20:46:33 +00:00
# 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)
2021-02-04 14:25:15 +00:00
# run only if we recieved all ARQ FRAMES per ARQ BURST
2021-02-01 20:46:33 +00:00
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
2021-01-16 14:04:24 +00:00
2021-02-04 14:25:15 +00:00
print("len ARQ_RX_BURST_BUFFER: " + str(len(static.ARQ_RX_BURST_BUFFER)-1))
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])
2021-02-01 20:46:33 +00:00
#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)
2020-12-30 17:16:32 +00:00
# IF BURST CRC IS CORRECT, APPEND BURST TO BUFFER AND SEND ACK FRAME
2021-02-01 20:46:33 +00:00
if burst_payload_crc == data_in[2:4]:
2021-01-16 14:04:24 +00:00
2021-02-04 14:25:15 +00:00
#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
2020-12-30 17:16:32 +00:00
2021-02-01 20:46:33 +00:00
#BUILDING ACK FRAME -----------------------------------------------
ack_payload = bytes(burst_payload_crc)
2021-02-01 20:46:33 +00:00
ack_frame = b'<'+ bytes(burst_payload_crc) # < = 60
ack_buffer = bytearray(static.ARQ_ACK_PAYLOAD_PER_FRAME)
2020-12-30 17:16:32 +00:00
ack_buffer[:len(ack_frame)] = ack_frame # set buffersize to length of data which will be send
#TRANSMIT ACK FRAME -----------------------------------------------
2021-02-04 14:25:15 +00:00
# we need to wait until RX is finished on TX side --> seems to be not necessary if modulation buffer allocation for TX is correct
#time.sleep(2) #3.8
2021-02-01 20:46:33 +00:00
logging.info("ARQ | TX | SENDING ARQ BURST ACK [" + str(data_in[1:3]) +"]")
2021-01-21 07:33:45 +00:00
modem.transmit_arq_ack(ack_buffer)
2021-02-01 20:46:33 +00:00
# ----------------------------------------------------------------
2021-01-02 10:17:01 +00:00
2021-02-01 20:46:33 +00:00
static.ARQ_RX_BURST_BUFFER = [] # CLEAR RX BURST BUFFER AFTER SENDING DATA
2020-12-30 17:16:32 +00:00
else: #IF burst payload crc and input crc are NOT equal
2021-02-04 14:25:15 +00:00
logging.info("ARQ BURST CRC NOT EQUAL! [" + str(data_in[2:4]) + "]")
2021-01-21 20:00:21 +00:00
static.ARQ_RX_BURST_BUFFER = [] #erase ARQ RX Burst buffer
2021-01-02 10:17:01 +00:00
# LOOP THOUGH FRAME BUFFER AND STICK EVERYTHING TOGETHER
# WE ALSO CHECK FOR FRAME HEADER AND LAST FRAME
2020-12-30 17:16:32 +00:00
complete_frame = bytearray()
2021-02-01 20:46:33 +00:00
#print(static.ARQ_RX_FRAME_BUFFER)
2020-12-30 17:16:32 +00:00
for frame in range(len(static.ARQ_RX_FRAME_BUFFER)):
complete_frame = complete_frame + static.ARQ_RX_FRAME_BUFFER[frame]
2021-02-01 20:46:33 +00:00
2020-12-30 17:16:32 +00:00
# -------- DETECT IF WE ALREADY RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
2021-02-01 20:46:33 +00:00
if complete_frame[4:6].startswith(static.FRAME_BOF) or burst_total_payload[4:6].startswith(static.FRAME_BOF): #5:7
2020-12-31 09:25:45 +00:00
static.FRAME_CRC = complete_frame[2:4]
2021-02-04 14:25:15 +00:00
static.ARQ_FRAME_BOF_RECEIVED = True
2020-12-30 17:16:32 +00:00
# -------- DETECT IF WE HAVE ALREADY RECEIVED THE LAST FRAME
2021-01-21 20:00:21 +00:00
if burst_total_payload.rstrip(b'\x00').endswith(static.FRAME_EOF):
2021-02-04 14:25:15 +00:00
static.ARQ_FRAME_EOF_RECEIVED = True
#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:
# NOW WE TRY TO SEPARATE THE FRAME CRC FOR A CRC CALCULATION
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)
2021-02-01 20:46:33 +00:00
2021-02-04 14:25:15 +00:00
#IF THE FRAME PAYLOAD CRC IS EQUAL TO THE FRAME CRC WHICH IS KNOWN FROM THE HEADER --> SUCCESS
if frame_payload_crc == static.FRAME_CRC:
logging.info("ARQ | RX | DATA FRAME SUCESSFULLY RECEIVED! - TIME TO PARTY")
static.RX_BUFFER.append(frame_payload)
# clearing buffers
static.ARQ_RX_FRAME_BUFFER = []
static.ARQ_FRAME_BOF_RECEIVED = False
static.ARQ_FRAME_EOF_RECEIVED = False
print("----------------------------------------------------------------")
print(static.RX_BUFFER[-1])
print("----------------------------------------------------------------")
# HERE: WE COULD SEND ACK FOR TOTAL FRAME
else:
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():
2021-02-01 20:46:33 +00:00
logging.info("ARQ | RX | ACK RCVD!")
2021-01-21 20:00:21 +00:00
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
2021-02-01 20:46:33 +00:00
2020-12-27 21:38:49 +00:00
def transmit(data_out):
2021-02-01 20:46:33 +00:00
2021-01-20 21:51:14 +00:00
2021-02-01 20:46:33 +00:00
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_DATA_PAYLOAD_PER_FRAME - 4 #3 ohne ARQ_TX_N_FRAMES_PER_BURST
2021-01-20 21:51:14 +00:00
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
2021-02-01 20:46:33 +00:00
#print(static.FREEDV_DATA_PAYLOAD_PER_FRAME)
#print(static.ARQ_PAYLOAD_PER_FRAME)
#print(n_bursts_prediction)
n_bursts_prediction = n_bursts_prediction.to_bytes(2, byteorder='big') #65535
frame_payload_crc = helpers.get_crc_16(data_out)
2021-01-02 10:17:01 +00:00
# This is the total frame with frame header, which will be send
2021-01-20 21:51:14 +00:00
data_out = n_bursts_prediction + frame_payload_crc + static.FRAME_BOF + data_out + static.FRAME_EOF
# 2 2 2 N 2
2021-02-01 20:46:33 +00:00
#print(data_out)
#print(len(data_out))
#print(static.ARQ_PAYLOAD_PER_FRAME)
2021-01-02 10:17:01 +00:00
# --------------------------------------------- LETS CREATE A BUFFER BY SPLITTING THE FILES INTO PEACES
static.TX_BUFFER = [data_out[i:i+static.ARQ_PAYLOAD_PER_FRAME] for i in range(0, len(data_out), static.ARQ_PAYLOAD_PER_FRAME)]
static.TX_BUFFER_SIZE = len(static.TX_BUFFER)
2021-02-01 20:46:33 +00:00
#print(static.TX_BUFFER)
2021-02-01 20:46:33 +00:00
logging.info("ARQ | TX | TOTAL PAYLOAD BYTES/FRAMES TO SEND: " + str(len(data_out)) + " / " + str(static.TX_BUFFER_SIZE))
2021-01-16 14:04:24 +00:00
2021-01-20 21:51:14 +00:00
# --------------------------------------------- THIS IS THE MAIN LOOP-----------------------------------------------------------------
2021-01-20 22:42:45 +00:00
2021-01-02 10:17:01 +00:00
static.ARQ_N_SENT_FRAMES = 0 # SET N SENT FRAMES TO 0 FOR A NEW SENDING CYCLE
2021-01-02 08:41:58 +00:00
while static.ARQ_N_SENT_FRAMES <= static.TX_BUFFER_SIZE:
2021-01-02 10:17:01 +00:00
2021-01-02 15:37:34 +00:00
print("static.ARQ_N_SENT_FRAMES: " + str(static.ARQ_N_SENT_FRAMES))
2021-01-20 21:51:14 +00:00
static.ARQ_TX_N_FRAMES_PER_BURST = get_n_frames_per_burst()
2021-01-20 22:42:45 +00:00
2021-01-20 21:51:14 +00:00
# ----------- CREATE FRAME TOTAL PAYLOAD TO BE ABLE TO CREATE CRC FOR IT
2021-01-02 10:17:01 +00:00
burst_total_payload = bytearray()
try: # DETECT IF LAST BURST TO PREVENT INDEX ERROR OF BUFFER
2021-01-20 21:51:14 +00:00
for i in range(static.ARQ_TX_N_FRAMES_PER_BURST): # Loop through TX_BUFFER LIST
# make sure we have always a filled buffer with the length of payload per frame
2021-01-02 08:41:58 +00:00
burst_raw_payload = static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + i]
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
2021-01-20 21:51:14 +00:00
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
except IndexError: # IF LAST BURST DETECTED BUILD CRC WITH LESS FRAMES AND SET static.ARQ_TX_N_FRAMES_PER_BURST TO VALUE OF REST!
burst_total_payload = bytearray() # reset burst_total_payload because of possible input remaining of detecting loop one step above
2021-01-20 21:51:14 +00:00
if static.ARQ_N_SENT_FRAMES == 0 and (static.ARQ_TX_N_FRAMES_PER_BURST > static.TX_BUFFER_SIZE): #WE CANT DO MODULO 0 --> CHECK IF FIRST FRAME == LAST FRAME
static.ARQ_TX_N_FRAMES_PER_BURST = static.TX_BUFFER_SIZE
2021-01-20 21:51:14 +00:00
elif static.ARQ_N_SENT_FRAMES == 1 and (static.ARQ_TX_N_FRAMES_PER_BURST > static.TX_BUFFER_SIZE): # MODULO 1 WILL ALWAYS BE 0 --> THIS FIXES IT
static.ARQ_TX_N_FRAMES_PER_BURST = static.TX_BUFFER_SIZE - static.ARQ_N_SENT_FRAMES
2021-01-02 15:37:34 +00:00
else:
2021-01-20 21:51:14 +00:00
static.ARQ_TX_N_FRAMES_PER_BURST = (static.TX_BUFFER_SIZE % static.ARQ_N_SENT_FRAMES)
2021-01-02 15:37:34 +00:00
2021-01-20 21:51:14 +00:00
print("ARQ_TX_N_FRAMES_PER_BURST OF LAST BURST: " + str(static.ARQ_TX_N_FRAMES_PER_BURST))
for i in range(static.ARQ_TX_N_FRAMES_PER_BURST): #bytearray(b'111111111111111111111111222222222222222222222222')
2021-01-02 10:17:01 +00:00
2021-01-20 21:51:14 +00:00
# make sure we have always a filled buffer with the length of payload per frame
burst_raw_payload = static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + i]
burst_payload = bytearray(static.ARQ_PAYLOAD_PER_FRAME)
2021-01-20 21:51:14 +00:00
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
2021-02-01 20:46:33 +00:00
#print(burst_total_payload)
2021-01-20 21:51:14 +00:00
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES_PER_BURST
2021-01-20 21:51:14 +00:00
static.ARQ_BURST_PAYLOAD_CRC = helpers.get_crc_16(burst_total_payload)
2021-01-16 14:04:24 +00:00
2021-01-02 10:17:01 +00:00
#--------------------------------------------- N ATTEMPTS TO SEND BURSTS IF ACK RECEPTION FAILS
2020-12-27 21:38:49 +00:00
for static.TX_N_RETRIES in range(static.TX_N_MAX_RETRIES):
2021-01-20 21:51:14 +00:00
print("SENDING")
2021-01-20 22:42:45 +00:00
# lets start a thread to transmit nonblocking
TRANSMIT_ARQ_BURST_THREAD = threading.Thread(target=modem.transmit_arq_burst, name="TRANSMIT_ARQ_BURST")
TRANSMIT_ARQ_BURST_THREAD.start()
2021-02-04 14:25:15 +00:00
# lets wait during sending. After sending is finished we will continue
2021-01-20 22:42:45 +00:00
while static.ARQ_STATE == 'SENDING_DATA':
time.sleep(0.05)
2021-02-01 20:46:33 +00:00
2021-01-20 22:42:45 +00:00
2021-01-02 10:17:01 +00:00
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
2021-02-04 14:25:15 +00:00
#reset timer and ack state
static.ARQ_ACK_RECEIVED = 0
static.ARQ_ACK_TIMEOUT = 0
2021-02-01 20:46:33 +00:00
logging.info("ARQ | WAITING FOR ACK")
2021-01-20 14:03:42 +00:00
static.ARQ_STATE = 'RECEIVING_ACK'
2021-02-01 20:46:33 +00:00
2021-02-04 14:25:15 +00:00
2021-02-01 20:46:33 +00:00
timer = threading.Timer(static.ARQ_ACK_TIMEOUT_SECONDS, arq_ack_timeout)
2020-12-27 21:38:49 +00:00
timer.start()
2021-01-20 22:42:45 +00:00
2021-02-01 20:46:33 +00:00
# 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
2021-01-02 10:17:01 +00:00
# --------------------------- WHILE TIMEOUT NOT REACHED AND NO ACK RECEIVED --> LISTEN
2021-01-20 22:42:45 +00:00
while static.ARQ_ACK_TIMEOUT == 0 and static.ARQ_ACK_RECEIVED == 0:
time.sleep(0.01) # lets reduce CPU load a little bit
2021-02-04 14:25:15 +00:00
print(static.ARQ_STATE)
2021-02-01 20:46:33 +00:00
2021-01-20 22:42:45 +00:00
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED
if static.ARQ_ACK_RECEIVED == 1:
2021-01-02 10:17:01 +00:00
#-----------IF ACK RECEIVED, INCREMENT ITERATOR FOR MAIN LOOP TO PROCEED WITH NEXT FRAMES/BURST
2021-01-20 21:51:14 +00:00
static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST
2020-12-27 21:38:49 +00:00
break
2021-02-01 20:46:33 +00:00
if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
2021-02-04 14:25:15 +00:00
logging.info("ARQ | ACK TIMEOUT | SENDING ARQ BURST AGAIN")
2020-12-27 21:38:49 +00:00
# ----------- if no ACK received and out of retries.....stop frame sending
2021-02-04 14:25:15 +00:00
#if static.ARQ_ACK_RECEIVED == 0 and static.ARQ_ACK_TIMEOUT == 1:
# logging.info("ARQ | TX | NO ACK RECEIVED | DATA FRAME NEEDS TO BE RESEND!")
# break
2020-12-27 21:38:49 +00:00
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT
2021-01-02 08:41:58 +00:00
if static.ARQ_N_SENT_FRAMES == static.TX_BUFFER_SIZE:
2021-02-04 14:25:15 +00:00
break
2021-01-02 08:41:58 +00:00
2021-01-02 10:17:01 +00:00
# ------------ TIMER TO WAIT UNTIL NEXT PACKAGE WILL BE SEND TO PREVENT TIME ISSEUS --> NEEDS TO BE IMPROVED LATER
2021-02-01 20:46:33 +00:00
#time.sleep(3)
2020-12-27 21:38:49 +00:00
2021-01-02 10:17:01 +00:00
# IF TX BUFFER IS EMPTY / ALL FRAMES HAVE BEEN SENT --> HERE WE COULD ADD AN static.VAR for IDLE STATE
2021-02-01 20:46:33 +00:00
#logging.info("ARQ | TX | FRAME SUCESSFULLY TRANSMITTED! - TIME TO PARTY")
logging.info("ARQ | TX | BUFFER EMPTY")
2021-01-21 20:00:21 +00:00
print(static.ARQ_N_SENT_FRAMES)
print(static.ARQ_TX_N_FRAMES_PER_BURST)
# - RESET COUNTERS
static.ARQ_N_SENT_FRAMES = 0
static.ARQ_TX_N_FRAMES_PER_BURST = 0
2021-02-01 20:46:33 +00:00
static.ARQ_ACK_RECEIVED = 0
2021-01-16 14:04:24 +00:00
2021-01-02 15:37:34 +00:00
# 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():
2021-01-16 14:04:24 +00:00
2021-02-04 14:25:15 +00:00
#n_frames_per_burst = randrange(1,5)
n_frames_per_burst = 2
2021-01-02 08:41:58 +00:00
return n_frames_per_burst