-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanchester.py
More file actions
795 lines (600 loc) · 33.1 KB
/
manchester.py
File metadata and controls
795 lines (600 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
from __future__ import annotations
import logging
from typing import Any, Dict, Iterable
class ManchesterMixin:
"""Mixin providing Manchester signal encoding/decoding methods.
Manchester signals represent binary data where:
- 0 is represented as "10" (high-to-low transition)
- 1 is represented as "01" (low-to-high transition)
Methods in this mixin handle protocol-specific variations of Manchester
encoding and conversion to hex or other output formats.
"""
def _convert_mc_hex_to_bits(self, name: str, raw_hex: str, polarity_invert: bool, hlen: int) -> tuple[int, str | None]:
"""Converts raw hex data to a bit string, applying polarity inversion if necessary.
This encapsulates the logic from Perl lines 2851-2876.
Args:
name: Device name for logging.
raw_hex: The raw data as hex string (D=...).
polarity_invert: Whether to invert the hex string before conversion.
hlen: Length of the hex string.
Returns:
Tuple: (1, bit_string) on success or (-1, error_message) on failure.
"""
blen = hlen * 4
if polarity_invert:
# Perl: $rawDataInverted =~ tr/0123456789ABCDEF/FEDCBA9876543210/;
raw_hex_to_use = raw_hex.translate(str.maketrans('0123456789ABCDEF', 'FEDCBA9876543210'))
else:
raw_hex_to_use = raw_hex
# Perl: unpack("B$blen", pack("H$hlen", $rawData))
try:
bit_data = self.hex_to_bin_str(raw_hex_to_use)
self._logging(f"{name}: extracted data {bit_data} (bin)", 5)
return (1, bit_data)
except Exception as e:
self._logging(f"{name}: Error during hex to bin conversion: {e}", 3)
return (-1, f"Hex to bin conversion failed: {e}")
def _demodulate_mc_data(self, name: str, protocol_id: int, clock: int, raw_hex: str, mcbitnum: int, messagetype: str, version: str | None) -> tuple[int, str, dict[str, Any]]:
"""
Performs common MC checks (clock/length/polarity/conversion) and delegates final decoding.
This encapsulates the logic from SIGNALduino_Parse_MC (lines 2854-2917).
Args:
name: Device name for logging.
protocol_id: The ID of the protocol being tested.
clock: The clock value (C=...).
raw_hex: The raw data as hex string (D=...).
mcbitnum: The expected bit length (L=...).
messagetype: The message type ('MC' or 'Mc').
version: The firmware version string.
Returns:
List of tuples: [(rcode, dmsg, metadata)] where rcode is 1 on success.
"""
from sd_protocols import SDProtocols
# 1. Clock/Length Check (Perl lines 2857-2859)
length_min = int(self.check_property(protocol_id, 'length_min', -1))
if mcbitnum < length_min:
self._logging(f"{name}: Parse_MC, bit_length {mcbitnum} too short (min {length_min})", 5)
return ( -1, 'message is too short', {})
# Check if protocol data is longer than maximum (Perl lines 2862-2864)
length_max = int(self.check_property(protocol_id, 'length_max', 9999))
if mcbitnum > length_max:
self._logging(f"{name}: Parse_MC, bit_length {mcbitnum} too long (max {length_max})", 5)
return ( -1, 'message is too long', {})
clockrange = self.get_property(protocol_id, 'clockrange')
if clockrange and len(clockrange) >= 2:
clock_min, clock_max = clockrange, clockrange
if not (clock > clock_min and clock < clock_max):
self._logging(f"{name}: Parse_MC, clock {clock} not in range ({clock_min}..{clock_max})", 5)
return (-1, 'clock out of range', {})
self._logging(f"{name}: Parse_MC, clock and min length matched", 5)
# 2. Polarity Check (Perl lines 2865-2871)
polarity_invert = (self.check_property(protocol_id, 'polarity', '') == 'invert')
self._logging(f"{name}: polarityInvert={polarity_invert}", 5)
if messagetype == 'Mc' or (version and version[:6] == 'V 3.2.'):
polarity_invert = polarity_invert ^ 1
self._logging(f"{name}: polarityInvert toggled to {polarity_invert}", 5)
# 3. Convert Hex to Bit Data (Perl lines 2873-2878)
hlen = len(raw_hex)
rcode, bit_data = self._convert_mc_hex_to_bits(name, raw_hex, polarity_invert, hlen)
if rcode == -1:
return (rcode, bit_data, {})
# 4. Call protocol-specific method (Perl lines 2880-2915)
method_name_full = self.get_property(protocol_id, 'method')
if not method_name_full:
self._logging(f"{name}: Parse_MC, Error: Unknown method referenced by '{protocol_id}'", 5)
return [(-1, 'Protocol method not defined', {})]
# Extract method name part, assuming format 'module.method_name' or just 'method_name'
method_name = method_name_full.split('.')[-1]
if hasattr(self, method_name) and callable(getattr(self, method_name)):
method_func = getattr(self, method_name)
# Perl call: $method->($hash->{protocolObject},$name,$bitData,$id,$mcbitnum)
# Python call: method_func(self, name, bit_data, protocol_id, len(bit_data))
# Note: mcbitnum passed here is the length of the *decoded* bit string, which is what Perl uses as the 5th argument.
rcode, res = method_func(self, name, bit_data, protocol_id, len(bit_data))
else:
self._logging(f"{name}: Parse_MC, Error: Unknown method {method_name} referenced by '{method_name_full}'. Please define it or check protocol configuration.", 5)
return (-1, f'Unknown protocol method {method_name_full}', {})
if rcode == -1:
res = res if res is not None else 'Decoding failed'
self._logging(f"{name}: Parse_MC, protocol does not match return from method: ({res})", 5)
return (-1, res, {})
# 5. Formatting $dmsg (Perl lines 2888-2889)
preamble = self.check_property(protocol_id, 'preamble', '')
dmsg = f"{preamble}{res}"
self._logging(f"{name}: Parse_MC, Decoded payload: {res}", 4)
metadata = {
"protocol_id": protocol_id,
"rssi": None,
"freq_afc": None,
}
self._logging(f"{name}: Parse_MC, successfully decoded MC protocol id {protocol_id} dmsg {dmsg}", 4)
return (1, dmsg, metadata)
def _demodulate_mn_data(self, name: str, protocol_id: str, msg_data: Dict[str, Any]) -> list[dict[str, Any]]:
"""
Performs checks and delegates decoding for MN messages.
Args:
name: Device name for logging.
protocol_id: The ID of the protocol being tested.
msg_data: Dictionary containing message data, including 'data' (raw hex).
Returns:
List of dicts: [{protocol_id: str, payload: str, meta: dict}] where rcode is 1 on success.
"""
# MN messages do not use Manchester decoding, only protocol specific decoding
# 1. Call protocol-specific method (Perl lines SIGNALduino_Parse_MN)
method_name_full = self.get_property(protocol_id, 'method')
if not method_name_full:
self._logging(f"{name}: Parse_MN, Error: Unknown method referenced by '{protocol_id}'", 5)
return []
# Extract method name part, assuming format 'module.method_name' or just 'method_name'
method_name = method_name_full.split('.')[-1]
if hasattr(self, method_name) and callable(getattr(self, method_name)):
method_func = getattr(self, method_name)
# Python call: method_func(msg_data, msg_type) where msg_type is 'MN'
try:
# The result should be a list of dicts like in _demodulate_mc_data or a direct result
demodulated_list = method_func(msg_data, 'MN')
except TypeError:
self._logging(f"{name}: Parse_MN, Method {method_name} failed due to wrong signature/arguments.", 3)
return []
else:
self._logging(f"{name}: Parse_MN, Error: Unknown method {method_name} referenced by '{method_name_full}'. Please define it or check protocol configuration.", 5)
return []
if not isinstance(demodulated_list, list) or not demodulated_list:
self._logging(f"{name}: Parse_MN, protocol does not match return from method.", 5)
return []
# For MN, we usually only expect one result and the preamble is not used.
for decoded in demodulated_list:
if not isinstance(decoded, dict) or "protocol_id" not in decoded:
self._logging(f"{name}: Parse_MN, Invalid result from demodulator: {decoded}", 3)
continue
self._logging(f"{name}: Parse_MN, successfully decoded MN protocol id {protocol_id}", 4)
# Return a list containing a single dictionary in the expected format
return [
{
"protocol_id": str(decoded["protocol_id"]),
"payload": str(decoded.get("payload", "")),
"meta": decoded.get("meta", {}),
}
]
return []
def mcBit2Funkbus(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Funkbus (ID 119) Manchester signal with parity & checksum validation.
Funkbus protocol uses Manchester-encoded signals with parity bits
and CRC-like checksum validation. The demodulated signal must pass
both parity and checksum verification.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (typically 119 for Funkbus)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success
(-1, error_message) on parity/checksum error
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = self.get_property(protocol_id, "length_max")
if length_max is not None and mcbitnum > int(length_max):
return (-1, 'message is too long')
self._logging(f"lib/mcBitFunkbus, {name} Funkbus: raw={bit_data}", 5)
# Convert Manchester: 1->lh, 0->hl, then decode to differential manchester
converted = bit_data.replace('1', 'lh').replace('0', 'hl')
s_bitmsg = self.mc2dmc(converted)
# Protocol-specific bit arrangement
protocol_id_int = int(protocol_id) if isinstance(protocol_id, str) else protocol_id
if protocol_id_int == 119:
# Funkbus specific: look for sync pattern '01100'
pos = s_bitmsg.find('01100')
if pos >= 0 and pos < 5:
s_bitmsg = '001' + s_bitmsg[pos:]
if len(s_bitmsg) < 48:
return (-1, 'wrong bits at begin')
else:
return (-1, 'wrong bits at begin')
else:
s_bitmsg = '0' + s_bitmsg
# Calculate parity and checksum
hex_data = ""
xor_val = 0
chk = 0
parity = 0
for i in range(6): # 6 bytes
byte_str = s_bitmsg[i*8:(i+1)*8]
data = int(byte_str, 2)
hex_data += f"{data:02X}"
if i < 5:
xor_val ^= data
else:
chk = data & 0x0F
xor_val ^= data & 0xE0
data &= 0xF0
# Parity calculation
temp = data
while temp:
parity ^= (temp & 1)
temp >>= 1
if parity == 1:
return (-1, 'parity error')
# Checksum validation
xor_nibble = ((xor_val & 0xF0) >> 4) ^ (xor_val & 0x0F)
result = 0
if xor_nibble & 0x8:
result ^= 0xC
if xor_nibble & 0x4:
result ^= 0x2
if xor_nibble & 0x2:
result ^= 0x8
if xor_nibble & 0x1:
result ^= 0x3
if result != chk:
return (-1, 'checksum error')
self._logging(f"lib/mcBitFunkbus, {name} Funkbus: len={len(s_bitmsg)} parity={parity} result={result} chk={hex_data}", 4)
return (1, hex_data)
def mcBit2Sainlogic(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Sainlogic weather sensor Manchester signal.
Sainlogic sensors transmit 128-bit Manchester-encoded messages.
This handler synchronizes the bitstream if needed and extracts
the message portion.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
Raises:
Returns error tuple if message is too short/long or sync pattern not found
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2Sainlogic, protocol {protocol_id}, length {mcbitnum}", 5)
self._logging(f"{name}: lib/mcBit2Sainlogic, {bit_data}", 5)
length_max = int(self.check_property(protocol_id, "length_max", 0))
if mcbitnum > length_max:
return (-1, 'message is too long')
if mcbitnum < 128:
start = bit_data.find('010100')
self._logging(f"{name}: lib/mcBit2Sainlogic, protocol {protocol_id}, start found at pos {start}", 5)
if start < 0 or start > 10:
self._logging(f"{name}: lib/mcBit2Sainlogic, protocol {protocol_id}, start 010100 not found", 4)
return (-1, f"{name}: lib/mcBit2Sainlogic, start 010100 not found")
# Prepend '1' bits until we have 10+ bits before the sync pattern
while start < 10:
bit_data = '1' + bit_data
start = bit_data.find('010100')
# Trim to 128 bits
bit_data = bit_data[:128]
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2Sainlogic, {bit_data}", 5)
length_min = int(self.check_property(protocol_id, "length_min", 0))
if mcbitnum < length_min:
return (-1, 'message is too short')
return (1, self.bin_str_2_hex_str(bit_data))
def mcBit2AS(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode AS (ambient sound / weather sensor) Manchester signal.
AS protocol uses a "1100" sync pattern (repeated high values).
The message is extracted between two sync patterns.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
Raises:
Returns (-1, None) if valid AS message pattern not detected
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
# Look for AS sync pattern "1100" starting at position 16+
start_pos = bit_data.find('1100', 16)
if start_pos >= 0:
# Valid AS detected!
self._logging("lib/mcBit2AS, AS protocol detected", 5)
# Find next sync pattern (message end)
end_pos = bit_data.find('1100', start_pos + 16)
if end_pos == -1:
end_pos = len(bit_data)
message_length = end_pos - start_pos
length_min = int(self.check_property(protocol_id, "length_min", -1))
if message_length < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and message_length > length_max:
return (-1, 'message is too long')
msgbits = bit_data[start_pos:]
ashex = self.bin_str_2_hex_str(msgbits)
self._logging(f"{name}: AS, protocol converted to hex: ({ashex}) with length ({message_length}) bits", 5)
return (1, ashex)
# Wenn kein Sync-Pattern gefunden wird, aber die Länge ok ist, konvertiere trotzdem
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and mcbitnum > length_max:
return (-1, 'message is too long')
ashex = self.bin_str_2_hex_str(bit_data)
return (1, ashex)
def mcBit2Hideki(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Hideki temperature/humidity sensor Manchester signal.
Hideki sensors transmit variable-length Manchester messages.
This handler extracts and converts the message to hex.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2Hideki, protocol {protocol_id}, length {mcbitnum}", 5)
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and mcbitnum > length_max:
return (-1, 'message is too long')
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: Hideki converted to hex: {hex_msg}", 5)
return (1, hex_msg)
def mcBit2Maverick(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Maverick (BBQ thermometer) Manchester signal.
Maverick sensors transmit Manchester-encoded temperature and
identification data in a fixed-length message format.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2Maverick, protocol {protocol_id}, length {mcbitnum}", 5)
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and mcbitnum > length_max:
return (-1, 'message is too long')
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: Maverick converted to hex: {hex_msg}", 5)
return (1, hex_msg)
def mcBit2OSV1(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Oregon Scientific V1 weather sensor Manchester signal.
Oregon Scientific V1 sensors use Manchester encoding for weather
station data transmission. This handler processes V1 protocol format.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2OSV1, protocol {protocol_id}, length {mcbitnum}", 5)
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and mcbitnum > length_max:
return (-1, 'message is too long')
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: OSV1 converted to hex: {hex_msg}", 5)
return (1, hex_msg)
def mcBit2OSV2o3(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Oregon Scientific V2/V3 weather sensor Manchester signal.
Oregon Scientific V2 and V3 sensors use enhanced Manchester encoding
with parity/checksum validation for improved reliability.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2OSV2o3, protocol {protocol_id}, length {mcbitnum}", 5)
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and mcbitnum > length_max:
return (-1, 'message is too long')
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: OSV2o3 converted to hex: {hex_msg}", 5)
return (1, hex_msg)
def mcBit2OSPIR(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Oregon Scientific PIR (motion) sensor Manchester signal.
Oregon Scientific PIR sensors transmit motion detection data in
Manchester-encoded format with specific protocol structure.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2OSPIR, protocol {protocol_id}, length {mcbitnum}", 5)
length_min = int(self.check_property(protocol_id, "length_min", -1))
if mcbitnum < length_min:
return (-1, 'message is too short')
length_max = int(self.check_property(protocol_id, "length_max", 9999))
if length_max is not None and mcbitnum > length_max:
return (-1, 'message is too long')
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: OSPIR converted to hex: {hex_msg}", 5)
return (1, hex_msg)
def mcRaw(self, name: str, bit_data: str, protocol_id: int, mcbitnum: int, other_arg: Any = None) -> tuple[int, str | None]:
"""
Default output helper for Manchester signals.
Checks for length_max and returns a hex string.
Args:
name: Device/message name for logging.
bit_data: Raw Manchester-encoded bitstring.
protocol_id: Protocol identifier (ID).
mcbitnum: Bit length (from L=).
other_arg: Dummy argument to match the 6 positional arguments from _demodulate_mc_data.
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure.
"""
# mcbitnum is directly provided by the caller in this case
# if mcbitnum is None:
# mcbitnum = len(bit_data)
length_max = int(self.check_property(protocol_id, "length_max", 0))
mcbitnum_int = int(mcbitnum)
if mcbitnum_int > length_max:
return (-1, "message is too long")
# binStr2hexStr in Perl -> self.bin_str_2_hex_str in Python
return (1, self.bin_str_2_hex_str(bit_data))
def mcBit2TFA(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode TFA (Dostmann) weather station Manchester signal.
TFA weather stations transmit Manchester-encoded sensor data
with temperature, humidity, and pressure information.
This implementation includes duplicate message detection based on Perl logic.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2TFA, protocol {protocol_id}, length {mcbitnum}", 5)
preamble_pos = -1
message_end = -1
retmsg = ''
messages = []
i = 1
# Perl pattern for initial sync: if ($bitData =~ m/(1{9}101)/xms )
# Python equivalent for the start pattern is hard to use directly with index():
# We look for '111111111101' which is the end pattern in Perl's loop,
# but the logic seems to start *after* the preamble '1{9}101' which is 10 bits long.
# Since the Perl code starts searching from $preamble_pos, we need to find that start first.
# Let's search for the end pattern '1111111111101' (13 bits) as a message separator in a do-while loop,
# starting the search from a point determined by the first pattern match.
# In Perl, the first check finds the start: $preamble_pos = index($bitData,'1111111111101',$preamble_pos);
# It seems the first '1{9}101' is used to *initialize* $preamble_pos, but the loop logic is critical.
# A simpler interpretation matching the Python structure is to look for the first valid *message* start.
# Replicating Perl's setup: find the first '1{9}101' to set the starting point.
# In Perl: if ($bitData =~ m/(1{9}101)/xms ) { $preamble_pos=$+; ... }
start_match_pos = bit_data.find('111111111101')
if start_match_pos == -1:
return (-1, 'sync not found')
preamble_pos = start_match_pos + 12 # Position after the *first* end pattern found.
# Replicating the do-while loop logic
# Perl: do { ... } while ($message_end < $mcbitnum);
# The loop iterates as long as the found end ($message_end) is before the total length ($mcbitnum).
while message_end < mcbitnum:
# Perl: $message_end = index($bitData,'1111111111101',$preamble_pos);
message_end = bit_data.find('1111111111101', preamble_pos)
if message_end < preamble_pos:
message_end = mcbitnum # If not found, use the total length as the end marker
message_length = message_end - preamble_pos
part_str = bit_data[preamble_pos:message_end]
self._logging(f"{name}: lib/mcBit2TFA, message start({i})={preamble_pos} end={message_end} with length={message_length}", 4)
self._logging(f"{name}: lib/mcBit2TFA, message part({i})={part_str}", 5)
# Perl: my ($rcode, $rtxt) = $self->LengthInRange($id, $message_length);
rcode, rtxt = self.length_in_range(protocol_id, message_length)
if rcode: # if ($rcode)
hex_val = self.bin_str_2_hex_str(part_str)
messages.append(hex_val)
self._logging(f"{name}: lib/mcBit2TFA, message part({i})={hex_val}", 4)
else: # else { $retmsg = q[, ] . $rtxt; }
retmsg = ', ' + rtxt
# Perl: $preamble_pos=index($bitData,'1101',$message_end)+4;
# The Perl code searches for '1101' after the end and adds 4. This looks like another sync pattern.
preamble_pos = bit_data.find('1101', message_end)
if preamble_pos != -1:
preamble_pos += 4
else:
# If the next sync '1101' isn't found, stop looping to prevent infinite loop if end wasn't mcbitnum.
message_end = mcbitnum
i += 1
# Perl: return ($i,q[loop error, please report this data $bitData]) if ($i==10);
if i == 10:
return (-1, f'loop error, please report this data {bit_data}')
# Perl: my %seen; my @dupmessages = map { 1==$seen{$_}++ ? $_ : () } @messages;
seen = {}
dupmessages = []
for msg in messages:
if seen.get(msg, 0) == 1:
dupmessages.append(msg)
seen[msg] = seen.get(msg, 0) + 1
# Perl: if (scalar(@dupmessages) > 0 ) { return (1,$dupmessages); } else { return (-1,qq[ no duplicate found$retmsg]); }
if len(dupmessages) > 0:
hex_msg = dupmessages # Return the first duplicate found
self._logging(f"{name}: TFA converted to hex (duplicate found): {hex_msg}", 4)
return (1, hex_msg)
else:
return (-1, f' no duplicate found{retmsg}')
def mcBit2Grothe(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Grothe weather sensor Manchester signal.
Grothe sensors transmit fixed 32-bit Manchester-encoded messages.
This handler validates the message length and converts to hex.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring (must be 32 bits)
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
Note:
Grothe protocol requires exactly 32 bits. Messages with different
lengths are rejected.
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2Grothe, bitdata: {bit_data} ({mcbitnum})", 5)
# Grothe requires exactly 32 bits
if mcbitnum != 32:
self._logging(f"{name}: lib/mcBit2Grothe, ERROR - expected 32 bits, got {mcbitnum}", 3)
return (-1, f"message must be 32 bits, got {mcbitnum}")
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: Grothe converted to hex: {hex_msg}", 5)
return (1, hex_msg)
def mcBit2SomfyRTS(self, name, bit_data, protocol_id, mcbitnum=None):
"""Decode Somfy RTS roller shutter/blind control Manchester signal.
Somfy RTS devices transmit 56-bit or 57-bit Manchester-encoded messages.
If 57 bits are received, the first bit is skipped (index 1-56).
This handler validates the message length and converts to hex.
Args:
name: Device/message name for logging
bit_data: Raw Manchester-encoded bitstring (56 or 57 bits)
protocol_id: Protocol identifier (string or int)
mcbitnum: Bit length (defaults to length of bit_data)
Returns:
Tuple: (1, hex_string) on success or (-1, error_message) on failure
Note:
If message is 57 bits, the first bit is discarded, keeping bits 1-56.
Final message must be exactly 56 bits.
"""
if mcbitnum is None:
mcbitnum = len(bit_data)
self._logging(f"{name}: lib/mcBit2SomfyRTS, bitdata: {bit_data} ({mcbitnum})", 5)
# Handle 57-bit message (discard first bit)
if mcbitnum == 57:
bit_data = bit_data[1:57] # Keep bits from index 1 to 56
self._logging(f"{name}: lib/mcBit2SomfyRTS, bitdata: {bit_data}, truncated to length: {len(bit_data)}", 5)
# Validate final length must be 56 bits
if len(bit_data) != 56:
self._logging(f"{name}: lib/mcBit2SomfyRTS, ERROR - expected 56 bits, got {len(bit_data)}", 3)
return (-1, f"message must be 56 bits, got {len(bit_data)}")
hex_msg = self.bin_str_2_hex_str(bit_data)
self._logging(f"{name}: SomfyRTS converted to hex: {hex_msg}", 5)
return (1, hex_msg)