• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #else
33 #define iio_snprintf snprintf
34 #endif
35 
36 #ifdef _WIN32
37 #   ifdef LIBIIO_EXPORTS
38 #	define __api __declspec(dllexport)
39 #   else
40 #	define __api __declspec(dllimport)
41 #   endif
42 #elif __GNUC__ >= 4
43 #   define __api __attribute__((visibility ("default")))
44 #else
45 #   define __api
46 #endif
47 
48 #ifdef WITH_MATLAB_BINDINGS_API
49 #include "bindings/matlab/iio-wrapper.h"
50 #endif
51 
52 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
53 #define BIT(x) (1 << (x))
54 #define BIT_MASK(bit) BIT((bit) % 32)
55 #define BIT_WORD(bit) ((bit) / 32)
56 #define TEST_BIT(addr, bit) (!!(*(((uint32_t *) addr) + BIT_WORD(bit)) \
57 		& BIT_MASK(bit)))
58 #define SET_BIT(addr, bit) \
59 	*(((uint32_t *) addr) + BIT_WORD(bit)) |= BIT_MASK(bit)
60 #define CLEAR_BIT(addr, bit) \
61 	*(((uint32_t *) addr) + BIT_WORD(bit)) &= ~BIT_MASK(bit)
62 
63 
64 /* ntohl/htonl are a nightmare to use in cross-platform applications,
65  * since they are defined in different headers on different platforms.
66  * iio_be32toh/iio_htobe32 are just clones of ntohl/htonl. */
iio_be32toh(uint32_t word)67 static inline uint32_t iio_be32toh(uint32_t word)
68 {
69 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
70 #ifdef __GNUC__
71 	return __builtin_bswap32(word);
72 #else
73 	return ((word & 0xff) << 24) | ((word & 0xff00) << 8) |
74 		((word >> 8) & 0xff00) | ((word >> 24) & 0xff);
75 #endif
76 #else
77 	return word;
78 #endif
79 }
80 
iio_htobe32(uint32_t word)81 static inline uint32_t iio_htobe32(uint32_t word)
82 {
83 	return iio_be32toh(word);
84 }
85 
86 /* Allocate zeroed out memory */
zalloc(size_t size)87 static inline void *zalloc(size_t size)
88 {
89 	return calloc(1, size);
90 }
91 
92 enum iio_attr_type {
93 	IIO_ATTR_TYPE_DEVICE = 0,
94 	IIO_ATTR_TYPE_DEBUG,
95 	IIO_ATTR_TYPE_BUFFER,
96 };
97 
98 struct iio_backend_ops {
99 	struct iio_context * (*clone)(const struct iio_context *ctx);
100 	ssize_t (*read)(const struct iio_device *dev, void *dst, size_t len,
101 			uint32_t *mask, size_t words);
102 	ssize_t (*write)(const struct iio_device *dev,
103 			const void *src, size_t len);
104 	int (*open)(const struct iio_device *dev,
105 			size_t samples_count, bool cyclic);
106 	int (*close)(const struct iio_device *dev);
107 	int (*get_fd)(const struct iio_device *dev);
108 	int (*set_blocking_mode)(const struct iio_device *dev, bool blocking);
109 
110 	void (*cancel)(const struct iio_device *dev);
111 
112 	int (*set_kernel_buffers_count)(const struct iio_device *dev,
113 			unsigned int nb_blocks);
114 	ssize_t (*get_buffer)(const struct iio_device *dev,
115 			void **addr_ptr, size_t bytes_used,
116 			uint32_t *mask, size_t words);
117 
118 	ssize_t (*read_device_attr)(const struct iio_device *dev,
119 			const char *attr, char *dst, size_t len, enum iio_attr_type);
120 	ssize_t (*write_device_attr)(const struct iio_device *dev,
121 			const char *attr, const char *src,
122 			size_t len, enum iio_attr_type);
123 	ssize_t (*read_channel_attr)(const struct iio_channel *chn,
124 			const char *attr, char *dst, size_t len);
125 	ssize_t (*write_channel_attr)(const struct iio_channel *chn,
126 			const char *attr, const char *src, size_t len);
127 
128 	int (*get_trigger)(const struct iio_device *dev,
129 			const struct iio_device **trigger);
130 	int (*set_trigger)(const struct iio_device *dev,
131 			const struct iio_device *trigger);
132 
133 	void (*shutdown)(struct iio_context *ctx);
134 
135 	int (*get_version)(const struct iio_context *ctx, unsigned int *major,
136 			unsigned int *minor, char git_tag[8]);
137 
138 	int (*set_timeout)(struct iio_context *ctx, unsigned int timeout);
139 };
140 
141 /*
142  * If these structures are updated, the qsort functions defined in sort.c
143  * may need to be updated.
144  */
145 
146 struct iio_context_pdata;
147 struct iio_device_pdata;
148 struct iio_channel_pdata;
149 struct iio_scan_backend_context;
150 
151 struct iio_channel_attr {
152 	char *name;
153 	char *filename;
154 };
155 
156 struct iio_context {
157 	struct iio_context_pdata *pdata;
158 	const struct iio_backend_ops *ops;
159 	const char *name;
160 	char *description;
161 
162 	struct iio_device **devices;
163 	unsigned int nb_devices;
164 
165 	char *xml;
166 
167 	char **attrs;
168 	char **values;
169 	unsigned int nb_attrs;
170 };
171 
172 struct iio_channel {
173 	struct iio_device *dev;
174 	struct iio_channel_pdata *pdata;
175 	void *userdata;
176 
177 	bool is_output;
178 	bool is_scan_element;
179 	struct iio_data_format format;
180 	char *name, *id;
181 	long index;
182 	enum iio_modifier modifier;
183 	enum iio_chan_type type;
184 
185 	struct iio_channel_attr *attrs;
186 	unsigned int nb_attrs;
187 
188 	unsigned int number;
189 };
190 
191 struct iio_device {
192 	const struct iio_context *ctx;
193 	struct iio_device_pdata *pdata;
194 	void *userdata;
195 
196 	char *name, *id;
197 
198 	char **attrs;
199 	unsigned int nb_attrs;
200 
201 	char **buffer_attrs;
202 	unsigned int nb_buffer_attrs;
203 
204 	char **debug_attrs;
205 	unsigned int nb_debug_attrs;
206 
207 	struct iio_channel **channels;
208 	unsigned int nb_channels;
209 
210 	uint32_t *mask;
211 	size_t words;
212 };
213 
214 struct iio_buffer {
215 	const struct iio_device *dev;
216 	void *buffer, *userdata;
217 	size_t length, data_length;
218 
219 	uint32_t *mask;
220 	unsigned int dev_sample_size;
221 	unsigned int sample_size;
222 	bool is_output, dev_is_high_speed;
223 };
224 
225 struct iio_context_info {
226 	char *description;
227 	char *uri;
228 };
229 
230 struct iio_scan_result {
231 	size_t size;
232 	struct iio_context_info **info;
233 };
234 
235 struct iio_context_info ** iio_scan_result_add(
236 	struct iio_scan_result *scan_result, size_t num);
237 
238 void free_channel(struct iio_channel *chn);
239 void free_device(struct iio_device *dev);
240 
241 char *iio_channel_get_xml(const struct iio_channel *chn, size_t *len);
242 char *iio_device_get_xml(const struct iio_device *dev, size_t *len);
243 
244 char *iio_context_create_xml(const struct iio_context *ctx);
245 int iio_context_init(struct iio_context *ctx);
246 
247 bool iio_device_is_tx(const struct iio_device *dev);
248 int iio_device_open(const struct iio_device *dev,
249 		size_t samples_count, bool cyclic);
250 int iio_device_close(const struct iio_device *dev);
251 int iio_device_set_blocking_mode(const struct iio_device *dev, bool blocking);
252 ssize_t iio_device_read_raw(const struct iio_device *dev,
253 		void *dst, size_t len, uint32_t *mask, size_t words);
254 ssize_t iio_device_write_raw(const struct iio_device *dev,
255 		const void *src, size_t len);
256 int iio_device_get_poll_fd(const struct iio_device *dev);
257 
258 int read_double(const char *str, double *val);
259 int write_double(char *buf, size_t len, double val);
260 
261 struct iio_context * local_create_context(void);
262 struct iio_context * network_create_context(const char *hostname);
263 struct iio_context * xml_create_context_mem(const char *xml, size_t len);
264 struct iio_context * xml_create_context(const char *xml_file);
265 struct iio_context * usb_create_context(unsigned int bus, unsigned int address,
266 		unsigned int interface);
267 struct iio_context * usb_create_context_from_uri(const char *uri);
268 struct iio_context * serial_create_context_from_uri(const char *uri);
269 
270 int local_context_scan(struct iio_scan_result *scan_result);
271 
272 struct iio_scan_backend_context * usb_context_scan_init(void);
273 void usb_context_scan_free(struct iio_scan_backend_context *ctx);
274 
275 int usb_context_scan(struct iio_scan_backend_context *ctx,
276 		struct iio_scan_result *scan_result);
277 
278 /* This function is not part of the API, but is used by the IIO daemon */
279 __api ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,
280 		const uint32_t *mask, size_t words);
281 
282 void iio_channel_init_finalize(struct iio_channel *chn);
283 unsigned int find_channel_modifier(const char *s, size_t *len_p);
284 
285 char *iio_strdup(const char *str);
286 
287 int iio_context_add_attr(struct iio_context *ctx,
288 		const char *key, const char *value);
289 
290 #undef __api
291 
292 #endif /* __IIO_PRIVATE_H__ */
293