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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
|
/*
*
* Connection Manager
*
* Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 <errno.h>
#include <stdlib.h>
#include <gdbus.h>
#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/device.h>
#include <connman/inet.h>
#include <connman/dbus.h>
#include <connman/log.h>
#define BLUEZ_SERVICE "org.bluez"
#define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager"
#define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter"
#define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device"
#define BLUEZ_NETWORK_INTERFACE BLUEZ_SERVICE ".Network"
#define LIST_ADAPTERS "ListAdapters"
#define ADAPTER_ADDED "AdapterAdded"
#define ADAPTER_REMOVED "AdapterRemoved"
#define PROPERTY_CHANGED "PropertyChanged"
#define GET_PROPERTIES "GetProperties"
#define SET_PROPERTY "SetProperty"
#define CONNECT "Connect"
#define DISCONNECT "Disconnect"
#define TIMEOUT 5000
typedef void (* properties_callback_t) (DBusConnection *connection,
const char *path,
DBusMessage *message,
void *user_data);
struct properties_data {
DBusConnection *connection;
DBusMessage *message;
properties_callback_t callback;
void *user_data;
};
static void get_properties_reply(DBusPendingCall *call, void *user_data)
{
struct properties_data *data = user_data;
DBusMessage *reply;
const char *path;
reply = dbus_pending_call_steal_reply(call);
if (reply == NULL)
goto done;
path = dbus_message_get_path(data->message);
data->callback(data->connection, path, reply, data->user_data);
dbus_message_unref(reply);
done:
dbus_message_unref(data->message);
g_free(data);
}
static void get_properties(DBusConnection *connection,
const char *path, const char *interface,
properties_callback_t callback, void *user_data)
{
struct properties_data *data;
DBusMessage *message;
DBusPendingCall *call;
DBG("path %s interface %s", path, interface);
data = g_try_new0(struct properties_data, 1);
if (data == NULL)
return;
message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
interface, GET_PROPERTIES);
if (message == NULL) {
g_free(data);
return;
}
if (dbus_connection_send_with_reply(connection, message,
&call, TIMEOUT) == FALSE) {
connman_error("Failed to get properties for %s", interface);
dbus_message_unref(message);
g_free(data);
return;
}
if (call == NULL) {
connman_error("D-Bus connection not available");
dbus_message_unref(message);
g_free(data);
return;
}
data->connection = connection;
data->message = message;
data->callback = callback;
data->user_data = user_data;
dbus_pending_call_set_notify(call, get_properties_reply, data, NULL);
}
struct adapter_data {
DBusConnection *connection;
};
struct network_data {
DBusConnection *connection;
char *interface;
};
static int pan_probe(struct connman_network *network)
{
struct connman_device *device = connman_network_get_device(network);
struct adapter_data *adapter;
struct network_data *data;
DBG("network %p", network);
if (device == NULL)
return -EINVAL;
adapter = connman_device_get_data(device);
if (adapter == NULL)
return -EINVAL;
data = g_try_new0(struct network_data, 1);
if (data == NULL)
return -ENOMEM;
data->connection = adapter->connection;
connman_network_set_data(network, data);
return 0;
}
static void pan_remove(struct connman_network *network)
{
struct network_data *data = connman_network_get_data(network);
DBG("network %p", network);
connman_network_set_data(network, NULL);
g_free(data);
}
static void connect_reply(DBusPendingCall *call, void *user_data)
{
struct connman_network *network = user_data;
struct network_data *data = connman_network_get_data(network);
DBusMessage *reply;
DBusError error;
const char *interface = NULL;
int index;
DBG("network %p", network);
reply = dbus_pending_call_steal_reply(call);
if (reply == NULL)
return;
dbus_error_init(&error);
if (dbus_message_get_args(reply, &error,
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_INVALID) == FALSE) {
if (dbus_error_is_set(&error) == TRUE) {
connman_error("%s", error.message);
dbus_error_free(&error);
} else
connman_error("Wrong arguments for connect");
goto done;
}
if (interface == NULL)
goto done;
DBG("interface %s", interface);
data->interface = g_strdup(interface);
index = connman_inet_ifindex(interface);
connman_network_set_index(network, index);
connman_network_set_connected(network, TRUE);
done:
dbus_message_unref(reply);
}
static int pan_connect(struct connman_network *network)
{
struct network_data *data = connman_network_get_data(network);
const char *path = connman_network_get_string(network, "Node");
const char *uuid = "nap";
DBusMessage *message;
DBusPendingCall *call;
DBG("network %p", network);
message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
BLUEZ_NETWORK_INTERFACE, CONNECT);
if (message == NULL)
return -ENOMEM;
dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(data->connection, message,
&call, TIMEOUT * 10) == FALSE) {
connman_error("Failed to connect service");
dbus_message_unref(message);
return -EINVAL;
}
if (call == NULL) {
connman_error("D-Bus connection not available");
dbus_message_unref(message);
return -EINVAL;
}
dbus_pending_call_set_notify(call, connect_reply, network, NULL);
dbus_message_unref(message);
return -EINPROGRESS;
}
static void disconnect_reply(DBusPendingCall *call, void *user_data)
{
struct connman_network *network = user_data;
struct network_data *data = connman_network_get_data(network);
DBusMessage *reply;
DBusError error;
DBG("network %p", network);
reply = dbus_pending_call_steal_reply(call);
if (reply == NULL)
return;
dbus_error_init(&error);
if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
if (dbus_error_is_set(&error) == TRUE) {
connman_error("%s", error.message);
dbus_error_free(&error);
} else
connman_error("Wrong arguments for disconnect");
goto done;
}
g_free(data->interface);
data->interface = NULL;
connman_network_set_connected(network, FALSE);
connman_network_set_index(network, -1);
done:
dbus_message_unref(reply);
}
static int pan_disconnect(struct connman_network *network)
{
struct network_data *data = connman_network_get_data(network);
const char *path = connman_network_get_string(network, "Node");
DBusMessage *message;
DBusPendingCall *call;
DBG("network %p", network);
if (data->interface == NULL)
return -EINVAL;
message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
BLUEZ_NETWORK_INTERFACE, DISCONNECT);
if (message == NULL)
return -ENOMEM;
dbus_message_append_args(message, DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(data->connection, message,
&call, TIMEOUT) == FALSE) {
connman_error("Failed to disconnect service");
dbus_message_unref(message);
return -EINVAL;
}
if (call == NULL) {
connman_error("D-Bus connection not available");
dbus_message_unref(message);
return -EINVAL;
}
dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
dbus_message_unref(message);
return -EINPROGRESS;
}
static struct connman_network_driver pan_driver = {
.name = "bluetooth-pan",
.type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
.probe = pan_probe,
.remove = pan_remove,
.connect = pan_connect,
.disconnect = pan_disconnect,
};
static int bluetooth_probe(struct connman_device *adapter)
{
struct adapter_data *data;
DBG("adapter %p", adapter);
data = g_try_new0(struct adapter_data, 1);
if (data == NULL)
return -ENOMEM;
data->connection = connman_dbus_get_connection();
if (data->connection == NULL) {
g_free(data);
return -EIO;
}
connman_device_set_data(adapter, data);
return 0;
}
static void bluetooth_remove(struct connman_device *adapter)
{
struct adapter_data *data = connman_device_get_data(adapter);
DBG("adapter %p", adapter);
connman_device_set_data(adapter, NULL);
dbus_connection_unref(data->connection);
g_free(data);
}
static void powered_reply(DBusPendingCall *call, void *user_data)
{
DBusMessage *reply;
DBG("");
reply = dbus_pending_call_steal_reply(call);
dbus_message_unref(reply);
}
static int change_powered(DBusConnection *connection, const char *path,
dbus_bool_t powered)
{
DBusMessage *message;
DBusMessageIter iter;
DBusPendingCall *call;
DBG("");
message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
if (message == NULL)
return -ENOMEM;
dbus_message_iter_init_append(message, &iter);
connman_dbus_property_append_variant(&iter, "Powered",
DBUS_TYPE_BOOLEAN, &powered);
if (dbus_connection_send_with_reply(connection, message,
&call, TIMEOUT) == FALSE) {
connman_error("Failed to change Powered property");
dbus_message_unref(message);
return -EINVAL;
}
if (call == NULL) {
connman_error("D-Bus connection not available");
dbus_message_unref(message);
return -EINVAL;
}
dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
dbus_message_unref(message);
return -EINPROGRESS;
}
static int bluetooth_enable(struct connman_device *adapter)
{
struct adapter_data *data = connman_device_get_data(adapter);
const char *path = connman_device_get_string(adapter, "Node");
DBG("adapter %p", adapter);
return change_powered(data->connection, path, TRUE);
}
static int bluetooth_disable(struct connman_device *adapter)
{
struct adapter_data *data = connman_device_get_data(adapter);
const char *path = connman_device_get_string(adapter, "Node");
DBG("adapter %p", adapter);
return change_powered(data->connection, path, FALSE);
}
static int bluetooth_scan(struct connman_device *adapter)
{
DBG("adapter %p", adapter);
return -EIO;
}
static struct connman_device_driver bluetooth_driver = {
.name = "bluetooth",
.type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
.probe = bluetooth_probe,
.remove = bluetooth_remove,
.enable = bluetooth_enable,
.disable = bluetooth_disable,
.scan = bluetooth_scan,
};
static GSList *adapter_list = NULL;
static void free_adapters(void)
{
GSList *list;
DBG("");
for (list = adapter_list; list; list = list->next) {
struct connman_device *adapter = list->data;
connman_device_unregister(adapter);
connman_device_unref(adapter);
}
g_slist_free(adapter_list);
adapter_list = NULL;
}
static struct connman_device *find_adapter(const char *path)
{
GSList *list;
DBG("path %s", path);
for (list = adapter_list; list; list = list->next) {
struct connman_device *adapter = list->data;
const char *adapter_path = connman_device_get_string(adapter,
"Node");
if (adapter_path == NULL)
continue;
if (g_str_equal(adapter_path, path) == TRUE)
return adapter;
}
return NULL;
}
static void device_properties(DBusConnection *connection, const char *path,
DBusMessage *message, void *user_data)
{
struct connman_device *device = user_data;
const char *node = g_basename(path);
struct connman_network *network;
DBG("path %s", path);
network = connman_device_get_network(device, node);
if (network != NULL)
return;
network = connman_network_create(node,
CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
if (network == NULL)
return;
connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
connman_network_set_string(network, "Node", path);
connman_device_add_network(device, network);
}
static void check_devices(struct connman_device *adapter,
DBusConnection *connection, DBusMessageIter *array)
{
DBusMessageIter value;
if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
return;
dbus_message_iter_recurse(array, &value);
while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
const char *path;
dbus_message_iter_get_basic(&value, &path);
get_properties(connection, path, BLUEZ_DEVICE_INTERFACE,
device_properties, adapter);
dbus_message_iter_next(&value);
}
}
static void property_changed(DBusConnection *connection, DBusMessage *message)
{
const char *path = dbus_message_get_path(message);
struct connman_device *adapter;
DBusMessageIter iter, value;
const char *key;
DBG("path %s", path);
adapter = find_adapter(path);
if (adapter == NULL)
return;
if (dbus_message_iter_init(message, &iter) == FALSE)
return;
dbus_message_iter_get_basic(&iter, &key);
dbus_message_iter_next(&iter);
dbus_message_iter_recurse(&iter, &value);
if (g_str_equal(key, "Powered") == TRUE) {
gboolean val;
dbus_message_iter_get_basic(&value, &val);
connman_device_set_powered(adapter, val);
} else if (g_str_equal(key, "Discovering") == TRUE) {
gboolean val;
dbus_message_iter_get_basic(&value, &val);
connman_device_set_scanning(adapter, val);
}
}
static void parse_adapter_properties(struct connman_device *adapter,
DBusConnection *connection,
DBusMessage *reply)
{
DBusMessageIter array, dict;
if (dbus_message_iter_init(reply, &array) == FALSE)
return;
if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
return;
dbus_message_iter_recurse(&array, &dict);
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entry, value;
const char *key;
dbus_message_iter_recurse(&dict, &entry);
dbus_message_iter_get_basic(&entry, &key);
dbus_message_iter_next(&entry);
dbus_message_iter_recurse(&entry, &value);
if (g_str_equal(key, "Powered") == TRUE) {
gboolean val;
dbus_message_iter_get_basic(&value, &val);
connman_device_set_powered(adapter, val);
} else if (g_str_equal(key, "Discovering") == TRUE) {
gboolean val;
dbus_message_iter_get_basic(&value, &val);
connman_device_set_scanning(adapter, val);
} else if (g_str_equal(key, "Devices") == TRUE) {
check_devices(adapter, connection, &value);
}
dbus_message_iter_next(&dict);
}
}
static void adapter_properties(DBusConnection *connection, const char *path,
DBusMessage *message, void *user_data)
{
const char *node = g_basename(path);
struct connman_device *adapter;
DBG("path %s", path);
adapter = find_adapter(path);
if (adapter != NULL)
goto done;
adapter = connman_device_create(node, CONNMAN_DEVICE_TYPE_BLUETOOTH);
if (adapter == NULL)
return;
connman_device_set_string(adapter, "Node", path);
if (node != NULL && g_str_has_prefix(node, "hci") == TRUE) {
int index;
errno = 0;
index = atoi(node + 3);
if (errno == 0)
connman_device_set_index(adapter, index);
}
connman_device_set_interface(adapter, node);
connman_device_set_policy(adapter, CONNMAN_DEVICE_POLICY_MANUAL);
connman_device_set_mode(adapter, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
if (connman_device_register(adapter) < 0) {
connman_device_unref(adapter);
return;
}
adapter_list = g_slist_append(adapter_list, adapter);
done:
parse_adapter_properties(adapter, connection, message);
}
static void add_adapter(DBusConnection *connection, const char *path)
{
DBG("path %s", path);
get_properties(connection, path, BLUEZ_ADAPTER_INTERFACE,
adapter_properties, NULL);
}
static void remove_adapter(DBusConnection *connection, const char *path)
{
struct connman_device *adapter;
DBG("path %s", path);
adapter = find_adapter(path);
if (adapter == NULL)
return;
adapter_list = g_slist_remove(adapter_list, adapter);
connman_device_unregister(adapter);
connman_device_unref(adapter);
}
static void list_adapters_reply(DBusPendingCall *call, void *user_data)
{
DBusConnection *connection = user_data;
DBusMessage *reply;
DBusError error;
char **adapters;
int i, num_adapters;
DBG("");
reply = dbus_pending_call_steal_reply(call);
dbus_error_init(&error);
if (dbus_message_get_args(reply, &error,
DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
&adapters, &num_adapters,
DBUS_TYPE_INVALID) == FALSE) {
if (dbus_error_is_set(&error) == TRUE) {
connman_error("%s", error.message);
dbus_error_free(&error);
} else
connman_error("Wrong arguments for adapter list");
goto done;
}
for (i = 0; i < num_adapters; i++)
get_properties(connection, adapters[i],
BLUEZ_ADAPTER_INTERFACE,
adapter_properties, NULL);
g_strfreev(adapters);
done:
dbus_message_unref(reply);
}
static void bluetooth_connect(DBusConnection *connection, void *user_data)
{
DBusMessage *message;
DBusPendingCall *call;
DBG("connection %p", connection);
message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
if (message == NULL)
return;
if (dbus_connection_send_with_reply(connection, message,
&call, TIMEOUT) == FALSE) {
connman_error("Failed to get Bluetooth adapters");
goto done;
}
if (call == NULL) {
connman_error("D-Bus connection not available");
goto done;
}
dbus_pending_call_set_notify(call, list_adapters_reply,
connection, NULL);
done:
dbus_message_unref(message);
}
static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
{
DBG("connection %p", connection);
free_adapters();
}
static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
DBusMessage *message, void *user_data)
{
if (dbus_message_has_interface(message,
BLUEZ_MANAGER_INTERFACE) == FALSE &&
dbus_message_has_interface(message,
BLUEZ_ADAPTER_INTERFACE) == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBG("connection %p", connection);
if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
PROPERTY_CHANGED) == TRUE) {
property_changed(connection, message);
} else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
ADAPTER_ADDED) == TRUE) {
const char *path;
dbus_message_get_args(message, NULL,
DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
add_adapter(connection, path);
} else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
ADAPTER_REMOVED) == TRUE) {
const char *path;
dbus_message_get_args(message, NULL,
DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
remove_adapter(connection, path);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static DBusConnection *connection;
static guint watch;
static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
",interface=" BLUEZ_MANAGER_INTERFACE;
static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
",interface=" BLUEZ_MANAGER_INTERFACE;
static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
",interface=" BLUEZ_ADAPTER_INTERFACE;
static int bluetooth_init(void)
{
int err = -EIO;
connection = connman_dbus_get_connection();
if (connection == NULL)
return -EIO;
if (dbus_connection_add_filter(connection, bluetooth_signal,
NULL, NULL) == FALSE)
goto unref;
err = connman_network_driver_register(&pan_driver);
if (err < 0)
goto remove;
err = connman_device_driver_register(&bluetooth_driver);
if (err < 0) {
connman_network_driver_unregister(&pan_driver);
goto remove;
}
watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
bluetooth_connect, bluetooth_disconnect, NULL, NULL);
if (watch == 0) {
connman_device_driver_unregister(&bluetooth_driver);
connman_network_driver_unregister(&pan_driver);
err = -EIO;
goto remove;
}
if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
bluetooth_connect(connection, NULL);
dbus_bus_add_match(connection, added_rule, NULL);
dbus_bus_add_match(connection, removed_rule, NULL);
dbus_bus_add_match(connection, adapter_rule, NULL);
dbus_connection_flush(connection);
return 0;
remove:
dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
unref:
dbus_connection_unref(connection);
return err;
}
static void bluetooth_exit(void)
{
dbus_bus_remove_match(connection, adapter_rule, NULL);
dbus_bus_remove_match(connection, removed_rule, NULL);
dbus_bus_remove_match(connection, added_rule, NULL);
dbus_connection_flush(connection);
g_dbus_remove_watch(connection, watch);
free_adapters();
connman_device_driver_unregister(&bluetooth_driver);
connman_network_driver_unregister(&pan_driver);
dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
dbus_connection_unref(connection);
}
CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)
|