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