2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2015 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libjaylink.h"
26 #include "libjaylink-internal.h"
31 * Transport abstraction layer.
37 * This function must be called before any other function of the transport
38 * abstraction layer for the given device handle is called.
40 * @param[in,out] devh Device handle.
42 * @retval JAYLINK_OK Success.
43 * @retval JAYLINK_ERR_IO Input/output error.
44 * @retval JAYLINK_ERR Other error conditions.
46 JAYLINK_PRIV
int transport_open(struct jaylink_device_handle
*devh
)
50 switch (devh
->dev
->interface
) {
52 ret
= transport_usb_open(devh
);
55 ret
= transport_tcp_open(devh
);
58 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
59 devh
->dev
->interface
);
69 * After this function has been called no other function of the transport
70 * abstraction layer for the given device handle must be called.
72 * @param[in,out] devh Device handle.
74 * @retval JAYLINK_OK Success.
75 * @retval JAYLINK_ERR Other error conditions.
77 JAYLINK_PRIV
int transport_close(struct jaylink_device_handle
*devh
)
81 switch (devh
->dev
->interface
) {
83 ret
= transport_usb_close(devh
);
86 ret
= transport_tcp_close(devh
);
89 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
90 devh
->dev
->interface
);
98 * Start a write operation for a device.
100 * The data of a write operation must be written with at least one call of
101 * transport_write(). It is required that all data of a write operation is
102 * written before an other write and/or read operation is started.
104 * @param[in,out] devh Device handle.
105 * @param[in] length Number of bytes of the write operation.
106 * @param[in] has_command Determines whether the data of the write operation
107 * contains the protocol command.
109 * @retval JAYLINK_OK Success.
110 * @retval JAYLINK_ERR_ARG Invalid arguments.
112 JAYLINK_PRIV
int transport_start_write(struct jaylink_device_handle
*devh
,
113 size_t length
, bool has_command
)
117 switch (devh
->dev
->interface
) {
118 case JAYLINK_HIF_USB
:
119 ret
= transport_usb_start_write(devh
, length
, has_command
);
121 case JAYLINK_HIF_TCP
:
122 ret
= transport_tcp_start_write(devh
, length
, has_command
);
125 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
126 devh
->dev
->interface
);
134 * Start a read operation for a device.
136 * The data of a read operation must be read with at least one call of
137 * transport_read(). It is required that all data of a read operation is read
138 * before an other write and/or read operation is started.
140 * @param[in,out] devh Device handle.
141 * @param[in] length Number of bytes of the read operation.
143 * @retval JAYLINK_OK Success.
144 * @retval JAYLINK_ERR_ARG Invalid arguments.
146 JAYLINK_PRIV
int transport_start_read(struct jaylink_device_handle
*devh
,
151 switch (devh
->dev
->interface
) {
152 case JAYLINK_HIF_USB
:
153 ret
= transport_usb_start_read(devh
, length
);
155 case JAYLINK_HIF_TCP
:
156 ret
= transport_tcp_start_read(devh
, length
);
159 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
160 devh
->dev
->interface
);
168 * Start a write and read operation for a device.
170 * This function starts a write and read operation as the consecutive call of
171 * transport_start_write() and transport_start_read() but has a different
172 * meaning from the protocol perspective and can therefore not be replaced by
173 * these functions and vice versa.
175 * @note The write operation must be completed first before the read operation
178 * @param[in,out] devh Device handle.
179 * @param[in] write_length Number of bytes of the write operation.
180 * @param[in] read_length Number of bytes of the read operation.
181 * @param[in] has_command Determines whether the data of the write operation
182 * contains the protocol command.
184 * @retval JAYLINK_OK Success.
185 * @retval JAYLINK_ERR_ARG Invalid arguments.
187 JAYLINK_PRIV
int transport_start_write_read(struct jaylink_device_handle
*devh
,
188 size_t write_length
, size_t read_length
, bool has_command
)
192 switch (devh
->dev
->interface
) {
193 case JAYLINK_HIF_USB
:
194 ret
= transport_usb_start_write_read(devh
, write_length
,
195 read_length
, has_command
);
197 case JAYLINK_HIF_TCP
:
198 ret
= transport_tcp_start_write_read(devh
, write_length
,
199 read_length
, has_command
);
202 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
203 devh
->dev
->interface
);
211 * Write data to a device.
213 * Before this function is used transport_start_write() or
214 * transport_start_write_read() must be called to start a write operation. The
215 * total number of written bytes must not exceed the number of bytes of the
218 * @note A write operation will be performed and the data will be sent to the
219 * device when the number of written bytes reaches the number of bytes of
220 * the write operation. Before that the data will be written into a
223 * @param[in,out] devh Device handle.
224 * @param[in] buffer Buffer to write data from.
225 * @param[in] length Number of bytes to write.
227 * @retval JAYLINK_OK Success.
228 * @retval JAYLINK_ERR_ARG Invalid arguments.
229 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
230 * @retval JAYLINK_ERR_IO Input/output error.
231 * @retval JAYLINK_ERR Other error conditions.
233 JAYLINK_PRIV
int transport_write(struct jaylink_device_handle
*devh
,
234 const uint8_t *buffer
, size_t length
)
238 switch (devh
->dev
->interface
) {
239 case JAYLINK_HIF_USB
:
240 ret
= transport_usb_write(devh
, buffer
, length
);
242 case JAYLINK_HIF_TCP
:
243 ret
= transport_tcp_write(devh
, buffer
, length
);
246 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
247 devh
->dev
->interface
);
255 * Read data from a device.
257 * Before this function is used transport_start_read() or
258 * transport_start_write_read() must be called to start a read operation. The
259 * total number of read bytes must not exceed the number of bytes of the read
262 * @param[in,out] devh Device handle.
263 * @param[out] buffer Buffer to read data into on success. Its content is
264 * undefined on failure.
265 * @param[in] length Number of bytes to read.
267 * @retval JAYLINK_OK Success.
268 * @retval JAYLINK_ERR_ARG Invalid arguments.
269 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
270 * @retval JAYLINK_ERR_IO Input/output error.
271 * @retval JAYLINK_ERR Other error conditions.
273 JAYLINK_PRIV
int transport_read(struct jaylink_device_handle
*devh
,
274 uint8_t *buffer
, size_t length
)
278 switch (devh
->dev
->interface
) {
279 case JAYLINK_HIF_USB
:
280 ret
= transport_usb_read(devh
, buffer
, length
);
282 case JAYLINK_HIF_TCP
:
283 ret
= transport_tcp_read(devh
, buffer
, length
);
286 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u.",
287 devh
->dev
->interface
);