forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.c
2465 lines (1964 loc) · 61.4 KB
/
compat.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
*
* Libiio 0.x to 1.x compat library
*
* Copyright (C) 2022 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "dynamic.h"
#include "iio-config.h"
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define snprintf sprintf_s
#endif
#if (defined(_WIN32) || defined(__MBED__))
#ifndef _SSIZE_T_DEFINED
typedef ptrdiff_t ssize_t;
#define _SSIZE_T_DEFINED
#endif
#else
#include <sys/types.h>
#endif
struct iio_attr;
struct iio_block;
struct iio_buffer;
struct iio_channel;
struct iio_channels_mask;
struct iio_context;
struct iio_context_params;
struct iio_data_format;
struct iio_device;
struct iio_scan;
struct compat {
void *lib;
/* Scan */
struct iio_scan * (*iio_scan)(const struct iio_context_params *, const char *);
void (*iio_scan_destroy)(struct iio_scan *);
size_t (*iio_scan_get_results_count)(const struct iio_scan *);
const char * (*iio_scan_get_description)(const struct iio_scan *, size_t);
const char * (*iio_scan_get_uri)(const struct iio_scan *, size_t );
/* Backends */
bool (*iio_has_backend)(const struct iio_context_params *, const char *);
unsigned int (*iio_get_builtin_backends_count)(void);
const char * (*iio_get_builtin_backend)(unsigned int);
/* Context */
struct iio_context * (*iio_create_context)(const struct iio_context_params *,
const char *);
void (*iio_context_destroy)(struct iio_context *);
void (*iio_context_set_data)(struct iio_context *, void *);
void * (*iio_context_get_data)(const struct iio_context *);
const char * (*iio_context_get_version_tag)(const struct iio_context *);
unsigned int (*iio_context_get_version_major)(const struct iio_context *);
unsigned int (*iio_context_get_version_minor)(const struct iio_context *);
const char * (*iio_context_get_name)(const struct iio_context *);
const char * (*iio_context_get_description)(const struct iio_context *);
char * (*iio_context_get_xml)(const struct iio_context *);
const struct iio_context_params *
(*iio_context_get_params)(const struct iio_context *);
unsigned int (*iio_context_get_devices_count)(const struct iio_context *);
struct iio_device * (*iio_context_get_device)(const struct iio_context *,
unsigned int);
struct iio_device * (*iio_context_find_device)(const struct iio_context *,
const char *);
int (*iio_context_set_timeout)(struct iio_context *, unsigned int);
/* Context attributes */
unsigned int (*iio_context_get_attrs_count)(const struct iio_context *);
const struct iio_attr * (*iio_context_get_attr)(const struct iio_context *,
unsigned int);
const struct iio_attr * (*iio_context_find_attr)(const struct iio_context *,
const char *);
/* Devices */
const struct iio_context * (*iio_device_get_context)(const struct iio_device *);
const char * (*iio_device_get_id)(const struct iio_device *);
const char * (*iio_device_get_name)(const struct iio_device *);
const char * (*iio_device_get_label)(const struct iio_device *);
void * (*iio_device_get_data)(const struct iio_device *);
void (*iio_device_set_data)(struct iio_device *, void *);
int (*iio_device_reg_read)(struct iio_device *, uint32_t, uint32_t *);
int (*iio_device_reg_write)(struct iio_device *, uint32_t, uint32_t);
unsigned int (*iio_device_get_channels_count)(const struct iio_device *);
struct iio_channel * (*iio_device_get_channel)(const struct iio_device *,
unsigned int);
struct iio_channel * (*iio_device_find_channel)(const struct iio_device *,
const char *, bool);
unsigned int (*iio_device_get_attrs_count)(const struct iio_device *);
const struct iio_attr * (*iio_device_get_attr)(const struct iio_device *,
unsigned int);
const struct iio_attr * (*iio_device_find_attr)(const struct iio_device *,
const char *);
unsigned int (*iio_device_get_debug_attrs_count)(const struct iio_device *);
const struct iio_attr * (*iio_device_get_debug_attr)(const struct iio_device *,
unsigned int);
const struct iio_attr * (*iio_device_find_debug_attr)(const struct iio_device *,
const char *);
const struct iio_device * (*iio_device_get_trigger)(const struct iio_device *);
int (*iio_device_set_trigger)(const struct iio_device *,
const struct iio_device *);
bool (*iio_device_is_trigger)(const struct iio_device *);
ssize_t (*iio_device_get_sample_size)(const struct iio_device *,
const struct iio_channels_mask *);
/* Channels */
const struct iio_device * (*iio_channel_get_device)(const struct iio_channel *);
const char * (*iio_channel_get_id)(const struct iio_channel *);
const char * (*iio_channel_get_name)(const struct iio_channel *);
bool (*iio_channel_is_output)(const struct iio_channel *);
bool (*iio_channel_is_scan_element)(const struct iio_channel *);
void (*iio_channel_set_data)(struct iio_channel *, void *);
void * (*iio_channel_get_data)(const struct iio_channel *);
unsigned int (*iio_channel_get_attrs_count)(const struct iio_channel *);
const struct iio_attr * (*iio_channel_get_attr)(const struct iio_channel *,
unsigned int);
const struct iio_attr * (*iio_channel_find_attr)(const struct iio_channel *,
const char *);
unsigned int (*iio_channel_get_type)(const struct iio_channel *);
unsigned int (*iio_channel_get_modifier)(const struct iio_channel *);
long (*iio_channel_get_index)(const struct iio_channel *);
const struct iio_data_format *
(*iio_channel_get_data_format)(const struct iio_channel *);
void (*iio_channel_convert)(const struct iio_channel *,
void *, const void *);
void (*iio_channel_convert_inverse)(const struct iio_channel *,
void *, const void *);
void (*iio_channel_enable)(const struct iio_channel *,
struct iio_channels_mask *);
void (*iio_channel_disable)(const struct iio_channel *,
struct iio_channels_mask *);
bool (*iio_channel_is_enabled)(const struct iio_channel *,
const struct iio_channels_mask *);
/* Channel masks */
struct iio_channels_mask * (*iio_create_channels_mask)(unsigned int);
void (*iio_channels_mask_destroy)(struct iio_channels_mask *);
/* Buffers */
struct iio_buffer * (*iio_device_create_buffer)(const struct iio_device *,
unsigned int,
const struct iio_channels_mask *);
void (*iio_buffer_destroy)(struct iio_buffer *);
void (*iio_buffer_cancel)(struct iio_buffer *);
int (*iio_buffer_enable)(struct iio_buffer *);
int (*iio_buffer_disable)(struct iio_buffer *);
unsigned int (*iio_buffer_get_attrs_count)(const struct iio_buffer *);
const struct iio_attr * (*iio_buffer_get_attr)(const struct iio_buffer *,
unsigned int);
const struct iio_attr * (*iio_buffer_find_attr)(const struct iio_buffer *,
const char *);
const struct iio_device * (*iio_buffer_get_device)(const struct iio_buffer *);
const struct iio_channels_mask *
(*iio_buffer_get_channels_mask)(const struct iio_buffer *);
void (*iio_buffer_set_data)(struct iio_buffer *, void *);
void * (*iio_buffer_get_data)(const struct iio_buffer *);
/* Blocks */
struct iio_block * (*iio_buffer_create_block)(struct iio_buffer *, size_t);
void (*iio_block_destroy)(struct iio_block *);
struct iio_buffer * (*iio_block_get_buffer)(const struct iio_block *);
int (*iio_block_enqueue)(struct iio_block *, size_t, bool);
int (*iio_block_dequeue)(struct iio_block *, bool);
void * (*iio_block_start)(const struct iio_block *);
void * (*iio_block_first)(const struct iio_block *,
const struct iio_channel *);
void * (*iio_block_end)(const struct iio_block *);
ssize_t (*iio_block_foreach_sample)(const struct iio_block *,
const struct iio_channels_mask *,
ssize_t (*callback)(const struct iio_channel *,
void *, size_t, void *),
void *);
/* Attributes */
const char * (*iio_attr_get_name)(const struct iio_attr *);
const char * (*iio_attr_get_filename)(const struct iio_attr *);
const char * (*iio_attr_get_static_value)(const struct iio_attr *);
ssize_t (*iio_attr_read_raw)(const struct iio_attr *, char *, size_t);
int (*iio_attr_read_bool)(const struct iio_attr *, bool *);
int (*iio_attr_read_longlong)(const struct iio_attr *, long long *);
int (*iio_attr_read_double)(const struct iio_attr *, double *);
ssize_t (*iio_attr_write_raw)(const struct iio_attr *, const void *, size_t);
int (*iio_attr_write_string)(const struct iio_attr *, const char *);
int (*iio_attr_write_bool)(const struct iio_attr *, bool);
int (*iio_attr_write_longlong)(const struct iio_attr *, long long);
int (*iio_attr_write_double)(const struct iio_attr *, double);
/* Misc */
size_t (*iio_strlcpy)(char * __restrict, const char * __restrict, size_t);
char *(*iio_strdup)(const char *);
void (*iio_strerror)(int, char *, size_t);
void (*iio_prm_printf)(const struct iio_context_params *params,
unsigned int msg_level,
const char *fmt, ...);
};
static struct compat *compat_lib_ptr;
#define IIO_CALL(fn) (*compat_lib_ptr->fn)
struct iio_context_info {
char *description;
char *uri;
};
struct iio_scan_block {
struct iio_scan *ctx;
struct iio_context_info **info;
ssize_t ctx_cnt;
};
struct iio_channel_compat {
void *userdata;
};
struct iio_device_compat {
void *userdata;
struct iio_channels_mask *mask;
unsigned int nb_kernel_buffers;
bool is_tx;
unsigned int nb_channels;
struct iio_buffer *buf;
};
struct iio_context_compat {
void *userdata;
unsigned int nb_devices;
char *xml;
};
struct iio_buffer_compat {
void *userdata;
size_t size, samples_count, sample_size;
bool cyclic, nonblock, enabled, enqueued;
unsigned int nb_blocks, curr;
bool all_enqueued;
struct iio_block * blocks[];
};
static inline int iio_err(const void *ptr)
{
return (uintptr_t) ptr >= (uintptr_t) -4095 ? (int)(intptr_t) ptr : 0;
}
static bool iio_device_is_tx(const struct iio_device *dev)
{
struct iio_channel *chn;
unsigned int i, nb_channels;
nb_channels = IIO_CALL(iio_device_get_channels_count)(dev);
for (i = 0; i < nb_channels; i++) {
chn = IIO_CALL(iio_device_get_channel)(dev, i);
if (IIO_CALL(iio_channel_is_output)(chn)
&& IIO_CALL(iio_channel_is_scan_element)(chn)) {
return true;
}
}
return false;
}
static int iio_init_context_compat(struct iio_context *ctx)
{
struct iio_context_compat *compat;
struct iio_device_compat *dev_compat;
struct iio_channel_compat *chn_compat;
struct iio_device *dev;
struct iio_channel *chn;
size_t size = sizeof(*compat);
unsigned int i, j, nb_devices, nb_channels;
unsigned int chn_counter = 0;
int err;
nb_devices = IIO_CALL(iio_context_get_devices_count)(ctx);
for (i = 0; i < nb_devices; i++) {
dev = IIO_CALL(iio_context_get_device)(ctx, i);
nb_channels = IIO_CALL(iio_device_get_channels_count)(dev);
size += sizeof(*dev_compat) + nb_channels * sizeof(*chn_compat);
}
compat = calloc(1, size);
if (!compat)
return -ENOMEM;
compat->xml = IIO_CALL(iio_context_get_xml)(ctx);
err = iio_err(compat->xml);
if (err)
goto err_free_compat;
compat->nb_devices = nb_devices;
for (i = 0; i < nb_devices; i++) {
dev = IIO_CALL(iio_context_get_device)(ctx, i);
nb_channels = IIO_CALL(iio_device_get_channels_count)(dev);
dev_compat = (void *)((uintptr_t)compat
+ sizeof(*compat)
+ i * sizeof(*dev_compat));
dev_compat->nb_channels = nb_channels;
dev_compat->is_tx = iio_device_is_tx(dev);
dev_compat->nb_kernel_buffers = 4;
IIO_CALL(iio_device_set_data)(dev, dev_compat);
if (nb_channels == 0)
continue;
dev_compat->mask = IIO_CALL(iio_create_channels_mask)(nb_channels);
if (!dev_compat->mask) {
err = -ENOMEM;
goto err_destroy_masks;
}
for (j = 0; j < nb_channels; j++) {
chn = IIO_CALL(iio_device_get_channel)(dev, j);
chn_compat = (void *)((uintptr_t)compat
+ sizeof(*compat)
+ nb_devices * sizeof(*dev_compat)
+ chn_counter++ * sizeof(*chn_compat));
IIO_CALL(iio_channel_set_data)(chn, chn_compat);
}
}
IIO_CALL(iio_context_set_data)(ctx, compat);
return 0;
err_destroy_masks:
for (; i > 0; i--) {
dev = IIO_CALL(iio_context_get_device)(ctx, i - 1);
dev_compat = IIO_CALL(iio_device_get_data)(dev);
if (dev_compat->mask)
IIO_CALL(iio_channels_mask_destroy)(dev_compat->mask);
}
free(compat->xml);
err_free_compat:
free(compat);
return err;
}
struct iio_context * iio_create_context_from_uri(const char *uri)
{
struct iio_context *ctx;
int err;
ctx = IIO_CALL(iio_create_context)(NULL, uri);
err = iio_err(ctx);
if (err)
goto err_set_errno;
err = iio_init_context_compat(ctx);
if (err)
goto err_destroy_context;
return ctx;
err_destroy_context:
IIO_CALL(iio_context_destroy)(ctx);
err_set_errno:
errno = -err;
return NULL;
}
struct iio_context * iio_context_clone(const struct iio_context *old_ctx)
{
const struct iio_context_params *params;
const struct iio_attr *attr;
struct iio_context *ctx;
const char *uri;
int err = -ENOENT;
attr = IIO_CALL(iio_context_find_attr)(old_ctx, "uri");
if (!attr)
goto err_set_errno;
uri = IIO_CALL(iio_attr_get_static_value)(attr);
params = IIO_CALL(iio_context_get_params)(old_ctx);
if (strncmp(uri, "local:", sizeof("local:") - 1)
&& strncmp(uri, "ip:", sizeof("ip:") - 1)) {
/* The .clone callback was only implemented by the local
* and network backends in Libiio <= v0.25. */
err = -ENOSYS;
goto err_set_errno;
}
ctx = IIO_CALL(iio_create_context)(params, uri);
err = iio_err(ctx);
if (err)
goto err_set_errno;
err = iio_init_context_compat(ctx);
if (err)
goto err_destroy_context;
return ctx;
err_destroy_context:
IIO_CALL(iio_context_destroy)(ctx);
err_set_errno:
errno = -err;
return NULL;
}
void iio_context_destroy(struct iio_context *ctx)
{
struct iio_context_compat *compat;
compat = IIO_CALL(iio_context_get_data)(ctx);
IIO_CALL(iio_context_destroy)(ctx);
free(compat->xml);
free(compat);
}
static struct iio_context *
create_context_with_arg(const char *prefix, const char *arg)
{
char buf[256];
snprintf(buf, sizeof(buf), "%s%s", prefix, arg);
return iio_create_context_from_uri(buf);
}
struct iio_context * iio_create_xml_context(const char *xml_file)
{
return create_context_with_arg("xml:", xml_file);
}
struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
{
size_t uri_len = sizeof("xml:") - 1;
struct iio_context *ctx;
char *uri;
uri = malloc(len + uri_len + 1);
if (!uri) {
errno = ENOMEM;
return NULL;
}
memcpy(uri, "xml:", uri_len);
memcpy(uri + uri_len, xml, len);
uri[uri_len + len] = '\0';
ctx = iio_create_context_from_uri(uri);
free(uri);
return ctx;
}
struct iio_context * iio_create_network_context(const char *hostname)
{
return create_context_with_arg("ip:", hostname);
}
struct iio_context * iio_create_local_context(void)
{
return iio_create_context_from_uri("local:");
}
struct iio_context * iio_create_default_context(void)
{
return iio_create_context_from_uri(NULL);
}
void iio_context_info_list_free(struct iio_context_info **list)
{
struct iio_context_info **it, *info;
if (!list)
return;
for (it = list; *it; it++) {
info = *it;
free(info->description);
free(info->uri);
}
free(list);
}
struct iio_scan *
iio_create_scan_context(const char *backends, unsigned int flags)
{
struct iio_scan *ctx;
char buf[256];
char *ptr = NULL;
int err;
if (backends) {
IIO_CALL(iio_strlcpy)(buf, backends, sizeof(buf));
/* iio_scan() requires a comma-separated list of backends */
for (ptr = buf; *ptr; ptr++) {
if (*ptr == ':')
*ptr = ',';
}
ptr = strstr(buf, "usb=");
if (ptr) {
/* If we have the 'usb' string with parameters, the
* separator between VID and PID should be a colon. */
ptr = strchr(ptr, ',');
if (ptr)
*ptr = ':';
}
ptr = buf;
}
ctx = IIO_CALL(iio_scan)(NULL, ptr);
err = iio_err(ctx);
if (err) {
errno = -err;
return NULL;
}
return ctx;
}
void iio_scan_context_destroy(struct iio_scan *ctx)
{
IIO_CALL(iio_scan_destroy)(ctx);
}
ssize_t iio_scan_context_get_info_list(struct iio_scan *ctx,
struct iio_context_info ***info)
{
struct iio_context_info *results, **results_ptr;
size_t nb = IIO_CALL(iio_scan_get_results_count)(ctx);
unsigned int i;
int ret = -ENOMEM;
const char *ptr;
char *dup;
results_ptr = malloc((nb + 1) * sizeof(*results_ptr));
if (!results_ptr)
return -ENOMEM;
results_ptr[nb] = NULL;
results = malloc(nb * sizeof(*results));
if (!results)
goto out_free_results_ptr;
for (i = 0; i < nb; i++) {
ptr = IIO_CALL(iio_scan_get_description)(ctx, i);
dup = IIO_CALL(iio_strdup)(ptr);
if (!dup)
goto out_free_results_list;
results[i].description = dup;
ptr = IIO_CALL(iio_scan_get_uri)(ctx, i);
dup = IIO_CALL(iio_strdup)(ptr);
if (!dup)
goto out_free_results_list;
results[i].uri = dup;
results_ptr[i] = &results[i];
}
*info = results_ptr;
return nb;
out_free_results_list:
iio_context_info_list_free(results_ptr);
free(results);
out_free_results_ptr:
free(results_ptr);
return ret;
}
const char *
iio_context_info_get_description(const struct iio_context_info *info)
{
return info->description;
}
const char * iio_context_info_get_uri(const struct iio_context_info *info)
{
return info->uri;
}
ssize_t iio_scan_block_scan(struct iio_scan_block *blk)
{
iio_context_info_list_free(blk->info);
blk->info = NULL;
blk->ctx_cnt = iio_scan_context_get_info_list(blk->ctx, &blk->info);
return blk->ctx_cnt;
}
struct iio_context_info *
iio_scan_block_get_info(struct iio_scan_block *blk, unsigned int index)
{
if (!blk->info || (ssize_t)index >= blk->ctx_cnt) {
errno = EINVAL;
return NULL;
}
return blk->info[index];
}
struct iio_scan_block *
iio_create_scan_block(const char *backend, unsigned int flags)
{
struct iio_scan_block *blk;
blk = calloc(1, sizeof(*blk));
if (!blk) {
errno = ENOMEM;
return NULL;
}
blk->ctx = iio_create_scan_context(backend, flags);
if (!blk->ctx) {
free(blk);
return NULL;
}
return blk;
}
void iio_scan_block_destroy(struct iio_scan_block *blk)
{
iio_context_info_list_free(blk->info);
iio_scan_context_destroy(blk->ctx);
free(blk);
}
int iio_context_get_version(const struct iio_context *ctx,
unsigned int *major, unsigned int *minor,
char git_tag[8])
{
const char *tag = IIO_CALL(iio_context_get_version_tag)(ctx);
if (git_tag)
IIO_CALL(iio_strlcpy)(git_tag, tag, 8);
if (major)
*major = IIO_CALL(iio_context_get_version_major)(ctx);
if (minor)
*minor = IIO_CALL(iio_context_get_version_minor)(ctx);
return 0;
}
const char * iio_context_get_name(const struct iio_context *ctx)
{
return IIO_CALL(iio_context_get_name)(ctx);
}
const char * iio_context_get_description(const struct iio_context *ctx)
{
return IIO_CALL(iio_context_get_description)(ctx);
}
const char * iio_context_get_xml(const struct iio_context *ctx)
{
struct iio_context_compat *compat = IIO_CALL(iio_context_get_data)(ctx);
return compat->xml;
}
unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
{
return IIO_CALL(iio_context_get_devices_count)(ctx);
}
struct iio_device * iio_context_get_device(const struct iio_context *ctx,
unsigned int index)
{
return IIO_CALL(iio_context_get_device)(ctx, index);
}
struct iio_device *
iio_context_find_device(const struct iio_context *ctx, const char *name)
{
return IIO_CALL(iio_context_find_device)(ctx, name);
}
int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout_ms)
{
return IIO_CALL(iio_context_set_timeout)(ctx, timeout_ms);
}
unsigned int iio_context_get_attrs_count(const struct iio_context *ctx)
{
return IIO_CALL(iio_context_get_attrs_count)(ctx);
}
int iio_context_get_attr(const struct iio_context *ctx, unsigned int index,
const char **name, const char **value)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_context_get_attr)(ctx, index);
if (!attr)
return -ENOENT;
if (name)
*name = IIO_CALL(iio_attr_get_name)(attr);
if (value)
*value = IIO_CALL(iio_attr_get_static_value)(attr);
return 0;
}
const char * iio_context_get_attr_value(const struct iio_context *ctx,
const char *name)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_context_find_attr)(ctx, name);
if (!attr)
return NULL;
return IIO_CALL(iio_attr_get_static_value)(attr);
}
const struct iio_context * iio_device_get_context(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_context)(dev);
}
const char * iio_device_get_id(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_id)(dev);
}
const char * iio_device_get_name(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_name)(dev);
}
const char * iio_device_get_label(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_label)(dev);
}
void * iio_device_get_data(const struct iio_device *dev)
{
struct iio_device_compat *compat = IIO_CALL(iio_device_get_data)(dev);
return compat->userdata;
}
void iio_device_set_data(struct iio_device *dev, void *data)
{
struct iio_device_compat *compat = IIO_CALL(iio_device_get_data)(dev);
compat->userdata = data;
}
int iio_device_identify_filename(const struct iio_device *dev,
const char *filename, struct iio_channel **chn,
const char **attr_out)
{
unsigned int i, j, nb_channels, nb_attrs;
const struct iio_attr *attr;
struct iio_channel *ch;
const char *fn, *name;
nb_channels = IIO_CALL(iio_device_get_channels_count)(dev);
for (i = 0; i < nb_channels; i++) {
ch = IIO_CALL(iio_device_get_channel)(dev, i);
nb_attrs = IIO_CALL(iio_channel_get_attrs_count)(ch);
for (j = 0; j < nb_attrs; j++) {
attr = IIO_CALL(iio_channel_get_attr)(ch, j);
fn = IIO_CALL(iio_attr_get_filename)(attr);
if (!strcmp(fn, filename)) {
*attr_out = IIO_CALL(iio_attr_get_name)(attr);
*chn = ch;
return 0;
}
}
}
nb_attrs = IIO_CALL(iio_device_get_attrs_count)(dev);
for (i = 0; i < nb_attrs; i++) {
attr = IIO_CALL(iio_device_get_attr)(dev, i);
name = IIO_CALL(iio_attr_get_name)(attr);
/* Devices attributes are named after their filename */
if (!strcmp(name, filename)) {
*attr_out = name;
*chn = NULL;
return 0;
}
}
nb_attrs = IIO_CALL(iio_device_get_debug_attrs_count)(dev);
for (i = 0; i < nb_attrs; i++) {
attr = IIO_CALL(iio_device_get_debug_attr)(dev, i);
name = IIO_CALL(iio_attr_get_name)(attr);
if (!strcmp(name, filename)) {
*attr_out = name;
*chn = NULL;
return 0;
}
}
return -EINVAL;
}
int iio_device_reg_read(struct iio_device *dev,
uint32_t address, uint32_t *value)
{
return IIO_CALL(iio_device_reg_read)(dev, address, value);
}
int iio_device_reg_write(struct iio_device *dev,
uint32_t address, uint32_t value)
{
return IIO_CALL(iio_device_reg_write)(dev, address, value);
}
unsigned int iio_device_get_channels_count(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_channels_count)(dev);
}
unsigned int iio_device_get_attrs_count(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_attrs_count)(dev);
}
const char * iio_device_get_attr(const struct iio_device *dev,
unsigned int index)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_device_get_attr)(dev, index);
if (!attr)
return NULL;
return IIO_CALL(iio_attr_get_name)(attr);
}
const char * iio_device_find_attr(const struct iio_device *dev,
const char *name)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_device_find_attr)(dev, name);
if (!attr)
return NULL;
return IIO_CALL(iio_attr_get_name)(attr);
}
unsigned int iio_device_get_buffer_attrs_count(const struct iio_device *dev)
{
struct iio_device_compat *dev_compat = IIO_CALL(iio_device_get_data)(dev);
if (dev_compat->buf)
return IIO_CALL(iio_buffer_get_attrs_count)(dev_compat->buf);
/* We don't have access to an iio_buffer structure... */
return 0;
}
const char * iio_device_get_buffer_attr(const struct iio_device *dev,
unsigned int index)
{
struct iio_device_compat *dev_compat = IIO_CALL(iio_device_get_data)(dev);
const struct iio_attr *attr;
if (dev_compat->buf) {
attr = IIO_CALL(iio_buffer_get_attr)(dev_compat->buf, index);
return attr ? IIO_CALL(iio_attr_get_name)(attr) : NULL;
}
/* We don't have access to an iio_buffer structure... */
return NULL;
}
const char * iio_device_find_buffer_attr(const struct iio_device *dev,
const char *name)
{
struct iio_device_compat *dev_compat = IIO_CALL(iio_device_get_data)(dev);
const struct iio_attr *attr;
if (dev_compat->buf) {
attr = IIO_CALL(iio_buffer_find_attr)(dev_compat->buf, name);
return attr ? IIO_CALL(iio_attr_get_name)(attr) : NULL;
}
/* We don't have access to an iio_buffer structure... */
return NULL;
}
unsigned int iio_device_get_debug_attrs_count(const struct iio_device *dev)
{
return IIO_CALL(iio_device_get_debug_attrs_count)(dev);
}
const char * iio_device_get_debug_attr(const struct iio_device *dev,
unsigned int index)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_device_get_debug_attr)(dev, index);
if (!attr)
return NULL;
return IIO_CALL(iio_attr_get_name)(attr);
}
const char * iio_device_find_debug_attr(const struct iio_device *dev,
const char *name)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_device_find_debug_attr)(dev, name);
if (!attr)
return NULL;
return IIO_CALL(iio_attr_get_name)(attr);
}
struct iio_channel * iio_device_get_channel(const struct iio_device *dev,
unsigned int index)
{
return IIO_CALL(iio_device_get_channel)(dev, index);
}
struct iio_channel * iio_device_find_channel(const struct iio_device *dev,
const char *name, bool output)
{
return IIO_CALL(iio_device_find_channel)(dev, name, output);
}
ssize_t iio_device_attr_read(const struct iio_device *dev,
const char *name, char *dst, size_t len)
{
const struct iio_attr *attr;
attr = IIO_CALL(iio_device_find_attr)(dev, name);
if (attr)
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);
return -ENOENT;
}
int iio_device_attr_read_bool(const struct iio_device *dev,