Implemented AX.25 KISS interface

This commit is contained in:
Mark Qvist 2018-04-08 12:26:57 +02:00
parent 9bfb7ae2d7
commit 07814919f2
3 changed files with 111 additions and 17 deletions

View File

@ -22,8 +22,14 @@ class KISS():
CMD_SETHARDWARE = chr(0x06)
CMD_RETURN = chr(0xFF)
class AX25():
PID_NOLAYER3 = chr(0xF0)
CTRL_UI = chr(0x03)
CRC_CORRECT = chr(0xF0)+chr(0xB8)
# TODO: THIS CLASS IS NOT YET IMPLEMENTED --- PLACEHOLDER ONLY ---
class AX25Interface(Interface):
class AX25KISSInterface(Interface):
MAX_CHUNK = 32768
owner = None
@ -34,10 +40,14 @@ class AX25Interface(Interface):
stopbits = None
serial = None
def __init__(self, owner, name, port, speed, databits, parity, stopbits, preamble, txtail, persistence, slottime):
def __init__(self, owner, name, callsign, ssid, port, speed, databits, parity, stopbits, preamble, txtail, persistence, slottime):
self.serial = None
self.owner = owner
self.name = name
self.src_call = callsign.upper()
self.src_ssid = ssid
self.dst_call = "APZRNS"
self.dst_ssid = 0
self.port = port
self.speed = speed
self.databits = databits
@ -46,6 +56,12 @@ class AX25Interface(Interface):
self.timeout = 100
self.online = False
if (len(self.src_call) < 3 or len(self.src_call) > 6):
raise ValueError("Invalid callsign for "+str(self))
if (self.src_ssid < 0 or self.src_ssid > 15):
raise ValueError("Invalid SSID for "+str(self))
self.preamble = preamble if preamble != None else 350;
self.txtail = txtail if txtail != None else 20;
self.persistence = persistence if persistence != None else 64;
@ -84,12 +100,12 @@ class AX25Interface(Interface):
thread.start()
self.online = True
RNS.log("Serial port "+self.port+" is now open")
RNS.log("Configuring KISS interface parameters...")
RNS.log("Configuring AX.25 KISS interface parameters...")
self.setPreamble(self.preamble)
self.setTxTail(self.txtail)
self.setPersistence(self.persistence)
self.setSlotTime(self.slottime)
RNS.log("KISS interface configured")
RNS.log("AX.25 KISS interface configured")
sleep(2)
else:
raise IOError("Could not open serial port")
@ -107,7 +123,7 @@ class AX25Interface(Interface):
kiss_command = KISS.FEND+KISS.CMD_TXDELAY+chr(preamble)+KISS.FEND
written = self.serial.write(kiss_command)
if written != len(kiss_command):
raise IOError("Could not configure KISS interface preamble to "+str(preamble_ms)+" (command value "+str(preamble)+")")
raise IOError("Could not configure AX.25 KISS interface preamble to "+str(preamble_ms)+" (command value "+str(preamble)+")")
def setTxTail(self, txtail):
txtail_ms = txtail
@ -120,7 +136,7 @@ class AX25Interface(Interface):
kiss_command = KISS.FEND+KISS.CMD_TXTAIL+chr(txtail)+KISS.FEND
written = self.serial.write(kiss_command)
if written != len(kiss_command):
raise IOError("Could not configure KISS interface TX tail to "+str(txtail_ms)+" (command value "+str(txtail)+")")
raise IOError("Could not configure AX.25 KISS interface TX tail to "+str(txtail_ms)+" (command value "+str(txtail)+")")
def setPersistence(self, persistence):
if persistence < 0:
@ -131,7 +147,7 @@ class AX25Interface(Interface):
kiss_command = KISS.FEND+KISS.CMD_P+chr(persistence)+KISS.FEND
written = self.serial.write(kiss_command)
if written != len(kiss_command):
raise IOError("Could not configure KISS interface persistence to "+str(persistence))
raise IOError("Could not configure AX.25 KISS interface persistence to "+str(persistence))
def setSlotTime(self, slottime):
slottime_ms = slottime
@ -144,7 +160,7 @@ class AX25Interface(Interface):
kiss_command = KISS.FEND+KISS.CMD_SLOTTIME+chr(slottime)+KISS.FEND
written = self.serial.write(kiss_command)
if written != len(kiss_command):
raise IOError("Could not configure KISS interface slot time to "+str(slottime_ms)+" (command value "+str(slottime)+")")
raise IOError("Could not configure AX.25 KISS interface slot time to "+str(slottime_ms)+" (command value "+str(slottime)+")")
def processIncoming(self, data):
@ -153,12 +169,36 @@ class AX25Interface(Interface):
def processOutgoing(self,data):
if self.online:
# gen src/dst
encoded_dst_ssid = 0x60 | (self.dst_ssid << 1)
encoded_src_ssid = 0x60 | (self.src_ssid << 1) | 0x01
addr = ""
for i in range(0,6):
if (i < len(self.dst_call)):
addr += chr(ord(self.dst_call[i])<<1)
else:
addr += chr(0x20)
addr += chr(encoded_dst_ssid)
for i in range(0,6):
if (i < len(self.src_call)):
addr += chr(ord(self.src_call[i])<<1)
else:
addr += chr(0x20)
addr += chr(encoded_src_ssid)
data = addr+AX25.CTRL_UI+AX25.PID_NOLAYER3+data
data = data.replace(chr(0xdb), chr(0xdb)+chr(0xdd))
data = data.replace(chr(0xc0), chr(0xdb)+chr(0xdc))
frame = chr(0xc0)+chr(0x00)+data+chr(0xc0)
written = self.serial.write(frame)
if written != len(frame):
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
kiss_frame = chr(0xc0)+chr(0x00)+data+chr(0xc0)
written = self.serial.write(kiss_frame)
if written != len(kiss_frame):
raise IOError("AX.25 interface only wrote "+str(written)+" bytes of "+str(len(kiss_frame)))
def readLoop(self):
@ -213,5 +253,5 @@ class AX25Interface(Interface):
RNS.log("The interface "+str(self.name)+" is now offline. Restart Reticulum to attempt reconnection.", RNS.LOG_ERROR)
def __str__(self):
return "KISSInterface["+self.name+"]"
return "AX25KISSInterface["+self.name+"]"

View File

@ -5,6 +5,7 @@ import sys
import serial
import threading
import time
import math
import RNS
class KISS():
@ -72,7 +73,9 @@ class RNodeInterface(Interface):
self.bandwidth = bandwidth
self.txpower = txpower
self.sf = sf
self.cr = 5 # Coding rate is hard-coded in firmware for now
self.state = KISS.RADIO_STATE_OFF
self.bitrate = 0
self.r_frequency = None
self.r_bandwidth = None
@ -218,6 +221,13 @@ class RNodeInterface(Interface):
return False
def updateBitrate(self):
try:
self.bitrate = self.r_sf * ( (4.0/self.cr) / (math.pow(2,self.r_sf)/(self.r_bandwidth/1000)) ) * 1000
self.bitrate_kbps = round(self.bitrate/1000.0, 2)
RNS.log(str(self)+" On-air bitrate is now "+str(self.bitrate_kbps)+ " kbps", RNS.LOG_DEBUG)
except:
self.bitrate = 0
def processIncoming(self, data):
self.owner.inbound(data, self)
@ -284,7 +294,8 @@ class RNodeInterface(Interface):
command_buffer = command_buffer+byte
if (len(command_buffer) == 4):
self.r_frequency = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3])
RNS.log("Radio reporting frequency is "+str(self.r_frequency), RNS.LOG_DEBUG)
RNS.log(str(self)+" Radio reporting frequency is "+str(self.r_frequency/1000000.0)+" MHz", RNS.LOG_DEBUG)
self.updateBitrate()
elif (command == KISS.CMD_BANDWIDTH):
if (byte == KISS.FESC):
@ -299,14 +310,16 @@ class RNodeInterface(Interface):
command_buffer = command_buffer+byte
if (len(command_buffer) == 4):
self.r_bandwidth = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3])
RNS.log("Radio reporting bandwidth is "+str(self.r_bandwidth), RNS.LOG_DEBUG)
RNS.log(str(self)+" Radio reporting bandwidth is "+str(self.r_bandwidth/1000.0)+" KHz", RNS.LOG_DEBUG)
self.updateBitrate()
elif (command == KISS.CMD_TXPOWER):
self.r_txpower = ord(byte)
RNS.log("Radio reporting TX power is "+str(self.r_txpower), RNS.LOG_DEBUG)
RNS.log(str(self)+" Radio reporting TX power is "+str(self.r_txpower)+" dBm", RNS.LOG_DEBUG)
elif (command == KISS.CMD_SF):
self.r_sf = ord(byte)
RNS.log("Radio reporting spreading factor is "+str(self.r_sf), RNS.LOG_DEBUG)
RNS.log(str(self)+" Radio reporting spreading factor is "+str(self.r_sf), RNS.LOG_DEBUG)
self.updateBitrate()
elif (command == KISS.CMD_RADIO_STATE):
self.r_state = ord(byte)
elif (command == KISS.CMD_RADIO_LOCK):

View File

@ -140,6 +140,47 @@ class Reticulum:
RNS.Transport.interfaces.append(interface)
if c["type"] == "AX25KISSInterface":
preamble = int(c["preamble"]) if "preamble" in c else None
txtail = int(c["txtail"]) if "txtail" in c else None
persistence = int(c["persistence"]) if "persistence" in c else None
slottime = int(c["slottime"]) if "slottime" in c else None
port = c["port"] if "port" in c else None
speed = int(c["speed"]) if "speed" in c else 9600
databits = int(c["databits"]) if "databits" in c else 8
parity = c["parity"] if "parity" in c else "N"
stopbits = int(c["stopbits"]) if "stopbits" in c else 1
callsign = c["callsign"] if "callsign" in c else ""
ssid = int(c["ssid"]) if "ssid" in c else -1
if port == None:
raise ValueError("No port specified for serial interface")
interface = AX25KISSInterface.AX25KISSInterface(
RNS.Transport,
name,
callsign,
ssid,
port,
speed,
databits,
parity,
stopbits,
preamble,
txtail,
persistence,
slottime
)
if "outgoing" in c and c["outgoing"].lower() == "true":
interface.OUT = True
else:
interface.OUT = False
RNS.Transport.interfaces.append(interface)
if c["type"] == "RNodeInterface":
frequency = int(c["frequency"]) if "frequency" in c else None
bandwidth = int(c["bandwidth"]) if "bandwidth" in c else None