libiio  1.0
Library for interfacing with IIO devices
iiod-responder.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) 2021 Analog Devices, Inc.
6  * Author: Paul Cercueil <paul.cercueil@analog.com>
7  */
8 
9 #ifndef __IIOD_RESPONDER_H__
10 #define __IIOD_RESPONDER_H__
11 
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #if (defined(_WIN32) || defined(__MBED__))
16 #ifndef _SSIZE_T_DEFINED
17 typedef ptrdiff_t ssize_t;
18 #define _SSIZE_T_DEFINED
19 #endif
20 #else
21 #include <sys/types.h>
22 #endif
23 
24 struct iiod_command_data;
25 struct iiod_responder;
26 struct iiod_io;
27 
28 enum iiod_opcode {
29  IIOD_OP_RESPONSE,
30  IIOD_OP_PRINT,
31  IIOD_OP_TIMEOUT,
32  IIOD_OP_READ_ATTR,
33  IIOD_OP_READ_DBG_ATTR,
34  IIOD_OP_READ_BUF_ATTR,
35  IIOD_OP_READ_CHN_ATTR,
36  IIOD_OP_WRITE_ATTR,
37  IIOD_OP_WRITE_DBG_ATTR,
38  IIOD_OP_WRITE_BUF_ATTR,
39  IIOD_OP_WRITE_CHN_ATTR,
40  IIOD_OP_GETTRIG,
41  IIOD_OP_SETTRIG,
42 
43  IIOD_OP_CREATE_BUFFER,
44  IIOD_OP_FREE_BUFFER,
45  IIOD_OP_ENABLE_BUFFER,
46  IIOD_OP_DISABLE_BUFFER,
47 
48  IIOD_OP_CREATE_BLOCK,
49  IIOD_OP_FREE_BLOCK,
50  IIOD_OP_TRANSFER_BLOCK,
51  IIOD_OP_ENQUEUE_BLOCK_CYCLIC,
52  IIOD_OP_RETRY_DEQUEUE_BLOCK,
53 
54  IIOD_OP_CREATE_EVSTREAM,
55  IIOD_OP_FREE_EVSTREAM,
56  IIOD_OP_READ_EVENT,
57 
58  IIOD_NB_OPCODES,
59 };
60 
61 struct iiod_command {
62  uint16_t client_id;
63  uint8_t op;
64  uint8_t dev;
65  int32_t code;
66 };
67 
68 struct iiod_buf {
69  void *ptr;
70  size_t size;
71 };
72 
73 struct iiod_responder_ops {
74  int (*cmd)(const struct iiod_command *cmd,
75  struct iiod_command_data *data, void *d);
76  ssize_t (*read)(void *d, const struct iiod_buf *buf, size_t nb);
77  ssize_t (*write)(void *d, const struct iiod_buf *buf, size_t nb);
78  ssize_t (*discard)(void *d, size_t bytes);
79 };
80 
81 /* Create / Destroy IIOD Responder. */
82 struct iiod_responder *
83 iiod_responder_create(const struct iiod_responder_ops *ops, void *d);
84 void iiod_responder_destroy(struct iiod_responder *responder);
85 
86 /* Set the timeout for I/O operations (default is 0 == infinite) */
87 void iiod_responder_set_timeout(struct iiod_responder *priv,
88  unsigned int timeout_ms);
89 void iiod_io_set_timeout(struct iiod_io *io, unsigned int timeout_ms);
90 
91 /* Read the current value of the micro-second counter */
92 uint64_t iiod_responder_read_counter_us(void);
93 
94 /* Stop the iiod_responder. */
95 void iiod_responder_stop(struct iiod_responder *responder);
96 
97 /* Wait until the iiod_responder stops. */
98 void iiod_responder_wait_done(struct iiod_responder *responder);
99 
100 /* Create a iiod_io instance, to be used for I/O. */
101 struct iiod_io *
102 iiod_responder_create_io(struct iiod_responder *responder, uint16_t id);
103 
104 struct iiod_io *
105 iiod_responder_get_default_io(struct iiod_responder *responder);
106 
107 /* Create a iiod_io suitable for responding to the given command.
108  * Initialized with the reference counter set to 1. */
109 struct iiod_io *
110 iiod_command_create_io(const struct iiod_command *cmd,
111  struct iiod_command_data *data);
112 
113 struct iiod_io *
114 iiod_command_get_default_io(struct iiod_command_data *data);
115 
116 /* Remove queued asynchronous requests for commands or responses. */
117 void iiod_io_cancel(struct iiod_io *io);
118 
119 /* Increase the internal reference counter */
120 void iiod_io_ref(struct iiod_io *io);
121 
122 /* Decrease the internal reference counter. If zero, the iiod_io instance is freed. */
123 void iiod_io_unref(struct iiod_io *io);
124 
125 /* Read the command's additional data, if any. */
126 int iiod_command_data_read(struct iiod_command_data *data,
127  const struct iiod_buf *buf);
128 
129 /* Send command or response to the remote */
130 int iiod_io_send_command(struct iiod_io *io,
131  const struct iiod_command *cmd,
132  const struct iiod_buf *buf, size_t nb);
133 int iiod_io_send_response(struct iiod_io *io, int32_t code,
134  const struct iiod_buf *buf, size_t nb);
135 
136 /* Send command, then read the response. */
137 int iiod_io_exec_command(struct iiod_io *io,
138  const struct iiod_command *cmd,
139  const struct iiod_buf *cmd_buf,
140  const struct iiod_buf *buf);
141 
142 /* Simplified version of iiod_io_exec_command.
143  * Send a simple command then read the response code. */
144 static inline int
145 iiod_io_exec_simple_command(struct iiod_io *io,
146  const struct iiod_command *cmd)
147 {
148  return iiod_io_exec_command(io, cmd, NULL, NULL);
149 }
150 
151 /* Asynchronous variants of the functions above */
152 int iiod_io_send_command_async(struct iiod_io *io,
153  const struct iiod_command *cmd,
154  const struct iiod_buf *buf, size_t nb);
155 int iiod_io_send_response_async(struct iiod_io *io, int32_t code,
156  const struct iiod_buf *buf, size_t nb);
157 
158 /* Wait for an async. command or response to be done sending */
159 int iiod_io_wait_for_command_done(struct iiod_io *io);
160 
161 _Bool iiod_io_command_is_done(struct iiod_io *io);
162 
163 /* Simplified version of iiod_io_send_response, to just send a code. */
164 static inline int
165 iiod_io_send_response_code(struct iiod_io *io, int32_t code)
166 {
167  return iiod_io_send_response(io, code, NULL, 0);
168 }
169 
170 /* Asynchronous variant of iiod_io_get_response */
171 int iiod_io_get_response_async(struct iiod_io *io,
172  const struct iiod_buf *buf, size_t nb);
173 
174 /* Wait for iiod_io_get_response_async to be done. */
175 int32_t iiod_io_wait_for_response(struct iiod_io *io);
176 
177 _Bool iiod_io_has_response(struct iiod_io *io);
178 
179 void iiod_io_cancel_response(struct iiod_io *io);
180 
181 #endif /* __IIOD_RESPONDER_H__ */
iiopp::value
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:707