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