3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2010 Nokia Corporation
6 * Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
38 #include <bluetooth/bluetooth.h>
39 #include <bluetooth/hci.h>
40 #include <bluetooth/sdp.h>
41 #include <bluetooth/sdp_lib.h>
42 #include <bluetooth/mgmt.h>
53 #define MGMT_BUF_SIZE 1024
60 static int max_index = -1;
61 static struct controller_info {
66 uint16_t manufacturer;
67 uint32_t supported_settings;
68 uint32_t current_settings;
73 gboolean pending_uuid;
74 GSList *pending_uuids;
76 gboolean pending_class;
80 gboolean pending_powered;
81 gboolean pending_cod_change;
82 } *controllers = NULL;
84 static int mgmt_sock = -1;
85 static guint mgmt_watch = 0;
87 static uint8_t mgmt_version = 0;
88 static uint16_t mgmt_revision = 0;
90 static void read_version_complete(int sk, void *buf, size_t len)
93 struct mgmt_rp_read_version *rp = buf;
95 if (len < sizeof(*rp)) {
96 error("Too small read version complete event"
97 " (probably an old kernel)");
101 mgmt_revision = btohs(bt_get_unaligned(&rp->revision));
102 mgmt_version = rp->version;
104 DBG("version %u revision %u", mgmt_version, mgmt_revision);
106 if (mgmt_version < 1) {
107 error("Version 1 of mgmt needed (kernel has version %u)",
112 memset(&hdr, 0, sizeof(hdr));
113 hdr.opcode = htobs(MGMT_OP_READ_INDEX_LIST);
114 hdr.index = htobs(MGMT_INDEX_NONE);
115 if (write(sk, &hdr, sizeof(hdr)) < 0)
116 error("Unable to read controller index list: %s (%d)",
117 strerror(errno), errno);
120 static void add_controller(uint16_t index)
122 struct controller_info *info;
124 if (index > max_index) {
125 size_t size = sizeof(struct controller_info) * (index + 1);
127 controllers = g_realloc(controllers, size);
130 info = &controllers[index];
132 memset(info, 0, sizeof(*info));
136 DBG("Added controller %u", index);
139 static void read_info(int sk, uint16_t index)
143 memset(&hdr, 0, sizeof(hdr));
144 hdr.opcode = htobs(MGMT_OP_READ_INFO);
145 hdr.index = htobs(index);
147 if (write(sk, &hdr, sizeof(hdr)) < 0)
148 error("Unable to send read_info command: %s (%d)",
149 strerror(errno), errno);
152 static void get_connections(int sk, uint16_t index)
156 memset(&hdr, 0, sizeof(hdr));
157 hdr.opcode = htobs(MGMT_OP_GET_CONNECTIONS);
158 hdr.index = htobs(index);
160 if (write(sk, &hdr, sizeof(hdr)) < 0)
161 error("Unable to send get_connections command: %s (%d)",
162 strerror(errno), errno);
165 static void mgmt_index_added(int sk, uint16_t index)
167 add_controller(index);
168 read_info(sk, index);
171 static void remove_controller(uint16_t index)
173 if (index > max_index)
176 if (!controllers[index].valid)
179 btd_manager_unregister_adapter(index);
181 g_slist_free_full(controllers[index].pending_uuids, g_free);
182 controllers[index].pending_uuids = NULL;
184 memset(&controllers[index], 0, sizeof(struct controller_info));
186 DBG("Removed controller %u", index);
189 static void mgmt_index_removed(int sk, uint16_t index)
191 remove_controller(index);
194 static int mgmt_set_mode(int index, uint16_t opcode, uint8_t val)
196 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_mode)];
197 struct mgmt_hdr *hdr = (void *) buf;
198 struct mgmt_mode *cp = (void *) &buf[sizeof(*hdr)];
200 memset(buf, 0, sizeof(buf));
201 hdr->opcode = htobs(opcode);
202 hdr->index = htobs(index);
203 hdr->len = htobs(sizeof(*cp));
207 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
213 static int mgmt_set_connectable(int index, gboolean connectable)
215 DBG("index %d connectable %d", index, connectable);
216 return mgmt_set_mode(index, MGMT_OP_SET_CONNECTABLE, connectable);
219 static int mgmt_set_discoverable(int index, gboolean discoverable,
222 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_discoverable)];
223 struct mgmt_hdr *hdr = (void *) buf;
224 struct mgmt_cp_set_discoverable *cp = (void *) &buf[sizeof(*hdr)];
226 DBG("index %d discoverable %d", index, discoverable);
228 memset(buf, 0, sizeof(buf));
229 hdr->opcode = htobs(MGMT_OP_SET_DISCOVERABLE);
230 hdr->index = htobs(index);
231 hdr->len = htobs(sizeof(*cp));
233 cp->val = discoverable;
234 cp->timeout = timeout;
236 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
242 static int mgmt_set_pairable(int index, gboolean pairable)
244 DBG("index %d pairable %d", index, pairable);
245 return mgmt_set_mode(index, MGMT_OP_SET_PAIRABLE, pairable);
248 static inline int mgmt_powered(uint32_t settings)
250 return (settings & MGMT_SETTING_POWERED) != 0;
253 static inline int mgmt_connectable(uint32_t settings)
255 return (settings & MGMT_SETTING_CONNECTABLE) != 0;
258 static inline int mgmt_fast_connectable(uint32_t settings)
260 return (settings & MGMT_SETTING_FAST_CONNECTABLE) != 0;
263 static inline int mgmt_discoverable(uint32_t settings)
265 return (settings & MGMT_SETTING_DISCOVERABLE) != 0;
268 static inline int mgmt_pairable(uint32_t settings)
270 return (settings & MGMT_SETTING_PAIRABLE) != 0;
273 static inline int mgmt_ssp(uint32_t settings)
275 return (settings & MGMT_SETTING_SSP) != 0;
278 static inline int mgmt_bredr(uint32_t settings)
280 return (settings & MGMT_SETTING_BREDR) != 0;
283 static inline int mgmt_high_speed(uint32_t settings)
285 return (settings & MGMT_SETTING_HS) != 0;
288 static inline int mgmt_low_energy(uint32_t settings)
290 return (settings & MGMT_SETTING_LE) != 0;
293 static uint8_t create_mode(uint32_t settings)
297 if (mgmt_connectable(settings))
300 if (mgmt_discoverable(settings))
301 mode |= SCAN_INQUIRY;
306 static void update_settings(struct btd_adapter *adapter, uint32_t settings)
308 struct controller_info *info;
311 uint16_t index, discoverable_timeout;
313 DBG("new settings %x", settings);
315 btd_adapter_get_mode(adapter, NULL, &on_mode, &discoverable_timeout,
318 index = adapter_get_dev_id(adapter);
320 info = &controllers[index];
322 if (on_mode == MODE_DISCOVERABLE && !mgmt_discoverable(settings)) {
323 if(!mgmt_connectable(settings))
324 mgmt_set_connectable(index, TRUE);
325 mgmt_set_discoverable(index, TRUE, discoverable_timeout);
326 } else if (on_mode == MODE_CONNECTABLE && !mgmt_connectable(settings)) {
327 mgmt_set_connectable(index, TRUE);
328 } else if (mgmt_powered(settings)) {
329 adapter_mode_changed(adapter, create_mode(settings));
332 if (mgmt_pairable(settings) != pairable)
333 mgmt_set_pairable(index, pairable);
335 if (mgmt_ssp(info->supported_settings) && !mgmt_ssp(settings))
336 mgmt_set_mode(index, MGMT_OP_SET_SSP, 1);
338 if (mgmt_low_energy(info->supported_settings) &&
339 !mgmt_low_energy(settings))
340 mgmt_set_mode(index, MGMT_OP_SET_LE, 1);
343 static int mgmt_update_powered(struct btd_adapter *adapter,
344 struct controller_info *info,
347 if (!mgmt_powered(settings)) {
348 btd_adapter_stop(adapter);
349 g_slist_free_full(info->pending_uuids, g_free);
350 info->pending_uuids = NULL;
351 info->pending_uuid = FALSE;
352 info->pending_class = FALSE;
353 info->pending_cod_change = FALSE;
357 btd_adapter_start(adapter);
359 update_settings(adapter, settings);
364 static int mode_changed(uint32_t s1, uint32_t s2)
366 if (mgmt_connectable(s1) != mgmt_connectable(s2))
369 if (mgmt_discoverable(s1) != mgmt_discoverable(s2))
375 static void mgmt_new_settings(int sk, uint16_t index, void *buf, size_t len)
377 uint32_t settings, *ev = buf;
378 struct controller_info *info;
379 struct btd_adapter *adapter;
380 gboolean old_power, new_power, old_pairable, new_pairable;
382 if (len < sizeof(*ev)) {
383 error("Too small new settings event");
387 DBG("hci%u new settings", index);
389 if (index > max_index) {
390 error("Unexpected index %u in new_settings event", index);
394 info = &controllers[index];
396 adapter = manager_find_adapter(&info->bdaddr);
397 if (adapter == NULL) {
398 DBG("Adapter not found");
402 settings = bt_get_le32(ev);
404 old_power = mgmt_powered(info->current_settings);
405 new_power = mgmt_powered(settings);
407 if (new_power != old_power)
408 mgmt_update_powered(adapter, info, settings);
409 else if (new_power && mode_changed(settings, info->current_settings))
410 adapter_mode_changed(adapter, create_mode(settings));
412 old_pairable = mgmt_pairable(info->current_settings);
413 new_pairable = mgmt_pairable(settings);
415 /* Check for pairable change, except when powered went from True
416 * to False (in which case we always get all settings as False) */
417 if ((!old_power || new_power) && new_pairable != old_pairable)
418 btd_adapter_pairable_changed(adapter, mgmt_pairable(settings));
420 info->current_settings = settings;
423 static void bonding_complete(struct controller_info *info, bdaddr_t *bdaddr,
426 struct btd_adapter *adapter;
428 adapter = manager_find_adapter(&info->bdaddr);
430 adapter_bonding_complete(adapter, bdaddr, status);
433 static void mgmt_new_link_key(int sk, uint16_t index, void *buf, size_t len)
435 struct mgmt_ev_new_link_key *ev = buf;
436 struct controller_info *info;
438 if (len != sizeof(*ev)) {
439 error("mgmt_new_link_key event size mismatch (%zu != %zu)",
444 DBG("Controller %u new key of type %u pin_len %u", index,
445 ev->key.type, ev->key.pin_len);
447 if (index > max_index) {
448 error("Unexpected index %u in new_key event", index);
452 if (ev->key.pin_len > 16) {
453 error("Invalid PIN length (%u) in new_key event",
458 info = &controllers[index];
461 btd_event_link_key_notify(&info->bdaddr, &ev->key.addr.bdaddr,
462 ev->key.val, ev->key.type,
465 bonding_complete(info, &ev->key.addr.bdaddr, 0);
468 static void mgmt_device_connected(int sk, uint16_t index, void *buf, size_t len)
470 struct mgmt_ev_device_connected *ev = buf;
471 struct eir_data eir_data;
472 struct controller_info *info;
476 if (len < sizeof(*ev)) {
477 error("Too small device_connected event");
481 eir_len = bt_get_le16(&ev->eir_len);
482 if (len < sizeof(*ev) + eir_len) {
483 error("Too small device_connected event");
487 ba2str(&ev->addr.bdaddr, addr);
489 DBG("hci%u device %s connected eir_len %u", index, addr, eir_len);
491 if (index > max_index) {
492 error("Unexpected index %u in device_connected event", index);
496 info = &controllers[index];
498 memset(&eir_data, 0, sizeof(eir_data));
500 eir_parse(&eir_data, ev->eir, eir_len);
502 btd_event_conn_complete(&info->bdaddr, &ev->addr.bdaddr,
507 eir_data_free(&eir_data);
510 static void mgmt_device_disconnected(int sk, uint16_t index, void *buf,
513 struct mgmt_addr_info *ev = buf;
514 struct controller_info *info;
517 if (len < sizeof(*ev)) {
518 error("Too small device_disconnected event");
522 ba2str(&ev->bdaddr, addr);
524 DBG("hci%u device %s disconnected", index, addr);
526 if (index > max_index) {
527 error("Unexpected index %u in device_disconnected event", index);
531 info = &controllers[index];
533 btd_event_disconn_complete(&info->bdaddr, &ev->bdaddr);
536 static void mgmt_connect_failed(int sk, uint16_t index, void *buf, size_t len)
538 struct mgmt_ev_connect_failed *ev = buf;
539 struct controller_info *info;
542 if (len < sizeof(*ev)) {
543 error("Too small connect_failed event");
547 ba2str(&ev->addr.bdaddr, addr);
549 DBG("hci%u %s status %u", index, addr, ev->status);
551 if (index > max_index) {
552 error("Unexpected index %u in connect_failed event", index);
556 info = &controllers[index];
558 btd_event_conn_failed(&info->bdaddr, &ev->addr.bdaddr, ev->status);
560 /* In the case of security mode 3 devices */
561 bonding_complete(info, &ev->addr.bdaddr, ev->status);
564 static int mgmt_pincode_reply(int index, bdaddr_t *bdaddr, const char *pin,
567 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pin_code_reply)];
568 struct mgmt_hdr *hdr = (void *) buf;
572 ba2str(bdaddr, addr);
573 DBG("index %d addr %s pinlen %zu", index, addr, pin_len);
575 memset(buf, 0, sizeof(buf));
578 struct mgmt_cp_pin_code_neg_reply *cp;
580 hdr->opcode = htobs(MGMT_OP_PIN_CODE_NEG_REPLY);
581 hdr->len = htobs(sizeof(*cp));
582 hdr->index = htobs(index);
584 cp = (void *) &buf[sizeof(*hdr)];
585 bacpy(&cp->addr.bdaddr, bdaddr);
586 cp->addr.type = BDADDR_BREDR;
588 buf_len = sizeof(*hdr) + sizeof(*cp);
590 struct mgmt_cp_pin_code_reply *cp;
595 hdr->opcode = htobs(MGMT_OP_PIN_CODE_REPLY);
596 hdr->len = htobs(sizeof(*cp));
597 hdr->index = htobs(index);
599 cp = (void *) &buf[sizeof(*hdr)];
600 bacpy(&cp->addr.bdaddr, bdaddr);
601 cp->addr.type = BDADDR_BREDR;
602 cp->pin_len = pin_len;
603 memcpy(cp->pin_code, pin, pin_len);
605 buf_len = sizeof(*hdr) + sizeof(*cp);
608 if (write(mgmt_sock, buf, buf_len) < 0)
614 static void mgmt_pin_code_request(int sk, uint16_t index, void *buf, size_t len)
616 struct mgmt_ev_pin_code_request *ev = buf;
617 struct controller_info *info;
621 if (len < sizeof(*ev)) {
622 error("Too small pin_code_request event");
626 ba2str(&ev->addr.bdaddr, addr);
628 DBG("hci%u %s", index, addr);
630 if (index > max_index) {
631 error("Unexpected index %u in pin_code_request event", index);
635 info = &controllers[index];
637 err = btd_event_request_pin(&info->bdaddr, &ev->addr.bdaddr,
640 error("btd_event_request_pin: %s", strerror(-err));
641 mgmt_pincode_reply(index, &ev->addr.bdaddr, NULL, 0);
645 static int mgmt_confirm_reply(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
648 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_user_confirm_reply)];
649 struct mgmt_hdr *hdr = (void *) buf;
650 struct mgmt_cp_user_confirm_reply *cp;
653 ba2str(bdaddr, addr);
654 DBG("index %d addr %s success %d", index, addr, success);
656 memset(buf, 0, sizeof(buf));
659 hdr->opcode = htobs(MGMT_OP_USER_CONFIRM_REPLY);
661 hdr->opcode = htobs(MGMT_OP_USER_CONFIRM_NEG_REPLY);
663 hdr->len = htobs(sizeof(*cp));
664 hdr->index = htobs(index);
666 cp = (void *) &buf[sizeof(*hdr)];
667 bacpy(&cp->addr.bdaddr, bdaddr);
668 cp->addr.type = bdaddr_type;
670 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
676 static int mgmt_passkey_reply(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
679 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_user_passkey_reply)];
680 struct mgmt_hdr *hdr = (void *) buf;
684 ba2str(bdaddr, addr);
685 DBG("index %d addr %s passkey %06u", index, addr, passkey);
687 memset(buf, 0, sizeof(buf));
689 hdr->index = htobs(index);
690 if (passkey == INVALID_PASSKEY) {
691 struct mgmt_cp_user_passkey_neg_reply *cp;
693 hdr->opcode = htobs(MGMT_OP_USER_PASSKEY_NEG_REPLY);
694 hdr->len = htobs(sizeof(*cp));
696 cp = (void *) &buf[sizeof(*hdr)];
697 bacpy(&cp->addr.bdaddr, bdaddr);
698 cp->addr.type = bdaddr_type;
700 buf_len = sizeof(*hdr) + sizeof(*cp);
702 struct mgmt_cp_user_passkey_reply *cp;
704 hdr->opcode = htobs(MGMT_OP_USER_PASSKEY_REPLY);
705 hdr->len = htobs(sizeof(*cp));
707 cp = (void *) &buf[sizeof(*hdr)];
708 bacpy(&cp->addr.bdaddr, bdaddr);
709 cp->addr.type = bdaddr_type;
710 cp->passkey = htobl(passkey);
712 buf_len = sizeof(*hdr) + sizeof(*cp);
715 if (write(mgmt_sock, buf, buf_len) < 0)
721 static void mgmt_passkey_request(int sk, uint16_t index, void *buf, size_t len)
723 struct mgmt_ev_user_passkey_request *ev = buf;
724 struct controller_info *info;
728 if (len < sizeof(*ev)) {
729 error("Too small passkey_request event");
733 ba2str(&ev->addr.bdaddr, addr);
735 DBG("hci%u %s", index, addr);
737 if (index > max_index) {
738 error("Unexpected index %u in passkey_request event", index);
742 info = &controllers[index];
744 err = btd_event_user_passkey(&info->bdaddr, &ev->addr.bdaddr);
746 error("btd_event_user_passkey: %s", strerror(-err));
747 mgmt_passkey_reply(index, &ev->addr.bdaddr, ev->addr.type,
752 static void mgmt_passkey_notify(int sk, uint16_t index, void *buf, size_t len)
754 struct mgmt_ev_passkey_notify *ev = buf;
755 struct controller_info *info;
760 if (len < sizeof(*ev)) {
761 error("Too small passkey_notify event");
765 ba2str(&ev->addr.bdaddr, addr);
767 DBG("hci%u %s", index, addr);
769 if (index > max_index) {
770 error("Unexpected index %u in passkey_notify event", index);
774 info = &controllers[index];
776 passkey = bt_get_le32(&ev->passkey);
778 DBG("passkey %06u entered %u", passkey, ev->entered);
780 err = btd_event_user_notify(&info->bdaddr, &ev->addr.bdaddr,
783 error("btd_event_user_notify: %s", strerror(-err));
786 struct confirm_data {
792 static gboolean confirm_accept(gpointer user_data)
794 struct confirm_data *data = user_data;
795 struct controller_info *info = &controllers[data->index];
797 DBG("auto-accepting incoming pairing request");
799 if (data->index > max_index || !info->valid)
802 mgmt_confirm_reply(data->index, &data->bdaddr, data->type, TRUE);
807 static void mgmt_user_confirm_request(int sk, uint16_t index, void *buf,
810 struct mgmt_ev_user_confirm_request *ev = buf;
811 struct controller_info *info;
815 if (len < sizeof(*ev)) {
816 error("Too small user_confirm_request event");
820 ba2str(&ev->addr.bdaddr, addr);
822 DBG("hci%u %s confirm_hint %u", index, addr, ev->confirm_hint);
824 if (index > max_index) {
825 error("Unexpected index %u in user_confirm_request event",
830 if (ev->confirm_hint) {
831 struct confirm_data *data;
833 data = g_new0(struct confirm_data, 1);
835 bacpy(&data->bdaddr, &ev->addr.bdaddr);
836 data->type = ev->addr.type;
838 g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 1,
839 confirm_accept, data, g_free);
843 info = &controllers[index];
845 err = btd_event_user_confirm(&info->bdaddr, &ev->addr.bdaddr,
848 error("btd_event_user_confirm: %s", strerror(-err));
849 mgmt_confirm_reply(index, &ev->addr.bdaddr, ev->addr.type,
854 static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
856 if (uuid->type == SDP_UUID16)
857 sdp_uuid16_to_uuid128(uuid128, uuid);
858 else if (uuid->type == SDP_UUID32)
859 sdp_uuid32_to_uuid128(uuid128, uuid);
861 memcpy(uuid128, uuid, sizeof(*uuid));
864 static int mgmt_add_uuid(int index, uuid_t *uuid, uint8_t svc_hint)
866 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_add_uuid)];
867 struct mgmt_hdr *hdr = (void *) buf;
868 struct mgmt_cp_add_uuid *cp = (void *) &buf[sizeof(*hdr)];
869 struct controller_info *info = &controllers[index];
873 DBG("index %d", index);
875 if (info->pending_uuid) {
876 struct pending_uuid *pending = g_new0(struct pending_uuid, 1);
878 memcpy(&pending->uuid, uuid, sizeof(*uuid));
879 pending->svc_hint = svc_hint;
881 info->pending_uuids = g_slist_append(info->pending_uuids,
886 uuid_to_uuid128(&uuid128, uuid);
888 memset(buf, 0, sizeof(buf));
889 hdr->opcode = htobs(MGMT_OP_ADD_UUID);
890 hdr->len = htobs(sizeof(*cp));
891 hdr->index = htobs(index);
893 ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
894 htob128(&uint128, (uint128_t *) cp->uuid);
896 cp->svc_hint = svc_hint;
898 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
901 info->pending_uuid = TRUE;
906 static int mgmt_remove_uuid(int index, uuid_t *uuid)
908 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_remove_uuid)];
909 struct mgmt_hdr *hdr = (void *) buf;
910 struct mgmt_cp_remove_uuid *cp = (void *) &buf[sizeof(*hdr)];
914 DBG("index %d", index);
916 uuid_to_uuid128(&uuid128, uuid);
918 memset(buf, 0, sizeof(buf));
919 hdr->opcode = htobs(MGMT_OP_REMOVE_UUID);
920 hdr->len = htobs(sizeof(*cp));
921 hdr->index = htobs(index);
923 ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
924 htob128(&uint128, (uint128_t *) cp->uuid);
926 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
932 static int clear_uuids(int index)
936 memset(&uuid_any, 0, sizeof(uuid_any));
937 uuid_any.type = SDP_UUID128;
939 return mgmt_remove_uuid(index, &uuid_any);
942 static void read_index_list_complete(int sk, void *buf, size_t len)
944 struct mgmt_rp_read_index_list *rp = buf;
948 if (len < sizeof(*rp)) {
949 error("Too small read index list complete event");
953 num = btohs(bt_get_unaligned(&rp->num_controllers));
955 if (num * sizeof(uint16_t) + sizeof(*rp) != len) {
956 error("Incorrect packet size for index list event");
960 for (i = 0; i < num; i++) {
963 index = btohs(bt_get_unaligned(&rp->index[i]));
965 add_controller(index);
966 read_info(sk, index);
970 static int mgmt_set_powered(int index, gboolean powered)
972 struct controller_info *info = &controllers[index];
974 DBG("index %d powered %d pending_uuid %u", index, powered,
978 if (info->pending_uuid) {
979 info->pending_powered = TRUE;
983 info->pending_powered = FALSE;
986 return mgmt_set_mode(index, MGMT_OP_SET_POWERED, powered);
989 static int mgmt_set_name(int index, const char *name)
991 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_local_name)];
992 struct mgmt_hdr *hdr = (void *) buf;
993 struct mgmt_cp_set_local_name *cp = (void *) &buf[sizeof(*hdr)];
995 DBG("index %d, name %s", index, name);
997 memset(buf, 0, sizeof(buf));
998 hdr->opcode = htobs(MGMT_OP_SET_LOCAL_NAME);
999 hdr->len = htobs(sizeof(*cp));
1000 hdr->index = htobs(index);
1002 strncpy((char *) cp->name, name, sizeof(cp->name) - 1);
1004 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1010 static int mgmt_set_dev_class(int index, uint8_t major, uint8_t minor)
1012 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_dev_class)];
1013 struct mgmt_hdr *hdr = (void *) buf;
1014 struct mgmt_cp_set_dev_class *cp = (void *) &buf[sizeof(*hdr)];
1015 struct controller_info *info = &controllers[index];
1017 DBG("index %d major %u minor %u", index, major, minor);
1019 if (info->pending_uuid) {
1020 info->major = major;
1021 info->minor = minor;
1022 info->pending_class = TRUE;
1026 memset(buf, 0, sizeof(buf));
1027 hdr->opcode = htobs(MGMT_OP_SET_DEV_CLASS);
1028 hdr->len = htobs(sizeof(*cp));
1029 hdr->index = htobs(index);
1034 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1040 static void read_info_complete(int sk, uint16_t index, void *buf, size_t len)
1042 struct mgmt_rp_read_info *rp = buf;
1043 struct controller_info *info;
1044 struct btd_adapter *adapter;
1046 uint8_t mode, major, minor;
1049 if (len < sizeof(*rp)) {
1050 error("Too small read info complete event");
1054 if (index > max_index) {
1055 error("Unexpected index %u in read info complete", index);
1059 info = &controllers[index];
1061 bacpy(&info->bdaddr, &rp->bdaddr);
1062 info->version = rp->version;
1063 info->manufacturer = btohs(bt_get_unaligned(&rp->manufacturer));
1065 memcpy(&info->supported_settings, &rp->supported_settings,
1066 sizeof(info->supported_settings));
1067 memcpy(&info->current_settings, &rp->current_settings,
1068 sizeof(info->current_settings));
1070 memcpy(info->dev_class, rp->dev_class, sizeof(info->dev_class));
1072 ba2str(&info->bdaddr, addr);
1073 DBG("hci%u addr %s version %u manufacturer %u class 0x%02x%02x%02x\n",
1074 index, addr, info->version, info->manufacturer,
1075 info->dev_class[2], info->dev_class[1], info->dev_class[0]);
1076 DBG("hci%u settings", index);
1077 DBG("hci%u name %s", index, (char *) rp->name);
1078 DBG("hci%u short name %s", index, (char *) rp->short_name);
1082 adapter = btd_manager_register_adapter(index,
1083 mgmt_powered(info->current_settings));
1084 if (adapter == NULL) {
1085 error("mgmtops: unable to register adapter");
1089 update_settings(adapter, info->current_settings);
1091 name = btd_adapter_get_name(adapter);
1093 DBG("mgmtops setting name %s", name);
1096 mgmt_set_name(index, name);
1098 adapter_name_changed(adapter, (char *) rp->name);
1100 btd_adapter_get_class(adapter, &major, &minor);
1101 mgmt_set_dev_class(index, major, minor);
1103 btd_adapter_get_mode(adapter, &mode, NULL, NULL, NULL);
1104 if (mode == MODE_OFF && mgmt_powered(info->current_settings)) {
1105 mgmt_set_powered(index, FALSE);
1109 if (mode != MODE_OFF && !mgmt_powered(info->current_settings))
1110 mgmt_set_powered(index, TRUE);
1112 get_connections(sk, index);
1113 btd_adapter_start(adapter);
1116 btd_adapter_unref(adapter);
1119 static void disconnect_complete(int sk, uint16_t index, uint8_t status,
1120 void *buf, size_t len)
1122 struct mgmt_rp_disconnect *rp = buf;
1123 struct controller_info *info;
1126 if (len < sizeof(*rp)) {
1127 error("Too small disconnect complete event");
1131 ba2str(&rp->addr.bdaddr, addr);
1134 error("Disconnecting %s failed with status %u", addr, status);
1138 DBG("hci%d %s disconnected", index, addr);
1140 if (index > max_index) {
1141 error("Unexpected index %u in disconnect complete", index);
1145 info = &controllers[index];
1147 btd_event_disconn_complete(&info->bdaddr, &rp->addr.bdaddr);
1149 bonding_complete(info, &rp->addr.bdaddr, HCI_CONNECTION_TERMINATED);
1152 static void pair_device_complete(int sk, uint16_t index, uint8_t status,
1153 void *buf, size_t len)
1155 struct mgmt_rp_pair_device *rp = buf;
1156 struct controller_info *info;
1159 if (len < sizeof(*rp)) {
1160 error("Too small pair_device complete event");
1164 ba2str(&rp->addr.bdaddr, addr);
1166 DBG("hci%d %s pairing complete status %u", index, addr, status);
1168 if (index > max_index) {
1169 error("Unexpected index %u in pair_device complete", index);
1173 info = &controllers[index];
1175 bonding_complete(info, &rp->addr.bdaddr, status);
1178 static void get_connections_complete(int sk, uint16_t index, void *buf,
1181 struct mgmt_rp_get_connections *rp = buf;
1182 struct controller_info *info;
1185 if (len < sizeof(*rp)) {
1186 error("Too small get_connections complete event");
1190 if (len < (sizeof(*rp) + (rp->conn_count * sizeof(bdaddr_t)))) {
1191 error("Too small get_connections complete event");
1195 if (index > max_index) {
1196 error("Unexpected index %u in get_connections complete",
1201 info = &controllers[index];
1203 for (i = 0; i < rp->conn_count; i++) {
1204 bdaddr_t *bdaddr = g_memdup(&rp->addr[i], sizeof(bdaddr_t));
1205 info->connections = g_slist_append(info->connections, bdaddr);
1209 static void set_local_name_complete(int sk, uint16_t index, void *buf,
1212 struct mgmt_cp_set_local_name *rp = buf;
1213 struct controller_info *info;
1214 struct btd_adapter *adapter;
1216 if (len < sizeof(*rp)) {
1217 error("Too small set_local_name complete event");
1221 DBG("hci%d name %s", index, (char *) rp->name);
1223 if (index > max_index) {
1224 error("Unexpected index %u in set_local_name complete", index);
1228 info = &controllers[index];
1230 adapter = manager_find_adapter(&info->bdaddr);
1231 if (adapter == NULL) {
1232 DBG("Adapter not found");
1236 adapter_name_changed(adapter, (char *) rp->name);
1239 static void read_local_oob_data_complete(int sk, uint16_t index, void *buf,
1242 struct mgmt_rp_read_local_oob_data *rp = buf;
1243 struct btd_adapter *adapter;
1245 if (len != sizeof(*rp)) {
1246 error("read_local_oob_data_complete event size mismatch "
1247 "(%zu != %zu)", len, sizeof(*rp));
1251 if (index > max_index) {
1252 error("Unexpected index %u in read_local_oob_data_complete",
1257 DBG("hci%u", index);
1259 adapter = manager_find_adapter_by_id(index);
1262 oob_read_local_data_complete(adapter, rp->hash, rp->randomizer);
1265 static void start_discovery_complete(int sk, uint16_t index, uint8_t status,
1266 void *buf, size_t len)
1268 uint8_t *type = buf;
1269 struct btd_adapter *adapter;
1271 if (len != sizeof(*type)) {
1272 error("start_discovery_complete event size mismatch "
1273 "(%zu != %zu)", len, sizeof(*type));
1277 DBG("hci%u type %u status %u", index, *type, status);
1279 if (index > max_index) {
1280 error("Invalid index %u in start_discovery_complete", index);
1287 adapter = manager_find_adapter_by_id(index);
1289 /* Start discovery failed, inform upper layers. */
1290 adapter_set_discovering(adapter, FALSE);
1293 static void read_local_oob_data_failed(int sk, uint16_t index)
1295 struct btd_adapter *adapter;
1297 if (index > max_index) {
1298 error("Unexpected index %u in read_local_oob_data_failed",
1303 DBG("hci%u", index);
1305 adapter = manager_find_adapter_by_id(index);
1308 oob_read_local_data_complete(adapter, NULL, NULL);
1311 static void handle_pending_uuids(uint16_t index)
1313 struct controller_info *info;
1314 struct pending_uuid *pending;
1316 DBG("index %d", index);
1318 info = &controllers[index];
1320 info->pending_uuid = FALSE;
1322 if (g_slist_length(info->pending_uuids) == 0) {
1323 if (info->pending_class) {
1324 info->pending_class = FALSE;
1325 mgmt_set_dev_class(index, info->major, info->minor);
1328 if (info->pending_powered) {
1329 info->pending_powered = FALSE;
1330 mgmt_set_powered(index, TRUE);
1336 pending = info->pending_uuids->data;
1338 mgmt_add_uuid(index, &pending->uuid, pending->svc_hint);
1340 info->pending_uuids = g_slist_remove(info->pending_uuids, pending);
1344 static void mgmt_add_uuid_complete(int sk, uint16_t index, void *buf,
1347 DBG("index %d", index);
1349 if (index > max_index) {
1350 error("Unexpected index %u in add_uuid_complete event", index);
1354 handle_pending_uuids(index);
1357 static void mgmt_cmd_complete(int sk, uint16_t index, void *buf, size_t len)
1359 struct mgmt_ev_cmd_complete *ev = buf;
1364 if (len < sizeof(*ev)) {
1365 error("Too small management command complete event packet");
1369 opcode = btohs(bt_get_unaligned(&ev->opcode));
1374 case MGMT_OP_READ_VERSION:
1375 read_version_complete(sk, ev->data, len);
1377 case MGMT_OP_READ_INDEX_LIST:
1378 read_index_list_complete(sk, ev->data, len);
1380 case MGMT_OP_READ_INFO:
1381 read_info_complete(sk, index, ev->data, len);
1383 case MGMT_OP_SET_POWERED:
1384 mgmt_new_settings(sk, index, ev->data, len);
1386 case MGMT_OP_SET_DISCOVERABLE:
1387 mgmt_new_settings(sk, index, ev->data, len);
1389 case MGMT_OP_SET_CONNECTABLE:
1390 mgmt_new_settings(sk, index, ev->data, len);
1392 case MGMT_OP_SET_PAIRABLE:
1393 mgmt_new_settings(sk, index, ev->data, len);
1395 case MGMT_OP_SET_SSP:
1396 DBG("set_ssp complete");
1397 #ifdef __TIZEN_PATCH__
1398 mgmt_new_settings(sk, index, ev->data, len);
1401 case MGMT_OP_SET_LE:
1402 DBG("set_le complete");
1403 #ifdef __TIZEN_PATCH__
1404 mgmt_new_settings(sk, index, ev->data, len);
1407 case MGMT_OP_ADD_UUID:
1408 mgmt_add_uuid_complete(sk, index, ev->data, len);
1410 case MGMT_OP_REMOVE_UUID:
1411 DBG("remove_uuid complete");
1413 case MGMT_OP_SET_DEV_CLASS:
1414 DBG("set_dev_class complete");
1416 case MGMT_OP_LOAD_LINK_KEYS:
1417 DBG("load_link_keys complete");
1419 case MGMT_OP_CANCEL_PAIR_DEVICE:
1420 DBG("cancel_pair_device complete");
1422 case MGMT_OP_UNPAIR_DEVICE:
1423 DBG("unpair_device complete");
1425 case MGMT_OP_DISCONNECT:
1426 DBG("disconnect complete");
1427 disconnect_complete(sk, index, ev->status, ev->data, len);
1429 case MGMT_OP_GET_CONNECTIONS:
1430 get_connections_complete(sk, index, ev->data, len);
1432 case MGMT_OP_PIN_CODE_REPLY:
1433 DBG("pin_code_reply complete");
1435 case MGMT_OP_PIN_CODE_NEG_REPLY:
1436 DBG("pin_code_neg_reply complete");
1438 case MGMT_OP_SET_IO_CAPABILITY:
1439 DBG("set_io_capability complete");
1441 case MGMT_OP_PAIR_DEVICE:
1442 pair_device_complete(sk, index, ev->status, ev->data, len);
1444 case MGMT_OP_USER_CONFIRM_REPLY:
1445 DBG("user_confirm_reply complete");
1447 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1448 DBG("user_confirm_net_reply complete");
1450 case MGMT_OP_SET_LOCAL_NAME:
1451 set_local_name_complete(sk, index, ev->data, len);
1453 case MGMT_OP_READ_LOCAL_OOB_DATA:
1454 read_local_oob_data_complete(sk, index, ev->data, len);
1456 case MGMT_OP_ADD_REMOTE_OOB_DATA:
1457 DBG("add_remote_oob_data complete");
1459 case MGMT_OP_REMOVE_REMOTE_OOB_DATA:
1460 DBG("remove_remote_oob_data complete");
1462 case MGMT_OP_BLOCK_DEVICE:
1463 DBG("block_device complete");
1465 case MGMT_OP_UNBLOCK_DEVICE:
1466 DBG("unblock_device complete");
1468 case MGMT_OP_SET_FAST_CONNECTABLE:
1469 DBG("set_fast_connectable complete");
1471 case MGMT_OP_START_DISCOVERY:
1472 start_discovery_complete(sk, index, ev->status, ev->data, len);
1474 case MGMT_OP_STOP_DISCOVERY:
1475 DBG("stop_discovery complete");
1477 case MGMT_OP_SET_DEVICE_ID:
1478 DBG("set_did complete");
1481 error("Unknown command complete for opcode %u", opcode);
1486 static void mgmt_add_uuid_busy(int sk, uint16_t index)
1488 struct controller_info *info;
1490 DBG("index %d", index);
1492 info = &controllers[index];
1493 info->pending_cod_change = TRUE;
1496 static void mgmt_cmd_status(int sk, uint16_t index, void *buf, size_t len)
1498 struct mgmt_ev_cmd_status *ev = buf;
1501 if (len < sizeof(*ev)) {
1502 error("Too small management command status event packet");
1506 opcode = btohs(bt_get_unaligned(&ev->opcode));
1509 DBG("%s (0x%04x) cmd_status %u", mgmt_opstr(opcode), opcode,
1515 case MGMT_OP_READ_LOCAL_OOB_DATA:
1516 read_local_oob_data_failed(sk, index);
1518 case MGMT_OP_ADD_UUID:
1519 if (ev->status == MGMT_STATUS_BUSY) {
1520 mgmt_add_uuid_busy(sk, index);
1526 error("hci%u: %s (0x%04x) failed: %s (0x%02x)", index,
1527 mgmt_opstr(opcode), opcode, mgmt_errstr(ev->status),
1531 static void mgmt_controller_error(int sk, uint16_t index, void *buf, size_t len)
1533 struct mgmt_ev_controller_error *ev = buf;
1535 if (len < sizeof(*ev)) {
1536 error("Too small management controller error event packet");
1540 DBG("index %u error_code %u", index, ev->error_code);
1543 static void mgmt_auth_failed(int sk, uint16_t index, void *buf, size_t len)
1545 struct controller_info *info;
1546 struct mgmt_ev_auth_failed *ev = buf;
1548 if (len < sizeof(*ev)) {
1549 error("Too small mgmt_auth_failed event packet");
1553 DBG("hci%u auth failed status %u", index, ev->status);
1555 if (index > max_index) {
1556 error("Unexpected index %u in auth_failed event", index);
1560 info = &controllers[index];
1562 bonding_complete(info, &ev->addr.bdaddr, ev->status);
1565 static void mgmt_local_name_changed(int sk, uint16_t index, void *buf, size_t len)
1567 struct mgmt_cp_set_local_name *ev = buf;
1568 struct controller_info *info;
1569 struct btd_adapter *adapter;
1571 if (len < sizeof(*ev)) {
1572 error("Too small mgmt_local_name_changed event packet");
1576 DBG("hci%u local name changed: %s", index, (char *) ev->name);
1578 if (index > max_index) {
1579 error("Unexpected index %u in name_changed event", index);
1583 info = &controllers[index];
1585 adapter = manager_find_adapter(&info->bdaddr);
1587 adapter_name_changed(adapter, (char *) ev->name);
1590 static void mgmt_device_found(int sk, uint16_t index, void *buf, size_t len)
1592 struct mgmt_ev_device_found *ev = buf;
1593 struct controller_info *info;
1598 gboolean confirm_name;
1600 if (len < sizeof(*ev)) {
1601 error("mgmt_device_found too short (%zu bytes)", len);
1605 eir_len = bt_get_le16(&ev->eir_len);
1606 if (len != sizeof(*ev) + eir_len) {
1607 error("mgmt_device_found event size mismatch (%zu != %zu)",
1608 len, sizeof(*ev) + eir_len);
1612 if (index > max_index) {
1613 error("Unexpected index %u in device_found event", index);
1617 info = &controllers[index];
1624 flags = btohl(ev->flags);
1626 ba2str(&ev->addr.bdaddr, addr);
1627 DBG("hci%u addr %s, rssi %d flags 0x%04x eir_len %u",
1628 index, addr, ev->rssi, flags, eir_len);
1630 if (flags & MGMT_DEV_FOUND_LEGACY_PAIRING)
1631 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1634 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1637 confirm_name = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
1639 btd_event_device_found(&info->bdaddr, &ev->addr.bdaddr,
1641 ev->rssi, confirm_name,
1645 static void mgmt_discovering(int sk, uint16_t index, void *buf, size_t len)
1647 struct mgmt_ev_discovering *ev = buf;
1648 struct controller_info *info;
1649 struct btd_adapter *adapter;
1651 if (len < sizeof(*ev)) {
1652 error("Too small discovering event");
1656 DBG("Controller %u type %u discovering %u", index,
1657 ev->type, ev->discovering);
1659 if (index > max_index) {
1660 error("Unexpected index %u in discovering event", index);
1664 info = &controllers[index];
1666 adapter = manager_find_adapter(&info->bdaddr);
1670 adapter_set_discovering(adapter, ev->discovering);
1673 static void mgmt_device_blocked(int sk, uint16_t index, void *buf, size_t len)
1675 struct controller_info *info;
1676 struct mgmt_ev_device_blocked *ev = buf;
1679 if (len < sizeof(*ev)) {
1680 error("Too small mgmt_device_blocked event packet");
1684 ba2str(&ev->addr.bdaddr, addr);
1685 DBG("Device blocked, index %u, addr %s", index, addr);
1687 if (index > max_index) {
1688 error("Unexpected index %u in device_blocked event", index);
1692 info = &controllers[index];
1694 btd_event_device_blocked(&info->bdaddr, &ev->addr.bdaddr);
1697 static void mgmt_device_unblocked(int sk, uint16_t index, void *buf, size_t len)
1699 struct controller_info *info;
1700 struct mgmt_ev_device_unblocked *ev = buf;
1703 if (len < sizeof(*ev)) {
1704 error("Too small mgmt_device_unblocked event packet");
1708 ba2str(&ev->addr.bdaddr, addr);
1709 DBG("Device unblocked, index %u, addr %s", index, addr);
1711 if (index > max_index) {
1712 error("Unexpected index %u in device_unblocked event", index);
1716 info = &controllers[index];
1718 btd_event_device_unblocked(&info->bdaddr, &ev->addr.bdaddr);
1721 static void mgmt_device_unpaired(int sk, uint16_t index, void *buf, size_t len)
1723 struct controller_info *info;
1724 struct mgmt_ev_device_unpaired *ev = buf;
1727 if (len < sizeof(*ev)) {
1728 error("Too small mgmt_device_unpaired event packet");
1732 ba2str(&ev->addr.bdaddr, addr);
1733 DBG("Device upaired, index %u, addr %s", index, addr);
1735 if (index > max_index) {
1736 error("Unexpected index %u in device_unpaired event", index);
1740 info = &controllers[index];
1742 btd_event_device_unpaired(&info->bdaddr, &ev->addr.bdaddr);
1745 static void mgmt_new_ltk(int sk, uint16_t index, void *buf, size_t len)
1747 struct mgmt_ev_new_long_term_key *ev = buf;
1748 struct controller_info *info;
1750 if (len != sizeof(*ev)) {
1751 error("mgmt_new_ltk event size mismatch (%zu != %zu)",
1756 DBG("Controller %u new LTK authenticated %u enc_size %u", index,
1757 ev->key.authenticated, ev->key.enc_size);
1759 if (index > max_index) {
1760 error("Unexpected index %u in new_key event", index);
1764 info = &controllers[index];
1766 if (ev->store_hint) {
1767 btd_event_ltk_notify(&info->bdaddr, &ev->key.addr.bdaddr,
1768 ev->key.addr.type, ev->key.val, ev->key.master,
1769 ev->key.authenticated, ev->key.enc_size,
1770 ev->key.ediv, ev->key.rand);
1774 bonding_complete(info, &ev->key.addr.bdaddr, 0);
1777 static void mgmt_cod_changed(int sk, uint16_t index)
1779 struct controller_info *info;
1781 DBG("index %d", index);
1783 if (index > max_index) {
1784 error("Unexpected index %u in mgmt_cod_changed event", index);
1788 info = &controllers[index];
1790 if (info->pending_cod_change) {
1791 info->pending_cod_change = FALSE;
1792 handle_pending_uuids(index);
1796 static gboolean mgmt_event(GIOChannel *io, GIOCondition cond, gpointer user_data)
1798 char buf[MGMT_BUF_SIZE];
1799 struct mgmt_hdr *hdr = (void *) buf;
1802 uint16_t len, opcode, index;
1804 DBG("cond %d", cond);
1806 if (cond & G_IO_NVAL)
1809 sk = g_io_channel_unix_get_fd(io);
1811 if (cond & (G_IO_ERR | G_IO_HUP)) {
1812 error("Error on management socket");
1816 ret = read(sk, buf, sizeof(buf));
1818 error("Unable to read from management socket: %s (%d)",
1819 strerror(errno), errno);
1823 DBG("Received %zd bytes from management socket", ret);
1825 if (ret < MGMT_HDR_SIZE) {
1826 error("Too small Management packet");
1830 opcode = btohs(bt_get_unaligned(&hdr->opcode));
1831 len = btohs(bt_get_unaligned(&hdr->len));
1832 index = btohs(bt_get_unaligned(&hdr->index));
1834 if (ret != MGMT_HDR_SIZE + len) {
1835 error("Packet length mismatch. ret %zd len %u", ret, len);
1840 case MGMT_EV_CMD_COMPLETE:
1841 mgmt_cmd_complete(sk, index, buf + MGMT_HDR_SIZE, len);
1843 case MGMT_EV_CMD_STATUS:
1844 mgmt_cmd_status(sk, index, buf + MGMT_HDR_SIZE, len);
1846 case MGMT_EV_CONTROLLER_ERROR:
1847 mgmt_controller_error(sk, index, buf + MGMT_HDR_SIZE, len);
1849 case MGMT_EV_INDEX_ADDED:
1850 mgmt_index_added(sk, index);
1852 case MGMT_EV_INDEX_REMOVED:
1853 mgmt_index_removed(sk, index);
1855 case MGMT_EV_NEW_SETTINGS:
1856 mgmt_new_settings(sk, index, buf + MGMT_HDR_SIZE, len);
1858 case MGMT_EV_CLASS_OF_DEV_CHANGED:
1859 mgmt_cod_changed(sk, index);
1861 case MGMT_EV_NEW_LINK_KEY:
1862 mgmt_new_link_key(sk, index, buf + MGMT_HDR_SIZE, len);
1864 case MGMT_EV_DEVICE_CONNECTED:
1865 mgmt_device_connected(sk, index, buf + MGMT_HDR_SIZE, len);
1867 case MGMT_EV_DEVICE_DISCONNECTED:
1868 mgmt_device_disconnected(sk, index, buf + MGMT_HDR_SIZE, len);
1870 case MGMT_EV_CONNECT_FAILED:
1871 mgmt_connect_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1873 case MGMT_EV_PIN_CODE_REQUEST:
1874 mgmt_pin_code_request(sk, index, buf + MGMT_HDR_SIZE, len);
1876 case MGMT_EV_USER_CONFIRM_REQUEST:
1877 mgmt_user_confirm_request(sk, index, buf + MGMT_HDR_SIZE, len);
1879 case MGMT_EV_AUTH_FAILED:
1880 mgmt_auth_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1882 case MGMT_EV_LOCAL_NAME_CHANGED:
1883 mgmt_local_name_changed(sk, index, buf + MGMT_HDR_SIZE, len);
1885 case MGMT_EV_DEVICE_FOUND:
1886 mgmt_device_found(sk, index, buf + MGMT_HDR_SIZE, len);
1888 case MGMT_EV_DISCOVERING:
1889 mgmt_discovering(sk, index, buf + MGMT_HDR_SIZE, len);
1891 case MGMT_EV_DEVICE_BLOCKED:
1892 mgmt_device_blocked(sk, index, buf + MGMT_HDR_SIZE, len);
1894 case MGMT_EV_DEVICE_UNBLOCKED:
1895 mgmt_device_unblocked(sk, index, buf + MGMT_HDR_SIZE, len);
1897 case MGMT_EV_DEVICE_UNPAIRED:
1898 mgmt_device_unpaired(sk, index, buf + MGMT_HDR_SIZE, len);
1900 case MGMT_EV_USER_PASSKEY_REQUEST:
1901 mgmt_passkey_request(sk, index, buf + MGMT_HDR_SIZE, len);
1903 case MGMT_EV_PASSKEY_NOTIFY:
1904 mgmt_passkey_notify(sk, index, buf + MGMT_HDR_SIZE, len);
1906 case MGMT_EV_NEW_LONG_TERM_KEY:
1907 mgmt_new_ltk(sk, index, buf + MGMT_HDR_SIZE, len);
1910 error("Unknown Management opcode %u (index %u)", opcode, index);
1917 static int mgmt_setup(void)
1919 struct mgmt_hdr hdr;
1920 struct sockaddr_hci addr;
1922 GIOCondition condition;
1925 dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
1929 memset(&addr, 0, sizeof(addr));
1930 addr.hci_family = AF_BLUETOOTH;
1931 addr.hci_dev = HCI_DEV_NONE;
1932 addr.hci_channel = HCI_CHANNEL_CONTROL;
1934 if (bind(dd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1939 memset(&hdr, 0, sizeof(hdr));
1940 hdr.opcode = htobs(MGMT_OP_READ_VERSION);
1941 hdr.index = htobs(MGMT_INDEX_NONE);
1942 if (write(dd, &hdr, sizeof(hdr)) < 0) {
1947 io = g_io_channel_unix_new(dd);
1948 condition = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
1949 mgmt_watch = g_io_add_watch(io, condition, mgmt_event, NULL);
1950 g_io_channel_unref(io);
1954 info("Bluetooth Management interface initialized");
1963 static void mgmt_cleanup(void)
1965 g_free(controllers);
1969 if (mgmt_sock >= 0) {
1974 if (mgmt_watch > 0) {
1975 g_source_remove(mgmt_watch);
1980 static int mgmt_start_discovery(int index)
1982 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
1983 struct mgmt_hdr *hdr = (void *) buf;
1984 struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
1985 struct controller_info *info = &controllers[index];
1987 DBG("index %d", index);
1989 info->discov_type = 0;
1991 if (mgmt_bredr(info->current_settings))
1992 hci_set_bit(BDADDR_BREDR, &info->discov_type);
1994 if (mgmt_low_energy(info->current_settings)) {
1995 hci_set_bit(BDADDR_LE_PUBLIC, &info->discov_type);
1996 hci_set_bit(BDADDR_LE_RANDOM, &info->discov_type);
1999 memset(buf, 0, sizeof(buf));
2000 hdr->opcode = htobs(MGMT_OP_START_DISCOVERY);
2001 hdr->len = htobs(sizeof(*cp));
2002 hdr->index = htobs(index);
2004 cp->type = info->discov_type;
2006 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2012 static int mgmt_stop_discovery(int index)
2014 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
2015 struct mgmt_hdr *hdr = (void *) buf;
2016 struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
2017 struct controller_info *info = &controllers[index];
2019 DBG("index %d", index);
2021 memset(buf, 0, sizeof(buf));
2022 hdr->opcode = htobs(MGMT_OP_STOP_DISCOVERY);
2023 hdr->len = htobs(sizeof(*cp));
2024 hdr->index = htobs(index);
2026 cp->type = info->discov_type;
2028 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2034 static int mgmt_set_fast_connectable(int index, gboolean enable)
2036 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_mode)];
2037 struct mgmt_hdr *hdr = (void *) buf;
2038 struct mgmt_mode *cp = (void *) &buf[sizeof(*hdr)];
2040 DBG("index %d enable %d", index, enable);
2042 memset(buf, 0, sizeof(buf));
2043 hdr->opcode = htobs(MGMT_OP_SET_FAST_CONNECTABLE);
2044 hdr->len = htobs(sizeof(*cp));
2045 hdr->index = htobs(index);
2049 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2055 static int mgmt_read_clock(int index, bdaddr_t *bdaddr, int which, int timeout,
2056 uint32_t *clock, uint16_t *accuracy)
2060 ba2str(bdaddr, addr);
2061 DBG("index %d addr %s which %d timeout %d", index, addr, which,
2067 static int mgmt_read_bdaddr(int index, bdaddr_t *bdaddr)
2070 struct controller_info *info = &controllers[index];
2072 ba2str(&info->bdaddr, addr);
2073 DBG("index %d addr %s", index, addr);
2078 bacpy(bdaddr, &info->bdaddr);
2083 static int mgmt_block_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2085 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_block_device)];
2086 struct mgmt_hdr *hdr = (void *) buf;
2087 struct mgmt_cp_block_device *cp;
2091 ba2str(bdaddr, addr);
2092 DBG("index %d addr %s", index, addr);
2094 memset(buf, 0, sizeof(buf));
2096 hdr->opcode = htobs(MGMT_OP_BLOCK_DEVICE);
2097 hdr->len = htobs(sizeof(*cp));
2098 hdr->index = htobs(index);
2100 cp = (void *) &buf[sizeof(*hdr)];
2101 bacpy(&cp->addr.bdaddr, bdaddr);
2102 cp->addr.type = bdaddr_type;
2104 buf_len = sizeof(*hdr) + sizeof(*cp);
2106 if (write(mgmt_sock, buf, buf_len) < 0)
2112 static int mgmt_unblock_device(int index, bdaddr_t *bdaddr,
2113 uint8_t bdaddr_type)
2115 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_unblock_device)];
2116 struct mgmt_hdr *hdr = (void *) buf;
2117 struct mgmt_cp_unblock_device *cp;
2121 ba2str(bdaddr, addr);
2122 DBG("index %d addr %s", index, addr);
2124 memset(buf, 0, sizeof(buf));
2126 hdr->opcode = htobs(MGMT_OP_UNBLOCK_DEVICE);
2127 hdr->len = htobs(sizeof(*cp));
2128 hdr->index = htobs(index);
2130 cp = (void *) &buf[sizeof(*hdr)];
2131 bacpy(&cp->addr.bdaddr, bdaddr);
2132 cp->addr.type = bdaddr_type;
2134 buf_len = sizeof(*hdr) + sizeof(*cp);
2136 if (write(mgmt_sock, buf, buf_len) < 0)
2142 static int mgmt_get_conn_list(int index, GSList **conns)
2144 struct controller_info *info = &controllers[index];
2146 DBG("index %d", index);
2148 *conns = info->connections;
2149 info->connections = NULL;
2154 static int mgmt_disconnect(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2156 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_disconnect)];
2157 struct mgmt_hdr *hdr = (void *) buf;
2158 struct mgmt_cp_disconnect *cp = (void *) &buf[sizeof(*hdr)];
2161 ba2str(bdaddr, addr);
2162 DBG("index %d %s", index, addr);
2164 memset(buf, 0, sizeof(buf));
2165 hdr->opcode = htobs(MGMT_OP_DISCONNECT);
2166 hdr->len = htobs(sizeof(*cp));
2167 hdr->index = htobs(index);
2169 bacpy(&cp->addr.bdaddr, bdaddr);
2170 cp->addr.type = bdaddr_type;
2172 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2173 error("write: %s (%d)", strerror(errno), errno);
2178 static int mgmt_unpair_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2180 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_unpair_device)];
2181 struct mgmt_hdr *hdr = (void *) buf;
2182 struct mgmt_cp_unpair_device *cp = (void *) &buf[sizeof(*hdr)];
2185 ba2str(bdaddr, addr);
2186 DBG("index %d addr %s", index, addr);
2188 memset(buf, 0, sizeof(buf));
2189 hdr->opcode = htobs(MGMT_OP_UNPAIR_DEVICE);
2190 hdr->len = htobs(sizeof(*cp));
2191 hdr->index = htobs(index);
2193 bacpy(&cp->addr.bdaddr, bdaddr);
2194 cp->addr.type = bdaddr_type;
2197 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2203 static int mgmt_encrypt_link(int index, bdaddr_t *dst, bt_hci_result_t cb,
2209 DBG("index %d addr %s", index, addr);
2214 static int mgmt_set_did(int index, uint16_t vendor, uint16_t product,
2215 uint16_t version, uint16_t source)
2217 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_device_id)];
2218 struct mgmt_hdr *hdr = (void *) buf;
2219 struct mgmt_cp_set_device_id *cp = (void *) &buf[sizeof(*hdr)];
2221 DBG("index %d source %x vendor %x product %x version %x",
2222 index, source, vendor, product, version);
2224 memset(buf, 0, sizeof(buf));
2225 hdr->opcode = htobs(MGMT_OP_SET_DEVICE_ID);
2226 hdr->len = htobs(sizeof(*cp));
2227 hdr->index = htobs(index);
2229 cp->source = htobs(source);
2230 cp->vendor = htobs(vendor);
2231 cp->product = htobs(product);
2232 cp->version = htobs(version);
2234 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2240 static int mgmt_disable_cod_cache(int index)
2242 DBG("index %d", index);
2244 /* The cache control is handled automatically for mgmt */
2248 static int mgmt_restore_powered(int index)
2250 DBG("index %d", index);
2254 static int mgmt_load_link_keys(int index, GSList *keys, gboolean debug_keys)
2257 struct mgmt_hdr *hdr;
2258 struct mgmt_cp_load_link_keys *cp;
2259 struct mgmt_link_key_info *key;
2260 size_t key_count, cp_size;
2264 key_count = g_slist_length(keys);
2266 DBG("index %d keys %zu debug_keys %d", index, key_count, debug_keys);
2268 cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2270 buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2275 hdr->opcode = htobs(MGMT_OP_LOAD_LINK_KEYS);
2276 hdr->len = htobs(cp_size);
2277 hdr->index = htobs(index);
2279 cp = (void *) (buf + sizeof(*hdr));
2280 cp->debug_keys = debug_keys;
2281 cp->key_count = htobs(key_count);
2283 for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2284 struct link_key_info *info = l->data;
2286 bacpy(&key->addr.bdaddr, &info->bdaddr);
2287 key->addr.type = BDADDR_BREDR;
2288 key->type = info->type;
2289 memcpy(key->val, info->key, 16);
2290 key->pin_len = info->pin_len;
2293 if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2303 static int mgmt_set_io_capability(int index, uint8_t io_capability)
2305 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_io_capability)];
2306 struct mgmt_hdr *hdr = (void *) buf;
2307 struct mgmt_cp_set_io_capability *cp = (void *) &buf[sizeof(*hdr)];
2309 DBG("hci%d io_capability 0x%02x", index, io_capability);
2311 memset(buf, 0, sizeof(buf));
2312 hdr->opcode = htobs(MGMT_OP_SET_IO_CAPABILITY);
2313 hdr->len = htobs(sizeof(*cp));
2314 hdr->index = htobs(index);
2316 cp->io_capability = io_capability;
2318 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2324 static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t addr_type, uint8_t io_cap)
2326 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pair_device)];
2327 struct mgmt_hdr *hdr = (void *) buf;
2328 struct mgmt_cp_pair_device *cp = (void *) &buf[sizeof(*hdr)];
2331 ba2str(bdaddr, addr);
2332 DBG("hci%d bdaddr %s io_cap 0x%02x", index, addr, io_cap);
2334 memset(buf, 0, sizeof(buf));
2335 hdr->opcode = htobs(MGMT_OP_PAIR_DEVICE);
2336 hdr->len = htobs(sizeof(*cp));
2337 hdr->index = htobs(index);
2339 bacpy(&cp->addr.bdaddr, bdaddr);
2340 cp->addr.type = addr_type;
2341 cp->io_cap = io_cap;
2343 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2349 static int mgmt_cancel_bonding(int index, bdaddr_t *bdaddr)
2351 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_addr_info)];
2352 struct mgmt_hdr *hdr = (void *) buf;
2353 struct mgmt_addr_info *cp = (void *) &buf[sizeof(*hdr)];
2356 ba2str(bdaddr, addr);
2357 DBG("hci%d bdaddr %s", index, addr);
2359 memset(buf, 0, sizeof(buf));
2360 hdr->opcode = htobs(MGMT_OP_CANCEL_PAIR_DEVICE);
2361 hdr->len = htobs(sizeof(*cp));
2362 hdr->index = htobs(index);
2364 bacpy(&cp->bdaddr, bdaddr);
2366 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2372 static int mgmt_read_local_oob_data(int index)
2374 struct mgmt_hdr hdr;
2376 DBG("hci%d", index);
2378 hdr.opcode = htobs(MGMT_OP_READ_LOCAL_OOB_DATA);
2380 hdr.index = htobs(index);
2382 if (write(mgmt_sock, &hdr, sizeof(hdr)) < 0)
2388 static int mgmt_add_remote_oob_data(int index, bdaddr_t *bdaddr,
2389 uint8_t *hash, uint8_t *randomizer)
2391 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_add_remote_oob_data)];
2392 struct mgmt_hdr *hdr = (void *) buf;
2393 struct mgmt_cp_add_remote_oob_data *cp = (void *) &buf[sizeof(*hdr)];
2396 ba2str(bdaddr, addr);
2397 DBG("hci%d bdaddr %s", index, addr);
2399 memset(buf, 0, sizeof(buf));
2401 hdr->opcode = htobs(MGMT_OP_ADD_REMOTE_OOB_DATA);
2402 hdr->index = htobs(index);
2403 hdr->len = htobs(sizeof(*cp));
2405 bacpy(&cp->addr.bdaddr, bdaddr);
2406 memcpy(cp->hash, hash, 16);
2407 memcpy(cp->randomizer, randomizer, 16);
2409 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2415 static int mgmt_remove_remote_oob_data(int index, bdaddr_t *bdaddr)
2417 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_remove_remote_oob_data)];
2418 struct mgmt_hdr *hdr = (void *) buf;
2419 struct mgmt_cp_remove_remote_oob_data *cp = (void *) &buf[sizeof(*hdr)];
2422 ba2str(bdaddr, addr);
2423 DBG("hci%d bdaddr %s", index, addr);
2425 memset(buf, 0, sizeof(buf));
2427 hdr->opcode = htobs(MGMT_OP_REMOVE_REMOTE_OOB_DATA);
2428 hdr->index = htobs(index);
2429 hdr->len = htobs(sizeof(*cp));
2431 bacpy(&cp->addr.bdaddr, bdaddr);
2433 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2439 static int mgmt_confirm_name(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
2440 gboolean name_known)
2442 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_confirm_name)];
2443 struct mgmt_hdr *hdr = (void *) buf;
2444 struct mgmt_cp_confirm_name *cp = (void *) &buf[sizeof(*hdr)];
2447 ba2str(bdaddr, addr);
2448 DBG("hci%d bdaddr %s name_known %u", index, addr, name_known);
2450 memset(buf, 0, sizeof(buf));
2452 hdr->opcode = htobs(MGMT_OP_CONFIRM_NAME);
2453 hdr->index = htobs(index);
2454 hdr->len = htobs(sizeof(*cp));
2456 bacpy(&cp->addr.bdaddr, bdaddr);
2457 cp->addr.type = bdaddr_type;
2458 cp->name_known = name_known;
2460 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2466 static int mgmtops_load_ltks(int index, GSList *keys)
2469 struct mgmt_hdr *hdr;
2470 struct mgmt_cp_load_long_term_keys *cp;
2471 struct mgmt_ltk_info *key;
2472 size_t key_count, cp_size;
2476 key_count = g_slist_length(keys);
2478 DBG("index %d keys %zu", index, key_count);
2480 cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2482 buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2487 hdr->opcode = htobs(MGMT_OP_LOAD_LONG_TERM_KEYS);
2488 hdr->len = htobs(cp_size);
2489 hdr->index = htobs(index);
2491 cp = (void *) (buf + sizeof(*hdr));
2492 cp->key_count = htobs(key_count);
2494 for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2495 struct smp_ltk_info *info = l->data;
2497 bacpy(&key->addr.bdaddr, &info->bdaddr);
2498 key->addr.type = info->bdaddr_type;
2499 memcpy(key->val, info->val, sizeof(info->val));
2500 memcpy(key->rand, info->rand, sizeof(info->rand));
2501 memcpy(&key->ediv, &info->ediv, sizeof(key->ediv));
2502 key->authenticated = info->authenticated;
2503 key->master = info->master;
2504 key->enc_size = info->enc_size;
2507 if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2517 static struct btd_adapter_ops mgmt_ops = {
2518 .setup = mgmt_setup,
2519 .cleanup = mgmt_cleanup,
2520 .set_powered = mgmt_set_powered,
2521 .set_discoverable = mgmt_set_discoverable,
2522 .set_pairable = mgmt_set_pairable,
2523 .start_discovery = mgmt_start_discovery,
2524 .stop_discovery = mgmt_stop_discovery,
2525 .set_name = mgmt_set_name,
2526 .set_dev_class = mgmt_set_dev_class,
2527 .set_fast_connectable = mgmt_set_fast_connectable,
2528 .read_clock = mgmt_read_clock,
2529 .read_bdaddr = mgmt_read_bdaddr,
2530 .block_device = mgmt_block_device,
2531 .unblock_device = mgmt_unblock_device,
2532 .get_conn_list = mgmt_get_conn_list,
2533 .disconnect = mgmt_disconnect,
2534 .remove_bonding = mgmt_unpair_device,
2535 .pincode_reply = mgmt_pincode_reply,
2536 .confirm_reply = mgmt_confirm_reply,
2537 .passkey_reply = mgmt_passkey_reply,
2538 .encrypt_link = mgmt_encrypt_link,
2539 .set_did = mgmt_set_did,
2540 .add_uuid = mgmt_add_uuid,
2541 .remove_uuid = mgmt_remove_uuid,
2542 .disable_cod_cache = mgmt_disable_cod_cache,
2543 .restore_powered = mgmt_restore_powered,
2544 .load_keys = mgmt_load_link_keys,
2545 .set_io_capability = mgmt_set_io_capability,
2546 .create_bonding = mgmt_create_bonding,
2547 .cancel_bonding = mgmt_cancel_bonding,
2548 .read_local_oob_data = mgmt_read_local_oob_data,
2549 .add_remote_oob_data = mgmt_add_remote_oob_data,
2550 .remove_remote_oob_data = mgmt_remove_remote_oob_data,
2551 .confirm_name = mgmt_confirm_name,
2552 .load_ltks = mgmtops_load_ltks,
2555 static int mgmt_init(void)
2557 #ifdef __TIZEN_PATCH__
2558 return btd_register_adapter_ops(&mgmt_ops, FALSE);
2560 return btd_register_adapter_ops(&mgmt_ops, TRUE);
2564 static void mgmt_exit(void)
2566 btd_adapter_cleanup_ops(&mgmt_ops);
2569 BLUETOOTH_PLUGIN_DEFINE(mgmtops, VERSION,
2570 BLUETOOTH_PLUGIN_PRIORITY_LOW, mgmt_init, mgmt_exit)