forked from i4things/NodeAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIoTThing.h
More file actions
1076 lines (935 loc) · 30.5 KB
/
Copy pathIoTThing.h
File metadata and controls
1076 lines (935 loc) · 30.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
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
/**
USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
THE PRODUCT.
<p/>
IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD B2N LTD., ITS
RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
DERIVED FROM THIS SOURCE CODE FILE.
*/
//class IoTThing
//{
// public:
//
// IoTThing(uint8_t slaveSelectPin_,
// uint8_t interruptPin_,
// uint8_t resetPin_,
//
// uint8_t key_[16],
// uint64_t id_,
// void (* receive_callback_)(uint8_t buf_[], uint8_t size, int16_t rssi) = NULL,
// uint8_t requencyRange = IoTThing_868,
// uint64_t gateway_id_ = IoTThing_OPEN_GATEWAY_ID);
//
// // call before using
// void init();
//
// // set id ( in case after start needs to be changed)
// void set_id(uint64_t id_ );
//
// // set key ( in case after start needs to be changed)
// void set_key(uint8_t key_[16]);
//
// // set gateway id ( in case after start needs to be changed)
// void set_gateway_id(uint64_t gateway_id_ );
//
// // register to receive callback after the message has been acknowledged from the gateway
// void register_ack(void (* ack_callback_)(int16_t rssi_));
//
// // register to receive callback after the message has failed/ in msec
// void register_timeout(void (* timeout_callback_)(uint16_t timeout_));
//
// // return signal strength in %
// uint8_t signal_strength();
//
// // return total messages sent
// uint32_t total_messages();
//
// //return total acknowledged messages send
// uint32_t ack_messages();
//
// //return received messages count
// uint32_t recv_messages();
//
// //return total retransmit messages send
// uint32_t retransmit_messages()
//
// //add discrete data
// //use this if you want for example to store decimal(floating-point) temperature between -20 and +60 in one byte
// // pos is the position from which the data will be written
// // e.g. : add_discrete(buf, 0, -20.0, 60.0, 23.65, 1);
// //you need to ensure that you at least have container_size bytes available in the buffer
// //1 <= conainer_size <= 4
// //on return pos will have the value of first free byte(next postion after the data)
// static void add_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, double value_, uint8_t container_size_);
//
// //read discrete value
// static double get_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, uint8_t container_size_);
//
// //add unsigned integer data up to 62bit ( 0 - 4611686018427387904) - the container size will be adjusted automatically depending on the value
// // pos is the position from which the data will be written
// //you need to ensure that you have at least 8 bytes available in the buffer - as this is the maximum bytes that the number can occupy in worst case scenario
// //on return pos will have the value of first free byte(next postion after the data)
// static void add_uint(uint8_t buf_[], uint8_t &pos_, uint64_t value_);
//
// // get unsigned integer value
// static uint64_t get_uint(uint8_t buf_[], uint8_t &pos_)
//
// // return false if a error is logged
// // max message size in bytes is 31
// bool send(uint8_t buf_[], uint8_t size_);
//
// // return true - if all previous tasks has been completed and ready to accept new data
// bool is_ready();
//
// // call if you want to stop retry's to send the message
// void cancel();
//
// // return true - if last message sending has hit timeout and failed
// bool timeout_hit();
//
// // please call in the main loop to be able to dispatch data and menage logic
// void work();
//};
#include <RH_RF95.h>
#include <SPI.h> // Not actualy used but needed to compile
#pragma push_macro("LOG64_ENABLED")
//#undef LOG64_ENABLED
class RH_RF95_IoTThing : public RH_RF95
{
public:
RH_RF95_IoTThing(uint8_t slaveSelectPin = SS, uint8_t interruptPin = 2, RHGenericSPI& spi = hardware_spi) : RH_RF95(slaveSelectPin, interruptPin, spi)
{
}
bool isChannelActive()
{
_curRssi = spiRead(RH_RF95_REG_2C_RSSI_WIDEBAND);
if (_curHFport)
_curRssi -= 157;
else
_curRssi -= 164;
return (_curRssi > -128);
}
bool setFrequency(float centre)
{
_curHFport = (centre >= 779.0);
return RH_RF95::setFrequency(centre);
}
protected :
int8_t _curSNR;
int16_t _curRssi;
bool _curHFport;
};
#define IoTThing_433 0
#define IoTThing_868 1
#define IoTThing_915 2
#define IoTThing_FREQ_CNT 3
#define IoTThing_TIMEOUT_FREQ_CHG_COUNT 4
#define IoTThing_MIN_RSSI (-150)
#define IoTThing_MAX_MESSAGE_LEN 31
#define IoTThing_ACK_MESSAGE_LEN 7
#define IoTThing_MESSAGE_TIMEOUT 60000
#define IoTThing_ACK_TIMEOUT 400
#define IoTThing_MAX_ACK_TIMEOUT 5000
#define IoTThing_MAGIC 8606
#define IoTThing_OPEN_GATEWAY_ID 10
class IoTThing
{
public:
IoTThing(uint8_t slaveSelectPin_,
uint8_t interruptPin_,
uint8_t resetPin_,
uint8_t key_[16],
uint64_t id_,
void (* receive_callback_)(uint8_t buf_[], uint8_t size, int16_t rssi) = NULL,
uint8_t requencyRange = IoTThing_868,
uint64_t gateway_id_ = IoTThing_OPEN_GATEWAY_ID) :
resetPin(resetPin_),
driver(RH_RF95_IoTThing(slaveSelectPin_, interruptPin_)),
id(id_),
ack_callback(NULL),
timeout_callback(NULL),
receive_callback(receive_callback_),
freq_idx(0),
gateway_id(gateway_id_)
{
memcpy(key, key_, 16);
randomSeed(analogRead(0));
switch (requencyRange)
{
case 0 : all_freq[0] = 433.1f; all_freq[1] = 433.3f, all_freq[2] = 433.5f; break;
case 1 : all_freq[0] = 868.1f; all_freq[1] = 868.3f, all_freq[2] = 868.5f; break;
case 2 : all_freq[0] = 915.1f; all_freq[1] = 915.3f, all_freq[2] = 915.5f; break;
default : all_freq[0] = 868.1f; all_freq[1] = 868.3f, all_freq[2] = 868.5f; break;
}
rndFrequency();
}
public:
// call before using
void init()
{
timeout = false;
last = 0;
total = 0;
total_ack = 0;
total_recv = 0;
total_retransmit = 0;
rssi = IoTThing_MIN_RSSI;
active_start = 0;
active_start_a = 0;
timeout_count = 0;
seq = random(126) + 1; // as * -1 is used for ack
a = NULL;
q = NULL;
ack = NULL;
start_ack = 0;
timeout_ack = 0;
buf_size = 0;
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
// manual reset
digitalWrite(resetPin, LOW);
delay(10);
digitalWrite(resetPin, HIGH);
delay(10);
if (!driver.init())
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: INIT FAILED"));
LOG64_NEW_LINE;
#endif
}
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: FREQ : "));
LOG64_SET(String(getFrequency(), 2));
LOG64_NEW_LINE;
#endif
if (!driver.setFrequency(getFrequency()))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: FREQ FAILED"));
LOG64_SET(getFrequency());
LOG64_NEW_LINE;
#endif
}
// max power
driver.setTxPower(23, false);
driver.available(); // turns the driver ON
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: INIT"));
LOG64_NEW_LINE;
#endif
}
// set id ( in case after start needs to be changed)
void set_id(uint64_t id_ )
{
id = id_;
}
// set key ( in case after start needs to be changed)
void set_key(uint8_t key_[16])
{
memcpy(key, key_, 16);
}
// set gateway id ( in case after start needs to be changed)
void set_gateway_id(uint64_t gateway_id_ )
{
gateway_id = gateway_id_;
}
// register for to receive callback after the message has been acknowledged from the gateway
void register_ack(void (* ack_callback_)(int16_t rssi))
{
ack_callback = ack_callback_;
}
// register to receive callback after the message has failed/ in msec
void register_timeout(void (* timeout_callback_)(uint16_t timeout))
{
timeout_callback = timeout_callback_;
}
public:
// return signal strength in %
uint8_t signal_strength()
{
int16_t rssi_ = ((rssi - IoTThing_MIN_RSSI) * 100) / (IoTThing_MIN_RSSI * (-1));
if (rssi_ < 0)
{
rssi_ = 0;
}
else if (rssi_ > 100)
{
rssi_ = 100;
}
return rssi_;
}
// return total messages sent
uint32_t total_messages()
{
return total;
}
//return total acknowledged messages send
uint32_t ack_messages()
{
return total_ack;
}
//return received messages count for the gateway
uint32_t recv_messages()
{
return total_recv;
}
//return total acknowledged messages send
uint32_t retransmit_messages()
{
return total_retransmit;
}
public:
//add discrete data
//use this if you want for example to store decimal(floating-point) temperature between -20 and +60 in one byte
// pos is the position from which the data will be written
// e.g. : add_discrete(buf, 0, -20.0, 60.0, 23.65, 1);
//you need to ensure that you at least have container_size bytes available in the buffer
//1 <= conainer_size <= 4
//on return pos will have the value of first free byte(next postion after the data)
static void add_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, double value_, uint8_t container_size_)
{
double dev_;
switch (container_size_)
{
case 1 : {
dev_ = (max_ - min_) / 255.0d;
} ; break;
case 2 : {
dev_ = (max_ - min_) / 65535.0d;
} ; break;
case 3 : {
dev_ = (max_ - min_) / 16777215.0d;
} ; break;
case 4 : {
dev_ = (max_ - min_) / 4294967295.0d;
} ; break;
default : dev_ = (max_ - min_) / 255.0d;
};
uint32_t dis_ = (uint32_t)round((value_ - min_) / dev_);
memcpy((void *)&buf_[pos_], (void *) &dis_, container_size_);
pos_ += container_size_;
}
static double get_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, uint8_t container_size_)
{
double dev_;
switch (container_size_)
{
case 1 : {
dev_ = (max_ - min_) / 255.0d;
} ; break;
case 2 : {
dev_ = (max_ - min_) / 65535.0d;
} ; break;
case 3 : {
dev_ = (max_ - min_) / 16777215.0d;
} ; break;
case 4 : {
dev_ = (max_ - min_) / 4294967295.0d;
} ; break;
default : dev_ = (max_ - min_) / 255.0d;
};
uint32_t c = 0;
memcpy((void *) &c, (void *) &buf_[pos_], container_size_);
pos_ += container_size_;
return (min_ + (((double)c) * dev_));
}
//add unsigned integer data up to 62bit ( 0 - 4611686018427387904) - the container will be adjusted automatically depending on the value
//you need to ensure that you have at least 8 bytes available in the buffer - as this is the maximum bytes that the number can occupy in worst case scenario
static void add_uint(uint8_t buf_[], uint8_t &pos_, uint64_t value_)
{
put_ots(value_, buf_, pos_);
}
static uint64_t get_uint(uint8_t buf_[], uint8_t &pos_)
{
return get_ots(buf_, pos_);
}
public:
// return false if a error is logged
// format byte(len), byte(crc), byte(seq), byte[4](receiver_id), byte[4](sender_id),byte[](encrypted_payload)
// seq <=+127 - 0 is not valid sequence
// payload max size 31
bool send(uint8_t buf_[], uint8_t size_)
{
if ((q != NULL) || (ack != NULL))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: NO SPACE FOR MSG."));
LOG64_NEW_LINE;
#endif
return false;
}
if ((size_ >= driver.maxMessageLength()) || (size_ > (IoTThing_MAX_MESSAGE_LEN)))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: MSG TOO BIG["));
LOG64_SET(size_);
LOG64_SET(F("]"));
LOG64_NEW_LINE;
#endif
return false;
}
uint8_t *enc;
size_t enc_size;
enc = (uint8_t *) xxtea_encrypt(buf_, size_, key, &enc_size);
uint8_t raw[IoTThing_MAX_MESSAGE_LEN + 1 + 18];
uint8_t pos_;
//raw[0] = CRC
raw[1] = seq;
pos_ = 2;
put_ots(gateway_id, raw, pos_ );
put_ots(id, raw, pos_ );
memcpy(&raw[pos_], enc , enc_size);
free(enc);
pos_ += enc_size;
raw[0] = crc(raw, pos_, 1);
memcpy(buf, raw , pos_);
buf_size = pos_;
q = buf;
last = millis();
timeout = false;
return true;
}
// return true - if all previouse tasks has been completed and ready to accept new data
bool is_ready()
{
return ((q == NULL) && (ack == NULL));
}
// return true - if last message sending has hit timeout and failed
bool timeout_hit()
{
return timeout;
}
// call if you want to stop retry's to send the message
void cancel()
{
if ((q == NULL) && (ack != NULL))
{
last = 0;
timeout = true;
seq = 1; // as * -1 is used for ack
q = NULL;
ack = NULL;
start_ack = 0;
timeout_ack = 0;
buf_size = 0;
timeout_count = 0;
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: MESSAGE CANCELED"));
LOG64_NEW_LINE;
#endif
}
}
public :
// please call in the main loop to be able to dispatch data and menage logic
void work()
{
if (a == NULL)
{
uint8_t buf_[IoTThing_MAX_MESSAGE_LEN + 18 + 1];
uint8_t size_ = IoTThing_MAX_MESSAGE_LEN + 18 + 1;
if (driver.recv(buf_, &size_))
{
if (size_ <= (IoTThing_MAX_MESSAGE_LEN + 18 + 1) )
{
if (crc_chk(buf_, size_))
{
if (get_id_to(buf_, 2) == id)
{
rssi = driver.lastRssi();
//possible ack
int8_t msg_seq = get_seq(buf_, 1);
if (msg_seq < 0)
{
if (ack != NULL)
{
// ack
total_ack++;
if ((++seq) > 126)
{
seq = 1; // as * -1 is used for ack
}
ack = NULL;
last = 0;
buf_size = 0;
timeout_count = 0;
if (ack_callback != NULL)
{
ack_callback(rssi);
}
}
}
else
{
total_recv++;
get_ack(buf_, buf_a, buf_a_size);
a = buf_a;
if (receive_callback != NULL)
{
//crc
//seq
uint8_t sz = 2;
get_ots(buf_, sz); // target
get_ots(buf_, sz); // source
size_t out_size = 0;
uint8_t * dec = xxtea_decrypt(&buf_[sz], size_ - sz, key, & out_size);
if ((dec != NULL) && (out_size > 0) && (dec[0] == crc(dec, (uint16_t)out_size, 1)))
{
receive_callback(&dec[1], (uint8_t)(out_size - 1), rssi);
}
else
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: CRC CHECK FOR RECEIVED MSG FAILED"));
LOG64_NEW_LINE;
#endif
}
if (dec != NULL)
{
free(dec);
}
}
}
}
}
else
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: CRC CHECK FAILED"));
LOG64_NEW_LINE;
#endif
}
}
}
}
if (a != NULL)
{
// we really have something for dispatch
bool active = driver.isChannelActive();
if (active)
{
if (active_start_a == 0)
{
// clear low priority dispatch if waiting for free channel
active_start = 0;
//start the waiting
active_start_a = millis();
wait_timeout = random(IoTThing_ACK_TIMEOUT, 2 * IoTThing_ACK_TIMEOUT);
}
else
{
if (((uint32_t)(((uint32_t)millis()) - active_start_a)) >= wait_timeout);
{
active = false;
#if defined (LOG64_ENABLED)
LOG64_SET("IoTT : ACK : ACTIVE TIMEOUT HIT");
LOG64_NEW_LINE;
#endif
}
}
}
if (!active)
{
send_to_driver(buf_a, buf_a_size);
active_start_a = 0;
a = NULL;
}
}
else if (q != NULL)
{
bool active = driver.isChannelActive();
if (active)
{
if (active_start == 0)
{
//start the waiting
active_start = millis();
wait_timeout = random(IoTThing_ACK_TIMEOUT, 2 * IoTThing_ACK_TIMEOUT);
}
else
{
if (((uint32_t)(((uint32_t)millis()) - active_start)) >= wait_timeout);
{
active = false;
#if defined (LOG64_ENABLED)
LOG64_SET("IoTT : ACTIVE TIMEOUT HIT");
LOG64_NEW_LINE;
#endif
}
}
}
if (!active)
{
send_to_driver(buf, buf_size);
total++;
ack = q;
q = NULL;
start_ack = millis();
uint32_t timeout_divider = IoTThing_MAX_ACK_TIMEOUT / IoTThing_ACK_TIMEOUT;
timeout_ack = (((uint8_t)random(timeout_divider)) * IoTThing_ACK_TIMEOUT) + (IoTThing_ACK_TIMEOUT * 3) + ((uint32_t)random(100));
active_start = 0;
}
}
if ((q == NULL) && (ack != NULL) && (((uint32_t)(((uint32_t)millis()) - start_ack)) > timeout_ack))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: RETRANSMIT"));
LOG64_NEW_LINE;
#endif
if ((++timeout_count) >= IoTThing_TIMEOUT_FREQ_CHG_COUNT)
{
timeout_count = 0;
changeFrequency();
if (!driver.setFrequency(getFrequency()))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: FREQ FAILED"));
LOG64_SET(getFrequency());
LOG64_NEW_LINE;
#endif
}
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: FREQ : "));
LOG64_SET(String(getFrequency(), 2));
LOG64_NEW_LINE;
#endif
}
total_retransmit++;
q = ack;
ack = NULL;
rssi = IoTThing_MIN_RSSI;
}
if (((q == NULL) && (ack != NULL)) && (((uint32_t)(((uint32_t)millis()) - last)) > IoTThing_MESSAGE_TIMEOUT))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: TIMEOUT"));
LOG64_NEW_LINE;
#endif
cancel();
if (timeout_callback != NULL)
{
timeout_callback(IoTThing_MESSAGE_TIMEOUT);
}
}
};
private :
void rndFrequency()
{
for (uint8_t i = 0; i < (IoTThing_FREQ_CNT - 1); i++)
{
uint8_t x = random(i, IoTThing_FREQ_CNT );
uint8_t t = rnd_freq_idx[i];
rnd_freq_idx[i] = rnd_freq_idx[x];
rnd_freq_idx[x] = t;
}
}
float getFrequency()
{
return all_freq[rnd_freq_idx[freq_idx]];
}
void changeFrequency()
{
if ((++freq_idx) == IoTThing_FREQ_CNT)
{
rndFrequency();
freq_idx = 0;
}
}
private:
// calculate checksum
uint8_t crc(uint8_t buf_[],
uint16_t end_index_,
uint16_t start_index)
{
uint32_t res = IoTThing_MAGIC; // add magic
for (uint16_t i = start_index; i < end_index_; i++)
{
uint8_t c = buf_[i];
res = (res << 1) ^ c;
res = res & 0xFFFFFFFF;
}
return (uint8_t)(res & 0xFF);
}
// check message checksum
bool crc_chk(uint8_t buf_[],
uint8_t size_)
{
return (buf_[0] == crc(buf_, size_, 1));
}
// extract the sequence from a message
int8_t get_seq(uint8_t buf_[], uint8_t pos)
{
return (int8_t)buf_[pos];
}
// extract the id to from a message
uint64_t get_id_to(uint8_t buf_[], uint8_t pos_)
{
return get_ots(buf_, pos_);
}
// format byte(len), byte(crc), byte(seq), byte[4](receiver_id)
// len 7
// seq >=-127 - 0 is not valid sequence
void get_ack(uint8_t buf_[], uint8_t buf_ack[], uint8_t & ack_size)
{
uint8_t pos_;
// read
pos_ = 2;
uint64_t to_id = get_ots(buf_, pos_);
uint64_t from_id = get_ots(buf_, pos_);
// write
//buf_ack[0] = crc;
buf_ack[1] = (uint8_t)(((int8_t)buf_[1]) * ((int8_t) - 1));
pos_ = 2;
put_ots(from_id, buf_ack, pos_ );
buf_ack[0] = crc(buf_ack, pos_ , 1);
ack_size = pos_;
}
// send message to driver
void send_to_driver(uint8_t buf_[], uint8_t size_)
{
if (!driver.send(buf_, size_))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: SEND FAILED"));
LOG64_NEW_LINE;
#endif
}
else if (!driver.waitPacketSent(IoTThing_ACK_TIMEOUT))
{
// need reinit
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: DRIVER BLOCKED : RESET"));
LOG64_NEW_LINE;
#endif
init();
return;
}
}
private:
static uint64_t get_ots(uint8_t buf_[], uint8_t & pos_)
{
uint8_t start_ = pos_;
switch (buf_[start_] & 0x3)
{
case 0:
{
pos_ += 1;
return (uint64_t) ((buf_[start_] >> 2) & 0x3F);
}
case 1:
{
pos_ += 2;
return (uint64_t)((((buf_[start_ + 1] << 8) | (buf_[start_])) >> 2) & 0x3FFF);
}
case 2:
{
pos_ += 4;
return (uint64_t)((((((uint32_t)buf_[start_ + 3]) << 24) | (((uint32_t)buf_[start_ + 2]) << 16) | (((uint32_t)buf_[start_ + 1]) << 8) | ((uint32_t)buf_[start_])) >> 2) & 0x3FFFFFFFL);
}
case 3:
{
pos_ += 8;
return (((((uint64_t)buf_[start_ + 7]) << 56) | (((uint64_t)buf_[start_ + 6]) << 48) | (((uint64_t)buf_[start_ + 5]) << 40) | (((uint64_t)buf_[start_ + 4]) >> 32) | (((uint64_t)buf_[start_ + 3]) << 24)
| (((uint64_t)buf_[start_ + 2]) << 16) | (((uint64_t)buf_[start_ + 1]) << 8) | ((uint64_t)buf_[start_])) >> 2) & 0x3FFFFFFFFFFFFFFFLL;
}
}
}
static void put_ots(uint64_t v, uint8_t buf_[], uint8_t & pos_ )
{
v = (v << 2);
uint8_t start_ = pos_;
if (v < 0x40LL)
{
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 1;
}
else if (v < 0x4000LL)
{
v = v | 1;
buf_[start_ + 1] = (uint8_t)((v >> 8) & 0xFFLL);
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 2;
}
else if (v < 0x40000000LL)
{
v = v | 2;
buf_[start_ + 3] = (uint8_t)((v >> 24) & 0xFFLL);
buf_[start_ + 2] = (uint8_t)((v >> 16) & 0xFFLL);
buf_[start_ + 1] = (uint8_t)((v >> 8) & 0xFFLL);
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 4;
}
else if (v < 0x4000000000000000LL)
{
v = v | 3;
buf_[start_ + 7] = (uint8_t)((v >> 56) & 0xFFLL);
buf_[start_ + 6] = (uint8_t)((v >> 48) & 0xFFLL);
buf_[start_ + 5] = (uint8_t)((v >> 40) & 0xFFLL);
buf_[start_ + 4] = (uint8_t)((v >> 32) & 0xFFLL);
buf_[start_ + 3] = (uint8_t)((v >> 24) & 0xFFLL);
buf_[start_ + 2] = (uint8_t)((v >> 16) & 0xFFLL);
buf_[start_ + 1] = (uint8_t)((v >> 8) & 0xFFLL);
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 8;
}
}
private :
uint8_t buf[IoTThing_MAX_MESSAGE_LEN + 18 + 1];
uint8_t buf_size;
uint8_t buf_a[IoTThing_MAX_MESSAGE_LEN + 18 + 1];
uint8_t buf_a_size;
int8_t seq; // as * -1 is used for ack
uint8_t * a;
uint8_t * q; // used as flaf
uint8_t * ack; // used as flag
uint32_t start_ack;
uint32_t timeout_ack;
uint32_t total;
uint32_t total_ack;
uint32_t total_recv;
uint32_t total_retransmit;
bool timeout;
uint8_t resetPin;
uint32_t last;
uint8_t __attribute__ ((aligned (4))) key[16];
uint64_t id;
RH_RF95_IoTThing driver;
void (* ack_callback)(int16_t rssi_);
void (* timeout_callback)(uint16_t timeout_);
int16_t rssi;
uint32_t active_start;
uint32_t active_start_a;
uint32_t wait_timeout;
uint8_t freq_idx;
float all_freq[IoTThing_FREQ_CNT];
uint8_t rnd_freq_idx[IoTThing_FREQ_CNT] = { 0 , 1, 2 };
uint8_t timeout_count;
uint64_t gateway_id;
void (* receive_callback)(uint8_t buf_[], uint8_t size_, int16_t rssi_);
private :
/**********************************************************\
| |
| XXTEA encryption algorithm library for C. |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Authors: Chen fei <cf850118@163.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Feb 7, 2016 |
| |
| With changes |
| |
| License (MIT) |
| |
\**********************************************************/
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define DELTA 0x9e3779b9
uint32_t * xxtea_to_uint_array_size(const uint8_t * data, size_t len, size_t * out_len) {
uint32_t *out;
++len;
size_t n = (((len & 3) == 0) ? (len >> 2) : ((len >> 2) + 1));
if (n < 2) n = 2;
--len;
out = (uint32_t *)malloc(n << 2);
if (!out) return NULL;
*out_len = n;
memcpy(&((uint8_t *)out)[1], data, len);
((uint8_t *)out)[0] = (uint8_t)len;
return out;
}
uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, size_t * out_len) {
uint32_t *out;
out = (uint32_t *)malloc(len);
memcpy(out, data, len);
*out_len = len >> 2;
return out;
}
uint8_t * xxtea_to_ubyte_array_size(const uint32_t * data, size_t len, size_t * out_len) {
uint8_t *out;
size_t n;
n = ((uint8_t *)data)[0];
out = (uint8_t *)malloc(n);
memcpy(out, &((uint8_t *)data)[1], n);
*out_len = n;
return out;
}
uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, size_t * out_len) {
uint8_t *out;
size_t n;
n = len << 2;
out = (uint8_t *)malloc(n);
memcpy(out, data, n);
*out_len = n;
return out;
}
uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key)
{
uint32_t n = (uint32_t)len - 1;
uint32_t z, y = data[0], p, q = 6 + 52 / (n + 1), sum = q * DELTA, e;
if (n < 1) return data;
while (sum != 0)
{
e = sum >> 2 & 3;
for (p = n; p > 0; p--)
{
z = data[p - 1];
y = data[p] -= MX;
}