Skip to content

Commit

Permalink
Trap for failed parsing of packets on KISS
Browse files Browse the repository at this point in the history
This adds a try block around the aprslib.parse() for packets incoming
on the KISS interface.  Often times we'll get invalid packets and
this prevents stack dumps to the log.
  • Loading branch information
hemna committed Jan 24, 2025
1 parent edeba7f commit 9501a63
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions aprsd/client/kiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from aprsd.packets import core

CONF = cfg.CONF
LOG = logging.getLogger("APRSD")
LOG = logging.getLogger('APRSD')
LOGU = logger


Expand All @@ -27,15 +27,15 @@ def stats(self, serializable=False) -> dict:
if serializable:
keepalive = keepalive.isoformat()
stats = {
"connected": self.is_connected,
"connection_keepalive": keepalive,
"transport": self.transport(),
'connected': self.is_connected,
'connection_keepalive': keepalive,
'transport': self.transport(),
}
if self.transport() == client.TRANSPORT_TCPKISS:
stats["host"] = CONF.kiss_tcp.host
stats["port"] = CONF.kiss_tcp.port
stats['host'] = CONF.kiss_tcp.host
stats['port'] = CONF.kiss_tcp.port
elif self.transport() == client.TRANSPORT_SERIALKISS:
stats["device"] = CONF.kiss_serial.device
stats['device'] = CONF.kiss_serial.device
return stats

@staticmethod
Expand All @@ -56,15 +56,15 @@ def is_configured():
transport = KISSClient.transport()
if transport == client.TRANSPORT_SERIALKISS:
if not CONF.kiss_serial.device:
LOG.error("KISS serial enabled, but no device is set.")
LOG.error('KISS serial enabled, but no device is set.')
raise exception.MissingConfigOptionException(
"kiss_serial.device is not set.",
'kiss_serial.device is not set.',
)
elif transport == client.TRANSPORT_TCPKISS:
if not CONF.kiss_tcp.host:
LOG.error("KISS TCP enabled, but no host is set.")
LOG.error('KISS TCP enabled, but no host is set.')
raise exception.MissingConfigOptionException(
"kiss_tcp.host is not set.",
'kiss_tcp.host is not set.',
)

return True
Expand All @@ -91,8 +91,8 @@ def keepalive_log(self):
if ka := self._client.aprsd_keepalive:
keepalive = timeago.format(ka)
else:
keepalive = "N/A"
LOGU.opt(colors=True).info(f"<green>Client keepalive {keepalive}</green>")
keepalive = 'N/A'
LOGU.opt(colors=True).info(f'<green>Client keepalive {keepalive}</green>')

@staticmethod
def transport():
Expand All @@ -104,8 +104,8 @@ def transport():

def decode_packet(self, *args, **kwargs):
"""We get a frame, which has to be decoded."""
LOG.debug(f"kwargs {kwargs}")
frame = kwargs["frame"]
LOG.debug(f'kwargs {kwargs}')
frame = kwargs['frame']
LOG.debug(f"Got an APRS Frame '{frame}'")
# try and nuke the * from the fromcall sign.
# frame.header._source._ch = False
Expand All @@ -114,26 +114,29 @@ def decode_packet(self, *args, **kwargs):
# msg = frame.tnc2
# LOG.debug(f"Decoding {msg}")

raw = aprslib.parse(str(frame))
packet = core.factory(raw)
if isinstance(packet, core.ThirdPartyPacket):
return packet.subpacket
else:
return packet
try:
raw = aprslib.parse(str(frame))
packet = core.factory(raw)
if isinstance(packet, core.ThirdPartyPacket):
return packet.subpacket
else:
return packet
except Exception as ex:
LOG.error(f'Error decoding packet: {ex}')

def setup_connection(self):
try:
self._client = kiss.KISS3Client()
self.connected = self.login_status["success"] = True
self.connected = self.login_status['success'] = True
except Exception as ex:
self.connected = self.login_status["success"] = False
self.login_status["message"] = str(ex)
self.connected = self.login_status['success'] = False
self.login_status['message'] = str(ex)
return self._client

def consumer(self, callback, blocking=False, immortal=False, raw=False):
try:
self._client.consumer(callback)
self.keepalive = datetime.datetime.now()
except Exception as ex:
LOG.error(f"Consumer failed {ex}")
LOG.error(f'Consumer failed {ex}')
LOG.error(ex)

0 comments on commit 9501a63

Please sign in to comment.