Skip to content

Commit 211f709

Browse files
committed
fix: mc parser and tests
1 parent 6b7b039 commit 211f709

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

sd_protocols/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ def length_in_range(self, protocol_id, message_length):
141141
# Check minimum length
142142
min_len = self.check_property(protocol_id, 'length_min', -1)
143143
if message_length < min_len:
144-
return (0, 'message is to short')
144+
return (0, 'message is too short')
145145

146146
# Check maximum length
147147
max_len = self.get_property(protocol_id, 'length_max')
148148
if max_len is not None and message_length > max_len:
149-
return (0, 'message is to long')
149+
return (0, 'message is too long')
150150

151151
return (1, '')
152152

signalduino/parser/mc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from __future__ import annotations
44

55
import logging
6+
import re
7+
68
from typing import Any, Dict, Iterable
79

810
from sd_protocols import SDProtocols
@@ -47,6 +49,11 @@ def parse(self, frame: RawFrame) -> Iterable[DecodedMessage]:
4749
msg_data["mcbitnum"] = msg_data["L"]
4850
msg_data["messagetype"] = msg_data.get("M", "MC") # M or MC from header M[cC]
4951

52+
raw_hex = msg_data["raw_hex"]
53+
if not re.fullmatch(r"[0-9a-fA-F]+", raw_hex):
54+
self.logger.warning("Ignoring MC message with non-hexadecimal raw_hex: %s", raw_hex)
55+
return
56+
5057
self._extract_metadata(frame, msg_data)
5158

5259
try:

tests/test_mc_parser.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ def mc_parser(mock_protocols, logger):
2424
"MC;LL=-762;LH=544;SL=-402;SH=345;D=DB6D5B54;C=342;L=48;R=32;", # Funkbus (PID 119) - 48 bits
2525
"119",
2626
"2C175F30008F", # Expected Funkbus hex output (48 bits -> 12 Hex chars)
27-
-32.0,
27+
-58.0,
2828
),
2929
(
3030
"MC;LL=100;LH=100;D=11112222;C=500;L=32;R=10;", # Grothe (PID 108) - 32 bits
3131
"108",
3232
"AAAAAAAA", # Expected Grothe hex output (32 bits -> 8 Hex chars)
33-
-10.0,
33+
-69.0,
3434
),
3535
],
3636
)
3737
def test_mc_parser_valid_message(mc_parser, mock_protocols, line, expected_protocol, expected_payload, expected_rssi):
3838
"""Test valid MC messages."""
3939
frame = RawFrame(line=line)
4040
demodulated = [{"protocol_id": expected_protocol, "payload": expected_payload}]
41-
mock_protocols.demodulate.return_value = demodulated
41+
mock_protocols.demodulate_mc.return_value = demodulated
4242

4343
result = list(mc_parser.parse(frame))
4444

45-
mock_protocols.demodulate.assert_called_once()
45+
mock_protocols.demodulate_mc.assert_called_once()
4646
assert len(result) == 1
4747
assert result[0].protocol_id == expected_protocol
4848
assert result[0].payload == expected_payload
@@ -52,15 +52,15 @@ def test_mc_parser_valid_message(mc_parser, mock_protocols, line, expected_proto
5252
@pytest.mark.parametrize(
5353
"line, log_message, expects_demodulate_call, raises_exception",
5454
[
55-
("MC;LL=-762;LH=544;D=DB6;C=342;L=12;R=bar;", "Could not parse RSSI value: bar", True, False),
55+
("MC;LL=-762;LH=544;D=DB6;C=342;L=12;R=bar;", "Could not parse RSSI value: bar", False, True),
5656
(
5757
"MC;LL=-653;LH=679;SL=-310;SH=351;C=332;L=21;R=20;",
58-
"Ignoring MC message without data (D)",
58+
"Ignoring MC message missing required fields (D, C, or L)",
5959
False, # No 'D=' part, so demodulate is not called
6060
False,
6161
),
6262
("FOO;LL=1;D=FF;", "Not an MC message", False, False),
63-
("MC;LL=-2738;LH=3121;SL=-1268;SH=1667;D=GGD9FF0E;C=1465;L=32;R=246;", "Error during MC demodulation for line:", True, True),
63+
("MC;LL=-2738;LH=3121;SL=-1268;SH=1667;D=GGD9FF0E;C=1465;L=32;R=246;", "Ignoring MC message with non-hexadecimal raw_hex:", False, True),
6464
],
6565
)
6666
def test_mc_parser_corrupt_messages(mc_parser, mock_protocols, caplog, line, log_message, expects_demodulate_call, raises_exception):
@@ -88,14 +88,14 @@ def test_mc_parser_corrupt_messages(mc_parser, mock_protocols, caplog, line, log
8888
@pytest.mark.parametrize(
8989
"line, expects_demodulate_call",
9090
[
91-
("MC;LL=-2883;LH=2982;XX=-1401;SH=1509;D=AF7EFF2E;C=1466;L=31;R=14;", True),
92-
("MC;LL=-2895;LH=2976;S=-1401;SH=1685;D=AFBEFFCE;C=1492;L=31;R=23;", True),
93-
("MC;LL=-2901;LH=2958{SL=-1412;SH=1509;D=AFBEFFCE;C=1463;L=31;R=17;", True),
94-
("MC;LH=-2889;LH=2963;SL=-1420;SH=1514;D=AF377F87;C=1464;L=32;R=11;", True),
95-
("MC;LL=-2872:LH=2985;SL=-1401;SH=1527;D=AFFB7F2B;C=1464;L=32;R=10;", True),
96-
("MC;LL=-2868;LL=-1416;SH=1525;D=AFBB7F4B;C=1468;L=32;R=16;", True),
97-
("MC;LL=-762;LH=544;SL=-402;SH=345;D=DB6D5B54;C=342;L=30;R=32;", True), # Too long (sd_protocols responsibility)
98-
("MC;LL=-762;LH=544;SL=-402;SH=345;D=DB6;C=342;L=12;R=32;", True), # Too short (sd_protocols responsibility)
91+
("MC;LL=-2883;LH=2982;XX=-1401;SH=1509;D=AF7EFF2E;C=1466;L=31;R=14;", False),
92+
("MC;LL=-2895;LH=2976;S=-1401;SH=1685;D=AFBEFFCE;C=1492;L=31;R=23;", False),
93+
("MC;LL=-2901;LH=2958{SL=-1412;SH=1509;D=AFBEFFCE;C=1463;L=31;R=17;", False),
94+
("MC;LH=-2889;LH=2963;SL=-1420;SH=1514;D=AF377F87;C=1464;L=32;R=11;", False),
95+
("MC;LL=-2872:LH=2985;SL=-1401;SH=1527;D=AFFB7F2B;C=1464;L=32;R=10;", False),
96+
("MC;LL=-2868;LL=-1416;SH=1525;D=AFBB7F4B;C=1468;L=32;R=16;", False),
97+
("MC;LL=-762;LH=544;SL=-402;SH=345;D=DB6D5B54;C=342;L=30;R=32;", False), # Too long (sd_protocols responsibility)
98+
("MC;LL=-762;LH=544;SL=-402;SH=345;D=DB6;C=342;L=12;R=32;", False), # Too short (sd_protocols responsibility)
9999
],
100100
)
101101
def test_mc_parser_demodulate_failures(mc_parser, mock_protocols, line, expects_demodulate_call):

0 commit comments

Comments
 (0)