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
2020-12-28 14:20:51 +00:00
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
2021-01-06 12:17:17 +00:00
import helpers
2021-02-05 13:40:32 +00:00
import main
2020-12-27 21:38:49 +00:00
2021-02-08 15:33:11 +00:00
2021-01-06 12:17:17 +00:00
modem = modem . RF ( )
2020-12-29 10:10:02 +00:00
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 #
2021-01-06 12:17:17 +00:00
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-02-08 20:25:22 +00:00
def arq_rpt_timeout ( ) :
static . ARQ_RPT_TIMEOUT = True
2021-01-16 14:04:24 +00:00
2020-12-28 14:20:51 +00:00
def data_received ( data_in ) :
2021-02-08 18:00:12 +00:00
2021-02-05 13:40:32 +00:00
# arqframe = frame_type + \ # 1 [:1] # frame type and current number of arq frame of (current) burst
# bytes([static.ARQ_TX_N_FRAMES_PER_BURST]) + \ # 1 [1:2] # total number of arq frames per (current) burst
# static.ARQ_N_CURRENT_ARQ_FRAME + \ # 2 [2:4] # current arq frame number
# static.ARQ_N_TOTAL_ARQ_FRAMES + \ # 2 [4:6] # total number arq frames
# static.ARQ_BURST_PAYLOAD_CRC + \ # 2 [6:8] # arq crc
# payload_data # N [8:N] # payload data
2021-02-08 18:00:12 +00:00
# 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
2021-02-01 20:46:33 +00:00
static . ARQ_N_RX_FRAMES_PER_BURSTS = int . from_bytes ( bytes ( data_in [ 1 : 2 ] ) , " big " ) #get number of bursts from received frame
2021-02-05 13:40:32 +00:00
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_BURST_PAYLOAD_CRC = data_in [ 6 : 8 ]
2021-02-06 14:11:42 +00:00
logging . debug ( " ---------------------------------------------------------------- " )
logging . debug ( " ARQ_N_FRAME: " + str ( static . ARQ_N_FRAME ) )
logging . debug ( " ARQ_N_RX_FRAMES_PER_BURSTS: " + str ( static . ARQ_N_RX_FRAMES_PER_BURSTS ) )
logging . debug ( " ARQ_RX_N_CURRENT_ARQ_FRAME: " + str ( static . ARQ_RX_N_CURRENT_ARQ_FRAME ) )
logging . debug ( " ARQ_N_ARQ_FRAMES_PER_DATA_FRAME: " + str ( static . ARQ_N_ARQ_FRAMES_PER_DATA_FRAME ) )
logging . debug ( " static.ARQ_BURST_PAYLOAD_CRC: " + str ( static . ARQ_BURST_PAYLOAD_CRC ) )
logging . debug ( " ---------------------------------------------------------------- " )
2021-02-05 13:40:32 +00:00
2021-01-16 14:04:24 +00:00
2021-02-04 16:51:01 +00:00
arq_percent_burst = int ( ( static . ARQ_N_FRAME / static . ARQ_N_RX_FRAMES_PER_BURSTS ) * 100 )
2021-02-05 13:40:32 +00:00
arq_percent_frame = int ( ( ( static . ARQ_RX_N_CURRENT_ARQ_FRAME ) / static . ARQ_N_ARQ_FRAMES_PER_DATA_FRAME ) * 100 )
logging . info ( " ARQ | RX | ARQ FRAME [ " + str ( static . ARQ_N_FRAME ) + " / " + str ( static . ARQ_N_RX_FRAMES_PER_BURSTS ) + " ] [ " + str ( arq_percent_burst ) . zfill ( 3 ) + " % ] --- TOTAL [ " + str ( static . ARQ_RX_N_CURRENT_ARQ_FRAME ) + " / " + str ( static . ARQ_N_ARQ_FRAMES_PER_DATA_FRAME ) + " ] [ " + str ( arq_percent_frame ) . zfill ( 3 ) + " % ] " )
2021-02-04 16:51:01 +00:00
2021-02-08 15:33:11 +00:00
2020-12-29 10:10:02 +00:00
2021-02-05 13:40:32 +00:00
#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
if static . ARQ_N_ARQ_FRAMES_PER_DATA_FRAME != len ( static . ARQ_RX_FRAME_BUFFER ) and static . ARQ_RX_N_CURRENT_ARQ_FRAME == 1 :
2021-02-08 18:00:12 +00:00
static . ARQ_RX_FRAME_BUFFER = [ ]
2021-02-05 13:40:32 +00:00
for i in range ( 0 , static . ARQ_N_ARQ_FRAMES_PER_DATA_FRAME + 1 ) :
static . ARQ_RX_FRAME_BUFFER . insert ( i , None )
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
2021-02-08 20:25:22 +00:00
print ( len ( static . ARQ_RX_BURST_BUFFER ) )
print ( static . ARQ_N_FRAME )
2021-02-08 18:00:12 +00:00
if static . ARQ_N_RX_FRAMES_PER_BURSTS != len ( static . ARQ_RX_BURST_BUFFER ) and static . ARQ_N_FRAME == 1 and frame_type != 51 :
static . ARQ_RX_BURST_BUFFER = [ ]
2021-02-08 20:25:22 +00:00
print ( " ITERATOR!!!!!!!!!!!!!! " )
2021-02-01 20:46:33 +00:00
for i in range ( 0 , static . ARQ_N_RX_FRAMES_PER_BURSTS + 1 ) :
static . ARQ_RX_BURST_BUFFER . insert ( i , None )
2021-02-05 13:40:32 +00:00
2021-02-08 15:33:11 +00:00
2021-02-05 13:40:32 +00:00
# now we add the incoming data to the specified position in our lists
2021-02-01 20:46:33 +00:00
static . ARQ_RX_BURST_BUFFER [ static . ARQ_N_FRAME ] = bytes ( data_in )
2021-02-05 13:40:32 +00:00
static . ARQ_RX_FRAME_BUFFER [ static . ARQ_RX_N_CURRENT_ARQ_FRAME ] = bytes ( data_in )
2021-02-08 18:00:12 +00:00
2021-02-08 20:25:22 +00:00
for i in range ( len ( static . ARQ_RX_BURST_BUFFER ) ) :
print ( static . ARQ_RX_BURST_BUFFER [ i ] )
# - ------------------------- ARQ BURST CHECKER
2021-02-06 14:11:42 +00:00
2021-02-04 14:25:15 +00:00
# run only if we recieved all ARQ FRAMES per ARQ BURST
2021-02-08 20:25:22 +00:00
#if static.ARQ_N_FRAME == static.ARQ_N_RX_FRAMES_PER_BURSTS and static.ARQ_RX_BURST_BUFFER.count(None) == 1: #if received bursts are equal to burst number in frame
if static . ARQ_RX_BURST_BUFFER . count ( None ) == 1 : #count nones
2021-02-08 18:00:12 +00:00
logging . info ( " ARQ | TX | BURST ACK " )
2021-01-16 14:04:24 +00:00
2021-02-08 18:00:12 +00:00
#BUILDING ACK FRAME FOR BURST -----------------------------------------------
ack_payload = b ' ACK FRAME '
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
2020-12-30 17:16:32 +00:00
2021-02-08 18:00:12 +00:00
#TRANSMIT ACK FRAME FOR BURST-----------------------------------------------
2021-02-08 15:33:11 +00:00
modem . transmit_arq_ack ( ack_buffer )
2021-02-08 18:00:12 +00:00
#clear burst buffer
2021-02-08 15:33:11 +00:00
static . ARQ_RX_BURST_BUFFER = [ ]
2021-02-08 18:00:12 +00:00
#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 :
print ( " RPT FRAME NEEDS TO BE SEND!!!! " )
# --------------- CHECK WHICH BURST FRAMES WE ARE MISSING -------------------------------------------
missing_frames = b ' '
for burst in range ( 1 , len ( static . ARQ_RX_BURST_BUFFER ) ) :
if static . ARQ_RX_BURST_BUFFER [ burst ] == None :
print ( burst )
burst = burst . to_bytes ( 2 , byteorder = ' big ' )
missing_frames + = burst
print ( " MISSING FRAMES: " + str ( missing_frames ) )
rpt_payload = missing_frames
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
2021-02-05 13:40:32 +00:00
2021-02-08 18:00:12 +00:00
modem . transmit_arq_ack ( rpt_buffer )
2021-02-05 13:40:32 +00:00
# ---------------------------- FRAME MACHINE
# --------------- CHECK IF WE ARE MISSING FRAMES -------------------------------------------
2021-02-06 14:11:42 +00:00
#for frame in range(1,len(static.ARQ_RX_FRAME_BUFFER)):
# if static.ARQ_RX_FRAME_BUFFER[frame] == None:
# print("Missing frames:" + str(frame))
2021-02-05 13:40:32 +00:00
# --------------- IF LIST NOT CONTAINS "None" stick everything together
complete_data_frame = bytearray ( )
2021-02-08 20:25:22 +00:00
print ( " static.ARQ_RX_FRAME_BUFFER.count(None) " + str ( static . ARQ_RX_FRAME_BUFFER . count ( None ) ) )
2021-02-05 13:40:32 +00:00
if static . ARQ_RX_FRAME_BUFFER . count ( None ) == 1 : ## 1 because position 0 of list will alaways be None in our case
2021-02-08 20:25:22 +00:00
print ( " DECODING FRAME! " )
2021-02-05 13:40:32 +00:00
for frame in range ( 1 , len ( static . ARQ_RX_FRAME_BUFFER ) ) :
raw_arq_frame = static . ARQ_RX_FRAME_BUFFER [ frame ]
arq_frame_payload = raw_arq_frame [ 8 : ]
# -------- DETECT IF WE RECEIVED A FRAME HEADER THEN SAVE DATA TO GLOBALS
if arq_frame_payload [ 2 : 4 ] . startswith ( static . FRAME_BOF ) :
static . FRAME_CRC = arq_frame_payload [ : 2 ]
static . ARQ_FRAME_BOF_RECEIVED = True
arq_frame_payload = arq_frame_payload . split ( static . FRAME_BOF )
arq_frame_payload = arq_frame_payload [ 1 ]
# -------- DETECT IF WE RECEIVED A FRAME FOOTER THEN SAVE DATA TO GLOBALS
if arq_frame_payload . rstrip ( b ' \x00 ' ) . endswith ( static . FRAME_EOF ) :
static . ARQ_FRAME_EOF_RECEIVED = True
arq_frame_payload = arq_frame_payload . split ( static . FRAME_EOF )
arq_frame_payload = arq_frame_payload [ 0 ]
2021-02-06 14:11:42 +00:00
2021-02-08 15:33:11 +00:00
# --------- AFTER WE SEPARATED BOF AND EOF, STICK EVERYTHING TOGETHER
2021-02-05 13:40:32 +00:00
complete_data_frame = complete_data_frame + arq_frame_payload
2021-02-08 18:00:12 +00:00
2021-02-05 13:40:32 +00:00
2021-02-04 14:25:15 +00:00
#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 :
2021-02-05 13:40:32 +00:00
frame_payload_crc = helpers . get_crc_16 ( complete_data_frame )
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 " )
2021-02-08 15:33:11 +00:00
#append received frame to RX_BUFFER
2021-02-05 13:40:32 +00:00
static . RX_BUFFER . append ( complete_data_frame )
2021-02-08 15:33:11 +00:00
2021-02-06 14:11:42 +00:00
#BUILDING ACK FRAME FOR DATA FRAME -----------------------------------------------
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-----------------------------------------------
logging . info ( " ARQ | TX | ARQ DATA FRAME ACK [ " + str ( static . FRAME_CRC . hex ( ) ) + " ] " )
modem . transmit_arq_ack ( ack_buffer )
2021-02-04 16:51:01 +00:00
# clearing buffers and resetting counters
2021-02-05 13:40:32 +00:00
static . ARQ_RX_BURST_BUFFER = [ ]
2021-02-04 14:25:15 +00:00
static . ARQ_RX_FRAME_BUFFER = [ ]
static . ARQ_FRAME_BOF_RECEIVED = False
static . ARQ_FRAME_EOF_RECEIVED = False
2021-02-04 16:51:01 +00:00
static . ARQ_N_ARQ_FRAMES_PER_DATA_FRAME = 0
2021-02-04 14:25:15 +00:00
2021-02-06 14:11:42 +00:00
#print("----------------------------------------------------------------")
#print(static.RX_BUFFER[-1])
#print("----------------------------------------------------------------")
2021-02-05 13:40:32 +00:00
2021-02-04 14:25:15 +00:00
else :
logging . info ( " ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED! " )
2021-02-08 18:00:12 +00:00
2021-02-08 20:25:22 +00:00
#break
2020-12-28 14:20:51 +00:00
2021-02-06 14:11:42 +00:00
def burst_ack_received ( ) :
2020-12-28 14:20:51 +00:00
2021-02-08 18:00:12 +00:00
logging . info ( " ARQ | RX | BURST 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-08 20:25:22 +00:00
static . RPT_ACK_RECEIVED = False
2021-02-08 18:00:12 +00:00
def burst_rpt_received ( data_in ) :
logging . info ( " ARQ | RX | BURST RPT RCVD! " )
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_RPT_RECEIVED = True
#static.ARQ_RPT_FRAMES = b'123'
missing = 3
missing = missing . to_bytes ( 2 , byteorder = ' big ' )
static . ARQ_RPT_FRAMES . insert ( 0 , missing )
print ( bytes ( data_in ) )
2021-02-06 14:11:42 +00:00
def frame_ack_received ( ) :
2021-02-08 18:00:12 +00:00
logging . info ( " ARQ | RX | FRAME ACK RCVD! " )
2021-02-06 14:11:42 +00:00
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
2020-12-27 21:38:49 +00:00
def transmit ( data_out ) :
2021-02-01 20:46:33 +00:00
2021-02-05 13:40:32 +00:00
static . ARQ_PAYLOAD_PER_FRAME = static . FREEDV_DATA_PAYLOAD_PER_FRAME - 8 #3 ohne ARQ_TX_N_FRAMES_PER_BURST
frame_header_length = 4
2021-02-08 15:33:11 +00:00
n_arq_frames_per_data_frame = ( len ( data_out ) + frame_header_length ) / / static . ARQ_PAYLOAD_PER_FRAME + ( ( len ( data_out ) + frame_header_length ) % static . ARQ_PAYLOAD_PER_FRAME > 0 )
2021-02-05 13:40:32 +00:00
2021-01-06 12:17:17 +00:00
frame_payload_crc = helpers . get_crc_16 ( data_out )
2021-02-08 15:33:11 +00:00
2021-01-02 10:17:01 +00:00
# This is the total frame with frame header, which will be send
2021-02-05 13:40:32 +00:00
data_out = frame_payload_crc + static . FRAME_BOF + data_out + static . FRAME_EOF
# 2 2 N 2
2021-02-04 16:51:01 +00:00
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-28 14:20:51 +00:00
2021-02-04 16:51:01 +00:00
logging . info ( " ARQ | TX | DATA FRAME --- BYTES: " + str ( len ( data_out ) ) + " ARQ FRAMES: " + 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-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-02-08 15:33:11 +00:00
2021-01-02 10:17:01 +00:00
try : # DETECT IF LAST BURST TO PREVENT INDEX ERROR OF BUFFER
2021-01-20 21:51:14 +00:00
2021-02-08 15:33:11 +00:00
for i in range ( static . ARQ_TX_N_FRAMES_PER_BURST ) : # Loop through TX_BUFFER LIST
len ( static . TX_BUFFER [ static . ARQ_N_SENT_FRAMES + i ] ) #we calculate the length to trigger a list index error
2021-01-20 21:51:14 +00:00
except IndexError : # IF LAST BURST DETECTED BUILD CRC WITH LESS FRAMES AND SET static.ARQ_TX_N_FRAMES_PER_BURST TO VALUE OF REST!
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-02-08 15:33:11 +00:00
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
2021-02-04 16:51:01 +00:00
static . ARQ_TX_N_FRAMES_PER_BURST = static . TX_BUFFER_SIZE - static . ARQ_N_SENT_FRAMES
2021-02-08 15:33:11 +00:00
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-02-08 15:33:11 +00:00
2021-02-05 13:40:32 +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-02-06 14:11:42 +00:00
if static . ARQ_N_SENT_FRAMES + 1 < = static . TX_BUFFER_SIZE :
2021-02-08 15:33:11 +00:00
logging . info ( " ARQ | TX | F:[ " + str ( static . ARQ_N_SENT_FRAMES + 1 ) + " - " + str ( static . ARQ_N_SENT_FRAMES + static . ARQ_TX_N_FRAMES_PER_BURST ) + " ] | T:[ " + str ( static . ARQ_N_SENT_FRAMES ) + " / " + str ( static . TX_BUFFER_SIZE ) + " ] [ " + str ( int ( static . ARQ_N_SENT_FRAMES / ( static . TX_BUFFER_SIZE ) * 100 ) ) . zfill ( 3 ) + " % ] | A:[ " + str ( static . TX_N_RETRIES + 1 ) + " / " + str ( static . TX_N_MAX_RETRIES ) + " ] " )
2021-02-06 14:11:42 +00:00
2021-02-08 15:33:11 +00:00
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
2021-02-06 14:11:42 +00:00
static . ARQ_FRAME_ACK_RECEIVED = 0
2021-02-04 14:25:15 +00:00
static . ARQ_ACK_RECEIVED = 0
static . ARQ_ACK_TIMEOUT = 0
2021-02-06 14:11:42 +00:00
logging . debug ( " ARQ | RX | WAITING FOR BURST ACK " )
2021-01-20 14:03:42 +00:00
static . ARQ_STATE = ' RECEIVING_ACK '
2021-02-08 20:25:22 +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-06 12:17:17 +00:00
2021-01-02 10:17:01 +00:00
# --------------------------- WHILE TIMEOUT NOT REACHED AND NO ACK RECEIVED --> LISTEN
2021-02-08 18:00:12 +00:00
while static . ARQ_ACK_TIMEOUT == 0 and static . ARQ_RPT_RECEIVED == False and static . ARQ_ACK_RECEIVED == 0 :
2021-01-20 22:42:45 +00:00
time . sleep ( 0.01 ) # lets reduce CPU load a little bit
2021-02-04 16:51:01 +00:00
#print(static.ARQ_STATE)
2021-02-08 20:25:22 +00:00
2021-02-08 18:00:12 +00:00
#--------------------------------------------------------------------------------------------------------------
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 . start ( )
# lets wait during sending. After sending is finished we will continue
while static . ARQ_STATE == ' SENDING_DATA ' :
2021-02-08 20:25:22 +00:00
time . sleep ( 0.05 )
2021-02-08 18:00:12 +00:00
static . ARQ_FRAME_ACK_RECEIVED = 0
static . ARQ_ACK_RECEIVED = 0
static . ARQ_ACK_TIMEOUT = 0
static . ARQ_STATE = ' RECEIVING_ACK '
2021-02-08 20:25:22 +00:00
timer = threading . Timer ( static . ARQ_RPT_TIMEOUT_SECONDS , arq_rpt_timeout )
2021-02-08 18:00:12 +00:00
timer . start ( )
2021-02-08 20:25:22 +00:00
print ( " kommen wir hier überhaupt an? " )
while static . ARQ_ACK_TIMEOUT == 0 and static . ARQ_ACK_RECEIVED == 0 and static . ARQ_RPT_TIMEOUT == False :
2021-02-08 18:00:12 +00:00
time . sleep ( 0.01 ) # lets reduce CPU load a little bit
2021-02-08 20:25:22 +00:00
print ( " waiting for ack while rpt " )
if static . ARQ_ACK_RECEIVED == 1 :
print ( " ACK WHILE RPT " )
time . sleep ( 1 )
static . ARQ_ACK_TIMEOUT = 1
static . ARQ_RPT_RECEIVED = False
static . ARQ_RPT_TIMEOUT == False
break
2021-02-08 18:00:12 +00:00
#--------------------------------------------------------------------------------------------------------------
2021-02-08 20:25:22 +00:00
if static . ARQ_ACK_RECEIVED == 0 and static . ARQ_ACK_TIMEOUT == 1 and static . ARQ_RPT_TIMEOUT == True :
2021-02-04 16:51:01 +00:00
#logging.info("ARQ | RX | ACK TIMEOUT | SENDING ARQ BURST AGAIN")
pass
2021-02-04 14:25:15 +00:00
2021-02-06 14:11:42 +00:00
#--------------- BREAK LOOP IF ACK HAS BEEN RECEIVED OR FRAME ACK HAS BEEN RECEIVED
2021-02-08 20:25:22 +00:00
if static . ARQ_ACK_RECEIVED == 1 :
print ( " der interator increment ist wichtig! " )
2021-02-06 14:11:42 +00:00
#-----------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
break
#--------------- BREAK LOOP IF FRAME ACK HAS BEEN RECEIVED EARLIER AS EXPECTED
if static . ARQ_FRAME_ACK_RECEIVED == 1 :
logging . info ( " ARQ | RX | EARLY FRAME ACK RECEIVED - STOPPING TX " )
#static.ARQ_N_SENT_FRAMES = #static.TX_BUFFER_SIZE
static . ARQ_N_SENT_FRAMES = static . ARQ_N_SENT_FRAMES + static . ARQ_TX_N_FRAMES_PER_BURST
break
2020-12-27 21:38:49 +00:00
# ----------- if no ACK received and out of retries.....stop frame sending
2021-02-06 14:11:42 +00:00
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! " )
2021-02-08 15:33:11 +00:00
2021-02-04 16:51:01 +00:00
break
2020-12-27 21:38:49 +00:00
2021-02-06 14:11:42 +00:00
#-------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT AND WE GOT A FRAME ACK
if static . ARQ_N_SENT_FRAMES == static . TX_BUFFER_SIZE and static . ARQ_FRAME_ACK_RECEIVED == 1 :
logging . info ( " ARQ | RX | REGULAR FRAME ACK RECEIVED - DATA TRANSMITTED! " )
2021-02-04 14:25:15 +00:00
break
2021-02-06 14:11:42 +00:00
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-06 14:11:42 +00:00
2021-02-01 20:46:33 +00:00
logging . info ( " ARQ | TX | BUFFER EMPTY " )
2021-01-21 20:00:21 +00:00
# - 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-02-06 14:11:42 +00:00
static . ARQ_FRAME_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-08 20:25:22 +00:00
#n_frames_per_burst = randrange(1,10)
n_frames_per_burst = 4
2021-01-02 08:41:58 +00:00
return n_frames_per_burst