This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
AskSinMain.cpp
1656 lines (1319 loc) · 67.3 KB
/
AskSinMain.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin driver implementation
// 2013-08-03 <horst@diebittners.de> Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
//- AskSin protocol functions ---------------------------------------------------------------------------------------------
//- with a lot of support from martin876 at FHEM forum
//- -----------------------------------------------------------------------------------------------------------------------
#include "AskSinMain.h"
//- some macros and definitions -------------------------------------------------------------------------------------------
#define _pgmB pgm_read_byte // short hand for PROGMEM read
#define _pgmW pgm_read_word
#define maxPayload 16 // defines the length of send_conf_poll and send_peer_poll
//- -----------------------------------------------------------------------------------------------------------------------
//- -----------------------------------------------------------------------------------------------------------------------
//- Homematic communication functions -------------------------------------------------------------------------------------
//- -----------------------------------------------------------------------------------------------------------------------
s_intGDO0 intGDO0; // structure to store interrupt number and pointer to active AskSin class instance
uint8_t bCast[] = {0,0,0,0}; // broad cast address
// public://--------------------------------------------------------------------------------------------------------------
//- homematic public protocol functions
void HM::init(void) {
#ifdef AS_DBG || AS_DBG_Explain
Serial.begin(57600); // serial setup
//Serial << F("AskSin debug enabled...\n"); // ...and some information
#endif
// register handling setup
prepEEprom(); // check the eeprom for first time boot, prepares the eeprom and loads the defaults
loadRegs();
// communication setup
cc.init(); // init the TRX868 module
intGDO0.nbr = cc.gdo0Int; // make the gdo0 interrupt public
attachInterrupt(intGDO0.nbr,isrGDO0,FALLING); // attach the interrupt
memcpy_P(hmId, &dParm.p[17], 3); // initialize hmId
statusLed.setHandle(this); // make the main class visible for status led
hm.stayAwake(1000);
}
void HM::poll(void) { // task scheduler
if (int0_flag) cc1101Recv_poll();
if (recv.data[0] > 0) recv_poll(); // trace the received string and decide what to do further
if (send.counter > 0) send_poll(); // something to send in the buffer?
if (conf.act > 0) send_conf_poll(); // some config to be send out
if (pevt.act > 0) send_peer_poll(); // send peer events
power_poll();
module_poll(); // poll the registered channel modules
statusLed.poll(); // poll the status leds
battery.poll(); // poll the battery check
}
void HM::cc1101Recv_poll(void) {
detachInterrupt(intGDO0.nbr); // switch interrupt off
if (hm.cc.receiveData(hm.recv.data)) { // is something in the receive string
hm.hm_dec(hm.recv.data); // decode the content
}
int0_flag = 0;
attachInterrupt(intGDO0.nbr,isrGDO0,FALLING); // switch interrupt on again
}
void HM::send_out(void) {
if (bitRead(send.data[2],5)) {
send.retries = dParm.maxRetr; // check for ACK request and set max retries counter
} else {
send.retries = 1; // otherwise send only one time
}
send.burst = bitRead(send.data[2],4); // burst necessary?
if (memcmp(&send.data[7], hmId, 3) == 0) { // if the message is addressed to us,
memcpy(recv.data,send.data,send.data[0]+1); // then copy in receive buffer. could be the case while sending from serial console
send.counter = 0; // no need to fire
} else { // it's not for us, so encode and put in send queue
send.counter = 1; // and fire
send.timer = 0;
}
}
/**
* Perform a device reset.
* At reset, all eeprom data was cleared.
*/
void HM::reset(void) {
setEeWo(ee[0].magicNr,0); // clear magic byte in eeprom and step in initRegisters
prepEEprom(); // check the eeprom for first time boot, prepares the eeprom and loads the defaults
loadRegs();
statusLed.set(STATUSLED_2, STATUSLED_MODE_BLINKSFAST, 5); // blink LED2 5 times short
}
/**
* Set the power mode of the device
*
* There are 3 power modes for the TRX868 module
* TX mode will switched on while something is in the send queue:
*
* 0 - RX mode enabled by default, take approx 17ma
*
* 1 - RX is in burst mode, RX will be switched on every 250ms to check if there is a carrier signal
* if yes - RX will stay enabled until timeout is reached, prolongation of timeout via receive function seems not necessary
* to be able to receive an ACK, RX mode should be switched on by send function
* if no - RX will go in idle mode and wait for the next carrier sense check
*
* 2 - RX is off by default, TX mode is enabled while sending something
* configuration mode is required in this setup to be able to receive at least pairing and config request strings
* should be realized by a 30 sec timeout function for RX mode
*
* as output we need a status indication if TRX868 module is in receive, send or idle mode
* idle mode is then the indication for power down mode of AVR
*/
void HM::setPowerMode(uint8_t mode) {
if (mode == 1) { // no power savings, RX is in receiving mode
set_sleep_mode(SLEEP_MODE_IDLE); // normal power saving
} else if (mode == 2) { // some power savings, RX is in burst mode
powr.parTO = 15000; // pairing timeout
powr.minTO = 2000; // stay awake for 2 seconds after sending
powr.nxtTO = millis() + 250; // check in 250ms for a burst signal
// MCUSR &= ~(1<<WDRF); // clear the reset flag
WDTCSR |= (1<<WDCE) | (1<<WDE); // set control register to change and enable the watch dog
WDTCSR = 1<<WDP2; // 250 ms
powr.wdTme = 250; // store the watch dog time for adding in the poll function
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // max power saving
} else if (mode == 3) { // most power savings, RX is off beside a special function where RX stay in receive for 30 sec
// MCUSR &= ~(1<<WDRF); // clear the reset flag
WDTCSR |= (1<<WDCE) | (1<<WDE); // set control register to change and enable the watch dog
WDTCSR = 1<<WDP2; // 250 ms
//WDTCSR = 1<<WDP1 | 1<<WDP2; // 1000 ms
//WDTCSR = 1<<WDP0 | 1<<WDP1 | 1<<WDP2; // 2000 ms
//WDTCSR = 1<<WDP0 | 1<<WDP3; // 8000 ms
powr.wdTme = 250; // store the watch dog time for adding in the poll function
//powr.wdTme = 8190; // store the watch dog time for adding in the poll function
}
if ((mode == 3) || (mode == 4)) { // most power savings, RX is off beside a special function where RX stay in receive for 30 sec
powr.parTO = 15000; // pairing timeout
powr.minTO = 1000; // stay awake for 1 seconds after sending
powr.nxtTO = millis() + 4000; // stay 4 seconds awake to finish boot time
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // max power saving
}
powr.mode = mode; // set power mode
powr.state = 1; // after init of the TRX module it is in RX mode
//Serial << "pwr.mode:" << powr.mode << '\n';
}
void HM::stayAwake(uint32_t xMillis) {
if (powr.state == 0) {
cc.detectBurst(); // if TRX is in sleep, switch it on
}
powr.state = 1; // remember TRX state
xMillis += millis();
if (powr.nxtTO > xMillis) {
return; // set the new timeout only if necessary
}
powr.nxtTO = xMillis; // stay awake for some time by setting next check time
}
/**
* Some functions for checking the config, preparing eeprom and load defaults
* to eeprom or in regs structure
*/
void HM::printConfig(void) {
uint8_t peer[4];
s_cnlDefType t;
// print content of devDef incl slice string table
Serial << F("\ncontent of dDef for ") << dDef.lstNbr << F(" elements\n");
for (uint8_t i = 0; i < dDef.lstNbr; i++) {
memcpy_P((void*)&t, &dDef.chPtr[i], sizeof(s_cnlDefType));
if ((t.lst == 3) || (t.lst == 4)) getCnlListByPeerIdx(t.cnl, 0); // load list3/4
Serial << F("cnl:") << t.cnl << F(", lst:") << t.lst << F(", pMax:") << t.pMax << '\n';
Serial << F("idx:") << t.sIdx << F(", len:") << t.sLen << F(", addr:") << t.pAddr << '\n';
Serial << F("regs: ") << pHex(dDef.slPtr + t.sIdx, t.sLen) << '\n';
Serial << F("data: ") << pHex((uint8_t*)t.pRegs, t.sLen) << ' ';
if (t.pMax == 0) {
Serial << F("\n\n");
continue;
}
for (uint8_t j = 1; j < t.pMax; j++) {
getCnlListByPeerIdx(t.cnl, j); // load list3/4
Serial << pHex((uint8_t*)t.pRegs, t.sLen) << ' ';
}
Serial << '\n';
for (uint8_t j = 0; j < t.pMax; j++) {
getPeerByIdx(t.cnl, j, peer);
Serial << pHex(peer, 4) << F(" ");
}
Serial << F("\n\n");
}
Serial << '\n';
}
void HM::prepEEprom(void) {
uint16_t crc = 0; // define variable for storing crc
uint8_t *p = (uint8_t*)dDef.chPtr; // cast devDef to char
for (uint8_t i = 0; i < dDef.lstNbr; i++) { // step through all lines of chDefType table
for (uint8_t j = 0; j < 9; j++) { // step through the first 9 bytes
crc = crc16(crc, _pgmB(&p[(i*11)+j])); // calculate the 16bit checksum for the table
}
}
#ifdef RPDB_DBG // some debug message
Serial << F("prepEEprom t_crc: ") << crc << F(", e_crc: ") << getEeWo(ee[0].magicNr) << '\n';
#endif
if (crc != getEeWo(ee[0].magicNr)) { // compare against eeprom's magic number
Serial << "prep eeprom\n";
#ifdef RPDB_DBG
Serial << F("format eeprom for:\n");
Serial << F("peerDB, addr:") << ee[0].peerDB << F(", len:") << ee[1].peerDB << '\n';
Serial << F("regsDB, addr:") << ee[0].regsDB << F(", len:") << ee[1].regsDB << '\n';
#endif
clrEeBl(ee[0].peerDB, ee[1].peerDB); // format the eeprom area
_delay_ms(50); // give eeprom some time
loadDefaults(); // do we have some default settings
_delay_ms(50); // give eeprom some time
}
setEeWo(ee[0].magicNr,crc); // write magic number to it's position
}
void HM::loadDefaults(void) {
if (dtRegs.nbr) {
#ifdef RPDB_DBG
Serial << F("set defaults:\n");
#endif
for (uint8_t i = 0; i < dtRegs.nbr; i++) { // step through the table
s_defaultRegsTbl *t = &dtRegs.ptr[i]; // pointer for better handling
// check if we search for regs or peer
uint16_t eeAddr = 0;
if (t->prIn == 0) { // we are going for peer
eeAddr = cdPeerAddrByCnlIdx(t->cnl ,t->pIdx); // get the eeprom address
} else if (t->prIn == 1) { // we are going for regs
eeAddr = cdListAddrByCnlLst(t->cnl, t->lst ,t->pIdx); // get the eeprom address
}
// find the respective address and write to eeprom
for (uint8_t j = 0; j < t->len; j++) { // step through the bytes
setEeBy(eeAddr++,_pgmB(&t->b[j])); // read from PROGMEM and write to eeprom
}
#ifdef RPDB_DBG // some debug message
Serial << F("cnl:") << t->cnl << F(", lst:") << t->lst << F(", idx:") << t->pIdx << ", addr:" << eeAddr << ", data: " << pHexPGM(t->b, t->len) << '\n';
#endif
}
}
}
void HM::loadRegs(void) {
// default regs filled regarding the cnlDefTable
for (uint8_t i = 0; i < dDef.lstNbr; i++) { // step through the list
const s_cnlDefType *t = &dDef.chPtr[i]; // pointer for easier handling
getEeBl(ee[0].regsDB + _pgmW(&t->pAddr), _pgmB(&t->sLen), (void*)_pgmW(&t->pRegs)); // load content from eeprom to user structure
}
// fill the master id
uint8_t ret = getRegAddr(0,0,0,0x0a,3,dParm.MAID); // get regs for 0x0a
//Serial << "MAID:" << pHex(dParm.MAID,3) << '\n';
}
void HM::regCnlModule(uint8_t cnl, s_mod_dlgt Delegate, uint16_t *mainList, uint16_t *peerList) {
modTbl[cnl].mDlgt = Delegate;
modTbl[cnl].use = 1;
// register the call back address for the list pointers
for (uint8_t i = 0; i < dDef.lstNbr; i++) { // step through the list
const s_cnlDefType *t = &dDef.chPtr[i]; // pointer for easier handling
if (_pgmB(&t->cnl) != cnl) continue;
if (_pgmB(&t->lst) >= 3) *peerList = _pgmW(&t->pRegs);
if (_pgmB(&t->lst) <= 1) *mainList = _pgmW(&t->pRegs);
}
}
uint32_t HM::getHMID(void) {
uint8_t a[3];
a[0] = hmId[2];
a[1] = hmId[1];
a[2] = hmId[0];
a[3] = 0;
return *(uint32_t*)&a;
}
uint8_t HM::getMsgCnt(void) {
return send.mCnt - 1;
}
//- -----------------------------------------------------------------------------------------------------------------------
//- external functions for pairing and communicating with the module
//- -----------------------------------------------------------------------------------------------------------------------
/**
* Send a pairing request to master
*
* 01 02 03 04 05 06 07
* 1A 00 A2 00 3F A6 5C 00 00 00 10 80 02 50 53 30 30 30 30 30 30 30 31 9F 04 01 01
*/
void HM::startPairing(void) {
statusLed.set(STATUSLED_2, STATUSLED_MODE_BLINKFAST); // led blink in config mode
//if (powr.mode > 1) stayAwake(powr.parTO); // stay awake for the next 30 seconds
memcpy_P(send_payLoad, dParm.p, 17); // copy details out of register.h
send_prep(send.mCnt++,0xA2,0x00,dParm.MAID ,send_payLoad,17);
}
void HM::sendInfoActuatorStatus(uint8_t cnl, uint8_t status, uint8_t flag) {
if (memcmp(dParm.MAID,bCast,3) == 0) {
return; // not paired, nothing to send
}
// "10;p01=06" => { txt => "INFO_ACTUATOR_STATUS", params => {
// CHANNEL => "2,2",
// STATUS => '4,2',
// UNKNOWN => "6,2",
// RSSI => '08,02,$val=(-1)*(hex($val))' } },
send_payLoad[0] = 0x06; // INFO_ACTUATOR_STATUS
send_payLoad[1] = cnl; // channel
send_payLoad[2] = status; // status
send_payLoad[3] = flag; // unknown
send_payLoad[4] = cc.rssi; // RSSI
// if it is an answer to a CONFIG_STATUS_REQUEST we have to use the same message id as the request
uint8_t tCnt;
if ((recv.data[3] == 0x01) && (recv.data[11] == 0x0E)) {
tCnt = recv_rCnt;
} else {
tCnt = send.mCnt++;
}
send_prep(tCnt,0xA4,0x10,dParm.MAID,send_payLoad,5); // prepare the message
}
void HM::sendACKStatus(uint8_t cnl, uint8_t status, uint8_t douolo) {
//if (memcmp(regDev.pairCentral,broadCast,3) == 0) return; // not paired, nothing to send
// "02;p01=01" => { txt => "ACK_STATUS", params => {
// CHANNEL => "02,2",
// STATUS => "04,2",
// DOWN => '06,02,$val=(hex($val)&0x20)?1:0',
// UP => '06,02,$val=(hex($val)&0x10)?1:0',
// LOWBAT => '06,02,$val=(hex($val)&0x80)?1:0',
// RSSI => '08,02,$val=(-1)*(hex($val))', }},
send_payLoad[0] = 0x01; // ACK Status
send_payLoad[1] = cnl; // channel
send_payLoad[2] = status; // status
send_payLoad[3] = douolo | (battery.state << 7); // down, up, low battery
send_payLoad[4] = cc.rssi; // RSSI
// l> 0E EA 80 02 1F B7 4A 63 19 63 01 01 C8 00 4B
//send_prep(recv_rCnt,0x80,0x02,regDev.pairCentral,send_payLoad,5); // prepare the message
send_prep(recv_rCnt,0x80,0x02,recv_reID,send_payLoad,5); // prepare the message
}
void HM::sendPeerREMOTE(uint8_t cnl, uint8_t longPress, uint8_t lowBat) {
// no data needed, because it is a (40)REMOTE EVENT
// "40" => { txt => "REMOTE" , params => {
// BUTTON => '00,2,$val=(hex($val)&0x3F)',
// LONG => '00,2,$val=(hex($val)&0x40)?1:0',
// LOWBAT => '00,2,$val=(hex($val)&0x80)?1:0',
// COUNTER => "02,2", } },
// type source target msg cnt
// l> 0B 0A B4 40 1F A6 5C 1F B7 4A 01 01 (l:12)(160188)
// l> 0B 0B B4 40 1F A6 5C 1F B7 4A 41 02 (l:12)(169121)
pevt.t = cnlDefbyPeer(cnl); // get the index number in cnlDefType
if (pevt.t == NULL) {
return;
}
//pevt.msgCnt = dParm.cnlCnt[cnl];
pevt.msgCnt = modTbl[cnl].msgCnt;
if (longPress == 1) {
pevt.reqACK = 0; // repeated messages do not need an ACK
} else {
pevt.reqACK = 0x20;
}
pevt.type = 0x40; // we want to send an remote event
if (battery.state) {
lowBat = 1;
}
pevt.data[0] = cnl | ((longPress)?1:0) << 6 | lowBat << 7; // construct message
pevt.data[1] = modTbl[cnl].msgCnt++; // increase event counter, important for switch event
// pevt.data[1] = dParm.cnlCnt[cnl]++; // increase event counter, important for switch event
pevt.len = 2; // 2 bytes payload
pevt.act = 1; // active, 1 = yes, 0 = no
//Serial << "remote; cdpIdx:" << pevt.cdpIdx << ", type:" << pHexB(pevt.type) << ", rACK:" << pHexB(pevt.reqACK) << ", msgCnt:" << pevt.msgCnt << ", data:" << pHex(pevt.data,pevt.len) << '\n';
}
//void HM::sendPeerWEATHER(uint8_t cnl, uint16_t temp, uint8_t hum, uint16_t pres, uint32_t lux) {
// debugging
void HM::sendPeerWEATHER(uint8_t cnl, uint16_t temp, uint8_t hum, uint16_t pres, uint32_t lux) {
// TEMP => '00,4,$val=((hex($val)&0x3FFF)/10)*((hex($val)&0x4000)?-1:1)',
// HUM => '04,2,$val=(hex($val))', } },
// type source target temp hum
// l> 0C 0A B4 70 1F A6 5C 1F B7 4A 01 01 01 (l:13)(160188)
// l> 0B 0B B4 40 1F A6 5C 1F B7 4A 41 02 (l:12)(169121)
pevt.t = cnlDefbyPeer(cnl); // get the index number in cnlDefType
if (pevt.t == NULL) return;
pevt.type = 0x70; // 0x70 -> frame-id for weather event
pevt.reqACK = 0x20; // we like to get an ACK
pevt.data[0] = (temp >> 8) & 0xFF | battery.state << 7; // temperature data
pevt.data[1] = temp & 0xFF;
pevt.data[2] = hum; // humidity
pevt.data[3] = (pres >> 8) & 0xFF; // air pressure
pevt.data[4] = pres & 0xFF;
pevt.data[5] = (lux >> 24) & 0xFFFFFF; // luminosity
lux = lux & 0xFFFFFF;
pevt.data[6] = (lux >> 16) & 0xFFFF;
lux = lux & 0xFFFF;
pevt.data[7] = (lux >> 8) & 0xFF;
pevt.data[8] = lux & 0xFF;
pevt.data[9] = (battery.voltage >> 8) & 0xFF; // battery voltage
pevt.data[10] = battery.voltage & 0xFF;
pevt.len = 11;
pevt.msgCnt++;
pevt.act = 1; // active, 1 = yes, 0 = no
}
void HM::sendPeerRAW(uint8_t cnl, uint8_t type, uint8_t *data, uint8_t len) {
// validate the input, and fill the respective variables in the struct
// message handling is taken from send_peer_poll
if (cnl > dDef.cnlNbr) return; // channel out of range, do nothing
// if (pevt.act) return; // already sending an event, leave
// if (doesListExist(cnl,4) == 0) { // check if a list4 exist, otherwise leave
//Serial << "sendPeerREMOTE failed\n";
// return;
// }
// set variables in struct and make send_peer_poll active
// pevt.cnl = cnl; // peer database channel
// pevt.type = type; // message type
// pevt.mFlg = 0xA2;
// if (len > 0) { // copy data if there are some
// memcpy(pevt.data, data, len); // data to send
// pevt.len = len; // len of data to send
// }
// pevt.act = 1; // active, 1 = yes, 0 = no
//statusLed.setset(STATUSLED_BOTH, STATUSLED_MODE_BLINKSFAST, 1); // blink led1 one time
}
void HM::send_ACK(void) {
uint8_t payLoad[] = {0x00}; // ACK
send_prep(recv_rCnt,0x80,0x02,recv_reID,payLoad,1);
}
void HM::send_NACK(void) {
uint8_t payLoad[] = {0x80}; // NACK
send_prep(recv_rCnt,0x80,0x02,recv_reID,payLoad,1);
}
// protected://-----------------------------------------------------------------------------------------------------------
//- hm communication functions
void HM::recv_poll(void) { // handles the receive objects
// do some checkups
if (memcmp(&recv.data[7], hmId, 3) == 0) {
recv.forUs = 1; // for us
} else {
recv.forUs = 0;
}
if (memcmp(&recv.data[7], bCast, 3) == 0) {
recv.bCast = 1; // or a broadcast
} else {
recv.bCast = 0; // otherwise only a log message
}
// show debug message
#ifdef AS_DBG // some debug message
if(recv.forUs) {
Serial << F("-> ");
} else if(recv.bCast) {
Serial << F("b> ");
} else {
Serial << F("l> ");
}
Serial << pHexL(recv.data, recv.data[0]+1) << pTime();
exMsg(recv.data); // explain message
#endif
// if it's not for us or a broadcast message we could skip
if ((recv.forUs == 0) && (recv.bCast == 0)) {
recv.data[0] = 0; // clear receive string
return;
}
// ToDo: Workaround for pairing problems with CCU. We should fix this
// x:1A CA E5, y: 16 30 83
/*
// is the message from a valid sender (pair or peer), if not then exit - takes ~2ms
if (isPairKnown(recv_reID) == 0) { // check against peers
#if defined(AS_DBG) // some debug message
Serial << "pair/peer did not fit, exit\n";
#endif
recv.data[0] = 0; // clear receive string
return;
}
*/
// check if it was a repeated message, delete while already received
if (recv_isRpt) { // check repeated flag
#if defined(AS_DBG) // some debug message
Serial << F(" repeated message; ");
if (recv.mCnt == recv_rCnt) {
Serial << F("already received - skip\n");
} else {
Serial << F("not received before...\n");
}
#endif
if (recv.mCnt == recv_rCnt) { // already received
recv.data[0] = 0; // therefore ignore
return; // and skip
}
}
// if the message comes from pair, we should remember the message id
if (isPairKnown(recv_reID)) {
recv.mCnt = recv_rCnt;
}
if (((recv.forUs) || (recv.bCast)) && (recv_isMsg)) { // message is a config message
if (recv_msgTp == 0x01) { // configuration message handling
recv_PairConfig();
} else if (recv_msgTp == 0x02) { // message seems to be an ACK
send.counter = 0;
if (pevt.act == 1) {
// we got an ACK after key press?
statusLed.stop(STATUSLED_BOTH);
statusLed.set(STATUSLED_1, STATUSLED_MODE_BLINKSFAST, 1); // blink led 1 once
}
} else if (recv_msgTp == 0x11) { // pair event handling
recv_PairEvent();
} else if (recv_msgTp >= 0x12) { // peer event handling
recv_PeerEvent();
}
#if defined(AS_DBG) // some debug message
else Serial << F("\nUNKNOWN MESSAGE, PLEASE REPORT!\n\n");
#endif
}
if (recv.forUs) {
main_Jump(); // does main sketch want's to be informed?
}
//to do: if it is a broadcast message, do something with
recv.data[0] = 0; // otherwise ignore
}
void HM::send_poll(void) { // handles the send queue
unsigned long mils = millis();
if((send.counter <= send.retries) && (send.timer <= mils)) { // not all sends done and timing is OK
// here we encode and send the string
hm_enc(send.data); // encode the string
detachInterrupt(intGDO0.nbr); // disable interrupt otherwise we could get some new content while we copy the buffer
cc.sendData(send.data,send.burst); // and send
attachInterrupt(intGDO0.nbr,isrGDO0,FALLING); // enable the interrupt again
hm_dec(send.data); // decode the string
// setting some variables
send.counter++; // increase send counter
send.timer = mils + dParm.timeOut; // set the timer for next action
powr.state = 1; // remember TRX module status, after sending it is always in RX mode
if ((powr.mode > 0) && (powr.nxtTO < (mils + powr.minTO))) stayAwake(powr.minTO); // stay awake for some time
#if defined(AS_DBG) // some debug messages
Serial << F("<- ") << pHexL(send.data, send.data[0]+1) << pTime();
#endif
if (pevt.act == 1) {
statusLed.set(STATUSLED_BOTH, STATUSLED_MODE_BLINKSFAST, 1); // blink led 1 and led 2 once after key press
}
}
if((send.counter > send.retries) && (send.counter < dParm.maxRetr)) { // all send but don't wait for an ACK
send.counter = 0; send.timer = 0; // clear send flag
}
if((send.counter > send.retries) && (send.timer <= mils)) { // max retries achieved, but seems to have no answer
send.counter = 0; send.timer = 0; // cleanup of send buffer
// todo: error handling, here we could jump some were to blink a led or whatever
if (pevt.act == 1) {
statusLed.stop(STATUSLED_BOTH);
statusLed.set(STATUSLED_2, STATUSLED_MODE_BLINKSLOW, 1); // blink the led 2 once if key press before
}
#if defined(AS_DBG)
Serial << F("-> NA ") << pTime();
#endif
}
} // ready, should work
void HM::send_conf_poll(void) {
if (send.counter > 0) return; // send queue is busy, let's wait
uint8_t len;
statusLed.set(STATUSLED_1, STATUSLED_MODE_BLINKSFAST, 1); // blink the led 1 once
//Serial << "send conf poll\n";
if (conf.type == 0x01) {
// answer Cnl Peer Peer Peer Peer
// l> 1A 05 A0 10 1E 7A AD 63 19 63 01 1F A6 5C 02 1F A6 5C 01 11 22 33 02 11 22 33 01
// Cnl Termination
// l> 0E 06 A0 10 1E 7A AD 63 19 63 01 00 00 00 00
len = getPeerForMsg(conf.channel, send_payLoad+1); // get peer list
if (len == 0x00) { // check if all done
memset(&conf, 0, sizeof(conf)); // clear the channel struct
return; // exit
} else if (len == 0xff) { // failure, out of range
memset(&conf, 0, sizeof(conf)); // clear the channel struct
send_NACK();
} else { // seems to be ok, answer
send_payLoad[0] = 0x01; // INFO_PEER_LIST
send_prep(conf.mCnt++,0xA0,0x10,conf.reID,send_payLoad,len+1); // prepare the message
//send_prep(send.mCnt++,0xA0,0x10,conf.reID,send_payLoad,len+1); // prepare the message
}
} else if (conf.type == 0x02) {
// INFO_PARAM_RESPONSE_PAIRS message
// RegL_01: 30:06 32:50 34:4B 35:50 56:00 57:24 58:01 59:01 00:00
// l> 1A 04 A0 10 1E 7A AD 63 19 63 02 30 06 32 50 34 4B 35 50 56 00 57 24 58 01 59 01 (l:27)(131405)
len = getListForMsg2(conf.channel, conf.list, conf.peer, send_payLoad+1); // get the message
if (len == 0) { // check if all done
memset(&conf, 0, sizeof(conf)); // clear the channel struct
return; // and exit
} else if (len == 0xff) { // failure, out of range
memset(&conf, 0, sizeof(conf)); // clear the channel struct
send_NACK();
} else { // seems to be ok, answer
send_payLoad[0] = 0x02; // INFO_PARAM_RESPONSE_PAIRS
send_prep(conf.mCnt++,0xA0,0x10,conf.reID,send_payLoad,len+1); // prepare the message
//send_prep(send.mCnt++,0xA0,0x10,conf.reID,send_payLoad,len+1); // prepare the message
}
} else if (conf.type == 0x03) {
// INFO_PARAM_RESPONSE_SEQ message
// RegL_01: 30:06 32:50 34:4B 35:50 56:00 57:24 58:01 59:01 00:00
// l> 1A 04 A0 10 1E 7A AD 63 19 63 02 30 06 32 50 34 4B 35 50 56 00 57 24 58 01 59 01 (l:27)(131405)
}
}
void HM::send_peer_poll(void) {
// step through the peers and send the message
// if we didn't found a peer to send to, then send status to master
static uint8_t pPtr = 0, msgCnt_L, statSend;
static s_cnlDefType *t, *tL;
if (send.counter > 0) return; // something is in the send queue, lets wait for the next free slot
// first time run detection
if ((tL == pevt.t) && (msgCnt_L == pevt.msgCnt)) { // nothing changed
if ((statSend == 1) && (pPtr >= _pgmB(&t->pMax))) { // check if we are through
pevt.act = 0; // no need to jump in again
return;
}
} else { // start again
tL = pevt.t; // remember for next time
msgCnt_L = pevt.msgCnt;
pPtr = 0; // set peer pointer to start
statSend = 0; // if loop is on end and statSend is 0 then we have to send to master
t = pevt.t; // some shorthand
}
// check if peer pointer is through and status to master must be send - return if done
if ((statSend == 0) && ( pPtr >= _pgmB(&t->pMax))) {
send_prep(send.mCnt++,(0x82|pevt.reqACK),pevt.type,dParm.MAID,pevt.data,pevt.len); // prepare the message
statSend = 1;
return;
}
// step through the peers, get the respective list4 and send the message
uint32_t tPeer;
uint8_t ret = getPeerByIdx(_pgmB(&t->cnl),pPtr,(uint8_t*)&tPeer);
//Serial << "pP:" << pPtr << ", tp:" << pHex((uint8_t*)&tPeer,4) << '\n';
if (tPeer == 0) {
pPtr++;
return;
}
// if we are here we have a valid peer address to send to
statSend = 1; // first peer address found, no need to send status to the master
// now we need the respective list4 to know if we have to send a burst message
// in regLstByte there are two information. peer needs AES and burst needed
// AES will be ignored at the moment, but burst needed will be translated into the message flag - bit 0 in regLstByte, translated to bit 4 = burst transmission in msgFlag
uint8_t lB[1];
getRegAddr(_pgmB(&t->cnl),_pgmB(&t->lst),pPtr++,0x01,1,lB); // get regs for 0x01
//Serial << "rB:" << pHexB(lB[0]) << '\n';
send_prep(send.mCnt++,(0x82|pevt.reqACK|bitRead(lB[0],0)<<4),pevt.type,(uint8_t*)&tPeer,pevt.data,pevt.len); // prepare the message
}
void HM::power_poll(void) {
// there are 3 power modes for the TRX868 module
// TX mode will switched on while something is in the send queue
// 1 - RX mode enabled by default, take approx 17ma
// 2 - RX is in burst mode, RX will be switched on every 250ms to check if there is a carrier signal
// if yes - RX will stay enabled until timeout is reached, prolongation of timeout via receive function seems not necessary
// to be able to receive an ACK, RX mode should be switched on by send function
// if no - RX will go in idle mode and wait for the next carrier sense check
// 3 - RX is off by default, TX mode is enabled while sending something
// configuration mode is required in this setup to be able to receive at least pairing and config request strings
// should be realized by a 15 sec timeout function for RX mode
// system time in millis will be hold by a regular wakeup from the watchdog timer
// 4 - Same as power mode 3 but without watchdog
if (powr.mode == 0) return; // in mode 0 there is nothing to do
unsigned long mils = millis();
if (powr.nxtTO > mils) return; // no need to do anything
if (send.counter > 0) return; // send queue not empty
if ((powr.mode == 2) && (powr.state == 0)) {
uint32_t nxtTO;
// power mode 2, module is in sleep and next check is reached
if (cc.detectBurst()) { // check for a burst signal, if we have one, we should stay awake
nxtTO = millis() + powr.minTO; // schedule next timeout with some delay
} else { // no burst was detected, go to sleep in next cycle
nxtTO = millis(); // set timer accordingly
}
powr.state = 1;
powr.nxtTO = nxtTO;
return;
} else if ((powr.mode == 2) && (powr.state == 1)) {
// power mode 2, module is active and next check is reached
cc.setPowerDownState(); // go to sleep
powr.state = 0;
powr.nxtTO = millis() + 250; // schedule next check in 250 ms
} else if ((powr.mode >= 3) && (powr.state == 1)) {
// power mode 3, check RX mode against timer. typically RX is off beside a special command to switch RX on for at least 30 seconds
cc.setPowerDownState(); // go to sleep
powr.state = 0;
} else if ((powr.mode > 1) && (powr.state == 0)) { // TRX module is off, so lets sleep for a while
// sleep for mode 2, 3 and 4
// Serial.println ("Goto sleep"); delay(100); //hfm debug
statusLed.stop(STATUSLED_BOTH); // stop blinking, because we are going to sleep
if ((powr.mode == 2) || (powr.mode == 3)) {
WDTCSR |= (1<<WDIE); // enable watch dog if power mode 2 or 3
}
// Serial << ":";
// delay(100);
ADCSRA = 0; // disable ADC
uint8_t xPrr = PRR; // turn off various modules
PRR = 0xFF;
sleep_enable(); // enable the sleep mode
MCUCR = (1<<BODS)|(1<<BODSE); // turn off brown-out enable in software
MCUCR = (1<<BODS); // must be done right before sleep
sleep_cpu(); // goto sleep
/* wake up here */
sleep_disable(); // disable sleep
if ((powr.mode == 2) || (powr.mode == 3)) {
WDTCSR &= ~(1<<WDIE); // disable watch dog
}
PRR = xPrr; // restore modules
if (wd_flag == 1) { // add the watchdog time to millis()
wd_flag = 0; // to detect the next watch dog timeout
timer0_millis += powr.wdTme; // add watchdog time to millis() function
} else {
stayAwake(powr.minTO); // stay awake for some time, if the wakeup where not raised from watchdog
}
// Serial << ".";
}
}
void HM::module_poll(void) {
for (uint8_t i = 0; i <= dDef.cnlNbr; i++) {
if (modTbl[i].use) modTbl[i].mDlgt(0,0,0,NULL,0);
}
}
void HM::hm_enc(uint8_t *buf) {
buf[1] = (~buf[1]) ^ 0x89;
uint8_t buf2 = buf[2];
uint8_t prev = buf[1];
uint8_t i;
for (i=2; i<buf[0]; i++) {
prev = (prev + 0xdc) ^ buf[i];
buf[i] = prev;
}
buf[i] ^= buf2;
}
void HM::hm_dec(uint8_t *buf) {
uint8_t prev = buf[1];
buf[1] = (~buf[1]) ^ 0x89;
uint8_t i, t;
for (i=2; i<buf[0]; i++) {
t = buf[i];
buf[i] = (prev + 0xdc) ^ buf[i];
prev = t;
}
buf[i] ^= buf[2];
}
void HM::exMsg(uint8_t *buf) {
#ifdef AS_DBG_Explain
#define b_len buf[0]
#define b_msgTp buf[3]
#define b_by10 buf[10]
#define b_by11 buf[11]
Serial << F(" "); // save some byte and send 3 blanks once, instead of having it in every if
if ((b_msgTp == 0x00)) {
Serial << F("DEVICE_INFO; fw: ") << pHex(&buf[10],1) << F(", type: ") << pHex(&buf[11],2) << F(", serial: ") << pHex(&buf[13],10) << '\n';
Serial << F(" , class: ") << pHex(&buf[23],1) << F(", pCnlA: ") << pHex(&buf[24],1) << F(", pCnlB: ") << pHex(&buf[25],1) << F(", na: ") << pHex(&buf[26],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x01)) {
Serial << F("CONFIG_PEER_ADD; cnl: ") << pHex(&buf[10],1) << F(", peer: ") << pHex(&buf[12],3) << F(", pCnlA: ") << pHex(&buf[15],1) << F(", pCnlB: ") << pHex(&buf[16],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x02)) {
Serial << F("CONFIG_PEER_REMOVE; cnl: ") << pHex(&buf[10],1) << F(", peer: ") << pHex(&buf[12],3) << F(", pCnlA: ") << pHex(&buf[15],1) << F(", pCnlB: ") << pHex(&buf[16],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x03)) {
Serial << F("CONFIG_PEER_LIST_REQ; cnl: ") << pHex(&buf[10],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x04)) {
Serial << F("CONFIG_PARAM_REQ; cnl: ") << pHex(&buf[10],1) << F(", peer: ") << pHex(&buf[12],3) << F(", pCnl: ") << pHex(&buf[15],1) << F(", lst: ") << pHex(&buf[16],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x05)) {
Serial << F("CONFIG_START; cnl: ") << pHex(&buf[10],1) << F(", peer: ") << pHex(&buf[12],3) << F(", pCnl: ") << pHex(&buf[15],1) << F(", lst: ") << pHex(&buf[16],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x06)) {
Serial << F("CONFIG_END; cnl: ") << pHex(&buf[10],1);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x08)) {
Serial << F("CONFIG_WRITE_INDEX; cnl: ") << pHex(&buf[10],1) << F(", data: ") << pHex(&buf[12],(buf[0]-11));
} else if ((b_msgTp == 0x01) && (b_by11 == 0x09)) {
Serial << F("CONFIG_SERIAL_REQ");
} else if ((b_msgTp == 0x01) && (b_by11 == 0x0A)) {
Serial << F("PAIR_SERIAL, serial: ") << pHex(&buf[12],10);
} else if ((b_msgTp == 0x01) && (b_by11 == 0x0E)) {
Serial << F("CONFIG_STATUS_REQUEST, cnl: ") << pHex(&buf[10],1);
} else if ((b_msgTp == 0x02) && (b_by10 == 0x00)) {
if (b_len == 0x0A) Serial << F("ACK");
else Serial << F("ACK; data: ") << pHex(&buf[11],b_len-10);
} else if ((b_msgTp == 0x02) && (b_by10 == 0x01)) {
Serial << F("ACK_STATUS; cnl: ") << pHex(&buf[11],1) << F(", status: ") << pHex(&buf[12],1) << F(", down/up/loBat: ") << pHex(&buf[13],1);
if (b_len > 13) Serial << F(", rssi: ") << pHex(&buf[14],1);
} else if ((b_msgTp == 0x02) && (b_by10 == 0x02)) {
Serial << F("ACK2");
} else if ((b_msgTp == 0x02) && (b_by10 == 0x04)) {
Serial << F("ACK_PROC; para1: ") << pHex(&buf[11],2) << F(", para2: ") << pHex(&buf[13],2) << F(", para3: ") << pHex(&buf[15],2) << F(", para4: ") << pHex(&buf[17],1);
} else if ((b_msgTp == 0x02) && (b_by10 == 0x80)) {
Serial << F("NACK");
} else if ((b_msgTp == 0x02) && (b_by10 == 0x84)) {
Serial << F("NACK_TARGET_INVALID");
} else if ((b_msgTp == 0x03)) {
Serial << F("AES_REPLY; data: ") << pHex(&buf[10],b_len-9);
} else if ((b_msgTp == 0x04) && (b_by10 == 0x01)) {
Serial << F("TOpHMLAN:SEND_AES_CODE; cnl: ") << pHex(&buf[11],1);
} else if ((b_msgTp == 0x04)) {
Serial << F("TO_ACTOR:SEND_AES_CODE; code: ") << pHex(&buf[11],1);
} else if ((b_msgTp == 0x10) && (b_by10 == 0x00)) {
Serial << F("INFO_SERIAL; serial: ") << pHex(&buf[11],10);
} else if ((b_msgTp == 0x10) && (b_by10 == 0x01)) {
Serial << F("INFO_PEER_LIST; peer1: ") << pHex(&buf[11],4);
if (b_len >= 19) Serial << F(", peer2: ") << pHex(&buf[15],4);
if (b_len >= 23) Serial << F(", peer3: ") << pHex(&buf[19],4);
if (b_len >= 27) Serial << F(", peer4: ") << pHex(&buf[23],4);
} else if ((b_msgTp == 0x10) && (b_by10 == 0x02)) {
Serial << F("INFO_PARAM_RESPONSE_PAIRS; data: ") << pHex(&buf[11],b_len-10);
} else if ((b_msgTp == 0x10) && (b_by10 == 0x03)) {
Serial << F("INFO_PARAM_RESPONSE_SEQ; offset: ") << pHex(&buf[11],1) << F(", data: ") << pHex(&buf[12],b_len-11);
} else if ((b_msgTp == 0x10) && (b_by10 == 0x04)) {
Serial << F("INFO_PARAMETER_CHANGE; cnl: ") << pHex(&buf[11],1) << F(", peer: ") << pHex(&buf[12],4) << F(", pLst: ") << pHex(&buf[16],1) << F(", data: ") << pHex(&buf[17],b_len-16);
} else if ((b_msgTp == 0x10) && (b_by10 == 0x06)) {
Serial << F("INFO_ACTUATOR_STATUS; cnl: ") << pHex(&buf[11],1) << F(", status: ") << pHex(&buf[12],1) << F(", na: ") << pHex(&buf[13],1);
if (b_len > 13) Serial << F(", rssi: ") << pHex(&buf[14],1);
} else if ((b_msgTp == 0x11) && (b_by10 == 0x02)) {
Serial << F("SET; cnl: ") << pHex(&buf[11],1) << F(", value: ") << pHex(&buf[12],1) << F(", rampTime: ") << pHex(&buf[13],2) << F(", duration: ") << pHex(&buf[15],2);
} else if ((b_msgTp == 0x11) && (b_by10 == 0x03)) {
Serial << F("STOP_CHANGE; cnl: ") << pHex(&buf[11],1);
} else if ((b_msgTp == 0x11) && (b_by10 == 0x04) && (b_by11 == 0x00)) {
Serial << F("RESET");
} else if ((b_msgTp == 0x11) && (b_by10 == 0x80)) {
Serial << F("LED; cnl: ") << pHex(&buf[11],1) << F(", color: ") << pHex(&buf[12],1);
} else if ((b_msgTp == 0x11) && (b_by10 == 0x81) && (b_by11 == 0x00)) {
Serial << F("LED_ALL; Led1To16: ") << pHex(&buf[12],4);
} else if ((b_msgTp == 0x11) && (b_by10 == 0x81)) {
Serial << F("LED; cnl: ") << pHex(&buf[11],1) << F(", time: ") << pHex(&buf[12],1) << F(", speed: ") << pHex(&buf[13],1);
} else if ((b_msgTp == 0x11) && (b_by10 == 0x82)) {
Serial << F("SLEEPMODE; cnl: ") << pHex(&buf[11],1) << F(", mode: ") << pHex(&buf[12],1);
} else if ((b_msgTp == 0x12)) {
Serial << F("HAVE_DATA");
} else if ((b_msgTp == 0x3E)) {
Serial << F("SWITCH; dst: ") << pHex(&buf[10],3) << F(", na: ") << pHex(&buf[13],1) << F(", cnl: ") << pHex(&buf[14],1) << F(", counter: ") << pHex(&buf[15],1);