aboutsummaryrefslogtreecommitdiff
path: root/libjaylink/transport.c
blob: 0c276b33d163d89bf9b677b988f466b110a8dd14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * This file is part of the libjaylink project.
 *
 * Copyright (C) 2014-2015 Marc Schink <jaylink-dev@marcschink.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "libjaylink.h"
#include "libjaylink-internal.h"

/**
 * @file
 *
 * Transport abstraction layer.
 */

/**
 * Open a device.
 *
 * This function must be called before any other function of the transport
 * abstraction layer for the given device handle is called.
 *
 * @param[in,out] devh Device handle.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR Other error conditions.
 */
JAYLINK_PRIV int transport_open(struct jaylink_device_handle *devh)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_open(devh);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_open(devh);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}

/**
 * Close a device.
 *
 * After this function has been called no other function of the transport
 * abstraction layer for the given device handle must be called.
 *
 * @param[in,out] devh Device handle.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR Other error conditions.
 */
JAYLINK_PRIV int transport_close(struct jaylink_device_handle *devh)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_close(devh);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_close(devh);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}

/**
 * Start a write operation for a device.
 *
 * The data of a write operation must be written with at least one call of
 * transport_write(). It is required that all data of a write operation is
 * written before an other write and/or read operation is started.
 *
 * @param[in,out] devh Device handle.
 * @param[in] length Number of bytes of the write operation.
 * @param[in] has_command Determines whether the data of the write operation
 *                        contains the protocol command.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 */
JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh,
		size_t length, bool has_command)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_start_write(devh, length, has_command);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_start_write(devh, length, has_command);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}

/**
 * Start a read operation for a device.
 *
 * The data of a read operation must be read with at least one call of
 * transport_read(). It is required that all data of a read operation is read
 * before an other write and/or read operation is started.
 *
 * @param[in,out] devh Device handle.
 * @param[in] length Number of bytes of the read operation.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 */
JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh,
		size_t length)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_start_read(devh, length);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_start_read(devh, length);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}

/**
 * Start a write and read operation for a device.
 *
 * This function starts a write and read operation as the consecutive call of
 * transport_start_write() and transport_start_read() but has a different
 * meaning from the protocol perspective and can therefore not be replaced by
 * these functions and vice versa.
 *
 * @note The write operation must be completed first before the read operation
 *       must be processed.
 *
 * @param[in,out] devh Device handle.
 * @param[in] write_length Number of bytes of the write operation.
 * @param[in] read_length Number of bytes of the read operation.
 * @param[in] has_command Determines whether the data of the write operation
 *                        contains the protocol command.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 */
JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh,
		size_t write_length, size_t read_length, bool has_command)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_start_write_read(devh, write_length,
			read_length, has_command);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_start_write_read(devh, write_length,
			read_length, has_command);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}

/**
 * Write data to a device.
 *
 * Before this function is used transport_start_write() or
 * transport_start_write_read() must be called to start a write operation. The
 * total number of written bytes must not exceed the number of bytes of the
 * write operation.
 *
 * @note A write operation will be performed and the data will be sent to the
 *       device when the number of written bytes reaches the number of bytes of
 *       the write operation. Before that the data will be written into a
 *       buffer.
 *
 * @param[in,out] devh Device handle.
 * @param[in] buffer Buffer to write data from.
 * @param[in] length Number of bytes to write.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR Other error conditions.
 */
JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh,
		const uint8_t *buffer, size_t length)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_write(devh, buffer, length);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_write(devh, buffer, length);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}

/**
 * Read data from a device.
 *
 * Before this function is used transport_start_read() or
 * transport_start_write_read() must be called to start a read operation. The
 * total number of read bytes must not exceed the number of bytes of the read
 * operation.
 *
 * @param[in,out] devh Device handle.
 * @param[out] buffer Buffer to read data into on success. Its content is
 *                    undefined on failure.
 * @param[in] length Number of bytes to read.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR Other error conditions.
 */
JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh,
		uint8_t *buffer, size_t length)
{
	int ret;

	switch (devh->dev->iface) {
#ifdef HAVE_LIBUSB
	case JAYLINK_HIF_USB:
		ret = transport_usb_read(devh, buffer, length);
		break;
#endif
	case JAYLINK_HIF_TCP:
		ret = transport_tcp_read(devh, buffer, length);
		break;
	default:
		log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.",
			devh->dev->iface);
		return JAYLINK_ERR;
	}

	return ret;
}