From: Eugene Crosser Date: Sat, 9 Jul 2022 17:58:29 +0000 (+0200) Subject: cleanup framing/deframing of beesure X-Git-Tag: 1.90~32 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=5ef83cb7db7464a5a625b0b7c86c4e25ebbb0de1 cleanup framing/deframing of beesure --- diff --git a/loctrkd/beesure.py b/loctrkd/beesure.py index 9b0255c..a0d802b 100755 --- a/loctrkd/beesure.py +++ b/loctrkd/beesure.py @@ -24,6 +24,7 @@ from typing import ( __all__ = ( "Stream", "class_by_prefix", + "enframe", "inline_response", "parse_message", "probe_buffer", @@ -67,11 +68,6 @@ class Stream: self.imei: Optional[str] = None self.datalen: int = 0 - @staticmethod - def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes: - assert imei is not None and len(imei) == 10 - return f"[LT*{imei:10s}*{len(buffer):04X}*".encode() + buffer + b"]" - def recv(self, segment: bytes) -> List[Union[bytes, str]]: """ Process next segment of the stream. Return successfully deframed @@ -96,12 +92,13 @@ class Stream: ) self.buffer = self.buffer[toskip:] # From this point, buffer starts with a packet header - if self.imei is not None and self.imei != imei: + if self.imei is None: + self.imei = imei + if self.imei != imei: msgs.append( f"Packet's imei {imei} mismatches" - f" previous value {self.imei}" + f" previous value {self.imei}, old value kept" ) - self.imei = imei self.datalen = datalen if len(self.buffer) < self.datalen + 21: # Incomplete packet break @@ -125,6 +122,13 @@ class Stream: return ret +def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes: + assert imei is not None and len(imei) == 10 + off, vid, _, dlen = _framestart(buffer) + assert off == 0 + return f"[{vid:2s}*{imei:10s}*{dlen:04X}*".encode() + buffer[20:] + + ### Parser/Constructor ### @@ -289,7 +293,8 @@ class BeeSurePkt(metaclass=MetaPkt): @property def packed(self) -> bytes: - return self.encode().encode() # first is object's, second str's + buffer = self.encode().encode() + return f"[LT*0000000000*{len(buffer):04X}*".encode() + buffer + b"]" class UNKNOWN(BeeSurePkt): @@ -411,7 +416,7 @@ def proto_by_name(name: str) -> str: def proto_of_message(packet: bytes) -> str: - return PROTO_PREFIX + packet.split(b",")[0].decode() + return PROTO_PREFIX + packet[20:-1].split(b",")[0].decode() def imei_from_packet(packet: bytes) -> Optional[str]: diff --git a/loctrkd/collector.py b/loctrkd/collector.py index 136ecba..9834500 100644 --- a/loctrkd/collector.py +++ b/loctrkd/collector.py @@ -27,16 +27,16 @@ MAXBUFFER: int = 4096 class ProtoModule: class Stream: - @staticmethod - def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes: - ... - def recv(self, segment: bytes) -> List[Union[bytes, str]]: ... def close(self) -> bytes: ... + @staticmethod + def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes: + ... + @staticmethod def probe_buffer(buffer: bytes) -> bool: ... @@ -139,9 +139,9 @@ class Client: return msgs def send(self, buffer: bytes) -> None: - assert self.stream is not None + assert self.stream is not None and self.pmod is not None try: - self.sock.send(self.stream.enframe(buffer, imei=self.imei)) + self.sock.send(self.pmod.enframe(buffer, imei=self.imei)) except OSError as e: log.error( "Sending to fd %d (IMEI %s): %s", diff --git a/loctrkd/watch.py b/loctrkd/watch.py index ab041ed..7221d2b 100644 --- a/loctrkd/watch.py +++ b/loctrkd/watch.py @@ -39,11 +39,11 @@ def runserver(conf: ConfigParser) -> None: try: while True: zmsg = Bcast(zsub.recv()) - print("Bcast:", zmsg) + print("I" if zmsg.is_incoming else "O", zmsg.proto, zmsg.imei) for pmod in pmods: if zmsg.proto.startswith(pmod.PROTO_PREFIX): msg = pmod.parse_message(zmsg.packet, zmsg.is_incoming) - print("I" if zmsg.is_incoming else "O", zmsg.imei, msg) + print(msg) except KeyboardInterrupt: pass diff --git a/loctrkd/zx303proto.py b/loctrkd/zx303proto.py index ebead92..c47c216 100755 --- a/loctrkd/zx303proto.py +++ b/loctrkd/zx303proto.py @@ -92,10 +92,6 @@ class Stream: def __init__(self) -> None: self.buffer = b"" - @staticmethod - def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes: - return b"xx" + buffer + b"\r\n" - def recv(self, segment: bytes) -> List[Union[bytes, str]]: """ Process next segment of the stream. Return successfully deframed @@ -150,6 +146,10 @@ class Stream: return ret +def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes: + return b"xx" + buffer + b"\r\n" + + ### Parser/Constructor ###