-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSensor.cpp
887 lines (757 loc) · 25.5 KB
/
Sensor.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
/* ===================================================
Sensor.cpp
* ====================================================
* Sensor decoding from 433 Message
*
* Created on: 17 sept. 2013
* Author: disk91 / (c) http://www.disk91.com
*
* added comme sensor Oregon
* to decode
* ===================================================
*/
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <sstream>
#include <iostream>
#include "Sensor.h"
//#define SENSORDEBUG // Large debug trace
//#define SENSORTRACE // Small debug trace to verify error only
const char OregonSensorV2::_sensorId[] = "OSV2";
const char OregonSensorV3::_sensorId[] = "OSV3";
// ——————————————————
// Construction – init variable then call decode function
Sensor::Sensor( ) :
_temperature(0),
_humidity(0),
_rain(0),
_train(0),
_direction(-1),
_speed(0),
_pressure(0),
_channel(-1),
_sensorClass(SENS_CLASS_NONE),
_sensorType(-1),
_availableInfos(0)
// time(&creationTime);
{
}
// —————————————————
// availablePressure() – return true if valid && have wind speed
bool Sensor::availablePressure() const {
return (_availableInfos.isFlagsSet(isValid|havePressure));
}
// —————————————————
// availableSpeed() – return true if valid && have wind speed
bool Sensor::availableSpeed() const{
return (_availableInfos.isFlagsSet(isValid|haveSpeed));
}
// —————————————————
// availableDirection() – return true if valid && have wind direction
bool Sensor::availableDirection() const{
return (_availableInfos.isFlagsSet(isValid|haveDirection));
}
// —————————————————
// availableRain() – return true if valid && have Rain
bool Sensor::availableRain() const{
return (_availableInfos.isFlagsSet(isValid|haveRain));
}
// —————————————————
// availableTemp() – return true if valid && have Temp
bool Sensor::availableTemp() const{
return (_availableInfos.isFlagsSet(isValid|haveTemperature));
}
// —————————————————
// availableHumidity() – return true if valid && have Humidity
bool Sensor::availableHumidity() const{
return (_availableInfos.isFlagsSet(isValid|haveHumidity));
}
// —————————————————
// isBatteryLow() – return true if valid && haveBattery && flag set
bool Sensor::isBatteryLow() const{
return (_availableInfos.isFlagsSet(isValid|haveBattery|battery));
}
// —————————————————
// getPressure() – return Wind speed in Mb
double Sensor::getPressure() const{
return _pressure;
}
// —————————————————
// getSpeed() – return Wind speed in Degre°
double Sensor::getSpeed() const{
return _speed;
}
// —————————————————
// getDirection() – return Wind direction in Degre°
double Sensor::getDirection() const{
return _direction;
}
// —————————————————
// getRain() – return Rain in mm/h
double Sensor::getRain() const{
return _rain;
}
// —————————————————
// getTemperature() – return temperature in C°
double Sensor::getTemperature() const{
return _temperature;
}
// —————————————————
// getHumidity() – return humidity in % (base 100)
double Sensor::getHumidity() const{
return _humidity;
}
// —————————————————
// haveChannel() – return true if valid && haveChannel
bool Sensor::hasChannel() const{
return (_availableInfos.isFlagsSet(isValid|haveChannel));
}
// —————————————————
// isDecoded() – return true if valid
bool Sensor::isDecoded() const{
return (_availableInfos.isFlagsSet(isValid));
}
// —————————————————
// getChannel() – return channel (1,2,3)
int Sensor::getChannel() const {
return _channel;
}
// —————————————————
// getCreationTime() – return object creation time
//time_t Sensor::getCreationTime() {
// return creationTime;
// }
// —————————————————
// getSensClass() – return sensor class
int Sensor::getSensClass() const {
return _sensorClass;
}
// —————————————————
// getSensType() – return sensor type
int Sensor::getSensType() const {
return _sensorType;
}
// —————————————————
// getSensorName() – return sensor name
const std::string& Sensor::getSensorName() const {
return _sensorName;
}
// ---------------------------------------------------
// getIntFromChar() - get int from hex char(-1 if error)
int Sensor::getIntFromChar(char c) const {
std::stringstream ss;
ss << std::hex << c;
int i;
ss >> i;
if(!ss.fail())
return i;
return -1;
}
// ---------------------------------------------------
// getIntFromString() - get an unsigned int value from
// the given string. -1 if error
int Sensor::getIntFromString(char * s) const {
std::stringstream ss;
ss << std::hex << s;
int i;
ss >> i;
if(!ss.fail())
return i;
return -1;
}
// --------------------------------------------------
// getDoubleFromString() - get double value related to
// BCD encoded string XXX = XX.X
double Sensor::getDoubleFromString(char * s) const {
std::stringstream ss;
ss << std::dec << s;
int r = -1;
ss >> r;
if(!ss.fail())
return (r / 10.0);
return -1.;
}
/*Fin copiado de original*/
// ==================================================================
// ——————————————————
// Build the right sensor type by identifying the
// header code
Sensor * Sensor::getRightSensor(char * s) {
int len = strlen(s);
if (len > 4) {
#ifdef SENSORDEBUG
printf("Sensor::getRightSensor – create of (%s)\n",s);
#endif
if(strncmp(s, OregonSensorV2::_sensorId, strlen(OregonSensorV2::_sensorId)) == 0) {
#ifdef SENSORDEBUG
printf("Sensor::getRightSensor – create OregonSensorV2\n");
#endif
return new OregonSensorV2(s);
} else if ( strncmp(s, OregonSensorV3::_sensorId, strlen(OregonSensorV3::_sensorId)) == 0) {
#ifdef SENSORDEBUG
printf("Sensor::getRightSensor – create OregonSensorV3\n");
#endif
return new OregonSensorV3(s);
}
#ifdef SENSORDEBUG
else {
std::cout << "[Sensor::getRightSensor] right length but unknown signature:" << s << std::endl;
}
#endif
} else {
std::cout << "[Sensor::getRightSensor] dont know how to decode: " << s << std::endl;
}
return NULL;
}
// ==================================================================
OregonSensorV2::OregonSensorV2(char * _strval) : Sensor( ) {
_sensorClass = SENS_CLASS_OS;
_availableInfos.setFlags(decode(_strval));
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
//
// Support :
// – THGR122NX : Temp + Humidity
// OSV2 1A2D1002 502060552A4C
// In correct order :
// OSV2 A 1D20 1 20 0 502 0 655 2A 4C
// OSV2 => decoding header
// A => Sync header
// 1D20 => THGR122NX ID
// – THN132N : Temp
// OSV2 EA4C20809822D013
// In correct order :
// OSV2 A EC40 2 08 8 922 D0 13
//
// ——————————————————————————————
bool OregonSensorV2::decode(char * _str) {
char * pt = & _str[5];
int len = strlen(_str);
char sensorId[5]; int isensorId;
// Proceed the right sensor
if (len > 11) {
sensorId[0] = pt[0];sensorId[1] = pt[3];sensorId[2] = pt[2];sensorId[3] = pt[5];sensorId[4] ='\0';
isensorId = getIntFromString(sensorId);
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(%s)(0x%4X)\n",sensorId, isensorId);
#endif
switch (isensorId) {
case 0x5D60:
_sensorType=0x5D60;
_sensorName = "BTHG968";
return decode_BTHG968(pt); break;
case 0x1D20:
_sensorType=0x1D20;
_sensorName = "THGR122NX";
return decode_THGR122NX(pt); break;
case 0x1D30:
_sensorType=0x1D30;
_sensorName="THGRN228NX";
return decode_THGRN228NX(pt); break;
case 0xEC40:
_sensorType=0xEC40;
_sensorName = "THN132N";
return decode_THN132N(pt); break;
case 0x3D00:
_sensorType=0x3D00;
_sensorName = "WGR918";
return decode_WGR918(pt); break;
case 0x2D10:
_sensorType=0x2D10;
_sensorName = "RGR918";
return decode_RGR918(pt); break;
default:
std::cout << "Unknown sensor id: " << std::hex << isensorId << std::endl;
return false;
break;
}
}
else {
std::cout << "OSV2 - decode: bad length" << std::endl;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
// – BTHG968 temperature + hygro + pression atmospherique
// ——————————————————————————————
bool OregonSensorV2::decode_BTHG968(char * pt) {
char temp[4]; double dtemp; // Temp in BCD
char tempS; int itempS; // Sign 0 = positif
char humid[4]; double dhumid; // Humid in BCD
char pressure[3]; double ipressure;
char crc[3];
int icrc;
int len = strlen(pt);
if ( len == 20 ) {
temp[0] = pt[10] ; temp[1] = pt[11] ; temp[2] = pt[8] ; temp[3] ='\0';
tempS = pt[13];
humid[0] = pt[15] ; humid[1] = pt[12]; humid[2] = '0' ; humid[3] ='\0';
pressure[0] = pt[16];pressure[1]=pt[17];pressure[2]='\0';
crc[0] = pt[18] ; crc[1] = pt[19] ; crc[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(%s) temp(%s) sign(%c) humid(%s) pressure(%s) crc(%s)\n", "5D60",temp,tempS,humid, pressure, crc);
#endif
// Conversion to int value
itempS = getIntFromChar(tempS) & 0x08;
icrc = getIntFromString(crc);
dtemp = getDoubleFromString(temp);
dhumid = getDoubleFromString(humid);
ipressure = getIntFromString(pressure);
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(0x%04X) temp(%f) sign(%d) humid(%d) pressure(%d) cksum(0x%02X) crc(0x%02X)\n", 0x5D60,dtemp,itempS,dhumid, ipressure, icrc);
#endif
// Check SUM & CRC
// if ( validate(pt,16,icrc,ichecksum) == true ) {
// now we can decode the important flag and fill the object
_availableInfos.setFlags(haveTemperature);
_temperature = (itempS == 0)?dtemp:-dtemp;
_availableInfos.setFlags(haveHumidity);
_humidity = dhumid;
_availableInfos.setFlags(havePressure);
_pressure = (856 + ipressure);
return true;
// } else return false;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
// – RGR918 PLUVIOMETRE
// ——————————————————————————————
bool OregonSensorV2::decode_RGR918(char * pt) {
char rain[4]; double irain; // Temp
char checksum[19]; int ichecksum;
char crc[3]; int icrc;
int len = strlen(pt);
if ( len == 20 ) {
//channel = pt[4];
rain[0] = pt[10] ; rain[1] = pt[11] ; rain[2] = pt[8] ; rain[3] ='\0';
checksum[0] = pt[18]; checksum[1] = pt[19]; checksum[2] ='\0';
crc[0] = pt[18] ; crc[1] = pt[19] ; crc[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV2 – decode raw: id(%s) rain(%s) cksum(%s) crc(%s)\n", "2D10",rain,checksum,crc);
#endif
// Conversion to int value
ichecksum = getIntFromString(checksum);
irain = getDoubleFromString(rain);
icrc = getIntFromString(crc);
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(0x%04X) rain(%f) cksum(0x%02X) crc(0x%02X) \n", 0x2D10,irain,ichecksum,icrc);
#endif
// Check SUM & CRC
// if ( validate(pt,16,icrc,ichecksum) == true ) {
// now we can decode the important flag and fill the object
_availableInfos.setFlags(haveRain);
_rain = (10 * irain);
return true;
// } else return false;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
// – WGR918 ANEMOMETER
// ——————————————————————————————
bool OregonSensorV2::decode_WGR918(char * pt) {
char dir[4]; double idir; // direction en degres
char speed[4]; double ispeed; // vitesse en km/h
char checksum[3]; int ichecksum;
char crc[3]; int icrc;
int len = strlen(pt);
if ( len == 20 ) {
dir[0] = pt[10] ; dir[1] = pt[11] ; dir[2] = pt[8] ; dir[3] ='\0';
speed[0] = pt[12] ; speed[1] = pt[13]; speed[2] = pt[14]; speed[3] ='\0';
checksum[0] = pt[16]; checksum[1] = pt[17]; checksum[2] ='\0';
crc[0] = pt[18] ; crc[1] = pt[19] ; crc[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(%s) dir(%s) speed(%s) cksum(%s) crc(%s)\n", "3D00",dir,speed, checksum, crc);
#endif
// Conversion to int value
ichecksum = getIntFromString(checksum);
icrc = getIntFromString(crc);
idir = getDoubleFromString(dir);
ispeed = getDoubleFromString(speed);
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(0x%04X) dir(%f) speed(%f) cksum(0x%02X) crc(0x%02X)\n",0x3D00,idir,ispeed, ichecksum,icrc);
#endif
// Check SUM & CRC
// if ( validate(pt,16,icrc,ichecksum) == true ) {
// now we can decode the important flag and fill the object
_availableInfos.setFlags(haveDirection);
_direction = (10 * idir);
_availableInfos.setFlags(haveSpeed);
_speed = (0.1 * ispeed)* 3.6;
return true;
// } else return false;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
// – THGRN228NX : Temp + Humidity
// ——————————————————————————————
bool OregonSensorV2::decode_THGRN228NX(char * pt) {
char channel; int ichannel; // values 1,2,4
char rolling[3]; int irolling;
char battery; int ibattery; // value & 0x4
char temp[4]; double dtemp; // Temp in BCD
char tempS; int itempS; // Sign 0 = positif
char humid[4]; double dhumid; // Humid in BCD
char checksum[3]; int ichecksum;
char crc[3]; int icrc;
int len = strlen(pt);
if ( len == 20 ) {
channel = pt[4];
rolling[0]=pt[7]; rolling[1]=pt[6]; rolling[2]='\0';
battery = pt[9];
temp[0] = pt[10] ; temp[1] = pt[11] ; temp[2] = pt[8] ; temp[3] ='\0';
tempS = pt[13];
humid[0] = pt[15] ; humid[1] = pt[12]; humid[2] = '0' ; humid[3] ='\0';
checksum[0] = pt[16]; checksum[1] = pt[17]; checksum[2] ='\0';
crc[0] = pt[18] ; crc[1] = pt[19] ; crc[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(%s) ch(%c) bat(%c) temp(%s) sign(%c) humid(%s) cksum(%s) crc(%s)\n","1D30",channel,battery,temp,tempS,humid, checksum, crc);
#endif
// Conversion to int value
ichannel = getIntFromChar(channel);
irolling = getIntFromString(rolling);
ibattery = getIntFromChar(battery);
itempS = getIntFromChar(tempS) & 0x08;
ichecksum = getIntFromString(checksum);
icrc = getIntFromString(crc);
dtemp = getDoubleFromString(temp);
dhumid = getDoubleFromString(humid);
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(0x%04X) ch(%d) bat(%d) temp(%f) sign(%d) humid(%f) cksum(0x%02X) crc(0x%02X)\n",0x1D30,ichannel,ibattery,dtemp,itempS,dhumid, ichecksum, icrc);
#endif
// Check SUM & CRC
if ( validate(pt,16,icrc,ichecksum) == true ) {
// now we can decode the important flag and fill the object
_availableInfos.setFlags(haveChannel);
_channel = (ichannel != 4)?ichannel:3;
_availableInfos.setFlags(haveBattery);
if(ibattery & 0x4) {
_availableInfos.setFlags(battery);
}
_availableInfos.setFlags(haveTemperature);
_temperature = (itempS == 0)?dtemp:-dtemp;
_availableInfos.setFlags(haveHumidity);
_humidity = dhumid;
return true;
} else return false;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
// – THGR122NX : Temp + Humidity
// ——————————————————————————————
bool OregonSensorV2::decode_THGR122NX(char * pt) {
char channel; int ichannel; // values 1,2,4
char rolling[3]; int irolling;
char battery; int ibattery; // value & 0x4
char temp[4]; double dtemp; // Temp in BCD
char tempS; int itempS; // Sign 0 = positif
char humid[4]; double dhumid; // Humid in BCD
char checksum[3]; int ichecksum;
char crc[3]; int icrc;
int len = strlen(pt);
if ( len == 20 ) {
channel = pt[4];
rolling[0]=pt[7]; rolling[1]=pt[6]; rolling[2]='\0';
battery = pt[9];
temp[0] = pt[10] ; temp[1] = pt[11] ; temp[2] = pt[8] ; temp[3] ='\0';
tempS = pt[13];
humid[0] = pt[15] ; humid[1] = pt[12]; humid[2] = '0' ; humid[3] ='\0';
checksum[0] = pt[16]; checksum[1] = pt[17]; checksum[2] ='\0';
crc[0] = pt[18] ; crc[1] = pt[19] ; crc[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(%s) ch(%c) bat(%c) temp(%s) sign(%c) humid(%s) cksum(%s) crc(%s)\n","1D20",channel,battery,temp,tempS,humid, checksum, crc);
#endif
// Conversion to int value
ichannel = getIntFromChar(channel);
irolling = getIntFromString(rolling);
ibattery = getIntFromChar(battery);
itempS = getIntFromChar(tempS) & 0x08;
ichecksum = getIntFromString(checksum);
icrc = getIntFromString(crc);
dtemp = getDoubleFromString(temp);
dhumid = getDoubleFromString(humid);
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(0x%04X) ch(%d) bat(%d) temp(%f) sign(%d) humid(%f) cksum(0x%02X) crc(0x%02X)\n",0x1D20,ichannel,ibattery,dtemp,itempS,dhumid, ichecksum, icrc);
#endif
// Check SUM & CRC
if ( validate(pt,16,icrc,ichecksum) == true ) {
// now we can decode the important flag and fill the object
_availableInfos.setFlags(haveChannel);
_channel = (ichannel != 4)?ichannel:3;
_availableInfos.setFlags(haveBattery);
if(ibattery & 0x4) {
_availableInfos.setFlags(battery);
}
_availableInfos.setFlags(haveTemperature);
_temperature = (itempS == 0)?dtemp:-dtemp;
_availableInfos.setFlags(haveHumidity);
_humidity = dhumid;
return true;
} else return false;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V2 protocol for specific
// Oregon Devices
// – THN132N : Temp
// ——————————————————————————————
bool OregonSensorV2::decode_THN132N(char * pt) {
char channel; int ichannel; // values 1,2,4
char rolling[3]; int irolling;
char battery; int ibattery; // value & 0x4
char temp[4]; double dtemp; // Temp in BCD
char tempS; int itempS; // Sign 0 = positif
char checksum[3]; int ichecksum;
int len = strlen(pt);
if ( len == 16 ) {
channel = pt[4];
rolling[0]=pt[7]; rolling[1]=pt[6]; rolling[2]='\0';
battery = pt[9];
temp[0] = pt[10] ; temp[1] = pt[11] ; temp[2] = pt[8] ; temp[3] ='\0';
tempS = pt[13];
checksum[0] = pt[15]; checksum[1] = pt[12]; checksum[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV2 – decode : id(%s) ch(%c) bat(%c) temp(%s) sign(%c) cksum(%s) \n","EC040",channel,battery,temp,tempS, checksum);
#endif
// Conversion to int value
ichannel = getIntFromChar(channel);
irolling = getIntFromString(rolling);
ibattery = getIntFromChar(battery) & 0x04;
itempS = getIntFromChar(tempS) & 0x08;
ichecksum = getIntFromString(checksum);
dtemp = getDoubleFromString(temp);
// Check SUM
int _sum = getIntFromChar(pt[0]);
for ( int i = 2 ; i <= 11 ; i++ ) _sum += getIntFromChar(pt[i]);
_sum += getIntFromChar(pt[13]);
#ifdef SENSORDEBUG
printf("OSV2 - decode : id(0x%04X) ch(%d) bat(%d) temp(%f) sign(%d) cksum(0x%02X) _chksum(0x%02X)\n",0xEC40,ichannel,ibattery,dtemp,itempS, ichecksum, _sum);
#endif
if ( _sum == ichecksum ){
_availableInfos.setFlags(haveChannel);
_channel = (ichannel != 4)?ichannel:3;
_availableInfos.setFlags(haveBattery);
if(ibattery != 0) {
_availableInfos.setFlags(battery);
}
_availableInfos.setFlags(haveTemperature);
_temperature = (itempS == 0)?dtemp:-dtemp;
_availableInfos.setFlags(haveHumidity);
return true;
} else return false;
}
return false;
}
// —————————————————–
// Validate CRC and Checksum value from the signal
// Starts at the Sync header digit
// return true if both are valid
bool OregonSensorV2::validate(char * _str, int _len, int _CRC, int _SUM) {
int i,j,c,CRC,SUM;
CRC =0x43;
int CCIT_POLY = 0x07;
SUM = 0x00;
// swap each 2 digit
char __str[100];
for (j=0 ; j < _len ; j+=2){
__str[j] = _str[j+1];
__str[j+1] = _str[j];
}
__str[_len]='\0'; // recopie de
for (j=1; j< _len; j++)
{
c = getIntFromChar(__str[j]);
SUM += c;
CRC ^= c;
// Because we have to skip the rolling value in the CRC computation
if ( j != 6 && j != 7 ) {
for(i = 0; i < 4; i++) {
if( (CRC & 0x80) != 0 )
CRC = ( (CRC << 1) ^ CCIT_POLY ) & 0xFF;
else
CRC = (CRC << 1 ) & 0xFF;
}
}
}
// CRC is 8b but the len is quartet based and we start are digit 1
if ( ! (_len & 1) ) {
for(i = 0; i<4; i++) {
if( (CRC & 0x80) != 0 )
CRC = ( (CRC << 1) ^ CCIT_POLY ) & 0xFF;
else
CRC = (CRC << 1 ) & 0xFF;
}
}
#ifdef SENSORDEBUG
printf("Validate OOK – SUM : 0x%02X(0x%02X) CRC : 0x%02X(0x%02X)\n",SUM,_SUM,CRC,_CRC);
#endif
// Do not check crc anymore as depend on sensor it is not working as expected
if ( SUM == _SUM /* && CRC == _CRC */ ) return true;
else {
#ifdef SENSORTRACE
printf("OSV2 – validate err (%s) SUM : 0x%02X(0x%02X) CRC : 0x%02X(0x%02X)\n",_str,SUM,_SUM,CRC,_CRC);
#endif
}
return false;
}
OregonSensorV3::OregonSensorV3(char * _strval) : Sensor( ) {
_sensorClass = SENS_CLASS_OS;
_availableInfos.setFlags(decode(_strval));
}
bool OregonSensorV3::decode(char * _str) {
char * pt = & _str[5];
int len = strlen(_str);
char sensorId[5]; int isensorId;
// Proceed the right sensor
if (len > 11) {
sensorId[0] = pt[0];sensorId[1] = pt[3];sensorId[2] = pt[2];sensorId[3] = pt[5];sensorId[4] ='\0';
isensorId = getIntFromString(sensorId);
#ifdef SENSORDEBUG
printf("OSV3 – decode : id(%s)(0x%4X)\n",sensorId, isensorId);
#endif
switch (isensorId) {
case 0xF824:
_sensorType=0xF824;
_sensorName = "THGR810";
return decode_THGR810(pt); break;
default:
std::cout << "Unknown sensor id: " << std::hex << isensorId << std::endl;
return false;
break;
}
}
else {
std::cout << "OSV3 - decode: bad length" << std::endl;
}
return false;
}
// —————————————————————————————
// Decode OregonScientific V3 protocol for specific
// Oregon Devices
// – THGR810 : Temp + Humidity
// ——————————————————————————————
bool OregonSensorV3::decode_THGR810(char * pt) {
char channel; int ichannel; // values 1,2,4
char rolling[3]; int irolling;
char battery; int ibattery; // value & 0x4
char temp[4]; double dtemp; // Temp in BCD
char tempS; int itempS; // Sign 0 = positif
char humid[4]; double dhumid; // Humid in BCD
char checksum[3]; int ichecksum;
char crc[3]; int icrc;
int len = strlen(pt);
if ( len == 20 ) {
channel = pt[4];
rolling[0]=pt[7]; rolling[1]=pt[6]; rolling[2]='\0';
battery = pt[9];
temp[0] = pt[10] ; temp[1] = pt[11] ; temp[2] = pt[8] ; temp[3] ='\0';
tempS = pt[13];
humid[0] = pt[15] ; humid[1] = pt[12]; humid[2] = '0' ; humid[3] ='\0';
checksum[0] = pt[16]; checksum[1] = pt[17]; checksum[2] ='\0';
crc[0] = pt[18] ; crc[1] = pt[19] ; crc[2] ='\0';
#ifdef SENSORDEBUG
printf("OSV3 – decode : id(%s) ch(%c) bat(%c) temp(%s) sign(%c) humid(%s) cksum(%s) crc(%s)\n","A824",channel,battery,temp,tempS,humid, checksum, crc);
#endif
// Conversion to int value
ichannel = getIntFromChar(channel);
irolling = getIntFromString(rolling);
ibattery = getIntFromChar(battery);
itempS = getIntFromChar(tempS) & 0x08;
ichecksum = getIntFromString(checksum);
icrc = getIntFromString(crc);
dtemp = getDoubleFromString(temp);
dhumid = getDoubleFromString(humid);
#ifdef SENSORDEBUG
printf("OSV3 – decode : id(0x%04X) ch(%d) bat(%d) temp(%f) sign(%d) humid(%f) cksum(0x%02X) crc(0x%02X)\n",0x1D30,ichannel,ibattery,dtemp,itempS,dhumid, ichecksum, icrc);
#endif
// Check SUM & CRC
if ( validate(pt,16,icrc,ichecksum) == true ) {
// now we can decode the important flag and fill the object
_availableInfos.setFlags(haveChannel);
_channel = ichannel;
_availableInfos.setFlags(haveBattery);
if(ibattery & 0x4) {
_availableInfos.setFlags(battery);
}
_availableInfos.setFlags(haveTemperature);
_temperature = (itempS == 0)?dtemp:-dtemp;
_availableInfos.setFlags(haveHumidity);
_humidity = dhumid;
return true;
}
else
return false;
}
return false;
}
// —————————————————–
// Validate CRC and Checksum value from the signal
// Starts at the Sync header digit
// return true if both are valid
// seems to be the same as OSV2 ???
bool OregonSensorV3::validate(char * _str, int _len, int _CRC, int _SUM) {
int i,j,c,CRC,SUM;
CRC =0x43;
int CCIT_POLY = 0x07;
SUM = 0x00;
// swap each 2 digit
char __str[100];
for (j=0 ; j < _len ; j+=2){
__str[j] = _str[j+1];
__str[j+1] = _str[j];
}
__str[_len]='\0'; // recopie de
for (j=1; j< _len; j++)
{
c = getIntFromChar(__str[j]);
SUM += c;
CRC ^= c;
// Because we have to skip the rolling value in the CRC computation
if ( j != 6 && j != 7 ) {
for(i = 0; i < 4; i++) {
if( (CRC & 0x80) != 0 )
CRC = ( (CRC << 1) ^ CCIT_POLY ) & 0xFF;
else
CRC = (CRC << 1 ) & 0xFF;
}
}
}
// CRC is 8b but the len is quartet based and we start are digit 1
if ( ! (_len & 1) ) {
for(i = 0; i<4; i++) {
if( (CRC & 0x80) != 0 )
CRC = ( (CRC << 1) ^ CCIT_POLY ) & 0xFF;
else
CRC = (CRC << 1 ) & 0xFF;
}
}
#ifdef SENSORDEBUG
printf("Validate OOK – SUM : 0x%02X(0x%02X) CRC : 0x%02X(0x%02X)\n",SUM,_SUM,CRC,_CRC);
#endif
// Do not check crc anymore as depend on sensor it is not working as expected
if ( SUM == _SUM /* && CRC == _CRC */ ) return true;
else {
#ifdef SENSORTRACE
printf("OSV3 – validate err (%s) SUM : 0x%02X(0x%02X) CRC : 0x%02X(0x%02X)\n",_str,SUM,_SUM,CRC,_CRC);
#endif
}
return false;
}