-
Notifications
You must be signed in to change notification settings - Fork 27
/
oregon.c
3464 lines (2827 loc) · 114 KB
/
oregon.c
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
/*----------------------------------------------------------------------------+
| |
| Oregon Sensor Support for HEYU |
| Copyright 2008 Charles W. Sullivan |
| |
| |
| As used herein, HEYU is a trademark of Daniel B. Suthers. |
| X10, CM11A, and ActiveHome are trademarks of X-10 (USA) Inc. |
| The author is not affiliated with either entity. |
| |
| Charles W. Sullivan |
| Co-author and Maintainer |
| Greensboro, North Carolina |
| Email ID: cwsulliv01 |
| Email domain: -at- heyu -dot- org |
| |
+----------------------------------------------------------------------------*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#include <ctype.h>
#include <time.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "x10.h"
#include "process.h"
#include "x10state.h"
#include "oregon.h"
extern int sptty;
extern int i_am_state, i_am_aux, i_am_relay, i_am_rfxmeter;
extern char *funclabel[];
extern void create_flagslist ( unsigned char, unsigned long, char * );
extern double celsius2temp ( double, char, double );
extern double temp2celsius ( double, char, double );
extern void intvstrfunc ( char *, long *, time_t, time_t );
extern void update_activity_timeout ( ALIAS *, int );
extern CONFIG config;
extern CONFIG *configp;
extern struct x10global_st x10global;
extern x10hcode_t *x10state;
extern unsigned int modmask[NumModMasks][16];
extern int lock_for_write(), munlock();
extern unsigned int ore_ignore[];
extern int ore_ignore_size;
#if 0
int str2hex ( char *str, unsigned char *buf, int *bufsiz )
{
char minibuf[16];
char *sp;
int j;
if ( (strlen(str) % 2) != 0 ) {
fprintf(stderr, "Invalid string length %d\n", strlen(str));
return 1;
}
sp = str;
j = 0;
while ( *sp != '\0' ) {
minibuf[0] = *sp++;
minibuf[1] = *sp++;
minibuf[2] = '\0';
buf[j++] = (unsigned char)strtol(minibuf, NULL, 16);
}
if ( buf[0] != 8 * (j - 1) ) {
fprintf(stderr, "Miscount: buf[0] = %d, bufsiz = %d\n", buf[0]/8, j - 1);
return 1;
}
memmove(buf, buf + 1, j - 1);
*bufsiz = j - 1;
return 0;
}
unsigned int bcd2dec ( unsigned int bcd, int digits )
{
unsigned int dec = 0, mult = 1, mask =0x0f;
int j;
for ( j = 0; j < digits; j++ ) {
dec += mult * ((bcd & mask) >> (4 * j));
mult *= 10;
mask <<= 4;
}
return dec;
}
#endif
int is_ore_ignored ( unsigned int saddr )
{
#ifdef HAVE_FEATURE_ORE
int j = 0;
while ( j < ore_ignore_size ) {
if ( saddr == ore_ignore[j] ) {
return 1;
}
j++;
}
#endif
return 0;
}
#ifdef HAVE_FEATURE_ORE
char *forecast_txt( int fcast )
{
static char *text[] = {"", "Sunny ", "PtCloudy ", "Cloudy ", "Rain "};
return text[fcast & 0x07];
}
unsigned char forecast ( unsigned char rawcode )
{
return (rawcode == 0x0Cu) ? 1 :
(rawcode == 0x06u) ? 2 :
(rawcode == 0x02u) ? 3 :
(rawcode == 0x03u) ? 4 : 0;
}
int wind_dir ( unsigned char *buf, unsigned char mode )
{
/* Wind direction as decidegrees (0-3599) */
if ( mode == 1 ) {
/* OreWind1 and OreWind2 */
return ((buf[4] & 0xf0) >> 4) * 225;
}
else if ( mode == 2 ) {
/* OreWind3 */
return ((buf[5] & 0xf0) >> 4) * 1000 +
(buf[5] & 0x0f) * 100 +
((buf[4] & 0xf0) >> 4) * 10;
}
return 0;
}
char *compass_point ( double windir )
{
return (windir < 11.26) ? "N" :
(windir < 33.76) ? "NNE" :
(windir < 56.26) ? "NE" :
(windir < 78.76) ? "ENE" :
(windir < 101.26) ? "E" :
(windir < 123.76) ? "ESE" :
(windir < 146.26) ? "SE" :
(windir < 168.76) ? "SSE" :
(windir < 191.26) ? "S" :
(windir < 213.76) ? "SSW" :
(windir < 236.26) ? "SW" :
(windir < 258.76) ? "WSW" :
(windir < 281.26) ? "W" :
(windir < 303.76) ? "WNW" :
(windir < 326.26) ? "NW" :
(windir < 348.76) ? "NNW" : "N";
}
/*-----------------------------------------------------+
| Beaufort scale for wind speeds in decimeters/second |
+-----------------------------------------------------*/
int beaufort_scale ( int dwind )
{
return (dwind < 2) ? 0 :
(dwind < 16) ? 1 :
(dwind < 34) ? 2 :
(dwind < 55) ? 3 :
(dwind < 80) ? 4 :
(dwind < 108) ? 5 :
(dwind < 139) ? 6 :
(dwind < 172) ? 7 :
(dwind < 208) ? 8 :
(dwind < 254) ? 9 :
(dwind < 285) ? 10 :
(dwind < 327) ? 11 : 12;
}
unsigned char nybble_sum( int nbytes, unsigned char *buf )
{
int j;
unsigned char sum = 0;
for ( j = 0; j < nbytes; j++ ) {
sum += (buf[j] & 0x0f) + ((buf[j] >> 4) & 0x0f);
}
return sum;
}
unsigned char cse ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(7, buf) - buf[7];
return (unsigned char)(ssum & 0xff) - 0x18u;
}
unsigned char cs7 ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(7, buf) - buf[7];
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char cs8 ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(8, buf) - buf[8];
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char cs9 ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(9, buf) - buf[9];
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char cs10 ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(10, buf) - buf[10] ;
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char cs11 ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(11, buf) - buf[11] ;
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char cs12 ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(10, buf + 1) + (buf[11] & 0x0f);
ssum -= ((buf[12] & 0x0f) << 4) + ((buf[11] >> 4) & 0x0f);
return (unsigned char)ssum & 0xff;
}
unsigned char csw ( unsigned char *buf )
/* Note: Same as csWHL() */
{
short ssum;
ssum = nybble_sum(6, buf) + (buf[6] & 0x0f)
- ((buf[6] >> 4) & 0x0f) - ((buf[7] << 4) & 0xf0);
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char cs2HL ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(8, buf) + (buf[8] & 0x0f) -
((buf[8] >> 4) & 0x0f) - ((buf[9] << 4) & 0xf0);
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char csRHL ( unsigned char *buf )
{
short ssum;
ssum = nybble_sum(9, buf) + (buf[9] & 0x0f)
- ((buf[9] >> 4) & 0x0f) - ((buf[10] << 4) & 0xf0);
return (unsigned char)(ssum & 0xff) - 0x0A;
}
unsigned char csWHL ( unsigned char *buf )
/* Note: Same as csw() */
{
short ssum;
ssum = nybble_sum(6, buf) + (buf[6] & 0x0f) -
((buf[6] >> 4) & 0x0f) - ((buf[7] << 4) & 0xf0);
return (unsigned char)(ssum & 0xff) - 0x0Au;
}
unsigned char csWgt1 ( unsigned char *buf )
{
if ( (buf[0] & 0xf0) != (buf[5] & 0xf0) ||
(buf[1] & 0x0f) != (buf[6] & 0x0f) ||
(buf[6] & 0xf0) != 0x10 ||
(buf[5] & 0x0e) != 0x00 )
return 1;
return 0;
}
unsigned char csNull ( unsigned char *buf )
{
return 0;
}
unsigned char csFail ( unsigned char *buf )
{
return 1;
}
/* Types
GR101: GR101
TEMP1: THR128,THx138
TEMP2: THN132N,THWR288
TEMP3: THWR800
TH1: THGN122N,THGR122NX,THGR228N,THGR268
TH2: THGR810
TH3: RTGR328N
TH4: THGR328N
TH5: WTGR800
TH6: THGR918
THB1: BTHR918
THB2: BTHR918N,BTHR968
RAIN1: RGR126,RGR682,RGR918
RAIN2: PCR800
RAIN3:
WIND1: WTGR800
WIND2: WGR800
WIND3: STR918,WGR918
UV1: UVR138
UV2: UVN800
DT1: RTGR328N
ELEC1: cent-a-meter
ELEC2: OWL CM119
WEIGHT1: BWR101,BWR102
*/
/* Channel modes */
enum {CM0, CM1, CM2, CM3};
/* Battery modes */
enum {BM0, BM1, BM2, BM3, BM4};
static struct oregon_st {
unsigned char minbits;
unsigned char trulen;
unsigned char id0mask;
unsigned char id1mask;
unsigned char id0;
unsigned char id1;
int nvar;
unsigned char chanmode;
unsigned char batmode;
unsigned char specmode; /* Special handling */
unsigned short bpoffset;
unsigned char support_mode; /* 0 = Unsupported; 1 = Normal sensor; 2 = Dummy sensor */
unsigned char addr_mask;
int addr_byte;
char *slabel;
unsigned char subtype;
unsigned char (*chksumf)(unsigned char *);
} orechk[] = {
{88, 11, 0xff, 0xff, 0x5a, 0x5d, 3, CM1, BM1, 0, 795, 1, 0xff, 4, "ORE_THB1", OreTHB1, cs10 },
{88, 11, 0xff, 0xff, 0x5a, 0x6d, 3, CM1, BM1, 0, 856, 1, 0xff, 4, "ORE_THB2", OreTHB2, cs10 },
{84, 11, 0xff, 0xff, 0x2a, 0x19, 2, CM0, BM1, 0, 0, 1, 0xff, 4, "ORE_RAIN2", OreRain2, csRHL },
{84, 11, 0xff, 0xff, 0x06, 0xe4, 2, CM0, BM1, 0, 0, 1, 0xff, 4, "ORE_RAIN3", OreRain3, csRHL },
{80, 10, 0xff, 0xff, 0x2a, 0x1d, 2, CM0, BM1, 0, 0, 1, 0xff, 4, "ORE_RAIN1", OreRain1, cs2HL },
{80, 10, 0xff, 0xff, 0x1a, 0x99, 3, CM0, BM1, 1, 0, 1, 0xff, 4, "ORE_WIND1", OreWind1, cs9 },
{80, 10, 0xff, 0xff, 0x1a, 0x89, 3, CM0, BM1, 0, 0, 1, 0xff, 4, "ORE_WIND2", OreWind2, cs9 },
{80, 10, 0xff, 0xff, 0x3a, 0x0d, 3, CM0, BM2, 0, 0, 1, 0xff, 4, "ORE_WIND3", OreWind3, cs9 },
{72, 9, 0xff, 0xff, 0x0a, 0x4d, 1, CM1, BM1, 0, 0, 1, 0xff, 4, "ORE_T1", OreTemp1, cs8 },
{72, 9, 0xff, 0xff, 0x1a, 0x2d, 2, CM1, BM1, 0, 0, 1, 0xff, 4, "ORE_TH1", OreTH1, cs8 },
{72, 9, 0xff, 0xff, 0xfa, 0x28, 2, CM2, BM1, 0, 0, 1, 0xff, 4, "ORE_TH2", OreTH2, cs8 },
{72, 9, 0x0f, 0xff, 0x0a, 0xcc, 2, CM2, BM1, 0, 0, 1, 0xff, 4, "ORE_TH3", OreTH3, cs8 },
{72, 9, 0xff, 0xff, 0xca, 0x2c, 2, CM2, BM1, 0, 0, 1, 0xff, 4, "ORE_TH4", OreTH4, cs8 },
{72, 9, 0xff, 0xff, 0xfa, 0xb8, 2, CM0, BM2, 0, 0, 1, 0xff, 4, "ORE_TH5", OreTH5, cs8 },
{72, 9, 0xff, 0xff, 0x1a, 0x3d, 2, CM1, BM2, 0, 0, 1, 0xff, 4, "ORE_TH6", OreTH6, cs8 },
{64, 8, 0xff, 0xff, 0xda, 0x78, 1, CM0, BM1, 0, 0, 1, 0xff, 4, "ORE_UV2", OreUV2, cs7 },
{60, 8, 0xff, 0xff, 0xea, 0x7c, 1, CM0, BM1, 0, 0, 1, 0xff, 4, "ORE_UV1", OreUV1, csWHL },
{60, 8, 0xff, 0xff, 0xea, 0x4c, 1, CM1, BM1, 0, 0, 1, 0xff, 4, "ORE_T2", OreTemp2, csw },
{60, 8, 0xff, 0xff, 0xca, 0x48, 1, CM1, BM1, 0, 0, 1, 0xff, 4, "ORE_T3", OreTemp3, csw },
{56, 7, 0x0f, 0x00, 0x0c, 0x00, 1, CM0, BM0, 0, 0, 1, 0xf0, 2, "ORE_WGT1", OreWeight1, csWgt1},
{64, 8, 0xff, 0x80, 0xea, 0x00, 1, CM0, BM3, 0, 0, 1, 0xff, 3, "ELS_ELEC1", OreElec1, cse },
{108, 11, 0xff, 0x00, 0x1a, 0x00, 2, CM3, BM0, 0, 0, 1, 0xff, 3, "OWL_ELEC2", OreElec2, cs12 },
{108, 11, 0xff, 0x00, 0x2a, 0x00, 2, CM3, BM0, 0, 0, 1, 0xff, 3, "OWL_ELEC2", OreElec2, cs12 },
{108, 11, 0xff, 0x00, 0x3a, 0x00, 2, CM3, BM0, 0, 0, 1, 0xff, 3, "OWL_ELEC2", OreElec2, cs12 },
{96, 11, 0xff, 0xff, 0x8a, 0xec, 1, CM2, BM1, 1, 0, 1, 0xff, 4, "ORE_DT1", OreDT1, cs11 },
{88, 11, 0xff, 0xff, 0xf0, 0xf0, 1, CM0, BM0, 0, 0, 2, 0xff, 4, "ORE_TEMU", OreTemu, csFail}, /* Dummy */
{88, 11, 0xff, 0xff, 0xf0, 0xf0, 2, CM0, BM0, 0, 0, 2, 0xff, 4, "ORE_THEMU", OreTHemu, csFail}, /* Dummy */
{88, 11, 0xff, 0xff, 0xf0, 0xf0, 3, CM0, BM0, 0, 0, 2, 0xff, 4, "ORE_THBEMU", OreTHBemu, csFail}, /* Dummy */
/* Unsupported Oregon sensors */
{64, 8, 0x0f, 0x00, 0x03, 0x00, 2, CM0, BM0, 0, 0, 0, 0xf0, 2, "ORE_WGT2", OreWeight2, csNull}, /* OreGR101 */
};
int orechk_size = sizeof(orechk)/sizeof(struct oregon_st);
unsigned char battery_status ( char *batstr, unsigned char *batlvl,
unsigned char *buf, unsigned char mode )
{
unsigned char status;
*batstr = '\0';
*batlvl = 0;
switch ( mode) {
case BM0 : /* Battery field absent or unknown */
status = 0;
break;
case BM1 : /* Has only low battery flag */
status = (buf[4] & 0x04) ? ORE_LOBAT : 0;
break;
case BM2 : /* Has battery level */
status = ORE_BATLVL;
*batlvl = 10 - (buf[4] & 0x0fu);
sprintf(batstr, "BatLvl %d%% ", *batlvl * 10);
if ( (*batlvl * 10) <= configp->ore_lobat )
status |= ORE_LOBAT;
break;
case BM3 : /* Electrisave */
status = (buf[1] & 0x10) ? ORE_LOBAT : 0;
break;
default :
status = 0;
break;
}
return status;
}
unsigned char channelval( char *chstr, unsigned char *buf, unsigned char mode )
{
unsigned char chval;
switch ( mode ) {
case CM0 : /* Channel field absent or unknown */
chval = 1;
sprintf(chstr, "Ch 1 ");
break;
case CM1 : /* 3-bit channel indicator */
switch ( buf[2] & 0x70 ) {
case 0 :
case 0x10 : chval = 1; sprintf(chstr, "Ch 1 "); break;
case 0x20 : chval = 2; sprintf(chstr, "Ch 2 "); break;
case 0x40 : chval = 3; sprintf(chstr, "Ch 3 "); break;
default : chval = 0; sprintf(chstr, "Ch ? "); break;
}
break;
case CM2 : /* 4-bit channel value */
chval = (buf[2] >> 4) & 0x0f;
sprintf(chstr, "Ch %d ", chval);
break;
case CM3: /* Owl CM119 */
switch ( buf[0] & 0xf0 ) {
case 0x10 : chval = 1; sprintf(chstr, "Ch 1 "); break;
case 0x20 : chval = 2; sprintf(chstr, "Ch 2 "); break;
case 0x30 : chval = 3; sprintf(chstr, "Ch 3 "); break;
default : chval = 0; sprintf(chstr, "Ch ? "); break;
}
break;
default :
chval = 0;
*chstr = '\0';
break;
}
return chval;
}
#endif /* HAVE_FEATURE_ORE */
unsigned char oretype ( unsigned char *xbuf, unsigned char *subindx, unsigned char *subtype,
unsigned char *trulen, unsigned long *addr, int *nseq )
{
#ifdef HAVE_FEATURE_ORE
int j = 0;
while ( j < orechk_size ) {
if ( ((xbuf[0] & 0x7f) >= orechk[j].minbits) &&
(xbuf[1] & orechk[j].id0mask) == orechk[j].id0 &&
(xbuf[2] & orechk[j].id1mask) == orechk[j].id1 &&
orechk[j].chksumf(xbuf + 1) == 0 ) {
*trulen = orechk[j].trulen;
*addr = (unsigned short)(xbuf[orechk[j].addr_byte] & orechk[j].addr_mask);
/* If transmitter and address are shared by two sensors, modify one address */
if ( orechk[j].specmode == 1 ) {
*addr = (*addr + 1) & 0x00ff;
}
*nseq = (int)orechk[j].nvar;
*subindx = j;
*subtype = orechk[j].subtype;
if ( orechk[j].support_mode || configp->disp_ore_all == YES ) {
return 0;
}
else {
return 1;
}
}
j++;
}
return 1;
#else
return 1;
#endif /* HAVE_FEATURE_ORE */
}
#ifdef HAVE_FEATURE_ORE
static void tm_to_dtstring(struct tm *tm, char *buf, unsigned len)
{
char *s = NULL;
int result;
if (tm)
s = asctime(tm);
if (!s)
s = "----";
result = snprintf(buf, len, "DT %s", s);
if (result > 0) {
s = strchr(buf, '\n');
if (s)
*s = '\0';
}
}
static char *show_oredt(unsigned long *data_storage)
{
time_t time;
struct tm *tm = NULL;
static char buf[32];
if (*data_storage & ORE_VALID) {
time = c_oredt(data_storage);
tm = localtime(&time);
}
tm_to_dtstring(tm, buf, sizeof(buf));
return buf;
}
static void oredt1_to_tm(unsigned char *vdatap, struct tm *tm)
{
tm->tm_sec = (vdatap[4] >> 4) + 10 * (vdatap[ 5] & 0xf);
tm->tm_min = (vdatap[5] >> 4) + 10 * (vdatap[ 6] & 0xf);
tm->tm_hour = (vdatap[6] >> 4) + 10 * (vdatap[ 7] & 0xf);
tm->tm_mday = (vdatap[7] >> 4) + 10 * (vdatap[ 8] & 0xf);
tm->tm_mon = (vdatap[8] >> 4) - 1;
tm->tm_wday = (vdatap[ 9] & 0x7);
tm->tm_year = (vdatap[9] >> 4) + 10 * (vdatap[10] & 0xf) + 100;
}
static int translate_oredt(unsigned char *vdatap, unsigned char subtype,
unsigned long *data_storage, char *buf, unsigned len)
{
struct tm tm = {
.tm_isdst = -1,
};
time_t *valp = (time_t *)(data_storage + 1), *lastchvalp = valp + 1;
time_t delta;
int unchanged;
switch(subtype) {
case OreDT1:
oredt1_to_tm(vdatap, &tm);
break;
}
*valp = mktime(&tm);
tm_to_dtstring(&tm, buf, len);
delta = abs(*valp - *lastchvalp);
unchanged = delta / 60 < configp->ore_chgbits_dt ? 1 : 0;
if (!unchanged)
*lastchvalp = *valp;
return unchanged;
}
#endif
/*----------------------------------------------------------------------------+
| Translate Oregon RF data |
| Buffer reference: ST_COMMAND, ST_GENLONG, plus: |
| vtype, subindx, vidhi, vidlo, seq, buflen, buffer |
| [2] [3] [4] [5] [6] [7] [8+] |
+----------------------------------------------------------------------------*/
char *translate_oregon( unsigned char *buf, unsigned char *sunchanged, int *launchp )
{
#ifdef HAVE_FEATURE_ORE
static char outbuf[160];
static char intvstr[32];
long intv;
char flagslist[80];
ALIAS *aliasp;
LAUNCHER *launcherp;
extern unsigned int signal_source;
unsigned char func, *vdatap, vtype, subindx, subtype, seq, counter, unchanged;
int humid, lasthumid;
int deciamps, lastdeciamps;
int dspeed, lastdspeed, davspeed, lastdavspeed, direction, lastdirection;
int baro, lastbaro;
unsigned short vident, idmask;
int unit, loc, statusoffset;
int dtempc, lastdtempc, nvar;
int cweight, lastcweight;
int buflen;
unsigned long longvdata, longvdata2, longvdata3;
long delta, tipfactor;
unsigned long drain, lastdrain, train, multiplier;
int uvfactor, lastuvfactor;
char hc;
unsigned char actfunc, oactfunc;
unsigned int trigaddr, mask, active, trigactive;
unsigned int launched, bmaplaunch;
unsigned long afuncmap, ofuncmap;
int j, k, found, index = -1;
unsigned char hcode, ucode, trig = 0;
unsigned int bitmap = 0, changestate;
unsigned long vflags = 0, vflags_mask = 0;
static unsigned long prevdata[3], lastchdata[3];
static unsigned int startupstate;
static char chstr[8];
static unsigned char channel;
static unsigned char blevel, wgtnum;
static unsigned char status;
static unsigned char batchange;
unsigned char batstatus;
static char batstr[32];
static char pwrstr[32];
static char bftstr[16];
char cntrstr[16];
unsigned long *prevvalp, *lastchvalp;
unsigned long uknbytes;
unsigned char fcast;
char *fcast_txt;
char minibuf[32];
unsigned char extra;
char rawstring[32];
char flipstr[32];
char *unitstring = "";
int minroll;
unsigned long energyhi, energylo, power, lastchpower, chgbits;
unsigned long long energyll, lastchll;
long long deltall;
double temp, dbaro, weight, dbldir;
int ct1, ct2, ct3;
launcherp = configp->launcherp;
aliasp = configp->aliasp;
*launchp = -1;
*sunchanged = unchanged = 0;
flagslist[0] = '\0';
*outbuf = '\0';
changestate = 0;
vtype = buf[2];
subindx = buf[3];
seq = buf[6];
buflen = buf[7];
vdatap = buf + 8;
func = VdataFunc;
subtype = orechk[subindx].subtype;
channel = channelval(chstr, vdatap, orechk[subindx].chanmode);
if ( configp->oreid_16 == YES )
vident = (unsigned short)buf[5] | (channel << 8);
else
vident = (unsigned short)buf[5];
x10global.longvdata = 0;
x10global.lastvtype = vtype;
hcode = ucode = 0;
found = 0;
j = 0;
/* Look up the alias, if any */
idmask = configp->oreid_mask;
while ( !found && aliasp && aliasp[j].line_no > 0 ) {
if ( aliasp[j].vtype == vtype && aliasp[j].subtype == subtype &&
(bitmap = aliasp[j].unitbmap) > 0 &&
(ucode = single_bmap_unit(aliasp[j].unitbmap)) != 0xff ) {
for ( k = 0; k < aliasp[j].nident; k++ ) {
if ( (aliasp[j].ident[k] & idmask) == (vident & idmask) ) {
index = j;
found = 1;
break;
}
}
}
j++;
}
if ( !found || !aliasp ) {
if ( seq == 1 ) {
sprintf(outbuf, "func %12s : Type %s %sID 0x%02X Data 0x",
"RFdata", orechk[subindx].slabel, chstr, vident);
for ( j = 0; j < buflen; j++ ) {
sprintf(outbuf + strlen(outbuf), "%02x", vdatap[j]);
}
}
return outbuf;
}
nvar = orechk[subindx].nvar;
hc = aliasp[index].housecode;
hcode = hc2code(hc);
unit = code2unit(ucode);
longvdata2 = longvdata3 = 0;
/* Whether or not to display channel in output */
if ( configp->ore_display_chan == NO )
*chstr = '\0';
batstatus = battery_status(batstr, &blevel, vdatap, orechk[subindx].batmode);
if ( configp->ore_display_batlvl == NO )
*batstr = '\0';
*rawstring = '\0';
*flipstr = '\0';
*bftstr = '\0';
*cntrstr = '\0';
/* Save interval since last transmission */
if ( seq == 1 ) {
intvstrfunc(intvstr, &intv, time(NULL), x10state[hcode].timestamp[ucode]);
x10global.interval = intv;
}
switch ( subtype ) {
case OreWind1 :
case OreWind2 :
case OreWind3 :
loc = aliasp[index].storage_index;
if ( seq == 1 ) {
/* Wind Average Speed */
vflags_mask = SEC_LOBAT;
longvdata = (unsigned long)(channel & 0x0fu) << ORE_CHANSHFT;
status = ORE_VALID;
status |= batstatus;
longvdata |= status | ((unsigned long)blevel << ORE_BATSHFT);
/* Get previous data */
for ( j = 0; j < nvar; j++ ) {
prevdata[j] = x10global.data_storage[loc + j];
lastchdata[j] = x10global.data_storage[loc + nvar + j];
}
batchange = (batstatus ^ (unsigned char)prevdata[0]) & ORE_LOBAT;
/* Average speed in decimeters/sec */
davspeed = ((vdatap[8] & 0xf0) >> 4) * 100 + (vdatap[8] & 0x0f) * 10 + ((vdatap[7] & 0xf0) >> 4);
longvdata |= (unsigned long)davspeed << ORE_DATASHFT;
if ( configp->ore_display_count == YES )
sprintf(rawstring, "[%u] ", davspeed);
if ( configp->ore_display_bft == YES )
sprintf(bftstr, " %dbft", beaufort_scale(davspeed));
lastdavspeed = (lastchdata[seq - 1] & ORE_DATAMSK) >> ORE_DATASHFT;
delta = abs(davspeed - lastdavspeed);
unchanged = (delta < configp->ore_chgbits_wavsp) ? 1 : 0;
func = OreWindAvSpFunc;
trig = OreWindAvSpTrig;
vflags |= (status & ORE_LOBAT) ? SEC_LOBAT : 0;
create_flagslist (vtype, vflags, flagslist);
sprintf(outbuf, "func %12s : hu %c%-2d %sAvSpeed %.1f%s%s %s%s%s(%s)",
funclabel[func], hc, unit, chstr, ((double)davspeed / 10.0) * configp->ore_windscale, configp->ore_windunits,
bftstr, rawstring, batstr, flagslist, aliasp[index].label);
}
else if ( seq == 2 ) {
/* Wind Instantaneous Speed */
vflags_mask = SEC_LOBAT;
longvdata = (unsigned long)(channel & 0x0fu) << ORE_CHANSHFT;
status = ORE_VALID;
status |= batstatus;
longvdata |= status | ((unsigned long)blevel << ORE_BATSHFT);
/* Process wind speeds as decimeters/sec */
dspeed = (vdatap[7] & 0x0f) * 100 +
((vdatap[6] & 0xf0) >> 4) * 10 + (vdatap[6] & 0x0f);
if ( configp->ore_display_count == YES )
sprintf(rawstring, "[%u] ", dspeed);
if ( configp->ore_display_bft == YES )
sprintf(bftstr, " %dbft", beaufort_scale(dspeed));
longvdata |= (unsigned long)dspeed << ORE_DATASHFT;
lastdspeed = (lastchdata[seq - 1] & ORE_DATAMSK) >> ORE_DATASHFT;
delta = abs(dspeed - lastdspeed);
unchanged = (delta < configp->ore_chgbits_wsp) ? 1 : 0;
func = OreWindSpFunc;
trig = OreWindSpTrig;
vflags |= (status & ORE_LOBAT) ? SEC_LOBAT : 0;
create_flagslist (vtype, vflags, flagslist);
sprintf(outbuf, "func %12s : hu %c%-2d %sSpeed %.1f%s%s %s%s%s(%s)",
funclabel[func], hc, unit, chstr,
((double)dspeed / 10.0) * configp->ore_windscale, configp->ore_windunits,
bftstr, rawstring, batstr, flagslist, aliasp[index].label);
}
else if ( seq == 3 ) {
/* Process wind direction data as decidegrees (0-3599) */
vflags_mask = SEC_LOBAT;
longvdata = (unsigned long)(channel & 0x0fu) << ORE_CHANSHFT;
status = ORE_VALID;
status |= batstatus;
longvdata |= status | ((unsigned long)blevel << ORE_BATSHFT);
if ( subtype == OreWind3 ) {
direction = ((vdatap[5] & 0xf0) >> 4) * 1000 +
(vdatap[5] & 0x0f) * 100 + ((vdatap[4] & 0xf0) >> 4) * 10;
}
else {
direction = ((vdatap[4] & 0xf0) >> 4) * 225;
}
longvdata |= (unsigned long)direction << ORE_DATASHFT;
lastdirection = (lastchdata[seq - 1] & ORE_DATAMSK) >> ORE_DATASHFT;
if ( !(lastchdata[seq -1] & ORE_VALID) ) {
unchanged = 0;
}
else if ( subtype == OreWind3 ) {
delta = (direction - lastdirection);
if ( delta > 1800 )
delta -= 3600;
else if ( delta < -1800 )
delta += 3600;
unchanged = ((abs(delta) / 10) < configp->ore_chgbits_wdir) ? 1 : 0;
}
else {
delta = ((direction - lastdirection) / 225 + 0x10) % 0x10;
unchanged = (abs(delta) < configp->ore_chgbits_wdir) ? 1 : 0;
}
func = OreWindDirFunc;
trig = OreWindDirTrig;
vflags |= (status & ORE_LOBAT) ? SEC_LOBAT : 0;
create_flagslist(vtype, vflags, flagslist);
direction = (direction + configp->ore_windsensordir + 3600) % 3600;
dbldir = (double)direction / 10.0;
*minibuf = '\0';
if ( configp->ore_winddir_mode & COMPASS_ANGLE )
sprintf(minibuf + strlen(minibuf), "%.1fdeg ", dbldir);
if ( configp->ore_winddir_mode & COMPASS_POINTS )
sprintf(minibuf + strlen(minibuf), "%s ", compass_point(dbldir));
sprintf(outbuf, "func %12s : hu %c%-2d %sDir %s%s%s(%s)",
funclabel[func], hc, unit, chstr, minibuf,
batstr, flagslist, aliasp[index].label);
}
else {
longvdata = 0;
break;
}
x10global.longvdata = longvdata;
x10global.data_storage[loc + seq - 1] = longvdata;
x10state[hcode].longdata = longvdata;
if ( unchanged == 0 ) {
// x10state[hcode].state[ChgState] |= bitmap;
changestate |= bitmap;
x10global.data_storage[loc + nvar + seq - 1] = longvdata;
}
else {
// x10state[hcode].state[ChgState] &= ~bitmap;
changestate &= ~bitmap;
}
break;
case OreRain1 :
case OreRain2 :
case OreRain3 :
loc = aliasp[index].storage_index;
func = aliasp[index].funclist[seq - 1];
statusoffset = aliasp[index].statusoffset[seq - 1];
/* Rain data storage. Note that here we are using one extra storage */
/* location each for the 32-bit rate and total rain. I.e., */
/* [loc] => Rate status, channel, and battery */
/* [loc + 1] => Rate 32-bit value */
/* [loc + 2] => Last changed Rate status, channel, and battery */
/* [loc + 3] => Last changed Rate 32-bit value */
/* [loc + 4] => Total status, channel, and battery */
/* [loc + 5] => Total 32-bit value */
/* [loc + 6] => Last changed total status, channel, and battery */
/* [loc + 7] => Last changed Total 32-bit value */
extra = 0;
*flipstr = '\0';
if ( func == OreRainRateFunc ) {
/* Rainfall Rate */
vflags_mask = SEC_LOBAT;
loc = aliasp[index].storage_index;
status = 0;
longvdata = (unsigned long)(channel & 0x0fu) << ORE_CHANSHFT;
status = ORE_VALID | batstatus;
longvdata |= status | ((unsigned long)blevel << ORE_BATSHFT);
prevvalp = &x10global.data_storage[loc + statusoffset];
lastchvalp = &x10global.data_storage[loc + statusoffset + 2];
if ( seq == 1 )
batchange = (batstatus ^ (unsigned char)(*prevvalp)) & ORE_LOBAT;