libiio
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 
53  IIOD_NB_OPCODES,
54 };
55 
56 struct iiod_command {
57  uint16_t client_id;
58  uint8_t op;
59  uint8_t dev;
60  int32_t code;
61 };
62 
63 struct iiod_buf {
64  void *ptr;
65  size_t size;
66 };
67 
69  int (*cmd)(const struct iiod_command *cmd,
70  struct iiod_command_data *data, void *d);
71  ssize_t (*read)(void *d, const struct iiod_buf *buf, size_t nb);
72  ssize_t (*write)(void *d, const struct iiod_buf *buf, size_t nb);
73  ssize_t (*discard)(void *d, size_t bytes);
74 };
75 
76 /* Create / Destroy IIOD Responder. */
77 struct iiod_responder *
78 iiod_responder_create(const struct iiod_responder_ops *ops, void *d);
79 void iiod_responder_destroy(struct iiod_responder *responder);
80 
81 /* Set the timeout for I/O operations (default is 0 == infinite) */
82 void iiod_responder_set_timeout(struct iiod_responder *priv,
83  unsigned int timeout_ms);
84 
85 /* Read the current value of the micro-second counter */
86 uint64_t iiod_responder_read_counter_us(void);
87 
88 /* Wait until the iiod_responder stops. */
89 void iiod_responder_wait_done(struct iiod_responder *responder);
90 
91 /* Create a iiod_io instance, to be used for I/O. */
92 struct iiod_io *
93 iiod_responder_create_io(struct iiod_responder *responder, uint16_t id);
94 
95 struct iiod_io *
96 iiod_responder_get_default_io(struct iiod_responder *responder);
97 
98 /* Create a iiod_io suitable for responding to the given command.
99  * Initialized with the reference counter set to 1. */
100 struct iiod_io *
101 iiod_command_create_io(const struct iiod_command *cmd,
102  struct iiod_command_data *data);
103 
104 struct iiod_io *
105 iiod_command_get_default_io(struct iiod_command_data *data);
106 
107 /* Remove queued asynchronous requests for commands or responses. */
108 void iiod_io_cancel(struct iiod_io *io);
109 
110 /* Increase the internal reference counter */
111 void iiod_io_ref(struct iiod_io *io);
112 
113 /* Decrease the internal reference counter. If zero, the iiod_io instance is freed. */
114 void iiod_io_unref(struct iiod_io *io);
115 
116 /* Read the command's additional data, if any. */
117 int iiod_command_data_read(struct iiod_command_data *data,
118  const struct iiod_buf *buf);
119 
120 /* Send command or response to the remote */
121 int iiod_io_send_command(struct iiod_io *io,
122  const struct iiod_command *cmd,
123  const struct iiod_buf *buf, size_t nb);
124 int iiod_io_send_response(struct iiod_io *io, intptr_t code,
125  const struct iiod_buf *buf, size_t nb);
126 
127 /* Send command, then read the response. */
128 int iiod_io_exec_command(struct iiod_io *io,
129  const struct iiod_command *cmd,
130  const struct iiod_buf *cmd_buf,
131  const struct iiod_buf *buf);
132 
133 /* Simplified version of iiod_io_exec_command.
134  * Send a simple command then read the response code. */
135 static inline int
136 iiod_io_exec_simple_command(struct iiod_io *io,
137  const struct iiod_command *cmd)
138 {
139  return iiod_io_exec_command(io, cmd, NULL, NULL);
140 }
141 
142 /* Asynchronous variants of the functions above */
143 int iiod_io_send_command_async(struct iiod_io *io,
144  const struct iiod_command *cmd,
145  const struct iiod_buf *buf, size_t nb);
146 int iiod_io_send_response_async(struct iiod_io *io, intptr_t code,
147  const struct iiod_buf *buf, size_t nb);
148 
149 /* Wait for an async. command or response to be done sending */
150 int iiod_io_wait_for_command_done(struct iiod_io *io);
151 
152 _Bool iiod_io_command_is_done(struct iiod_io *io);
153 
154 /* Simplified version of iiod_io_send_response, to just send a code. */
155 static inline int
156 iiod_io_send_response_code(struct iiod_io *io, intptr_t code)
157 {
158  return iiod_io_send_response(io, code, NULL, 0);
159 }
160 
161 /* Asynchronous variant of iiod_io_get_response */
162 int iiod_io_get_response_async(struct iiod_io *io,
163  const struct iiod_buf *buf, size_t nb);
164 
165 /* Wait for iiod_io_get_response_async to be done. */
166 intptr_t iiod_io_wait_for_response(struct iiod_io *io);
167 
168 _Bool iiod_io_has_response(struct iiod_io *io);
169 
170 void iiod_io_cancel_response(struct iiod_io *io);
171 
172 #endif /* __IIOD_RESPONDER_H__ */
Definition: iiod-responder.h:63
Definition: iiod-responder.c:39
Definition: iiod-responder.c:64
Definition: iiod-responder.h:56
Definition: iiod-responder.h:68