2022-04-01 15:18:18 +00:00
# MIT License
#
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2020-04-22 10:07:13 +00:00
from . Interface import Interface
2018-04-04 12:14:22 +00:00
from time import sleep
import sys
import threading
import time
import RNS
class KISS ( ) :
2020-08-13 10:15:56 +00:00
FEND = 0xC0
FESC = 0xDB
TFEND = 0xDC
TFESC = 0xDD
CMD_UNKNOWN = 0xFE
CMD_DATA = 0x00
CMD_TXDELAY = 0x01
CMD_P = 0x02
CMD_SLOTTIME = 0x03
CMD_TXTAIL = 0x04
CMD_FULLDUPLEX = 0x05
CMD_SETHARDWARE = 0x06
CMD_READY = 0x0F
CMD_RETURN = 0xFF
@staticmethod
def escape ( data ) :
data = data . replace ( bytes ( [ 0xdb ] ) , bytes ( [ 0xdb , 0xdd ] ) )
data = data . replace ( bytes ( [ 0xc0 ] ) , bytes ( [ 0xdb , 0xdc ] ) )
return data
2020-05-03 21:06:53 +00:00
2018-04-08 10:26:57 +00:00
class AX25 ( ) :
2020-08-13 10:15:56 +00:00
PID_NOLAYER3 = 0xF0
CTRL_UI = 0x03
CRC_CORRECT = bytes ( [ 0xF0 ] ) + bytes ( [ 0xB8 ] )
HEADER_SIZE = 16
2018-04-08 10:26:57 +00:00
class AX25KISSInterface ( Interface ) :
2020-08-13 10:15:56 +00:00
MAX_CHUNK = 32768
2022-04-17 17:35:31 +00:00
BITRATE_GUESS = 1200
2020-08-13 10:15:56 +00:00
owner = None
port = None
speed = None
databits = None
parity = None
stopbits = None
serial = None
def __init__ ( self , owner , name , callsign , ssid , port , speed , databits , parity , stopbits , preamble , txtail , persistence , slottime , flow_control ) :
2021-12-01 12:57:40 +00:00
import importlib
2021-12-01 12:39:51 +00:00
if importlib . util . find_spec ( ' serial ' ) != None :
import serial
else :
RNS . log ( " Using the AX.25 KISS interface requires a serial communication module to be installed. " , RNS . LOG_CRITICAL )
RNS . log ( " You can install one with the command: python3 -m pip install pyserial " , RNS . LOG_CRITICAL )
RNS . panic ( )
2023-09-30 17:11:10 +00:00
super ( ) . __init__ ( )
2022-05-29 13:43:50 +00:00
self . HW_MTU = 564
2024-10-07 08:52:43 +00:00
2021-12-05 13:35:25 +00:00
self . pyserial = serial
2020-08-13 10:15:56 +00:00
self . serial = None
self . owner = owner
self . name = name
self . src_call = callsign . upper ( ) . encode ( " ascii " )
self . src_ssid = ssid
2024-10-07 08:44:18 +00:00
self . dst_call = b " APZRNS "
2020-08-13 10:15:56 +00:00
self . dst_ssid = 0
self . port = port
self . speed = speed
self . databits = databits
self . parity = serial . PARITY_NONE
self . stopbits = stopbits
self . timeout = 100
self . online = False
2024-09-16 02:27:08 +00:00
self . bitrate = AX25KISSInterface . BITRATE_GUESS
2020-08-13 10:15:56 +00:00
self . packet_queue = [ ]
self . flow_control = flow_control
self . interface_ready = False
2021-09-03 08:56:49 +00:00
self . flow_control_timeout = 5
self . flow_control_locked = time . time ( )
2020-08-13 10:15:56 +00:00
if ( len ( self . src_call ) < 3 or len ( self . src_call ) > 6 ) :
2024-10-07 08:44:18 +00:00
raise ValueError ( f " Invalid callsign for { self } " )
2020-08-13 10:15:56 +00:00
if ( self . src_ssid < 0 or self . src_ssid > 15 ) :
2024-10-07 08:44:18 +00:00
raise ValueError ( f " Invalid SSID for { self } " )
2020-08-13 10:15:56 +00:00
self . preamble = preamble if preamble != None else 350 ;
self . txtail = txtail if txtail != None else 20 ;
self . persistence = persistence if persistence != None else 64 ;
self . slottime = slottime if slottime != None else 20 ;
if parity . lower ( ) == " e " or parity . lower ( ) == " even " :
self . parity = serial . PARITY_EVEN
if parity . lower ( ) == " o " or parity . lower ( ) == " odd " :
self . parity = serial . PARITY_ODD
try :
2021-12-05 13:35:25 +00:00
self . open_port ( )
2020-08-13 10:15:56 +00:00
except Exception as e :
2024-10-07 08:44:18 +00:00
RNS . log ( f " Could not open serial port for interface { self } " , RNS . LOG_ERROR )
2020-08-13 10:15:56 +00:00
raise e
if self . serial . is_open :
2021-12-05 13:35:25 +00:00
self . configure_device ( )
2020-08-13 10:15:56 +00:00
else :
2024-10-07 08:44:18 +00:00
raise OSError ( " Could not open serial port " )
2020-08-13 10:15:56 +00:00
2021-12-05 13:35:25 +00:00
def open_port ( self ) :
2024-10-07 08:44:18 +00:00
RNS . log ( f " Opening serial port { self . port } ... " , RNS . LOG_VERBOSE )
2021-12-05 13:35:25 +00:00
self . serial = self . pyserial . Serial (
port = self . port ,
baudrate = self . speed ,
bytesize = self . databits ,
parity = self . parity ,
stopbits = self . stopbits ,
xonxoff = False ,
rtscts = False ,
timeout = 0 ,
inter_byte_timeout = None ,
write_timeout = None ,
dsrdtr = False ,
)
def configure_device ( self ) :
# Allow time for interface to initialise before config
sleep ( 2.0 )
thread = threading . Thread ( target = self . readLoop )
2022-09-30 17:02:25 +00:00
thread . daemon = True
2021-12-05 13:35:25 +00:00
thread . start ( )
self . online = True
2024-10-07 08:44:18 +00:00
RNS . log ( f " Serial port { self . port } is now open " )
2021-12-05 13:35:25 +00:00
RNS . log ( " Configuring AX.25 KISS interface parameters... " )
self . setPreamble ( self . preamble )
self . setTxTail ( self . txtail )
self . setPersistence ( self . persistence )
self . setSlotTime ( self . slottime )
self . setFlowControl ( self . flow_control )
self . interface_ready = True
RNS . log ( " AX.25 KISS interface configured " )
2020-08-13 10:15:56 +00:00
def setPreamble ( self , preamble ) :
preamble_ms = preamble
preamble = int ( preamble_ms / 10 )
if preamble < 0 :
preamble = 0
if preamble > 255 :
preamble = 255
kiss_command = bytes ( [ KISS . FEND ] ) + bytes ( [ KISS . CMD_TXDELAY ] ) + bytes ( [ preamble ] ) + bytes ( [ KISS . FEND ] )
written = self . serial . write ( kiss_command )
if written != len ( kiss_command ) :
2024-10-07 08:44:18 +00:00
raise OSError ( f " Could not configure AX.25 KISS interface preamble to { preamble_ms } (command value { preamble } ) " )
2020-08-13 10:15:56 +00:00
def setTxTail ( self , txtail ) :
txtail_ms = txtail
txtail = int ( txtail_ms / 10 )
if txtail < 0 :
txtail = 0
if txtail > 255 :
txtail = 255
kiss_command = bytes ( [ KISS . FEND ] ) + bytes ( [ KISS . CMD_TXTAIL ] ) + bytes ( [ txtail ] ) + bytes ( [ KISS . FEND ] )
written = self . serial . write ( kiss_command )
if written != len ( kiss_command ) :
2024-10-07 08:44:18 +00:00
raise OSError ( f " Could not configure AX.25 KISS interface TX tail to { txtail_ms } (command value { txtail } ) " )
2020-08-13 10:15:56 +00:00
def setPersistence ( self , persistence ) :
if persistence < 0 :
persistence = 0
if persistence > 255 :
persistence = 255
kiss_command = bytes ( [ KISS . FEND ] ) + bytes ( [ KISS . CMD_P ] ) + bytes ( [ persistence ] ) + bytes ( [ KISS . FEND ] )
written = self . serial . write ( kiss_command )
if written != len ( kiss_command ) :
2024-10-07 08:44:18 +00:00
raise OSError ( f " Could not configure AX.25 KISS interface persistence to { persistence } " )
2020-08-13 10:15:56 +00:00
def setSlotTime ( self , slottime ) :
slottime_ms = slottime
slottime = int ( slottime_ms / 10 )
if slottime < 0 :
slottime = 0
if slottime > 255 :
slottime = 255
kiss_command = bytes ( [ KISS . FEND ] ) + bytes ( [ KISS . CMD_SLOTTIME ] ) + bytes ( [ slottime ] ) + bytes ( [ KISS . FEND ] )
written = self . serial . write ( kiss_command )
if written != len ( kiss_command ) :
2024-10-07 08:44:18 +00:00
raise OSError ( f " Could not configure AX.25 KISS interface slot time to { slottime_ms } (command value { slottime } ) " )
2020-08-13 10:15:56 +00:00
def setFlowControl ( self , flow_control ) :
kiss_command = bytes ( [ KISS . FEND ] ) + bytes ( [ KISS . CMD_READY ] ) + bytes ( [ 0x01 ] ) + bytes ( [ KISS . FEND ] )
written = self . serial . write ( kiss_command )
if written != len ( kiss_command ) :
if ( flow_control ) :
2024-10-07 08:44:18 +00:00
raise OSError ( " Could not enable AX.25 KISS interface flow control " )
2020-08-13 10:15:56 +00:00
else :
2024-10-07 08:44:18 +00:00
raise OSError ( " Could not enable AX.25 KISS interface flow control " )
2020-08-13 10:15:56 +00:00
def processIncoming ( self , data ) :
if ( len ( data ) > AX25 . HEADER_SIZE ) :
2021-09-24 18:10:04 +00:00
self . rxb + = len ( data )
2020-08-13 10:15:56 +00:00
self . owner . inbound ( data [ AX25 . HEADER_SIZE : ] , self )
def processOutgoing ( self , data ) :
2021-09-24 18:10:04 +00:00
datalen = len ( data )
2020-08-13 10:15:56 +00:00
if self . online :
if self . interface_ready :
if self . flow_control :
self . interface_ready = False
2021-09-03 08:56:49 +00:00
self . flow_control_locked = time . time ( )
2020-08-13 10:15:56 +00:00
encoded_dst_ssid = bytes ( [ 0x60 | ( self . dst_ssid << 1 ) ] )
encoded_src_ssid = bytes ( [ 0x60 | ( self . src_ssid << 1 ) | 0x01 ] )
addr = b " "
for i in range ( 0 , 6 ) :
if ( i < len ( self . dst_call ) ) :
addr + = bytes ( [ self . dst_call [ i ] << 1 ] )
else :
addr + = bytes ( [ 0x20 ] )
addr + = encoded_dst_ssid
for i in range ( 0 , 6 ) :
if ( i < len ( self . src_call ) ) :
addr + = bytes ( [ self . src_call [ i ] << 1 ] )
else :
addr + = bytes ( [ 0x20 ] )
addr + = encoded_src_ssid
data = addr + bytes ( [ AX25 . CTRL_UI ] ) + bytes ( [ AX25 . PID_NOLAYER3 ] ) + data
data = data . replace ( bytes ( [ 0xdb ] ) , bytes ( [ 0xdb ] ) + bytes ( [ 0xdd ] ) )
data = data . replace ( bytes ( [ 0xc0 ] ) , bytes ( [ 0xdb ] ) + bytes ( [ 0xdc ] ) )
kiss_frame = bytes ( [ KISS . FEND ] ) + bytes ( [ 0x00 ] ) + data + bytes ( [ KISS . FEND ] )
written = self . serial . write ( kiss_frame )
2021-09-24 18:10:04 +00:00
self . txb + = datalen
2020-08-13 10:15:56 +00:00
if written != len ( kiss_frame ) :
if self . flow_control :
self . interface_ready = True
2024-10-07 08:44:18 +00:00
raise OSError ( f " AX.25 interface only wrote { written } bytes of { len ( kiss_frame ) } " )
2020-08-13 10:15:56 +00:00
else :
self . queue ( data )
def queue ( self , data ) :
self . packet_queue . append ( data )
def process_queue ( self ) :
if len ( self . packet_queue ) > 0 :
data = self . packet_queue . pop ( 0 )
self . interface_ready = True
self . processOutgoing ( data )
elif len ( self . packet_queue ) == 0 :
self . interface_ready = True
def readLoop ( self ) :
try :
in_frame = False
escape = False
command = KISS . CMD_UNKNOWN
data_buffer = b " "
last_read_ms = int ( time . time ( ) * 1000 )
while self . serial . is_open :
if self . serial . in_waiting :
byte = ord ( self . serial . read ( 1 ) )
last_read_ms = int ( time . time ( ) * 1000 )
if ( in_frame and byte == KISS . FEND and command == KISS . CMD_DATA ) :
in_frame = False
self . processIncoming ( data_buffer )
elif ( byte == KISS . FEND ) :
in_frame = True
command = KISS . CMD_UNKNOWN
data_buffer = b " "
2022-05-29 13:43:50 +00:00
elif ( in_frame and len ( data_buffer ) < self . HW_MTU + AX25 . HEADER_SIZE ) :
2020-08-13 10:15:56 +00:00
if ( len ( data_buffer ) == 0 and command == KISS . CMD_UNKNOWN ) :
# We only support one HDLC port for now, so
# strip off the port nibble
byte = byte & 0x0F
command = byte
elif ( command == KISS . CMD_DATA ) :
if ( byte == KISS . FESC ) :
escape = True
else :
if ( escape ) :
if ( byte == KISS . TFEND ) :
byte = KISS . FEND
if ( byte == KISS . TFESC ) :
byte = KISS . FESC
escape = False
data_buffer = data_buffer + bytes ( [ byte ] )
elif ( command == KISS . CMD_READY ) :
self . process_queue ( )
else :
time_since_last = int ( time . time ( ) * 1000 ) - last_read_ms
if len ( data_buffer ) > 0 and time_since_last > self . timeout :
data_buffer = b " "
in_frame = False
command = KISS . CMD_UNKNOWN
escape = False
2021-09-03 08:56:49 +00:00
sleep ( 0.05 )
if self . flow_control :
if not self . interface_ready :
if time . time ( ) > self . flow_control_locked + self . flow_control_timeout :
2024-10-07 08:44:18 +00:00
RNS . log ( f " Interface { self } is unlocking flow control due to time-out. This should not happen. Your hardware might have missed a flow-control READY command, or maybe it does not support flow-control. " , RNS . LOG_WARNING )
2021-09-03 08:56:49 +00:00
self . process_queue ( )
2020-08-13 10:15:56 +00:00
except Exception as e :
self . online = False
2024-10-07 08:44:18 +00:00
RNS . log ( f " A serial port error occurred, the contained exception was: { e } " , RNS . LOG_ERROR )
RNS . log ( f " The interface { self } experienced an unrecoverable error and is now offline. " , RNS . LOG_ERROR )
2024-10-07 08:52:43 +00:00
2021-09-18 20:49:04 +00:00
if RNS . Reticulum . panic_on_interface_error :
RNS . panic ( )
2020-08-13 10:15:56 +00:00
2021-12-05 13:35:25 +00:00
RNS . log ( " Reticulum will attempt to reconnect the interface periodically. " , RNS . LOG_ERROR )
self . online = False
self . serial . close ( )
self . reconnect_port ( )
def reconnect_port ( self ) :
while not self . online :
try :
time . sleep ( 5 )
2024-10-07 08:44:18 +00:00
RNS . log ( f " Attempting to reconnect serial port { self . port } for { self } ... " , RNS . LOG_VERBOSE )
2021-12-05 13:35:25 +00:00
self . open_port ( )
if self . serial . is_open :
self . configure_device ( )
except Exception as e :
2024-10-07 08:44:18 +00:00
RNS . log ( f " Error while reconnecting port, the contained exception was: { e } " , RNS . LOG_ERROR )
2021-12-05 13:35:25 +00:00
2024-10-07 08:44:18 +00:00
RNS . log ( f " Reconnected serial port for { self } " )
2021-12-05 13:35:25 +00:00
2023-10-01 09:39:24 +00:00
def should_ingress_limit ( self ) :
return False
2020-08-13 10:15:56 +00:00
def __str__ ( self ) :
2024-10-07 08:44:18 +00:00
return f " AX25KISSInterface[ { self . name } ] "