forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indidriver.c
1869 lines (1559 loc) · 51.8 KB
/
indidriver.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
#if 0
INDI Driver Functions
Copyright (C) 2020 by Pawel Soja <[email protected]>
Copyright (C) 2003 - 2020 Jasem Mutlaq
Copyright (C) 2003 - 2006 Elwood C. Downey
This library is free software;
you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
#endif
#include "indidriver.h"
#include "base64.h"
#include "indicom.h"
#include "indidevapi.h"
#include "locale_compat.h"
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include "userio.h"
#include "indiuserio.h"
static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
int verbose; /* chatty */
char *me = ""; /* a.out name */
#define MAXRBUF 2048
/*! INDI property type */
enum
{
INDI_NUMBER,
INDI_SWITCH,
INDI_TEXT,
INDI_LIGHT,
INDI_BLOB,
INDI_UNKNOWN
};
// TODO use fast map
/* insure RO properties are never modified. RO Sanity Check */
typedef struct {
char propName[MAXINDINAME];
char devName[MAXINDIDEVICE];
IPerm perm;
const void *ptr;
int type;
} ROSC;
static ROSC *propCache = NULL;
static int nPropCache = 0; /* # of elements in roCheck */
static ROSC *rosc_new()
{
assert_mem(propCache = (ROSC *)(realloc(propCache, (nPropCache + 1) * sizeof *propCache)));
return &propCache[nPropCache++];
}
static void rosc_add(const char *propName, const char *devName, IPerm perm, const void *ptr, int type)
{
ROSC *SC = rosc_new();
strcpy(SC->propName, propName);
strcpy(SC->devName, devName);
SC->perm = perm;
SC->ptr = ptr;
SC->type = type;
}
/* Return pointer of property if already cached, NULL otherwise */
static ROSC *rosc_find(const char *propName, const char *devName)
{
for (int i = 0; i < nPropCache; i++)
if (!strcmp(propName, propCache[i].propName) && !strcmp(devName, propCache[i].devName))
return &propCache[i];
return NULL;
}
static void rosc_add_unique(const char *propName, const char *devName, IPerm perm, const void *ptr, int type)
{
if (rosc_find(propName, devName) == NULL)
rosc_add(propName, devName, perm, ptr, type);
}
/* tell Client to delete the property with given name on given device, or
* entire device if !name
*/
void IDDeleteVA(const char *dev, const char *name, const char *fmt, va_list ap)
{
const userio *io = userio_file();
pthread_mutex_lock(&stdout_mutex);
userio_xmlv1(io, stdout);
IUUserIODeleteVA(io, stdout, dev, name, fmt, ap);
fflush(stdout);
pthread_mutex_unlock(&stdout_mutex);
}
void IDDelete(const char *dev, const char *name, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
IDDeleteVA(dev, name, fmt, ap);
va_end(ap);
}
/* tell indiserver we want to snoop on the given device/property.
* name ignored if NULL or empty.
*/
void IDSnoopDevice(const char *snooped_device, const char *snooped_property)
{
// Ignore empty snooped device
if (snooped_device[0])
{
const userio *io = userio_file();
pthread_mutex_lock(&stdout_mutex);
userio_xmlv1(io, stdout);
IUUserIOGetProperties(io, stdout, snooped_device, snooped_property);
fflush(stdout);
pthread_mutex_unlock(&stdout_mutex);
}
}
/* tell indiserver whether we want BLOBs from the given snooped device.
* silently ignored if given device is not already registered for snooping.
*/
void IDSnoopBLOBs(const char *snooped_device, const char *snooped_property, BLOBHandling bh)
{
const userio *io = userio_file();
pthread_mutex_lock(&stdout_mutex);
userio_xmlv1(io, stdout);
IUUserIOEnableBLOB(io, stdout, snooped_device, snooped_property, bh);
fflush(stdout);
pthread_mutex_unlock(&stdout_mutex);
}
/* Update property switches in accord with states and names. */
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
{
ISwitch *so = NULL; // On switch pointer
assert(svp != NULL && "IUUpdateSwitch SVP is NULL");
/* store On switch pointer */
if (svp->r == ISR_1OFMANY)
{
so = IUFindOnSwitch(svp);
IUResetSwitch(svp);
}
for (int i = 0; i < n; i++)
{
ISwitch *sp = IUFindSwitch(svp, names[i]);
if (!sp)
{
svp->s = IPS_IDLE;
IDSetSwitch(svp, "Error: %s is not a member of %s (%s) property.", names[i], svp->label, svp->name);
return -1;
}
sp->s = states[i];
}
/* Consistency checks for ISR_1OFMANY after update. */
if (svp->r == ISR_1OFMANY)
{
int t_count = 0;
for (int i = 0; i < svp->nsp; i++)
{
if (svp->sp[i].s == ISS_ON)
t_count++;
}
if (t_count != 1)
{
IUResetSwitch(svp);
// restore previous state
if (so)
so->s = ISS_ON;
svp->s = IPS_IDLE;
IDSetSwitch(svp, "Error: invalid state switch for property %s (%s). %s.", svp->label, svp->name,
t_count == 0 ? "No switch is on" : "Too many switches are on");
return -1;
}
}
return 0;
}
/* Update property numbers in accord with values and names */
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
{
assert(nvp != NULL && "IUUpdateNumber NVP is NULL");
for (int i = 0; i < n; i++)
{
INumber *np = IUFindNumber(nvp, names[i]);
if (!np)
{
nvp->s = IPS_IDLE;
IDSetNumber(nvp, "Error: %s is not a member of %s (%s) property.", names[i], nvp->label, nvp->name);
return -1;
}
if (values[i] < np->min || values[i] > np->max)
{
nvp->s = IPS_ALERT;
IDSetNumber(nvp, "Error: Invalid range for %s (%s). Valid range is from %g to %g. Requested value is %g",
np->label, np->name, np->min, np->max, values[i]);
return -1;
}
}
/* First loop checks for error, second loop set all values atomically*/
for (int i = 0; i < n; i++)
{
INumber *np = IUFindNumber(nvp, names[i]);
np->value = values[i];
}
return 0;
}
/* Update property text in accord with texts and names */
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
{
assert(tvp != NULL && "IUUpdateText TVP is NULL");
for (int i = 0; i < n; i++)
{
IText *tp = IUFindText(tvp, names[i]);
if (!tp)
{
tvp->s = IPS_IDLE;
IDSetText(tvp, "Error: %s is not a member of %s (%s) property.", names[i], tvp->label, tvp->name);
return -1;
}
}
/* First loop checks for error, second loop set all values atomically*/
for (int i = 0; i < n; i++)
{
IText *tp = IUFindText(tvp, names[i]);
IUSaveText(tp, texts[i]);
}
return 0;
}
/* Update property BLOB in accord with BLOBs and names */
int IUUpdateBLOB(IBLOBVectorProperty *bvp, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[],
int n)
{
assert(bvp != NULL && "IUUpdateBLOB BVP is NULL");
for (int i = 0; i < n; i++)
{
IBLOB *bp = IUFindBLOB(bvp, names[i]);
if (!bp)
{
bvp->s = IPS_IDLE;
IDSetBLOB(bvp, "Error: %s is not a member of %s (%s) property.", names[i], bvp->label, bvp->name);
return -1;
}
}
/* First loop checks for error, second loop set all values atomically*/
for (int i = 0; i < n; i++)
{
IBLOB *bp = IUFindBLOB(bvp, names[i]);
IUSaveBLOB(bp, sizes[i], blobsizes[i], blobs[i], formats[i]);
}
return 0;
}
int IUSaveBLOB(IBLOB *bp, int size, int blobsize, char *blob, char *format)
{
bp->bloblen = blobsize;
bp->size = size;
bp->blob = blob;
strncpy(bp->format, format, MAXINDIFORMAT);
return 0;
}
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
{
strncpy(sp->name, name, sizeof(sp->name));
if (label[0])
strncpy(sp->label, label, sizeof(sp->label));
else
strncpy(sp->label, name, sizeof(sp->label));
sp->s = s;
sp->svp = NULL;
sp->aux = NULL;
}
void IUFillLight(ILight *lp, const char *name, const char *label, IPState s)
{
strncpy(lp->name, name, sizeof(lp->name));
if (label[0])
strncpy(lp->label, label, sizeof(lp->label));
else
strncpy(lp->label, name, sizeof(lp->label));
lp->s = s;
lp->lvp = NULL;
lp->aux = NULL;
}
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max,
double step, double value)
{
strncpy(np->name, name, sizeof(np->name));
if (label[0])
strncpy(np->label, label, sizeof(np->label));
else
strncpy(np->label, name, sizeof(np->label));
strncpy(np->format, format, sizeof(np->format));
np->min = min;
np->max = max;
np->step = step;
np->value = value;
np->nvp = NULL;
np->aux0 = NULL;
np->aux1 = NULL;
}
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
{
strncpy(tp->name, name, sizeof(tp->name));
if (label[0])
strncpy(tp->label, label, sizeof(tp->label));
else
strncpy(tp->label, name, sizeof(tp->label));
if (tp->text && tp->text[0])
free(tp->text);
tp->text = NULL;
tp->tvp = NULL;
tp->aux0 = NULL;
tp->aux1 = NULL;
if (initialText && initialText[0])
IUSaveText(tp, initialText);
}
void IUFillBLOB(IBLOB *bp, const char *name, const char *label, const char *format)
{
memset(bp, 0, sizeof(IBLOB));
strncpy(bp->name, name, sizeof(bp->name));
if (label[0])
strncpy(bp->label, label, sizeof(bp->label));
else
strncpy(bp->label, name, sizeof(bp->label));
strncpy(bp->format, format, sizeof(bp->format));
bp->blob = 0;
bp->bloblen = 0;
bp->size = 0;
bp->bvp = 0;
bp->aux0 = 0;
bp->aux1 = 0;
bp->aux2 = 0;
}
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name,
const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
{
strncpy(svp->device, dev, sizeof(svp->device));
strncpy(svp->name, name, sizeof(svp->name));
if (label[0])
strncpy(svp->label, label, sizeof(svp->label));
else
strncpy(svp->label, name, sizeof(svp->label));
strncpy(svp->group, group, sizeof(svp->group));
svp->timestamp[0] = '\0';
svp->p = p;
svp->r = r;
svp->timeout = timeout;
svp->s = s;
svp->sp = sp;
svp->nsp = nsp;
}
void IUFillLightVector(ILightVectorProperty *lvp, ILight *lp, int nlp, const char *dev, const char *name,
const char *label, const char *group, IPState s)
{
strncpy(lvp->device, dev, sizeof(lvp->device));
strncpy(lvp->name, name, sizeof(lvp->name));
if (label[0])
strncpy(lvp->label, label, sizeof(lvp->label));
else
strncpy(lvp->label, name, sizeof(lvp->label));
strncpy(lvp->group, group, sizeof(lvp->group));
lvp->timestamp[0] = '\0';
lvp->s = s;
lvp->lp = lp;
lvp->nlp = nlp;
}
void IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name,
const char *label, const char *group, IPerm p, double timeout, IPState s)
{
strncpy(nvp->device, dev, sizeof(nvp->device));
strncpy(nvp->name, name, sizeof(nvp->name));
if (label[0])
strncpy(nvp->label, label, sizeof(nvp->label));
else
strncpy(nvp->label, name, sizeof(nvp->label));
strncpy(nvp->group, group, sizeof(nvp->group));
nvp->timestamp[0] = '\0';
nvp->p = p;
nvp->timeout = timeout;
nvp->s = s;
nvp->np = np;
nvp->nnp = nnp;
}
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name,
const char *label, const char *group, IPerm p, double timeout, IPState s)
{
strncpy(tvp->device, dev, sizeof(tvp->device));
strncpy(tvp->name, name, sizeof(tvp->name));
if (label[0])
strncpy(tvp->label, label, sizeof(tvp->label));
else
strncpy(tvp->label, name, sizeof(tvp->label));
strncpy(tvp->group, group, sizeof(tvp->group));
tvp->timestamp[0] = '\0';
tvp->p = p;
tvp->timeout = timeout;
tvp->s = s;
tvp->tp = tp;
tvp->ntp = ntp;
}
void IUFillBLOBVector(IBLOBVectorProperty *bvp, IBLOB *bp, int nbp, const char *dev, const char *name,
const char *label, const char *group, IPerm p, double timeout, IPState s)
{
memset(bvp, 0, sizeof(IBLOBVectorProperty));
strncpy(bvp->device, dev, sizeof(bvp->device));
strncpy(bvp->name, name, sizeof(bvp->name));
if (label[0])
strncpy(bvp->label, label, sizeof(bvp->label));
else
strncpy(bvp->label, name, sizeof(bvp->label));
strncpy(bvp->group, group, sizeof(bvp->group));
bvp->timestamp[0] = '\0';
bvp->p = p;
bvp->timeout = timeout;
bvp->s = s;
bvp->bp = bp;
bvp->nbp = nbp;
}
/*****************************************************************************
* convenience functions for use in your implementation of ISSnoopDevice().
*/
/* crack the snooped driver setNumberVector or defNumberVector message into
* the given INumberVectorProperty.
* return 0 if type, device and name match and all members are present, else
* return -1
*/
int IUSnoopNumber(XMLEle *root, INumberVectorProperty *nvp)
{
char *dev, *name;
XMLEle *ep;
/* check and crack type, device, name and state */
if (strcmp(tagXMLEle(root) + 3, "NumberVector") || crackDN(root, &dev, &name, NULL) < 0)
return (-1);
if (strcmp(dev, nvp->device) || strcmp(name, nvp->name))
return (-1); /* not this property */
(void)crackIPState(findXMLAttValu(root, "state"), &nvp->s);
/* match each INumber with a oneNumber */
locale_char_t *orig = indi_locale_C_numeric_push();
for (int i = 0; i < nvp->nnp; i++)
{
for (ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (!strcmp(tagXMLEle(ep) + 3, "Number") && !strcmp(nvp->np[i].name, findXMLAttValu(ep, "name")))
{
if (f_scansexa(pcdataXMLEle(ep), &nvp->np[i].value) < 0)
{
indi_locale_C_numeric_pop(orig);
return (-1); /* bad number format */
}
break;
}
}
if (!ep)
{
indi_locale_C_numeric_pop(orig);
return (-1); /* element not found */
}
}
indi_locale_C_numeric_pop(orig);
/* ok */
return (0);
}
/* crack the snooped driver setTextVector or defTextVector message into
* the given ITextVectorProperty.
* return 0 if type, device and name match and all members are present, else
* return -1
*/
int IUSnoopText(XMLEle *root, ITextVectorProperty *tvp)
{
char *dev, *name;
XMLEle *ep;
/* check and crack type, device, name and state */
if (strcmp(tagXMLEle(root) + 3, "TextVector") || crackDN(root, &dev, &name, NULL) < 0)
return (-1);
if (strcmp(dev, tvp->device) || strcmp(name, tvp->name))
return (-1); /* not this property */
(void)crackIPState(findXMLAttValu(root, "state"), &tvp->s);
/* match each IText with a oneText */
for (int i = 0; i < tvp->ntp; i++)
{
for (ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (!strcmp(tagXMLEle(ep) + 3, "Text") && !strcmp(tvp->tp[i].name, findXMLAttValu(ep, "name")))
{
IUSaveText(&tvp->tp[i], pcdataXMLEle(ep));
break;
}
}
if (!ep)
return (-1); /* element not found */
}
/* ok */
return (0);
}
/* crack the snooped driver setLightVector or defLightVector message into
* the given ILightVectorProperty. it is not necessary that all ILight names
* be found.
* return 0 if type, device and name match, else return -1.
*/
int IUSnoopLight(XMLEle *root, ILightVectorProperty *lvp)
{
char *dev, *name;
XMLEle *ep;
/* check and crack type, device, name and state */
if (strcmp(tagXMLEle(root) + 3, "LightVector") || crackDN(root, &dev, &name, NULL) < 0)
return (-1);
if (strcmp(dev, lvp->device) || strcmp(name, lvp->name))
return (-1); /* not this property */
(void)crackIPState(findXMLAttValu(root, "state"), &lvp->s);
/* match each oneLight with one ILight */
for (ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (!strcmp(tagXMLEle(ep) + 3, "Light"))
{
const char *name = findXMLAttValu(ep, "name");
for (int i = 0; i < lvp->nlp; i++)
{
if (!strcmp(lvp->lp[i].name, name))
{
if (crackIPState(pcdataXMLEle(ep), &lvp->lp[i].s) < 0)
{
return (-1); /* unrecognized state */
}
break;
}
}
}
}
/* ok */
return (0);
}
/* crack the snooped driver setSwitchVector or defSwitchVector message into the
* given ISwitchVectorProperty. it is not necessary that all ISwitch names be
* found.
* return 0 if type, device and name match, else return -1.
*/
int IUSnoopSwitch(XMLEle *root, ISwitchVectorProperty *svp)
{
char *dev, *name;
XMLEle *ep;
/* check and crack type, device, name and state */
if (strcmp(tagXMLEle(root) + 3, "SwitchVector") || crackDN(root, &dev, &name, NULL) < 0)
return (-1);
if (strcmp(dev, svp->device) || strcmp(name, svp->name))
return (-1); /* not this property */
(void)crackIPState(findXMLAttValu(root, "state"), &svp->s);
/* match each oneSwitch with one ISwitch */
for (ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (!strcmp(tagXMLEle(ep) + 3, "Switch"))
{
const char *name = findXMLAttValu(ep, "name");
for (int i = 0; i < svp->nsp; i++)
{
if (!strcmp(svp->sp[i].name, name))
{
if (crackISState(pcdataXMLEle(ep), &svp->sp[i].s) < 0)
{
return (-1); /* unrecognized state */
}
break;
}
}
}
}
/* ok */
return (0);
}
/* crack the snooped driver setBLOBVector message into the given
* IBLOBVectorProperty. it is not necessary that all IBLOB names be found.
* return 0 if type, device and name match, else return -1.
* N.B. we assume any existing blob in bvp has been malloced, which we free
* and replace with a newly malloced blob if found.
*/
int IUSnoopBLOB(XMLEle *root, IBLOBVectorProperty *bvp)
{
char *dev, *name;
XMLEle *ep;
/* check and crack type, device, name and state */
if (strcmp(tagXMLEle(root), "setBLOBVector") || crackDN(root, &dev, &name, NULL) < 0)
return (-1);
if (strcmp(dev, bvp->device) || strcmp(name, bvp->name))
return (-1); /* not this property */
crackIPState(findXMLAttValu(root, "state"), &bvp->s);
for (ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (strcmp(tagXMLEle(ep), "oneBLOB") == 0)
{
XMLAtt *na = findXMLAtt(ep, "name");
if (na == NULL)
return (-1);
IBLOB *bp = IUFindBLOB(bvp, valuXMLAtt(na));
if (bp == NULL)
return (-1);
XMLAtt *fa = findXMLAtt(ep, "format");
XMLAtt *sa = findXMLAtt(ep, "size");
XMLAtt *ec = findXMLAtt(ep, "enclen");
if (fa && sa && ec)
{
int enclen = atoi(valuXMLAtt(ec));
assert_mem(bp->blob = realloc(bp->blob, 3 * enclen / 4));
bp->bloblen = from64tobits_fast(bp->blob, pcdataXMLEle(ep), enclen);
strncpy(bp->format, valuXMLAtt(fa), MAXINDIFORMAT);
bp->size = atoi(valuXMLAtt(sa));
}
}
}
/* ok */
return (0);
}
/* crack the given INDI XML element and call driver's IS* entry points as they
* are recognized.
* return 0 if ok else -1 with reason in msg[].
* N.B. exit if getProperties does not proclaim a compatible version.
*/
int dispatch(XMLEle *root, char msg[])
{
char *rtag = tagXMLEle(root);
XMLEle *ep;
int n;
if (verbose)
prXMLEle(stderr, root, 0);
if (!strcmp(rtag, "getProperties"))
{
XMLAtt *ap, *name, *dev;
double v;
/* check version */
ap = findXMLAtt(root, "version");
if (!ap)
{
fprintf(stderr, "%s: getProperties missing version\n", me);
exit(1);
}
v = atof(valuXMLAtt(ap));
if (v > INDIV)
{
fprintf(stderr, "%s: client version %g > %g\n", me, v, INDIV);
exit(1);
}
// Get device
dev = findXMLAtt(root, "device");
// Get property name
name = findXMLAtt(root, "name");
if (name && dev)
{
ROSC *prop = rosc_find(valuXMLAtt(name), valuXMLAtt(dev));
if (prop == NULL)
return 0;
switch (prop->type)
{
/* JM 2019-07-18: Why are we using setXXX here? should be defXXX */
case INDI_NUMBER:
//IDSetNumber((INumberVectorProperty *)(prop->ptr), NULL);
IDDefNumber((INumberVectorProperty *)(prop->ptr), NULL);
return 0;
case INDI_SWITCH:
//IDSetSwitch((ISwitchVectorProperty *)(prop->ptr), NULL);
IDDefSwitch((ISwitchVectorProperty *)(prop->ptr), NULL);
return 0;
case INDI_TEXT:
//IDSetText((ITextVectorProperty *)(prop->ptr), NULL);
IDDefText((ITextVectorProperty *)(prop->ptr), NULL);
return 0;
case INDI_BLOB:
//IDSetBLOB((IBLOBVectorProperty *)(prop->ptr), NULL);
IDDefBLOB((IBLOBVectorProperty *)(prop->ptr), NULL);
return 0;
default:
return 0;
}
}
ISGetProperties(dev ? valuXMLAtt(dev) : NULL);
return (0);
}
/* other commands might be from a snooped device.
* we don't know here which devices are being snooped so we send
* all remaining valid messages
*/
if (!strcmp(rtag, "setNumberVector") || !strcmp(rtag, "setTextVector") || !strcmp(rtag, "setLightVector") ||
!strcmp(rtag, "setSwitchVector") || !strcmp(rtag, "setBLOBVector") || !strcmp(rtag, "defNumberVector") ||
!strcmp(rtag, "defTextVector") || !strcmp(rtag, "defLightVector") || !strcmp(rtag, "defSwitchVector") ||
!strcmp(rtag, "defBLOBVector") || !strcmp(rtag, "message") || !strcmp(rtag, "delProperty"))
{
ISSnoopDevice(root);
return (0);
}
char *dev, *name;
/* pull out device and name */
if (crackDN(root, &dev, &name, msg) < 0)
return (-1);
if (rosc_find(name, dev) == NULL)
{
snprintf(msg, MAXRBUF, "Property %s is not defined in %s.", name, dev);
return -1;
}
/* ensure property is not RO */
for (int i = 0; i < nPropCache; i++)
{
if (!strcmp(propCache[i].propName, name) && !strcmp(propCache[i].devName, dev))
{
if (propCache[i].perm == IP_RO)
{
snprintf(msg, MAXRBUF, "Cannot set read-only property %s", name);
return -1;
}
else
break;
}
}
/* check tag in surmised decreasing order of likelyhood */
if (!strcmp(rtag, "newNumberVector"))
{
static double *doubles = NULL;
static char **names = NULL;
static int maxn = 0;
// Set locale to C and save previous value
locale_char_t *orig = indi_locale_C_numeric_push();
/* pull out each name/value pair */
for (n = 0, ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (strcmp(tagXMLEle(ep), "oneNumber") == 0)
{
XMLAtt *na = findXMLAtt(ep, "name");
if (na)
{
if (n >= maxn)
{
/* grow for this and another */
maxn = n + 1;
assert_mem(doubles = (double *)realloc(doubles, maxn * sizeof *doubles));
assert_mem(names = (char **)realloc(names, maxn * sizeof *names));
}
if (f_scansexa(pcdataXMLEle(ep), &doubles[n]) < 0)
IDMessage(dev, "[ERROR] %s: Bad format %s", name, pcdataXMLEle(ep));
else
names[n++] = valuXMLAtt(na);
}
}
}
// Reset locale settings to original value
indi_locale_C_numeric_pop(orig);
/* invoke driver if something to do, but not an error if not */
if (n > 0)
ISNewNumber(dev, name, doubles, names, n);
else
IDMessage(dev, "[ERROR] %s: newNumberVector with no valid members", name);
return (0);
}
if (!strcmp(rtag, "newSwitchVector"))
{
static ISState *states = NULL;
static char **names = NULL;
static int maxn = 0;
XMLEle *ep;
/* pull out each name/state pair */
for (n = 0, ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (strcmp(tagXMLEle(ep), "oneSwitch") == 0)
{
XMLAtt *na = findXMLAtt(ep, "name");
if (na)
{
if (n >= maxn)
{
maxn = n + 1;
assert_mem(states = (ISState *)realloc(states, maxn * sizeof *states));
assert_mem(names = (char **)realloc(names, maxn * sizeof *names));
}
if (strncmp(pcdataXMLEle(ep), "On", 2) == 0)
{
states[n] = ISS_ON;
names[n] = valuXMLAtt(na);
n++;
}
else if (strcmp(pcdataXMLEle(ep), "Off") == 0)
{
states[n] = ISS_OFF;
names[n] = valuXMLAtt(na);
n++;
}
else
IDMessage(dev, "[ERROR] %s: must be On or Off: %s", name, pcdataXMLEle(ep));
}
}
}
/* invoke driver if something to do, but not an error if not */
if (n > 0)
ISNewSwitch(dev, name, states, names, n);
else
IDMessage(dev, "[ERROR] %s: newSwitchVector with no valid members", name);
return (0);
}
if (!strcmp(rtag, "newTextVector"))
{
static char **texts = NULL;
static char **names = NULL;
static int maxn = 0;
/* pull out each name/text pair */
for (n = 0, ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (strcmp(tagXMLEle(ep), "oneText") == 0)
{
XMLAtt *na = findXMLAtt(ep, "name");
if (na)
{
if (n >= maxn)
{
maxn = n + 1;
assert_mem(texts = (char **)realloc(texts, maxn * sizeof *texts));
assert_mem(names = (char **)realloc(names, maxn * sizeof *names));
}
texts[n] = pcdataXMLEle(ep);
names[n] = valuXMLAtt(na);
n++;
}
}
}
/* invoke driver if something to do, but not an error if not */
if (n > 0)
ISNewText(dev, name, texts, names, n);
else
IDMessage(dev, "[ERROR] %s: set with no valid members", name);
return (0);
}
if (!strcmp(rtag, "newBLOBVector"))
{
static char **blobs = NULL;
static char **names = NULL;
static char **formats = NULL;
static int *blobsizes = NULL;
static int *sizes = NULL;
static int maxn = 0;
/* pull out each name/BLOB pair, decode */
for (n = 0, ep = nextXMLEle(root, 1); ep; ep = nextXMLEle(root, 0))
{
if (strcmp(tagXMLEle(ep), "oneBLOB") == 0)
{
XMLAtt *na = findXMLAtt(ep, "name");
XMLAtt *fa = findXMLAtt(ep, "format");