-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudioReceiver.py
1303 lines (1254 loc) · 79.8 KB
/
audioReceiver.py
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import numpy as np
from scipy.fft import rfft
import audioSettings
import queue
import configuration
import math
from scipy import signal
import threading
import logging
### import time
from dataclasses import dataclass
from bitarray import bitarray
from bitarray.util import ba2int
from cryptography.hazmat.primitives.asymmetric import x25519
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import copy # from copy import deepcopy
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes
from timeit import default_timer as cProfileTimer
import random
''''
This module implements the right side of this drawing:
wire connections:
(TX in) Voice -> BAND_STOP -> (+) -> (TX out) -> Channel -> (RX in) .-> BAND_STOP -> Voice (RX out)
^ |
| |
Code -> BAND_PASS ----- -> BAND_PASS -> Code
FFTs:
___ ___ ________ ________ ___ ___
|_| -> (+) -> | | ... -> | | ---- > |_|
^ |
__ | | __
___| |___ ----- --> ___| |___
'''
##############################################
# NOTE: about transition bands in Filters
# Filters with sharp frequency cutoffs can produce outputs that ring for a long
# time when they operate on signals with frequency content in the transition band.
# In general, therefore, the wider a transition band that can be tolerated,
# the better behaved the filter will be in the time domain.
##############################################
# settings "band pass filter" to recover CODE
# WARNING: BPF only to hear and/or plot CODE-Frequencies but NOT for decoding!
#################################################
BPF_LEFT_MARGIN = 400
BPF_RIGHT_MARGIN = 400
BPF_ORDER = 7
BPF_MAX_RIPPLE = 0.1
BPF_ELL_MIN_ATTENUATION = 145.0
# settings "band stop filter" to recover VOICE / Audio transmitted by the other side
BSF_LEFT_MARGIN = 400
BSF_RIGHT_MARGIN = 400
BSF_ORDER = 7
BSF_MAX_RIPPLE = 0.1
# settings "elliptic filter"
BSF_ELL_MIN_ATTENUATION = 145.0
# settings for "notsch filter" to remove "carrier" if configured
# Quality factor
Q = 0.3 # 0.3 # 3.0
# for undistorting voice
# TODO: investiage why 920Hz can be suppressed so well but other frequencies not..
f0 = 920.0 # Modulating frequency to be removed from distorted signal (Hz) - see NOTCH filter in audioTransmitter
# definitions for decoding state machine
SEARCH_PREAMBLE = 0
SEARCH_START = 1
DECODE_FRAME = 2
# definitions for decoding telegram fields (= sub-state of DECODE_FRAME)
DECODE_ADDRESS = 0
DECODE_SEQ_NR = 1
DECODE_SEQ_NR_ACK = 2
DECODE_COMMAND = 3
DECODE_DATA_LEN = 4
DECODE_DATA = 5
DECODE_END = 6
DECODE_CHECKSUM = 7
# take also ONE byte from PREAMBLE to avoid detecting START by coincidentially/random correct value
# we need to do this because otherwise we shall know exactly where the preamble finishes...but that is exactly what we want to find out!
LAST_PREAMBLE_BYTE_AND_START_BITS = bitarray([True,True,True,True,True,True,True,True,False,True,False,True,False,True,False,True]) # = b"\xFF\x55"
START_BITS = bitarray([False,True,False,True,False,True,False,True]) # = b"\x55"
END_BITS = bitarray([True,False,True,False,True,False,True,False]) # = b"\xAA"
# definitions for reception state
IDLE = 0
CALL_ACCEPTED = 1
KEY_START_RECEIVED = 2
KEY_END_RECEIVED = 3
class AudioReceiver():
# protocol
seqNrAckRx = [0] # reference to sequence number ACK from transmitter (correctly received)
seqNrAck = [0] # reference to sequence number for ACK
seqNrTx = [0] # reference to TX seqNr
# cipher object containing key and decryptor
cipher = [None]
peer_public_key_start = None # helper variable to pass 1st part of our public key
peer_public_key_end = None # helper variable to pass 2nd part of our public key
# out-band-verification of key-exchange using a session-code,
# which is an integer number derived from the common session key,
# which in turn results from both public keys.
session_code = ""
# flags
stream_on = [False]
transmit_on_ref = None # reference to flag for half-duplex communication
receive_on_ref = None # reference to flag for half-duplex communication
ack_received = [False, 0] # reference to flag for ACK received
send_ack = [False] # reference to flag for send ACK
call = False
call_accepted = False
call_rejected = False
call_end = False
key_start_received = False
key_end_received = False
startup_data_received = False
comm_token = [255]
have_token = True
# reception state
rx_state = IDLE
# constant definitions which depend on configuration settings
# constants for filters
BPF_F1 = audioSettings.CODE_SINE_FREQUENCY_ONE - BPF_LEFT_MARGIN
BPF_F2 = audioSettings.CODE_SINE_FREQUENCY_ZERO + BPF_RIGHT_MARGIN
BSF_F1 = audioSettings.CODE_SINE_FREQUENCY_ONE - BSF_LEFT_MARGIN
BSF_F2 = audioSettings.CODE_SINE_FREQUENCY_ZERO + BSF_RIGHT_MARGIN
# constants for detection algorithm
BIG_SCAN_ROUNDS = 4
BIG_STEP = audioSettings.LEN_BIT_ONE//BIG_SCAN_ROUNDS
SMALL_SCAN_ROUNDS = (audioSettings.LEN_BIT_ONE//BIG_SCAN_ROUNDS)*2
SMALL_STEP = 1
# state parse and decode
parse_state = SEARCH_PREAMBLE
decode_state = DECODE_ADDRESS
# performance statistics
avg_rx_time_ms = 0.0
time_old = 0.0
avg_in_amplitude_percent = 0
# communication statistics
telRxOk = 0
telRxNok = 0
# queues
qplot = queue.Queue() # to update matplotlib plot in "main loop"
# NOTE: for some reason, qin works better than using a circular buffer as we do in audioTransmitter
# here we don't have "bursts" of chunks that want to be put into buffer all at once as in the case of transmission
qin = queue.Queue() # audio data from RX in -> to be decoded AND to be forwarded to matplot over qplot
# input messages to be read e.g. by chat in GUI
inMessageQueue = queue.Queue()
inCommStatusQueue = queue.Queue()
# timer for setting receive_on_ref
receive_on_timer_event = threading.Event()
# TODO: better module variable?
telegram_bits = None
telegram_bits_start_pos = 0
telegram_bits_end_pos = 0
# filter BAND-PASS
# WARNING: BPF only to hear and/or plot CODE-Frequencies but NOT for decoding!
#################################################
sos_bandpass = None
z = None
# filter BAND-STOP
sos_bandstop = None
zBandStop = None
# NOTCH filter
sos_notch = None
zNotch = None
# helper variable
bit_prev = None
# definition and variable used to recover cut-bits between audio-chunks or parts of size N of audio-chunk
PREVIOUS_SAMPLES = 0
# samples from last round (or previous call) containing enough info to hold a complete "cut" PREAMBLE-LAST-BYTE + START marker (in general the used nr. of samples will be lower)
data_prev = None
@dataclass
class StartupDataClass:
comm_partner: str
startup_data = StartupDataClass("")
@dataclass
class TelegramClass:
address: int # int8
seqNr: int
seqNrAck: int
command: int # int8
data_length: int # int8
data: bytearray # [audioSettings.DATA_MAX_LEN_BYTES]
end: int # int8
checksum: int # int8 # calculated on bytes from START to last byte of DATA
decodedDataBytes: int # int8
seqNrRepeated: bool
telegram = TelegramClass(0,0,0,0,0,bytearray(audioSettings.DATA_MAX_LEN_BYTES),0,0,0,False)
data_part = bytearray(audioSettings.MAX_TEXT_LEN)
part_end_idx = 0
def __init__(self, glob_vars):
self.stream_on = glob_vars[0].stream_on
self.ack_received = glob_vars[0].ack_received
self.send_ack = glob_vars[0].send_ack
self.transmit_on_ref = glob_vars[0].transmit_on_ref
self.receive_on_ref = glob_vars[0].receive_on_ref
self.seqNrAck = glob_vars[0].seqNrAck
self.seqNrAckRx = glob_vars[0].seqNrAckRx
self.seqNrTx = glob_vars[0].seqNrTx
self.private_key = glob_vars[0].private_key
self.cipher = glob_vars[0].cipher
self.comm_token = glob_vars[0].comm_token
self.comm_token[0] = random.randint(0, 255)
self.have_token = True # assume for now we have the token
self.session_code = ""
self.peer_public_key_start = bytearray(0)
self.telRxOk = 0
self.telRxNok = 0
self.rx_state = IDLE
self.PREVIOUS_SAMPLES = audioSettings.START_LEN_SAMPLES + 8*audioSettings.LEN_BIT_ONE
self.data_prev = np.array([0.0]*(audioSettings.N + self.PREVIOUS_SAMPLES))
# TODO: better module variable?
self.telegram_bits = bitarray(audioSettings.TELEGRAM_MAX_LEN_BITS)
# filter BAND-PASS
# WARNING: BPF only to hear and/or plot CODE-Frequencies but NOT for decoding!
#################################################
self.sos_bandpass = signal.ellip(BPF_ORDER, BPF_MAX_RIPPLE, BPF_ELL_MIN_ATTENUATION,
# IMPORTANT: we need to divice by Nyquist frequency or pass fs as argument...one thing or the other..
# [BPF_F1 / audioSettings.NYQUIST_FREQUENCY, BPF_F2 / audioSettings.NYQUIST_FREQUENCY],'bandpass', analog=False, output='sos')
[self.BPF_F1, self.BPF_F2],'bandpass', analog=False, fs=audioSettings.SAMPLING_FREQUENCY, output='sos')
# TODO: check if detection can be improved by using a HIGH-PASS-FILTER instead:
# BPF_F1,'highpass', analog=False, fs=audioSettings.SAMPLING_FREQUENCY, output='sos')
# IMPORTANT: we need this TRICK to filter audio signal "in chunks":
self.z = np.zeros((self.sos_bandpass.shape[0], 2))
# filter BAND-STOP
self.sos_bandstop = signal.ellip(BSF_ORDER, BSF_MAX_RIPPLE, BSF_ELL_MIN_ATTENUATION,
# IMPORTANT: we need to divice by Nyquist frequency or pass fs as argument...one thing or the other..
# [BPF_F1 / audioSettings.NYQUIST_FREQUENCY, BPF_F2 / audioSettings.NYQUIST_FREQUENCY],'bandpass', analog=False, output='sos')
[self.BSF_F1, self.BSF_F2],'bandstop', analog=False, fs=audioSettings.SAMPLING_FREQUENCY, output='sos')
# IMPORTANT: we need this TRICK to filter audio signal "in chunks":
self.zBandStop = np.zeros((self.sos_bandstop.shape[0], 2))
# flag timer thread for half-duplex communication
receive_on_timer_thread = threading.Thread(target=self.thread_receive_on_timer, args=(1,))
receive_on_timer_thread.start()
# NOTCH filter
# TODO: in case "both" sides need a carrier then the frequencies need to be different, therefore we need a new definition different to CARRIER_FREQUENCY_HZ
b, a = signal.iirnotch(audioSettings.CARRIER_FREQUENCY_HZ, Q, fs=audioSettings.SAMPLING_FREQUENCY) # , output='sos') # cannot return sos for some reason..
# IMPORTANT: we need this TRICK to filter audio signal "in chunks":
Z, P, K = signal.tf2zpk(b, a)
self.sos_notch = signal.zpk2sos(Z, P, K)
self.zNotch = np.zeros((self.sos_notch.shape[0], 2))
# start decoder thread
decode = threading.Thread(target=self.thread_decode, args=(1,))
decode.start()
# status
self.inCommStatusQueue.put("") # ("RX:")
################################################################
# TODO: cannot invert if we are NOT exactly synchronized, right? we have different starts for currSample in RX
# investigate and correct this mess...use or implement something better..
currSample = 0
def undistortFunction(self, x):
# need to revert: ret = x*math.sin(2 * np.pi *f0*self.currSample/audioSettings.SAMPLING_FREQUENCY)*2
# ret = x * sin (F * sample) * 2
# TEST3 below is ==> ret = x / (2 * sin(F * sample))
#############################################################
# TEST1
#####
# ret= math.asin(x) * (2 * np.pi *f0*self.currSample/audioSettings.SAMPLING_FREQUENCY) * 2
# ret = ret/audioSettings.N
#####
# TEST2
#####
# if self.currSample == 0:
# ret = 0
# else:
# ret= math.asin(x/2) / (2 * np.pi *f0*self.currSample/audioSettings.SAMPLING_FREQUENCY)
# ret = ret/audioSettings.N
#####
# TEST3
#####
if math.sin(2 * np.pi *f0*self.currSample/audioSettings.SAMPLING_FREQUENCY) == 0:
ret = 0
else:
ret = x / (2*math.sin(2 * np.pi *f0*self.currSample/audioSettings.SAMPLING_FREQUENCY))
self.currSample += 1
return ret
undistort = np.vectorize(undistortFunction, otypes=[float])
##############################################################
def callback_wire_in(self, indata, outdata, frames, time, status):
# store time between callbacks in ms
self.avg_rx_time_ms = (float(time.currentTime) - self.time_old)*1000.0
self.time_old = float(time.currentTime)
if status:
logging.error("wire_in: "+str(status))
# filter audio output (RX out)
#################
if configuration.OUT_RX_HEAR_VOICE:
# TODO: Alternatives 1 and 2 should be the same..but they are not...which one is correct?
##################################################
# ALTERNATIVE 1: first undistort, then fiter
############
# UN-DISTORT voice
# if configuration.IN_RX_UNDISTORT:
# outdata[:frames, audioSettings.DEFAULT_CHANNEL] = self.undistort(self, indata[:frames, audioSettings.DEFAULT_CHANNEL])
# RX in -> BAND_STOP -> Voice
# outdata[:, audioSettings.DEFAULT_CHANNEL], self.zBandStop = signal.sosfilt(self.sos_bandstop, outdata[:, audioSettings.DEFAULT_CHANNEL], zi=self.zBandStop)
# else:
# RX in -> BAND_STOP -> Voice
# outdata[:, audioSettings.DEFAULT_CHANNEL], self.zBandStop = signal.sosfilt(self.sos_bandstop, indata[:, audioSettings.DEFAULT_CHANNEL], zi=self.zBandStop)
# ALTERNATIVE 2: first filter, then undistort
############
# RX in -> BAND_STOP -> Voice
outdata[:, audioSettings.DEFAULT_CHANNEL], self.zBandStop = signal.sosfilt(self.sos_bandstop, indata[:, audioSettings.DEFAULT_CHANNEL], zi=self.zBandStop)
# remove RX carrier
if audioSettings.REMOVE_RX_CARRIER:
outdata[:, audioSettings.DEFAULT_CHANNEL], self.zNotch = signal.sosfilt(self.sos_notch, outdata[:, audioSettings.DEFAULT_CHANNEL], zi=self.zNotch)
# UN-DISTORT voice
if configuration.IN_RX_UNDISTORT:
outdata[:frames, audioSettings.DEFAULT_CHANNEL] = self.undistort(self, outdata[:frames, audioSettings.DEFAULT_CHANNEL])
else:
# filter coding-range (remove left and right frequencies with Voice content)
# RX in -> BAND_PASS -> Code
outdata[:, audioSettings.DEFAULT_CHANNEL], self.z = signal.sosfilt(self.sos_bandpass, indata[:, audioSettings.DEFAULT_CHANNEL], zi=self.z)
# pass input audio to decoder
################
### if self.transmit_on_ref[0] == False: # half-duplex communication
# this is a BLOCKING call
self.qin.put(indata[:, audioSettings.DEFAULT_CHANNEL])
def callback_rx_in(self, indata, frames, time, status):
# TODO: BUT: time is always zero, why? HW-Bug???
##############################
# store time between callbacks in ms
self.avg_rx_time_ms = (float(time.currentTime) - self.time_old)*1000.0
self.time_old = float(time.currentTime)
if status:
logging.error("rx_in: "+str(status))
# pass input audio to decoder
################
### if self.transmit_on_ref[0] == False: # half-duplex communication
# this is a BLOCKING call
self.qin.put(indata[:, audioSettings.DEFAULT_CHANNEL])
# If START is correctly detected, then
# return sample position of field START (*** WARNING: this is RELATIVE to argument sample_buffer but the caller may pass another buffer with a different buffer offset !!!).
# If available, telegram contents starting and including ADDRESS,etc. are already copied to permanent bitarray buffer.
# NOTE: the algorithm is:
# rough-scan to detect "best" bit-gap in marker = START, coding with threshold between FFT(one) and FFT(zero), but first we find marker = last-PREAMBLE-byte followed by START
# fine-scan to detect START at approx. position found in rough-scan (moving left to right), considering best result given by "maximum minimum gap" between FFT(one) and FFT(zero) in any START-bit.
# Markers (PRE+START for rough scan and START for fine scan) shall be checked every time, otherwise we may be considering good gaps which belong to shifted and incorrect samples!
# Note that all bits in buffer will only be used in the especial case where START is found at fine-scan-step == 0, otherwise we always discard the last bit, because due to the offset it is "cut".
# In that case we will have some rest_samples which will be joined togeher with samples input to putInBitArrayBuffer() in the following call, in order to restore the "cut-bit".
'''
Example with bit-width = 40 samples
rough-scan-step = 10
fine-scan-step = 1
---------------------
| | | | |
---------------------
10 20 30 (3 rough steps,
best found in 30)
^ ^
| |
\________/
(20 fine scans around 30,
between 20 and 40,
best found in 33)
# '''
################################################################################################################
def getStartSamplePosition(self, sample_buffer):
# return value (default is error)
startSamplePosition = -1
# gap variables
max_min_rough_gap = 0
startSamplePositionRough = 0
# bit array
# we subtract one bit because in 39 out of 40 cases we will always have an offset and therefore have a cut-bit which needs to be discarded
# in the case where offset = 0, that is, where PRE+START are found at sample zero, we shall consider the last bit again
# but that is no longer relevant for bits[] becuase in that case what matters is tel_bits[], and its bits are re-calculated based e.g. on len(sample_buffer),
# therefore, not losing any bit.
NR_OF_BITS = int(len(sample_buffer)/audioSettings.LEN_BIT_ONE) - 1
# TODO: pre-allocate memory and set to zero here if necessary
bits = bitarray(NR_OF_BITS)
# search marker = last PREAMBLE byte followed by START in rough scan over complete buffer
# PRE+START shall be detectable already in at least one of these "rough" steps
############################################
for offset in range(self.BIG_STEP, self.BIG_SCAN_ROUNDS*self.BIG_STEP, self.BIG_STEP):
logging.debug("Rough scan on PRE+START with offset = " + str(offset) + " on nr. of bits = " + str(NR_OF_BITS))
# scan complete bit-stream with offset (rough scan)
# coding criteria: maximum of FFT(FREQ_ONE) and FFT(FREQ_ZERO)
#########################################
for i in range(NR_OF_BITS):
ffty = rfft(sample_buffer[offset + i*audioSettings.LEN_BIT_ONE:offset + (i+1)*audioSettings.LEN_BIT_ONE])
absfft = 2.0 * abs(ffty[:audioSettings.LEN_BIT_ONE//2])/audioSettings.LEN_BIT_ONE
# code bit according to FFT threshold
if absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] > absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE]:
bits[i] = True
else:
bits[i] = False
logging.debug("Bit stream in buffer with offset = " + str(offset))
logging.debug(bits)
# search start of frame by detecting pattern/marker in bits = last PREAMBLE-byte followed by START
iter = bits.itersearch(LAST_PREAMBLE_BYTE_AND_START_BITS)
startPosition = -1
for markerPosition in iter:
startPosition = markerPosition + 8 # +8 because we also searched for last byte of preamble..
# found PRE+START in complete bit-stream?
#########################
if startPosition != -1:
# translate startPosition to absolute samples coordinate system:
startSamplePositionRoughTemp = startPosition*audioSettings.LEN_BIT_ONE + offset
min_rough_gap = 999999
# TODO: average all gaps instead?
# determine GAPs of START considering offset (rough step)
##################################
for i in range(audioSettings.START_LEN_BYTES*8):
ffty = rfft(sample_buffer[startSamplePositionRoughTemp + i*audioSettings.LEN_BIT_ONE:startSamplePositionRoughTemp + (i + 1)*audioSettings.LEN_BIT_ONE])
absfft = 2.0 * abs(ffty[:audioSettings.LEN_BIT_ONE//2])/audioSettings.LEN_BIT_ONE
diff_rough = abs(absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] - absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE])
# we search for minimum gap between ONE and ZERO in each of the bits in START
if diff_rough < min_rough_gap:
min_rough_gap = diff_rough
# we search for maximum min_gap in BIG_SCAN_ROUNDS
if min_rough_gap > max_min_rough_gap:
max_min_rough_gap = min_rough_gap
startSamplePositionRough = startSamplePositionRoughTemp
# found best PRE+START in rough scan?
######################
if (offset == (self.BIG_SCAN_ROUNDS - 1)*self.BIG_STEP):
if max_min_rough_gap != 0:
logging.debug("Best START at best worst-case rough_gap = " + str(max_min_rough_gap))
logging.debug(" startSamplePositionRough = " + str(startSamplePositionRough))
# gap
max_min_gap = 0
diff = 0
# bitarray
# TODO: pre-allocate memory instead..
bits_start = bitarray(audioSettings.START_LEN_BYTES*8)
# fine scan on detected START with an accuracy given by SMALL_SCAN_ROUNDS
# we scan left and right of startSamplePositionRough.
# (from here onwards we don't care about PREAMBLE anymore)
for m in range(startSamplePositionRough - self.SMALL_SCAN_ROUNDS//2, startSamplePositionRough + self.SMALL_SCAN_ROUNDS//2, self.SMALL_STEP):
min_gap = 999999
# TODO: average all gaps instead?
# fine scan bits of START considerung:
# offset (rough step) and m (small step)
# coding criteria: maximum of minimum gaps between FFT(FREQ_ONE) and FFT(FREQ_ZERO)
#####################################################
for i in range(audioSettings.START_LEN_BYTES*8):
ffty = rfft(sample_buffer[m + i*audioSettings.LEN_BIT_ONE:m + (i + 1)*audioSettings.LEN_BIT_ONE])
absfft = 2.0 * abs(ffty[:audioSettings.LEN_BIT_ONE//2])/audioSettings.LEN_BIT_ONE
# code bit according to FFT threshold
if absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] > absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE]:
bits_start[i] = True
else:
bits_start[i] = False
# gaps
diff = abs(absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] - absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE])
# we search for minimum gap between ONE and ZERO in each of the bits in START
if diff < min_gap:
min_gap = diff
logging.debug("Bit stream in approx. START with fine scan step sample = " + str(m))
logging.debug(bits_start)
# search start of frame by detecting pattern/marker in bits = last PREAMBLE-byte followed by START
iter = bits_start.itersearch(START_BITS)
startPosition = -1
for markerPosition in iter:
startPosition = markerPosition
# found START marker?
if startPosition == 0:
# we search for maximum min_gap in SMALL_SCAN_ROUNDS
if min_gap > max_min_gap:
max_min_gap = min_gap
# return value
startSamplePosition = m
# found best START in fine scan?
###################
if m == startSamplePositionRough + self.SMALL_SCAN_ROUNDS//2 - self.SMALL_STEP:
if max_min_gap != 0:
# update RX volume for visualization
# RX volume based on signal coding START which contains both ones and zeros in the same amount
self.updateRxVolume(sample_buffer[startSamplePosition:startSamplePosition + audioSettings.START_LEN_SAMPLES])
# log
logging.debug("Best START at best worst-case fine gap = " + str(max_min_gap))
logging.debug(" bit start position fine = " + str(startPosition))
logging.debug(" startSamplePosition fine = " + str(startSamplePosition))
# calculate telegram bits using best result, beginning with ADDRESS
#######################################
sample_pos_address = startSamplePosition + audioSettings.START_LEN_SAMPLES
#######################################
#######################################
# WORKAROUND: we need to add 3 to sample_pos_address
# TODO: find out why we need this
# TODO: find out if we are losing/discarding the last bit in the stream (?)
sample_pos_address = sample_pos_address + 3
#######################################
#######################################
# final values
########
rest_samples = (len(sample_buffer) - sample_pos_address)%audioSettings.LEN_BIT_ONE
BITS_FROM_ADDRESS = (len(sample_buffer) - sample_pos_address - rest_samples)//audioSettings.LEN_BIT_ONE
########################################################
# WORKAROND not working: sometimes we obtain BITS_FROM_ADDRESS = -1 so we set it to zero in such case
# TODO: see why this happens
if BITS_FROM_ADDRESS < 0:
logging.error("ERROR: BITS_FROM_ADDRESS = " + str(BITS_FROM_ADDRESS)) # + ", we set it to zero.")
### BITS_FROM_ADDRESS = 0
#######
return -1
#######
########################################################
logging.debug("sample_pos_address = "+str(sample_pos_address))
logging.debug("nr. of rest samples = "+str(rest_samples))
logging.debug("BITS_FROM_ADDRESS = "+str(BITS_FROM_ADDRESS))
# allocate memory for tel_bits
# TODO: pre-allocate fix-memory instead and set to zero here if necessary
tel_bits = bitarray(BITS_FROM_ADDRESS)
# final scan with BEST found position
for i in range(BITS_FROM_ADDRESS):
ffty = rfft(sample_buffer[sample_pos_address + i*audioSettings.LEN_BIT_ONE:sample_pos_address + (i+1)*audioSettings.LEN_BIT_ONE])
absfft = 2.0 * abs(ffty[:audioSettings.LEN_BIT_ONE//2])/audioSettings.LEN_BIT_ONE
# code bit according to FFT threshold
if absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] > absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE]:
# now that we stopped scanning with offsets which may lead to insufficient signal-levels we need to check if we actually have a strong enough signal
if absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] > audioSettings.FFT_DETECTION_LEVEL:
tel_bits[i] = True
else:
logging.error("ERROR: START not found, bit ONE too weak with level = " + str(absfft[audioSettings.BIN_FREQUENCY_ONE_FINE]))
# return with error
return -1
else:
# now that we stopped scanning with offsets which may lead to insufficient signal-levels we need to check if we actually have a strong enough signal
if absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE] > audioSettings.FFT_DETECTION_LEVEL:
tel_bits[i] = False
else:
logging.error("ERROR: START not found, bit ZERO too weak with level = " + str(absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE]))
# return with error
return -1
# init variable
self.telegram.decodedDataBytes = 0
# store bits of telegram part
self.telegram_bits_start_pos = 0
self.telegram_bits_end_pos = BITS_FROM_ADDRESS # which is = len(tel_bits)
self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_end_pos] = tel_bits[:]
# store last samples
'''
If fine_step is NOT zero, then we definitely have a "cut-bit" at the end of the buffer.
Example of a ZERO cut-bit:
rest_samples (17) first_samples (23)
| _---_ | _---_ |
| / \ | / \ |
| / \ / \ /|
| \_ _/ \_ _/ |
| - | -.- |
# '''
# TODO: avoid this memory allocation and set to zero here if necessary
self.bit_prev = np.array([0.0]*(rest_samples))
self.bit_prev[0:rest_samples] = sample_buffer[len(sample_buffer) - rest_samples:]
logging.debug("telegram_bits:")
logging.debug(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_end_pos])
# START detected successfully!
# set half-duplex flag
if self.receive_on_ref[0]==False:
self.receive_on_timer_event.set()
else:
logging.error("ERROR: START not found in fine scan.")
else:
logging.error("ERROR: START not found in rough scan.")
# return of getStartSamplePosition()
return startSamplePosition
def putInBitArrayBuffer(self, sample_buffer):
# calculate telegram bits using offset determined by len(self.bit_prev) (previous rest_samples)
startSamplePosition = audioSettings.LEN_BIT_ONE - len(self.bit_prev)
rest_samples = (len(sample_buffer) - startSamplePosition)%audioSettings.LEN_BIT_ONE
BITS_FROM_TEL_PART = (len(sample_buffer) - startSamplePosition - rest_samples)//audioSettings.LEN_BIT_ONE
tel_bits = bitarray(BITS_FROM_TEL_PART)
logging.debug("*** putInBitArrayBuffer():")
logging.debug("startSamplePosition = "+str(startSamplePosition))
logging.debug("nr. of rest samples = "+str(rest_samples))
logging.debug("BITS_FROM_TEL_PART = "+str(BITS_FROM_TEL_PART))
# scan with found position
for i in range(BITS_FROM_TEL_PART):
ffty = rfft(sample_buffer[startSamplePosition + i*audioSettings.LEN_BIT_ONE:startSamplePosition + (i+1)*audioSettings.LEN_BIT_ONE])
absfft = 2.0 * abs(ffty[:audioSettings.LEN_BIT_ONE//2])/audioSettings.LEN_BIT_ONE
# code bit according to FFT threshold
# TODO: if we knew that this is a valid bit inside a "telegram-byte" we shall always check if we have a strong enough signal using FFT_DETECTION_LEVEL.
# Because we don't have that information (we should NOT have it at this "abstraction level"?) then we don't check that.
# We may have some "noise" after the telegram, which is also decoded...just to be discarded by the telegram-decoder afterwards.
if absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] > absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE]:
tel_bits[i] = True
else:
tel_bits[i] = False
# management of cut-bits
##############
'''
If startSamplePosition is NOT zero, then we have a "cut-bit" at start and end of buffer.
Example of a ZERO cut-bit:
rest_samples (17) first_samples (23)
| _---_ | _---_ |
| / \ | / \ |
| / \ / \ /|
| \_ _/ \_ _/ |
| - | -.- |
# '''
# recover cut-bit
##########
if startSamplePosition > 0: # this is the same as: if len(self.bit_prev) > 0:
complete_bit_samples = np.append(self.bit_prev, sample_buffer[:startSamplePosition])
ffty = rfft(complete_bit_samples)
absfft = 2.0 * abs(ffty[:audioSettings.LEN_BIT_ONE//2])/audioSettings.LEN_BIT_ONE
# code bit according to FFT threshold
bit = False
if absfft[audioSettings.BIN_FREQUENCY_ONE_FINE] > absfft[audioSettings.BIN_FREQUENCY_ZERO_FINE]:
bit = True
logging.debug("cut-bit:")
logging.debug(bit)
# copy cut-bit to telegram_bits
##################
self.telegram_bits[self.telegram_bits_end_pos:self.telegram_bits_end_pos+1] = bit
self.telegram_bits_end_pos += 1
# now add tel_bits to telegram_bits
#####################
self.telegram_bits[self.telegram_bits_end_pos:self.telegram_bits_end_pos+len(tel_bits)] = tel_bits[:]
self.telegram_bits_end_pos += len(tel_bits)
# store rest samples
############
# TODO: avoid this memory allocation and set to zero instead if required
self.bit_prev = np.array([0.0]*(rest_samples))
self.bit_prev[0:rest_samples] = sample_buffer[len(sample_buffer) - rest_samples:]
logging.debug("telegram_bits including cut-bit and next part:")
logging.debug(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_end_pos])
def decodeTelegram(self):
while (self.telegram_bits_end_pos - self.telegram_bits_start_pos) >= 8:
if self.decode_state == DECODE_ADDRESS:
self.telegram.address = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.START_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.START_LEN_BYTES*8
logging.info("ADDRESS = "+str(self.telegram.address))
# check address
# TODO: remove this hard-coded check...
if self.telegram.address == 1:
self.decode_state = DECODE_SEQ_NR
else:
# ADDRESS ERROR: we just go back to PREAMBLE search state
# the transmitter will re-send on timeout
self.parse_state = SEARCH_PREAMBLE
# WARNING: always reset sub-state when going back to SEARCH_PREAMBLE
self.decode_state = DECODE_ADDRESS
# reset half-duplex flag
if self.receive_on_ref[0]==True:
self.receive_on_timer_event.set()
# statistics
self.telRxNok += 1
logging.error("ADDRESS ERROR, address = "+str(self.telegram.address)+" not expected one = 1")
###################################################
# TODO: shall we do something like this to avoid trying to decode wrong data on next call?
# self.telegram_bits_start_pos = 0
# self.telegram_bits_end_pos = 0
###################################################
return # force return, dont delete this line!
elif self.decode_state == DECODE_SEQ_NR:
self.telegram.seqNr = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.SEQ_NR_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.SEQ_NR_LEN_BYTES*8
logging.info("SEQ_NR = "+str(self.telegram.seqNr))
### if self.rx_state == KEY_END_RECEIVED:
# check if seqNr ok
expectedSeqNr = (self.seqNrAck[0] + 1)%255
# new telegram?
if self.telegram.seqNr == expectedSeqNr:
self.telegram.seqNrRepeated = False
# we dont increment seqNrAck yet, we do that when we checked all other fields, especially the CRC
self.decode_state = DECODE_SEQ_NR_ACK
# repeated telegram?
elif self.telegram.seqNr == self.seqNrAck[0]:
# we dont increment or reset seqNr
# telegram will be discarded and acknowledged at the end if CRC and other things are ok
self.telegram.seqNrRepeated = True
self.decode_state = DECODE_SEQ_NR_ACK
# incorrect seqNrAck
else:
# SEQ ERROR: we just go back to PREAMBLE search state
# the transmitter will re-send on timeout
self.parse_state = SEARCH_PREAMBLE
# WARNING: always reset sub-state when going back to SEARCH_PREAMBLE
self.decode_state = DECODE_ADDRESS
# reset half-duplex flag
if self.receive_on_ref[0]==True:
self.receive_on_timer_event.set()
# statistics
self.telRxNok += 1
logging.error("SEQ_NR ERROR, seqNr = "+str(self.telegram.seqNr)+" not expected one = "+str(expectedSeqNr))
return # force return # DONT DELETE THIS LINE
### else:
### self.decode_state = DECODE_SEQ_NR_ACK
### logging.info("SEQ_NR not yet evaluated!")
elif self.decode_state == DECODE_SEQ_NR_ACK:
self.telegram.seqNrAck = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.SEQ_NR_ACK_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.SEQ_NR_ACK_LEN_BYTES*8
logging.info("SEQ_NR_ACK = "+str(self.telegram.seqNrAck))
self.decode_state = DECODE_COMMAND
elif self.decode_state == DECODE_COMMAND:
self.telegram.command = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.COMMAND_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.COMMAND_LEN_BYTES*8
self.decode_state = DECODE_DATA_LEN
logging.info("COMMAND = "+str(self.telegram.command)+" ("+audioSettings.CMD_STR[self.telegram.command]+")")
elif self.decode_state == DECODE_DATA_LEN:
self.telegram.data_length = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.DATA_SIZE_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.DATA_SIZE_LEN_BYTES*8
logging.info("DATA_LEN = "+str(self.telegram.data_length))
# check against max. possible data length
if self.telegram.data_length <= audioSettings.DATA_MAX_LEN_BYTES:
if self.telegram.data_length > 0:
self.decode_state = DECODE_DATA
else:
self.decode_state = DECODE_END
# data_length is too large!
else:
# DATA_LEN ERROR: we just go back to PREAMBLE search state
# the transmitter will re-send on timeout
self.parse_state = SEARCH_PREAMBLE
# WARNING: always reset sub-state when going back to SEARCH_PREAMBLE
self.decode_state = DECODE_ADDRESS
# reset half-duplex flag
if self.receive_on_ref[0]==True:
self.receive_on_timer_event.set()
# statistics
self.telRxNok += 1
logging.info("DATA_LEN ERROR, length = "+str(self.telegram.data_length)+" shall be <= "+str(audioSettings.DATA_MAX_LEN_BYTES))
return # force return # DONT DELETE THIS LINE
elif self.decode_state == DECODE_DATA:
self.telegram.data[self.telegram.decodedDataBytes] = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+8])
self.telegram_bits_start_pos += 8
self.telegram.decodedDataBytes += 1
logging.info("DATA["+str(self.telegram.decodedDataBytes-1)+"] = "+str(self.telegram.data[self.telegram.decodedDataBytes-1]))
if self.telegram.decodedDataBytes == self.telegram.data_length:
self.decode_state = DECODE_END
elif self.decode_state == DECODE_END:
self.telegram.end = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.END_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.END_LEN_BYTES*8
logging.info("END = "+str(self.telegram.end))
# pattern to detect END-byte pattern
if self.telegram.end == 170: # = 0xAA
self.decode_state = DECODE_CHECKSUM
else:
# END ERROR: we just go back to PREAMBLE search state
# the transmitter will re-send on timeout
self.parse_state = SEARCH_PREAMBLE
# WARNING: always reset sub-state when going back to SEARCH_PREAMBLE
self.decode_state = DECODE_ADDRESS
# reset half-duplex flag
if self.receive_on_ref[0]==True:
self.receive_on_timer_event.set()
# statistics
self.telRxNok += 1
logging.error("END ERROR, END = "+str(self.telegram.end)+" not expected one = 0xAA")
return # dont remove this line!
elif self.decode_state == DECODE_CHECKSUM:
self.telegram.checksum = ba2int(self.telegram_bits[self.telegram_bits_start_pos:self.telegram_bits_start_pos+audioSettings.CHECKSUM_LEN_BYTES*8])
self.telegram_bits_start_pos += audioSettings.CHECKSUM_LEN_BYTES*8
logging.info("CHECKSUM = "+str(self.telegram.checksum))
# calculate checksum
start = 85 # = b"\x55"
checksum = 0 # = b"\x00" # start value
checksum = checksum^start
checksum = checksum^self.telegram.address
checksum = checksum^self.telegram.seqNr
checksum = checksum^self.telegram.seqNrAck
checksum = checksum^self.telegram.command
checksum = checksum^self.telegram.data_length
for i in range(self.telegram.decodedDataBytes):
checksum = checksum^self.telegram.data[i]
logging.info("Calculated CHECKSUM = "+str(checksum))
# is checksum ok?
if checksum == self.telegram.checksum:
# process command
###########
masked_command = (self.telegram.command & audioSettings.COMMAND_MASK)
# TODO: remove use of resetSeqNrFlags
### resetSeqNrFlags = False
if self.telegram.seqNrRepeated == False:
# now update seqNrAck (the complete telegram was correct and had an increased seqNr)
self.seqNrAck[0] = (self.seqNrAck[0] + 1)%255
# statistics
self.telRxOk += 1
# process commands with increased seqNr
########################
if masked_command == audioSettings.COMMAND_CHAT_DATA:
# TODO: need to consider "\n" or hyperlink stuff, etc. ?
decryptor = self.cipher[0].decryptor()
data = decryptor.update(self.telegram.data[:self.telegram.decodedDataBytes]) + decryptor.finalize()
unpadder = padding.PKCS7(configuration.PADDING_BITS_LEN).unpadder()
unpadded_data = unpadder.update(data)
decryptedData = unpadded_data + unpadder.finalize()
decryptedData = decryptedData.decode('utf-8')
self.inMessageQueue.put(decryptedData)
self.inCommStatusQueue.put("RX: DATA")
logging.info("Received DATA: "+str(decryptedData))
elif masked_command == audioSettings.COMMAND_CHAT_DATA_START:
self.part_end_idx = 0
# TODO: need to consider "\n" or hyperlink stuff, etc. ?
self.data_part[self.part_end_idx:self.part_end_idx + self.telegram.decodedDataBytes] = self.telegram.data[:self.telegram.decodedDataBytes]
self.part_end_idx += self.telegram.decodedDataBytes
self.inCommStatusQueue.put("RX: Receiving data..")
self.inCommStatusQueue.put("RX: DATA START")
logging.info("Received DATA START: "+str(self.telegram.data[:self.telegram.decodedDataBytes]))
elif masked_command == audioSettings.COMMAND_CHAT_DATA_PART:
# TODO: need to consider "\n" or hyperlink stuff, etc. ?
self.data_part[self.part_end_idx:self.part_end_idx + self.telegram.decodedDataBytes] = self.telegram.data[:self.telegram.decodedDataBytes]
self.part_end_idx += self.telegram.decodedDataBytes
self.inCommStatusQueue.put("RX: DATA PART")
logging.info("Received DATA PART: "+str(self.telegram.data[:self.telegram.decodedDataBytes]))
elif masked_command == audioSettings.COMMAND_CHAT_DATA_END:
# TODO: need to consider "\n" or hyperlink stuff, etc. ?
self.data_part[self.part_end_idx:self.part_end_idx + self.telegram.decodedDataBytes] = self.telegram.data[:self.telegram.decodedDataBytes]
self.part_end_idx += self.telegram.decodedDataBytes
decryptor = self.cipher[0].decryptor()
data = decryptor.update(self.data_part[0:self.part_end_idx]) + decryptor.finalize()
unpadder = padding.PKCS7(configuration.PADDING_BITS_LEN).unpadder()
unpadded_data = unpadder.update(data)
decryptedData = unpadded_data + unpadder.finalize()
decryptedData = decryptedData.decode('utf-8')
self.inMessageQueue.put(decryptedData)
self.inCommStatusQueue.put("RX: DATA END")
logging.info("Received DATA END: "+str(decryptedData))
elif masked_command == audioSettings.COMMAND_CALL_REJECTED:
self.call_rejected = True
self.inCommStatusQueue.put("RX: CALL REJECTED")
logging.info("Received CALL REJECTED")
elif masked_command == audioSettings.COMMAND_CALL_END:
# set flag to reset sequence numbers
# TODO: check this removal from 2021.02.07-15:24 - remove permanently
# we need to ACK already with reset SeqNr because recepient has already reset seqNr too..
### resetSeqNrFlags = True
self.seqNrAck[0] = 0
self.seqNrAckRx[0] = 0
self.seqNrTx[0] = 0
# set flag
self.call_end = True
self.inCommStatusQueue.put("RX: CALL END")
logging.info("Received CALL END")
logging.info("SeqNrs reset!")
elif (masked_command == audioSettings.COMMAND_STARTUP_DATA_COMPLETE):
# TODO: add check against reception of retransmissions?
decryptor = self.cipher[0].decryptor()
data = decryptor.update(self.telegram.data[:self.telegram.decodedDataBytes]) + decryptor.finalize()
unpadder = padding.PKCS7(configuration.PADDING_BITS_LEN).unpadder()
unpadded_data = unpadder.update(data)
self.startup_data.comm_partner = unpadded_data + unpadder.finalize()
self.startup_data.comm_partner = self.startup_data.comm_partner.decode('utf-8')
self.startup_data_received = True
self.inCommStatusQueue.put("RX: STARTUP COMPLETE")
logging.info("Received STARTUP_DATA COMPLETE, COMM_PARTNER: "+str(self.startup_data.comm_partner))
# elif XXX: TODO: add here processing of other commands..
##################################
# process command without increased seqNr
#########################
elif masked_command == audioSettings.COMMAND_CALL:
# TODO: handle reception of retransmissions ?
# handle communication token
comm_token_partner = int.from_bytes(self.telegram.data[:self.telegram.decodedDataBytes], byteorder='big', signed=False)
if comm_token_partner > self.comm_token[0]:
self.have_token = False
elif comm_token_partner == self.comm_token[0]:
self.comm_token[0] = random.randint(0, 255)
# set flag
self.call = True
# statistics
self.telRxOk += 1
self.inCommStatusQueue.put("RX: CALL")
logging.info("Received CALL, we dont trigger ACK in this case..")
elif masked_command == audioSettings.COMMAND_CALL_ACCEPTED:
if self.rx_state == IDLE:
self.rx_state = CALL_ACCEPTED
self.call_accepted = True
self.inCommStatusQueue.put("RX: CALL ACCEPTED")
logging.info("Received CALL ACCEPTED")
else:
self.inCommStatusQueue.put("RX: CALL ACCEPTED (rep)")
logging.info("Received CALL ACCEPTED again, just ignore it.")
elif masked_command == audioSettings.COMMAND_KEY_START:
# accept if our call is accepted or if we accept their call...
if (self.rx_state == CALL_ACCEPTED) or (self.rx_state == IDLE):
self.rx_state = KEY_START_RECEIVED
# NOTE: with deep copy we asure we have a copy of the data so we dont use the data when it changes...overwriten by next message
# we do make dynamic allocation thaough, but dont have to manage indexes as we did in self.data_part
self.peer_public_key_start = copy.deepcopy(self.telegram.data[:self.telegram.decodedDataBytes])
self.key_start_received = True
self.inCommStatusQueue.put("RX: KEY START")
logging.info("Received KEY START : "+str(self.peer_public_key_start))
else:
self.inCommStatusQueue.put("RX: KEY START (rep)")
logging.info("Received KEY START AGAIN, just ignore it!")
elif masked_command == audioSettings.COMMAND_KEY_END:
if self.rx_state == KEY_START_RECEIVED:
self.rx_state = KEY_END_RECEIVED
# NOTE: with deep copy we asure we have a copy of the data so we dont use the data when it changes...overwriten by next message
# we do make dynamic allocation thaough, but dont have to manage indexes as we did in self.data_part
self.peer_public_key_end = copy.deepcopy(self.telegram.data[:self.telegram.decodedDataBytes])
# put together peer public key
public_key_peer_bytes = self.peer_public_key_start + self.peer_public_key_end
print("RX public_key_bytes as bytearray: "+str(public_key_peer_bytes))
public_key_peer_bytes = bytes(public_key_peer_bytes)
print("RX public_key_bytes as bytes: "+str(public_key_peer_bytes))
loaded_public_key_from_peer = x25519.X25519PublicKey.from_public_bytes(public_key_peer_bytes)
shared_key = self.private_key[0].exchange(loaded_public_key_from_peer)
# TODO: use a common password here?
iv = bytes(b"1234567890123456") # fix to a common value for both sides..
# NOTE: possible modes: CBC, GCM, CFB, CFB8, OFB
self.cipher[0] = Cipher(algorithms.AES(shared_key), modes.CBC(iv))
derived_session_key = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=b'out-band-verification',
).derive(shared_key)
# we create a session code calculated as an integer value based on the derived session key, not very conventional, but it works!
self.session_code = str(int(derived_session_key[0]+derived_session_key[1]*16+derived_session_key[2]*32))
self.key_end_received = True
self.inCommStatusQueue.put("RX: KEY END")
logging.info("Received KEY END: "+str(self.peer_public_key_end))
logging.info("Complete peer KEY: "+str(public_key_peer_bytes))
else:
self.inCommStatusQueue.put("RX: KEY END (rep)")
logging.info("Received KEY END AGAIN, just ignore it!")
elif (masked_command == audioSettings.COMMAND_STARTUP_DATA):
# TODO: add check against reception of retransmissions?
decryptor = self.cipher[0].decryptor()
data = decryptor.update(self.telegram.data[:self.telegram.decodedDataBytes]) + decryptor.finalize()
unpadder = padding.PKCS7(configuration.PADDING_BITS_LEN).unpadder()
unpadded_data = unpadder.update(data)
self.startup_data.comm_partner = unpadded_data + unpadder.finalize()
self.startup_data.comm_partner = self.startup_data.comm_partner.decode('utf-8')
self.startup_data_received = True
self.inCommStatusQueue.put("RX: STARTUP")
logging.info("Received STARTUP_DATA, COMM_PARTNER: "+str(self.startup_data.comm_partner))
elif self.telegram.command != audioSettings.COMMAND_TELEGRAM_ACK:
# statistics
self.telRxOk += 1 # TODO: can this be considered a CORRECT telegram? but it is repeated...hmm..
# Telegram really repeated
logging.info("Telegram with repeated SeqNr. Discard it BUT Acknowledge it.")
# process ACK
if (self.telegram.command & audioSettings.ACK_MASK) == audioSettings.COMMAND_TELEGRAM_ACK:
self.seqNrAckRx[0] = self.telegram.seqNrAck
self.ack_received[0] = True
self.ack_received[1] = cProfileTimer()
self.inCommStatusQueue.put("") # ("RX:") # clear status..
logging.info("Detected ACK with SeqNr = "+str(self.telegram.seqNrAck))
# trigger/send ACK
if self.telegram.command != audioSettings.COMMAND_TELEGRAM_ACK:
# this list shall contain ALL commands which require ACK - all other commands dont
if (masked_command == audioSettings.COMMAND_CHAT_DATA) or (masked_command == audioSettings.COMMAND_CHAT_DATA_START) or \
(masked_command == audioSettings.COMMAND_CHAT_DATA_PART) or (masked_command == audioSettings.COMMAND_CHAT_DATA_END) or \
(masked_command == audioSettings.COMMAND_CALL_REJECTED) or (masked_command == audioSettings.COMMAND_CALL_END) or \
(masked_command == audioSettings.COMMAND_STARTUP_DATA_COMPLETE):
self.send_ack[0] = True
self.inCommStatusQueue.put("") # ("RX:")
logging.info("Trigger Send ACK")
self.parse_state = SEARCH_PREAMBLE
# WARNING: always reset sub-state when going back to SEARCH_PREAMBLE
self.decode_state = DECODE_ADDRESS
# reset half-duplex flag
if self.receive_on_ref[0]==True:
self.receive_on_timer_event.set()
# do this "after" we reset receive_on_ref (RX -> OFF)
''' TODO: remove use of resetSeqNrFlags
if resetSeqNrFlags:
resetSeqNrFlags = False
# give enough time to send ACK to other side so it also resets its SeqNrs
# optimistic delay without retransmission
# TODO: consider making sure to get an ACK to this notification or implement life-signs
delay = audioSettings.TELEGRAM_MAX_LEN_SECONDS + audioSettings.CHANNEL_DELAY_SEC
time.sleep(delay)
self.seqNrAck[0] = 0
self.seqNrAckRx[0] = 0
self.seqNrTx[0] = 0
logging.info("SeqNrs reset!")
# '''
return # dont remove this line!
else:
# CHECKSUM ERROR: we just go back to PREAMBLE search state
# the transmitter will re-send on timeout
self.parse_state = SEARCH_PREAMBLE
# WARNING: always reset sub-state when going back to SEARCH_PREAMBLE
self.decode_state = DECODE_ADDRESS
# reset half-duplex flag
if self.receive_on_ref[0]==True:
self.receive_on_timer_event.set()
# statistics
self.telRxNok += 1
logging.error("CHECKSUM ERROR, checksum = "+str(self.telegram.checksum)+" not expected one = "+str(checksum))
return # force return (in case we decide to add further elifs later) # DONT DELETE THIS LINE
return