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/>.
23 #include <sys/types.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
36 #include "libjaylink.h"
37 #include "libjaylink-internal.h"
42 * Transport abstraction layer (TCP/IP).
46 #define CMD_SERVER 0x00
47 #define CMD_CLIENT 0x07
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
69 * Maximum length of the server name including trailing null-terminator in
72 #define SERVER_NAME_MAX_LENGTH 256
75 static int initialize_handle(struct jaylink_device_handle
*devh
)
77 struct jaylink_context
*ctx
;
81 devh
->buffer_size
= BUFFER_SIZE
;
82 devh
->buffer
= malloc(devh
->buffer_size
);
85 log_err(ctx
, "Transport buffer malloc failed.");
86 return JAYLINK_ERR_MALLOC
;
89 devh
->read_length
= 0;
90 devh
->bytes_available
= 0;
93 devh
->write_length
= 0;
99 static void cleanup_handle(struct jaylink_device_handle
*devh
)
104 static int _recv(struct jaylink_device_handle
*devh
, uint8_t *buffer
,
107 struct jaylink_context
*ctx
;
110 ctx
= devh
->dev
->ctx
;
115 if (!socket_recv(devh
->sock
, buffer
, &tmp
, 0)) {
116 log_err(ctx
, "Failed to receive data from device.");
117 return JAYLINK_ERR_IO
;
119 log_err(ctx
, "Failed to receive data from device: "
120 "remote connection closed.");
121 return JAYLINK_ERR_IO
;
127 log_dbg(ctx
, "Received %zu bytes from device.", tmp
);
133 static int handle_server_hello(struct jaylink_device_handle
*devh
)
136 struct jaylink_context
*ctx
;
137 uint8_t buf
[SERVER_HELLO_SIZE
];
138 char name
[SERVER_NAME_MAX_LENGTH
];
139 uint16_t proto_version
;
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.");
151 if (buf
[0] == RESP_MAX_CONNECTIONS
) {
152 log_err(ctx
, "Maximum number of connections reached.");
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
);
166 ret
= _recv(devh
, (uint8_t *)name
, length
);
168 if (ret
!= JAYLINK_OK
) {
169 log_err(ctx
, "Failed to receive server name.");
175 log_dbg(ctx
, "Server name: %s.", name
);
180 JAYLINK_PRIV
int transport_tcp_open(struct jaylink_device_handle
*devh
)
183 struct jaylink_context
*ctx
;
184 struct jaylink_device
*dev
;
185 struct addrinfo hints
;
186 struct addrinfo
*info
;
188 struct timeval timeout
;
194 log_dbg(ctx
, "Trying to open device (IPv4 address = %s).",
197 ret
= initialize_handle(devh
);
199 if (ret
!= JAYLINK_OK
) {
200 log_err(ctx
, "Initialize device handle failed.");
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
);
212 log_err(ctx
, "Address lookup failed.");
213 cleanup_handle(devh
);
219 for (rp
= info
; rp
!= NULL
; rp
= rp
->ai_next
) {
220 sock
= socket(rp
->ai_family
, rp
->ai_socktype
, rp
->ai_protocol
);
225 if (!connect(sock
, info
->ai_addr
, info
->ai_addrlen
))
235 log_err(ctx
, "Failed to open device.");
236 cleanup_handle(devh
);
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.");
249 cleanup_handle(devh
);
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.");
260 cleanup_handle(devh
);
266 ret
= handle_server_hello(devh
);
268 if (ret
!= JAYLINK_OK
) {
270 cleanup_handle(devh
);
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.");
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
;
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
;
316 devh
->buffer
[0] = CMD_CLIENT
;
323 JAYLINK_PRIV
int transport_tcp_start_read(struct jaylink_device_handle
*devh
,
326 struct jaylink_context
*ctx
;
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.",
343 devh
->read_length
= length
;
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.",
377 devh
->write_length
= write_length
;
381 devh
->buffer
[0] = CMD_CLIENT
;
385 devh
->read_length
= read_length
;
386 devh
->bytes_available
= 0;
392 static int _send(struct jaylink_device_handle
*devh
, const uint8_t *buffer
,
395 struct jaylink_context
*ctx
;
398 ctx
= devh
->dev
->ctx
;
403 if (!socket_send(devh
->sock
, buffer
, &tmp
, 0)) {
404 log_err(ctx
, "Failed to send data to device.");
405 return JAYLINK_ERR_IO
;
411 log_dbg(ctx
, "Sent %zu bytes to device.", tmp
);
417 static bool adjust_buffer(struct jaylink_device_handle
*devh
, size_t size
)
419 struct jaylink_context
*ctx
;
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)
431 size
= num
* BUFFER_SIZE
;
432 buffer
= realloc(devh
->buffer
, size
);
435 log_err(ctx
, "Failed to adjust buffer size to %zu bytes.",
440 devh
->buffer
= buffer
;
441 devh
->buffer_size
= size
;
443 log_dbg(ctx
, "Adjusted buffer size to %zu bytes.", size
);
448 JAYLINK_PRIV
int transport_tcp_write(struct jaylink_device_handle
*devh
,
449 const uint8_t *buffer
, size_t length
)
452 struct jaylink_context
*ctx
;
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
,
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
);
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
);
504 log_dbg(ctx
, "Buffer filled up with %zu bytes.", tmp
);
506 ret
= _send(devh
, devh
->buffer
, devh
->write_pos
+ tmp
);
510 if (ret
!= 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
)
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
,
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
);
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;
560 ret
= _recv(devh
, buffer
, length
);
562 if (ret
!= JAYLINK_OK
)
565 devh
->read_length
-= length
;