3 * neard - Near Field Communication manager
5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #define BLUEZ_SERVICE "org.bluez"
35 #define MANAGER_INTF BLUEZ_SERVICE ".Manager"
36 #define ADAPTER_INTF BLUEZ_SERVICE ".Adapter"
37 #define OOB_INTF BLUEZ_SERVICE ".OutOfBand"
38 #define DEFAULT_ADAPTER "DefaultAdapter"
39 #define ADAPTER_REMOVED "AdapterRemoved"
40 #define DEFAULT_ADAPTER_CHANGED "DefaultAdapterChanged"
41 #define ADAPTER_PROPERTY_CHANGED "PropertyChanged"
42 #define MANAGER_PATH "/"
43 #define OOB_AGENT "/org/neard/agent/neard_oob"
45 #define BT_NOINPUTOUTPUT "NoInputNoOutput"
46 #define BT_DISPLAY_YESNO "DisplayYesNo"
48 #define DBUS_MANAGER_INTF "org.freedesktop.DBus.ObjectManager"
49 #define AGENT_REGISTER_TIMEOUT 2
52 #define EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */
53 #define EIR_NAME_SHORT 0x08 /* shortened local name */
54 #define EIR_NAME_COMPLETE 0x09 /* complete local name */
56 /* Specific OOB EIRs */
57 #define EIR_CLASS_OF_DEVICE 0x0D /* class of device */
58 #define EIR_SP_HASH 0x0E /* simple pairing hash C */
59 #define EIR_SP_RANDOMIZER 0x0F /* simple pairing randomizer R */
61 #define EIR_DEVICE_ID 0x10 /* device ID */
62 #define EIR_SECURITY_MGR_FLAGS 0x11 /* security manager flags */
64 #define EIR_SIZE_LEN 1
65 #define EIR_HEADER_LEN (EIR_SIZE_LEN + 1)
66 #define BT_ADDRESS_SIZE 6
68 #define OOB_SP_SIZE 16
69 #define EIR_SIZE_MAX 255
71 struct near_oob_data {
74 char *bd_addr; /* oob mandatory */
77 char *bt_name; /* short or long name */
79 int class_of_device; /* Class of device */
86 uint8_t *spair_hash; /* OOB hash Key */
87 uint8_t *spair_randomizer; /* OOB randomizer key */
88 uint8_t authentication[OOB_SP_SIZE]; /* On BT 2.0 */
89 uint8_t security_manager_oob_flags; /* see BT Core 4.0 */
92 static DBusConnection *bt_conn;
93 static struct near_oob_data bt_def_oob_data;
96 static guint removed_watch;
97 static guint adapter_watch;
98 static guint adapter_props_watch;
100 static guint register_bluez_timer;
102 static void __bt_eir_free(struct near_oob_data *oob)
106 if (oob->def_adapter) {
107 g_free(oob->def_adapter);
108 oob->def_adapter = NULL;
112 g_free(oob->bd_addr);
117 g_free(oob->bt_name);
121 if (oob->spair_hash) {
122 g_free(oob->spair_hash);
123 oob->spair_hash = NULL;
126 if (oob->spair_randomizer) {
127 g_free(oob->spair_randomizer);
128 oob->spair_randomizer = NULL;
132 static void bt_eir_free(struct near_oob_data *oob)
139 /* D-Bus helper functions */
140 static int bt_generic_call(DBusConnection *conn,
141 struct near_oob_data *oob, /* user data */
142 const char *dest, /* method call */
144 const char *interface,
146 DBusPendingCallNotifyFunction bt_cb, /* callback */
147 int type, ...) /* params */
150 DBusPendingCall *pending;
156 msg = dbus_message_new_method_call(dest, path, interface, method);
159 near_error("Unable to allocate new D-Bus %s message", method);
163 va_start(args, type);
165 if (!dbus_message_append_args_valist(msg, type, args)) {
172 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
173 near_error("Sending %s failed", method);
179 near_error("D-Bus connection not available");
184 /* Prepare for notification */
185 dbus_pending_call_set_notify(pending, bt_cb, oob, NULL);
189 dbus_message_unref(msg);
193 static void bt_create_paired_device_cb(DBusPendingCall *pending,
198 struct near_oob_data *oob = user_data;
202 reply = dbus_pending_call_steal_reply(pending);
206 dbus_error_init(&error);
208 if (dbus_set_error_from_message(&error, reply)) {
209 near_error("%s", error.message);
210 dbus_error_free(&error);
214 DBG("Successful pairing");
217 /* task completed - clean memory*/
220 dbus_message_unref(reply);
221 dbus_pending_call_unref(pending);
224 static int bt_create_paired_device(DBusConnection *conn,
225 struct near_oob_data *oob,
226 const char *capabilities)
228 const char *agent_path = OOB_AGENT;
230 return bt_generic_call(bt_conn, oob, BLUEZ_SERVICE,
231 oob->def_adapter, ADAPTER_INTF, "CreatePairedDevice",
232 bt_create_paired_device_cb,
234 DBUS_TYPE_STRING, &oob->bd_addr,
235 DBUS_TYPE_OBJECT_PATH, &agent_path,
236 DBUS_TYPE_STRING, &capabilities,
240 static void bt_oob_add_remote_data_cb(DBusPendingCall *pending, void *user_data)
244 struct near_oob_data *oob = user_data;
248 reply = dbus_pending_call_steal_reply(pending);
252 dbus_error_init(&error);
254 if (dbus_set_error_from_message(&error, reply))
257 near_info("OOB data added");
259 dbus_message_unref(reply);
260 dbus_pending_call_unref(pending);
262 /* Jump to the next: Pairing !!!*/
263 DBG("Try to pair devices...");
264 bt_create_paired_device(bt_conn, oob, BT_DISPLAY_YESNO);
268 near_error("%s", error.message);
269 dbus_error_free(&error);
273 dbus_message_unref(reply);
274 dbus_pending_call_unref(pending);
277 static int bt_oob_add_remote_data(DBusConnection *conn,
278 struct near_oob_data *oob)
280 int16_t hash_len = 16;
281 int16_t rdm_len = 16;
283 return bt_generic_call(bt_conn, oob, BLUEZ_SERVICE,
284 oob->def_adapter, OOB_INTF, "AddRemoteData",
285 bt_oob_add_remote_data_cb,
287 DBUS_TYPE_STRING, &oob->bd_addr,
289 DBUS_TYPE_BYTE, &oob->spair_hash, hash_len,
291 DBUS_TYPE_BYTE, &oob->spair_randomizer, rdm_len,
295 /* Pairing: JustWorks or OOB */
296 static int bt_do_pairing(struct near_oob_data *oob)
300 DBG("%s", oob->bd_addr);
302 /* Is this a *real* oob pairing or a "JustWork" */
303 if ((oob->spair_hash) && (oob->spair_randomizer))
304 err = bt_oob_add_remote_data(bt_conn, oob);
306 err = bt_create_paired_device(bt_conn, oob,
310 near_error("Pairing failed. Err[%d]", err);
317 static int extract_properties(DBusMessage *reply, struct near_oob_data *oob)
323 DBusMessageIter array, dict;
325 if (!dbus_message_iter_init(reply, &array))
328 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
331 dbus_message_iter_recurse(&array, &dict);
333 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
334 DBusMessageIter entry, value;
337 dbus_message_iter_recurse(&dict, &entry);
338 dbus_message_iter_get_basic(&entry, &key);
340 dbus_message_iter_next(&entry);
341 dbus_message_iter_recurse(&entry, &value);
343 if (g_str_equal(key, "Address")) {
344 dbus_message_iter_get_basic(&value, &data);
346 /* Now, fill the local struct */
347 oob->bd_addr = g_try_malloc0(BT_ADDRESS_SIZE);
351 /* Address is like: "ff:ee:dd:cc:bb:aa" */
352 for (i = 5, j = 0 ; i >= 0; i--, j += 3)
353 oob->bd_addr[i] = strtol(data + j, NULL, 16);
354 DBG("local address: %s", data);
356 } else if (g_str_equal(key, "Name")) {
357 dbus_message_iter_get_basic(&value, &data);
358 oob->bt_name = g_strdup(data);
360 oob->bt_name_len = strlen(oob->bt_name);
361 DBG("local name: %s", oob->bt_name);
364 } else if (g_str_equal(key, "Class")) {
365 dbus_message_iter_get_basic(&value, &idata);
366 oob->class_of_device = idata;
368 } else if (g_str_equal(key, "Powered")) {
369 dbus_message_iter_get_basic(&value, &idata);
370 oob->powered = idata;
372 } else if (g_str_equal(key, "Discoverable")) {
373 dbus_message_iter_get_basic(&value, &idata);
374 oob->discoverable = idata;
376 } else if (g_str_equal(key, "Pairable")) {
377 dbus_message_iter_get_basic(&value, &idata);
378 oob->pairable = idata;
380 } else if (g_str_equal(key, "UUIDs")) {
381 oob->uuids_len = sizeof(value);
382 oob->uuids = g_try_malloc0(oob->uuids_len);
385 memcpy(oob->uuids, &value, oob->uuids_len);
388 dbus_message_iter_next(&dict);
394 static int bt_parse_properties(DBusMessage *reply, void *user_data)
396 struct near_oob_data *bt_props = user_data;
401 g_free(bt_props->bd_addr);
402 g_free(bt_props->bt_name);
404 /* Grab properties from dbus */
405 if (extract_properties(reply, bt_props) < 0)
411 g_free(bt_props->bd_addr);
412 bt_props->bd_addr = NULL;
414 g_free(bt_props->bt_name);
415 bt_props->bt_name = NULL;
420 static gboolean bt_adapter_property_changed(DBusConnection *conn,
421 DBusMessage *message,
424 DBusMessageIter iter;
426 const char *property;
428 if (!dbus_message_iter_init(message, &iter))
431 dbus_message_iter_get_basic(&iter, &property);
432 dbus_message_iter_next(&iter);
434 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
437 dbus_message_iter_recurse(&iter, &var);
439 if (g_str_equal(property, "Name")) {
442 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
445 dbus_message_iter_get_basic(&var, &name);
447 g_free(bt_def_oob_data.bt_name);
448 bt_def_oob_data.bt_name = g_strdup(name);
450 if (bt_def_oob_data.bt_name)
451 bt_def_oob_data.bt_name_len = strlen(name);
453 bt_def_oob_data.bt_name_len = 0;
455 DBG("%s: %s", property, name);
456 } else if (g_str_equal(property, "Class")) {
459 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_UINT32)
462 dbus_message_iter_get_basic(&var, &class);
463 bt_def_oob_data.class_of_device = class;
465 DBG("%s: %x", property, bt_def_oob_data.class_of_device);
466 } else if (g_str_equal(property, "Powered")) {
469 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
472 dbus_message_iter_get_basic(&var, &powered);
473 bt_def_oob_data.powered = powered;
475 DBG("%s: %u", property, bt_def_oob_data.powered);
481 /* Get default local adapter properties */
482 static void bt_get_properties_cb(DBusPendingCall *pending, void *user_data)
484 struct near_oob_data *bt_props = user_data;
491 reply = dbus_pending_call_steal_reply(pending);
495 dbus_error_init(&error);
497 if (dbus_set_error_from_message(&error, reply))
500 err = bt_parse_properties(reply, bt_props);
502 near_error("Problem parsing local properties %d", err);
504 DBG("Get Properties complete: %s", bt_props->def_adapter);
506 adapter_props_watch = g_dbus_add_signal_watch(bt_conn, NULL, NULL,
508 ADAPTER_PROPERTY_CHANGED,
509 bt_adapter_property_changed,
513 dbus_message_unref(reply);
514 dbus_pending_call_unref(pending);
518 near_error("%s", error.message);
519 dbus_error_free(&error);
521 dbus_message_unref(reply);
522 dbus_pending_call_unref(pending);
525 static void bt_get_default_adapter_cb(DBusPendingCall *pending, void *user_data)
527 struct near_oob_data *bt_props = user_data;
534 reply = dbus_pending_call_steal_reply(pending);
538 dbus_error_init(&error);
540 if (dbus_set_error_from_message(&error, reply))
543 if (!dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH,
544 &path, DBUS_TYPE_INVALID))
547 /* Save the default adapter */
548 bt_props->def_adapter = g_strdup(path);
549 DBG("Using default adapter %s", bt_props->def_adapter);
552 dbus_message_unref(reply);
553 dbus_pending_call_unref(pending);
555 /* Jump on getAdapterProperties */
556 bt_generic_call(bt_conn, bt_props,
558 bt_props->def_adapter,
559 ADAPTER_INTF, "GetProperties",
560 bt_get_properties_cb,
565 near_error("Could not get Bluetooth default adapter %s", error.message);
566 dbus_error_free(&error);
568 dbus_message_unref(reply);
569 dbus_pending_call_unref(pending);
572 static int bt_refresh_adapter_props(DBusConnection *conn, void *user_data)
574 DBG("%p %p", conn, user_data);
576 return bt_generic_call(conn, user_data,
578 MANAGER_PATH, MANAGER_INTF,
580 bt_get_default_adapter_cb,
584 /* Parse and fill the bluetooth oob information block */
585 static void bt_parse_eir(uint8_t *eir_data, uint16_t eir_data_len,
586 struct near_oob_data *oob, uint16_t *props)
591 DBG("total len: %u", eir_data_len);
593 while (len < eir_data_len - 1) {
594 uint8_t eir_len = eir_data[0]; /* EIR field length */
595 uint8_t eir_code; /* EIR field type*/
596 uint8_t data_len; /* EIR data length */
599 /* check for early termination */
605 /* Do not continue EIR Data parsing if got incorrect length */
606 if (len > eir_data_len)
609 data_len = eir_len - 1;
611 eir_code = eir_data[1]; /* EIR code */
614 DBG("type 0x%.2X data_len %u", eir_code, data_len);
618 case EIR_NAME_COMPLETE:
619 oob->bt_name = g_try_malloc0(data_len + 1); /* eos */
621 oob->bt_name_len = data_len;
622 memcpy(oob->bt_name, data, oob->bt_name_len);
623 oob->bt_name[data_len] = 0; /* end str*/
627 case EIR_CLASS_OF_DEVICE:
628 tmp = g_strdup_printf("%02X%02X%02X",
629 *data, *(data + 1), *(data + 2));
631 oob->class_of_device = strtol(tmp, NULL, 16);
632 *props |= OOB_PROPS_COD;
638 oob->spair_hash = g_try_malloc0(OOB_SP_SIZE);
639 if (oob->spair_hash) {
640 memcpy(oob->spair_hash, data, OOB_SP_SIZE);
641 *props |= OOB_PROPS_SP_HASH;
645 case EIR_SP_RANDOMIZER:
646 oob->spair_randomizer = g_try_malloc0(OOB_SP_SIZE);
647 if (oob->spair_randomizer) {
648 memcpy(oob->spair_randomizer,
650 *props |= OOB_PROPS_SP_RANDOM;
654 case EIR_SECURITY_MGR_FLAGS:
655 oob->security_manager_oob_flags = *data;
658 case EIR_UUID128_ALL:
659 /* TODO: Process uuids128
663 default: /* ignore and skip */
664 near_error("Unknown EIR x%02x (len: %d)", eir_code,
669 eir_data += eir_len + 1;
674 * Because of some "old" implementation, "version" will help
675 * to determine the record data structure.
676 * Some specifications are proprietary (eg. "short mode")
677 * and are not fully documented.
678 * mime_properties is a bitmask and should reflect the fields found in
681 int __near_bluetooth_parse_oob_record(struct carrier_data *data,
682 uint16_t *mime_properties,
685 struct near_oob_data *oob;
686 uint16_t bt_oob_data_size;
687 uint8_t *ptr = data->data;
693 oob = g_try_malloc0(sizeof(struct near_oob_data));
695 if (data->type == BT_MIME_V2_1) {
697 * Total OOB data size (including size bytes)
698 * Some implementations (e.g. Android 4.1) stores
699 * the data_size in big endian but NDEF forum spec (BT Secure
700 * Simple Pairing) requires a little endian. At the same time,
701 * the NDEF forum NDEF spec define a payload length as single
702 * byte (and the payload size IS the oob data size).
704 bt_oob_data_size = near_get_le16(ptr);
705 if (bt_oob_data_size > 0xFF) /* Big Endian */
706 bt_oob_data_size = GUINT16_FROM_BE(bt_oob_data_size);
708 bt_oob_data_size -= 2 ; /* remove oob datas size len */
710 /* First item: BD_ADDR (mandatory) */
711 ptr = &data->data[2];
712 oob->bd_addr = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
713 ptr[5], ptr[4], ptr[3], ptr[2], ptr[1], ptr[0]);
715 /* Skip to the next element (optional) */
716 ptr += BT_ADDRESS_SIZE;
717 bt_oob_data_size -= BT_ADDRESS_SIZE ;
719 if (bt_oob_data_size)
720 bt_parse_eir(ptr, bt_oob_data_size, oob,
722 } else if (data->type == BT_MIME_V2_0) {
723 marker = *ptr++; /* could be '$' */
725 oob->bd_addr = g_strdup_printf(
726 "%02X:%02X:%02X:%02X:%02X:%02X",
727 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
728 ptr = ptr + BT_ADDRESS_SIZE;
730 /* Class of device */
731 tmp = g_strdup_printf("%02X%02X%02X",
732 *ptr, *(ptr + 1), *(ptr + 2));
734 oob->class_of_device = strtol(tmp, NULL, 16);
739 /* "Short mode" seems to use a 4 bytes code
740 * instead of 16 bytes...
742 if (marker == '$') { /* Short NFC */
743 memcpy(oob->authentication, ptr, 4);
746 memcpy(oob->authentication, ptr, 16);
750 /* get the device name */
751 oob->bt_name_len = *ptr++;
752 oob->bt_name = g_try_malloc0(oob->bt_name_len+1);
754 memcpy(oob->bt_name, ptr, oob->bt_name_len);
755 oob->bt_name[oob->bt_name_len+1] = 0;
757 ptr = ptr + oob->bt_name_len;
768 /* check and get the default adapter */
769 oob->def_adapter = g_strdup(bt_def_oob_data.def_adapter);
770 if (!oob->def_adapter) {
771 near_error("bt_get_default_adapter failed");
776 return bt_do_pairing(oob);
779 int __near_bluetooth_pair(void *data)
781 struct near_oob_data *oob = data;
783 /* check and get the default adapter */
784 oob->def_adapter = g_strdup(bt_def_oob_data.def_adapter);
786 near_error("bt_get_default_adapter failed: %d", -EIO);
791 return bt_do_pairing(oob);
794 /* This function is synchronous as oob datas change on each session */
795 static int bt_sync_oob_readlocaldata(DBusConnection *conn, char *adapter_path,
797 char *spair_randomizer)
799 DBusMessage *message, *reply;
801 int hash_len, rndm_len;
803 message = dbus_message_new_method_call(BLUEZ_SERVICE, adapter_path,
804 OOB_INTF, "ReadLocalData");
808 dbus_error_init(&error);
810 reply = dbus_connection_send_with_reply_and_block(conn,
811 message, -1, &error);
813 dbus_message_unref(message);
816 if (dbus_error_is_set(&error)) {
817 near_error("%s", error.message);
818 dbus_error_free(&error);
820 near_error("Failed to set property");
825 if (!dbus_message_get_args(reply, NULL, DBUS_TYPE_ARRAY,
826 DBUS_TYPE_BYTE, spair_hash,
827 &hash_len, DBUS_TYPE_ARRAY,
828 DBUS_TYPE_BYTE, spair_randomizer,
829 &rndm_len, DBUS_TYPE_INVALID))
832 if ((hash_len != OOB_SP_SIZE) || (rndm_len != OOB_SP_SIZE)) {
833 DBG("no OOB data found !");
837 dbus_message_unref(reply);
838 DBG("OOB data found");
842 dbus_message_unref(reply);
847 * External API to get bt properties
848 * Prepare a "real" oob datas block
849 * mime_props is a bitmask we use to add or not specific fields in the
850 * oob frame (e.g.: OOB keys)
852 struct carrier_data *__near_bluetooth_local_get_properties(uint16_t mime_props)
854 struct carrier_data *data = NULL;
857 char hash[OOB_SP_SIZE];
858 char random[OOB_SP_SIZE];
860 /* Check adapter datas */
861 if (!bt_def_oob_data.def_adapter) {
862 near_error("No bt adapter info");
866 data = g_try_malloc0(sizeof(*data));
870 data->size = sizeof(uint16_t) /* stored oob size */
871 + BT_ADDRESS_SIZE; /* device address */
873 offset = sizeof(uint16_t); /* Skip size...will be filled later */
875 /* Now prepare data frame */
876 memcpy(data->data + offset, bt_def_oob_data.bd_addr, BT_ADDRESS_SIZE);
877 offset += BT_ADDRESS_SIZE;
880 data->size += COD_SIZE + EIR_HEADER_LEN;
882 data->data[offset++] = COD_SIZE + EIR_SIZE_LEN;
883 data->data[offset++] = EIR_CLASS_OF_DEVICE;
885 memcpy(data->data + offset,
886 (uint8_t *)&bt_def_oob_data.class_of_device, COD_SIZE);
890 * The following data are generated dynamically so we have to read the
891 * local oob data. Only add OOB pairing keys if needed.
893 if ((mime_props & OOB_PROPS_SP) != 0 &&
894 bt_sync_oob_readlocaldata(bt_conn,
895 bt_def_oob_data.def_adapter,
896 hash, random) == OOB_SP_SIZE) {
897 data->size += 2 * (OOB_SP_SIZE + EIR_HEADER_LEN);
900 data->data[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN;
901 data->data[offset++] = EIR_SP_HASH;
902 memcpy(data->data + offset, hash, OOB_SP_SIZE);
903 offset += OOB_SP_SIZE;
905 data->data[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN;
906 data->data[offset++] = EIR_SP_RANDOMIZER;
907 memcpy(data->data + offset, random, OOB_SP_SIZE);
908 offset += OOB_SP_SIZE;
912 if (bt_def_oob_data.bt_name) {
915 data->size += EIR_HEADER_LEN;
917 if (data->size + bt_def_oob_data.bt_name_len
919 name_len = EIR_SIZE_MAX - data->size;
920 data->data[offset++] = name_len + EIR_SIZE_LEN;
922 data->data[offset++] = EIR_NAME_COMPLETE;
924 name_len = bt_def_oob_data.bt_name_len;
925 data->data[offset++] = name_len + EIR_SIZE_LEN;
927 data->data[offset++] = EIR_NAME_SHORT;
930 data->size += name_len;
931 memcpy(data->data + offset, bt_def_oob_data.bt_name, name_len);
935 data->data[0] = data->size ;
937 if (bt_def_oob_data.powered)
938 data->state = CPS_ACTIVE;
940 data->state = CPS_INACTIVE;
949 /* BT adapter removed handler */
950 static gboolean bt_adapter_removed(DBusConnection *conn, DBusMessage *message,
953 DBusMessageIter iter;
954 struct near_oob_data *bt_props = user_data;
955 const char *adapter_path;
959 if (!bt_props->def_adapter)
962 g_dbus_remove_watch(bt_conn, adapter_props_watch);
963 adapter_props_watch = 0;
965 if (!dbus_message_iter_init(message, &iter))
968 dbus_message_iter_get_basic(&iter, &adapter_path);
970 if (g_strcmp0(adapter_path, bt_props->def_adapter) == 0) {
971 near_info("Remove the default adapter [%s]", adapter_path);
973 __bt_eir_free(bt_props);
974 bt_props->def_adapter = NULL;
980 /* BT default adapter changed handler */
981 static gboolean bt_default_adapter_changed(DBusConnection *conn,
982 DBusMessage *message,
985 struct near_oob_data *bt_props = user_data;
986 DBusMessageIter iter;
987 const char *adapter_path;
991 if (!dbus_message_iter_init(message, &iter))
994 g_dbus_remove_watch(bt_conn, adapter_props_watch);
995 adapter_props_watch = 0;
997 dbus_message_iter_get_basic(&iter, &adapter_path);
998 DBG("New default adapter [%s]", adapter_path);
1000 /* Disable the old one */
1001 __bt_eir_free(bt_props);
1002 bt_props->def_adapter = NULL;
1005 bt_refresh_adapter_props(conn, user_data);
1010 static void bt_dbus_disconnect_cb(DBusConnection *conn, void *user_data)
1012 near_error("D-Bus disconnect (BT)");
1016 static gboolean register_bluez(gpointer user_data)
1020 register_bluez_timer = 0;
1022 removed_watch = g_dbus_add_signal_watch(bt_conn, NULL, NULL,
1026 &bt_def_oob_data, NULL);
1029 adapter_watch = g_dbus_add_signal_watch(bt_conn, NULL, NULL,
1031 DEFAULT_ADAPTER_CHANGED,
1032 bt_default_adapter_changed,
1033 &bt_def_oob_data, NULL);
1035 if (removed_watch == 0 || adapter_watch == 0) {
1036 near_error("BlueZ event handlers failed to register.");
1037 g_dbus_remove_watch(bt_conn, removed_watch);
1038 g_dbus_remove_watch(bt_conn, adapter_watch);
1043 if (bt_refresh_adapter_props(bt_conn, user_data) < 0)
1044 near_error("Failed to get BT adapter properties");
1049 static void bt_connect(DBusConnection *conn, void *data)
1051 DBG("connection %p with %p", conn, data);
1053 if (__near_agent_handover_registered(HO_AGENT_BT)) {
1054 DBG("Agent already registered");
1059 * BlueZ 5 will register itself as HandoverAgent, give it some time
1060 * to do it before going legacy way.
1062 register_bluez_timer = g_timeout_add_seconds(AGENT_REGISTER_TIMEOUT,
1063 register_bluez, data);
1066 static void bt_disconnect(DBusConnection *conn, void *user_data)
1070 /* If timer is running no BlueZ watchers were registered yet */
1071 if (register_bluez_timer > 0) {
1072 g_source_remove(register_bluez_timer);
1073 register_bluez_timer = 0;
1077 __bt_eir_free(user_data);
1079 g_dbus_remove_watch(bt_conn, removed_watch);
1082 g_dbus_remove_watch(bt_conn, adapter_watch);
1085 g_dbus_remove_watch(bt_conn, adapter_props_watch);
1086 adapter_props_watch = 0;
1089 static int bt_prepare_handlers(DBusConnection *conn)
1091 if (__near_agent_handover_registered(HO_AGENT_BT))
1094 watch = g_dbus_add_service_watch(bt_conn, BLUEZ_SERVICE,
1097 &bt_def_oob_data, NULL);
1099 near_error("BlueZ service watch handler failed to register.");
1100 g_dbus_remove_watch(bt_conn, watch);
1107 void __near_bluetooth_legacy_start(void)
1111 bt_prepare_handlers(bt_conn);
1114 void __near_bluetooth_legacy_stop(void)
1118 g_dbus_remove_watch(bt_conn, watch);
1121 bt_disconnect(bt_conn, &bt_def_oob_data);
1124 /* Bluetooth exiting function */
1125 void __near_bluetooth_cleanup(void)
1132 __near_bluetooth_legacy_stop();
1134 dbus_connection_unref(bt_conn);
1138 * Bluetooth initialization function.
1139 * Allocate bt local settings storage
1140 * and setup event handlers
1142 int __near_bluetooth_init(void)
1148 dbus_error_init(&err);
1150 /* save the dbus connection */
1151 bt_conn = near_dbus_get_connection();
1153 if (dbus_error_is_set(&err)) {
1154 near_error("%s", err.message);
1155 dbus_error_free(&err);
1157 near_error("Can't register with system bus\n");
1161 /* dbus disconnect callback */
1162 g_dbus_set_disconnect_function(bt_conn, bt_dbus_disconnect_cb,
1165 /* Set bluez event handlers */
1166 return bt_prepare_handlers(bt_conn);