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/>.
28 #include "libjaylink.h"
29 #include "libjaylink-internal.h"
34 * Transport abstraction layer.
40 * This function must be called before any other function of the transport
41 * abstraction layer for the given device handle is called.
43 * @param[in,out] devh Device handle.
45 * @retval JAYLINK_OK Success.
46 * @retval JAYLINK_ERR_IO Input/output error.
47 * @retval JAYLINK_ERR Other error conditions.
49 JAYLINK_PRIV
int transport_open(struct jaylink_device_handle
*devh
)
53 switch (devh
->dev
->iface
) {
56 ret
= transport_usb_open(devh
);
60 ret
= transport_tcp_open(devh
);
63 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",
74 * After this function has been called no other function of the transport
75 * abstraction layer for the given device handle must be called.
77 * @param[in,out] devh Device handle.
79 * @retval JAYLINK_OK Success.
80 * @retval JAYLINK_ERR Other error conditions.
82 JAYLINK_PRIV
int transport_close(struct jaylink_device_handle
*devh
)
86 switch (devh
->dev
->iface
) {
89 ret
= transport_usb_close(devh
);
93 ret
= transport_tcp_close(devh
);
96 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",
105 * Start a write operation for a device.
107 * The data of a write operation must be written with at least one call of
108 * transport_write(). It is required that all data of a write operation is
109 * written before an other write and/or read operation is started.
111 * @param[in,out] devh Device handle.
112 * @param[in] length Number of bytes of the write operation.
113 * @param[in] has_command Determines whether the data of the write operation
114 * contains the protocol command.
116 * @retval JAYLINK_OK Success.
117 * @retval JAYLINK_ERR_ARG Invalid arguments.
119 JAYLINK_PRIV
int transport_start_write(struct jaylink_device_handle
*devh
,
120 size_t length
, bool has_command
)
124 switch (devh
->dev
->iface
) {
126 case JAYLINK_HIF_USB
:
127 ret
= transport_usb_start_write(devh
, length
, has_command
);
130 case JAYLINK_HIF_TCP
:
131 ret
= transport_tcp_start_write(devh
, length
, has_command
);
134 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",
143 * Start a read operation for a device.
145 * The data of a read operation must be read with at least one call of
146 * transport_read(). It is required that all data of a read operation is read
147 * before an other write and/or read operation is started.
149 * @param[in,out] devh Device handle.
150 * @param[in] length Number of bytes of the read operation.
152 * @retval JAYLINK_OK Success.
153 * @retval JAYLINK_ERR_ARG Invalid arguments.
155 JAYLINK_PRIV
int transport_start_read(struct jaylink_device_handle
*devh
,
160 switch (devh
->dev
->iface
) {
162 case JAYLINK_HIF_USB
:
163 ret
= transport_usb_start_read(devh
, length
);
166 case JAYLINK_HIF_TCP
:
167 ret
= transport_tcp_start_read(devh
, length
);
170 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",
179 * Start a write and read operation for a device.
181 * This function starts a write and read operation as the consecutive call of
182 * transport_start_write() and transport_start_read() but has a different
183 * meaning from the protocol perspective and can therefore not be replaced by
184 * these functions and vice versa.
186 * @note The write operation must be completed first before the read operation
189 * @param[in,out] devh Device handle.
190 * @param[in] write_length Number of bytes of the write operation.
191 * @param[in] read_length Number of bytes of the read operation.
192 * @param[in] has_command Determines whether the data of the write operation
193 * contains the protocol command.
195 * @retval JAYLINK_OK Success.
196 * @retval JAYLINK_ERR_ARG Invalid arguments.
198 JAYLINK_PRIV
int transport_start_write_read(struct jaylink_device_handle
*devh
,
199 size_t write_length
, size_t read_length
, bool has_command
)
203 switch (devh
->dev
->iface
) {
205 case JAYLINK_HIF_USB
:
206 ret
= transport_usb_start_write_read(devh
, write_length
,
207 read_length
, has_command
);
210 case JAYLINK_HIF_TCP
:
211 ret
= transport_tcp_start_write_read(devh
, write_length
,
212 read_length
, has_command
);
215 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",
224 * Write data to a device.
226 * Before this function is used transport_start_write() or
227 * transport_start_write_read() must be called to start a write operation. The
228 * total number of written bytes must not exceed the number of bytes of the
231 * @note A write operation will be performed and the data will be sent to the
232 * device when the number of written bytes reaches the number of bytes of
233 * the write operation. Before that the data will be written into a
236 * @param[in,out] devh Device handle.
237 * @param[in] buffer Buffer to write data from.
238 * @param[in] length Number of bytes to write.
240 * @retval JAYLINK_OK Success.
241 * @retval JAYLINK_ERR_ARG Invalid arguments.
242 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
243 * @retval JAYLINK_ERR_IO Input/output error.
244 * @retval JAYLINK_ERR Other error conditions.
246 JAYLINK_PRIV
int transport_write(struct jaylink_device_handle
*devh
,
247 const uint8_t *buffer
, size_t length
)
251 switch (devh
->dev
->iface
) {
253 case JAYLINK_HIF_USB
:
254 ret
= transport_usb_write(devh
, buffer
, length
);
257 case JAYLINK_HIF_TCP
:
258 ret
= transport_tcp_write(devh
, buffer
, length
);
261 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",
270 * Read data from a device.
272 * Before this function is used transport_start_read() or
273 * transport_start_write_read() must be called to start a read operation. The
274 * total number of read bytes must not exceed the number of bytes of the read
277 * @param[in,out] devh Device handle.
278 * @param[out] buffer Buffer to read data into on success. Its content is
279 * undefined on failure.
280 * @param[in] length Number of bytes to read.
282 * @retval JAYLINK_OK Success.
283 * @retval JAYLINK_ERR_ARG Invalid arguments.
284 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
285 * @retval JAYLINK_ERR_IO Input/output error.
286 * @retval JAYLINK_ERR Other error conditions.
288 JAYLINK_PRIV
int transport_read(struct jaylink_device_handle
*devh
,
289 uint8_t *buffer
, size_t length
)
293 switch (devh
->dev
->iface
) {
295 case JAYLINK_HIF_USB
:
296 ret
= transport_usb_read(devh
, buffer
, length
);
299 case JAYLINK_HIF_TCP
:
300 ret
= transport_tcp_read(devh
, buffer
, length
);
303 log_err(devh
->dev
->ctx
, "BUG: Invalid host interface: %u",