mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
Merge pull request #221 from DJ2LS/refactor_N2KIQ_data_enums
Use FRAME_TYPE enum instead of plain int.
This commit is contained in:
commit
eeab780086
|
@ -24,8 +24,9 @@ import sock
|
|||
import static
|
||||
import structlog
|
||||
import ujson as json
|
||||
from exceptions import NoCallsign
|
||||
from codec2 import FREEDV_MODE
|
||||
from exceptions import NoCallsign
|
||||
from static import FRAME_TYPE as FR_TYPE
|
||||
|
||||
TESTMODE = False
|
||||
|
||||
|
@ -237,13 +238,23 @@ class DATA:
|
|||
frametype = int.from_bytes(bytes(bytes_out[:1]), "big")
|
||||
_valid1, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[1:4]))
|
||||
_valid2, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[2:5]))
|
||||
if _valid1 or _valid2 or frametype in [200, 201, 210, 250]:
|
||||
if (
|
||||
_valid1
|
||||
or _valid2
|
||||
or frametype
|
||||
in [
|
||||
FR_TYPE.CQ.value,
|
||||
FR_TYPE.QRV.value,
|
||||
FR_TYPE.PING.value,
|
||||
FR_TYPE.BEACON.value
|
||||
]
|
||||
):
|
||||
|
||||
# CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------
|
||||
frame = frametype - 10
|
||||
n_frames_per_burst = int.from_bytes(bytes(bytes_out[1:2]), "big")
|
||||
|
||||
if 50 >= frametype >= 10:
|
||||
if FR_TYPE.BURST_51.value >= frametype >= FR_TYPE.BURST_01.value:
|
||||
# get snr of received data
|
||||
# FIXME: find a fix for this - after moving to classes, this no longer works
|
||||
# snr = self.calculate_snr(freedv)
|
||||
|
@ -260,92 +271,93 @@ class DATA:
|
|||
# self.c_lib.freedv_set_sync(freedv, 0)
|
||||
|
||||
# BURST ACK
|
||||
elif frametype == 60:
|
||||
elif frametype == FR_TYPE.BURST_ACK.value:
|
||||
self.log.debug("[TNC] ACK RECEIVED....")
|
||||
self.burst_ack_received(bytes_out[:-2])
|
||||
|
||||
# FRAME ACK
|
||||
elif frametype == 61:
|
||||
elif frametype == FR_TYPE.FR_ACK.value:
|
||||
self.log.debug("[TNC] FRAME ACK RECEIVED....")
|
||||
self.frame_ack_received()
|
||||
|
||||
# FRAME RPT
|
||||
elif frametype == 62:
|
||||
elif frametype == FR_TYPE.FR_REPEAT.value:
|
||||
self.log.debug("[TNC] REPEAT REQUEST RECEIVED....")
|
||||
self.burst_rpt_received(bytes_out[:-2])
|
||||
|
||||
# FRAME NACK
|
||||
elif frametype == 63:
|
||||
elif frametype == FR_TYPE.FR_NACK.value:
|
||||
self.log.debug("[TNC] FRAME NACK RECEIVED....")
|
||||
self.frame_nack_received(bytes_out[:-2])
|
||||
|
||||
# BURST NACK
|
||||
elif frametype == 64:
|
||||
elif frametype == FR_TYPE.BURST_NACK.value:
|
||||
self.log.debug("[TNC] BURST NACK RECEIVED....")
|
||||
self.burst_nack_received(bytes_out[:-2])
|
||||
|
||||
# CQ FRAME
|
||||
elif frametype == 200:
|
||||
elif frametype == FR_TYPE.CQ.value:
|
||||
self.log.debug("[TNC] CQ RECEIVED....")
|
||||
self.received_cq(bytes_out[:-2])
|
||||
|
||||
# QRV FRAME
|
||||
elif frametype == 201:
|
||||
elif frametype == FR_TYPE.QRV.value:
|
||||
self.log.debug("[TNC] QRV RECEIVED....")
|
||||
self.received_qrv(bytes_out[:-2])
|
||||
|
||||
# PING FRAME
|
||||
elif frametype == 210:
|
||||
elif frametype == FR_TYPE.PING.value:
|
||||
self.log.debug("[TNC] PING RECEIVED....")
|
||||
self.received_ping(bytes_out[:-2])
|
||||
|
||||
# PING ACK
|
||||
elif frametype == 211:
|
||||
elif frametype == FR_TYPE.PING_ACK.value:
|
||||
self.log.debug("[TNC] PING ACK RECEIVED....")
|
||||
self.received_ping_ack(bytes_out[:-2])
|
||||
|
||||
# SESSION OPENER
|
||||
elif frametype == 221:
|
||||
elif frametype == FR_TYPE.ARQ_SESSION_OPEN.value:
|
||||
self.log.debug("[TNC] OPEN SESSION RECEIVED....")
|
||||
self.received_session_opener(bytes_out[:-2])
|
||||
|
||||
# SESSION HEARTBEAT
|
||||
elif frametype == 222:
|
||||
elif frametype == FR_TYPE.ARQ_SESSION_HB.value:
|
||||
self.log.debug("[TNC] SESSION HEARTBEAT RECEIVED....")
|
||||
self.received_session_heartbeat(bytes_out[:-2])
|
||||
|
||||
# SESSION CLOSE
|
||||
elif frametype == 223:
|
||||
elif frametype == FR_TYPE.ARQ_SESSION_CLOSE.value:
|
||||
self.log.debug("[TNC] CLOSE ARQ SESSION RECEIVED....")
|
||||
self.received_session_close(bytes_out[:-2])
|
||||
|
||||
# ARQ FILE TRANSFER RECEIVED!
|
||||
elif frametype in [225, 227]:
|
||||
elif frametype in [
|
||||
FR_TYPE.ARQ_DC_OPEN_W.value,
|
||||
FR_TYPE.ARQ_DC_OPEN_N.value,
|
||||
]:
|
||||
self.log.debug("[TNC] ARQ arq_received_data_channel_opener")
|
||||
self.arq_received_data_channel_opener(bytes_out[:-2])
|
||||
|
||||
# ARQ CHANNEL IS OPENED
|
||||
elif frametype in [226, 228]:
|
||||
elif frametype in [
|
||||
FR_TYPE.ARQ_DC_OPEN_ACK_W.value,
|
||||
FR_TYPE.ARQ_DC_OPEN_ACK_N.value,
|
||||
]:
|
||||
self.log.debug("[TNC] ARQ arq_received_channel_is_open")
|
||||
self.arq_received_channel_is_open(bytes_out[:-2])
|
||||
|
||||
# ARQ MANUAL MODE TRANSMISSION
|
||||
elif 230 <= frametype <= 240:
|
||||
self.log.debug("[TNC] ARQ manual mode")
|
||||
self.arq_received_data_channel_opener(bytes_out[:-2])
|
||||
|
||||
# ARQ STOP TRANSMISSION
|
||||
elif frametype == 249:
|
||||
elif frametype == FR_TYPE.ARQ_STOP.value:
|
||||
self.log.debug("[TNC] ARQ received stop transmission")
|
||||
self.received_stop_transmission()
|
||||
|
||||
# this is outdated and we may remove it
|
||||
elif frametype == 250:
|
||||
elif frametype == FR_TYPE.BEACON.value:
|
||||
self.log.debug("[TNC] BEACON RECEIVED")
|
||||
self.received_beacon(bytes_out[:-2])
|
||||
|
||||
# TESTFRAMES
|
||||
elif frametype == 255:
|
||||
elif frametype == FR_TYPE.TEST_FRAME.value:
|
||||
self.log.debug("[TNC] TESTFRAME RECEIVED", frame=bytes_out[:])
|
||||
|
||||
# Unknown frame type
|
||||
|
@ -412,7 +424,7 @@ class DATA:
|
|||
def send_burst_ack_frame(self, snr) -> None:
|
||||
"""Build and send ACK frame for burst DATA frame"""
|
||||
ack_frame = bytearray(14)
|
||||
ack_frame[:1] = bytes([60])
|
||||
ack_frame[:1] = bytes([FR_TYPE.BURST_ACK.value])
|
||||
ack_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
ack_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
ack_frame[7:8] = bytes([int(snr)])
|
||||
|
@ -424,7 +436,7 @@ class DATA:
|
|||
def send_data_ack_frame(self, snr) -> None:
|
||||
"""Build and send ACK frame for received DATA frame"""
|
||||
ack_frame = bytearray(14)
|
||||
ack_frame[:1] = bytes([61])
|
||||
ack_frame[:1] = bytes([FR_TYPE.FR_ACK.value])
|
||||
ack_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
ack_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
ack_frame[7:8] = bytes([int(snr)])
|
||||
|
@ -451,7 +463,7 @@ class DATA:
|
|||
|
||||
# then create a repeat frame
|
||||
rpt_frame = bytearray(14)
|
||||
rpt_frame[:1] = bytes([62])
|
||||
rpt_frame[:1] = bytes([FR_TYPE.FR_REPEAT.value])
|
||||
rpt_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
rpt_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
rpt_frame[7:13] = missing_frames
|
||||
|
@ -463,7 +475,7 @@ class DATA:
|
|||
def send_burst_nack_frame(self, snr: float = 0) -> None:
|
||||
"""Build and send NACK frame for received DATA frame"""
|
||||
nack_frame = bytearray(14)
|
||||
nack_frame[:1] = bytes([63])
|
||||
nack_frame[:1] = bytes([FR_TYPE.FR_NACK.value])
|
||||
nack_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
nack_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
nack_frame[7:8] = bytes([int(snr)])
|
||||
|
@ -475,7 +487,7 @@ class DATA:
|
|||
def send_burst_nack_frame_watchdog(self, snr: float = 0) -> None:
|
||||
"""Build and send NACK frame for watchdog timeout"""
|
||||
nack_frame = bytearray(14)
|
||||
nack_frame[:1] = bytes([64])
|
||||
nack_frame[:1] = bytes([FR_TYPE.BURST_NACK.value])
|
||||
nack_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
nack_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
nack_frame[7:8] = bytes([int(snr)])
|
||||
|
@ -487,7 +499,7 @@ class DATA:
|
|||
def send_disconnect_frame(self) -> None:
|
||||
"""Build and send a disconnect frame"""
|
||||
disconnection_frame = bytearray(14)
|
||||
disconnection_frame[:1] = bytes([223])
|
||||
disconnection_frame[:1] = bytes([FR_TYPE.ARQ_SESSION_CLOSE.value])
|
||||
disconnection_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
disconnection_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
disconnection_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
|
@ -926,7 +938,9 @@ class DATA:
|
|||
# TX_N_FRAMES_PER_BURST = 1 is working
|
||||
|
||||
arqheader = bytearray()
|
||||
arqheader[:1] = bytes([10]) # bytes([10 + i])
|
||||
arqheader[:1] = bytes(
|
||||
[FR_TYPE.BURST_01.value]
|
||||
) # bytes([FRAME_TYPE.BURST_01.value + i])
|
||||
arqheader[1:2] = bytes([TX_N_FRAMES_PER_BURST])
|
||||
arqheader[2:5] = static.DXCALLSIGN_CRC
|
||||
arqheader[5:8] = static.MYCALLSIGN_CRC
|
||||
|
@ -1309,7 +1323,7 @@ class DATA:
|
|||
static.ARQ_SESSION_STATE = "connecting"
|
||||
|
||||
connection_frame = bytearray(14)
|
||||
connection_frame[:1] = bytes([221])
|
||||
connection_frame[:1] = bytes([FR_TYPE.ARQ_SESSION_OPEN.value])
|
||||
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
connection_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
connection_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
|
@ -1464,7 +1478,7 @@ class DATA:
|
|||
# static.ARQ_SESSION_STATE = "connected"
|
||||
|
||||
connection_frame = bytearray(14)
|
||||
connection_frame[:1] = bytes([222])
|
||||
connection_frame[:1] = bytes([FR_TYPE.ARQ_SESSION_HB.value])
|
||||
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
connection_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
|
||||
|
@ -1575,16 +1589,13 @@ class DATA:
|
|||
self.data_channel_last_received = int(time.time())
|
||||
|
||||
if static.LOW_BANDWIDTH_MODE:
|
||||
frametype = bytes([227])
|
||||
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_N.value])
|
||||
self.log.debug("[TNC] Requesting low bandwidth mode")
|
||||
|
||||
else:
|
||||
frametype = bytes([225])
|
||||
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_W.value])
|
||||
self.log.debug("[TNC] Requesting high bandwidth mode")
|
||||
|
||||
if 230 <= mode <= 240:
|
||||
self.log.debug("[TNC] Requesting manual mode --> not yet implemented ")
|
||||
frametype = bytes([mode])
|
||||
connection_frame = bytearray(14)
|
||||
connection_frame[:1] = frametype
|
||||
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
|
@ -1675,7 +1686,7 @@ class DATA:
|
|||
n_frames_per_burst = int.from_bytes(bytes(data_in[13:14]), "big")
|
||||
frametype = int.from_bytes(bytes(data_in[:1]), "big")
|
||||
# check if we received low bandwidth mode
|
||||
if frametype == 225:
|
||||
if frametype == FR_TYPE.ARQ_DC_OPEN_W.value:
|
||||
self.received_LOW_BANDWIDTH_MODE = False
|
||||
self.mode_list = self.mode_list_high_bw
|
||||
self.time_list = self.time_list_high_bw
|
||||
|
@ -1685,11 +1696,6 @@ class DATA:
|
|||
self.time_list = self.time_list_low_bw
|
||||
self.speed_level = len(self.mode_list) - 1
|
||||
|
||||
if 230 <= frametype <= 240:
|
||||
self.log.debug(
|
||||
"[TNC] arq_received_data_channel_opener: manual mode request"
|
||||
)
|
||||
|
||||
# Update modes we are listening to
|
||||
self.set_listening_modes(self.mode_list[self.speed_level])
|
||||
|
||||
|
@ -1728,10 +1734,10 @@ class DATA:
|
|||
|
||||
# Select the frame type based on the current TNC mode
|
||||
if static.LOW_BANDWIDTH_MODE or self.received_LOW_BANDWIDTH_MODE:
|
||||
frametype = bytes([228])
|
||||
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_ACK_N.value])
|
||||
self.log.debug("[TNC] Responding with low bandwidth mode")
|
||||
else:
|
||||
frametype = bytes([226])
|
||||
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_ACK_W.value])
|
||||
self.log.debug("[TNC] Responding with high bandwidth mode")
|
||||
|
||||
connection_frame = bytearray(14)
|
||||
|
@ -1775,7 +1781,7 @@ class DATA:
|
|||
)
|
||||
frametype = int.from_bytes(bytes(data_in[:1]), "big")
|
||||
|
||||
if frametype == 228:
|
||||
if frametype == FR_TYPE.ARQ_DC_OPEN_ACK_N.value:
|
||||
self.received_LOW_BANDWIDTH_MODE = True
|
||||
self.mode_list = self.mode_list_low_bw
|
||||
self.time_list = self.time_list_low_bw
|
||||
|
@ -1850,7 +1856,7 @@ class DATA:
|
|||
)
|
||||
|
||||
ping_frame = bytearray(14)
|
||||
ping_frame[:1] = bytes([210])
|
||||
ping_frame[:1] = bytes([FR_TYPE.PING.value])
|
||||
ping_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
ping_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
ping_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
|
@ -1907,7 +1913,7 @@ class DATA:
|
|||
)
|
||||
|
||||
ping_frame = bytearray(14)
|
||||
ping_frame[:1] = bytes([211])
|
||||
ping_frame[:1] = bytes([FR_TYPE.PING_ACK.value])
|
||||
ping_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
ping_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
ping_frame[7:13] = static.MYGRID
|
||||
|
@ -1964,7 +1970,7 @@ class DATA:
|
|||
"""
|
||||
self.log.warning("[TNC] Stopping transmission!")
|
||||
stop_frame = bytearray(14)
|
||||
stop_frame[:1] = bytes([249])
|
||||
stop_frame[:1] = bytes([FR_TYPE.ARQ_STOP.value])
|
||||
stop_frame[1:4] = static.DXCALLSIGN_CRC
|
||||
stop_frame[4:7] = static.MYCALLSIGN_CRC
|
||||
stop_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
|
@ -2027,7 +2033,7 @@ class DATA:
|
|||
)
|
||||
|
||||
beacon_frame = bytearray(14)
|
||||
beacon_frame[:1] = bytes([250])
|
||||
beacon_frame[:1] = bytes([FR_TYPE.BEACON.value])
|
||||
beacon_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
beacon_frame[9:13] = static.MYGRID[:4]
|
||||
self.log.info("[TNC] ENABLE FSK", state=static.ENABLE_FSK)
|
||||
|
@ -2105,7 +2111,7 @@ class DATA:
|
|||
cq="transmitting",
|
||||
)
|
||||
cq_frame = bytearray(14)
|
||||
cq_frame[:1] = bytes([200])
|
||||
cq_frame[:1] = bytes([FR_TYPE.CQ.value])
|
||||
cq_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
cq_frame[7:11] = helpers.encode_grid(static.MYGRID.decode("UTF-8"))
|
||||
|
||||
|
@ -2176,7 +2182,7 @@ class DATA:
|
|||
self.log.info("[TNC] Sending QRV!")
|
||||
|
||||
qrv_frame = bytearray(14)
|
||||
qrv_frame[:1] = bytes([201])
|
||||
qrv_frame[:1] = bytes([FR_TYPE.QRV.value])
|
||||
qrv_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign)
|
||||
qrv_frame[7:11] = helpers.encode_grid(static.MYGRID.decode("UTF-8"))
|
||||
|
||||
|
|
|
@ -130,22 +130,9 @@ class FRAME_TYPE(Enum):
|
|||
"""Lookup for frame types"""
|
||||
|
||||
BURST_01 = 10
|
||||
BURST_02 = 11
|
||||
BURST_03 = 12
|
||||
BURST_04 = 13
|
||||
BURST_05 = 14
|
||||
BURST_06 = 15
|
||||
BURST_07 = 16
|
||||
BURST_08 = 17
|
||||
BURST_09 = 18
|
||||
BURST_10 = 19
|
||||
BURST_11 = 20
|
||||
BURST_12 = 21
|
||||
BURST_13 = 22
|
||||
BURST_14 = 23
|
||||
BURST_15 = 24
|
||||
BURST_16 = 25
|
||||
ACK = 60
|
||||
# ...
|
||||
BURST_51 = 50
|
||||
BURST_ACK = 60
|
||||
FR_ACK = 61
|
||||
FR_REPEAT = 62
|
||||
FR_NACK = 63
|
||||
|
|
Loading…
Reference in a new issue