aboutsummaryrefslogtreecommitdiff
path: root/libjaylink/transport_tcp.c
blob: 1ba978a0a943aeb6080aa6b846a1455c12631a64 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
 * This file is part of the libjaylink project.
 *
 * Copyright (C) 2015-2017 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 <string.h>
#include <sys/types.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#endif

#include "libjaylink.h"
#include "libjaylink-internal.h"

/**
 * @file
 *
 * Transport abstraction layer (TCP/IP).
 */

/** @cond PRIVATE */
#define CMD_SERVER		0x00
#define CMD_CLIENT		0x07

/**
 * Response status code indicating that the maximum number of simultaneous
 * connections on the device has been reached.
 */
#define RESP_MAX_CONNECTIONS	0xfe

/** Buffer size in bytes. */
#define BUFFER_SIZE	2048

/** Timeout of a receive operation in milliseconds. */
#define RECV_TIMEOUT	5000
/** Timeout of a send operation in milliseconds. */
#define SEND_TIMEOUT	5000

/** String of the port number for the J-Link TCP/IP protocol. */
#define PORT_STRING	"19020"

/** Size of the server's hello message in bytes. */
#define SERVER_HELLO_SIZE	4
/**
 * Maximum length of the server name including trailing null-terminator in
 * bytes.
 */
#define SERVER_NAME_MAX_LENGTH	256
/** @endcond */

static int initialize_handle(struct jaylink_device_handle *devh)
{
	struct jaylink_context *ctx;

	ctx = devh->dev->ctx;

	devh->buffer_size = BUFFER_SIZE;
	devh->buffer = malloc(devh->buffer_size);

	if (!devh->buffer) {
		log_err(ctx, "Transport buffer malloc failed");
		return JAYLINK_ERR_MALLOC;
	}

	devh->read_length = 0;
	devh->bytes_available = 0;
	devh->read_pos = 0;

	devh->write_length = 0;
	devh->write_pos = 0;

	return JAYLINK_OK;
}

static void cleanup_handle(struct jaylink_device_handle *devh)
{
	free(devh->buffer);
}

static int _recv(struct jaylink_device_handle *devh, uint8_t *buffer,
		size_t length)
{
	struct jaylink_context *ctx;
	size_t tmp;

	ctx = devh->dev->ctx;

	while (length > 0) {
		tmp = length;

		if (!socket_recv(devh->sock, buffer, &tmp, 0)) {
			log_err(ctx, "Failed to receive data from device");
			return JAYLINK_ERR_IO;
		} else if (!tmp) {
			log_err(ctx, "Failed to receive data from device: "
				"remote connection closed");
			return JAYLINK_ERR_IO;
		}

		buffer += tmp;
		length -= tmp;

		log_dbgio(ctx, "Received %zu bytes from device", tmp);
	}

	return JAYLINK_OK;
}

static int handle_server_hello(struct jaylink_device_handle *devh)
{
	int ret;
	struct jaylink_context *ctx;
	uint8_t buf[SERVER_HELLO_SIZE];
	char name[SERVER_NAME_MAX_LENGTH];
	uint16_t proto_version;
	size_t length;

	ctx = devh->dev->ctx;

	ret = _recv(devh, buf, sizeof(buf));

	if (ret != JAYLINK_OK) {
		log_err(ctx, "Failed to receive hello message");
		return ret;
	}

	if (buf[0] == RESP_MAX_CONNECTIONS) {
		log_err(ctx, "Maximum number of connections reached");
		return JAYLINK_ERR;
	}

	if (buf[0] != CMD_SERVER) {
		log_err(ctx, "Invalid hello message received");
		return JAYLINK_ERR_PROTO;
	}

	proto_version = buffer_get_u16(buf, 1);

	log_dbg(ctx, "Protocol version: 0x%04x", proto_version);

	length = buf[3];
	ret = _recv(devh, (uint8_t *)name, length);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "Failed to receive server name");
		return ret;
	}

	name[length] = '\0';

	log_dbg(ctx, "Server name: %s", name);

	return JAYLINK_OK;
}

static int set_socket_timeouts(struct jaylink_device_handle *devh)
{
	struct jaylink_context *ctx;

	ctx = devh->dev->ctx;
#ifdef _WIN32
	DWORD timeout;

	timeout = RECV_TIMEOUT;

	if (!socket_set_option(devh->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
			sizeof(timeout))) {
		log_err(ctx, "Failed to set socket receive timeout");
		return JAYLINK_ERR;
	}

	timeout = SEND_TIMEOUT;

	if (!socket_set_option(devh->sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
			sizeof(timeout))) {
		log_err(ctx, "Failed to set socket send timeout");
		return JAYLINK_ERR;
	}
#else
	struct timeval timeout;

	timeout.tv_sec = RECV_TIMEOUT / 1000;
	timeout.tv_usec = (RECV_TIMEOUT % 1000) * 1000;

	if (!socket_set_option(devh->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
			sizeof(struct timeval))) {
		log_err(ctx, "Failed to set socket receive timeout");
		return JAYLINK_ERR;
	}

	timeout.tv_sec = SEND_TIMEOUT / 1000;
	timeout.tv_usec = (SEND_TIMEOUT % 1000) * 1000;

	if (!socket_set_option(devh->sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
			sizeof(struct timeval))) {
		log_err(ctx, "Failed to set socket send timeout");
		return JAYLINK_ERR;
	}
#endif
	return JAYLINK_OK;
}

JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh)
{
	int ret;
	struct jaylink_context *ctx;
	struct jaylink_device *dev;
	struct addrinfo hints;
	struct addrinfo *info;
	int sock;

	dev = devh->dev;
	ctx = dev->ctx;

	log_dbg(ctx, "Trying to open device (IPv4 address = %s)",
		dev->ipv4_address);

	ret = initialize_handle(devh);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "Initialize device handle failed");
		return ret;
	}

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	ret = getaddrinfo(dev->ipv4_address, PORT_STRING, &hints, &info);

	if (ret != 0) {
		log_err(ctx, "Address lookup failed");
		cleanup_handle(devh);
		return JAYLINK_ERR;
	}

	sock = -1;

	for (struct addrinfo *rp = info; rp != NULL; rp = rp->ai_next) {
		sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);

		if (sock < 0)
			continue;

		if (!connect(sock, info->ai_addr, info->ai_addrlen))
			break;

		socket_close(sock);
		sock = -1;
	}

	freeaddrinfo(info);

	if (sock < 0) {
		log_err(ctx, "Failed to open device");
		cleanup_handle(devh);
		return JAYLINK_ERR;
	}

	log_dbg(ctx, "Device opened successfully");

	devh->sock = sock;
	ret = set_socket_timeouts(devh);

	if (ret != JAYLINK_OK) {
		socket_close(sock);
		cleanup_handle(devh);
		return ret;
	}

	ret = handle_server_hello(devh);

	if (ret != JAYLINK_OK) {
		socket_close(sock);
		cleanup_handle(devh);
		return ret;
	}

	return JAYLINK_OK;
}

JAYLINK_PRIV int transport_tcp_close(struct jaylink_device_handle *devh)
{
	struct jaylink_context *ctx;

	ctx = devh->dev->ctx;

	log_dbg(ctx, "Closing device (IPv4 address = %s)",
		devh->dev->ipv4_address);

	cleanup_handle(devh);

	log_dbg(ctx, "Device closed successfully");

	return JAYLINK_OK;
}

JAYLINK_PRIV int transport_tcp_start_write(struct jaylink_device_handle *devh,
		size_t length, bool has_command)
{
	struct jaylink_context *ctx;

	if (!length)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;

	log_dbgio(ctx, "Starting write operation (length = %zu bytes)",
		length);

	if (devh->write_pos > 0)
		log_warn(ctx, "Last write operation left %zu bytes in the "
			"buffer", devh->write_pos);

	if (devh->write_length > 0)
		log_warn(ctx, "Last write operation was not performed");

	devh->write_length = length;
	devh->write_pos = 0;

	if (has_command) {
		devh->buffer[0] = CMD_CLIENT;
		devh->write_pos++;
	}

	return JAYLINK_OK;
}

JAYLINK_PRIV int transport_tcp_start_read(struct jaylink_device_handle *devh,
		size_t length)
{
	struct jaylink_context *ctx;

	if (!length)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;

	log_dbgio(ctx, "Starting read operation (length = %zu bytes)",
		length);

	if (devh->bytes_available > 0)
		log_dbg(ctx, "Last read operation left %zu bytes in the "
			"buffer", devh->bytes_available);

	if (devh->read_length > 0)
		log_warn(ctx, "Last read operation left %zu bytes",
			devh->read_length);

	devh->read_length = length;

	return JAYLINK_OK;
}

JAYLINK_PRIV int transport_tcp_start_write_read(
		struct jaylink_device_handle *devh, size_t write_length,
		size_t read_length, bool has_command)
{
	struct jaylink_context *ctx;

	if (!read_length || !write_length)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;

	log_dbgio(ctx, "Starting write / read operation (length = "
		"%zu / %zu bytes)", write_length, read_length);

	if (devh->write_pos > 0)
		log_warn(ctx, "Last write operation left %zu bytes in the "
			"buffer", devh->write_pos);

	if (devh->write_length > 0)
		log_warn(ctx, "Last write operation was not performed");

	if (devh->bytes_available > 0)
		log_warn(ctx, "Last read operation left %zu bytes in the "
			"buffer", devh->bytes_available);

	if (devh->read_length > 0)
		log_warn(ctx, "Last read operation left %zu bytes",
			devh->read_length);

	devh->write_length = write_length;
	devh->write_pos = 0;

	if (has_command) {
		devh->buffer[0] = CMD_CLIENT;
		devh->write_pos++;
	}

	devh->read_length = read_length;
	devh->bytes_available = 0;
	devh->read_pos = 0;

	return JAYLINK_OK;
}

static int _send(struct jaylink_device_handle *devh, const uint8_t *buffer,
		size_t length)
{
	struct jaylink_context *ctx;
	size_t tmp;

	ctx = devh->dev->ctx;

	while (length > 0) {
		tmp = length;

		if (!socket_send(devh->sock, buffer, &tmp, 0)) {
			log_err(ctx, "Failed to send data to device");
			return JAYLINK_ERR_IO;
		}

		buffer += tmp;
		length -= tmp;

		log_dbgio(ctx, "Sent %zu bytes to device", tmp);
	}

	return JAYLINK_OK;
}

static bool adjust_buffer(struct jaylink_device_handle *devh, size_t size)
{
	struct jaylink_context *ctx;
	uint8_t *buffer;
	size_t num;

	ctx = devh->dev->ctx;

	/* Adjust buffer size to a multiple of BUFFER_SIZE bytes. */
	num = size / BUFFER_SIZE;

	if (size % BUFFER_SIZE > 0)
		num++;

	size = num * BUFFER_SIZE;
	buffer = realloc(devh->buffer, size);

	if (!buffer) {
		log_err(ctx, "Failed to adjust buffer size to %zu bytes",
			size);
		return false;
	}

	devh->buffer = buffer;
	devh->buffer_size = size;

	log_dbg(ctx, "Adjusted buffer size to %zu bytes", size);

	return true;
}

JAYLINK_PRIV int transport_tcp_write(struct jaylink_device_handle *devh,
		const uint8_t *buffer, size_t length)
{
	int ret;
	struct jaylink_context *ctx;
	size_t tmp;

	ctx = devh->dev->ctx;

	if (length > devh->write_length) {
		log_err(ctx, "Requested to write %zu bytes but only %zu bytes "
			"are expected for the write operation", length,
			devh->write_length);
		return JAYLINK_ERR_ARG;
	}

	/*
	 * Store data in the buffer if the expected number of bytes for the
	 * write operation is not reached.
	 */
	if (length < devh->write_length) {
		if (devh->write_pos + length > devh->buffer_size) {
			if (!adjust_buffer(devh, devh->write_pos + length))
				return JAYLINK_ERR_MALLOC;
		}

		memcpy(devh->buffer + devh->write_pos, buffer, length);

		devh->write_length -= length;
		devh->write_pos += length;

		log_dbgio(ctx, "Wrote %zu bytes into buffer", length);
		return JAYLINK_OK;
	}

	/*
	 * Expected number of bytes for this write operation is reached and
	 * therefore the write operation will be performed.
	 */
	devh->write_length = 0;

	/* Send data directly to the device if the buffer is empty. */
	if (!devh->write_pos)
		return _send(devh, buffer, length);

	tmp = MIN(length, devh->buffer_size - devh->write_pos);

	/*
	 * Fill up the internal buffer in order to reduce the number of
	 * messages sent to the device for performance reasons.
	 */
	memcpy(devh->buffer + devh->write_pos, buffer, tmp);

	length -= tmp;
	buffer += tmp;

	log_dbgio(ctx, "Buffer filled up with %zu bytes", tmp);

	ret = _send(devh, devh->buffer, devh->write_pos + tmp);

	devh->write_pos = 0;

	if (ret != JAYLINK_OK)
		return ret;

	if (!length)
		return JAYLINK_OK;

	return _send(devh, buffer, length);
}

JAYLINK_PRIV int transport_tcp_read(struct jaylink_device_handle *devh,
		uint8_t *buffer, size_t length)
{
	int ret;
	struct jaylink_context *ctx;

	ctx = devh->dev->ctx;

	if (length > devh->read_length) {
		log_err(ctx, "Requested to read %zu bytes but only %zu bytes "
			"are expected for the read operation", length,
			devh->read_length);
		return JAYLINK_ERR_ARG;
	}

	if (length <= devh->bytes_available) {
		memcpy(buffer, devh->buffer + devh->read_pos, length);

		devh->read_length -= length;
		devh->bytes_available -= length;
		devh->read_pos += length;

		log_dbgio(ctx, "Read %zu bytes from buffer", length);
		return JAYLINK_OK;
	}

	if (devh->bytes_available) {
		memcpy(buffer, devh->buffer + devh->read_pos,
			devh->bytes_available);

		buffer += devh->bytes_available;
		length -= devh->bytes_available;
		devh->read_length -= devh->bytes_available;

		log_dbgio(ctx, "Read %zu bytes from buffer to flush it",
			devh->bytes_available);

		devh->bytes_available = 0;
		devh->read_pos = 0;
	}

	ret = _recv(devh, buffer, length);

	if (ret != JAYLINK_OK)
		return ret;

	devh->read_length -= length;

	return JAYLINK_OK;
}