Cleaned up whitespace

This commit is contained in:
Mark Qvist 2020-05-27 16:17:40 +02:00
parent a683e2df4d
commit 02525cee55
2 changed files with 449 additions and 449 deletions

View File

@ -7,13 +7,15 @@ from RNode import RNodeInterface
# We'll also define which serial port the # We'll also define which serial port the
# RNode is attached to. # RNode is attached to.
serialPort = "/dev/ttyUSB0" serialPort = "/dev/ttyUSB0"
# TODO: Remove
serialPort = "/dev/tty.usbserial-DN03E0FQ"
# This function gets called every time a # This function gets called every time a
# packet is received # packet is received
def gotPacket(data, rnode): def gotPacket(data, rnode):
print "Received a packet: "+data print("Received a packet: "+data)
print "RSSI: "+str(rnode.r_stat_rssi)+" dBm" print("RSSI: "+str(rnode.r_stat_rssi)+" dBm")
print "SNR: "+str(rnode.r_stat_snr)+" dB" print("SNR: "+str(rnode.r_stat_snr)+" dB")
# Create an RNode instance. This configures # Create an RNode instance. This configures
# and powers up the radio. # and powers up the radio.
@ -30,10 +32,10 @@ rnode = RNodeInterface(
# Enter a loop waiting for user input. # Enter a loop waiting for user input.
try: try:
print "Waiting for packets, hit enter to send a packet, Ctrl-C to exit" print("Waiting for packets, hit enter to send a packet, Ctrl-C to exit")
while True: while True:
raw_input() input()
rnode.send("Hello World!") rnode.send("Hello World!")
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
print "" print("")
exit() exit()

View File

@ -1,4 +1,3 @@
from __future__ import print_function
from time import sleep from time import sleep
import sys import sys
import serial import serial
@ -7,499 +6,498 @@ import time
import math import math
class KISS(): class KISS():
FEND = chr(0xC0) FEND = 0xC0
FESC = chr(0xDB) FESC = 0xDB
TFEND = chr(0xDC) TFEND = 0xDC
TFESC = chr(0xDD) TFESC = 0xDD
CMD_UNKNOWN = 0xFE
CMD_UNKNOWN = chr(0xFE) CMD_DATA = 0x00
CMD_DATA = chr(0x00) CMD_FREQUENCY = 0x01
CMD_FREQUENCY = chr(0x01) CMD_BANDWIDTH = 0x02
CMD_BANDWIDTH = chr(0x02) CMD_TXPOWER = 0x03
CMD_TXPOWER = chr(0x03) CMD_SF = 0x04
CMD_SF = chr(0x04) CMD_CR = 0x05
CMD_CR = chr(0x05) CMD_RADIO_STATE = 0x06
CMD_RADIO_STATE = chr(0x06) CMD_RADIO_LOCK = 0x07
CMD_RADIO_LOCK = chr(0x07) CMD_DETECT = 0x08
CMD_DETECT = chr(0x08) CMD_PROMISC = 0x0E
CMD_PROMISC = chr(0x0E) CMD_READY = 0x0F
CMD_READY = chr(0x0F) CMD_STAT_RX = 0x21
CMD_STAT_RX = chr(0x21) CMD_STAT_TX = 0x22
CMD_STAT_TX = chr(0x22) CMD_STAT_RSSI = 0x23
CMD_STAT_RSSI = chr(0x23) CMD_STAT_SNR = 0x24
CMD_STAT_SNR = chr(0x24) CMD_BLINK = 0x30
CMD_BLINK = chr(0x30) CMD_RANDOM = 0x40
CMD_RANDOM = chr(0x40) CMD_FW_VERSION = 0x50
CMD_FW_VERSION = chr(0x50) CMD_ROM_READ = 0x51
CMD_ROM_READ = chr(0x51)
DETECT_REQ = chr(0x73) DETECT_REQ = 0x73
DETECT_RESP = chr(0x46) DETECT_RESP = 0x46
RADIO_STATE_OFF = chr(0x00)
RADIO_STATE_ON = chr(0x01)
RADIO_STATE_ASK = chr(0xFF)
CMD_ERROR = chr(0x90)
ERROR_INITRADIO = chr(0x01)
ERROR_TXFAILED = chr(0x02)
ERROR_EEPROM_LOCKED = chr(0x03)
@staticmethod RADIO_STATE_OFF = 0x00
def escape(data): RADIO_STATE_ON = 0x01
data = data.replace(chr(0xdb), chr(0xdb)+chr(0xdd)) RADIO_STATE_ASK = 0xFF
data = data.replace(chr(0xc0), chr(0xdb)+chr(0xdc))
return data CMD_ERROR = chr0x90
ERROR_INITRADIO = chr0x01
ERROR_TXFAILED = chr0x02
ERROR_EEPROM_LOCKED = chr0x03
@staticmethod
def escape(data):
data = data.replace(bytes([0xdb]), bytes([0xdb, 0xdd]))
data = data.replace(bytes([0xc0]), bytes([0xdb, 0xdc]))
return data
class RNodeInterface(): class RNodeInterface():
MTU = 500 MTU = 500
MAX_CHUNK = 32768 MAX_CHUNK = 32768
FREQ_MIN = 137000000 FREQ_MIN = 137000000
FREQ_MAX = 1020000000 FREQ_MAX = 1020000000
LOG_CRITICAL = 0 LOG_CRITICAL = 0
LOG_ERROR = 1 LOG_ERROR = 1
LOG_WARNING = 2 LOG_WARNING = 2
LOG_NOTICE = 3 LOG_NOTICE = 3
LOG_INFO = 4 LOG_INFO = 4
LOG_VERBOSE = 5 LOG_VERBOSE = 5
LOG_DEBUG = 6 LOG_DEBUG = 6
LOG_EXTREME = 7 LOG_EXTREME = 7
RSSI_OFFSET = 157 RSSI_OFFSET = 157
SNR_OFFSET = 128 SNR_OFFSET = 128
def __init__(self, callback, name, port, frequency = None, bandwidth = None, txpower = None, sf = None, cr = None, loglevel = -1, flow_control = True): def __init__(self, callback, name, port, frequency = None, bandwidth = None, txpower = None, sf = None, cr = None, loglevel = -1, flow_control = True):
self.serial = None self.serial = None
self.loglevel = loglevel self.loglevel = loglevel
self.callback = callback self.callback = callback
self.name = name self.name = name
self.port = port self.port = port
self.speed = 115200 self.speed = 115200
self.databits = 8 self.databits = 8
self.parity = serial.PARITY_NONE self.parity = serial.PARITY_NONE
self.stopbits = 1 self.stopbits = 1
self.timeout = 100 self.timeout = 100
self.online = False self.online = False
self.frequency = frequency self.frequency = frequency
self.bandwidth = bandwidth self.bandwidth = bandwidth
self.txpower = txpower self.txpower = txpower
self.sf = sf self.sf = sf
self.cr = cr self.cr = cr
self.state = KISS.RADIO_STATE_OFF self.state = KISS.RADIO_STATE_OFF
self.bitrate = 0 self.bitrate = 0
self.r_frequency = None self.r_frequency = None
self.r_bandwidth = None self.r_bandwidth = None
self.r_txpower = None self.r_txpower = None
self.r_sf = None self.r_sf = None
self.r_cr = None self.r_cr = None
self.r_state = None self.r_state = None
self.r_lock = None self.r_lock = None
self.r_stat_rx = None self.r_stat_rx = None
self.r_stat_tx = None self.r_stat_tx = None
self.r_stat_rssi = None self.r_stat_rssi = None
self.r_stat_snr = None self.r_stat_snr = None
self.r_random = None self.r_random = None
self.packet_queue = [] self.packet_queue = []
self.flow_control = flow_control self.flow_control = flow_control
self.interface_ready = False self.interface_ready = False
self.validcfg = True self.validcfg = True
if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX): if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX):
self.log("Invalid frequency configured for "+str(self), RNodeInterface.LOG_ERROR) self.log("Invalid frequency configured for "+str(self), RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.txpower < 0 or self.txpower > 17): if (self.txpower < 0 or self.txpower > 17):
self.log("Invalid TX power configured for "+str(self), RNodeInterface.LOG_ERROR) self.log("Invalid TX power configured for "+str(self), RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.bandwidth < 7800 or self.bandwidth > 500000): if (self.bandwidth < 7800 or self.bandwidth > 500000):
self.log("Invalid bandwidth configured for "+str(self), RNodeInterface.LOG_ERROR) self.log("Invalid bandwidth configured for "+str(self), RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.sf < 7 or self.sf > 12): if (self.sf < 7 or self.sf > 12):
self.log("Invalid spreading factor configured for "+str(self), RNodeInterface.LOG_ERROR) self.log("Invalid spreading factor configured for "+str(self), RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (not self.validcfg): if (not self.validcfg):
raise ValueError("The configuration for "+str(self)+" contains errors, interface is offline") raise ValueError("The configuration for "+str(self)+" contains errors, interface is offline")
try: try:
self.log("Opening serial port "+self.port+"...") self.log("Opening serial port "+self.port+"...")
self.serial = serial.Serial( self.serial = serial.Serial(
port = self.port, port = self.port,
baudrate = self.speed, baudrate = self.speed,
bytesize = self.databits, bytesize = self.databits,
parity = self.parity, parity = self.parity,
stopbits = self.stopbits, stopbits = self.stopbits,
xonxoff = False, xonxoff = False,
rtscts = False, rtscts = False,
timeout = 0, timeout = 0,
inter_byte_timeout = None, inter_byte_timeout = None,
write_timeout = None, write_timeout = None,
dsrdtr = False, dsrdtr = False,
) )
except Exception as e: except Exception as e:
self.log("Could not open serial port for interface "+str(self), RNodeInterface.LOG_ERROR) self.log("Could not open serial port for interface "+str(self), RNodeInterface.LOG_ERROR)
raise e raise e
if self.serial.is_open: if self.serial.is_open:
sleep(2.0) sleep(2.0)
thread = threading.Thread(target=self.readLoop) thread = threading.Thread(target=self.readLoop)
thread.setDaemon(True) thread.setDaemon(True)
thread.start() thread.start()
self.online = True self.online = True
self.log("Serial port "+self.port+" is now open") self.log("Serial port "+self.port+" is now open")
self.log("Configuring RNode interface...", RNodeInterface.LOG_VERBOSE) self.log("Configuring RNode interface...", RNodeInterface.LOG_VERBOSE)
self.initRadio() self.initRadio()
if (self.validateRadioState()): if (self.validateRadioState()):
self.interface_ready = True self.interface_ready = True
self.log(str(self)+" is configured and powered up") self.log(str(self)+" is configured and powered up")
sleep(1.0) sleep(1.0)
else: else:
self.log("After configuring "+str(self)+", the actual radio parameters did not match your configuration.", RNodeInterface.LOG_ERROR) self.log("After configuring "+str(self)+", the actual radio parameters did not match your configuration.", RNodeInterface.LOG_ERROR)
self.log("Make sure that your hardware actually supports the parameters specified in the configuration", RNodeInterface.LOG_ERROR) self.log("Make sure that your hardware actually supports the parameters specified in the configuration", RNodeInterface.LOG_ERROR)
self.log("Aborting RNode startup", RNodeInterface.LOG_ERROR) self.log("Aborting RNode startup", RNodeInterface.LOG_ERROR)
self.serial.close() self.serial.close()
raise IOError("RNode interface did not pass validation") raise IOError("RNode interface did not pass validation")
else: else:
raise IOError("Could not open serial port") raise IOError("Could not open serial port")
def log(self, message, level): def log(self, message, level):
pass pass
def initRadio(self): def initRadio(self):
self.setFrequency() self.setFrequency()
self.setBandwidth() self.setBandwidth()
self.setTXPower() self.setTXPower()
self.setSpreadingFactor() self.setSpreadingFactor()
self.setRadioState(KISS.RADIO_STATE_ON) self.setRadioState(KISS.RADIO_STATE_ON)
def setFrequency(self): def setFrequency(self):
c1 = self.frequency >> 24 c1 = self.frequency >> 24
c2 = self.frequency >> 16 & 0xFF c2 = self.frequency >> 16 & 0xFF
c3 = self.frequency >> 8 & 0xFF c3 = self.frequency >> 8 & 0xFF
c4 = self.frequency & 0xFF c4 = self.frequency & 0xFF
data = KISS.escape(chr(c1)+chr(c2)+chr(c3)+chr(c4)) data = KISS.escape(chr(c1)+chr(c2)+chr(c3)+chr(c4))
kiss_command = KISS.FEND+KISS.CMD_FREQUENCY+data+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_FREQUENCY+data+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring frequency for "+self(str)) raise IOError("An IO error occurred while configuring frequency for "+self(str))
def setBandwidth(self): def setBandwidth(self):
c1 = self.bandwidth >> 24 c1 = self.bandwidth >> 24
c2 = self.bandwidth >> 16 & 0xFF c2 = self.bandwidth >> 16 & 0xFF
c3 = self.bandwidth >> 8 & 0xFF c3 = self.bandwidth >> 8 & 0xFF
c4 = self.bandwidth & 0xFF c4 = self.bandwidth & 0xFF
data = KISS.escape(chr(c1)+chr(c2)+chr(c3)+chr(c4)) data = KISS.escape(chr(c1)+chr(c2)+chr(c3)+chr(c4))
kiss_command = KISS.FEND+KISS.CMD_BANDWIDTH+data+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_BANDWIDTH+data+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring bandwidth for "+self(str)) raise IOError("An IO error occurred while configuring bandwidth for "+self(str))
def setTXPower(self): def setTXPower(self):
txp = chr(self.txpower) txp = chr(self.txpower)
kiss_command = KISS.FEND+KISS.CMD_TXPOWER+txp+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_TXPOWER+txp+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring TX power for "+self(str)) raise IOError("An IO error occurred while configuring TX power for "+self(str))
def setSpreadingFactor(self): def setSpreadingFactor(self):
sf = chr(self.sf) sf = chr(self.sf)
kiss_command = KISS.FEND+KISS.CMD_SF+sf+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_SF+sf+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring spreading factor for "+self(str)) raise IOError("An IO error occurred while configuring spreading factor for "+self(str))
def setCodingRate(self): def setCodingRate(self):
cr = chr(self.cr) cr = chr(self.cr)
kiss_command = KISS.FEND+KISS.CMD_CR+cr+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_CR+cr+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring coding rate for "+self(str)) raise IOError("An IO error occurred while configuring coding rate for "+self(str))
def setRadioState(self, state): def setRadioState(self, state):
kiss_command = KISS.FEND+KISS.CMD_RADIO_STATE+state+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_RADIO_STATE+state+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring radio state for "+self(str)) raise IOError("An IO error occurred while configuring radio state for "+self(str))
def validateRadioState(self): def validateRadioState(self):
self.log("Validating radio configuration for "+str(self)+"...", RNodeInterface.LOG_VERBOSE) self.log("Validating radio configuration for "+str(self)+"...", RNodeInterface.LOG_VERBOSE)
sleep(0.25); sleep(0.25);
if (self.frequency != self.r_frequency): if (self.frequency != self.r_frequency):
self.log("Frequency mismatch", RNodeInterface.LOG_ERROR) self.log("Frequency mismatch", RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.bandwidth != self.r_bandwidth): if (self.bandwidth != self.r_bandwidth):
self.log("Bandwidth mismatch", RNodeInterface.LOG_ERROR) self.log("Bandwidth mismatch", RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.txpower != self.r_txpower): if (self.txpower != self.r_txpower):
self.log("TX power mismatch", RNodeInterface.LOG_ERROR) self.log("TX power mismatch", RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.sf != self.r_sf): if (self.sf != self.r_sf):
self.log("Spreading factor mismatch", RNodeInterface.LOG_ERROR) self.log("Spreading factor mismatch", RNodeInterface.LOG_ERROR)
self.validcfg = False self.validcfg = False
if (self.validcfg): if (self.validcfg):
return True return True
else: else:
return False return False
def setPromiscuousMode(self, state): def setPromiscuousMode(self, state):
if state == True: if state == True:
kiss_command = KISS.FEND+KISS.CMD_PROMISC+chr(0x01)+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_PROMISC+chr(0x01)+KISS.FEND
else: else:
kiss_command = KISS.FEND+KISS.CMD_PROMISC+chr(0x00)+KISS.FEND kiss_command = KISS.FEND+KISS.CMD_PROMISC+chr(0x00)+KISS.FEND
written = self.serial.write(kiss_command) written = self.serial.write(kiss_command)
if written != len(kiss_command): if written != len(kiss_command):
raise IOError("An IO error occurred while configuring promiscuous mode for "+self(str)) raise IOError("An IO error occurred while configuring promiscuous mode for "+self(str))
def updateBitrate(self): def updateBitrate(self):
try: try:
self.bitrate = self.r_sf * ( (4.0/self.cr) / (math.pow(2,self.r_sf)/(self.r_bandwidth/1000)) ) * 1000 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) self.bitrate_kbps = round(self.bitrate/1000.0, 2)
self.log(str(self)+" On-air bitrate is now "+str(self.bitrate_kbps)+ " kbps", RNodeInterface.LOG_DEBUG) self.log(str(self)+" On-air bitrate is now "+str(self.bitrate_kbps)+ " kbps", RNodeInterface.LOG_DEBUG)
except: except:
self.bitrate = 0 self.bitrate = 0
def processIncoming(self, data): def processIncoming(self, data):
self.callback(data, self) self.callback(data, self)
def send(self, data): def send(self, data):
self.processOutgoing(data) self.processOutgoing(data)
def processOutgoing(self,data): def processOutgoing(self,data):
if self.online: if self.online:
if self.interface_ready: if self.interface_ready:
if self.flow_control: if self.flow_control:
self.interface_ready = False self.interface_ready = False
data = KISS.escape(data) data = KISS.escape(data)
frame = chr(0xc0)+chr(0x00)+data+chr(0xc0) frame = chr(0xc0)+chr(0x00)+data+chr(0xc0)
written = self.serial.write(frame) written = self.serial.write(frame)
if written != len(frame): if written != len(frame):
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data))) raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
else: else:
self.queue(data) self.queue(data)
def queue(self, data): def queue(self, data):
self.packet_queue.append(data) self.packet_queue.append(data)
def process_queue(self): def process_queue(self):
if len(self.packet_queue) > 0: if len(self.packet_queue) > 0:
data = self.packet_queue.pop(0) data = self.packet_queue.pop(0)
self.interface_ready = True self.interface_ready = True
self.processOutgoing(data) self.processOutgoing(data)
elif len(self.packet_queue) == 0: elif len(self.packet_queue) == 0:
self.interface_ready = True self.interface_ready = True
def readLoop(self): def readLoop(self):
try: try:
in_frame = False in_frame = False
escape = False escape = False
command = KISS.CMD_UNKNOWN command = KISS.CMD_UNKNOWN
data_buffer = "" data_buffer = ""
command_buffer = "" command_buffer = ""
last_read_ms = int(time.time()*1000) last_read_ms = int(time.time()*1000)
while self.serial.is_open: while self.serial.is_open:
if self.serial.in_waiting: if self.serial.in_waiting:
byte = self.serial.read(1) byte = self.serial.read(1)
last_read_ms = int(time.time()*1000) last_read_ms = int(time.time()*1000)
if (in_frame and byte == KISS.FEND and command == KISS.CMD_DATA): if (in_frame and byte == KISS.FEND and command == KISS.CMD_DATA):
in_frame = False in_frame = False
self.processIncoming(data_buffer) self.processIncoming(data_buffer)
data_buffer = "" data_buffer = ""
command_buffer = "" command_buffer = ""
elif (byte == KISS.FEND): elif (byte == KISS.FEND):
in_frame = True in_frame = True
command = KISS.CMD_UNKNOWN command = KISS.CMD_UNKNOWN
data_buffer = "" data_buffer = ""
command_buffer = "" command_buffer = ""
elif (in_frame and len(data_buffer) < RNodeInterface.MTU): elif (in_frame and len(data_buffer) < RNodeInterface.MTU):
if (len(data_buffer) == 0 and command == KISS.CMD_UNKNOWN): if (len(data_buffer) == 0 and command == KISS.CMD_UNKNOWN):
command = byte command = byte
elif (command == KISS.CMD_DATA): elif (command == KISS.CMD_DATA):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
data_buffer = data_buffer+byte data_buffer = data_buffer+byte
elif (command == KISS.CMD_FREQUENCY): elif (command == KISS.CMD_FREQUENCY):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
command_buffer = command_buffer+byte command_buffer = command_buffer+byte
if (len(command_buffer) == 4): 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]) self.r_frequency = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3])
self.log(str(self)+" Radio reporting frequency is "+str(self.r_frequency/1000000.0)+" MHz", RNodeInterface.LOG_DEBUG) self.log(str(self)+" Radio reporting frequency is "+str(self.r_frequency/1000000.0)+" MHz", RNodeInterface.LOG_DEBUG)
self.updateBitrate() self.updateBitrate()
elif (command == KISS.CMD_BANDWIDTH): elif (command == KISS.CMD_BANDWIDTH):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
command_buffer = command_buffer+byte command_buffer = command_buffer+byte
if (len(command_buffer) == 4): 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]) self.r_bandwidth = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3])
self.log(str(self)+" Radio reporting bandwidth is "+str(self.r_bandwidth/1000.0)+" KHz", RNodeInterface.LOG_DEBUG) self.log(str(self)+" Radio reporting bandwidth is "+str(self.r_bandwidth/1000.0)+" KHz", RNodeInterface.LOG_DEBUG)
self.updateBitrate() self.updateBitrate()
elif (command == KISS.CMD_TXPOWER): elif (command == KISS.CMD_TXPOWER):
self.r_txpower = ord(byte) self.r_txpower = ord(byte)
self.log(str(self)+" Radio reporting TX power is "+str(self.r_txpower)+" dBm", RNodeInterface.LOG_DEBUG) self.log(str(self)+" Radio reporting TX power is "+str(self.r_txpower)+" dBm", RNodeInterface.LOG_DEBUG)
elif (command == KISS.CMD_SF): elif (command == KISS.CMD_SF):
self.r_sf = ord(byte) self.r_sf = ord(byte)
self.log(str(self)+" Radio reporting spreading factor is "+str(self.r_sf), RNodeInterface.LOG_DEBUG) self.log(str(self)+" Radio reporting spreading factor is "+str(self.r_sf), RNodeInterface.LOG_DEBUG)
self.updateBitrate() self.updateBitrate()
elif (command == KISS.CMD_CR): elif (command == KISS.CMD_CR):
self.r_cr = ord(byte) self.r_cr = ord(byte)
self.log(str(self)+" Radio reporting coding rate is "+str(self.r_cr), RNodeInterface.LOG_DEBUG) self.log(str(self)+" Radio reporting coding rate is "+str(self.r_cr), RNodeInterface.LOG_DEBUG)
self.updateBitrate() self.updateBitrate()
elif (command == KISS.CMD_RADIO_STATE): elif (command == KISS.CMD_RADIO_STATE):
self.r_state = ord(byte) self.r_state = ord(byte)
elif (command == KISS.CMD_RADIO_LOCK): elif (command == KISS.CMD_RADIO_LOCK):
self.r_lock = ord(byte) self.r_lock = ord(byte)
elif (command == KISS.CMD_STAT_RX): elif (command == KISS.CMD_STAT_RX):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
command_buffer = command_buffer+byte command_buffer = command_buffer+byte
if (len(command_buffer) == 4): if (len(command_buffer) == 4):
self.r_stat_rx = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3]) self.r_stat_rx = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3])
elif (command == KISS.CMD_STAT_TX): elif (command == KISS.CMD_STAT_TX):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
command_buffer = command_buffer+byte command_buffer = command_buffer+byte
if (len(command_buffer) == 4): if (len(command_buffer) == 4):
self.r_stat_tx = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3]) self.r_stat_tx = ord(command_buffer[0]) << 24 | ord(command_buffer[1]) << 16 | ord(command_buffer[2]) << 8 | ord(command_buffer[3])
elif (command == KISS.CMD_STAT_RSSI): elif (command == KISS.CMD_STAT_RSSI):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
self.r_stat_rssi = ord(byte)-self.RSSI_OFFSET self.r_stat_rssi = ord(byte)-self.RSSI_OFFSET
elif (command == KISS.CMD_STAT_SNR): elif (command == KISS.CMD_STAT_SNR):
if (byte == KISS.FESC): if (byte == KISS.FESC):
escape = True escape = True
else: else:
if (escape): if (escape):
if (byte == KISS.TFEND): if (byte == KISS.TFEND):
byte = KISS.FEND byte = KISS.FEND
if (byte == KISS.TFESC): if (byte == KISS.TFESC):
byte = KISS.FESC byte = KISS.FESC
escape = False escape = False
self.r_stat_snr = ord(byte)-self.SNR_OFFSET self.r_stat_snr = ord(byte)-self.SNR_OFFSET
elif (command == KISS.CMD_RANDOM): elif (command == KISS.CMD_RANDOM):
self.r_random = ord(byte) self.r_random = ord(byte)
elif (command == KISS.CMD_ERROR): elif (command == KISS.CMD_ERROR):
if (byte == KISS.ERROR_INITRADIO): if (byte == KISS.ERROR_INITRADIO):
self.log(str(self)+" hardware initialisation error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR) self.log(str(self)+" hardware initialisation error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR)
elif (byte == KISS.ERROR_INITRADIO): elif (byte == KISS.ERROR_INITRADIO):
self.log(str(self)+" hardware TX error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR) self.log(str(self)+" hardware TX error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR)
else: else:
self.log(str(self)+" hardware error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR) self.log(str(self)+" hardware error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR)
elif (command == KISS.CMD_READY): elif (command == KISS.CMD_READY):
# TODO: add timeout and reset if ready # TODO: add timeout and reset if ready
# command never arrives # command never arrives
self.process_queue() self.process_queue()
else: else:
time_since_last = int(time.time()*1000) - last_read_ms time_since_last = int(time.time()*1000) - last_read_ms
if len(data_buffer) > 0 and time_since_last > self.timeout: if len(data_buffer) > 0 and time_since_last > self.timeout:
self.log(str(self)+" serial read timeout", RNodeInterface.LOG_DEBUG) self.log(str(self)+" serial read timeout", RNodeInterface.LOG_DEBUG)
data_buffer = "" data_buffer = ""
in_frame = False in_frame = False
command = KISS.CMD_UNKNOWN command = KISS.CMD_UNKNOWN
escape = False escape = False
sleep(0.08) sleep(0.08)
except Exception as e: except Exception as e:
self.online = False self.online = False
self.log("A serial port error occurred, the contained exception was: "+str(e), RNodeInterface.LOG_ERROR) self.log("A serial port error occurred, the contained exception was: "+str(e), RNodeInterface.LOG_ERROR)
self.log("The interface "+str(self.name)+" is now offline.", RNodeInterface.LOG_ERROR) self.log("The interface "+str(self.name)+" is now offline.", RNodeInterface.LOG_ERROR)
def log(self, msg, level=3): def log(self, msg, level=3):
logtimefmt = "%Y-%m-%d %H:%M:%S" logtimefmt = "%Y-%m-%d %H:%M:%S"
if self.loglevel >= level: if self.loglevel >= level:
timestamp = time.time() timestamp = time.time()
logstring = "["+time.strftime(logtimefmt)+"] ["+self.loglevelname(level)+"] "+msg logstring = "["+time.strftime(logtimefmt)+"] ["+self.loglevelname(level)+"] "+msg
print(logstring) print(logstring)
def loglevelname(self, level): def loglevelname(self, level):
if (level == RNodeInterface.LOG_CRITICAL): if (level == RNodeInterface.LOG_CRITICAL):
return "Critical" return "Critical"
if (level == RNodeInterface.LOG_ERROR): if (level == RNodeInterface.LOG_ERROR):
return "Error" return "Error"
if (level == RNodeInterface.LOG_WARNING): if (level == RNodeInterface.LOG_WARNING):
return "Warning" return "Warning"
if (level == RNodeInterface.LOG_NOTICE): if (level == RNodeInterface.LOG_NOTICE):
return "Notice" return "Notice"
if (level == RNodeInterface.LOG_INFO): if (level == RNodeInterface.LOG_INFO):
return "Info" return "Info"
if (level == RNodeInterface.LOG_VERBOSE): if (level == RNodeInterface.LOG_VERBOSE):
return "Verbose" return "Verbose"
if (level == RNodeInterface.LOG_DEBUG): if (level == RNodeInterface.LOG_DEBUG):
return "Debug" return "Debug"
if (level == RNodeInterface.LOG_EXTREME): if (level == RNodeInterface.LOG_EXTREME):
return "Extra" return "Extra"
def hexrep(data, delimit=True): def hexrep(data, delimit=True):
delimiter = ":" delimiter = ":"
if not delimit: if not delimit:
delimiter = "" delimiter = ""
hexrep = delimiter.join("{:02x}".format(ord(c)) for c in data) hexrep = delimiter.join("{:02x}".format(ord(c)) for c in data)
return hexrep return hexrep
def __str__(self): def __str__(self):
return "RNodeInterface["+self.name+"]" return "RNodeInterface["+self.name+"]"