-
Notifications
You must be signed in to change notification settings - Fork 16
/
wmemulator.c
503 lines (428 loc) · 8.73 KB
/
wmemulator.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include <sys/time.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
#include <poll.h>
#include <pthread.h>
#include "sdp.h"
#include "wiimote.h"
#include "input.h"
#include "input_sdl.h"
#include "input_socket.h"
#include "adapter.h"
#include "wm_print.h"
#define PSM_SDP 1
#define PSM_CTRL 0x11
#define PSM_INT 0x13
bdaddr_t host_bdaddr;
int has_host = 0;
int sdp_fd, ctrl_fd, int_fd;
int sock_sdp_fd, sock_ctrl_fd, sock_int_fd;
static int is_connected = 0;
//signal handler to break out of main loop
static int running = 1;
void sig_handler(int sig)
{
running = 0;
}
int create_socket()
{
int fd;
struct linger l = { .l_onoff = 1, .l_linger = 5 };
int opt = 0;
fd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if (fd < 0)
{
return -1;
}
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
{
close(fd);
return -1;
}
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0)
{
close(fd);
return -1;
}
if (setsockopt(fd, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0)
{
close(fd);
return -1;
}
return fd;
}
int l2cap_connect(bdaddr_t bdaddr, int psm)
{
int fd;
struct sockaddr_l2 addr;
fd = create_socket();
if (fd < 0)
{
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
addr.l2_psm = htobs(psm);
addr.l2_bdaddr = bdaddr;
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
close(fd);
return -1;
}
return fd;
}
int l2cap_listen(int psm)
{
int fd;
struct sockaddr_l2 addr;
fd = create_socket();
if (fd < 0)
{
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
addr.l2_psm = htobs(psm);
addr.l2_bdaddr = *BDADDR_ANY;
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
close(fd);
return -1;
}
if (listen(fd, 1) < 0)
{
close(fd);
return -1;
}
return fd;
}
int listen_for_connections()
{
#ifdef SDP_SERVER
sock_sdp_fd = l2cap_listen(PSM_SDP);
if (sock_sdp_fd < 0)
{
printf("can't listen on psm %d: %s\n", PSM_SDP, strerror(errno));
return -1;
}
#endif
sock_ctrl_fd = l2cap_listen(PSM_CTRL);
if (sock_ctrl_fd < 0)
{
printf("can't listen on psm %d: %s\n", PSM_CTRL, strerror(errno));
return -1;
}
sock_int_fd = l2cap_listen(PSM_INT);
if (sock_int_fd < 0)
{
printf("can't listen on psm %d: %s\n", PSM_INT, strerror(errno));
return -1;
}
return 0;
}
int accept_connection(int socket_fd, bdaddr_t * bdaddr)
{
int fd;
struct sockaddr_l2 addr;
socklen_t opt = sizeof(addr);
fd = accept(socket_fd, (struct sockaddr *)&addr, &opt);
if (fd < 0)
{
return -1;
}
if (bdaddr != NULL)
{
*bdaddr = addr.l2_bdaddr;
}
return fd;
}
int connect_to_host()
{
ctrl_fd = l2cap_connect(host_bdaddr, PSM_CTRL);
if (ctrl_fd < 0)
{
printf("can't connect to host psm %d: %s\n", PSM_CTRL, strerror(errno));
return -1;
}
int_fd = l2cap_connect(host_bdaddr, PSM_INT);
if (int_fd < 0)
{
printf("can't connect to host psm %d: %s\n", PSM_INT, strerror(errno));
return -1;
}
return 0;
}
void disconnect()
{
shutdown(sdp_fd, SHUT_RDWR);
shutdown(ctrl_fd, SHUT_RDWR);
shutdown(int_fd, SHUT_RDWR);
close(sdp_fd);
close(ctrl_fd);
close(int_fd);
sdp_fd = 0;
ctrl_fd = 0;
int_fd = 0;
}
void print_usage(char *argv0)
{
printf("usage: %s [ <wii-bdaddr> [ gui | unix <path> | ip <port> ] ]\n", argv0);
}
int main(int argc, char *argv[])
{
struct input_source input_source;
struct pollfd pfd[6];
unsigned char buf[256];
ssize_t len;
struct wiimote_state state;
int send_report_now = 1;
int input_result;
int failure = 0;
if (argc > 1)
{
if (strcmp(argv[1], "pair") == 0)
{
// Act as if nothing given.
}
else if (bachk(argv[1]) >= 0)
{
str2ba(argv[1], &host_bdaddr);
has_host = 1;
}
else
{
print_usage(*argv);
return 1;
}
}
if (argc <= 2 || strcmp(argv[2], "gui") == 0)
{
input_sdl_init();
input_source = input_source_sdl;
}
else if (argc > 3 && strcmp(argv[2], "unix") == 0)
{
input_socket_init_unix_at_path(argv[3]);
input_source = input_source_socket;
}
else if (argc > 3 && strcmp(argv[3], "ip") == 0)
{
input_socket_init_ip_on_port(argv[3]);
input_source = input_source_socket;
}
else
{
print_usage(*argv);
return 1;
}
//set up unload signals
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGHUP, sig_handler);
if (set_up_device(NULL) < 0)
{
printf("failed to set up Bluetooth device\n");
return 1;
}
#ifndef SDP_SERVER
if (register_wiimote_sdp_record() < 0)
{
printf("failed to add Wiimote SDP record\n");
restore_device();
return 1;
}
#endif
wiimote_init(&state);
if (has_host)
{
printf("connecting to host...\n");
if (connect_to_host() < 0)
{
printf("couldn't connect\n");
running = 0;
}
else
{
char straddr[18];
ba2str(&host_bdaddr, straddr);
printf("connected to %s\n", straddr);
is_connected = 1;
}
}
else
{
if (listen_for_connections() < 0)
{
printf("couldn't listen\n");
running = 0;
}
else
{
printf("listening for connections... (press wii's sync button)\n");
}
}
while (running)
{
memset(&pfd, 0, sizeof(pfd));
pfd[0].fd = sock_sdp_fd;
pfd[1].fd = sock_ctrl_fd;
pfd[2].fd = sock_int_fd;
pfd[3].fd = sdp_fd;
pfd[4].fd = ctrl_fd;
pfd[5].fd = int_fd;
if (!is_connected)
{
pfd[0].events = POLLIN;
pfd[1].events = POLLIN;
pfd[2].events = POLLIN;
pfd[3].events = POLLIN | POLLOUT;
}
else
{
pfd[4].events = POLLIN;
pfd[5].events = POLLIN;
pfd[5].events |= POLLOUT;
}
if (poll(pfd, 6, 20) < 0)
{
printf("poll error\n");
break;
}
if (pfd[4].revents & POLLERR)
{
printf("error on ctrl psm\n");
break;
}
if (pfd[5].revents & POLLERR)
{
printf("error on data psm\n");
break;
}
if (pfd[0].revents & POLLIN)
{
sdp_fd = accept_connection(pfd[0].fd, NULL);
if (sdp_fd < 0)
{
printf("error accepting sdp connection\n");
break;
}
}
if (pfd[1].revents & POLLIN)
{
ctrl_fd = accept_connection(pfd[1].fd, NULL);
if (ctrl_fd < 0)
{
printf("error accepting ctrl connection\n");
break;
}
}
if (pfd[2].revents & POLLIN)
{
int_fd = accept_connection(pfd[2].fd, &host_bdaddr);
if (int_fd < 0)
{
printf("error accepting int connection\n");
break;
}
char straddr[18];
ba2str(&host_bdaddr, straddr);
printf("connected to %s\n", straddr);
is_connected = 1;
has_host = 1;
}
if (pfd[3].revents & POLLIN)
{
len = recv(sdp_fd, buf, 32, MSG_DONTWAIT);
if (len > 0)
{
sdp_recv_data(buf, len);
}
}
if (pfd[3].revents & POLLOUT)
{
len = sdp_get_data(buf);
if (len > 0)
{
send(sdp_fd, buf, len, MSG_DONTWAIT);
}
}
if (pfd[5].revents & POLLIN)
{
len = recv(int_fd, buf, 32, MSG_DONTWAIT);
if (len > 0)
{
print_report(buf, len);
process_report(&state, buf, len);
}
}
input_result = input_update(&state, &input_source);
if (input_result)
{
running = 0;
if (input_result == -2)
{
power_off_host(&host_bdaddr);
}
else
{
graceful_disconnect(&host_bdaddr);
}
}
if (is_connected && send_report_now)
{
if (pfd[5].revents & POLLOUT)
{
len = generate_report(&state, buf);
if (len > 0)
{
print_report(buf, len);
send(int_fd, buf, len, MSG_DONTWAIT);
}
failure = 0;
}
else
{
if (++failure > 5)
{
printf("connection timed out, attemping to reconnect...\n");
disconnect();
is_connected = 0;
}
}
}
if (has_host && !is_connected)
{
if (connect_to_host() < 0)
{
usleep(500*1000);
}
else
{
printf("connected to host\n");
is_connected = 1;
}
}
}
printf("cleaning up...\n");
disconnect();
close(sock_sdp_fd);
close(sock_ctrl_fd);
close(sock_int_fd);
restore_device();
#ifndef SDP_SERVER
unregister_wiimote_sdp_record();
#endif
wiimote_destroy(&state);
input_source.unload();
return 0;
}