X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fgps303proto.py;h=e3776c3a529276fd333d619ea1946a8264f8337b;hb=d6d5b6c95b677d98777959040bac808e4c5499c7;hp=baa9a6cf64ac7af7b6fcb5605de3783f02b35955;hpb=7bf96d53196eb6caf035335260a2dc25cec72e57;p=loctrkd.git diff --git a/gps303/gps303proto.py b/gps303/gps303proto.py index baa9a6c..e3776c3 100755 --- a/gps303/gps303proto.py +++ b/gps303/gps303proto.py @@ -32,10 +32,11 @@ from typing import ( ) __all__ = ( - "GPS303Conn", + "Stream", "class_by_prefix", "inline_response", "parse_message", + "probe_buffer", "proto_by_name", "DecodeError", "Respond", @@ -84,7 +85,7 @@ __all__ = ( MAXBUFFER: int = 4096 -class GPS303Conn: +class Stream: def __init__(self) -> None: self.buffer = b"" @@ -879,6 +880,18 @@ def proto_of_message(packet: bytes) -> int: return packet[1] +def imei_from_packet(packet: bytes) -> Optional[str]: + if proto_of_message(packet) == LOGIN.PROTO: + msg = parse_message(packet) + if isinstance(msg, LOGIN): + return msg.imei + return None + + +def is_goodbye_packet(packet: bytes) -> bool: + return proto_of_message(packet) == HIBERNATION.PROTO + + def inline_response(packet: bytes) -> Optional[bytes]: proto = proto_of_message(packet) if proto in CLASSES: @@ -888,6 +901,15 @@ def inline_response(packet: bytes) -> Optional[bytes]: return None +def probe_buffer(buffer: bytes) -> bool: + framestart = buffer.find(b"xx") + if framestart < 0: + return False + if len(buffer) - framestart < 6: + return False + return True + + def parse_message(packet: bytes, is_incoming: bool = True) -> GPS303Pkt: """From a packet (without framing bytes) derive the XXX.In object""" length, proto = unpack("BB", packet[:2])