diff --git a/haskell/Dockerfile b/haskell/Dockerfile index 55c4046932..aa63d5e939 100644 --- a/haskell/Dockerfile +++ b/haskell/Dockerfile @@ -1,4 +1,4 @@ -FROM debian:stretch-slim +FROM debian:buster-slim RUN \ apt-get update && \ diff --git a/python/data/filter.bin b/python/data/filter.bin new file mode 100644 index 0000000000..c3258050a1 Binary files /dev/null and b/python/data/filter.bin differ diff --git a/python/sbp/client/framer.py b/python/sbp/client/framer.py index 17b976dfe9..5ec8c2ce50 100644 --- a/python/sbp/client/framer.py +++ b/python/sbp/client/framer.py @@ -31,14 +31,16 @@ class Framer(six.Iterator): Stream of bytes to read from. write : port Stream of bytes to write to. - verbose: boolean + verbose : boolean Increased verbosity of stdout / stderr. Default False - dispatcher: function + dispatcher : function function to call on each deframed message - skip_medata: boolean + skip_medata : boolean do not add "time" and "session-uuid" metadata - sender_id_filter: list - list of integer sender_ids to frame, with the exclusion of all others + sender_id_filter_list : list + list of integer sender_ids to handle, at the exclusion of all others. If the list is empty, will handle all senders. + message_type_filter : list + list of integer message types to handle, at the exclusion of all others. If the list is empty, will handle all senders. """ def __init__(self, @@ -48,7 +50,8 @@ def __init__(self, dispatcher=dispatch, into_buffer=True, skip_metadata=False, - sender_id_filter_list=[]): + sender_id_filter_list=[], + message_type_filter=[]): self._read = read self._write = write self._verbose = verbose @@ -59,6 +62,13 @@ def __init__(self, self._into_buffer = into_buffer self._skip_metadata = skip_metadata self._sender_id_filter_list = sender_id_filter_list + self._message_type_filter = message_type_filter + + def __enter__(self): + return self + + def __exit__(self, *args): + self.breakiter() def __iter__(self): self._broken = False @@ -142,7 +152,8 @@ def _receive(self): if self._verbose: print("crc mismatch: 0x%04X 0x%04X" % (msg_crc, crc)) return None - if (len(self._sender_id_filter_list) == 0 or sender in self._sender_id_filter_list): + if ((len(self._sender_id_filter_list) == 0 or sender in self._sender_id_filter_list) and + (len(self._message_type_filter) == 0 or msg_type in self._message_type_filter)): msg = SBP(msg_type, sender, msg_len, data, crc) try: msg = self._dispatch(msg) diff --git a/python/sbp/client/handler.py b/python/sbp/client/handler.py index da36e91596..a7977b2f5f 100644 --- a/python/sbp/client/handler.py +++ b/python/sbp/client/handler.py @@ -64,7 +64,6 @@ def gen_messages(): for msg, metadata in self._source: if msg.msg_type: yield (msg, metadata) - messages = gen_messages() while True: msg_and_metadata = None diff --git a/python/tests/sbp/client/test_handler.py b/python/tests/sbp/client/test_handler.py index 9678600ecf..cc244e85cf 100644 --- a/python/tests/sbp/client/test_handler.py +++ b/python/tests/sbp/client/test_handler.py @@ -16,8 +16,10 @@ import gc from sbp.client import Framer, Handler +from sbp.client.drivers.file_driver import FileDriver from sbp.msg import SBP from sbp.logging import MsgLog +from sbp.navigation import SBP_MSG_VEL_NED_GNSS class CallbackCounter(object): """ @@ -56,6 +58,16 @@ def test_framer_ok(): assert msg.length == 13 assert msg.crc == 0x4feb +def test_framer_filter(): + with open("./data/filter.bin", "rb") as fp: + with FileDriver(fp) as driver: + with Framer(driver.read, driver.write, message_type_filter=[SBP_MSG_VEL_NED_GNSS]) as framer: + assert sum(1 for _ in framer) == 160 + with open("./data/filter.bin", "rb") as fp: + with FileDriver(fp) as driver: + with Framer(driver.read, driver.write) as framer: + assert sum(1 for _ in framer) == 7056 + def until(p, limit=1000): for i in itertools.count(): if p(): diff --git a/python/tests/sbp/test_messages.py b/python/tests/sbp/test_messages.py index 5a0e8470d3..d5ae7b856b 100644 --- a/python/tests/sbp/test_messages.py +++ b/python/tests/sbp/test_messages.py @@ -55,6 +55,7 @@ def test_message_case_count(): missing_messages == EXPECTED_MISSING_MESSAGES ), "Missing tests for %d out of %d messages" % (missing_messages, total_messages) + @pytest.mark.parametrize("case", CASES) def test_message(case): assert_package(ROOTPATH + "/" + case)