libiio
ops.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 __OPS_H__
10 #define __OPS_H__
11 
12 #include "../iio-config.h"
13 #include "queue.h"
14 
15 #include <endian.h>
16 #include <errno.h>
17 #include <iio/iio.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <poll.h>
22 #include <sys/socket.h>
23 #include <unistd.h>
24 
25 #if WITH_AIO
26 #include <libaio.h>
27 #endif
28 
29 #define IIOD_PORT 30431
30 
31 #ifndef __bswap_constant_16
32 #define __bswap_constant_16(x) \
33  ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
34 #endif
35 
36 #ifndef __bswap_constant_32
37 #define __bswap_constant_32(x) \
38  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
39  (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
40 #endif
41 
42 #define BIT(x) (1 << (x))
43 #define BIT_MASK(bit) BIT((bit) % 32)
44 #define BIT_WORD(bit) ((bit) / 32)
45 #define TEST_BIT(addr, bit) (!!(*(((uint32_t *) addr) + BIT_WORD(bit)) \
46  & BIT_MASK(bit)))
47 
48 struct iio_task;
49 struct iiod_io;
50 struct thread_pool;
51 extern struct thread_pool *main_thread_pool;
52 struct DevEntry;
53 
54 enum iio_attr_type {
55  IIO_ATTR_TYPE_DEVICE,
56  IIO_ATTR_TYPE_DEBUG,
57  IIO_ATTR_TYPE_BUFFER,
58 };
59 
60 struct block_entry {
61  SLIST_ENTRY(block_entry) entry;
62  struct iio_block *block;
63  struct iiod_io *io;
64  uint64_t bytes_used;
65  uint16_t client_id;
66  bool cyclic;
67 };
68 
69 struct buffer_entry {
70  SLIST_ENTRY(buffer_entry) entry;
71  struct iio_device *dev;
72  struct iio_buffer *buf;
73  struct iio_task *enqueue_task, *dequeue_task;
74  uint32_t *words;
75  uint16_t idx;
76 };
77 
78 struct parser_pdata {
79  struct iio_context *ctx;
80  bool stop, binary, verbose;
81  int fd_in, fd_out;
82  int resp_fd[2];
83 
84  SLIST_HEAD(ParserDataThdHead, ThdEntry) thdlist_head;
85 
86  SLIST_HEAD(BufferList, buffer_entry) bufferlist;
87  SLIST_HEAD(BlockList, block_entry) blocklist;
88 
89  /* Used as temporaries placements by the lexer */
90  struct iio_device *dev;
91  struct iio_channel *chn;
92  bool channel_is_output;
93  bool fd_in_is_socket, fd_out_is_socket;
94  bool is_usb;
95 #if WITH_AIO
96  io_context_t aio_ctx[2];
97  int aio_eventfd[2];
98  pthread_mutex_t aio_mutex[2];
99 #endif
100  struct thread_pool *pool;
101  struct iiod_io *io;
102 
103  const void *xml_zstd;
104  size_t xml_zstd_len;
105 
106  ssize_t (*writefd)(struct parser_pdata *pdata, const void *buf, size_t len);
107  ssize_t (*readfd)(struct parser_pdata *pdata, void *buf, size_t len);
108 };
109 
111  struct DevEntry *entry;
112  unsigned int nb_blocks;
113 };
114 
115 extern bool server_demux; /* Defined in iiod.c */
116 
117 static inline void *zalloc(size_t size)
118 {
119  return calloc(1, size);
120 }
121 
122 void interpreter(struct iio_context *ctx, int fd_in, int fd_out, bool verbose,
123  bool is_socket, bool is_usb, bool use_aio, struct thread_pool *pool,
124  const void *xml_zstd, size_t xml_zstd_len);
125 
126 int init_usb_daemon(const char *ffs, unsigned int nb_pipes);
127 int start_usb_daemon(struct iio_context *ctx, const char *ffs,
128  bool debug, bool use_aio, unsigned int nb_pipes,
129  int ep0_fd, struct thread_pool *pool,
130  const void *xml_zstd, size_t xml_zstd_len);
131 int start_serial_daemon(struct iio_context *ctx, const char *uart_params,
132  bool debug, struct thread_pool *pool,
133  const void *xml_zstd, size_t xml_zstd_len);
134 
135 int binary_parse(struct parser_pdata *pdata);
136 
137 void enable_binary(struct parser_pdata *pdata);
138 
139 int open_dev(struct parser_pdata *pdata, struct iio_device *dev,
140  size_t samples_count, const char *mask, bool cyclic);
141 int close_dev(struct parser_pdata *pdata, struct iio_device *dev);
142 
143 ssize_t rw_dev(struct parser_pdata *pdata, struct iio_device *dev,
144  unsigned int nb, bool is_write);
145 
146 ssize_t read_dev_attr(struct parser_pdata *pdata, struct iio_device *dev,
147  const char *attr, enum iio_attr_type type);
148 ssize_t write_dev_attr(struct parser_pdata *pdata, struct iio_device *dev,
149  const char *attr, size_t len, enum iio_attr_type type);
150 
151 ssize_t read_chn_attr(struct parser_pdata *pdata, struct iio_channel *chn,
152  const char *attr);
153 ssize_t write_chn_attr(struct parser_pdata *pdata, struct iio_channel *chn,
154  const char *attr, size_t len);
155 
156 ssize_t get_trigger(struct parser_pdata *pdata, struct iio_device *dev);
157 ssize_t set_trigger(struct parser_pdata *pdata,
158  struct iio_device *dev, const char *trig);
159 
160 int set_timeout(struct parser_pdata *pdata, unsigned int timeout);
161 int set_buffers_count(struct parser_pdata *pdata,
162  struct iio_device *dev, long value);
163 
164 ssize_t read_line(struct parser_pdata *pdata, char *buf, size_t len);
165 ssize_t read_all(struct parser_pdata *pdata, void *dst, size_t len);
166 ssize_t write_all(struct parser_pdata *pdata, const void *src, size_t len);
167 
168 static __inline__ void output(struct parser_pdata *pdata, const char *text)
169 {
170  if (write_all(pdata, text, strlen(text)) <= 0)
171  pdata->stop = true;
172 }
173 
174 static __inline__ int poll_nointr(struct pollfd *pfd, unsigned int num_pfd)
175 {
176  int ret;
177 
178  do {
179  ret = poll(pfd, num_pfd, -1);
180  } while (ret == -1 && errno == EINTR);
181 
182  return ret;
183 }
184 
185 #endif /* __OPS_H__ */
Definition: task.c:26
Definition: ops.h:60
Represents an input or output channel of a device.
Definition: iio-private.h:106
Represents a device in the IIO context.
Definition: iio-private.h:130
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.c:30
Definition: ops.h:110
Public interface.
Definition: thread-pool.c:29
Definition: ops.h:78
Definition: iiod-responder.c:39
Definition: local-mmap.c:45
An input or output buffer, used to read or write samples.
Definition: iio-private.h:145
Definition: ops.h:69
A block of memory containing data samples.
Definition: block.c:16
Definition: ops.c:91
Contains the representation of an IIO context.
Definition: iio-private.h:81