My Project
Loading...
Searching...
No Matches
iio-backend.h
1/* SPDX-License-Identifier: MIT */
2/*
3 * libiio - Library for interfacing industrial I/O (IIO) devices
4 *
5 * Copyright (C) 2020 Analog Devices, Inc.
6 */
7
8#ifndef __IIO_BACKEND_H__
9#define __IIO_BACKEND_H__
10
11#include <iio/iio.h>
12#include <stdbool.h>
13
14#define __api __iio_api
15
16#define __api_export_if(x) ___api_export_if(x)
17#define ___api_export_if(x) ___api_export_if_##x
18#define ___api_export_if_0
19#define ___api_export_if_1 __iio_api_export
20
21/* https://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
22 * {NAME_MAX} : Maximum number of bytes in a filename
23 * {PATH_MAX} : Maximum number of bytes in a pathname
24 * {PAGESIZE} : Size in bytes of a page
25 * Too bad we work on non-POSIX systems
26 */
27#ifndef NAME_MAX
28#define NAME_MAX 256
29#endif
30#ifndef PATH_MAX
31#define PATH_MAX 4096
32#endif
33#ifndef PAGESIZE
34#define PAGESIZE 4096
35#endif
36
37#ifdef _MSC_BUILD
38#define inline __inline
39#define iio_sscanf sscanf_s
40#else
41#define iio_sscanf sscanf
42#endif
43
44#define IIO_ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
45
46/*
47 * Prototype of a user-supplied function that retrieves the number of ticks
48 * (as microseconds) from a clock source. The value is used as a 64-bit
49 * microsecond count; a 32-bit one would wrap after about 72 minutes.
50 */
51typedef uint64_t (*iio_get_ticks_us)(void);
52
53struct iio_buffer;
54struct iio_device;
55struct iio_context;
56struct iio_channel;
57struct iio_block_pdata;
58struct iio_buffer_pdata;
59struct iio_context_pdata;
60struct iio_device_pdata;
61struct iio_scan_element;
62struct iio_channel_pdata;
63struct iio_event_stream_pdata;
64
65enum iio_backend_api_ver {
66 IIO_BACKEND_API_V1 = 1,
67};
68
69enum iio_attr_type {
70 IIO_ATTR_TYPE_DEVICE = 0,
71 IIO_ATTR_TYPE_DEBUG,
72 IIO_ATTR_TYPE_DEVICE_EVENT,
73 IIO_ATTR_TYPE_BUFFER,
74 IIO_ATTR_TYPE_CHANNEL,
75 IIO_ATTR_TYPE_CHANNEL_EVENT,
76 IIO_ATTR_TYPE_CONTEXT,
77};
78
79#define CHN_ATTRLIST_IDX(type) (type - IIO_ATTR_TYPE_CHANNEL)
80
82 const struct iio_context *ctx;
83 const struct iio_device *dev;
84 const struct iio_channel *chn;
85 const struct iio_buffer *buf;
86};
87
88struct iio_attr {
89 union iio_pointer iio;
90 enum iio_attr_type type;
91 const char *name;
92 const char *filename;
93};
94
96 int (*scan)(const struct iio_context_params *params, struct iio_scan *ctx,
97 const char *args);
98 struct iio_context *(*create)(const struct iio_context_params *params, const char *uri);
99
100 ssize_t (*read_attr)(const struct iio_attr *attr, char *dst, size_t len);
101 ssize_t (*write_attr)(const struct iio_attr *attr, const char *src, size_t len);
102
103 const struct iio_device *(*get_trigger)(const struct iio_device *dev);
104 int (*set_trigger)(const struct iio_device *dev, const struct iio_device *trigger);
105
106 void (*shutdown)(struct iio_context *ctx);
107
108 int (*get_version)(const struct iio_context *ctx, unsigned int *major, unsigned int *minor,
109 unsigned int *patch, char git_tag[8]);
110
111 int (*set_timeout)(struct iio_context *ctx, int timeout);
112
113 struct iio_buffer_pdata *(*open_buffer)(const struct iio_device *dev, unsigned int idx,
114 struct iio_channels_mask *mask);
115 void (*close_buffer)(struct iio_buffer_pdata *pdata);
116 int (*enable_buffer)(struct iio_buffer_pdata *pdata, size_t nb_samples, bool enable,
117 bool cyclic);
118 void (*cancel_buffer)(struct iio_buffer_pdata *pdata);
119
120 ssize_t (*readbuf)(struct iio_buffer_pdata *pdata, void *dst, size_t len);
121 ssize_t (*writebuf)(struct iio_buffer_pdata *pdata, const void *src, size_t len);
122
123 struct iio_block_pdata *(*create_block)(
124 struct iio_buffer_pdata *pdata, size_t size, void **data);
125 void (*free_block)(struct iio_block_pdata *pdata);
126
127 int (*enqueue_block)(struct iio_block_pdata *pdata, size_t bytes_used, bool cyclic);
128 int (*dequeue_block)(struct iio_block_pdata *pdata, bool nonblock);
129
130 int (*get_dmabuf_fd)(struct iio_block_pdata *pdata);
131 int (*disable_cpu_access)(struct iio_block_pdata *pdata, bool disable);
132
133 struct iio_event_stream_pdata *(*open_ev)(const struct iio_device *dev);
134 void (*close_ev)(struct iio_event_stream_pdata *pdata);
135 int (*read_ev)(struct iio_event_stream_pdata *pdata, struct iio_event *out_event,
136 bool nonblock);
137
138 /* Atomic register operations */
139 int (*reg_read)(const struct iio_device *dev, uint32_t address, uint32_t *value);
140 int (*reg_write)(const struct iio_device *dev, uint32_t address, uint32_t value);
141
142 /* Channel format refresh */
143 int (*refresh_format)(const struct iio_channel *chn);
144
145 /* Connection liveness test */
146 int (*ping)(struct iio_context *ctx);
147};
148
157 unsigned int api_version;
158 const char *name;
159 const char *uri_prefix;
160 const struct iio_backend_ops *ops;
161 unsigned int default_timeout_ms;
162};
163
164/*
165 * If Libiio was compiled with external backend support (WITH_EXTERNAL_BACKEND),
166 * applications should implement their own backend and provide the
167 * iio_external_backend symbol.
168 */
169extern const struct iio_backend iio_external_backend;
170
171__api struct iio_context *iio_context_create_from_backend(const struct iio_context_params *params,
172 const struct iio_backend *backend, const char *description, unsigned int major,
173 unsigned int minor, unsigned int patch, const char *git_tag);
174
175__api struct iio_device *iio_context_add_device(
176 struct iio_context *ctx, const char *id, const char *name, const char *label);
177
178__api struct iio_channel *iio_device_add_channel(struct iio_device *dev, long index, const char *id,
179 const char *name, const char *label, bool output, bool scan_element,
180 const struct iio_data_format *fmt);
181
182__api struct iio_buffer *iio_device_add_buffer(struct iio_device *dev, unsigned int idx);
183
184__api int iio_context_add_attr(struct iio_context *ctx, const char *key, const char *value);
185__api int iio_device_add_attr(struct iio_device *dev, const char *name, enum iio_attr_type type);
186
187__api int iio_buffer_add_attr(struct iio_buffer *buf, const char *name);
188
189__api void iio_buffer_set_direction(struct iio_buffer *buf, const char *direction);
190
191__api int iio_buffer_add_scan_element(
192 struct iio_buffer *buf, const struct iio_channel *chn, const char *en_path);
193
194__api int iio_parse_format_string(const char *fmt_str, struct iio_data_format *fmt);
195
196__api int iio_channel_add_attr(struct iio_channel *chn, const char *name, enum iio_attr_type type,
197 const char *filename);
198
199__api struct iio_context_pdata *iio_context_get_pdata(const struct iio_context *ctx);
200
201__api void iio_context_set_pdata(struct iio_context *ctx, struct iio_context_pdata *data);
202
203__api struct iio_device_pdata *iio_device_get_pdata(const struct iio_device *dev);
204
205__api void iio_device_set_pdata(struct iio_device *dev, struct iio_device_pdata *data);
206
207__api struct iio_channel_pdata *iio_channel_get_pdata(const struct iio_channel *chn);
208
209__api void iio_channel_set_pdata(struct iio_channel *chn, struct iio_channel_pdata *data);
210
211__api int iio_scan_add_result(struct iio_scan *ctx, const char *desc, const char *uri);
212
213#if defined(__MINGW32__)
214#define __iio_printf __attribute__((__format__(gnu_printf, 3, 4)))
215#elif defined(__GNUC__)
216#define __iio_printf __attribute__((__format__(printf, 3, 4)))
217#else
218#define __iio_printf
219#endif
220
221__api __iio_printf ssize_t iio_snprintf(char *buf, size_t len, const char *fmt, ...);
222__api char *iio_strdup(const char *str);
223__api size_t iio_strlcpy(char *__restrict dst, const char *__restrict src, size_t dsize);
224__api FILE *iio_fopen(const char *path, const char *mode);
225
226__api struct iio_context *iio_create_context_from_xml(const struct iio_context_params *params,
227 const char *uri, const struct iio_backend *backend, const char *description,
228 const char **ctx_attr, const char **ctx_values, unsigned int nb_ctx_attrs);
229
235__api void iio_register_get_ticks_us_cb(iio_get_ticks_us cb);
236
237/* Allocate zeroed out memory */
238static inline void *zalloc(size_t size)
239{
240 return calloc(1, size);
241}
242
243static inline const struct iio_device *iio_attr_get_device(const struct iio_attr *attr)
244{
245 switch (attr->type) {
246 case IIO_ATTR_TYPE_CONTEXT:
247 return NULL;
248 case IIO_ATTR_TYPE_CHANNEL:
249 case IIO_ATTR_TYPE_CHANNEL_EVENT:
250 return iio_channel_get_device(attr->iio.chn);
251 case IIO_ATTR_TYPE_BUFFER:
252 return iio_buffer_get_device(attr->iio.buf);
253 default:
254 return attr->iio.dev;
255 }
256}
257
258#undef __api
259
260#endif /* __IIO_BACKEND_H__ */
const struct iio_device * iio_buffer_get_device(const struct iio_buffer *buf)
Retrieve a pointer to the iio_device structure.
const struct iio_device * iio_channel_get_device(const struct iio_channel *chn)
Retrieve a pointer to the iio_device structure.
Public interface.
Structure holding meta-data for an attribute.
Definition iio-backend.h:88
Definition iio-backend.h:95
Definition iio-backend.h:156
An input or output buffer, used to read or write samples.
Represents an input or output channel of a device.
IIO context creation information.
Definition iio.h:173
Contains the representation of an IIO context.
Contains the format of a data sample.
Definition iio.h:1516
Represents a device in the IIO context.
Represents a IIO event.
Definition iio.h:1437
Structure holding scanning information.
Definition iio-backend.h:81