1 /*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2014 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * */
18
19 #ifndef __IIO_PRIVATE_H__
20 #define __IIO_PRIVATE_H__
21
22 /* Include public interface */
23 #include "iio.h"
24
25 #include "iio-config.h"
26
27 #include <stdbool.h>
28
29 #ifdef _MSC_BUILD
30 #define inline __inline
31 #define iio_snprintf sprintf_s
32 #define iio_sscanf sscanf_s
33 #else
34 #define iio_snprintf snprintf
35 #define iio_sscanf sscanf
36 #endif
37
38 #ifdef _WIN32
39 # ifdef LIBIIO_EXPORTS
40 # define __api __declspec(dllexport)
41 # else
42 # define __api __declspec(dllimport)
43 # endif
44 #elif __GNUC__ >= 4
45 # define __api __attribute__((visibility ("default")))
46 #else
47 # define __api
48 #endif
49
50 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
51 #define BIT(x) (1 << (x))
52 #define BIT_MASK(bit) BIT((bit) % 32)
53 #define BIT_WORD(bit) ((bit) / 32)
54 #define TEST_BIT(addr, bit) (!!(*(((uint32_t *) addr) + BIT_WORD(bit)) \
55 & BIT_MASK(bit)))
56 #define SET_BIT(addr, bit) \
57 *(((uint32_t *) addr) + BIT_WORD(bit)) |= BIT_MASK(bit)
58 #define CLEAR_BIT(addr, bit) \
59 *(((uint32_t *) addr) + BIT_WORD(bit)) &= ~BIT_MASK(bit)
60
61 /* https://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
62 * {NAME_MAX} : Maximum number of bytes in a filename
63 * {PATH_MAX} : Maximum number of bytes in a pathname
64 * {PAGESIZE} : Size in bytes of a page
65 * Too bad we work on non-POSIX systems
66 */
67 #ifndef NAME_MAX
68 #define NAME_MAX 256
69 #endif
70 #ifndef PATH_MAX
71 #define PATH_MAX 4096
72 #endif
73 #ifndef PAGESIZE
74 #define PAGESIZE 4096
75 #endif
76
77 #define MAX_CHN_ID NAME_MAX /* encoded in the sysfs filename */
78 #define MAX_CHN_NAME NAME_MAX /* encoded in the sysfs filename */
79 #define MAX_DEV_ID NAME_MAX /* encoded in the sysfs filename */
80 #define MAX_DEV_NAME NAME_MAX /* encoded in the sysfs filename */
81 #define MAX_CTX_NAME NAME_MAX /* nominally "xml" */
82 #define MAX_CTX_DESC NAME_MAX /* nominally "linux ..." */
83 #define MAX_ATTR_NAME NAME_MAX /* encoded in the sysfs filename */
84 #define MAX_ATTR_VALUE PAGESIZE /* Linux page size, could be anything */
85
86 /* ntohl/htonl are a nightmare to use in cross-platform applications,
87 * since they are defined in different headers on different platforms.
88 * iio_be32toh/iio_htobe32 are just clones of ntohl/htonl. */
iio_be32toh(uint32_t word)89 static inline uint32_t iio_be32toh(uint32_t word)
90 {
91 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
92 #ifdef __GNUC__
93 return __builtin_bswap32(word);
94 #else
95 return ((word & 0xff) << 24) | ((word & 0xff00) << 8) |
96 ((word >> 8) & 0xff00) | ((word >> 24) & 0xff);
97 #endif
98 #else
99 return word;
100 #endif
101 }
102
iio_htobe32(uint32_t word)103 static inline uint32_t iio_htobe32(uint32_t word)
104 {
105 return iio_be32toh(word);
106 }
107
108 /* Allocate zeroed out memory */
zalloc(size_t size)109 static inline void *zalloc(size_t size)
110 {
111 return calloc(1, size);
112 }
113
114 enum iio_attr_type {
115 IIO_ATTR_TYPE_DEVICE = 0,
116 IIO_ATTR_TYPE_DEBUG,
117 IIO_ATTR_TYPE_BUFFER,
118 };
119
120 struct iio_backend_ops {
121 struct iio_context * (*clone)(const struct iio_context *ctx);
122 ssize_t (*read)(const struct iio_device *dev, void *dst, size_t len,
123 uint32_t *mask, size_t words);
124 ssize_t (*write)(const struct iio_device *dev,
125 const void *src, size_t len);
126 int (*open)(const struct iio_device *dev,
127 size_t samples_count, bool cyclic);
128 int (*close)(const struct iio_device *dev);
129 int (*get_fd)(const struct iio_device *dev);
130 int (*set_blocking_mode)(const struct iio_device *dev, bool blocking);
131
132 void (*cancel)(const struct iio_device *dev);
133
134 int (*set_kernel_buffers_count)(const struct iio_device *dev,
135 unsigned int nb_blocks);
136 ssize_t (*get_buffer)(const struct iio_device *dev,
137 void **addr_ptr, size_t bytes_used,
138 uint32_t *mask, size_t words);
139
140 ssize_t (*read_device_attr)(const struct iio_device *dev,
141 const char *attr, char *dst, size_t len, enum iio_attr_type);
142 ssize_t (*write_device_attr)(const struct iio_device *dev,
143 const char *attr, const char *src,
144 size_t len, enum iio_attr_type);
145 ssize_t (*read_channel_attr)(const struct iio_channel *chn,
146 const char *attr, char *dst, size_t len);
147 ssize_t (*write_channel_attr)(const struct iio_channel *chn,
148 const char *attr, const char *src, size_t len);
149
150 int (*get_trigger)(const struct iio_device *dev,
151 const struct iio_device **trigger);
152 int (*set_trigger)(const struct iio_device *dev,
153 const struct iio_device *trigger);
154
155 void (*shutdown)(struct iio_context *ctx);
156
157 int (*get_version)(const struct iio_context *ctx, unsigned int *major,
158 unsigned int *minor, char git_tag[8]);
159
160 int (*set_timeout)(struct iio_context *ctx, unsigned int timeout);
161 };
162
163 /*
164 * If these structures are updated, the qsort functions defined in sort.c
165 * may need to be updated.
166 */
167
168 struct iio_context_pdata;
169 struct iio_device_pdata;
170 struct iio_channel_pdata;
171 struct iio_scan_backend_context;
172
173 struct iio_channel_attr {
174 char *name;
175 char *filename;
176 };
177
178 struct iio_context {
179 struct iio_context_pdata *pdata;
180 const struct iio_backend_ops *ops;
181 const char *name;
182 char *description;
183
184 struct iio_device **devices;
185 unsigned int nb_devices;
186
187 char *xml;
188
189 char **attrs;
190 char **values;
191 unsigned int nb_attrs;
192 };
193
194 struct iio_channel {
195 struct iio_device *dev;
196 struct iio_channel_pdata *pdata;
197 void *userdata;
198
199 bool is_output;
200 bool is_scan_element;
201 struct iio_data_format format;
202 char *name, *id;
203 long index;
204 enum iio_modifier modifier;
205 enum iio_chan_type type;
206
207 struct iio_channel_attr *attrs;
208 unsigned int nb_attrs;
209
210 unsigned int number;
211 };
212
213 struct iio_device {
214 const struct iio_context *ctx;
215 struct iio_device_pdata *pdata;
216 void *userdata;
217
218 char *name, *id;
219
220 char **attrs;
221 unsigned int nb_attrs;
222
223 char **buffer_attrs;
224 unsigned int nb_buffer_attrs;
225
226 char **debug_attrs;
227 unsigned int nb_debug_attrs;
228
229 struct iio_channel **channels;
230 unsigned int nb_channels;
231
232 uint32_t *mask;
233 size_t words;
234 };
235
236 struct iio_buffer {
237 const struct iio_device *dev;
238 void *buffer, *userdata;
239 size_t length, data_length;
240
241 uint32_t *mask;
242 unsigned int dev_sample_size;
243 unsigned int sample_size;
244 bool is_output, dev_is_high_speed;
245 };
246
247 struct iio_context_info {
248 char *description;
249 char *uri;
250 };
251
252 struct iio_scan_result {
253 size_t size;
254 struct iio_context_info **info;
255 };
256
257 struct iio_context_info ** iio_scan_result_add(
258 struct iio_scan_result *scan_result, size_t num);
259
260 void free_channel(struct iio_channel *chn);
261 void free_device(struct iio_device *dev);
262
263 char *iio_channel_get_xml(const struct iio_channel *chn, size_t *len);
264 char *iio_device_get_xml(const struct iio_device *dev, size_t *len);
265
266 char *encode_xml_ndup(const char * input);
267 char *iio_context_create_xml(const struct iio_context *ctx);
268 int iio_context_init(struct iio_context *ctx);
269
270 bool iio_device_is_tx(const struct iio_device *dev);
271 int iio_device_open(const struct iio_device *dev,
272 size_t samples_count, bool cyclic);
273 int iio_device_close(const struct iio_device *dev);
274 int iio_device_set_blocking_mode(const struct iio_device *dev, bool blocking);
275 ssize_t iio_device_read_raw(const struct iio_device *dev,
276 void *dst, size_t len, uint32_t *mask, size_t words);
277 ssize_t iio_device_write_raw(const struct iio_device *dev,
278 const void *src, size_t len);
279 int iio_device_get_poll_fd(const struct iio_device *dev);
280
281 int read_double(const char *str, double *val);
282 int write_double(char *buf, size_t len, double val);
283
284 struct iio_context * local_create_context(void);
285 struct iio_context * network_create_context(const char *hostname);
286 struct iio_context * xml_create_context_mem(const char *xml, size_t len);
287 struct iio_context * xml_create_context(const char *xml_file);
288 struct iio_context * usb_create_context(unsigned int bus, uint16_t address,
289 uint16_t intrfc);
290 struct iio_context * usb_create_context_from_uri(const char *uri);
291 struct iio_context * serial_create_context_from_uri(const char *uri);
292
293 int local_context_scan(struct iio_scan_result *scan_result);
294
295 struct iio_scan_backend_context * usb_context_scan_init(void);
296 void usb_context_scan_free(struct iio_scan_backend_context *ctx);
297
298 int usb_context_scan(struct iio_scan_backend_context *ctx,
299 struct iio_scan_result *scan_result);
300
301 struct iio_scan_backend_context * dnssd_context_scan_init(void);
302 void dnssd_context_scan_free(struct iio_scan_backend_context *ctx);
303
304 int dnssd_context_scan(struct iio_scan_backend_context *ctx,
305 struct iio_scan_result *scan_result);
306
307 /* This function is not part of the API, but is used by the IIO daemon */
308 __api ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,
309 const uint32_t *mask, size_t words);
310
311 void iio_channel_init_finalize(struct iio_channel *chn);
312 unsigned int find_channel_modifier(const char *s, size_t *len_p);
313
314 char *iio_strdup(const char *str);
315 size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);
316 char * iio_getenv (char * envvar);
317
318 int iio_context_add_attr(struct iio_context *ctx,
319 const char *key, const char *value);
320
321 #undef __api
322
323 #endif /* __IIO_PRIVATE_H__ */
324