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
33
34
35
36
37
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
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
163
164
165
166
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
189
190
191
192
196
197
198
199
200
201
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
249
250
251
252
259
260
261
262
263
264
265
271
272
273
274
275
276
277
278
279
280
281
282
283
289
290
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
323
324
328
329
330
331
332
333
338
339
340
341
342
346
347
348
349
350
/* ... */
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* ... */#endif
#include "libjaylink.h"
#include "libjaylink-internal.h"
/* ... */
#define ADV_MESSAGE_SIZE 128
#define DISC_PORT 19020
#define DISC_MESSAGE_SIZE 64
#define DISC_TIMEOUT 20
static bool compare_devices(const void *a, const void *b)
{
const struct jaylink_device *dev;
const struct jaylink_device *new_dev;
dev = a;
new_dev = b;
if (dev->iface != JAYLINK_HIF_TCP)
return false;
if (memcmp(dev->ipv4_address, new_dev->ipv4_address,
sizeof(dev->ipv4_address)) != 0)
return false;
if (dev->serial_number != new_dev->serial_number)
return false;
if (memcmp(dev->mac_address, new_dev->mac_address,
sizeof(dev->mac_address)) != 0)
return false;
if (strcmp(dev->product_name, new_dev->product_name) != 0)
return false;
if (strcmp(dev->nickname, new_dev->nickname) != 0)
return false;
if (dev->hw_version.type != new_dev->hw_version.type)
return false;
if (dev->hw_version.major != new_dev->hw_version.major)
return false;
if (dev->hw_version.minor != new_dev->hw_version.minor)
return false;
if (dev->hw_version.revision != new_dev->hw_version.revision)
return false;
return true;
}{ ... }
static struct jaylink_device *find_device(struct list *list,
const struct jaylink_device *dev)
{
struct list *item;
item = list_find_custom(list, &compare_devices, dev);
if (item)
return item->data;
return NULL;
}{ ... }
static bool parse_adv_message(struct jaylink_device *dev,
const uint8_t *buffer)
{
struct in_addr in;
uint32_t tmp;
if (memcmp(buffer, "Found", 5) != 0)
return false;
/* ... */
memcpy(&in, buffer + 16, 4);
memcpy(dev->ipv4_address, inet_ntoa(in), sizeof(dev->ipv4_address));
memcpy(dev->mac_address, buffer + 32, sizeof(dev->mac_address));
dev->has_mac_address = true;
dev->serial_number = buffer_get_u32(buffer, 48);
dev->has_serial_number = true;
tmp = buffer_get_u32(buffer, 52);
dev->hw_version.type = (tmp / 1000000) % 100;
dev->hw_version.major = (tmp / 10000) % 100;
dev->hw_version.minor = (tmp / 100) % 100;
dev->hw_version.revision = tmp % 100;
dev->has_hw_version = true;
memcpy(dev->product_name, buffer + 64, sizeof(dev->product_name));
dev->product_name[JAYLINK_PRODUCT_NAME_MAX_LENGTH - 1] = '\0';
dev->has_product_name = isprint((unsigned char)dev->product_name[0]);
memcpy(dev->nickname, buffer + 96, sizeof(dev->nickname));
dev->nickname[JAYLINK_NICKNAME_MAX_LENGTH - 1] = '\0';
dev->has_nickname = isprint((unsigned char)dev->nickname[0]);
return true;
}{ ... }
static struct jaylink_device *probe_device(struct jaylink_context *ctx,
struct sockaddr_in *addr, const uint8_t *buffer)
{
struct jaylink_device tmp;
struct jaylink_device *dev;
/* ... */
log_dbg(ctx, "Received advertisement message (IPv4 address = %s)",
inet_ntoa(addr->sin_addr));
if (!parse_adv_message(&tmp, buffer)) {
log_dbg(ctx, "Received invalid advertisement message");
return NULL;
}if (!parse_adv_message(&tmp, buffer)) { ... }
log_dbg(ctx, "Found device (IPv4 address = %s)", tmp.ipv4_address);
log_dbg(ctx, "Device: MAC address = %02x:%02x:%02x:%02x:%02x:%02x",
tmp.mac_address[0], tmp.mac_address[1], tmp.mac_address[2],
tmp.mac_address[3], tmp.mac_address[4], tmp.mac_address[5]);
log_dbg(ctx, "Device: Serial number = %u", tmp.serial_number);
if (tmp.has_product_name)
log_dbg(ctx, "Device: Product = %s", tmp.product_name);
if (tmp.has_nickname)
log_dbg(ctx, "Device: Nickname = %s", tmp.nickname);
dev = find_device(ctx->discovered_devs, &tmp);
if (dev) {
log_dbg(ctx, "Ignoring already discovered device");
return NULL;
}if (dev) { ... }
dev = find_device(ctx->devs, &tmp);
if (dev) {
log_dbg(ctx, "Using existing device instance");
return jaylink_ref_device(dev);
}if (dev) { ... }
log_dbg(ctx, "Allocating new device instance");
dev = device_allocate(ctx);
if (!dev) {
log_warn(ctx, "Device instance malloc failed");
return NULL;
}if (!dev) { ... }
dev->iface = JAYLINK_HIF_TCP;
dev->serial_number = tmp.serial_number;
dev->has_serial_number = tmp.has_serial_number;
memcpy(dev->ipv4_address, tmp.ipv4_address, sizeof(dev->ipv4_address));
memcpy(dev->mac_address, tmp.mac_address, sizeof(dev->mac_address));
dev->has_mac_address = tmp.has_mac_address;
memcpy(dev->product_name, tmp.product_name, sizeof(dev->product_name));
dev->has_product_name = tmp.has_product_name;
memcpy(dev->nickname, tmp.nickname, sizeof(dev->nickname));
dev->has_nickname = tmp.has_nickname;
dev->hw_version = tmp.hw_version;
dev->has_hw_version = tmp.has_hw_version;
return dev;
}{ ... }
JAYLINK_PRIV int discovery_tcp_scan(struct jaylink_context *ctx)
{
int ret;
int sock;
int opt_value;
fd_set rfds;
struct sockaddr_in addr;
size_t addr_length;
struct timeval timeout;
uint8_t buf[ADV_MESSAGE_SIZE];
struct jaylink_device *dev;
size_t length;
size_t num_devs;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
log_err(ctx, "Failed to create discovery socket");
return JAYLINK_ERR;
}if (sock < 0) { ... }
opt_value = true;
if (!socket_set_option(sock, SOL_SOCKET, SO_BROADCAST, &opt_value,
sizeof(opt_value))) {
log_err(ctx, "Failed to enable broadcast option for discovery "
"socket");
socket_close(sock);
return JAYLINK_ERR;
}if (!socket_set_option(sock, SOL_SOCKET, SO_BROADCAST, &opt_value, sizeof(opt_value))) { ... }
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(DISC_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
if (!socket_bind(sock, (struct sockaddr *)&addr,
sizeof(struct sockaddr_in))) {
log_err(ctx, "Failed to bind discovery socket");
socket_close(sock);
return JAYLINK_ERR;
}if (!socket_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) { ... }
addr.sin_family = AF_INET;
addr.sin_port = htons(DISC_PORT);
addr.sin_addr.s_addr = INADDR_BROADCAST;
memset(buf, 0, DISC_MESSAGE_SIZE);
memcpy(buf, "Discover", 8);
log_dbg(ctx, "Sending discovery message");
length = DISC_MESSAGE_SIZE;
if (!socket_sendto(sock, (char *)buf, &length, 0,
(const struct sockaddr *)&addr, sizeof(addr))) {
log_err(ctx, "Failed to send discovery message");
socket_close(sock);
return JAYLINK_ERR_IO;
}if (!socket_sendto(sock, (char *)buf, &length, 0, (const struct sockaddr *)&addr, sizeof(addr))) { ... }
if (length < DISC_MESSAGE_SIZE) {
log_err(ctx, "Only sent %zu bytes of discovery message",
length);
socket_close(sock);
return JAYLINK_ERR_IO;
}if (length < DISC_MESSAGE_SIZE) { ... }
timeout.tv_sec = DISC_TIMEOUT / 1000;
timeout.tv_usec = (DISC_TIMEOUT % 1000) * 1000;
num_devs = 0;
while (true) {
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
ret = select(sock + 1, &rfds, NULL, NULL, &timeout);
if (ret <= 0)
break;
if (!FD_ISSET(sock, &rfds))
continue;
length = ADV_MESSAGE_SIZE;
addr_length = sizeof(struct sockaddr_in);
if (!socket_recvfrom(sock, buf, &length, 0,
(struct sockaddr *)&addr, &addr_length)) {
log_warn(ctx, "Failed to receive advertisement "
"message");
continue;
}if (!socket_recvfrom(sock, buf, &length, 0, (struct sockaddr *)&addr, &addr_length)) { ... }
/* ... */
if (length != ADV_MESSAGE_SIZE)
continue;
dev = probe_device(ctx, &addr, buf);
if (dev) {
ctx->discovered_devs = list_prepend(
ctx->discovered_devs, dev);
num_devs++;
}if (dev) { ... }
}while (true) { ... }
socket_close(sock);
if (ret < 0) {
log_err(ctx, "select() failed");
return JAYLINK_ERR;
}if (ret < 0) { ... }
log_dbg(ctx, "Found %zu TCP/IP device(s)", num_devs);
return JAYLINK_OK;
}{ ... }