configure.ac: Add switch to disable libusb support
[libjaylink.git] / libjaylink / transport_tcp.c
blobc89540b7e5739f7e7e3da6e953dd1824b1920a7d
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2015-2017 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 <string.h>
23 #include <sys/types.h>
25 #ifdef _WIN32
26 #include <winsock2.h>
27 #include <ws2tcpip.h>
28 #else
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33 #include <netinet/in.h>
34 #endif
36 #include "libjaylink.h"
37 #include "libjaylink-internal.h"
39 /**
40 * @file
42 * Transport abstraction layer (TCP/IP).
45 /** @cond PRIVATE */
46 #define CMD_SERVER 0x00
47 #define CMD_CLIENT 0x07
49 /**
50 * Response status code indicating that the maximum number of simultaneous
51 * connections on the device has been reached.
53 #define RESP_MAX_CONNECTIONS 0xfe
55 /** Buffer size in bytes. */
56 #define BUFFER_SIZE 2048
58 /** Timeout of a receive operation in milliseconds. */
59 #define RECV_TIMEOUT 5000
60 /** Timeout of a send operation in milliseconds. */
61 #define SEND_TIMEOUT 5000
63 /** String of the port number for the J-Link TCP/IP protocol. */
64 #define PORT_STRING "19020"
66 /** Size of the server's hello message in bytes. */
67 #define SERVER_HELLO_SIZE 4
68 /**
69 * Maximum length of the server name including trailing null-terminator in
70 * bytes.
72 #define SERVER_NAME_MAX_LENGTH 256
73 /** @endcond */
75 static int initialize_handle(struct jaylink_device_handle *devh)
77 struct jaylink_context *ctx;
79 ctx = devh->dev->ctx;
81 devh->buffer_size = BUFFER_SIZE;
82 devh->buffer = malloc(devh->buffer_size);
84 if (!devh->buffer) {
85 log_err(ctx, "Transport buffer malloc failed.");
86 return JAYLINK_ERR_MALLOC;
89 devh->read_length = 0;
90 devh->bytes_available = 0;
91 devh->read_pos = 0;
93 devh->write_length = 0;
94 devh->write_pos = 0;
96 return JAYLINK_OK;
99 static void cleanup_handle(struct jaylink_device_handle *devh)
101 free(devh->buffer);
104 static int _recv(struct jaylink_device_handle *devh, uint8_t *buffer,
105 size_t length)
107 struct jaylink_context *ctx;
108 size_t tmp;
110 ctx = devh->dev->ctx;
112 while (length > 0) {
113 tmp = length;
115 if (!socket_recv(devh->sock, buffer, &tmp, 0)) {
116 log_err(ctx, "Failed to receive data from device.");
117 return JAYLINK_ERR_IO;
118 } else if (!tmp) {
119 log_err(ctx, "Failed to receive data from device: "
120 "remote connection closed.");
121 return JAYLINK_ERR_IO;
124 buffer += tmp;
125 length -= tmp;
127 log_dbg(ctx, "Received %zu bytes from device.", tmp);
130 return JAYLINK_OK;
133 static int handle_server_hello(struct jaylink_device_handle *devh)
135 int ret;
136 struct jaylink_context *ctx;
137 uint8_t buf[SERVER_HELLO_SIZE];
138 char name[SERVER_NAME_MAX_LENGTH];
139 uint16_t proto_version;
140 size_t length;
142 ctx = devh->dev->ctx;
144 ret = _recv(devh, buf, sizeof(buf));
146 if (ret != JAYLINK_OK) {
147 log_err(ctx, "Failed to receive hello message.");
148 return ret;
151 if (buf[0] == RESP_MAX_CONNECTIONS) {
152 log_err(ctx, "Maximum number of connections reached.");
153 return JAYLINK_ERR;
156 if (buf[0] != CMD_SERVER) {
157 log_err(ctx, "Invalid hello message received.");
158 return JAYLINK_ERR_PROTO;
161 proto_version = buffer_get_u16(buf, 1);
163 log_dbg(ctx, "Protocol version: 0x%04x.", proto_version);
165 length = buf[3];
166 ret = _recv(devh, (uint8_t *)name, length);
168 if (ret != JAYLINK_OK) {
169 log_err(ctx, "Failed to receive server name.");
170 return ret;
173 name[length] = '\0';
175 log_dbg(ctx, "Server name: %s.", name);
177 return JAYLINK_OK;
180 JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh)
182 int ret;
183 struct jaylink_context *ctx;
184 struct jaylink_device *dev;
185 struct addrinfo hints;
186 struct addrinfo *info;
187 struct addrinfo *rp;
188 struct timeval timeout;
189 int sock;
191 dev = devh->dev;
192 ctx = dev->ctx;
194 log_dbg(ctx, "Trying to open device (IPv4 address = %s).",
195 dev->ipv4_address);
197 ret = initialize_handle(devh);
199 if (ret != JAYLINK_OK) {
200 log_err(ctx, "Initialize device handle failed.");
201 return ret;
204 memset(&hints, 0, sizeof(struct addrinfo));
205 hints.ai_family = AF_INET;
206 hints.ai_socktype = SOCK_STREAM;
207 hints.ai_protocol = IPPROTO_TCP;
209 ret = getaddrinfo(dev->ipv4_address, PORT_STRING, &hints, &info);
211 if (ret != 0) {
212 log_err(ctx, "Address lookup failed.");
213 cleanup_handle(devh);
214 return JAYLINK_ERR;
217 sock = -1;
219 for (rp = info; rp != NULL; rp = rp->ai_next) {
220 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
222 if (sock < 0)
223 continue;
225 if (!connect(sock, info->ai_addr, info->ai_addrlen))
226 break;
228 socket_close(sock);
229 sock = -1;
232 freeaddrinfo(info);
234 if (sock < 0) {
235 log_err(ctx, "Failed to open device.");
236 cleanup_handle(devh);
237 return JAYLINK_ERR;
240 log_dbg(ctx, "Device opened successfully.");
242 timeout.tv_sec = RECV_TIMEOUT / 1000;
243 timeout.tv_usec = (RECV_TIMEOUT % 1000) * 1000;
245 if (!socket_set_option(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
246 sizeof(struct timeval))) {
247 log_err(ctx, "Failed to set socket receive timeout.");
248 socket_close(sock);
249 cleanup_handle(devh);
250 return JAYLINK_ERR;
253 timeout.tv_sec = SEND_TIMEOUT / 1000;
254 timeout.tv_usec = (SEND_TIMEOUT % 1000) * 1000;
256 if (!socket_set_option(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
257 sizeof(struct timeval))) {
258 log_err(ctx, "Failed to set socket send timeout.");
259 socket_close(sock);
260 cleanup_handle(devh);
261 return JAYLINK_ERR;
264 devh->sock = sock;
266 ret = handle_server_hello(devh);
268 if (ret != JAYLINK_OK) {
269 socket_close(sock);
270 cleanup_handle(devh);
271 return ret;
274 return JAYLINK_OK;
277 JAYLINK_PRIV int transport_tcp_close(struct jaylink_device_handle *devh)
279 struct jaylink_context *ctx;
281 ctx = devh->dev->ctx;
283 log_dbg(ctx, "Closing device (IPv4 address = %s).",
284 devh->dev->ipv4_address);
286 cleanup_handle(devh);
288 log_dbg(ctx, "Device closed successfully.");
290 return JAYLINK_OK;
293 JAYLINK_PRIV int transport_tcp_start_write(struct jaylink_device_handle *devh,
294 size_t length, bool has_command)
296 struct jaylink_context *ctx;
298 if (!length)
299 return JAYLINK_ERR_ARG;
301 ctx = devh->dev->ctx;
303 log_dbg(ctx, "Starting write operation (length = %zu bytes).", length);
305 if (devh->write_pos > 0)
306 log_warn(ctx, "Last write operation left %zu bytes in the "
307 "buffer.", devh->write_pos);
309 if (devh->write_length > 0)
310 log_warn(ctx, "Last write operation was not performed.");
312 devh->write_length = length;
313 devh->write_pos = 0;
315 if (has_command) {
316 devh->buffer[0] = CMD_CLIENT;
317 devh->write_pos++;
320 return JAYLINK_OK;
323 JAYLINK_PRIV int transport_tcp_start_read(struct jaylink_device_handle *devh,
324 size_t length)
326 struct jaylink_context *ctx;
328 if (!length)
329 return JAYLINK_ERR_ARG;
331 ctx = devh->dev->ctx;
333 log_dbg(ctx, "Starting read operation (length = %zu bytes).", length);
335 if (devh->bytes_available > 0)
336 log_dbg(ctx, "Last read operation left %zu bytes in the "
337 "buffer.", devh->bytes_available);
339 if (devh->read_length > 0)
340 log_warn(ctx, "Last read operation left %zu bytes.",
341 devh->read_length);
343 devh->read_length = length;
345 return JAYLINK_OK;
348 JAYLINK_PRIV int transport_tcp_start_write_read(
349 struct jaylink_device_handle *devh, size_t write_length,
350 size_t read_length, bool has_command)
352 struct jaylink_context *ctx;
354 if (!read_length || !write_length)
355 return JAYLINK_ERR_ARG;
357 ctx = devh->dev->ctx;
359 log_dbg(ctx, "Starting write / read operation (length = "
360 "%zu / %zu bytes).", write_length, read_length);
362 if (devh->write_pos > 0)
363 log_warn(ctx, "Last write operation left %zu bytes in the "
364 "buffer.", devh->write_pos);
366 if (devh->write_length > 0)
367 log_warn(ctx, "Last write operation was not performed.");
369 if (devh->bytes_available > 0)
370 log_warn(ctx, "Last read operation left %zu bytes in the "
371 "buffer.", devh->bytes_available);
373 if (devh->read_length > 0)
374 log_warn(ctx, "Last read operation left %zu bytes.",
375 devh->read_length);
377 devh->write_length = write_length;
378 devh->write_pos = 0;
380 if (has_command) {
381 devh->buffer[0] = CMD_CLIENT;
382 devh->write_pos++;
385 devh->read_length = read_length;
386 devh->bytes_available = 0;
387 devh->read_pos = 0;
389 return JAYLINK_OK;
392 static int _send(struct jaylink_device_handle *devh, const uint8_t *buffer,
393 size_t length)
395 struct jaylink_context *ctx;
396 size_t tmp;
398 ctx = devh->dev->ctx;
400 while (length > 0) {
401 tmp = length;
403 if (!socket_send(devh->sock, buffer, &tmp, 0)) {
404 log_err(ctx, "Failed to send data to device.");
405 return JAYLINK_ERR_IO;
408 buffer += tmp;
409 length -= tmp;
411 log_dbg(ctx, "Sent %zu bytes to device.", tmp);
414 return JAYLINK_OK;
417 static bool adjust_buffer(struct jaylink_device_handle *devh, size_t size)
419 struct jaylink_context *ctx;
420 uint8_t *buffer;
421 size_t num;
423 ctx = devh->dev->ctx;
425 /* Adjust buffer size to a multiple of BUFFER_SIZE bytes. */
426 num = size / BUFFER_SIZE;
428 if (size % BUFFER_SIZE > 0)
429 num++;
431 size = num * BUFFER_SIZE;
432 buffer = realloc(devh->buffer, size);
434 if (!buffer) {
435 log_err(ctx, "Failed to adjust buffer size to %zu bytes.",
436 size);
437 return false;
440 devh->buffer = buffer;
441 devh->buffer_size = size;
443 log_dbg(ctx, "Adjusted buffer size to %zu bytes.", size);
445 return true;
448 JAYLINK_PRIV int transport_tcp_write(struct jaylink_device_handle *devh,
449 const uint8_t *buffer, size_t length)
451 int ret;
452 struct jaylink_context *ctx;
453 size_t tmp;
455 ctx = devh->dev->ctx;
457 if (length > devh->write_length) {
458 log_err(ctx, "Requested to write %zu bytes but only %zu bytes "
459 "are expected for the write operation.", length,
460 devh->write_length);
461 return JAYLINK_ERR_ARG;
465 * Store data in the buffer if the expected number of bytes for the
466 * write operation is not reached.
468 if (length < devh->write_length) {
469 if (devh->write_pos + length > devh->buffer_size) {
470 if (!adjust_buffer(devh, devh->write_pos + length))
471 return JAYLINK_ERR_MALLOC;
474 memcpy(devh->buffer + devh->write_pos, buffer, length);
476 devh->write_length -= length;
477 devh->write_pos += length;
479 log_dbg(ctx, "Wrote %zu bytes into buffer.", length);
480 return JAYLINK_OK;
484 * Expected number of bytes for this write operation is reached and
485 * therefore the write operation will be performed.
487 devh->write_length = 0;
489 /* Send data directly to the device if the buffer is empty. */
490 if (!devh->write_pos)
491 return _send(devh, buffer, length);
493 tmp = MIN(length, devh->buffer_size - devh->write_pos);
496 * Fill up the internal buffer in order to reduce the number of
497 * messages sent to the device for performance reasons.
499 memcpy(devh->buffer + devh->write_pos, buffer, tmp);
501 length -= tmp;
502 buffer += tmp;
504 log_dbg(ctx, "Buffer filled up with %zu bytes.", tmp);
506 ret = _send(devh, devh->buffer, devh->write_pos + tmp);
508 devh->write_pos = 0;
510 if (ret != JAYLINK_OK)
511 return ret;
513 if (!length)
514 return JAYLINK_OK;
516 return _send(devh, buffer, length);
519 JAYLINK_PRIV int transport_tcp_read(struct jaylink_device_handle *devh,
520 uint8_t *buffer, size_t length)
522 int ret;
523 struct jaylink_context *ctx;
525 ctx = devh->dev->ctx;
527 if (length > devh->read_length) {
528 log_err(ctx, "Requested to read %zu bytes but only %zu bytes "
529 "are expected for the read operation.", length,
530 devh->read_length);
531 return JAYLINK_ERR_ARG;
534 if (length <= devh->bytes_available) {
535 memcpy(buffer, devh->buffer + devh->read_pos, length);
537 devh->read_length -= length;
538 devh->bytes_available -= length;
539 devh->read_pos += length;
541 log_dbg(ctx, "Read %zu bytes from buffer.", length);
542 return JAYLINK_OK;
545 if (devh->bytes_available) {
546 memcpy(buffer, devh->buffer + devh->read_pos,
547 devh->bytes_available);
549 buffer += devh->bytes_available;
550 length -= devh->bytes_available;
551 devh->read_length -= devh->bytes_available;
553 log_dbg(ctx, "Read %zu bytes from buffer to flush it.",
554 devh->bytes_available);
556 devh->bytes_available = 0;
557 devh->read_pos = 0;
560 ret = _recv(devh, buffer, length);
562 if (ret != JAYLINK_OK)
563 return ret;
565 devh->read_length -= length;
567 return JAYLINK_OK;