-
Notifications
You must be signed in to change notification settings - Fork 1
/
IthoCC1101.cpp
1260 lines (1065 loc) · 35.7 KB
/
IthoCC1101.cpp
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
/*
* Author: Klusjesman, modified bij supersjimmie for Arduino/ESP8266
*/
#include "IthoCC1101.h"
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
// default constructor
IthoCC1101::IthoCC1101(uint8_t counter, uint8_t sendTries) : CC1101()
{
this->outIthoPacket.counter = counter;
this->outIthoPacket.previous = IthoLow;
this->sendTries = sendTries;
this->receiveState = ExpectMessageStart;
//fixed device id
this->outIthoPacket.deviceId[0] = 101;
this->outIthoPacket.deviceId[1] = 89;
this->outIthoPacket.deviceId[2] = 154;
this->outIthoPacket.deviceId[3] = 153;
this->outIthoPacket.deviceId[4] = 170;
this->outIthoPacket.deviceId[5] = 105;
this->outIthoPacket.deviceId[6] = 154;
this->outIthoPacket.deviceId[7] = 86;
} //IthoCC1101
// default destructor
IthoCC1101::~IthoCC1101()
{
} //~IthoCC1101
void IthoCC1101::initSendMessage1()
{
/*
Configuration reverse engineered from remote print. The commands below are used by IthoDaalderop.
Base frequency 868.299866MHz
Channel 0
Channel spacing 199.951172kHz
Carrier frequency 868.299866MHz
Xtal frequency 26.000000MHz
Data rate 8.00896kBaud
Manchester disabled
Modulation 2-FSK
Deviation 25.390625kHz
TX power ?
PA ramping enabled
Whitening disabled
*/
writeCommand(CC1101_SRES);
delayMicroseconds(1);
writeRegister(CC1101_IOCFG0 ,0x2E); //High impedance (3-state)
writeRegister(CC1101_FREQ2 ,0x21); //00100001 878MHz-927.8MHz
writeRegister(CC1101_FREQ1 ,0x65); //01100101
writeRegister(CC1101_FREQ0 ,0x6A); //01101010
writeRegister(CC1101_MDMCFG4 ,0x07); //00000111
writeRegister(CC1101_MDMCFG3 ,0x43); //01000011
writeRegister(CC1101_MDMCFG2 ,0x00); //00000000 2-FSK, no manchester encoding/decoding, no preamble/sync
writeRegister(CC1101_MDMCFG1 ,0x22); //00100010
writeRegister(CC1101_MDMCFG0 ,0xF8); //11111000
writeRegister(CC1101_CHANNR ,0x00); //00000000
writeRegister(CC1101_DEVIATN ,0x40); //01000000
writeRegister(CC1101_FREND0 ,0x17); //00010111 use index 7 in PA table
writeRegister(CC1101_MCSM0 ,0x18); //00011000 PO timeout Approx. 146µs - 171µs, Auto calibrate When going from IDLE to RX or TX (or FSTXON)
writeRegister(CC1101_FSCAL3 ,0xA9); //10101001
writeRegister(CC1101_FSCAL2 ,0x2A); //00101010
writeRegister(CC1101_FSCAL1 ,0x00); //00000000
writeRegister(CC1101_FSCAL0 ,0x11); //00010001
writeRegister(CC1101_FSTEST ,0x59); //01011001 For test only. Do not write to this register.
writeRegister(CC1101_TEST2 ,0x81); //10000001 For test only. Do not write to this register.
writeRegister(CC1101_TEST1 ,0x35); //00110101 For test only. Do not write to this register.
writeRegister(CC1101_TEST0 ,0x0B); //00001011 For test only. Do not write to this register.
writeRegister(CC1101_PKTCTRL0 ,0x12); //00010010 Enable infinite length packets, CRC disabled, Turn data whitening off, Serial Synchronous mode
writeRegister(CC1101_ADDR ,0x00); //00000000
writeRegister(CC1101_PKTLEN ,0xFF); //11111111 //Not used, no hardware packet handling
//0x6F,0x26,0x2E,0x8C,0x87,0xCD,0xC7,0xC0
writeBurstRegister(CC1101_PATABLE | CC1101_WRITE_BURST, (uint8_t*)ithoPaTableSend, 8);
writeCommand(CC1101_SIDLE);
writeCommand(CC1101_SIDLE);
writeCommand(CC1101_SIDLE);
writeRegister(CC1101_MDMCFG4 ,0x08); //00001000
writeRegister(CC1101_MDMCFG3 ,0x43); //01000011
writeRegister(CC1101_DEVIATN ,0x40); //01000000
writeRegister(CC1101_IOCFG0 ,0x2D); //GDO0_Z_EN_N. When this output is 0, GDO0 is configured as input (for serial TX data).
writeRegister(CC1101_IOCFG1 ,0x0B); //Serial Clock. Synchronous to the data in synchronous serial mode.
writeCommand(CC1101_STX);
writeCommand(CC1101_SIDLE);
delayMicroseconds(1);
writeCommand(CC1101_SIDLE);
writeRegister(CC1101_MDMCFG4 ,0x08); //00001000
writeRegister(CC1101_MDMCFG3 ,0x43); //01000011
writeRegister(CC1101_DEVIATN ,0x40); //01000000
//writeRegister(CC1101_IOCFG0 ,0x2D); //GDO0_Z_EN_N. When this output is 0, GDO0 is configured as input (for serial TX data).
//writeRegister(CC1101_IOCFG1 ,0x0B); //Serial Clock. Synchronous to the data in synchronous serial mode.
//Itho is using serial mode for transmit. We want to use the TX FIFO with fixed packet length for simplicity.
writeRegister(CC1101_IOCFG0 ,0x2E);
writeRegister(CC1101_IOCFG1 ,0x2E);
writeRegister(CC1101_PKTLEN , 19);
writeRegister(CC1101_PKTCTRL0 ,0x00);
writeRegister(CC1101_PKTCTRL1 ,0x00);
}
void IthoCC1101::initSendMessage2(IthoCommand command)
{
//finishTransfer();
writeCommand(CC1101_SIDLE);
delayMicroseconds(1);
writeRegister(CC1101_IOCFG0 ,0x2E);
delayMicroseconds(1);
writeRegister(CC1101_IOCFG1 ,0x2E);
delayMicroseconds(1);
writeCommand(CC1101_SIDLE);
writeCommand(CC1101_SPWD);
delayMicroseconds(1);
/*
Configuration reverse engineered from remote print. The commands below are used by IthoDaalderop.
Base frequency 868.299866MHz
Channel 0
Channel spacing 199.951172kHz
Carrier frequency 868.299866MHz
Xtal frequency 26.000000MHz
Data rate 38.3835kBaud
Manchester disabled
Modulation 2-FSK
Deviation 50.781250kHz
TX power ?
PA ramping enabled
Whitening disabled
*/
writeCommand(CC1101_SRES);
delayMicroseconds(1);
writeRegister(CC1101_IOCFG0 ,0x2E); //High impedance (3-state)
writeRegister(CC1101_FREQ2 ,0x21); //00100001 878MHz-927.8MHz
writeRegister(CC1101_FREQ1 ,0x65); //01100101
writeRegister(CC1101_FREQ0 ,0x6A); //01101010
writeRegister(CC1101_MDMCFG4 ,0x5A); //difference compared to message1
writeRegister(CC1101_MDMCFG3 ,0x83); //difference compared to message1
writeRegister(CC1101_MDMCFG2 ,0x00); //00000000 2-FSK, no manchester encoding/decoding, no preamble/sync
writeRegister(CC1101_MDMCFG1 ,0x22); //00100010
writeRegister(CC1101_MDMCFG0 ,0xF8); //11111000
writeRegister(CC1101_CHANNR ,0x00); //00000000
writeRegister(CC1101_DEVIATN ,0x50); //difference compared to message1
writeRegister(CC1101_FREND0 ,0x17); //00010111 use index 7 in PA table
writeRegister(CC1101_MCSM0 ,0x18); //00011000 PO timeout Approx. 146µs - 171µs, Auto calibrate When going from IDLE to RX or TX (or FSTXON)
writeRegister(CC1101_FSCAL3 ,0xA9); //10101001
writeRegister(CC1101_FSCAL2 ,0x2A); //00101010
writeRegister(CC1101_FSCAL1 ,0x00); //00000000
writeRegister(CC1101_FSCAL0 ,0x11); //00010001
writeRegister(CC1101_FSTEST ,0x59); //01011001 For test only. Do not write to this register.
writeRegister(CC1101_TEST2 ,0x81); //10000001 For test only. Do not write to this register.
writeRegister(CC1101_TEST1 ,0x35); //00110101 For test only. Do not write to this register.
writeRegister(CC1101_TEST0 ,0x0B); //00001011 For test only. Do not write to this register.
writeRegister(CC1101_PKTCTRL0 ,0x12); //00010010 Enable infinite length packets, CRC disabled, Turn data whitening off, Serial Synchronous mode
writeRegister(CC1101_ADDR ,0x00); //00000000
writeRegister(CC1101_PKTLEN ,0xFF); //11111111 //Not used, no hardware packet handling
//0x6F,0x26,0x2E,0x8C,0x87,0xCD,0xC7,0xC0
writeBurstRegister(CC1101_PATABLE | CC1101_WRITE_BURST, (uint8_t*)ithoPaTableSend, 8);
//difference, message1 sends a STX here
writeCommand(CC1101_SIDLE);
writeCommand(CC1101_SIDLE);
writeRegister(CC1101_MDMCFG4 ,0x5A); //difference compared to message1
writeRegister(CC1101_MDMCFG3 ,0x83); //difference compared to message1
writeRegister(CC1101_DEVIATN ,0x50); //difference compared to message1
writeRegister(CC1101_IOCFG0 ,0x2D); //GDO0_Z_EN_N. When this output is 0, GDO0 is configured as input (for serial TX data).
writeRegister(CC1101_IOCFG1 ,0x0B); //Serial Clock. Synchronous to the data in synchronous serial mode.
writeCommand(CC1101_STX);
writeCommand(CC1101_SIDLE);
writeRegister(CC1101_MDMCFG4 ,0x5A); //difference compared to message1
writeRegister(CC1101_MDMCFG3 ,0x83); //difference compared to message1
writeRegister(CC1101_DEVIATN ,0x50); //difference compared to message1
//writeRegister(CC1101_IOCFG0 ,0x2D); //GDO0_Z_EN_N. When this output is 0, GDO0 is configured as input (for serial TX data).
//writeRegister(CC1101_IOCFG1 ,0x0B); //Serial Clock. Synchronous to the data in synchronous serial mode.
//Itho is using serial mode for transmit. We want to use the TX FIFO with fixed packet length for simplicity.
writeRegister(CC1101_IOCFG0 ,0x2E);
writeRegister(CC1101_IOCFG1 ,0x2E);
writeRegister(CC1101_PKTCTRL0 ,0x00);
writeRegister(CC1101_PKTCTRL1 ,0x00);
switch (command)
{
case IthoJoin:
writeRegister(CC1101_PKTLEN , 72);
break;
case IthoLeave:
writeRegister(CC1101_PKTLEN , 57);
break;
default:
writeRegister(CC1101_PKTLEN , 50);
break;
}
}
void IthoCC1101::finishTransfer()
{
writeCommand(CC1101_SIDLE);
delayMicroseconds(1);
writeRegister(CC1101_IOCFG0 ,0x2E);
writeRegister(CC1101_IOCFG1 ,0x2E);
writeCommand(CC1101_SIDLE);
writeCommand(CC1101_SPWD);
}
void IthoCC1101::initReceive()
{
/*
Configuration reverse engineered from RFT print.
Base frequency 868.299866MHz
Channel 0
Channel spacing 199.951172kHz
Carrier frequency 868.299866MHz
Xtal frequency 26.000000MHz
Data rate 38.3835kBaud
RX filter BW 325.000000kHz
Manchester disabled
Modulation 2-FSK
Deviation 50.781250kHz
TX power 0x6F,0x26,0x2E,0x7F,0x8A,0x84,0xCA,0xC4
PA ramping enabled
Whitening disabled
*/
writeCommand(CC1101_SRES);
writeRegister(CC1101_TEST0 ,0x09);
writeRegister(CC1101_FSCAL2 ,0x00);
//0x6F,0x26,0x2E,0x7F,0x8A,0x84,0xCA,0xC4
writeBurstRegister(CC1101_PATABLE | CC1101_WRITE_BURST, (uint8_t*)ithoPaTableReceive, 8);
writeCommand(CC1101_SCAL);
//wait for calibration to finish
while ((readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER)) != CC1101_MARCSTATE_IDLE) yield();
writeRegister(CC1101_FSCAL2 ,0x00);
writeRegister(CC1101_MCSM0 ,0x18); //no auto calibrate
writeRegister(CC1101_FREQ2 ,0x21);
writeRegister(CC1101_FREQ1 ,0x65);
writeRegister(CC1101_FREQ0 ,0x6A);
writeRegister(CC1101_IOCFG0 ,0x2E); //High impedance (3-state)
writeRegister(CC1101_IOCFG2 ,0x2E); //High impedance (3-state)
writeRegister(CC1101_FSCTRL1 ,0x06);
writeRegister(CC1101_FSCTRL0 ,0x00);
writeRegister(CC1101_MDMCFG4 ,0x5A);
writeRegister(CC1101_MDMCFG3 ,0x83);
writeRegister(CC1101_MDMCFG2 ,0x00); //Enable digital DC blocking filter before demodulator, 2-FSK, Disable Manchester encoding/decoding, No preamble/sync
writeRegister(CC1101_MDMCFG1 ,0x22); //Disable FEC
writeRegister(CC1101_MDMCFG0 ,0xF8);
writeRegister(CC1101_CHANNR ,0x00);
writeRegister(CC1101_DEVIATN ,0x50);
writeRegister(CC1101_FREND1 ,0x56);
writeRegister(CC1101_FREND0 ,0x17);
writeRegister(CC1101_MCSM0 ,0x18); //no auto calibrate
writeRegister(CC1101_FOCCFG ,0x16);
writeRegister(CC1101_BSCFG ,0x6C);
writeRegister(CC1101_AGCCTRL2 ,0x43);
writeRegister(CC1101_AGCCTRL1 ,0x40);
writeRegister(CC1101_AGCCTRL0 ,0x91);
writeRegister(CC1101_FSCAL3 ,0xA9);
writeRegister(CC1101_FSCAL2 ,0x2A);
writeRegister(CC1101_FSCAL1 ,0x00);
writeRegister(CC1101_FSCAL0 ,0x11);
writeRegister(CC1101_FSTEST ,0x59);
writeRegister(CC1101_TEST2 ,0x81);
writeRegister(CC1101_TEST1 ,0x35);
writeRegister(CC1101_TEST0 ,0x0B);
writeRegister(CC1101_PKTCTRL1 ,0x04); //No address check, Append two bytes with status RSSI/LQI/CRC OK,
writeRegister(CC1101_PKTCTRL0 ,0x32); //Infinite packet length mode, CRC disabled for TX and RX, No data whitening, Asynchronous serial mode, Data in on GDO0 and data out on either of the GDOx pins
writeRegister(CC1101_ADDR ,0x00);
writeRegister(CC1101_PKTLEN ,0xFF);
writeRegister(CC1101_TEST0 ,0x09);
writeRegister(CC1101_FSCAL2 ,0x00);
writeCommand(CC1101_SCAL);
//wait for calibration to finish
while ((readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER)) != CC1101_MARCSTATE_IDLE) yield();
writeRegister(CC1101_MCSM0 ,0x18); //no auto calibrate
writeCommand(CC1101_SIDLE);
writeCommand(CC1101_SIDLE);
writeRegister(CC1101_MDMCFG2 ,0x00); //Enable digital DC blocking filter before demodulator, 2-FSK, Disable Manchester encoding/decoding, No preamble/sync
writeRegister(CC1101_IOCFG0 ,0x0D); //Serial Data Output. Used for asynchronous serial mode.
writeCommand(CC1101_SRX);
while ((readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER)) != CC1101_MARCSTATE_RX) yield();
initReceiveMessage1();
}
void IthoCC1101::initReceiveMessage1()
{
uint8_t marcState;
writeCommand(CC1101_SIDLE); //idle
//set datarate
writeRegister(CC1101_MDMCFG4 ,0x08);
writeRegister(CC1101_MDMCFG3 ,0x43);
writeRegister(CC1101_DEVIATN ,0x40);
//set fifo mode with fixed packet length and sync bytes
writeRegister(CC1101_PKTLEN , 15); //15 bytes message (sync at beginning of message is removed by CC1101)
writeRegister(CC1101_PKTCTRL0 ,0x00);
writeRegister(CC1101_SYNC1 ,170); //message1 byte2
writeRegister(CC1101_SYNC0 ,173); //message1 byte3
writeRegister(CC1101_MDMCFG2 ,0x02);
writeRegister(CC1101_PKTCTRL1 ,0x00);
writeCommand(CC1101_SRX); //switch to RX state
// Check that the RX state has been entered
while (((marcState = readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER)) & CC1101_BITS_MARCSTATE) != CC1101_MARCSTATE_RX)
{
if (marcState == CC1101_MARCSTATE_RXFIFO_OVERFLOW) // RX_OVERFLOW
writeCommand(CC1101_SFRX); //flush RX buffer
yield();
}
receiveState = ExpectMessageStart;
}
void IthoCC1101::initReceiveMessage2(IthoCommand expectedCommand)
{
uint8_t marcState;
writeCommand(CC1101_SIDLE); //idle
//set datarate
writeRegister(CC1101_MDMCFG4 ,0x5A);
writeRegister(CC1101_MDMCFG3 ,0x83);
writeRegister(CC1101_DEVIATN ,0x50);
//set packet length based on expected message
switch (expectedCommand)
{
case IthoJoin:
writeRegister(CC1101_PKTLEN ,64);
receiveState = ExpectJoinCommand;
break;
case IthoLeave:
writeRegister(CC1101_PKTLEN ,57);
receiveState = ExpectLeaveCommand;
break;
default:
writeRegister(CC1101_PKTLEN ,42); //42 bytes message (sync at beginning of message is removed by CC1101)
receiveState = ExpectNormalCommand;
break;
}
//set fifo mode with fixed packet length and sync bytes
writeRegister(CC1101_PKTCTRL0 ,0x00);
writeRegister(CC1101_SYNC1 ,170); //message2 byte6
writeRegister(CC1101_SYNC0 ,171); //message2 byte7
writeRegister(CC1101_MDMCFG2 ,0x02);
writeRegister(CC1101_PKTCTRL1 ,0x00);
writeCommand(CC1101_SRX); //switch to RX state
// Check that the RX state has been entered
while (((marcState = readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER)) & CC1101_BITS_MARCSTATE) != CC1101_MARCSTATE_RX)
{
if (marcState == CC1101_MARCSTATE_RXFIFO_OVERFLOW) // RX_OVERFLOW
writeCommand(CC1101_SFRX); //flush RX buffer
yield();
}
}
bool IthoCC1101::checkForNewPacket()
{
bool result = false;
uint8_t length;
switch (receiveState)
{
case ExpectMessageStart:
length = receiveData(&inMessage1, 15);
//check if message1 is received
if ((length > 0) && (isValidMessageStart()))
{
parseMessageStart();
//switch to message2 RF settings
initReceiveMessage2(inIthoPacket.command);
lastMessage1Received = millis();
}
break;
case ExpectNormalCommand:
length = receiveData(&inMessage2, 42);
//check if message2 is received
if ((length > 0) && (isValidMessageCommand()))
{
parseMessageCommand();
//bug detection
//testCreateMessage();
//switch back to message1 RF settings
initReceiveMessage1();
//both messages are received
result = true;
}
else
{
//check if waiting timeout is reached (message2 is expected within 22ms after message1)
if (millis() - lastMessage1Received > 24)
{
//switch back to message1 RF settings
initReceiveMessage1();
}
}
break;
case ExpectJoinCommand:
length = receiveData(&inMessage2, 64);
//check if message2 is received
if ((length > 0) && (isValidMessageCommand()) && isValidMessageJoin())
{
parseMessageCommand();
parseMessageJoin();
//bug detection
//testCreateMessage();
//switch back to message1 RF settings
initReceiveMessage1();
//both messages are received
result = true;
}
else
{
//check if waiting timeout is reached (message2 is expected within 22ms after message1)
if (millis() - lastMessage1Received > 24)
{
//switch back to message1 RF settings
initReceiveMessage1();
}
}
break;
case ExpectLeaveCommand:
length = receiveData(&inMessage2, 45);
//check if message2 is received
if ((length > 0) && (isValidMessageCommand()) && isValidMessageLeave())
{
parseMessageCommand();
parseMessageLeave();
//bug detection
//testCreateMessage();
//switch back to message1 RF settings
initReceiveMessage1();
//both messages are received
result = true;
}
else
{
//check if waiting timeout is reached (message2 is expected within 22ms after message1)
if (millis() - lastMessage1Received > 24)
{
//switch back to message1 RF settings
initReceiveMessage1();
}
}
break;
}
return result;
}
bool IthoCC1101::isValidMessageStart()
{
if (inMessage1.data[12] != 170)
{
return false;
}
return true;
}
bool IthoCC1101::isValidMessageCommand()
{
if (inMessage2.data[37] != 170)
{
return false;
}
return true;
}
bool IthoCC1101::isValidMessageJoin()
{
/*
if (inMessage2.data[37] != 170)
{
return false;
}
*/
return true;
}
bool IthoCC1101::isValidMessageLeave()
{
/*
if (inMessage2.data[37] != 170)
{
return false;
}
*/
return true;
}
void IthoCC1101::parseMessageStart()
{
bool isFullCommand = true;
bool isMediumCommand = true;
bool isLowCommand = true;
bool isTimer1Command = true;
bool isTimer2Command = true;
bool isTimer3Command = true;
bool isJoinCommand = true;
bool isLeaveCommand = true;
//copy device id from packet
// TODO: verify if this really is this the device id
inIthoPacket.deviceId[0] = inMessage1.data[2];
inIthoPacket.deviceId[1] = inMessage1.data[3];
inIthoPacket.deviceId[2] = inMessage1.data[4] & 0b11111110; //last bit is part of command
//copy command data from packet
//message1 command starts at index 5, last bit!
uint8_t commandBytes[7];
commandBytes[0] = inMessage1.data[5] & 0b00000001;
commandBytes[1] = inMessage1.data[6];
commandBytes[2] = inMessage1.data[7];
commandBytes[3] = inMessage1.data[8];
commandBytes[4] = inMessage1.data[9];
commandBytes[5] = inMessage1.data[10];
commandBytes[6] = inMessage1.data[11];
//match received commandBytes with known command bytes
for (int i=0; i<7; i++)
{
if (commandBytes[i] != ithoMessage1FullCommandBytes[i]) isFullCommand = false;
if (commandBytes[i] != ithoMessage1MediumCommandBytes[i]) isMediumCommand = false;
if (commandBytes[i] != ithoMessage1LowCommandBytes[i]) isLowCommand = false;
if (commandBytes[i] != ithoMessage1Timer1CommandBytes[i]) isTimer1Command = false;
if (commandBytes[i] != ithoMessage1Timer2CommandBytes[i]) isTimer2Command = false;
if (commandBytes[i] != ithoMessage1Timer3CommandBytes[i]) isTimer3Command = false;
if (commandBytes[i] != ithoMessage1JoinCommandBytes[i]) isJoinCommand = false;
if (commandBytes[i] != ithoMessage1LeaveCommandBytes[i]) isLeaveCommand = false;
}
//determine command
inIthoPacket.command = IthoUnknown;
if (isFullCommand) inIthoPacket.command = IthoFull;
if (isMediumCommand) inIthoPacket.command = IthoMedium;
if (isLowCommand) inIthoPacket.command = IthoLow;
if (isTimer1Command) inIthoPacket.command = IthoTimer1;
if (isTimer2Command) inIthoPacket.command = IthoTimer2;
if (isTimer3Command) inIthoPacket.command = IthoTimer3;
if (isJoinCommand) inIthoPacket.command = IthoJoin;
if (isLeaveCommand) inIthoPacket.command = IthoLeave;
//previous command
inIthoPacket.previous = getMessage1PreviousCommand(inMessage1.data[14]);
}
void IthoCC1101::parseMessageCommand()
{
bool isFullCommand = true;
bool isMediumCommand = true;
bool isLowCommand = true;
bool isTimer1Command = true;
bool isTimer2Command = true;
bool isTimer3Command = true;
bool isJoinCommand = true;
bool isLeaveCommand = true;
//device id
inIthoPacket.deviceId[0] = inMessage2.data[8];
inIthoPacket.deviceId[1] = inMessage2.data[9];
inIthoPacket.deviceId[2] = inMessage2.data[10];
inIthoPacket.deviceId[3] = inMessage2.data[11];
inIthoPacket.deviceId[4] = inMessage2.data[12];
inIthoPacket.deviceId[5] = inMessage2.data[13];
inIthoPacket.deviceId[6] = inMessage2.data[14];
inIthoPacket.deviceId[7] = inMessage2.data[15];
//counter1
uint8_t row0 = inMessage2.data[16];
uint8_t row1 = inMessage2.data[17];
uint8_t row2 = inMessage2.data[18] & 0b11110000; //4 bits are part of command
inIthoPacket.counter = calculateMessageCounter(row0, row1, row2);
//copy command data from packet
uint8_t commandBytes[15];
commandBytes[0] = inMessage2.data[18] & 0b00001111; //4 bits are part of counter1
commandBytes[1] = inMessage2.data[19];
commandBytes[2] = inMessage2.data[20];
commandBytes[3] = inMessage2.data[21];
commandBytes[4] = inMessage2.data[22];
commandBytes[5] = inMessage2.data[23];
commandBytes[6] = inMessage2.data[24];
commandBytes[7] = inMessage2.data[25];
commandBytes[8] = inMessage2.data[26];
commandBytes[9] = inMessage2.data[27];
commandBytes[10] = inMessage2.data[28];
commandBytes[11] = inMessage2.data[29];
commandBytes[12] = inMessage2.data[30];
commandBytes[13] = inMessage2.data[31];
commandBytes[14] = inMessage2.data[32];
//match received commandBytes with known command bytes
for (int i=0; i<15; i++)
{
if (commandBytes[i] != ithoMessage2FullCommandBytes[i]) isFullCommand = false;
if (commandBytes[i] != ithoMessage2MediumCommandBytes[i]) isMediumCommand = false;
if (commandBytes[i] != ithoMessage2LowCommandBytes[i]) isLowCommand = false;
if (commandBytes[i] != ithoMessage2Timer1CommandBytes[i]) isTimer1Command = false;
if (commandBytes[i] != ithoMessage2Timer2CommandBytes[i]) isTimer2Command = false;
if (commandBytes[i] != ithoMessage2Timer3CommandBytes[i]) isTimer3Command = false;
if (commandBytes[i] != ithoMessage2JoinCommandBytes[i]) isJoinCommand = false;
if (commandBytes[i] != ithoMessage2LeaveCommandBytes[i]) isLeaveCommand = false;
}
//determine command
inIthoPacket.command = IthoUnknown;
if (isFullCommand) inIthoPacket.command = IthoFull;
if (isMediumCommand) inIthoPacket.command = IthoMedium;
if (isLowCommand) inIthoPacket.command = IthoLow;
if (isTimer1Command) inIthoPacket.command = IthoTimer1;
if (isTimer2Command) inIthoPacket.command = IthoTimer2;
if (isTimer3Command) inIthoPacket.command = IthoTimer3;
if (isJoinCommand) inIthoPacket.command = IthoJoin;
if (isLeaveCommand) inIthoPacket.command = IthoLeave;
}
void IthoCC1101::parseMessageJoin()
{
/*
for (int i=0;i<inMessage2.length;i++)
{
Serial.print(inMessage2.data[i]);
Serial.print("\n");
}
*/
}
void IthoCC1101::parseMessageLeave()
{
/*
for (int i=0;i<inMessage2.length;i++)
{
Serial.print(inMessage2.data[i]);
Serial.print("\n");
}
*/
}
//check if we can generate the same input message based on the counter and command only (yes we can!)
void IthoCC1101::testCreateMessage()
{
CC1101Packet outMessage1;
CC1101Packet outMessage2;
//update itho packet data
outIthoPacket.previous = inIthoPacket.previous;
outIthoPacket.command = inIthoPacket.command;
outIthoPacket.counter = inIthoPacket.counter;
//get message1 bytes
createMessageStart(&outIthoPacket, &outMessage1);
for (int i=0; i<inMessage1.length;i++)
{
if (inMessage1.data[i] != outMessage1.data[i+4])
{
Serial.print("message1 not ok=byte");
Serial.print(i+4);
Serial.print(" (");
Serial.print(inMessage1.data[i]);
Serial.print("/");
Serial.print(outMessage1.data[i+4]);
Serial.print(")\n");
}
}
//get message2 bytes
switch (outIthoPacket.command)
{
case IthoJoin:
createMessageJoin(&outIthoPacket, &outMessage2);
break;
case IthoLeave:
createMessageLeave(&outIthoPacket, &outMessage2);
break;
default:
createMessageCommand(&outIthoPacket, &outMessage2);
break;
}
for (int i=0; i<inMessage2.length;i++)
{
if (inMessage2.data[i] != outMessage2.data[i+8])
{
Serial.print("message2 not ok=byte");
Serial.print(i+8);
Serial.print(" (");
Serial.print(inMessage2.data[i]);
Serial.print("/");
Serial.print(outMessage2.data[i+8]);
Serial.print(")\n");
}
}
}
void IthoCC1101::sendCommand(IthoCommand command)
{
CC1101Packet outMessage1;
CC1101Packet outMessage2;
uint8_t maxTries = sendTries;
uint8_t delaytime = 40;
//update itho packet data
outIthoPacket.previous = outIthoPacket.command;
outIthoPacket.command = command;
outIthoPacket.counter += 1;
//get message1 bytes
createMessageStart(&outIthoPacket, &outMessage1);
//get message2 bytes
switch (command)
{
case IthoJoin:
createMessageJoin(&outIthoPacket, &outMessage2);
break;
case IthoLeave:
createMessageLeave(&outIthoPacket, &outMessage2);
//the leave command needs to be transmitted for 1 second according the manual
maxTries = 30;
delaytime = 4;
break;
default:
createMessageCommand(&outIthoPacket, &outMessage2);
break;
}
Serial.print("send\n");
//send messages
for (int i=0;i<maxTries;i++)
{
//message1
initSendMessage1();
sendData(&outMessage1);
delay(4);
//message2
initSendMessage2(outIthoPacket.command);
sendData(&outMessage2);
finishTransfer();
delay(delaytime);
}
}
void IthoCC1101::createMessageStart(IthoPacket *itho, CC1101Packet *packet)
{
packet->length = 19;
//fixed
packet->data[0] = 170;
packet->data[1] = 170;
packet->data[2] = 170;
packet->data[3] = 173;
//??
packet->data[4] = 51;
packet->data[5] = 83;
packet->data[6] = 51;
packet->data[7] = 43;
packet->data[8] = 84;
packet->data[9] = 204; //last bit is part of command
//command
uint8_t *commandBytes = getMessage1CommandBytes(itho->command);
packet->data[9] = packet->data[9] | commandBytes[0]; //only last bit is set
packet->data[10] = commandBytes[1];
packet->data[11] = commandBytes[2];
packet->data[12] = commandBytes[3];
packet->data[13] = commandBytes[4];
packet->data[14] = commandBytes[5];
packet->data[15] = commandBytes[6];
//fixed
packet->data[16] = 170;
packet->data[17] = 171;
//previous command
packet->data[18] = getMessage1Byte18(itho->previous);
}
void IthoCC1101::createMessageCommand(IthoPacket *itho, CC1101Packet *packet)
{
packet->length = 50;
//fixed
packet->data[0] = 170;
packet->data[1] = 170;
packet->data[2] = 170;
packet->data[3] = 170;
packet->data[4] = 170;
packet->data[5] = 170;
packet->data[6] = 170;
packet->data[7] = 171;
packet->data[8] = 254;
packet->data[9] = 0;
packet->data[10] = 179;
packet->data[11] = 42;
packet->data[12] = 171;
packet->data[13] = 42;
packet->data[14] = 149;
packet->data[15] = 154;
//device id
packet->data[16] = itho->deviceId[0];
packet->data[17] = itho->deviceId[1];
packet->data[18] = itho->deviceId[2];
packet->data[19] = itho->deviceId[3];
packet->data[20] = itho->deviceId[4];
packet->data[21] = itho->deviceId[5];
packet->data[22] = itho->deviceId[6];
packet->data[23] = itho->deviceId[7];
//counter bytes
packet->data[24] = calculateMessage2Byte24(itho->counter);
packet->data[25] = calculateMessage2Byte25(itho->counter);
packet->data[26] = calculateMessage2Byte26(itho->counter);
//command
uint8_t *commandBytes = getMessage2CommandBytes(itho->command);
packet->data[26] = packet->data[26] | commandBytes[0];
packet->data[27] = commandBytes[1];
packet->data[28] = commandBytes[2];
packet->data[29] = commandBytes[3];
packet->data[30] = commandBytes[4];
packet->data[31] = commandBytes[5];
packet->data[32] = commandBytes[6];
packet->data[33] = commandBytes[7];
packet->data[34] = commandBytes[8];
packet->data[35] = commandBytes[9];
packet->data[36] = commandBytes[10];
packet->data[37] = commandBytes[11];
packet->data[38] = commandBytes[12];
packet->data[39] = commandBytes[13];
packet->data[40] = commandBytes[14];
//counter bytes
packet->data[41] = calculateMessage2Byte41(itho->counter, itho->command);
packet->data[42] = calculateMessage2Byte42(itho->counter, itho->command);
packet->data[43] = calculateMessage2Byte43(itho->counter, itho->command);
//fixed
packet->data[44] = 172;
packet->data[45] = 170;
packet->data[46] = 170;
packet->data[47] = 170;
packet->data[48] = 170;
packet->data[49] = 170;
}
void IthoCC1101::createMessageJoin(IthoPacket *itho, CC1101Packet *packet)
{
//message3 is an extension on message2
createMessageCommand(itho, packet);
packet->length = 72;
//part of device id??
packet->data[44] = itho->deviceId[3];
packet->data[45] = itho->deviceId[4];
packet->data[46] = itho->deviceId[5];
packet->data[47] = itho->deviceId[6];
packet->data[48] = itho->deviceId[7];
//command join
packet->data[49] = 85;
//fixed
packet->data[50] = 165;
packet->data[51] = 105;
packet->data[52] = 89;
packet->data[53] = 86;
packet->data[54] = 106;
packet->data[55] = 149;
//device id
packet->data[56] = itho->deviceId[0];
packet->data[57] = itho->deviceId[1];
packet->data[58] = itho->deviceId[2];
packet->data[59] = itho->deviceId[3];
packet->data[60] = itho->deviceId[4];
packet->data[61] = itho->deviceId[5];
packet->data[62] = itho->deviceId[6];
packet->data[63] = itho->deviceId[7];
//counter bytes
packet->data[64] = calculateMessage2Byte64(itho->counter);
packet->data[65] = calculateMessage2Byte65(itho->counter);
packet->data[66] = calculateMessage2Byte66(itho->counter);
//fixed
packet->data[67] = 202;
packet->data[68] = 170;
packet->data[69] = 170;
packet->data[70] = 170;
packet->data[71] = 170;
}
void IthoCC1101::createMessageLeave(IthoPacket *itho, CC1101Packet *packet)
{
//message3 is an extension on message2
createMessageCommand(itho, packet);
packet->length = 57;
//part of device id??
packet->data[44] = itho->deviceId[3];
packet->data[45] = itho->deviceId[4];
packet->data[46] = itho->deviceId[5];
packet->data[47] = itho->deviceId[6];
packet->data[48] = itho->deviceId[7];
//counter bytes
packet->data[49] = calculateMessage2Byte49(itho->counter);
packet->data[50] = calculateMessage2Byte50(itho->counter);
packet->data[51] = calculateMessage2Byte51(itho->counter);
//fixed
packet->data[52] = 202;
packet->data[53] = 170;
packet->data[54] = 170;
packet->data[55] = 170;
packet->data[56] = 170;
}
//calculate 0-255 number out of 3 counter bytes
uint8_t IthoCC1101::calculateMessageCounter(uint8_t byte24, uint8_t byte25, uint8_t byte26)
{
uint8_t result;