blob: ef8ffc4ec440639804ca6d844dde9e0692356485 [file] [log] [blame]
Paul Cercueilbb4401d2014-02-28 16:10:49 +01001/*
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
Paul Cercueil0b2ce712014-02-17 15:04:18 +010019#ifndef __IIO_PRIVATE_H__
20#define __IIO_PRIVATE_H__
21
Paul Cercueil00236242014-02-18 15:09:06 +010022/* Include public interface */
Paul Cercueil0b2ce712014-02-17 15:04:18 +010023#include "iio.h"
24
Paul Cercueil30f74d12017-01-24 10:58:37 +010025#include "iio-config.h"
26
Paul Cercueil0b2ce712014-02-17 15:04:18 +010027#include <stdbool.h>
28
Paul Cercueil1ab707c2016-02-09 17:12:25 +010029#ifdef _MSC_BUILD
Lars-Peter Clausend1be8382016-02-24 11:13:45 +010030#define inline __inline
Paul Cercueil9c9a5562017-01-24 10:48:31 +010031#define iio_snprintf sprintf_s
32#else
33#define iio_snprintf snprintf
34#endif
35
Paul Cercueil65f90e42016-02-25 16:00:10 +010036#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
Matt Fornero10c541a2016-12-15 15:37:56 -080048#ifdef WITH_MATLAB_BINDINGS_API
49#include "bindings/matlab/iio-wrapper.h"
50#endif
51
Paul Cercueil0967b1c2015-04-20 11:55:47 +020052#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)
Paul Cercueile1311222014-03-12 15:46:16 +010055#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
Paul Cercueil2042c902016-04-25 18:43:45 +020063
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. */
67static 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
81static inline uint32_t iio_htobe32(uint32_t word)
82{
83 return iio_be32toh(word);
84}
85
Lars-Peter Clausend1be8382016-02-24 11:13:45 +010086/* Allocate zeroed out memory */
87static inline void *zalloc(size_t size)
88{
89 return calloc(1, size);
90}
91
Matt Fornero81f04a52017-11-30 14:36:37 -050092enum iio_attr_type {
93 IIO_ATTR_TYPE_DEVICE = 0,
94 IIO_ATTR_TYPE_DEBUG,
95 IIO_ATTR_TYPE_BUFFER,
96};
97
Paul Cercueil0b2ce712014-02-17 15:04:18 +010098struct iio_backend_ops {
Paul Cercueil63d5e7c2014-10-28 14:33:08 +010099 struct iio_context * (*clone)(const struct iio_context *ctx);
Paul Cercueil45c575d2014-03-20 15:14:01 +0100100 ssize_t (*read)(const struct iio_device *dev, void *dst, size_t len,
101 uint32_t *mask, size_t words);
Paul Cercueilec1760d2014-02-21 11:31:20 +0100102 ssize_t (*write)(const struct iio_device *dev,
103 const void *src, size_t len);
Paul Cercueil92f15c22015-04-20 11:36:51 +0200104 int (*open)(const struct iio_device *dev,
105 size_t samples_count, bool cyclic);
Paul Cercueilec1760d2014-02-21 11:31:20 +0100106 int (*close)(const struct iio_device *dev);
Romain Roffé6a881702015-06-30 16:25:43 +0200107 int (*get_fd)(const struct iio_device *dev);
Romain Roffé0ea038d2015-06-30 13:35:38 +0200108 int (*set_blocking_mode)(const struct iio_device *dev, bool blocking);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100109
Lars-Peter Clausen48c01602016-04-20 13:10:05 +0200110 void (*cancel)(const struct iio_device *dev);
111
Romain Roffé6c385d92015-06-30 13:36:05 +0200112 int (*set_kernel_buffers_count)(const struct iio_device *dev,
113 unsigned int nb_blocks);
Paul Cercueil93f7e1f2014-04-23 16:49:56 +0200114 ssize_t (*get_buffer)(const struct iio_device *dev,
Paul Cercueil76ca8842015-03-05 11:16:16 +0100115 void **addr_ptr, size_t bytes_used,
116 uint32_t *mask, size_t words);
Paul Cercueil93f7e1f2014-04-23 16:49:56 +0200117
Paul Cercueil167d3112014-02-18 12:23:53 +0100118 ssize_t (*read_device_attr)(const struct iio_device *dev,
Matt Fornero81f04a52017-11-30 14:36:37 -0500119 const char *attr, char *dst, size_t len, enum iio_attr_type);
Paul Cercueil167d3112014-02-18 12:23:53 +0100120 ssize_t (*write_device_attr)(const struct iio_device *dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200121 const char *attr, const char *src,
Matt Fornero81f04a52017-11-30 14:36:37 -0500122 size_t len, enum iio_attr_type);
Paul Cercueil167d3112014-02-18 12:23:53 +0100123 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,
Paul Cercueilcecda352014-05-06 18:14:29 +0200126 const char *attr, const char *src, size_t len);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100127
Paul Cercueil24ffa532014-03-10 12:39:58 +0100128 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
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100133 void (*shutdown)(struct iio_context *ctx);
Paul Cercueile45f8762014-05-02 11:19:26 +0200134
Harvey Yangbc3f48e2019-10-22 11:17:28 +0800135 int (*request_client_id)(const struct iio_context *ctx);
136 int (*register_client_id)(const struct iio_device *dev);
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200137 int (*get_version)(const struct iio_context *ctx, unsigned int *major,
138 unsigned int *minor, char git_tag[8]);
Paul Cercueil4ca73542014-06-10 16:23:29 +0200139
140 int (*set_timeout)(struct iio_context *ctx, unsigned int timeout);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100141};
142
Robin Getz57295ce2018-05-24 21:45:08 +0000143/*
144 * If these structures are updated, the qsort functions defined in sort.c
145 * may need to be updated.
146 */
147
Paul Cercueil82b3c1c2014-02-24 13:15:56 +0100148struct iio_context_pdata;
Paul Cercueileaab6582014-02-21 09:35:59 +0100149struct iio_device_pdata;
150struct iio_channel_pdata;
Lars-Peter Clausen794ba032016-04-25 15:07:00 +0200151struct iio_scan_backend_context;
Paul Cercueil82b3c1c2014-02-24 13:15:56 +0100152
Paul Cercueilb34e0222014-05-05 15:32:38 +0200153struct iio_channel_attr {
154 char *name;
Paul Cercueil42d12352014-05-05 16:11:58 +0200155 char *filename;
Paul Cercueilb34e0222014-05-05 15:32:38 +0200156};
157
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100158struct iio_context {
Paul Cercueil82b3c1c2014-02-24 13:15:56 +0100159 struct iio_context_pdata *pdata;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100160 const struct iio_backend_ops *ops;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100161 const char *name;
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100162 char *description;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100163
164 struct iio_device **devices;
165 unsigned int nb_devices;
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200166
167 char *xml;
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100168
169 char **attrs;
170 char **values;
171 unsigned int nb_attrs;
Harvey Yangbc3f48e2019-10-22 11:17:28 +0800172 int client_id;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100173};
174
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100175struct iio_channel {
Paul Cercueileaab6582014-02-21 09:35:59 +0100176 struct iio_device *dev;
177 struct iio_channel_pdata *pdata;
Paul Cercueild96e61f2014-03-07 16:13:37 +0100178 void *userdata;
Paul Cercueileaab6582014-02-21 09:35:59 +0100179
Paul Cercueil35a01312014-02-20 10:56:57 +0100180 bool is_output;
Paul Cercueil85aaf482014-04-24 16:39:09 +0200181 bool is_scan_element;
Paul Cercueilcd6ce842014-03-14 13:21:06 +0100182 struct iio_data_format format;
Paul Cercueilbb618272014-02-20 11:35:52 +0100183 char *name, *id;
Paul Cercueilae88fde2014-03-12 11:47:10 +0100184 long index;
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200185 enum iio_modifier modifier;
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200186 enum iio_chan_type type;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100187
Paul Cercueilb34e0222014-05-05 15:32:38 +0200188 struct iio_channel_attr *attrs;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100189 unsigned int nb_attrs;
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200190
191 unsigned int number;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100192};
193
194struct iio_device {
195 const struct iio_context *ctx;
Paul Cercueileaab6582014-02-21 09:35:59 +0100196 struct iio_device_pdata *pdata;
Paul Cercueild96e61f2014-03-07 16:13:37 +0100197 void *userdata;
Paul Cercueileaab6582014-02-21 09:35:59 +0100198
Paul Cercueilbb618272014-02-20 11:35:52 +0100199 char *name, *id;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100200
Paul Cercueilbb618272014-02-20 11:35:52 +0100201 char **attrs;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100202 unsigned int nb_attrs;
203
Matt Fornero7f9598f2017-11-30 14:07:52 -0500204 char **buffer_attrs;
205 unsigned int nb_buffer_attrs;
206
Paul Cercueilddaa26a2014-04-14 17:32:18 +0200207 char **debug_attrs;
208 unsigned int nb_debug_attrs;
209
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100210 struct iio_channel **channels;
211 unsigned int nb_channels;
Paul Cercueilff778232014-03-24 14:23:08 +0100212
213 uint32_t *mask;
214 size_t words;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100215};
216
Paul Cercueila689cd92014-03-20 16:37:25 +0100217struct iio_buffer {
218 const struct iio_device *dev;
Paul Cercueila2d4bad2014-05-27 10:15:29 +0200219 void *buffer, *userdata;
Paul Cercueila689cd92014-03-20 16:37:25 +0100220 size_t length, data_length;
221
Paul Cercueil645ab972014-03-24 14:36:12 +0100222 uint32_t *mask;
Paul Cercueil9e5301d2014-11-19 15:34:28 +0100223 unsigned int dev_sample_size;
Paul Cercueila689cd92014-03-20 16:37:25 +0100224 unsigned int sample_size;
Paul Cercueil2a74bd72014-04-28 13:18:48 +0200225 bool is_output, dev_is_high_speed;
Paul Cercueila689cd92014-03-20 16:37:25 +0100226};
227
Lars-Peter Clausen08633732016-02-22 14:35:17 +0100228struct iio_context_info {
229 char *description;
230 char *uri;
231};
232
233struct iio_scan_result {
234 size_t size;
235 struct iio_context_info **info;
236};
237
238struct iio_context_info ** iio_scan_result_add(
239 struct iio_scan_result *scan_result, size_t num);
240
Paul Cercueil00236242014-02-18 15:09:06 +0100241void free_channel(struct iio_channel *chn);
242void free_device(struct iio_device *dev);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100243
Paul Cercueil42090d12014-02-24 12:32:23 +0100244char *iio_channel_get_xml(const struct iio_channel *chn, size_t *len);
245char *iio_device_get_xml(const struct iio_device *dev, size_t *len);
246
Paul Cercueilc1ed8482014-06-11 16:29:43 +0200247char *iio_context_create_xml(const struct iio_context *ctx);
Paul Cercueilfd387472015-08-05 10:34:19 +0200248int iio_context_init(struct iio_context *ctx);
Paul Cercueilae88fde2014-03-12 11:47:10 +0100249
Lars-Peter Clausen62bd5522015-03-27 16:06:52 +0100250bool iio_device_is_tx(const struct iio_device *dev);
Paul Cercueil6d3fb8d2014-05-27 10:04:00 +0200251int iio_device_open(const struct iio_device *dev,
252 size_t samples_count, bool cyclic);
253int iio_device_close(const struct iio_device *dev);
Romain Roffécead1da2015-06-30 13:35:51 +0200254int iio_device_set_blocking_mode(const struct iio_device *dev, bool blocking);
Paul Cercueil6d3fb8d2014-05-27 10:04:00 +0200255ssize_t iio_device_read_raw(const struct iio_device *dev,
256 void *dst, size_t len, uint32_t *mask, size_t words);
257ssize_t iio_device_write_raw(const struct iio_device *dev,
258 const void *src, size_t len);
Romain Roffé6a881702015-06-30 16:25:43 +0200259int iio_device_get_poll_fd(const struct iio_device *dev);
Paul Cercueil6d3fb8d2014-05-27 10:04:00 +0200260
Paul Cercueil542cbb42014-10-21 13:00:38 +0200261int read_double(const char *str, double *val);
Paul Cercueil225a3e12015-03-03 18:08:41 +0100262int write_double(char *buf, size_t len, double val);
Paul Cercueil542cbb42014-10-21 13:00:38 +0200263
Paul Cercueil63e52182014-12-11 12:52:48 +0100264struct iio_context * local_create_context(void);
265struct iio_context * network_create_context(const char *hostname);
266struct iio_context * xml_create_context_mem(const char *xml, size_t len);
267struct iio_context * xml_create_context(const char *xml_file);
Lars-Peter Clausen7a8e6b52016-02-22 13:25:12 +0100268struct iio_context * usb_create_context(unsigned int bus, unsigned int address,
269 unsigned int interface);
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100270struct iio_context * usb_create_context_from_uri(const char *uri);
Paul Cercueilbcb04522016-03-22 17:03:29 +0100271struct iio_context * serial_create_context_from_uri(const char *uri);
Paul Cercueil63e52182014-12-11 12:52:48 +0100272
Lars-Peter Clausen3db58d62016-04-25 15:00:54 +0200273int local_context_scan(struct iio_scan_result *scan_result);
274
Lars-Peter Clausen794ba032016-04-25 15:07:00 +0200275struct iio_scan_backend_context * usb_context_scan_init(void);
276void usb_context_scan_free(struct iio_scan_backend_context *ctx);
277
278int usb_context_scan(struct iio_scan_backend_context *ctx,
279 struct iio_scan_result *scan_result);
280
Paul Cercueile98500b2014-04-10 13:31:04 +0200281/* This function is not part of the API, but is used by the IIO daemon */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200282__api ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,
Paul Cercueil92f15c22015-04-20 11:36:51 +0200283 const uint32_t *mask, size_t words);
Paul Cercueil46825942014-03-18 14:28:49 +0100284
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200285void iio_channel_init_finalize(struct iio_channel *chn);
Lars-Peter Clausenca20c242018-01-24 11:04:10 +0100286unsigned int find_channel_modifier(const char *s, size_t *len_p);
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200287
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200288char *iio_strdup(const char *str);
289
Paul Cercueila09970b2016-11-24 17:30:07 +0100290int iio_context_add_attr(struct iio_context *ctx,
291 const char *key, const char *value);
292
Paul Cercueil65f90e42016-02-25 16:00:10 +0100293#undef __api
294
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100295#endif /* __IIO_PRIVATE_H__ */