2021-02-25 21:01:52 -05:00
|
|
|
import logging
|
|
|
|
|
2022-11-22 13:32:19 -05:00
|
|
|
import aprslib
|
|
|
|
from ax253 import Frame
|
|
|
|
import kiss
|
|
|
|
|
|
|
|
from aprsd import messaging
|
|
|
|
from aprsd.utils import trace
|
2021-08-30 13:34:25 -04:00
|
|
|
|
2021-02-25 21:01:52 -05:00
|
|
|
|
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
|
|
|
|
|
2022-11-22 13:32:19 -05:00
|
|
|
class KISS3Client:
|
2021-02-25 21:01:52 -05:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
self.setup()
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
# we can be TCP kiss or Serial kiss
|
|
|
|
if "serial" in self.config["kiss"] and self.config["kiss"]["serial"].get(
|
|
|
|
"enabled",
|
|
|
|
False,
|
|
|
|
):
|
|
|
|
LOG.debug(
|
2022-11-22 13:32:19 -05:00
|
|
|
"KISS({}) Serial connection to {}".format(
|
|
|
|
kiss.__version__,
|
2021-02-25 21:01:52 -05:00
|
|
|
self.config["kiss"]["serial"]["device"],
|
|
|
|
),
|
|
|
|
)
|
2022-11-22 13:32:19 -05:00
|
|
|
self.kiss = kiss.SerialKISS(
|
|
|
|
port=self.config["kiss"]["serial"]["device"],
|
|
|
|
speed=self.config["kiss"]["serial"].get("baudrate", 9600),
|
|
|
|
strip_df_start=True,
|
2021-02-25 21:01:52 -05:00
|
|
|
)
|
|
|
|
elif "tcp" in self.config["kiss"] and self.config["kiss"]["tcp"].get(
|
|
|
|
"enabled",
|
|
|
|
False,
|
|
|
|
):
|
|
|
|
LOG.debug(
|
2022-11-22 13:32:19 -05:00
|
|
|
"KISS({}) TCP Connection to {}:{}".format(
|
|
|
|
kiss.__version__,
|
2021-09-01 14:48:22 -04:00
|
|
|
self.config["kiss"]["tcp"]["host"],
|
|
|
|
self.config["kiss"]["tcp"]["port"],
|
2021-02-25 21:01:52 -05:00
|
|
|
),
|
|
|
|
)
|
2022-11-22 13:32:19 -05:00
|
|
|
self.kiss = kiss.TCPKISS(
|
|
|
|
host=self.config["kiss"]["tcp"]["host"],
|
|
|
|
port=int(self.config["kiss"]["tcp"]["port"]),
|
|
|
|
strip_df_start=True,
|
2021-02-25 21:01:52 -05:00
|
|
|
)
|
|
|
|
|
2022-11-22 13:32:19 -05:00
|
|
|
LOG.debug("Starting KISS interface connection")
|
|
|
|
self.kiss.start()
|
2021-02-25 21:01:52 -05:00
|
|
|
|
2022-11-22 13:32:19 -05:00
|
|
|
@trace.trace
|
2021-02-25 21:01:52 -05:00
|
|
|
def stop(self):
|
2022-11-22 13:32:19 -05:00
|
|
|
try:
|
|
|
|
self.kiss.stop()
|
|
|
|
self.kiss.loop.call_soon_threadsafe(
|
|
|
|
self.kiss.protocol.transport.close,
|
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
LOG.exception(ex)
|
2021-02-25 21:01:52 -05:00
|
|
|
|
2021-09-17 09:32:30 -04:00
|
|
|
def set_filter(self, filter):
|
|
|
|
# This does nothing right now.
|
|
|
|
pass
|
|
|
|
|
2022-11-22 13:32:19 -05:00
|
|
|
def parse_frame(self, frame_bytes):
|
|
|
|
frame = Frame.from_bytes(frame_bytes)
|
|
|
|
# Now parse it with aprslib
|
|
|
|
packet = aprslib.parse(str(frame))
|
|
|
|
kwargs = {
|
|
|
|
"frame": str(frame),
|
|
|
|
"packet": packet,
|
|
|
|
}
|
|
|
|
self._parse_callback(**kwargs)
|
2022-07-28 16:24:25 -04:00
|
|
|
|
2022-11-22 13:32:19 -05:00
|
|
|
def consumer(self, callback, blocking=False, immortal=False, raw=False):
|
|
|
|
LOG.debug("Start blocking KISS consumer")
|
|
|
|
self._parse_callback = callback
|
|
|
|
self.kiss.read(callback=self.parse_frame, min_frames=None)
|
|
|
|
LOG.debug("END blocking KISS consumer")
|
2021-02-25 21:01:52 -05:00
|
|
|
|
|
|
|
def send(self, msg):
|
|
|
|
"""Send an APRS Message object."""
|
2022-08-03 09:29:27 -04:00
|
|
|
|
|
|
|
# payload = (':%-9s:%s' % (
|
|
|
|
# msg.tocall,
|
|
|
|
# payload
|
|
|
|
# )).encode('US-ASCII'),
|
|
|
|
# payload = str(msg).encode('US-ASCII')
|
2022-11-24 11:27:58 -05:00
|
|
|
payload = None
|
|
|
|
path = ["WIDE1-1", "WIDE2-1"]
|
2022-11-22 13:32:19 -05:00
|
|
|
if isinstance(msg, messaging.AckMessage):
|
|
|
|
msg_payload = f"ack{msg.id}"
|
2022-11-24 11:27:58 -05:00
|
|
|
elif isinstance(msg, messaging.RawMessage):
|
|
|
|
payload = msg.message.encode("US-ASCII")
|
|
|
|
path = ["WIDE2-1"]
|
2022-11-22 13:32:19 -05:00
|
|
|
else:
|
|
|
|
msg_payload = f"{msg.message}{{{str(msg.id)}"
|
2022-11-24 11:27:58 -05:00
|
|
|
|
|
|
|
if not payload:
|
|
|
|
payload = (
|
|
|
|
":{:<9}:{}".format(
|
|
|
|
msg.tocall,
|
|
|
|
msg_payload,
|
|
|
|
)
|
|
|
|
).encode("US-ASCII")
|
|
|
|
|
2022-08-03 09:29:27 -04:00
|
|
|
LOG.debug(f"Send '{payload}' TO KISS")
|
2022-11-22 13:32:19 -05:00
|
|
|
frame = Frame.ui(
|
|
|
|
destination=msg.tocall,
|
|
|
|
source=msg.fromcall,
|
2022-11-24 11:27:58 -05:00
|
|
|
path=path,
|
2022-11-22 13:32:19 -05:00
|
|
|
info=payload,
|
2021-02-25 21:01:52 -05:00
|
|
|
)
|
2022-11-22 13:32:19 -05:00
|
|
|
self.kiss.write(frame)
|