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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2006-2008 Nokia Corporation
* Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sdp.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <gdbus.h>
#include "logging.h"
#include "hcid.h"
#include "adapter.h"
#include "device.h"
#include "agent.h"
#define REQUEST_TIMEOUT (60 * 1000) /* 60 seconds */
typedef enum {
AGENT_REQUEST_PASSKEY,
AGENT_REQUEST_CONFIRMATION,
AGENT_REQUEST_PINCODE,
AGENT_REQUEST_AUTHORIZE,
AGENT_REQUEST_CONFIRM_MODE
} agent_request_type_t;
struct agent {
struct btd_adapter *adapter;
char *name;
char *path;
uint8_t capability;
struct agent_request *request;
int exited;
agent_remove_cb remove_cb;
void *remove_cb_data;
guint listener_id;
};
struct agent_request {
agent_request_type_t type;
struct agent *agent;
DBusMessage *msg;
DBusPendingCall *call;
void *cb;
void *user_data;
GDestroyNotify destroy;
};
static DBusConnection *connection = NULL;
static int request_fallback(struct agent_request *req,
DBusPendingCallNotifyFunction function);
static void agent_release(struct agent *agent)
{
DBusMessage *message;
debug("Releasing agent %s, %s", agent->name, agent->path);
if (agent->request)
agent_cancel(agent);
message = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "Release");
if (message == NULL) {
error("Couldn't allocate D-Bus message");
return;
}
g_dbus_send_message(connection, message);
}
static int send_cancel_request(struct agent_request *req)
{
DBusMessage *message;
message = dbus_message_new_method_call(req->agent->name, req->agent->path,
"org.bluez.Agent", "Cancel");
if (message == NULL) {
error("Couldn't allocate D-Bus message");
return -ENOMEM;
}
g_dbus_send_message(connection, message);
return 0;
}
static void agent_request_free(struct agent_request *req, gboolean destroy)
{
if (req->msg)
dbus_message_unref(req->msg);
if (req->call)
dbus_pending_call_unref(req->call);
if (req->agent && req->agent->request)
req->agent->request = NULL;
if (destroy && req->destroy)
req->destroy(req->user_data);
g_free(req);
}
static void agent_exited(DBusConnection *conn, void *user_data)
{
struct agent *agent = user_data;
debug("Agent exited without calling Unregister");
agent->exited = TRUE;
agent_free(agent);
}
void agent_free(struct agent *agent)
{
if (!agent)
return;
if (agent->remove_cb)
agent->remove_cb(agent, agent->remove_cb_data);
if (agent->request) {
DBusError err;
agent_pincode_cb pincode_cb;
agent_cb cb;
dbus_error_init(&err);
dbus_set_error_const(&err, "org.bluez.Error.Failed", "Canceled");
switch (agent->request->type) {
case AGENT_REQUEST_PINCODE:
pincode_cb = agent->request->cb;
pincode_cb(agent, &err, NULL, agent->request->user_data);
break;
default:
cb = agent->request->cb;
cb(agent, &err, agent->request->user_data);
}
dbus_error_free(&err);
agent_cancel(agent);
}
if (!agent->exited) {
g_dbus_remove_watch(connection, agent->listener_id);
agent_release(agent);
}
g_free(agent->name);
g_free(agent->path);
g_free(agent);
}
struct agent *agent_create(struct btd_adapter *adapter, const char *name,
const char *path, uint8_t capability,
agent_remove_cb cb, void *remove_cb_data)
{
struct agent *agent;
agent = g_new0(struct agent, 1);
agent->adapter = adapter;
agent->name = g_strdup(name);
agent->path = g_strdup(path);
agent->capability = capability;
agent->remove_cb = cb;
agent->remove_cb_data = remove_cb_data;
agent->listener_id = g_dbus_add_disconnect_watch(connection, name,
agent_exited, agent,
NULL);
return agent;
}
static struct agent_request *agent_request_new(struct agent *agent,
agent_request_type_t type,
void *cb,
void *user_data,
GDestroyNotify destroy)
{
struct agent_request *req;
req = g_new0(struct agent_request, 1);
req->agent = agent;
req->type = type;
req->cb = cb;
req->user_data = user_data;
req->destroy = destroy;
return req;
}
int agent_cancel(struct agent *agent)
{
if (!agent->request)
return -EINVAL;
if (agent->request->call)
dbus_pending_call_cancel(agent->request->call);
if (!agent->exited)
send_cancel_request(agent->request);
agent_request_free(agent->request, TRUE);
agent->request = NULL;
return 0;
}
static void simple_agent_reply(DBusPendingCall *call, void *user_data)
{
struct agent_request *req = user_data;
struct agent *agent = req->agent;
DBusMessage *message;
DBusError err;
agent_cb cb = req->cb;
/* steal_reply will always return non-NULL since the callback
* is only called after a reply has been received */
message = dbus_pending_call_steal_reply(call);
dbus_error_init(&err);
if (dbus_set_error_from_message(&err, message)) {
if ((g_str_equal(DBUS_ERROR_UNKNOWN_METHOD, err.name) ||
g_str_equal(DBUS_ERROR_NO_REPLY, err.name)) &&
request_fallback(req, simple_agent_reply) == 0) {
dbus_error_free(&err);
return;
}
error("Agent replied with an error: %s, %s",
err.name, err.message);
cb(agent, &err, req->user_data);
if (dbus_error_has_name(&err, DBUS_ERROR_NO_REPLY)) {
agent_cancel(agent);
dbus_message_unref(message);
dbus_error_free(&err);
return;
}
dbus_error_free(&err);
goto done;
}
dbus_error_init(&err);
if (!dbus_message_get_args(message, &err, DBUS_TYPE_INVALID)) {
error("Wrong reply signature: %s", err.message);
cb(agent, &err, req->user_data);
dbus_error_free(&err);
goto done;
}
cb(agent, NULL, req->user_data);
done:
dbus_message_unref(message);
agent->request = NULL;
agent_request_free(req, TRUE);
}
static int agent_call_authorize(struct agent_request *req,
const char *device_path,
const char *uuid)
{
struct agent *agent = req->agent;
req->msg = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "Authorize");
if (!req->msg) {
error("Couldn't allocate D-Bus message");
return -ENOMEM;
}
dbus_message_append_args(req->msg,
DBUS_TYPE_OBJECT_PATH, &device_path,
DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(connection, req->msg,
&req->call, REQUEST_TIMEOUT) == FALSE) {
error("D-Bus send failed");
return -EIO;
}
dbus_pending_call_set_notify(req->call, simple_agent_reply, req, NULL);
return 0;
}
int agent_authorize(struct agent *agent,
const char *path,
const char *uuid,
agent_cb cb,
void *user_data,
GDestroyNotify destroy)
{
struct agent_request *req;
int err;
if (agent->request)
return -EBUSY;
req = agent_request_new(agent, AGENT_REQUEST_AUTHORIZE, cb,
user_data, destroy);
err = agent_call_authorize(req, path, uuid);
if (err < 0) {
agent_request_free(req, FALSE);
return -ENOMEM;
}
agent->request = req;
debug("authorize request was sent for %s", path);
return 0;
}
static void pincode_reply(DBusPendingCall *call, void *user_data)
{
struct agent_request *req = user_data;
struct agent *agent = req->agent;
struct btd_adapter *adapter = agent->adapter;
agent_pincode_cb cb = req->cb;
DBusMessage *message;
DBusError err;
bdaddr_t sba;
size_t len;
char *pin;
adapter_get_address(adapter, &sba);
/* steal_reply will always return non-NULL since the callback
* is only called after a reply has been received */
message = dbus_pending_call_steal_reply(call);
dbus_error_init(&err);
if (dbus_set_error_from_message(&err, message)) {
if ((g_str_equal(DBUS_ERROR_UNKNOWN_METHOD, err.name) ||
g_str_equal(DBUS_ERROR_NO_REPLY, err.name)) &&
request_fallback(req, pincode_reply) == 0) {
dbus_error_free(&err);
return;
}
error("Agent replied with an error: %s, %s",
err.name, err.message);
cb(agent, &err, NULL, req->user_data);
dbus_error_free(&err);
goto done;
}
dbus_error_init(&err);
if (!dbus_message_get_args(message, &err,
DBUS_TYPE_STRING, &pin,
DBUS_TYPE_INVALID)) {
error("Wrong passkey reply signature: %s", err.message);
cb(agent, &err, NULL, req->user_data);
dbus_error_free(&err);
goto done;
}
len = strlen(pin);
dbus_error_init(&err);
if (len > 16 || len < 1) {
error("Invalid passkey length from handler");
dbus_set_error_const(&err, "org.bluez.Error.InvalidArgs",
"Invalid passkey length");
cb(agent, &err, NULL, req->user_data);
dbus_error_free(&err);
goto done;
}
set_pin_length(&sba, len);
cb(agent, NULL, pin, req->user_data);
done:
if (message)
dbus_message_unref(message);
dbus_pending_call_cancel(req->call);
agent->request = NULL;
agent_request_free(req, TRUE);
}
static int pincode_request_new(struct agent_request *req, const char *device_path,
dbus_bool_t numeric)
{
struct agent *agent = req->agent;
req->msg = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "RequestPinCode");
if (req->msg == NULL) {
error("Couldn't allocate D-Bus message");
return -ENOMEM;
}
dbus_message_append_args(req->msg, DBUS_TYPE_OBJECT_PATH, &device_path,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(connection, req->msg,
&req->call, REQUEST_TIMEOUT) == FALSE) {
error("D-Bus send failed");
return -EIO;
}
dbus_pending_call_set_notify(req->call, pincode_reply, req, NULL);
return 0;
}
int agent_request_pincode(struct agent *agent, struct btd_device *device,
agent_pincode_cb cb, void *user_data,
GDestroyNotify destroy)
{
struct agent_request *req;
const gchar *dev_path = device_get_path(device);
int err;
if (agent->request)
return -EBUSY;
req = agent_request_new(agent, AGENT_REQUEST_PINCODE, cb,
user_data, destroy);
err = pincode_request_new(req, dev_path, FALSE);
if (err < 0)
goto failed;
agent->request = req;
return 0;
failed:
g_free(req);
return err;
}
static int confirm_mode_change_request_new(struct agent_request *req,
const char *mode)
{
struct agent *agent = req->agent;
req->msg = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "ConfirmModeChange");
if (req->msg == NULL) {
error("Couldn't allocate D-Bus message");
return -ENOMEM;
}
dbus_message_append_args(req->msg,
DBUS_TYPE_STRING, &mode,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(connection, req->msg,
&req->call, REQUEST_TIMEOUT) == FALSE) {
error("D-Bus send failed");
return -EIO;
}
dbus_pending_call_set_notify(req->call, simple_agent_reply, req, NULL);
return 0;
}
int agent_confirm_mode_change(struct agent *agent, const char *new_mode,
agent_cb cb, void *user_data,
GDestroyNotify destroy)
{
struct agent_request *req;
int err;
if (agent->request)
return -EBUSY;
debug("Calling Agent.ConfirmModeChange: name=%s, path=%s, mode=%s",
agent->name, agent->path, new_mode);
req = agent_request_new(agent, AGENT_REQUEST_CONFIRM_MODE,
cb, user_data, destroy);
err = confirm_mode_change_request_new(req, new_mode);
if (err < 0)
goto failed;
agent->request = req;
return 0;
failed:
agent_request_free(req, FALSE);
return err;
}
static void passkey_reply(DBusPendingCall *call, void *user_data)
{
struct agent_request *req = user_data;
struct agent *agent = req->agent;
agent_passkey_cb cb = req->cb;
DBusMessage *message;
DBusError err;
uint32_t passkey;
/* steal_reply will always return non-NULL since the callback
* is only called after a reply has been received */
message = dbus_pending_call_steal_reply(call);
dbus_error_init(&err);
if (dbus_set_error_from_message(&err, message)) {
if ((g_str_equal(DBUS_ERROR_UNKNOWN_METHOD, err.name) ||
g_str_equal(DBUS_ERROR_NO_REPLY, err.name)) &&
request_fallback(req, passkey_reply) == 0) {
dbus_error_free(&err);
return;
}
error("Agent replied with an error: %s, %s",
err.name, err.message);
cb(agent, &err, 0, req->user_data);
dbus_error_free(&err);
goto done;
}
dbus_error_init(&err);
if (!dbus_message_get_args(message, &err,
DBUS_TYPE_UINT32, &passkey,
DBUS_TYPE_INVALID)) {
error("Wrong passkey reply signature: %s", err.message);
cb(agent, &err, 0, req->user_data);
dbus_error_free(&err);
goto done;
}
cb(agent, NULL, passkey, req->user_data);
done:
if (message)
dbus_message_unref(message);
dbus_pending_call_cancel(req->call);
agent->request = NULL;
agent_request_free(req, TRUE);
}
static int passkey_request_new(struct agent_request *req,
const char *device_path)
{
struct agent *agent = req->agent;
req->msg = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "RequestPasskey");
if (req->msg == NULL) {
error("Couldn't allocate D-Bus message");
return -ENOMEM;
}
dbus_message_append_args(req->msg, DBUS_TYPE_OBJECT_PATH, &device_path,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(connection, req->msg,
&req->call, REQUEST_TIMEOUT) == FALSE) {
error("D-Bus send failed");
return -EIO;
}
dbus_pending_call_set_notify(req->call, passkey_reply, req, NULL);
return 0;
}
int agent_request_passkey(struct agent *agent, struct btd_device *device,
agent_passkey_cb cb, void *user_data,
GDestroyNotify destroy)
{
struct agent_request *req;
const gchar *dev_path = device_get_path(device);
int err;
if (agent->request)
return -EBUSY;
debug("Calling Agent.RequestPasskey: name=%s, path=%s",
agent->name, agent->path);
req = agent_request_new(agent, AGENT_REQUEST_PASSKEY, cb,
user_data, destroy);
err = passkey_request_new(req, dev_path);
if (err < 0)
goto failed;
agent->request = req;
return 0;
failed:
agent_request_free(req, FALSE);
return err;
}
static int confirmation_request_new(struct agent_request *req,
const char *device_path,
uint32_t passkey)
{
struct agent *agent = req->agent;
req->msg = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "RequestConfirmation");
if (req->msg == NULL) {
error("Couldn't allocate D-Bus message");
return -ENOMEM;
}
dbus_message_append_args(req->msg,
DBUS_TYPE_OBJECT_PATH, &device_path,
DBUS_TYPE_UINT32, &passkey,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(connection, req->msg,
&req->call, REQUEST_TIMEOUT) == FALSE) {
error("D-Bus send failed");
return -EIO;
}
dbus_pending_call_set_notify(req->call, simple_agent_reply, req, NULL);
return 0;
}
int agent_request_confirmation(struct agent *agent, struct btd_device *device,
uint32_t passkey, agent_cb cb,
void *user_data, GDestroyNotify destroy)
{
struct agent_request *req;
const gchar *dev_path = device_get_path(device);
int err;
if (agent->request)
return -EBUSY;
debug("Calling Agent.RequestConfirmation: name=%s, path=%s, passkey=%06u",
agent->name, agent->path, passkey);
req = agent_request_new(agent, AGENT_REQUEST_CONFIRMATION, cb,
user_data, destroy);
err = confirmation_request_new(req, dev_path, passkey);
if (err < 0)
goto failed;
agent->request = req;
return 0;
failed:
agent_request_free(req, FALSE);
return err;
}
static int request_fallback(struct agent_request *req,
DBusPendingCallNotifyFunction function)
{
struct btd_adapter *adapter = req->agent->adapter;
struct agent *adapter_agent = adapter_get_agent(adapter);
DBusMessage *msg;
if (req->agent == adapter_agent || adapter_agent == NULL)
return -EINVAL;
dbus_pending_call_cancel(req->call);
msg = dbus_message_copy(req->msg);
dbus_message_set_destination(msg, adapter_agent->name);
dbus_message_set_path(msg, adapter_agent->path);
if (dbus_connection_send_with_reply(connection, msg,
&req->call, REQUEST_TIMEOUT) == FALSE) {
error("D-Bus send failed");
dbus_message_unref(msg);
return -EIO;
}
req->agent->request = NULL;
req->agent = adapter_agent;
req->agent->request = req;
dbus_message_unref(req->msg);
req->msg = msg;
dbus_pending_call_set_notify(req->call, function, req, NULL);
return 0;
}
int agent_display_passkey(struct agent *agent, struct btd_device *device,
uint32_t passkey)
{
DBusMessage *message;
const gchar *dev_path = device_get_path(device);
message = dbus_message_new_method_call(agent->name, agent->path,
"org.bluez.Agent", "DisplayPasskey");
if (!message) {
error("Couldn't allocate D-Bus message");
return -1;
}
dbus_message_append_args(message,
DBUS_TYPE_OBJECT_PATH, &dev_path,
DBUS_TYPE_UINT32, &passkey,
DBUS_TYPE_INVALID);
if (!g_dbus_send_message(connection, message)) {
error("D-Bus send failed");
dbus_message_unref(message);
return -1;
}
return 0;
}
uint8_t agent_get_io_capability(struct agent *agent)
{
return agent->capability;
}
gboolean agent_matches(struct agent *agent, const char *name, const char *path)
{
if (g_str_equal(agent->name, name) && g_str_equal(agent->path, path))
return TRUE;
return FALSE;
}
gboolean agent_is_busy(struct agent *agent, void *user_data)
{
if (!agent->request)
return FALSE;
if (user_data && user_data != agent->request->user_data)
return FALSE;
return TRUE;
}
void agent_exit(void)
{
dbus_connection_unref(connection);
connection = NULL;
}
void agent_init(void)
{
connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
}
|