Bump package version to 0.4.0
[libjaylink.git] / libjaylink / transport.c
blobad9d6f54589f914406252fcd664566bf434be6f0
1 /*
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/>.
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <string.h>
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include "libjaylink.h"
29 #include "libjaylink-internal.h"
31 /**
32 * @file
34 * Transport abstraction layer.
37 /**
38 * Open a device.
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)
51 int ret;
53 switch (devh->dev->iface) {
54 #ifdef HAVE_LIBUSB
55 case JAYLINK_HIF_USB:
56 ret = transport_usb_open(devh);
57 break;
58 #endif
59 case JAYLINK_HIF_TCP:
60 ret = transport_tcp_open(devh);
61 break;
62 default:
63 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
64 devh->dev->iface);
65 return JAYLINK_ERR;
68 return ret;
71 /**
72 * Close a device.
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)
84 int ret;
86 switch (devh->dev->iface) {
87 #ifdef HAVE_LIBUSB
88 case JAYLINK_HIF_USB:
89 ret = transport_usb_close(devh);
90 break;
91 #endif
92 case JAYLINK_HIF_TCP:
93 ret = transport_tcp_close(devh);
94 break;
95 default:
96 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
97 devh->dev->iface);
98 return JAYLINK_ERR;
101 return ret;
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)
122 int ret;
124 switch (devh->dev->iface) {
125 #ifdef HAVE_LIBUSB
126 case JAYLINK_HIF_USB:
127 ret = transport_usb_start_write(devh, length, has_command);
128 break;
129 #endif
130 case JAYLINK_HIF_TCP:
131 ret = transport_tcp_start_write(devh, length, has_command);
132 break;
133 default:
134 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
135 devh->dev->iface);
136 return JAYLINK_ERR;
139 return ret;
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,
156 size_t length)
158 int ret;
160 switch (devh->dev->iface) {
161 #ifdef HAVE_LIBUSB
162 case JAYLINK_HIF_USB:
163 ret = transport_usb_start_read(devh, length);
164 break;
165 #endif
166 case JAYLINK_HIF_TCP:
167 ret = transport_tcp_start_read(devh, length);
168 break;
169 default:
170 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
171 devh->dev->iface);
172 return JAYLINK_ERR;
175 return ret;
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
187 * must be processed.
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)
201 int ret;
203 switch (devh->dev->iface) {
204 #ifdef HAVE_LIBUSB
205 case JAYLINK_HIF_USB:
206 ret = transport_usb_start_write_read(devh, write_length,
207 read_length, has_command);
208 break;
209 #endif
210 case JAYLINK_HIF_TCP:
211 ret = transport_tcp_start_write_read(devh, write_length,
212 read_length, has_command);
213 break;
214 default:
215 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
216 devh->dev->iface);
217 return JAYLINK_ERR;
220 return ret;
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
229 * write operation.
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
234 * buffer.
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)
249 int ret;
251 switch (devh->dev->iface) {
252 #ifdef HAVE_LIBUSB
253 case JAYLINK_HIF_USB:
254 ret = transport_usb_write(devh, buffer, length);
255 break;
256 #endif
257 case JAYLINK_HIF_TCP:
258 ret = transport_tcp_write(devh, buffer, length);
259 break;
260 default:
261 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
262 devh->dev->iface);
263 return JAYLINK_ERR;
266 return ret;
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
275 * operation.
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)
291 int ret;
293 switch (devh->dev->iface) {
294 #ifdef HAVE_LIBUSB
295 case JAYLINK_HIF_USB:
296 ret = transport_usb_read(devh, buffer, length);
297 break;
298 #endif
299 case JAYLINK_HIF_TCP:
300 ret = transport_tcp_read(devh, buffer, length);
301 break;
302 default:
303 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u",
304 devh->dev->iface);
305 return JAYLINK_ERR;
308 return ret;