FreeDATA/arq.py

312 lines
15 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
"""
# CRC aller payloads via XOR scrambeln und dann eine CRC8 mitsenden
import logging
import crcengine
import threading
import time
2020-12-27 21:38:49 +00:00
import static
import modem
import other
2021-01-02 08:41:58 +00:00
from random import randrange
2020-12-27 21:38:49 +00:00
modem = modem.RF()
2020-12-27 21:38:49 +00:00
crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_PAYLOAD_PER_FRAME - 6
def data_received(data_in):
ARQ_N_RX_BURSTS = int.from_bytes(bytes(data_in[:1]), "big") - 10
static.ARQ_RX_BURST_BUFFER.append(data_in) #append data to RX BUFFER
2020-12-30 17:16:32 +00:00
#print(ARQ_N_RX_BURSTS)
2020-12-30 17:16:32 +00:00
burst_total_payload = bytearray()
#while static.ACK_RX_TIMEOUT == 0: #define timeout where data has to be received untl error occurs
if len(static.ARQ_RX_BURST_BUFFER) == ARQ_N_RX_BURSTS: #if received bursts are equal to burst number in frame
2020-12-30 17:16:32 +00:00
#burst_total_payload = bytearray()
for n_raw_frame in range(0,len(static.ARQ_RX_BURST_BUFFER)):
burst_frame = static.ARQ_RX_BURST_BUFFER[n_raw_frame] #get burst frame
burst_payload = burst_frame[3:] #remove frame type and burst CRC
burst_total_payload = burst_total_payload + burst_payload #stick bursts together
2020-12-30 17:16:32 +00:00
# ------------------ caculate CRC of BURST
#print(burst_total_payload)
burst_payload_crc = crc_algorithm(burst_total_payload)
burst_payload_crc = burst_payload_crc.to_bytes(2, byteorder='big')
2020-12-30 17:16:32 +00:00
#print(burst_payload_crc)
2020-12-30 17:16:32 +00:00
# IF BURST CRC IS CORRECT, APPEND BURST TO BUFFER AND SEND ACK FRAME
if burst_payload_crc == data_in[1:3]:
2021-01-02 10:17:01 +00:00
# WAIT SOME TIME TO PREVENT TIMING ISSUES --> NEEDS TO BE OPTIMIZED LATER
time.sleep(2)
2020-12-30 17:16:32 +00:00
logging.info("BURST CRC ARE EQUAL!")
logging.info("TX | SENDING ARQ BURST ACK [" + str(data_in[1:3]) +"]")
2021-01-02 10:17:01 +00:00
#print(burst_total_payload)
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
#print(data_in[7:9])
#BUILDING ACK FRAME -----------------------------------------------
ack_frame = b'\7' + bytes(burst_payload_crc)
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
#TRANSMIT ACK FRAME -----------------------------------------------
modem.Transmit(ack_buffer)
2021-01-02 10:17:01 +00:00
static.ARQ_RX_BURST_BUFFER = [] # CLEAR RX BURST BUFFER
2020-12-30 17:16:32 +00:00
else: #IF burst payload crc and input crc are NOT equal
print("CRC NOT EQUAL!!!!!")
print(data_in[1:3])
static.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()
for frame in range(len(static.ARQ_RX_FRAME_BUFFER)):
complete_frame = complete_frame + static.ARQ_RX_FRAME_BUFFER[frame]
# -------- DETECT IF WE ALREADY RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
2020-12-31 09:25:45 +00:00
#if burst_total_payload[4:6].startswith(b'\xAA\xAA'):
if complete_frame[4:6].startswith(b'\xAA\xAA') or burst_total_payload[4:6].startswith(b'\xAA\xAA'):
2020-12-30 17:16:32 +00:00
#print("DAS IST DER ERSTE BURST MIT BOF!!!")
2020-12-31 09:25:45 +00:00
#print("FRAME BURSTS = " + str(complete_frame[:2]))
#print("FRAME CRC = " + str(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")
2020-12-30 17:16:32 +00:00
# -------- DETECT IF WE HAVE ALREADY RECEIVED THE LAST FRAME
#if burst_total_payload.rstrip(b'\x00').endswith(b'\xFF\xFF'):
#print("DAS IST DER LETZTE BURST MIT EOF!!!")
2021-01-02 10:17:01 +00:00
# NOW WE TRY TO SEPARATE THE FRAME CRC FOR A CRC CALCULATION
2020-12-30 17:16:32 +00:00
frame_payload = complete_frame.rstrip(b'\x00') #REMOVE x00
2021-01-02 10:17:01 +00:00
frame_payload = frame_payload[6:-2] #THIS IS THE FRAME PAYLOAD
2020-12-30 17:16:32 +00:00
frame_payload_crc = crc_algorithm(frame_payload)
frame_payload_crc = frame_payload_crc.to_bytes(2, byteorder='big')
2021-01-02 10:17:01 +00:00
#IF THE FRAME PAYLOAD CRC IS EQUAL TO THE FRAME CRC WHICH IS KNOWN FROM THE HEADER --> SUCCESS
2020-12-30 17:16:32 +00:00
if frame_payload_crc == static.FRAME_CRC:
2021-01-02 10:17:01 +00:00
logging.info("RX | FILE SUCESSFULL RECEIVED! - TIME TO PARTY")
2020-12-30 17:16:32 +00:00
static.RX_BUFFER.append(frame_payload)
static.ARQ_RX_FRAME_BUFFER = []
2021-01-02 10:17:01 +00:00
#print(static.RX_BUFFER[-1])
2020-12-30 17:16:32 +00:00
# HERE: SEND ACK FOR TOTAL FRAME!!!
2021-01-02 10:17:01 +00:00
#else:
#print("CRC FOR FRAME NOT EQUAL!")
# print("FRAME PAYLOAD CRC: " + str(frame_payload_crc))
# print("FRAME PAYLOAD: " + str(frame_payload))
#print("COMPLETE FRAME: " + str(complete_frame))
2020-12-30 17:16:32 +00:00
#static.ARQ_RX_FRAME_BUFFER = [] # ---> BUFFER ERST LÖSCHEN WENN MINDESTANZAHL AN BURSTS ERHALTEN WORDEN SIND
2020-12-30 17:16:32 +00:00
def ack_received():
logging.info("RX | ACK RCVD!")
static.ACK_TIMEOUT = 1 #Force timer to stop waiting
static.ACK_RECEIVED = 1 #Force data loops of TNC to stop and continue with next frame
# static.ARQ_ACK_WAITING_FOR_ID
2021-01-02 10:17:01 +00:00
2020-12-27 21:38:49 +00:00
def transmit(data_out):
static.ARQ_PAYLOAD_PER_FRAME = static.FREEDV_PAYLOAD_PER_FRAME - 3
2021-01-02 10:17:01 +00:00
#----------------------- BUILD A FRAME WITH CRC AND N BURSTS SO THE RECEIVER GETS THIS INFORMATION
frame_BOF = b'\xAA\xAA'
frame_EOF = b'\xFF\xFF'
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 = n_bursts_prediction.to_bytes(2, byteorder='big') #65535
frame_payload_crc = crc_algorithm(data_out)
frame_payload_crc = frame_payload_crc.to_bytes(2, byteorder='big')
2021-01-02 10:17:01 +00:00
# This is the total frame with frame header, which will be send
data_out = n_bursts_prediction + frame_payload_crc + frame_BOF + data_out + frame_EOF
# 2 2 2 N 2
2021-01-02 10:17:01 +00:00
#print(data_out)
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)
2020-12-27 21:38:49 +00:00
logging.info("TX | TOTAL PAYLOAD BYTES/FRAMES TO SEND: " + str(len(data_out)) + " / " + str(static.TX_BUFFER_SIZE))
2021-01-02 10:17:01 +00:00
2021-01-02 08:41:58 +00:00
2021-01-02 10:17:01 +00:00
###static.ACK_RECEIVED = 0 # SET ACK RECEIVED TO 0 IF NOT ALREADY DONE TO BE SURE WE HAVE A NEW CYCLE
2021-01-02 08:41:58 +00:00
2021-01-02 10:17:01 +00:00
# --------------------------------------------- THIS IS THE MAIN LOOP
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 08:41:58 +00:00
2021-01-02 10:17:01 +00:00
############################----------RANDOM FRAMES PER BURST MACHINE --> HELPER FOR SIMULATING DYNAMIC ARQ UNTIL BURST MACHINE IS BUILD
static.ARQ_TX_N_FRAMES = get_n_frames_per_burst(len(data_out))
static.ACK_RECEIVED = 0 # SET ACK RECEIVED TO 0 IF NOT ALREADY DONE TO BE SURE WE HAVE A NEW CYCLE
2021-01-02 08:41:58 +00:00
#################################################################
2021-01-02 10:17:01 +00:00
# ----------- SET FRAME PAYLOAD TO BE ABLE TO CREATE CRC
burst_total_payload = bytearray()
try: # DETECT IF LAST BURST TO PREVENT INDEX ERROR OF BUFFER
for i in range(static.ARQ_TX_N_FRAMES): #bytearray(b'111111111111111111111111222222222222222222222222')
2021-01-02 10:17:01 +00:00
# we need to make sure, payload data is always as long as static.ARQ_PAYLOAD_PER_FRAME beacuse of CRC!
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)
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!
burst_total_payload = bytearray() # reset burst_total_payload because of possible input remaining of detecting loop one step above
2021-01-02 08:41:58 +00:00
n_last_burst = (static.TX_BUFFER_SIZE % static.ARQ_N_SENT_FRAMES)
static.ARQ_TX_N_FRAMES = n_last_burst
for i in range(n_last_burst): #bytearray(b'111111111111111111111111222222222222222222222222')
2021-01-02 10:17:01 +00:00
# we need to make sure, payload data is always as long as static.ARQ_PAYLOAD_PER_FRAME beacuse of CRC!
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)
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
2021-01-02 10:17:01 +00:00
# ----------- GENERATE PAYLOAD CRC FOR ARQ_TX_N_FRAMES
2020-12-27 21:38:49 +00:00
burst_payload_crc = crc_algorithm(burst_total_payload)
burst_payload_crc = burst_payload_crc.to_bytes(2, byteorder='big')
static.ARQ_ACK_WAITING_FOR_ID = burst_payload_crc #set the global variable so we know for which ACK we are waiting for
2020-12-27 21:38:49 +00:00
2021-01-02 08:41:58 +00:00
#------------------ BUILD ARQBURSTS---------------------------------------------
2020-12-27 21:38:49 +00:00
arqburst = []
for i in range(static.ARQ_TX_N_FRAMES):
frame_type = 10 + static.ARQ_TX_N_FRAMES
frame_type = bytes([frame_type])
2021-01-02 08:41:58 +00:00
payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + i])
arqframe = frame_type + burst_payload_crc + payload_data
2020-12-27 21:38:49 +00:00
buffer = bytearray(static.FREEDV_PAYLOAD_PER_FRAME) # create TX buffer
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
2021-01-02 10:17:01 +00:00
arqburst.append(buffer) #append data to a buffer array, so we can loop through it
2020-12-27 21:38:49 +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-02 10:17:01 +00:00
static.ACK_RECEIVED = 0 #SET AGAIN ACK_RECEIVED TO 0 SO WE CAN BE SURE WE ARE WAITING FOR THE CORRECT ACK
2020-12-27 21:38:49 +00:00
# ----------------------- 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):
2021-01-02 10:17:01 +00:00
logging.info("TX | SENDING BURST [" + str(n+1) + " / " + str(static.ARQ_TX_N_FRAMES) + "] [" + str(static.ARQ_N_SENT_FRAMES + n+1) + " / " + str(static.TX_BUFFER_SIZE) + "] [" + str(burst_payload_crc) + "]")
2020-12-27 21:38:49 +00:00
modem.Transmit(arqburst[n])
2021-01-02 10:17:01 +00:00
#LETS SLEEP SOME TIME FOR TX COOLDOWN --> CAN BE REMOVED LATER
time.sleep(2)
2021-01-02 10:17:01 +00:00
# --------------------------- START TIMER FOR WAITING FOR ACK ---> IF TIMEOUT REACHED, ACK_TIMEOUT = 1
2020-12-27 21:38:49 +00:00
static.ACK_TIMEOUT = 0
timer = threading.Timer(static.ACK_TIMEOUT_SECONDS * static.ARQ_TX_N_FRAMES, other.timeout)
timer.start()
logging.info("TX | WAITING FOR ACK")
2021-01-02 10:17:01 +00:00
# --------------------------- WHILE TIMEOUT NOT REACHED AND NO ACK RECEIVED --> LISTEN
2020-12-27 21:38:49 +00:00
while static.ACK_TIMEOUT == 0 and static.ACK_RECEIVED == 0:
static.MODEM_RECEIVE = True
2021-01-02 10:17:01 +00:00
#else:
#logging.info("TX | ACK TIMEOUT - SENDING AGAIN")
2020-12-27 21:38:49 +00:00
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED
2021-01-02 08:41:58 +00:00
######if static.ACK_RECEIVED == 1:
2020-12-27 21:38:49 +00:00
if static.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-02 08:41:58 +00:00
static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES
2020-12-27 21:38:49 +00:00
break
# ----------- if no ACK received and out of retries.....stop frame sending
if static.ACK_RECEIVED == 0:
logging.info("TX | NO ACK RECEIVED | FRAME NEEDS TO BE RESEND!")
break
#-------------------------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:
2020-12-27 21:38:49 +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
time.sleep(5)
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
logging.info("TX | BUFFER EMPTY")
# BURST MACHINE TO DEFINE N BURSTS PER FRAME
2021-01-02 08:41:58 +00:00
def get_n_frames_per_burst(len_data):
2021-01-02 10:17:01 +00:00
# I THINK WE CAN REMOVE THIS CHECK
2021-01-02 08:41:58 +00:00
if len_data <= static.ARQ_PAYLOAD_PER_FRAME:
n_frames_per_burst = 1
else:
2021-01-02 10:17:01 +00:00
#n_frames_per_burst = 2
n_frames_per_burst = randrange(1,5)
2021-01-02 08:41:58 +00:00
return n_frames_per_burst