forked from argilo/secplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
secplus_tx.grc
730 lines (727 loc) · 29.1 KB
/
secplus_tx.grc
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
options:
parameters:
author: ''
catch_exceptions: 'True'
category: '[GRC Hier Blocks]'
cmake_opt: ''
comment: ''
copyright: ''
description: ''
gen_cmake: 'On'
gen_linking: dynamic
generate_options: no_gui
hier_block_src_path: '.:'
id: secplus_tx
max_nouts: '0'
output_language: python
placement: (0,0)
qt_qss_theme: ''
realtime_scheduling: ''
run: 'True'
run_command: '{python} -u {filename}'
run_options: run
sizing_mode: fixed
thread_safe_setters: ''
title: ''
window_size: (1000,1000)
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 12.0]
rotation: 0
state: enabled
blocks:
- name: samp_rate
id: variable
parameters:
comment: ''
value: 2e6
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 244]
rotation: 0
state: enabled
- name: seq
id: variable
parameters:
comment: ''
value: '[0]*100 + secplus.encode_ook(rolling, fixed, fast=False)*4 + [0]*100'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 180]
rotation: 0
state: enabled
- name: analog_sig_source_x_0
id: analog_sig_source_x
parameters:
affinity: ''
alias: ''
amp: '0.9'
comment: ''
freq: 300e3
maxoutbuf: '0'
minoutbuf: '0'
offset: '0'
phase: '0'
samp_rate: samp_rate
showports: 'False'
type: complex
waveform: analog.GR_COS_WAVE
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [776, 292.0]
rotation: 180
state: enabled
- name: blocks_multiply_xx_0
id: blocks_multiply_xx
parameters:
affinity: ''
alias: ''
comment: ''
maxoutbuf: '0'
minoutbuf: '0'
num_inputs: '2'
type: complex
vlen: '1'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [632, 304.0]
rotation: 180
state: enabled
- name: blocks_repeat_0
id: blocks_repeat
parameters:
affinity: ''
alias: ''
comment: ''
interp: '1000'
maxoutbuf: '0'
minoutbuf: '0'
type: complex
vlen: '1'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [392, 172.0]
rotation: 0
state: enabled
- name: blocks_vector_source_x_0
id: blocks_vector_source_x
parameters:
affinity: ''
alias: ''
comment: ''
maxoutbuf: '0'
minoutbuf: '0'
repeat: 'False'
tags: '[]'
type: complex
vector: seq
vlen: '1'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [232, 156.0]
rotation: 0
state: enabled
- name: fixed
id: parameter
parameters:
alias: ''
comment: ''
hide: none
label: Fixed code
short_id: ''
type: intx
value: '1234567890'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [576, 12.0]
rotation: 0
state: true
- name: freq
id: parameter
parameters:
alias: ''
comment: ''
hide: none
label: Frequency
short_id: f
type: intx
value: '315150000'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [328, 12.0]
rotation: 0
state: true
- name: osmosdr_sink_0
id: osmosdr_sink
parameters:
affinity: ''
alias: ''
ant0: ''
ant1: ''
ant10: ''
ant11: ''
ant12: ''
ant13: ''
ant14: ''
ant15: ''
ant16: ''
ant17: ''
ant18: ''
ant19: ''
ant2: ''
ant20: ''
ant21: ''
ant22: ''
ant23: ''
ant24: ''
ant25: ''
ant26: ''
ant27: ''
ant28: ''
ant29: ''
ant3: ''
ant30: ''
ant31: ''
ant4: ''
ant5: ''
ant6: ''
ant7: ''
ant8: ''
ant9: ''
args: ''
bb_gain0: '0'
bb_gain1: '20'
bb_gain10: '20'
bb_gain11: '20'
bb_gain12: '20'
bb_gain13: '20'
bb_gain14: '20'
bb_gain15: '20'
bb_gain16: '20'
bb_gain17: '20'
bb_gain18: '20'
bb_gain19: '20'
bb_gain2: '20'
bb_gain20: '20'
bb_gain21: '20'
bb_gain22: '20'
bb_gain23: '20'
bb_gain24: '20'
bb_gain25: '20'
bb_gain26: '20'
bb_gain27: '20'
bb_gain28: '20'
bb_gain29: '20'
bb_gain3: '20'
bb_gain30: '20'
bb_gain31: '20'
bb_gain4: '20'
bb_gain5: '20'
bb_gain6: '20'
bb_gain7: '20'
bb_gain8: '20'
bb_gain9: '20'
bw0: '0'
bw1: '0'
bw10: '0'
bw11: '0'
bw12: '0'
bw13: '0'
bw14: '0'
bw15: '0'
bw16: '0'
bw17: '0'
bw18: '0'
bw19: '0'
bw2: '0'
bw20: '0'
bw21: '0'
bw22: '0'
bw23: '0'
bw24: '0'
bw25: '0'
bw26: '0'
bw27: '0'
bw28: '0'
bw29: '0'
bw3: '0'
bw30: '0'
bw31: '0'
bw4: '0'
bw5: '0'
bw6: '0'
bw7: '0'
bw8: '0'
bw9: '0'
clock_source0: ''
clock_source1: ''
clock_source2: ''
clock_source3: ''
clock_source4: ''
clock_source5: ''
clock_source6: ''
clock_source7: ''
comment: ''
corr0: '0'
corr1: '0'
corr10: '0'
corr11: '0'
corr12: '0'
corr13: '0'
corr14: '0'
corr15: '0'
corr16: '0'
corr17: '0'
corr18: '0'
corr19: '0'
corr2: '0'
corr20: '0'
corr21: '0'
corr22: '0'
corr23: '0'
corr24: '0'
corr25: '0'
corr26: '0'
corr27: '0'
corr28: '0'
corr29: '0'
corr3: '0'
corr30: '0'
corr31: '0'
corr4: '0'
corr5: '0'
corr6: '0'
corr7: '0'
corr8: '0'
corr9: '0'
freq0: freq - 300e3
freq1: 100e6
freq10: 100e6
freq11: 100e6
freq12: 100e6
freq13: 100e6
freq14: 100e6
freq15: 100e6
freq16: 100e6
freq17: 100e6
freq18: 100e6
freq19: 100e6
freq2: 100e6
freq20: 100e6
freq21: 100e6
freq22: 100e6
freq23: 100e6
freq24: 100e6
freq25: 100e6
freq26: 100e6
freq27: 100e6
freq28: 100e6
freq29: 100e6
freq3: 100e6
freq30: 100e6
freq31: 100e6
freq4: 100e6
freq5: 100e6
freq6: 100e6
freq7: 100e6
freq8: 100e6
freq9: 100e6
gain0: '0'
gain1: '10'
gain10: '10'
gain11: '10'
gain12: '10'
gain13: '10'
gain14: '10'
gain15: '10'
gain16: '10'
gain17: '10'
gain18: '10'
gain19: '10'
gain2: '10'
gain20: '10'
gain21: '10'
gain22: '10'
gain23: '10'
gain24: '10'
gain25: '10'
gain26: '10'
gain27: '10'
gain28: '10'
gain29: '10'
gain3: '10'
gain30: '10'
gain31: '10'
gain4: '10'
gain5: '10'
gain6: '10'
gain7: '10'
gain8: '10'
gain9: '10'
if_gain0: '47'
if_gain1: '20'
if_gain10: '20'
if_gain11: '20'
if_gain12: '20'
if_gain13: '20'
if_gain14: '20'
if_gain15: '20'
if_gain16: '20'
if_gain17: '20'
if_gain18: '20'
if_gain19: '20'
if_gain2: '20'
if_gain20: '20'
if_gain21: '20'
if_gain22: '20'
if_gain23: '20'
if_gain24: '20'
if_gain25: '20'
if_gain26: '20'
if_gain27: '20'
if_gain28: '20'
if_gain29: '20'
if_gain3: '20'
if_gain30: '20'
if_gain31: '20'
if_gain4: '20'
if_gain5: '20'
if_gain6: '20'
if_gain7: '20'
if_gain8: '20'
if_gain9: '20'
maxoutbuf: '0'
minoutbuf: '0'
nchan: '1'
num_mboards: '1'
sample_rate: samp_rate
sync: sync
time_source0: ''
time_source1: ''
time_source2: ''
time_source3: ''
time_source4: ''
time_source5: ''
time_source6: ''
time_source7: ''
type: fc32
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [304, 244.0]
rotation: 180
state: enabled
- name: rolling
id: parameter
parameters:
alias: ''
comment: ''
hide: none
label: Rolling code
short_id: ''
type: intx
value: '1234567890'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [448, 12.0]
rotation: 0
state: true
- name: secplus
id: epy_module
parameters:
alias: ''
comment: ''
source_code: "#\n# Copyright 2016,2020-2023 Clayton Smith ([email protected])\n\
#\n# This file is part of secplus.\n#\n# secplus is free software: you can redistribute\
\ it and/or modify\n# it under the terms of the GNU General Public License as\
\ published by\n# the Free Software Foundation, either version 3 of the License,\
\ or\n# (at your option) any later version.\n#\n# secplus is distributed in\
\ the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even\
\ the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\
\ See the\n# GNU General Public License for more details.\n#\n# You should\
\ have received a copy of the GNU General Public License\n# along with secplus.\
\ If not, see <http://www.gnu.org/licenses/>.\n#\n\n\"\"\"This module encodes\
\ and decodes Security+ and Security+ 2.0 rolling and fixed\ncodes, provides\
\ utility functions to prepare on-off keying sequences for\ntransmission, and\
\ pretty-prints the codes. It can be used to build stand-alone\napplications.\n\
\"\"\"\n\n\ndef encode(rolling, fixed):\n \"\"\"Encode a Security+ payload\
\ into 40 payload symbols\n\n Arguments:\n rolling -- the rolling code\n\
\ fixed -- the fixed code\n\n Raises a ValueError if the rolling or fixed\
\ code is too large.\n \"\"\"\n\n if rolling >= 2**32:\n raise\
\ ValueError(\"Rolling code must be less than 2^32\")\n if fixed >= 3**20:\n\
\ raise ValueError(\"Fixed code must be less than 3^20\")\n\n rolling\
\ = int(f\"{rolling & 0xfffffffe:032b}\"[::-1], 2)\n rolling_base3 = [0]\
\ * 20\n fixed_base3 = [0] * 20\n for i in range(19, -1, -1):\n \
\ rolling_base3[i] = rolling % 3\n rolling //= 3\n fixed_base3[i]\
\ = fixed % 3\n fixed //= 3\n code = []\n for i in range(20):\n\
\ if i in [0, 10]:\n acc = 0\n acc += rolling_base3[i]\n\
\ code.append(rolling_base3[i])\n acc += fixed_base3[i]\n \
\ code.append(acc % 3)\n return code\n\n\ndef decode(code):\n \"\"\"\
Decode a Security+ transmission and return the rolling and fixed codes.\n\n\
\ Arguments:\n code -- a list containing the 40 payload symbols from a\
\ pair of packets\n \"\"\"\n\n rolling = 0\n fixed = 0\n\n for i\
\ in range(0, 40, 2):\n if i in [0, 20]:\n acc = 0\n\n \
\ digit = code[i]\n rolling = (rolling * 3) + digit\n acc\
\ += digit\n\n digit = (code[i+1] - acc) % 3\n fixed = (fixed\
\ * 3) + digit\n acc += digit\n\n rolling = int(f\"{rolling:032b}\"\
[::-1], 2)\n return rolling, fixed\n\n\ndef _v2_calc_parity(fixed, data):\n\
\ data &= 0xffff0fff\n parity = (fixed >> 32) & 0xf\n for offset in\
\ range(0, 32, 4):\n parity ^= ((data >> offset) & 0xf)\n data |=\
\ (parity << 12)\n return data\n\n\ndef _v2_check_parity(fixed, data):\n\
\ parity = (fixed >> 32) & 0xf\n for offset in range(0, 32, 4):\n \
\ parity ^= ((data >> offset) & 0xf)\n if parity != 0:\n raise\
\ ValueError(\"Parity bits are incorrect\")\n\n\ndef _encode_v2_rolling(rolling):\n\
\ rolling = int(f\"{rolling:028b}\"[::-1], 2)\n rolling_base3 = [0] *\
\ 18\n for i in range(17, -1, -1):\n rolling_base3[i] = rolling %\
\ 3\n rolling //= 3\n rolling1 = rolling_base3[14:18] + rolling_base3[6:10]\
\ + rolling_base3[1:2]\n rolling2 = rolling_base3[10:14] + rolling_base3[2:6]\
\ + rolling_base3[0:1]\n return rolling1, rolling2\n\n\ndef _decode_v2_rolling(rolling1,\
\ rolling2):\n rolling_digits = rolling2[8:] + rolling1[8:]\n rolling_digits\
\ += rolling2[4:8] + rolling1[4:8]\n rolling_digits += rolling2[:4] + rolling1[:4]\n\
\n rolling = 0\n for digit in rolling_digits:\n rolling = (rolling\
\ * 3) + digit\n if rolling >= 2**28:\n raise ValueError(\"Rolling\
\ code was not in expected range\")\n return int(f\"{rolling:028b}\"[::-1],\
\ 2)\n\n\ndef _v2_combine_halves(rolling1, rolling2, fixed1, fixed2, data1,\
\ data2):\n rolling = _decode_v2_rolling(rolling1, rolling2)\n fixed =\
\ int(\"\".join(str(bit) for bit in fixed1 + fixed2), 2)\n if data1 is None:\n\
\ data = None\n else:\n data = int(\"\".join(str(bit) for bit\
\ in data1 + data2), 2)\n _v2_check_parity(fixed, data)\n return rolling,\
\ fixed, data\n\n\n_ORDER = {\n 0b0000: (0, 2, 1),\n 0b0001: (2, 0, 1),\n\
\ 0b0010: (0, 1, 2),\n 0b0100: (1, 2, 0),\n 0b0101: (1, 0, 2),\n \
\ 0b0110: (2, 1, 0),\n 0b1000: (1, 2, 0),\n 0b1001: (2, 1, 0),\n 0b1010:\
\ (0, 1, 2),\n}\n\n_INVERT = {\n 0b0000: (True, True, False),\n 0b0001:\
\ (False, True, False),\n 0b0010: (False, False, True),\n 0b0100: (True,\
\ True, True),\n 0b0101: (True, False, True),\n 0b0110: (False, True,\
\ True),\n 0b1000: (True, False, False),\n 0b1001: (False, False, False),\n\
\ 0b1010: (True, False, True),\n}\n\n\ndef _v2_scramble(indicator, parts):\n\
\ order = _ORDER[(indicator[0] << 3) | (indicator[1] << 2) | (indicator[2]\
\ << 1) | indicator[3]]\n invert = _INVERT[(indicator[4] << 3) | (indicator[5]\
\ << 2) | (indicator[6] << 1) | indicator[7]]\n\n parts_permuted = [parts[order[i]]\
\ for i in range(3)]\n for i in range(3):\n if invert[i]:\n \
\ parts_permuted[i] = [bit ^ 1 for bit in parts_permuted[i]]\n\n payload\
\ = []\n for i in range(len(parts_permuted[0])):\n payload += [parts_permuted[0][i],\
\ parts_permuted[1][i], parts_permuted[2][i]]\n return payload\n\n\ndef _v2_unscramble(indicator,\
\ payload):\n try:\n order = _ORDER[(indicator[0] << 3) | (indicator[1]\
\ << 2) | (indicator[2] << 1) | indicator[3]]\n invert = _INVERT[(indicator[4]\
\ << 3) | (indicator[5] << 2) | (indicator[6] << 1) | indicator[7]]\n except\
\ KeyError:\n raise ValueError(\"Illegal value for ternary bit\")\n\n\
\ parts_permuted = [payload[0::3], payload[1::3], payload[2::3]]\n for\
\ i in range(3):\n if invert[i]:\n parts_permuted[i] = [bit\
\ ^ 1 for bit in parts_permuted[i]]\n\n parts = [[], [], []]\n for i in\
\ range(3):\n parts[order[i]] = parts_permuted[i]\n\n return parts\n\
\n\ndef _encode_v2_half_parts(rolling, fixed, data):\n indicator = []\n \
\ for digit in rolling[:4]:\n indicator.append(digit >> 1)\n \
\ indicator.append(digit & 1)\n\n parts = [fixed[:10], fixed[10:], []]\n\
\ for digit in rolling[4:]:\n parts[2].append(digit >> 1)\n \
\ parts[2].append(digit & 1)\n\n if data is None:\n packet_type =\
\ 0\n if data is not None:\n packet_type = 1\n parts[0] +=\
\ data[:8]\n parts[1] += data[8:]\n for digit in rolling[:4]:\n\
\ parts[2].append(digit >> 1)\n parts[2].append(digit\
\ & 1)\n\n payload = _v2_scramble(indicator, parts)\n\n return packet_type,\
\ indicator, payload\n\n\ndef _decode_v2_half_parts(packet_type, indicator,\
\ payload):\n if packet_type == 0:\n payload_length = 30\n elif\
\ packet_type == 1:\n payload_length = 54\n elif packet_type == 2:\n\
\ raise ValueError(\"Unsupported packet type\")\n else:\n raise\
\ ValueError(\"Invalid packet type\")\n\n if len(payload) != payload_length:\n\
\ raise ValueError(\"Incorrect payload length\")\n\n parts = _v2_unscramble(indicator,\
\ payload)\n\n rolling = []\n for i in range(0, len(indicator), 2):\n\
\ rolling.append((indicator[i] << 1) | indicator[i+1])\n for i in\
\ range(0, len(parts[2]), 2):\n rolling.append((parts[2][i] << 1) | parts[2][i+1])\n\
\ if 3 in rolling:\n raise ValueError(\"Illegal value for ternary\
\ bit\")\n\n fixed = parts[0][:10] + parts[1][:10]\n\n if packet_type\
\ == 0:\n data = None\n elif packet_type == 1:\n if rolling[:4]\
\ != rolling[-4:]:\n raise ValueError(\"Last four ternary bits do\
\ not repeat first four\")\n rolling = rolling[:-4]\n data = parts[0][10:]\
\ + parts[1][10:]\n\n return rolling, fixed, data\n\n\ndef _v2_check_limits(rolling,\
\ fixed, data):\n if rolling >= 2**28:\n raise ValueError(\"Rolling\
\ code must be less than 2^28\")\n if fixed >= 2**40:\n raise ValueError(\"\
Fixed code must be less than 2^40\")\n if data is not None:\n if data\
\ >= 2**32:\n raise ValueError(\"Data must be less than 2^32\")\n\
\n\ndef _encode_v2_half(rolling, fixed, data):\n packet_type, indicator,\
\ payload = _encode_v2_half_parts(rolling, fixed, data)\n packet_type_bits\
\ = [packet_type >> 1, packet_type & 1]\n return packet_type_bits + indicator\
\ + payload\n\n\ndef encode_v2(rolling, fixed, data=None):\n \"\"\"Encode\
\ a Security+ 2.0 payload into 80 or 128 bits\n\n Arguments:\n rolling\
\ -- the rolling code (28 bits)\n fixed -- the fixed code (40 bits)\n \
\ data -- the data (32 bits, optional)\n\n Raises a ValueError if the rolling\
\ or fixed code is too large.\n \"\"\"\n\n _v2_check_limits(rolling, fixed,\
\ data)\n\n rolling1, rolling2 = _encode_v2_rolling(rolling)\n\n fixed_bits\
\ = [int(bit) for bit in f\"{fixed:040b}\"]\n fixed1 = fixed_bits[:20]\n\
\ fixed2 = fixed_bits[20:]\n\n if data is None:\n data1 = None\n\
\ data2 = None\n else:\n data = _v2_calc_parity(fixed, data)\n\
\ data_bits = [int(bit) for bit in f\"{data:032b}\"]\n data1 =\
\ data_bits[:16]\n data2 = data_bits[16:]\n\n return _encode_v2_half(rolling1,\
\ fixed1, data1) + _encode_v2_half(rolling2, fixed2, data2)\n\n\ndef _decode_v2_half(code):\n\
\ packet_type = (code[0] << 1) | code[1]\n indicator = code[2:10]\n \
\ payload = code[10:]\n return _decode_v2_half_parts(packet_type, indicator,\
\ payload)\n\n\ndef decode_v2(code):\n \"\"\"Decode a Security+ 2.0 transmission\
\ and return the rolling code, fixed\n code, and data.\n\n Arguments:\n\
\ code -- a list containing the 80 or 128 payload bits from a pair of packets\n\
\n Raises a ValueError if the payload bits are invalid for any reason.\n\
\ \"\"\"\n half_len = len(code) // 2\n rolling1, fixed1, data1 = _decode_v2_half(code[:half_len])\n\
\ rolling2, fixed2, data2 = _decode_v2_half(code[half_len:])\n return\
\ _v2_combine_halves(rolling1, rolling2, fixed1, fixed2, data1, data2)\n\n\n\
def _encode_wireline_half(rolling, fixed, data):\n _, indicator, payload\
\ = _encode_v2_half_parts(rolling, fixed, data)\n return indicator + [0,\
\ 0] + payload\n\n\ndef encode_wireline(rolling, fixed, data):\n \"\"\"Encode\
\ a Security+ 2.0 wireline payload into 19 bytes\n\n Arguments:\n rolling\
\ -- the rolling code (28 bits)\n fixed -- the fixed code (40 bits)\n \
\ data -- the data (32 bits)\n\n Raises a ValueError if the rolling code,\
\ fixed code, or data is too large.\n \"\"\"\n\n _v2_check_limits(rolling,\
\ fixed, data)\n\n rolling1, rolling2 = _encode_v2_rolling(rolling)\n\n \
\ fixed_bits = [int(bit) for bit in f\"{fixed:040b}\"]\n fixed1 = fixed_bits[:20]\n\
\ fixed2 = fixed_bits[20:]\n\n data = _v2_calc_parity(fixed, data)\n \
\ data_bits = [int(bit) for bit in f\"{data:032b}\"]\n data1 = data_bits[:16]\n\
\ data2 = data_bits[16:]\n\n payload_bits = _encode_wireline_half(rolling1,\
\ fixed1, data1) + _encode_wireline_half(rolling2, fixed2, data2)\n packet\
\ = [0x55, 0x01, 0x00]\n for n in range(len(payload_bits) // 8):\n \
\ byte = 0\n for bit in range(8):\n byte |= payload_bits[n\
\ * 8 + bit] << (7 - bit)\n packet.append(byte)\n return bytes(packet)\n\
\n\ndef _decode_wireline_half(code):\n if code[8:10] != [0, 0]:\n \
\ raise ValueError(\"Unexpected values for bits 8 and 9\")\n indicator =\
\ code[:8]\n payload = code[10:]\n return _decode_v2_half_parts(1, indicator,\
\ payload)\n\n\ndef decode_wireline(code):\n \"\"\"Decode a Security+ 2.0\
\ wireline transmission and return the rolling code,\n fixed code, and data.\n\
\n Arguments:\n code -- a bytes object with the 19 bytes of a serial packet\n\
\n Raises a ValueError if the payload bytes are invalid for any reason.\n\
\ \"\"\"\n if not isinstance(code, bytes):\n raise ValueError(\"\
Input must be bytes\")\n if len(code) != 19:\n raise ValueError(\"\
Input must be 19 bytes long\")\n if code[:3] != bytes([0x55, 0x01, 0x00]):\n\
\ raise ValueError(\"First three bytes must be 0x55, 0x01, 0x00\")\n\n\
\ code_bits = []\n for b in code[3:]:\n for bit in range(7, -1,\
\ -1):\n code_bits.append((b >> bit) & 1)\n\n half_len = len(code_bits)\
\ // 2\n rolling1, fixed1, data1 = _decode_wireline_half(code_bits[:half_len])\n\
\ rolling2, fixed2, data2 = _decode_wireline_half(code_bits[half_len:])\n\
\ return _v2_combine_halves(rolling1, rolling2, fixed1, fixed2, data1, data2)\n\
\n\n_OOK = {\n -1: [0, 0, 0, 0],\n 0: [0, 0, 0, 1],\n 1: [0, 0, 1,\
\ 1],\n 2: [0, 1, 1, 1]\n}\n\n\ndef encode_ook(rolling, fixed, fast=True):\n\
\ \"\"\"Encode a Security+ payload and produce an OOK stream for transmission\n\
\n The resulting pulses should be transmitted at a baud rate of 2000. That\
\ is,\n each 0 (off) or 1 (on) should be 0.5 ms long.\n\n Arguments:\n\
\ rolling -- the rolling code\n fixed -- the fixed code\n fast -- when\
\ True, shortens the time between packets\n \"\"\"\n\n code = encode(rolling,\
\ fixed)\n blank = [-1] * (10 if fast else 29)\n code = [0] + code[0:20]\
\ + blank + [2] + code[20:40] + blank\n ook_bits = []\n for symbol in\
\ code:\n ook_bits += _OOK[symbol]\n return ook_bits\n\n\ndef _manchester(code):\n\
\ output = []\n for bit in code:\n if bit == 0:\n output\
\ += [1, 0]\n else:\n output += [0, 1]\n return output\n\
\n\ndef encode_v2_manchester(rolling, fixed, data=None, fast=True):\n \"\"\
\"Encode a Security+ 2.0 payload and produce a Manchester stream for transmission\n\
\n The resulting pulses should be transmitted at a baud rate of 4000. That\
\ is,\n each 0 (off) or 1 (on) should be 0.25 ms long.\n\n Arguments:\n\
\ rolling -- the rolling code (28 bits)\n fixed -- the fixed code (40\
\ bits)\n data -- the data (32 bits, optional)\n fast -- when True, shortens\
\ the time between packets\n \"\"\"\n\n preamble = [0, 0, 0, 0, 0, 0,\
\ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]\n code = encode_v2(rolling, fixed,\
\ data)\n half_len = len(code) // 2\n packet1 = preamble + [0, 0] + code[:half_len]\n\
\ packet2 = preamble + [0, 1] + code[half_len:]\n blank = [0] * (33 if\
\ fast else 400 - 2 * len(packet1))\n\n return _manchester(packet1) + blank\
\ + _manchester(packet2) + blank\n\n\ndef pretty(rolling, fixed):\n \"\"\"\
Pretty-print a Security+ rolling and fixed code\"\"\"\n return f\"Security+:\
\ rolling={rolling} fixed={fixed} ({_fixed_pretty(fixed)})\"\n\n\ndef _fixed_pretty(fixed):\n\
\ switch_id = fixed % 3\n id0 = (fixed // 3) % 3\n id1 = (fixed //\
\ 3**2) % 3\n\n result = f\"id1={id1} id0={id0} switch={switch_id}\"\n\n\
\ if id1 == 0:\n pad_id = (fixed // 3**3) % (3**7)\n result\
\ += f\" pad_id={pad_id}\"\n pin = (fixed // 3**10) % (3**9)\n \
\ if 0 <= pin <= 9999:\n result += f\" pin={pin:04}\"\n elif\
\ 10000 <= pin <= 11029:\n result += \" pin=enter\"\n pin_suffix\
\ = (fixed // 3**19) % 3\n if pin_suffix == 1:\n result +=\
\ \"#\"\n elif pin_suffix == 2:\n result += \"*\"\n else:\n\
\ remote_id = (fixed // 3**3)\n result += f\" remote_id={remote_id}\"\
\n if switch_id == 0:\n button = \"middle\"\n elif\
\ switch_id == 1:\n button = \"left\"\n else:\n \
\ button = \"right\"\n result += f\" button={button}\"\n\n return\
\ result\n\n\ndef pretty_v2(rolling, fixed, data=None):\n \"\"\"Pretty-print\
\ a Security+ 2.0 rolling code, fixed code, and data\"\"\"\n pretty = f\"\
Security+ 2.0: rolling=0x{rolling:07x} fixed=0x{fixed:010x} ({_fixed_pretty_v2(fixed)})\"\
\n if data is not None:\n pretty += f\" data=0x{data:08x} ({_data_pretty_v2(fixed,\
\ data)})\"\n return pretty\n\n\ndef _fixed_pretty_v2(fixed):\n button\
\ = (fixed >> 32) & 0xf\n remote_id = fixed & 0xf0ffffffff\n return f\"\
button={button} remote_id=0x{remote_id:010x}\"\n\n\ndef _data_pretty_v2(fixed,\
\ data):\n button = (fixed >> 32) & 0xf\n\n byte1 = data >> 24\n byte2\
\ = (data >> 16) & 0xff\n pin = (byte2 << 8) | byte1\n\n tail = data &\
\ 0xfff\n\n if button == 3:\n return f\"pin=enter tail=0x{tail:03x}\"\
\n else:\n if button == 1:\n suffix = \"*\"\n elif\
\ button == 2:\n suffix = \"#\"\n else:\n suffix\
\ = \"\"\n\n return f\"pin={pin:04}{suffix} tail=0x{tail:03x}\"\n\n\n\
_WIRELINE_COMMANDS = {\n # sent by opener\n 0x081: \"status\",\n 0x084:\
\ \"unknown_1\",\n 0x085: \"unknown_2\",\n 0x0a1: \"pair_3_resp\",\n \
\ 0x284: \"motor_on\",\n 0x393: \"learn_3_resp\",\n 0x401: \"pair_2_resp\"\
,\n 0x48c: \"openings\",\n\n # sent by switch\n 0x080: \"get_status\"\
,\n 0x0a0: \"pair_3\",\n 0x181: \"learn_2\",\n 0x18c: \"lock\",\n \
\ 0x280: \"open\",\n 0x281: \"light\",\n 0x285: \"motion\",\n 0x391:\
\ \"learn_1\",\n 0x392: \"learn_3\",\n 0x400: \"pair_2\",\n 0x48b:\
\ \"get_openings\",\n}\n\n_DOOR_STATUS = {\n 1: \"open\",\n 2: \"closed\"\
,\n 3: \"stopped\",\n 4: \"opening\",\n 5: \"closing\",\n}\n\n\ndef\
\ pretty_wireline(rolling, fixed, data):\n cmd = ((fixed >> 24) & 0xf00)\
\ | (data & 0xff)\n command_name = _WIRELINE_COMMANDS.get(cmd, \"<unknown>\"\
)\n\n nibble = (data >> 8) & 0xf\n byte1 = (data >> 16) & 0xff\n byte2\
\ = (data >> 24) & 0xff\n\n msg = \"\"\n if cmd == 0x080:\n msg\
\ = f\"byte2={byte2}\"\n elif cmd == 0x081:\n door = _DOOR_STATUS.get(nibble,\
\ \"<unknown>\")\n learn = (byte2 >> 5) & 1\n unk1 = (byte2 >>\
\ 4) & 1\n unk2 = (byte2 >> 2) & 1\n light = (byte2 >> 1) & 1\n\
\ lock = byte2 & 1\n blocked = (byte1 >> 6) & 1\n unk3\
\ = (byte1 >> 5) & 1\n msg = f\"door={door} learn={learn} light={light}\
\ lock={lock} blocked={blocked^1} unk1={unk1} unk2={unk2} unk3={unk3}\"\n \
\ elif cmd == 0x0a1:\n msg = f\"byte1={byte1}\"\n elif cmd == 0x181:\n\
\ msg = f\"nibble={nibble}\"\n elif cmd == 0x280:\n msg = \"\
pressed\" if byte1 == 1 else \"released\"\n elif cmd == 0x391:\n msg\
\ = f\"nibble={nibble}\"\n elif cmd == 0x393:\n msg = f\"nibble={nibble}\"\
\n elif cmd == 0x48c:\n openings = (byte1 << 8) | byte2\n msg\
\ = f\"number={openings}\"\n\n return f\"rolling=0x{rolling:07x} fixed=0x{fixed:010x}\
\ data=0x{data:08x} cmd=0x{cmd:03x} {command_name} {msg}\".strip()\n"
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [8, 92.0]
rotation: 0
state: enabled
- name: single_pole_iir_filter_xx_0
id: single_pole_iir_filter_xx
parameters:
affinity: ''
alias: ''
alpha: '0.1'
comment: ''
maxoutbuf: '0'
minoutbuf: '0'
type: complex
vlen: '1'
states:
bus_sink: false
bus_source: false
bus_structure: null
coordinate: [568, 172.0]
rotation: 0
state: enabled
connections:
- [analog_sig_source_x_0, '0', blocks_multiply_xx_0, '1']
- [blocks_multiply_xx_0, '0', osmosdr_sink_0, '0']
- [blocks_repeat_0, '0', single_pole_iir_filter_xx_0, '0']
- [blocks_vector_source_x_0, '0', blocks_repeat_0, '0']
- [single_pole_iir_filter_xx_0, '0', blocks_multiply_xx_0, '0']
metadata:
file_format: 1
grc_version: 3.10.9.2