-
Notifications
You must be signed in to change notification settings - Fork 318
/
iiod-client.c
1867 lines (1491 loc) · 41.9 KB
/
iiod-client.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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014-2020 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "iio-config.h"
#include "iio-private.h"
#include "iiod-responder.h"
#include <iio/iiod-client.h>
#include <iio/iio-backend.h>
#include <iio/iio-debug.h>
#include <iio/iio-lock.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#if WITH_ZSTD
#include <zstd.h>
#endif
struct iiod_client {
const struct iio_context_params *params;
struct iiod_client_pdata *desc;
const struct iiod_client_ops *ops;
struct iio_mutex *lock;
struct iiod_responder *responder;
/* TODO: atomic? */
uint16_t next_evstream_idx;
};
struct iiod_client_io {
struct iiod_client *client;
bool cyclic;
size_t samples_count;
size_t buffer_size;
const struct iio_device *dev;
};
static int iiod_client_enable_binary(struct iiod_client *client);
struct iiod_client_buffer_pdata {
struct iiod_client *client;
struct iiod_client *client_fb;
struct iiod_client_io *io;
struct iio_channels_mask *mask;
const struct iio_device *dev;
uint16_t idx;
/* TODO: atomic? */
uint16_t next_block_idx;
};
struct iio_block_pdata {
struct iiod_client_buffer_pdata *buffer;
struct iiod_io *io;
struct iio_mutex *lock;
size_t size;
uint64_t bytes_used;
uint16_t idx;
void *data;
bool enqueued;
bool retry_dequeue;
};
struct iio_event_stream_pdata {
struct iiod_client *client;
const struct iio_device *dev;
struct iio_event event;
struct iiod_io *io;
uint16_t idx;
};
void iiod_client_mutex_lock(struct iiod_client *client)
{
iio_mutex_lock(client->lock);
}
void iiod_client_mutex_unlock(struct iiod_client *client)
{
iio_mutex_unlock(client->lock);
}
static ssize_t iiod_client_read_integer(struct iiod_client *client, int *val)
{
bool accept_eol = false, has_read_line = !!client->ops->read_line;
unsigned int i, nb, first = 0, timeout_ms = client->params->timeout_ms;
unsigned int remaining = 0;
int64_t start_time = 0, diff_ms;
char buf[1024], *end;
ssize_t ret;
int value;
if (has_read_line) {
ret = client->ops->read_line(client->desc, buf,
sizeof(buf), timeout_ms);
if (ret < 0)
return ret;
nb = (unsigned int) ret;
} else {
start_time = iiod_responder_read_counter_us();
nb = sizeof(buf);
}
for (i = 0; i < nb; i++) {
if (!has_read_line) {
diff_ms = (iiod_responder_read_counter_us() - start_time) / 1000;
if (timeout_ms) {
if (diff_ms >= timeout_ms)
return -ETIMEDOUT;
remaining = (unsigned int)((int64_t)timeout_ms - diff_ms);
}
ret = client->ops->read(client->desc, &buf[i], 1, remaining);
if (ret < 0)
return ret;
}
if (buf[i] != '\n')
accept_eol = true;
else if (!accept_eol)
first = i + 1;
else
break;
};
if (i == nb)
return -EINVAL;
buf[i] = '\0';
errno = 0;
value = (int) strtol(&buf[first], &end, 10);
if (buf == end || errno == ERANGE)
return -EINVAL;
*val = value;
return 0;
}
static ssize_t iiod_client_write_all(struct iiod_client *client,
const void *src, size_t len)
{
const struct iiod_client_ops *ops = client->ops;
struct iiod_client_pdata *desc = client->desc;
uintptr_t ptr = (uintptr_t) src;
unsigned int remaining = 0, timeout_ms = client->params->timeout_ms;
uint64_t start_time, diff_ms;
ssize_t ret;
start_time = iiod_responder_read_counter_us();
if (iiod_client_uses_binary_interface(client))
timeout_ms = 0;
while (len) {
diff_ms = (iiod_responder_read_counter_us() - start_time) / 1000;
if (timeout_ms) {
if (diff_ms >= timeout_ms)
return -ETIMEDOUT;
remaining = (unsigned int)((int64_t)timeout_ms - diff_ms);
}
ret = ops->write(desc, (const void *) ptr, len, remaining);
if (ret < 0) {
if (ret == -EINTR)
continue;
else
return ret;
}
if (ret == 0)
return -EPIPE;
ptr += ret;
len -= ret;
}
return (ssize_t) (ptr - (uintptr_t) src);
}
static int iiod_client_exec_command(struct iiod_client *client, const char *cmd)
{
int resp;
ssize_t ret;
ret = iiod_client_write_all(client, cmd, strlen(cmd));
if (ret < 0)
return (int) ret;
ret = iiod_client_read_integer(client, &resp);
return ret < 0 ? (int) ret : resp;
}
static ssize_t iiod_client_read_all(struct iiod_client *client,
void *dst, size_t len)
{
const struct iiod_client_ops *ops = client->ops;
uintptr_t ptr = (uintptr_t) dst;
unsigned int remaining = 0, timeout_ms = client->params->timeout_ms;
uint64_t start_time, diff_ms;
ssize_t ret;
start_time = iiod_responder_read_counter_us();
if (iiod_client_uses_binary_interface(client))
timeout_ms = 0;
while (len) {
diff_ms = (iiod_responder_read_counter_us() - start_time) / 1000;
if (timeout_ms) {
if (diff_ms >= timeout_ms)
return -ETIMEDOUT;
remaining = (unsigned int)((int64_t)timeout_ms - diff_ms);
}
ret = ops->read(client->desc, (void *) ptr, len, remaining);
if (ret < 0) {
if (ret == -EINTR)
continue;
else
return ret;
}
if (ret == 0)
return -EPIPE;
ptr += ret;
len -= ret;
}
return (ssize_t) (ptr - (uintptr_t) dst);
}
static void iiod_client_cancel(struct iiod_client *client)
{
if (client->ops->cancel)
client->ops->cancel(client->desc);
}
struct iiod_client * iiod_client_new(const struct iio_context_params *params,
struct iiod_client_pdata *desc,
const struct iiod_client_ops *ops)
{
struct iiod_client *client;
int err;
client = malloc(sizeof(*client));
if (!client)
return iio_ptr(-ENOMEM);
client->lock = iio_mutex_create();
err = iio_err(client->lock);
if (err)
goto err_free_client;
client->params = params;
client->ops = ops;
client->desc = desc;
client->responder = NULL;
client->next_evstream_idx = (uint16_t)-1;
err = iiod_client_enable_binary(client);
if (err)
goto err_free_lock;
err = iiod_client_set_timeout(client, params->timeout_ms);
if (err)
goto err_free_responder;
return client;
err_free_responder:
if (client->responder) {
iiod_client_cancel(client);
iiod_responder_destroy(client->responder);
}
err_free_lock:
iio_mutex_destroy(client->lock);
err_free_client:
free(client);
return iio_ptr(err);
}
void iiod_client_destroy(struct iiod_client *client)
{
if (client->responder) {
iiod_client_cancel(client);
iiod_responder_destroy(client->responder);
}
iio_mutex_destroy(client->lock);
free(client);
}
static int iio_device_get_index(const struct iio_device *dev)
{
const struct iio_context *ctx = iio_device_get_context(dev);
unsigned int idx;
for (idx = 0; idx < iio_context_get_devices_count(ctx); idx++)
if (dev == iio_context_get_device(ctx, idx))
return idx;
return -ENODEV; /* Cannot happen */
}
static const struct iio_device *
iiod_client_get_trigger_new(struct iiod_client *client,
const struct iio_device *dev)
{
const struct iio_context *ctx = iio_device_get_context(dev);
struct iiod_io *io = iiod_responder_get_default_io(client->responder);
struct iiod_command cmd;
int ret;
cmd.op = IIOD_OP_GETTRIG;
cmd.dev = (uint8_t) iio_device_get_index(dev);
ret = iiod_io_exec_simple_command(io, &cmd);
if (ret < 0)
return iio_ptr(ret);
return iio_context_get_device(ctx, ret);
}
const struct iio_device * iiod_client_get_trigger(struct iiod_client *client,
const struct iio_device *dev)
{
const struct iio_context *ctx = iio_device_get_context(dev);
unsigned int i, nb_devices = iio_context_get_devices_count(ctx);
char buf[1024];
unsigned int name_len;
int ret;
if (iiod_client_uses_binary_interface(client))
return iiod_client_get_trigger_new(client, dev);
iio_snprintf(buf, sizeof(buf), "GETTRIG %s\r\n",
iio_device_get_id(dev));
iio_mutex_lock(client->lock);
ret = iiod_client_exec_command(client, buf);
if (ret == 0)
ret = -ENODEV;
if (ret <= 0)
goto out_unlock;
if ((unsigned int) ret > sizeof(buf) - 1) {
ret = -EIO;
goto out_unlock;
}
name_len = ret;
ret = (int) iiod_client_read_all(client, buf, name_len + 1);
if (ret < 0)
goto out_unlock;
ret = -ENXIO;
for (i = 0; i < nb_devices; i++) {
struct iio_device *cur = iio_context_get_device(ctx, i);
if (iio_device_is_trigger(cur)) {
const char *name = iio_device_get_name(cur);
if (!name)
continue;
if (!strncmp(name, buf, name_len)) {
iio_mutex_unlock(client->lock);
return cur;
}
}
}
out_unlock:
iio_mutex_unlock(client->lock);
return iio_ptr(ret);
}
static int iiod_client_set_trigger_new(struct iiod_client *client,
const struct iio_device *dev,
const struct iio_device *trigger)
{
struct iiod_io *io = iiod_responder_get_default_io(client->responder);
struct iiod_command cmd;
cmd.op = IIOD_OP_SETTRIG;
cmd.dev = (uint8_t) iio_device_get_index(dev);
if (!trigger)
cmd.code = -1;
else
cmd.code = iio_device_get_index(trigger);
return iiod_io_exec_simple_command(io, &cmd);
}
int iiod_client_set_trigger(struct iiod_client *client,
const struct iio_device *dev,
const struct iio_device *trigger)
{
char buf[1024];
int ret;
if (iiod_client_uses_binary_interface(client))
return iiod_client_set_trigger_new(client, dev, trigger);
if (trigger) {
iio_snprintf(buf, sizeof(buf), "SETTRIG %s %s\r\n",
iio_device_get_id(dev),
iio_device_get_id(trigger));
} else {
iio_snprintf(buf, sizeof(buf), "SETTRIG %s\r\n",
iio_device_get_id(dev));
}
iio_mutex_lock(client->lock);
ret = iiod_client_exec_command(client, buf);
iio_mutex_unlock(client->lock);
return ret;
}
static unsigned int calculate_remote_timeout(unsigned int timeout_ms)
{
/* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
* by the remote. Is there something better to do here? */
return timeout_ms / 2;
}
int iiod_client_set_timeout(struct iiod_client *client, unsigned int timeout)
{
unsigned int remote_timeout = calculate_remote_timeout(timeout);
struct iiod_io *io;
int ret;
if (iiod_client_uses_binary_interface(client)) {
struct iiod_command cmd;
iiod_responder_set_timeout(client->responder, timeout);
cmd.op = IIOD_OP_TIMEOUT;
cmd.code = remote_timeout;
io = iiod_responder_get_default_io(client->responder);
ret = iiod_io_exec_simple_command(io, &cmd);
} else {
char buf[1024];
iio_mutex_lock(client->lock);
iio_snprintf(buf, sizeof(buf), "TIMEOUT %u\r\n", remote_timeout);
ret = iiod_client_exec_command(client, buf);
iio_mutex_unlock(client->lock);
if (ret == -EINVAL) {
/* The TIMEOUT command is not implemented in tinyiiod
* based programs; so ignore if we get -EINVAL here. */
prm_dbg(client->params, "Unable to set remote timeout\n");
ret = 0;
}
}
return ret;
}
static int iiod_client_discard(struct iiod_client *client,
char *buf, size_t buf_len, size_t to_discard)
{
do {
size_t read_len;
ssize_t ret;
if (to_discard > buf_len)
read_len = buf_len;
else
read_len = to_discard;
ret = iiod_client_read_all(client, buf, read_len);
if (ret < 0)
return (int) ret;
to_discard -= (size_t) ret;
} while (to_discard);
return 0;
}
static ssize_t iiod_client_read_attr_new(struct iiod_client *client,
const struct iio_attr *attr,
char *dest, size_t len)
{
struct iiod_io *io = iiod_responder_get_default_io(client->responder);
const struct iio_channel *chn;
const struct iio_device *dev;
const struct iio_buffer *buf;
struct iiod_command cmd = { 0 };
unsigned int i;
uint16_t arg1, arg2 = 0;
struct iiod_buf iiod_buf;
switch (attr->type) {
case IIO_ATTR_TYPE_CHANNEL:
chn = attr->iio.chn;
dev = iio_channel_get_device(chn);
cmd.op = IIOD_OP_READ_CHN_ATTR;
for (i = 0; i < iio_device_get_channels_count(dev); i++)
if (iio_device_get_channel(dev, i) == chn)
break;
arg2 = (uint16_t) i;
for (i = 0; i < iio_channel_get_attrs_count(chn); i++)
if (iio_channel_get_attr(chn, i) == attr)
break;
if (i == iio_channel_get_attrs_count(chn))
return -ENOENT;
arg1 = (uint16_t) i;
break;
case IIO_ATTR_TYPE_DEVICE:
dev = attr->iio.dev;
cmd.op = IIOD_OP_READ_ATTR;
for (i = 0; i < iio_device_get_attrs_count(dev); i++)
if (iio_device_get_attr(dev, i) == attr)
break;
if (i == iio_device_get_attrs_count(dev))
return -ENOENT;
arg1 = (uint16_t) i;
break;
case IIO_ATTR_TYPE_DEBUG:
dev = attr->iio.dev;
cmd.op = IIOD_OP_READ_DBG_ATTR;
for (i = 0; i < iio_device_get_debug_attrs_count(dev); i++)
if (iio_device_get_debug_attr(dev, i) == attr)
break;
if (i == iio_device_get_debug_attrs_count(dev))
return -ENOENT;
arg1 = (uint16_t) i;
break;
case IIO_ATTR_TYPE_BUFFER:
buf = attr->iio.buf;
dev = iio_buffer_get_device(buf);
cmd.op = IIOD_OP_READ_BUF_ATTR;
for (i = 0; i < iio_buffer_get_attrs_count(buf); i++)
if (iio_buffer_get_attr(buf, i) == attr)
break;
if (i == iio_buffer_get_attrs_count(buf))
return -ENOENT;
arg1 = (uint16_t) i;
arg2 = (uint16_t) buf->idx;
break;
default:
return -EINVAL;
}
cmd.dev = (uint8_t) iio_device_get_index(dev);
cmd.code = (arg1 << 16) | arg2;
iiod_buf.ptr = dest;
iiod_buf.size = len;
return iiod_io_exec_command(io, &cmd, NULL, &iiod_buf);
}
ssize_t iiod_client_attr_read(struct iiod_client *client,
const struct iio_attr *attr,
char *dest, size_t len)
{
const struct iio_device *dev;
const char *id;
char buf[1024];
ssize_t ret;
if (iiod_client_uses_binary_interface(client))
return iiod_client_read_attr_new(client, attr, dest, len);
switch (attr->type) {
case IIO_ATTR_TYPE_CHANNEL:
dev = iio_channel_get_device(attr->iio.chn);
break;
case IIO_ATTR_TYPE_BUFFER:
dev = iio_buffer_get_device(attr->iio.buf);
break;
default:
dev = attr->iio.dev;
break;
}
id = iio_device_get_id(dev);
switch (attr->type) {
case IIO_ATTR_TYPE_CHANNEL:
iio_snprintf(buf, sizeof(buf), "READ %s %s %s %s\r\n", id,
iio_channel_is_output(attr->iio.chn) ? "OUTPUT" : "INPUT",
iio_channel_get_id(attr->iio.chn), attr->name);
break;
case IIO_ATTR_TYPE_DEVICE:
iio_snprintf(buf, sizeof(buf), "READ %s %s\r\n",
id, attr->name);
break;
case IIO_ATTR_TYPE_DEBUG:
iio_snprintf(buf, sizeof(buf), "READ %s DEBUG %s\r\n",
id, attr->name);
break;
case IIO_ATTR_TYPE_BUFFER:
iio_snprintf(buf, sizeof(buf), "READ %s BUFFER %s\r\n",
id, attr->name);
break;
case IIO_ATTR_TYPE_CONTEXT:
return -EINVAL;
}
iio_mutex_lock(client->lock);
ret = (ssize_t) iiod_client_exec_command(client, buf);
if (ret < 0)
goto out_unlock;
if ((size_t) ret + 1 > len) {
iiod_client_discard(client, dest, len, ret + 1);
ret = -EIO;
goto out_unlock;
}
/* +1: Also read the trailing \n */
ret = iiod_client_read_all(client, dest, ret + 1);
if (ret > 0) {
/* Discard the trailing \n */
ret--;
/* Replace it with a \0 just in case */
dest[ret] = '\0';
}
out_unlock:
iio_mutex_unlock(client->lock);
return ret;
}
static ssize_t iiod_client_write_attr_new(struct iiod_client *client,
const struct iio_attr *attr,
const char *src, size_t len)
{
struct iiod_io *io = iiod_responder_get_default_io(client->responder);
const struct iio_channel *chn;
const struct iio_device *dev;
const struct iio_buffer *buf;
struct iiod_command cmd = { 0 };
uint16_t arg1, arg2 = 0;
struct iiod_buf iiod_buf[2];
uint64_t length = (uint64_t) len;
unsigned int i;
int ret;
switch (attr->type) {
case IIO_ATTR_TYPE_CHANNEL:
chn = attr->iio.chn;
dev = iio_channel_get_device(chn);
cmd.op = IIOD_OP_WRITE_CHN_ATTR;
for (i = 0; i < iio_device_get_channels_count(dev); i++)
if (iio_device_get_channel(dev, i) == chn)
break;
arg2 = (uint16_t) i;
for (i = 0; i < iio_channel_get_attrs_count(chn); i++)
if (iio_channel_get_attr(chn, i) == attr)
break;
if (i == iio_channel_get_attrs_count(chn))
return -ENOENT;
arg1 = (uint16_t) i;
break;
case IIO_ATTR_TYPE_DEVICE:
dev = attr->iio.dev;
cmd.op = IIOD_OP_WRITE_ATTR;
for (i = 0; i < iio_device_get_attrs_count(dev); i++)
if (iio_device_get_attr(dev, i) == attr)
break;
if (i == iio_device_get_attrs_count(dev))
return -ENOENT;
arg1 = (uint16_t) i;
break;
case IIO_ATTR_TYPE_DEBUG:
dev = attr->iio.dev;
cmd.op = IIOD_OP_WRITE_DBG_ATTR;
for (i = 0; i < iio_device_get_debug_attrs_count(dev); i++)
if (iio_device_get_debug_attr(dev, i) == attr)
break;
if (i == iio_device_get_debug_attrs_count(dev))
return -ENOENT;
arg1 = (uint16_t) i;
break;
case IIO_ATTR_TYPE_BUFFER:
buf = attr->iio.buf;
dev = iio_buffer_get_device(buf);
cmd.op = IIOD_OP_WRITE_BUF_ATTR;
for (i = 0; i < iio_buffer_get_attrs_count(buf); i++)
if (iio_buffer_get_attr(buf, i) == attr)
break;
if (i == iio_buffer_get_attrs_count(buf))
return -ENOENT;
arg1 = (uint16_t) i;
arg2 = (uint16_t) buf->idx;
break;
default:
return -EINVAL;
}
cmd.dev = (uint8_t) iio_device_get_index(dev);
cmd.code = (arg1 << 16) | arg2;
iiod_buf[0].ptr = &length;
iiod_buf[0].size = sizeof(length);
iiod_buf[1].ptr = (void *) src;
iiod_buf[1].size = len;
iiod_io_get_response_async(io, NULL, 0);
ret = iiod_io_send_command(io, &cmd, iiod_buf, ARRAY_SIZE(iiod_buf));
if (ret < 0) {
iiod_io_cancel(io);
return ret;
}
return (ssize_t) iiod_io_wait_for_response(io);
}
ssize_t iiod_client_attr_write(struct iiod_client *client,
const struct iio_attr *attr,
const char *src, size_t len)
{
const struct iio_device *dev;
const char *id;
char buf[1024];
ssize_t ret;
int resp;
if (iiod_client_uses_binary_interface(client))
return iiod_client_write_attr_new(client, attr, src, len);
switch (attr->type) {
case IIO_ATTR_TYPE_CHANNEL:
dev = iio_channel_get_device(attr->iio.chn);
break;
case IIO_ATTR_TYPE_BUFFER:
dev = iio_buffer_get_device(attr->iio.buf);
break;
default:
dev = attr->iio.dev;
break;
}
id = iio_device_get_id(dev);
switch (attr->type) {
case IIO_ATTR_TYPE_CHANNEL:
iio_snprintf(buf, sizeof(buf), "WRITE %s %s %s %s %lu\r\n", id,
iio_channel_is_output(attr->iio.chn) ? "OUTPUT" : "INPUT",
iio_channel_get_id(attr->iio.chn), attr->name,
(unsigned long) len);
break;
case IIO_ATTR_TYPE_DEVICE:
iio_snprintf(buf, sizeof(buf), "WRITE %s %s %lu\r\n",
id, attr->name, (unsigned long) len);
break;
case IIO_ATTR_TYPE_DEBUG:
iio_snprintf(buf, sizeof(buf), "WRITE %s DEBUG %s %lu\r\n",
id, attr->name, (unsigned long) len);
break;
case IIO_ATTR_TYPE_BUFFER:
iio_snprintf(buf, sizeof(buf), "WRITE %s BUFFER %s %lu\r\n",
id, attr->name, (unsigned long) len);
break;
case IIO_ATTR_TYPE_CONTEXT:
return -EINVAL;
}
iio_mutex_lock(client->lock);
ret = iiod_client_write_all(client, buf, strlen(buf));
if (ret < 0)
goto out_unlock;
ret = iiod_client_write_all(client, src, len);
if (ret < 0)
goto out_unlock;
ret = iiod_client_read_integer(client, &resp);
if (ret < 0)
goto out_unlock;
ret = (ssize_t) resp;
out_unlock:
iio_mutex_unlock(client->lock);
return ret;
}
static int iiod_client_cmd(const struct iiod_command *cmd,
struct iiod_command_data *data, void *d)
{
/* We don't support receiving commands. */
return -EINVAL;
}
static ssize_t
iiod_client_read_cb(void *d, const struct iiod_buf *buf, size_t nb)
{
struct iiod_client *client = d;
ssize_t ret, count = 0;
unsigned int i;
for (i = 0; i < nb; i++) {
ret = iiod_client_read_all(client, buf[i].ptr, buf[i].size);
if (ret <= 0)
return ret;
count += ret;
}
return count;
}
static ssize_t
iiod_client_write_cb(void *d, const struct iiod_buf *buf, size_t nb)
{
struct iiod_client *client = d;
ssize_t ret, count = 0;
unsigned int i;
for (i = 0; i < nb; i++) {
ret = iiod_client_write_all(client, buf[i].ptr, buf[i].size);
if (ret <= 0)
return ret;
count += ret;
}
return count;
}
static ssize_t iiod_client_discard_cb(void *d, size_t bytes)
{
struct iiod_client *client = d;
char buf[0x1000];
int ret;
ret = iiod_client_discard(client, buf, sizeof(buf), bytes);
if (ret < 0)
return ret;
return bytes;
}
static const struct iiod_responder_ops iiod_client_ops = {
.cmd = iiod_client_cmd,
.read = iiod_client_read_cb,
.write = iiod_client_write_cb,
.discard = iiod_client_discard_cb,
};
static int iiod_client_enable_binary(struct iiod_client *client)
{
int ret;
ret = iiod_client_exec_command(client, "BINARY\r\n");
/* If the BINARY command fail, don't create the responder */
if (ret != 0)
return 0;
client->responder = iiod_responder_create(&iiod_client_ops, client);
if (!client->responder) {
prm_err(client->params, "Unable to create responder\n");
return -ENOMEM;
}
return 0;
}
#if WITH_ZSTD
static int iiod_client_send_print(struct iiod_client *client,
void *buf, size_t buf_len)
{
struct iiod_io *io = iiod_responder_get_default_io(client->responder);
struct iiod_command cmd = { .op = IIOD_OP_PRINT };
struct iiod_buf iiod_buf = { .ptr = buf, .size = buf_len };
return iiod_io_exec_command(io, &cmd, NULL, &iiod_buf);
}
static struct iio_context *
iiod_client_create_context_private_new(struct iiod_client *client,
const struct iio_backend *backend,
const char *description,
const char **ctx_attrs,
const char **ctx_values,
unsigned int nb_ctx_attrs)
{
size_t xml_len = 0x10000, uri_len = sizeof("xml:") - 1;
struct iio_context *ctx = NULL;
unsigned long long len;
char *xml_zstd, *xml;
bool is_zstd;
int ret;
xml = malloc(xml_len + uri_len);
if (!xml)
return iio_ptr(-ENOMEM);
memcpy(xml, "xml:", uri_len);
ret = iiod_client_send_print(client, &xml[uri_len], xml_len);
if (ret < 0) {
prm_perror(client->params, -ret,
"Unable to send PRINT command");
goto out_free_xml;
}
xml_len = ret;
is_zstd = strncmp(xml, "xml:<?xml", sizeof("xml:<?xml") - 1) != 0;
if (is_zstd)
prm_dbg(client->params, "Received ZSTD-compressed XML string.\n");
else
prm_dbg(client->params, "Received uncompressed XML string.\n");
if (is_zstd) {
len = ZSTD_getFrameContentSize(&xml[uri_len], xml_len);
if (len == ZSTD_CONTENTSIZE_UNKNOWN ||
len == ZSTD_CONTENTSIZE_ERROR) {
ret = -EIO;
goto out_free_xml;
}
xml_zstd = malloc(uri_len + len + 1);
if (!xml_zstd) {
ret = -ENOMEM;
goto out_free_xml;
}
xml_len = ZSTD_decompress(&xml_zstd[uri_len], len, &xml[uri_len], xml_len);
if (ZSTD_isError(xml_len)) {
prm_err(client->params, "Unable to decompress ZSTD data: %s\n",
ZSTD_getErrorName(xml_len));
ret = -EIO;
free(xml_zstd);
goto out_free_xml;
}
memcpy(xml_zstd, "xml:", uri_len);
xml_zstd[uri_len + xml_len] = '\0';
/* Free compressed data, make "xml" point to uncompressed data */
free(xml);
xml = xml_zstd;