transport: Add initial TCP/IP support
[libjaylink.git] / libjaylink / transport.c
blob11837a4f91ca13d70c2638df565e409c12b59dc4
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 #include "libjaylink.h"
26 #include "libjaylink-internal.h"
28 /**
29 * @file
31 * Transport abstraction layer.
34 /**
35 * Open a device.
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)
48 int ret;
50 switch (devh->dev->interface) {
51 case JAYLINK_HIF_USB:
52 ret = transport_usb_open(devh);
53 break;
54 case JAYLINK_HIF_TCP:
55 ret = transport_tcp_open(devh);
56 break;
57 default:
58 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
59 devh->dev->interface);
60 return JAYLINK_ERR;
63 return ret;
66 /**
67 * Close a device.
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)
79 int ret;
81 switch (devh->dev->interface) {
82 case JAYLINK_HIF_USB:
83 ret = transport_usb_close(devh);
84 break;
85 case JAYLINK_HIF_TCP:
86 ret = transport_tcp_close(devh);
87 break;
88 default:
89 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
90 devh->dev->interface);
91 return JAYLINK_ERR;
94 return ret;
97 /**
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)
115 int ret;
117 switch (devh->dev->interface) {
118 case JAYLINK_HIF_USB:
119 ret = transport_usb_start_write(devh, length, has_command);
120 break;
121 case JAYLINK_HIF_TCP:
122 ret = transport_tcp_start_write(devh, length, has_command);
123 break;
124 default:
125 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
126 devh->dev->interface);
127 return JAYLINK_ERR;
130 return ret;
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,
147 size_t length)
149 int ret;
151 switch (devh->dev->interface) {
152 case JAYLINK_HIF_USB:
153 ret = transport_usb_start_read(devh, length);
154 break;
155 case JAYLINK_HIF_TCP:
156 ret = transport_tcp_start_read(devh, length);
157 break;
158 default:
159 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
160 devh->dev->interface);
161 return JAYLINK_ERR;
164 return ret;
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
176 * must be processed.
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)
190 int ret;
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);
196 break;
197 case JAYLINK_HIF_TCP:
198 ret = transport_tcp_start_write_read(devh, write_length,
199 read_length, has_command);
200 break;
201 default:
202 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
203 devh->dev->interface);
204 return JAYLINK_ERR;
207 return ret;
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
216 * write operation.
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
221 * buffer.
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)
236 int ret;
238 switch (devh->dev->interface) {
239 case JAYLINK_HIF_USB:
240 ret = transport_usb_write(devh, buffer, length);
241 break;
242 case JAYLINK_HIF_TCP:
243 ret = transport_tcp_write(devh, buffer, length);
244 break;
245 default:
246 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
247 devh->dev->interface);
248 return JAYLINK_ERR;
251 return ret;
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
260 * operation.
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)
276 int ret;
278 switch (devh->dev->interface) {
279 case JAYLINK_HIF_USB:
280 ret = transport_usb_read(devh, buffer, length);
281 break;
282 case JAYLINK_HIF_TCP:
283 ret = transport_tcp_read(devh, buffer, length);
284 break;
285 default:
286 log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
287 devh->dev->interface);
288 return JAYLINK_ERR;
291 return ret;