libiio
iio-private.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * libiio - Library for interfacing industrial I/O (IIO) devices
4  *
5  * Copyright (C) 2014 Analog Devices, Inc.
6  * Author: Paul Cercueil <paul.cercueil@analog.com>
7  */
8 
9 #ifndef __IIO_PRIVATE_H__
10 #define __IIO_PRIVATE_H__
11 
12 #include "iio-config.h"
13 
14 /* Include public interface */
15 #include <iio/iio.h>
16 #include <iio/iio-backend.h>
17 #include <iio/iio-debug.h>
18 
19 #include <stdbool.h>
20 
21 #define MAX_CHN_ID NAME_MAX /* encoded in the sysfs filename */
22 #define MAX_CHN_NAME NAME_MAX /* encoded in the sysfs filename */
23 #define MAX_DEV_ID NAME_MAX /* encoded in the sysfs filename */
24 #define MAX_DEV_NAME NAME_MAX /* encoded in the sysfs filename */
25 #define MAX_CTX_NAME NAME_MAX /* nominally "xml" */
26 #define MAX_CTX_DESC NAME_MAX /* nominally "linux ..." */
27 #define MAX_ATTR_NAME NAME_MAX /* encoded in the sysfs filename */
28 #define MAX_ATTR_VALUE (8 * PAGESIZE) /* 8x Linux page size, could be anything */
29 
30 #ifdef _MSC_BUILD
31 /* Windows only runs on little-endian systems */
32 #define is_little_endian() true
33 #else
34 #define is_little_endian() (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
35 #endif
36 
37 #define BIT(x) (1 << (x))
38 #define BIT_MASK(bit) BIT((bit) % 32)
39 #define BIT_WORD(bit) ((bit) / 32)
40 
41 /* ntohl/htonl are a nightmare to use in cross-platform applications,
42  * since they are defined in different headers on different platforms.
43  * iio_be32toh/iio_htobe32 are just clones of ntohl/htonl. */
44 static inline uint32_t iio_be32toh(uint32_t word)
45 {
46  if (!is_little_endian())
47  return word;
48 
49 #ifdef __GNUC__
50  return __builtin_bswap32(word);
51 #else
52  return ((word & 0xff) << 24) | ((word & 0xff00) << 8) |
53  ((word >> 8) & 0xff00) | ((word >> 24) & 0xff);
54 #endif
55 }
56 
57 static inline uint32_t iio_htobe32(uint32_t word)
58 {
59  return iio_be32toh(word);
60 }
61 
62 /*
63  * If these structures are updated, the qsort functions defined in sort.c
64  * may need to be updated.
65  */
66 
67 struct iio_buffer_pdata;
68 struct iio_context_pdata;
69 struct iio_device_pdata;
70 struct iio_channel_pdata;
71 struct iio_directory;
72 struct iio_module;
73 struct iio_mutex;
74 struct iio_task;
75 
77  char *name;
78  char *filename;
79 };
80 
81 struct iio_context {
82  struct iio_context_pdata *pdata;
83  const struct iio_backend_ops *ops;
84  const char *name;
85  char *description;
86  void *userdata;
87 
88  unsigned int major;
89  unsigned int minor;
90  char *git_tag;
91 
92  struct iio_device **devices;
93  unsigned int nb_devices;
94 
95  char *xml;
96 
97  char **attrs;
98  char **values;
99  unsigned int nb_attrs;
100 
101  struct iio_context_params params;
102 
103  struct iio_module *lib;
104 };
105 
106 struct iio_channel {
107  struct iio_device *dev;
108  struct iio_channel_pdata *pdata;
109  void *userdata;
110 
111  bool is_output;
112  bool is_scan_element;
113  struct iio_data_format format;
114  char *name, *id;
115  long index;
116  enum iio_modifier modifier;
117  enum iio_chan_type type;
118 
119  struct iio_channel_attr *attrs;
120  unsigned int nb_attrs;
121 
122  unsigned int number;
123 };
124 
126  char **names;
127  unsigned int num;
128 };
129 
130 struct iio_device {
131  const struct iio_context *ctx;
132  struct iio_device_pdata *pdata;
133  void *userdata;
134 
135  char *name, *id, *label;
136 
137  struct iio_dev_attrs attrs;
138  struct iio_dev_attrs buffer_attrs;
139  struct iio_dev_attrs debug_attrs;
140 
141  struct iio_channel **channels;
142  unsigned int nb_channels;
143 };
144 
145 struct iio_buffer {
146  const struct iio_device *dev;
147  struct iio_buffer_pdata *pdata;
148 
149  void *userdata;
150  size_t length;
151 
152  struct iio_channels_mask *mask;
153  unsigned int idx;
154 
155  struct iio_task *worker;
156 
157  size_t block_size;
158 
159  /* Mutex to protect nb_blocks. Should really be an atomic... */
160  struct iio_mutex *lock;
161  unsigned int nb_blocks;
162 };
163 
164 struct iio_context_info {
165  char *description;
166  char *uri;
167 };
168 
170  size_t words;
171  uint32_t mask[];
172 };
173 
174 int iio_channels_mask_copy(struct iio_channels_mask *dst,
175  const struct iio_channels_mask *src);
176 
177 static inline bool
178 iio_channels_mask_test_bit(const struct iio_channels_mask *mask,
179  unsigned int bit)
180 {
181  return mask->mask[BIT_WORD(bit)] & BIT_MASK(bit);
182 }
183 
184 static inline void
185 iio_channels_mask_set_bit(struct iio_channels_mask *mask, unsigned int bit)
186 {
187  mask->mask[BIT_WORD(bit)] |= BIT_MASK(bit);
188 }
189 
190 static inline void
191 iio_channels_mask_clear_bit(struct iio_channels_mask *mask, unsigned int bit)
192 {
193  mask->mask[BIT_WORD(bit)] &= ~BIT_MASK(bit);
194 }
195 
196 struct iio_module * iio_open_module(const struct iio_context_params *params,
197  const char *name);
198 void iio_release_module(struct iio_module *module);
199 
200 const struct iio_backend * iio_module_get_backend(struct iio_module *module);
201 
202 void free_channel(struct iio_channel *chn);
203 void free_device(struct iio_device *dev);
204 
205 ssize_t iio_snprintf_channel_xml(char *str, ssize_t slen,
206  const struct iio_channel *chn);
207 ssize_t iio_snprintf_device_xml(char *str, ssize_t slen,
208  const struct iio_device *dev);
209 
210 int iio_context_init(struct iio_context *ctx);
211 
212 bool iio_device_is_tx(const struct iio_device *dev);
213 
214 int read_double(const char *str, double *val);
215 int write_double(char *buf, size_t len, double val);
216 
217 bool iio_list_has_elem(const char *list, const char *elem);
218 
219 struct iio_context *
220 iio_create_dynamic_context(const struct iio_context_params *params,
221  const char *uri);
222 int iio_dynamic_scan(const struct iio_context_params *params,
223  struct iio_scan *ctx, const char *backends);
224 
225 void iio_channel_init_finalize(struct iio_channel *chn);
226 unsigned int find_channel_modifier(const char *s, size_t *len_p);
227 
228 char *iio_strndup(const char *str, size_t n);
229 char *iio_strtok_r(char *str, const char *delim, char **saveptr);
230 char * iio_getenv (char * envvar);
231 uint64_t iio_read_counter_us(void);
232 
233 int iio_context_add_device(struct iio_context *ctx, struct iio_device *dev);
234 
235 int iio_context_add_attr(struct iio_context *ctx,
236  const char *key, const char *value);
237 
238 int add_iio_dev_attr(struct iio_device *dev, struct iio_dev_attrs *attrs,
239  const char *attr, const char *type);
240 
241 __cnst const struct iio_context_params *get_default_params(void);
242 
243 extern const struct iio_backend iio_ip_backend;
244 extern const struct iio_backend iio_local_backend;
245 extern const struct iio_backend iio_serial_backend;
246 extern const struct iio_backend iio_usb_backend;
247 extern const struct iio_backend iio_xml_backend;
248 
249 extern const struct iio_backend *iio_backends[];
250 extern const unsigned int iio_backends_size;
251 
252 ssize_t iio_xml_print_and_sanitized_param(char *ptr, ssize_t len,
253  const char *before, char *param,
254  const char *after);
255 
256 static inline void iio_update_xml_indexes(ssize_t ret, char **ptr, ssize_t *len,
257  ssize_t *alen)
258 {
259  if (*ptr) {
260  *ptr += ret;
261  *len -= ret;
262  }
263  *alen += ret;
264 }
265 
266 bool iio_channel_is_hwmon(const char *id);
267 
268 int iio_block_io(struct iio_block *block);
269 
270 #endif /* __IIO_PRIVATE_H__ */
Definition: task.c:26
Definition: iio-backend.h:127
Definition: dynamic.c:21
Definition: lock-windows.c:18
Definition: local.h:20
Structure holding scanning information.
Definition: scan.c:16
Represents an input or output channel of a device.
Definition: iio-private.h:106
Definition: iio-private.h:76
Represents a device in the IIO context.
Definition: iio-private.h:130
iio_modifier
IIO channel modifier.
Definition: iio.h:207
double value(Channel ch)
Reads the value of a channel by using "input" or "raw" attribute and applying "scale" and "offset" if...
Definition: iiopp.h:806
Definition: ops.h:110
Definition: network.c:56
iio_chan_type
IIO channel type.
Definition: iio.h:161
Public interface.
Definition: local-mmap.c:45
Definition: iio-private.h:125
An input or output buffer, used to read or write samples.
Definition: iio-private.h:145
Definition: iio-private.h:169
IIO context creation information.
Definition: iio.h:126
Definition: compat.c:298
Definition: local.c:53
A block of memory containing data samples.
Definition: block.c:16
Contains the format of a data sample.
Definition: iio.h:1430
Definition: iio-backend.h:66
Contains the representation of an IIO context.
Definition: iio-private.h:81