-
Notifications
You must be signed in to change notification settings - Fork 24
/
rgpio.c
1805 lines (1369 loc) · 38.7 KB
/
rgpio.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
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <netdb.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "rgpiod.h"
#include "rgpio.h"
#include "lgCfg.h"
#include "lgDbg.h"
#include "lgMD5.h"
#define LG_MAX_REPORTS_PER_READ 4096
#define STACK_SIZE (256*1024)
#define MAX_SBC 32
typedef void (*CBF_t) ();
struct callback_s
{
int id;
int sbc;
int chip;
int gpio;
int edge;
CBF_t f;
void * user;
callback_t *prev;
callback_t *next;
};
typedef struct
{
size_t count; // number of elements
size_t bytes; // bytes per element
size_t size; // total number of bytes
const void *ptr;
} lgExtent_t;
/* GLOBALS ---------------------------------------------------------------- */
static int gAbort = 0;
static int gPiInUse [MAX_SBC];
static int gPigCommand [MAX_SBC];
static int gPigHandle [MAX_SBC];
static int gPigNotify [MAX_SBC];
static uint32_t gLastLevel [MAX_SBC];
static pthread_t *gPthNotify [MAX_SBC];
static pthread_mutex_t gCmdMutex [MAX_SBC];
static int gCancelState [MAX_SBC];
static uint8_t *gMsgBuf [MAX_SBC];
static callback_t *gCallBackFirst = 0;
static callback_t *gCallBackLast = 0;
/* PRIVATE ---------------------------------------------------------------- */
static uint64_t xMakeSalt(void)
{
struct timespec xts;
clock_gettime(CLOCK_REALTIME, &xts);
return ((xts.tv_sec + xts.tv_nsec) * random()) + (random() + xts.tv_nsec);
}
static void xStopAll(void)
{
static int xInited = 0;
if (!xInited) gAbort = 1;
fflush(NULL);
xInited = 1;
signal(SIGINT, SIG_DFL);
raise(SIGINT);
}
static void xSignalHandler(int signum)
{
xStopAll();
}
static void _pml(int sbc)
{
int cancelState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelState);
pthread_mutex_lock(&gCmdMutex[sbc]);
gCancelState[sbc] = cancelState;
}
static void _pmu(int sbc)
{
int cancelState;
cancelState = gCancelState[sbc];
pthread_mutex_unlock(&gCmdMutex[sbc]);
pthread_setcancelstate(cancelState, NULL);
}
static int lg_command
(int sbc, int command, int extents, lgExtent_t *ext, int rl)
{
int i;
lgCmd_p h;
uint8_t *p;
size_t len;
if ((sbc < 0) || (sbc >= MAX_SBC) || !gPiInUse[sbc])
{
return lgif_unconnected_sbc;
}
if (gAbort)
{
rgpiod_stop(sbc);
return lgif_unconnected_sbc;
}
_pml(sbc);
p = gMsgBuf[sbc];
h = (lgCmd_p) p;
h->magic = LG_MAGIC;
h->size = 0;
h->cmd = command;
h->doubles = 0;
h->longs = 0;
h->shorts = 0;
p += sizeof(lgCmd_t);
for (i=0; i<extents; i++)
{
h->size += ext[i].size;
memcpy(p, ext[i].ptr, ext[i].size);
p += ext[i].size;
switch(ext[i].bytes)
{
case 8:
h->doubles += ext[i].count;
break;
case 4:
h->longs += ext[i].count;
break;
case 2:
h->shorts += ext[i].count;
break;
}
}
len = sizeof(lgCmd_t) + h->size;
/*
printf("tx=%s\n", lgDbgStr2Hex(len, (char *)h));
*/
if (send(gPigCommand[sbc], h, len, 0) != len)
{
_pmu(sbc);
return lgif_bad_send;
}
if (recv(gPigCommand[sbc], h, sizeof(lgCmd_t), MSG_WAITALL) !=
sizeof(lgCmd_t))
{
_pmu(sbc);
return lgif_bad_recv;
}
if (rl) _pmu(sbc);
/*
printf("rx=%s\n", lgDbgStr2Hex(sizeof(lgCmd_t), (char *)h));
*/
return h->status;
}
static int lg_notify(int sbc)
{
lgCmd_t h;
if ((sbc < 0) || (sbc >= MAX_SBC) || !gPiInUse[sbc])
return lgif_unconnected_sbc;
h.magic = LG_MAGIC;
h.size = 0;
h.cmd = LG_CMD_NOIB;
h.doubles = 0;
h.longs = 0;
h.shorts = 0;
_pml(sbc);
if (send(gPigNotify[sbc], &h, sizeof(h), 0) != sizeof(h))
{
_pmu(sbc);
return lgif_bad_send;
}
if (recv(gPigNotify[sbc], &h, sizeof(h), MSG_WAITALL) != sizeof(h))
{
_pmu(sbc);
return lgif_bad_recv;
}
_pmu(sbc);
return h.status;
}
static int lg_command_0(int sbc, int command, int rl)
{return lg_command(sbc, command, 0, NULL, rl);}
static int lg_command_1(int sbc, int command, int p1, int rl)
{
lgExtent_t ext[1];
uint32_t pars[]={p1};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
return lg_command(sbc, command, 1, ext, rl);
}
static int lg_command_2(int sbc, int command, int p1, int p2, int rl)
{
lgExtent_t ext[1];
uint32_t pars[]={p1, p2};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
return lg_command(sbc, command, 1, ext, rl);
}
static int lg_command_3(int sbc, int command, int p1, int p2, int p3, int rl)
{
lgExtent_t ext[1];
uint32_t pars[]={p1, p2, p3};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
return lg_command(sbc, command, 1, ext, rl);
}
static int lgOpenSocket(const char *addrStr, const char *portStr)
{
int sock, err, opt;
struct addrinfo hints, *res, *rp;
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
err = getaddrinfo (addrStr, portStr, &hints, &res);
if (err) return lgif_bad_getaddrinfo;
for (rp=res; rp!=NULL; rp=rp->ai_next)
{
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == -1) continue;
/* Disable the Nagle algorithm. */
opt = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(int));
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break;
}
freeaddrinfo(res);
if (rp == NULL) return lgif_bad_connect;
return sock;
}
static void dispatch_notification(int sbc, lgGpioReport_t *r)
{
callback_t *p;
/*
fprintf(stderr, "ts=%"PRIu64" c=%d g=%d l=%d f=%d\n",
r->timestamp, r->chip, r->gpio, r->level, r->flags);
*/
if (r->flags == 0)
{
p = gCallBackFirst;
while (p)
{
if ((p->sbc == sbc) && (p->chip == r->chip) && (p->gpio == r->gpio))
{
(p->f)(sbc, p->chip, p->gpio, r->level, r->timestamp, p->user);
}
p = p->next;
}
}
else /* no flags currently defined, ignore */
{
}
}
static void *pthNotifyThread(void *x)
{
static int got = 0;
int sbc;
int bytes, r;
lgGpioReport_t report[LG_MAX_REPORTS_PER_READ];
sbc = *((int*)x);
free(x); /* memory allocated in rgpiod_start */
while (1)
{
bytes = read(gPigNotify[sbc], (char*)&report+got, sizeof(report)-got);
if (bytes > 0) got += bytes;
else break;
r = 0;
while (got >= sizeof(lgGpioReport_t))
{
dispatch_notification(sbc, &report[r]);
r++;
got -= sizeof(lgGpioReport_t);
}
/* copy any partial report to start of array */
if (got && r) report[0] = report[r];
}
fprintf(stderr, "notify thread for sbc %d broke with read error %d\n",
sbc, bytes);
while (1) sleep(1);
return NULL;
}
static int intCallback(
int sbc, int chip, int gpio, int edge, void *f, void *user)
{
static int id = 0;
callback_t *p;
/*
printf("sbc=%d chip=%d gpio=%d edge=%d f=%p u=%p\n",
sbc, chip, gpio, edge, f, user);
*/
if ((chip < 64) && (gpio < 64) && (edge <= 3) && f)
{
/* prevent duplicates */
p = gCallBackFirst;
while (p)
{
if ((p->sbc == sbc) &&
(p->chip == chip) &&
(p->gpio == gpio) &&
(p->edge == edge) &&
(p->f == f))
{
return lgif_duplicate_callback;
}
p = p->next;
}
p = malloc(sizeof(callback_t));
if (p)
{
if (!gCallBackFirst) gCallBackFirst = p;
p->id = id++;
p->sbc = sbc;
p->chip = chip;
p->gpio = gpio;
p->edge = edge;
p->f = f;
p->user = user;
p->next = 0;
p->prev = gCallBackLast;
if (p->prev) (p->prev)->next = p;
gCallBackLast = p;
return p->id;
}
return lgif_bad_malloc;
}
return lgif_bad_callback;
}
static int recvMax(int sbc, void *buf, int bufsize, int sent)
{
/*
Copy at most bufSize bytes from the receieved message to
buf (if buf non-null). Discard the rest of the message.
*/
uint8_t scratch[4096];
int remaining, fetch, count;
remaining = sent;
if (sent < bufsize) count = sent; else count = bufsize;
if (count && buf)
{
recv(gPigCommand[sbc], buf, count, MSG_WAITALL);
remaining -= count;
/*
printf("recvMax(bs=%d sent=%d): %s\n",
bufsize, sent, lgDbgStr2Hex(count, (unsigned char *)buf));
*/
}
while (remaining)
{
fetch = remaining;
if (fetch > sizeof(scratch)) fetch = sizeof(scratch);
recv(gPigCommand[sbc], scratch, fetch, MSG_WAITALL);
remaining -= fetch;
}
return count;
}
/* PUBLIC ----------------------------------------------------------------- */
/* START/STOP */
int rgpiod_start(const char *addrStr, const char *portStr)
{
static int xInited = 0;
int sbc;
int *userdata;
struct sigaction new_action, old_action;
const char *userStr;
if (!xInited)
{
for (sbc=0; sbc<MAX_SBC; sbc++)
pthread_mutex_init(&gCmdMutex[sbc], NULL);
/* Set up the structure to specify the new action. */
new_action.sa_handler = xSignalHandler;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction (SIGINT, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction (SIGINT, &new_action, NULL);
atexit(xStopAll);
xInited = 1;
}
for (sbc=0; sbc<MAX_SBC; sbc++)
{
_pml(sbc);
if (!gPiInUse[sbc])
{
gPiInUse[sbc] = 1;
_pmu(sbc);
break;
}
_pmu(sbc);
}
if (sbc >= MAX_SBC) return lgif_too_many_pis;
if ((!addrStr) || (!strlen(addrStr)))
{
addrStr = getenv(LG_ENVADDR);
if ((!addrStr) || (!strlen(addrStr)))
{
addrStr = LG_DEFAULT_SOCKET_ADDR_STR;
}
}
if ((!portStr) || (!strlen(portStr)))
{
portStr = getenv(LG_ENVPORT);
if ((!portStr) || (!strlen(portStr)))
{
portStr = LG_DEFAULT_SOCKET_PORT_STR;
}
}
gPigCommand[sbc] = lgOpenSocket(addrStr, portStr);
if (gPigCommand[sbc] >= 0)
{
gPigNotify[sbc] = lgOpenSocket(addrStr, portStr);
if (gPigNotify[sbc] >= 0)
{
gPigHandle[sbc] = lg_notify(sbc);
if (gPigHandle[sbc] < 0) return lgif_bad_noib;
else
{
gLastLevel[sbc] = 0;
/* must be freed by pthNotifyThread */
userdata = malloc(sizeof(*userdata));
*userdata = sbc;
gPthNotify[sbc] = thread_start(pthNotifyThread, userdata);
if (gPthNotify[sbc])
{
gMsgBuf[sbc] = malloc(CMD_MAX_EXTENSION);
if (gMsgBuf[sbc] != NULL)
{
userStr = getenv(LG_ENVUSER);
if (userStr && strlen(userStr))
{
lgu_set_user(sbc, userStr, NULL);
}
return sbc;
}
else return lgif_bad_malloc;
}
else return lgif_notify_failed;
}
}
else return gPigNotify[sbc];
}
else return gPigCommand[sbc];
}
void rgpiod_stop(int sbc)
{
if ((sbc < 0) || (sbc >= MAX_SBC) || !gPiInUse[sbc]) return;
if (gPthNotify[sbc])
{
thread_stop(gPthNotify[sbc]);
gPthNotify[sbc] = 0;
}
if (gPigCommand[sbc] >= 0)
{
//lg_command_0(sbc, LG_CMD_FREE, 1);
if (gPigHandle[sbc] >= 0)
{
//lg_command_1(sbc, LG_CMD_NC, gPigHandle[sbc], 1);
gPigHandle[sbc] = -1;
}
close(gPigCommand[sbc]);
gPigCommand[sbc] = -1;
}
if (gPigNotify[sbc] >= 0)
{
close(gPigNotify[sbc]);
gPigNotify[sbc] = -1;
}
_pml(sbc);
gPiInUse[sbc] = 0;
free(gMsgBuf[sbc]);
gMsgBuf[sbc] = NULL;
_pmu(sbc);
}
/* FILES */
int file_open(int sbc, const char *file, int mode)
{
lgExtent_t ext[2];
uint32_t pars[] = {mode};
int len;
len = strlen(file);
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
ext[1].size = len;
ext[1].count = len;
ext[1].bytes = 1;
ext[1].ptr = file;
return lg_command(sbc, LG_CMD_FO, 2, ext, 1);
}
int file_close(int sbc, int handle)
{return lg_command_1(sbc, LG_CMD_FC, handle, 1);}
int file_write(int sbc, int handle, const char *buf, int count)
{
lgExtent_t ext[2];
uint32_t pars[]={handle};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
ext[1].size = count;
ext[1].ptr = buf;
return lg_command(sbc, LG_CMD_FW, 2, ext, 1);
}
int file_read(int sbc, int handle, char *buf, int count)
{
int bytes;
bytes = lg_command_2(sbc, LG_CMD_FR, handle, count, 0);
if (bytes > 0)
{
bytes = recvMax(sbc, buf, count, bytes);
}
_pmu(sbc);
return bytes;
}
int file_seek(int sbc, int handle, int32_t seekOffset, int seekFrom)
{return lg_command_3(sbc, LG_CMD_FS, handle, seekOffset, seekFrom, 1);}
int file_list(int sbc, const char *fpat, char *buf, int count)
{
int len;
int bytes;
uint32_t pars[] = {60000};
lgExtent_t ext[2];
len = strlen(fpat);
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
ext[1].size = len;
ext[1].count = len;
ext[1].bytes = 1;
ext[1].ptr = fpat;
bytes = lg_command(sbc, LG_CMD_FL, 2, ext, 0);
if (bytes > 0)
{
bytes = recvMax(sbc, buf, count, bytes);
}
_pmu(sbc);
return bytes;
}
/* GPIO */
int gpiochip_open(int sbc, int device)
{
int status;
status = lg_command_1(sbc, LG_CMD_GO, device, 1);
if (status >= 0) status |= (device<<16);
return status;
}
int gpiochip_close(int sbc, int handle)
{return lg_command_1(sbc, LG_CMD_GC, handle&0xffff, 1);}
int gpio_get_chip_info(int sbc, int handle, lgChipInfo_p chipInfP)
{
int status;
int bytes;
lgExtent_t ext[1];
uint32_t pars[] = {handle&0xffff};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
bytes = lg_command(sbc, LG_CMD_GIC, 1, ext, 0);
if (bytes > 0)
{
recvMax(sbc, chipInfP, sizeof(lgChipInfo_t), bytes);
status = LG_OKAY;
}
else status = bytes;
_pmu(sbc);
return status;
}
int gpio_get_line_info(int sbc, int handle, int gpio, lgLineInfo_p lineInfP)
{
int status;
int bytes;
lgExtent_t ext[1];
uint32_t pars[] = {handle&0xffff, gpio};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
bytes = lg_command(sbc, LG_CMD_GIL, 1, ext, 0);
if (bytes > 0)
{
recvMax(sbc, lineInfP, sizeof(lgLineInfo_t), bytes);
status = LG_OKAY;
}
else status = bytes;
_pmu(sbc);
return status;
}
int gpio_get_mode(int sbc, int handle, int gpio)
{return lg_command_2(sbc, LG_CMD_GMODE, handle&0xffff, gpio, 1);}
int group_read(int sbc, int handle, int group, uint64_t *value)
{
int status;
int bytes;
lgExtent_t ext[1];
uint32_t pars[] = {handle&0xffff, group};
uint8_t retval[12];
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
bytes = lg_command(sbc, LG_CMD_GGR, 1, ext, 0);
if (bytes > 0)
{
recvMax(sbc, &retval, 12, bytes);
*value = *(uint64_t*)retval;
status = *(uint32_t*)(retval+8);
}
else status = bytes;
_pmu(sbc);
return status;
}
int group_write(
int sbc, int handle, int group, uint64_t groupBits, uint64_t groupMask)
{
lgExtent_t ext[2];
uint64_t parq[] = {groupBits, groupMask};
uint32_t pars[] = {handle&0xffff, group};
ext[0].size = sizeof(parq);
ext[0].count = sizeof(parq)/sizeof(parq[0]);
ext[0].bytes = sizeof(parq[0]);
ext[0].ptr = &parq;
ext[1].size = sizeof(pars);
ext[1].count = sizeof(pars)/sizeof(pars[0]);
ext[1].bytes = sizeof(pars[0]);
ext[1].ptr = &pars;
return lg_command(sbc, LG_CMD_GGWX, 2, ext, 1);
}
int gpio_claim_input(int sbc, int handle, int lFlags, int gpio)
{return lg_command_3(sbc, LG_CMD_GSIX, handle&0xffff, lFlags, gpio, 1);}
int group_claim_input(
int sbc, int handle, int lFlags, int size, const int *gpio)
{
lgExtent_t ext[2];
uint32_t pars[] = {handle&0xffff, lFlags};
int i;
int status=LG_NO_MEMORY;
uint32_t *g32 = malloc(size * 4);
if (g32)
{
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
ext[1].size = size*4;
ext[1].count = size;
ext[1].bytes = 4;
ext[1].ptr = g32;
for (i=0; i<size; i++) g32[i] = gpio[i];
status = lg_command(sbc, LG_CMD_GSGIX, 2, ext, 1);
free (g32);
}
return status;
}
int gpio_claim_output(
int sbc, int handle, int lFlags, int gpio, int value)
{
lgExtent_t ext[1];
uint32_t pars[] = {handle&0xffff, lFlags, gpio, value};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
return lg_command(sbc, LG_CMD_GSOX, 1, ext, 1);
}
int group_claim_output(
int sbc, int handle, int lFlags,
int size, const int *gpio, const int *values)
{
lgExtent_t ext[2];
uint32_t pars[] = {handle&0xffff, lFlags};
int i;
int status=LG_NO_MEMORY;
uint32_t *g32 = malloc(size * 8);
if (g32)
{
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
ext[1].size = size*8;
ext[1].count = size*2;
ext[1].bytes = 4;
ext[1].ptr = g32;
for (i=0; i<size; i++)
{
g32[i] = gpio[i];
g32[i+size] = values[i];
}
status = lg_command(sbc, LG_CMD_GSGOX, 2, ext, 1);
free (g32);
}
return status;
}
int gpio_free(int sbc, int handle, int gpio)
{return lg_command_2(sbc, LG_CMD_GSF, handle&0xffff, gpio, 1);}
int group_free(int sbc, int handle, int gpio)
{return lg_command_2(sbc, LG_CMD_GSGF, handle&0xffff, gpio, 1);}
int gpio_read(int sbc, int handle, int gpio)
{return lg_command_2(sbc, LG_CMD_GR, handle&0xffff, gpio, 1);}
int gpio_write(int sbc, int handle, int gpio, int value)
{return lg_command_3(sbc, LG_CMD_GW, handle&0xffff, gpio, value, 1);}
int gpio_set_debounce_time(int sbc, int handle, int gpio, int debounce_us)
{
return lg_command_3(
sbc, LG_CMD_GDEB, handle&0xffff, gpio, debounce_us, 1);
}
int gpio_set_watchdog_time(int sbc, int handle, int gpio, int watchdog_us)
{
return lg_command_3(
sbc, LG_CMD_GWDOG, handle&0xffff, gpio, watchdog_us, 1);
}
int tx_pulse(
int sbc, int handle, int gpio,
int micros_on, int micros_off,
int offset, int cycles)
{
lgExtent_t ext[1];
uint32_t pars[] =
{handle&0xffff, gpio, micros_on, micros_off, offset, cycles};
ext[0].size = sizeof(pars);
ext[0].count = sizeof(pars)/sizeof(pars[0]);
ext[0].bytes = sizeof(pars[0]);
ext[0].ptr = &pars;
return lg_command(sbc, LG_CMD_GPX, 1, ext, 1);
}
int tx_pwm(
int sbc, int handle, int gpio,
float pwmFrequency, float pwmDutyCycle,
int offset, int cycles)
{
lgExtent_t ext[1];
uint32_t pars[] =
{handle&0xffff, gpio, pwmFrequency*1000, pwmDutyCycle*1000,
offset, cycles};
ext[0].size = sizeof(pars);