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 struct confirm_data {
758 static gboolean confirm_accept(gpointer user_data)
760 struct confirm_data *data = user_data;
761 struct controller_info *info = &controllers[data->index];
763 DBG("auto-accepting incoming pairing request");
765 if (data->index > max_index || !info->valid)
768 mgmt_confirm_reply(data->index, &data->bdaddr, data->type, TRUE);
773 static void mgmt_user_confirm_request(int sk, uint16_t index, void *buf,
776 struct mgmt_ev_user_confirm_request *ev = buf;
777 struct controller_info *info;
781 if (len < sizeof(*ev)) {
782 error("Too small user_confirm_request event");
786 ba2str(&ev->addr.bdaddr, addr);
788 DBG("hci%u %s confirm_hint %u", index, addr, ev->confirm_hint);
790 if (index > max_index) {
791 error("Unexpected index %u in user_confirm_request event",
796 if (ev->confirm_hint) {
797 struct confirm_data *data;
799 data = g_new0(struct confirm_data, 1);
801 bacpy(&data->bdaddr, &ev->addr.bdaddr);
802 data->type = ev->addr.type;
804 g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 1,
805 confirm_accept, data, g_free);
809 info = &controllers[index];
811 err = btd_event_user_confirm(&info->bdaddr, &ev->addr.bdaddr,
814 error("btd_event_user_confirm: %s", strerror(-err));
815 mgmt_confirm_reply(index, &ev->addr.bdaddr, ev->addr.type,
820 static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
822 if (uuid->type == SDP_UUID16)
823 sdp_uuid16_to_uuid128(uuid128, uuid);
824 else if (uuid->type == SDP_UUID32)
825 sdp_uuid32_to_uuid128(uuid128, uuid);
827 memcpy(uuid128, uuid, sizeof(*uuid));
830 static int mgmt_add_uuid(int index, uuid_t *uuid, uint8_t svc_hint)
832 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_add_uuid)];
833 struct mgmt_hdr *hdr = (void *) buf;
834 struct mgmt_cp_add_uuid *cp = (void *) &buf[sizeof(*hdr)];
835 struct controller_info *info = &controllers[index];
839 DBG("index %d", index);
841 if (info->pending_uuid) {
842 struct pending_uuid *pending = g_new0(struct pending_uuid, 1);
844 memcpy(&pending->uuid, uuid, sizeof(*uuid));
845 pending->svc_hint = svc_hint;
847 info->pending_uuids = g_slist_append(info->pending_uuids,
852 uuid_to_uuid128(&uuid128, uuid);
854 memset(buf, 0, sizeof(buf));
855 hdr->opcode = htobs(MGMT_OP_ADD_UUID);
856 hdr->len = htobs(sizeof(*cp));
857 hdr->index = htobs(index);
859 ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
860 htob128(&uint128, (uint128_t *) cp->uuid);
862 cp->svc_hint = svc_hint;
864 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
867 info->pending_uuid = TRUE;
872 static int mgmt_remove_uuid(int index, uuid_t *uuid)
874 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_remove_uuid)];
875 struct mgmt_hdr *hdr = (void *) buf;
876 struct mgmt_cp_remove_uuid *cp = (void *) &buf[sizeof(*hdr)];
880 DBG("index %d", index);
882 uuid_to_uuid128(&uuid128, uuid);
884 memset(buf, 0, sizeof(buf));
885 hdr->opcode = htobs(MGMT_OP_REMOVE_UUID);
886 hdr->len = htobs(sizeof(*cp));
887 hdr->index = htobs(index);
889 ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
890 htob128(&uint128, (uint128_t *) cp->uuid);
892 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
898 static int clear_uuids(int index)
902 memset(&uuid_any, 0, sizeof(uuid_any));
903 uuid_any.type = SDP_UUID128;
905 return mgmt_remove_uuid(index, &uuid_any);
908 static void read_index_list_complete(int sk, void *buf, size_t len)
910 struct mgmt_rp_read_index_list *rp = buf;
914 if (len < sizeof(*rp)) {
915 error("Too small read index list complete event");
919 num = btohs(bt_get_unaligned(&rp->num_controllers));
921 if (num * sizeof(uint16_t) + sizeof(*rp) != len) {
922 error("Incorrect packet size for index list event");
926 for (i = 0; i < num; i++) {
929 index = btohs(bt_get_unaligned(&rp->index[i]));
931 add_controller(index);
932 read_info(sk, index);
936 static int mgmt_set_powered(int index, gboolean powered)
938 struct controller_info *info = &controllers[index];
940 DBG("index %d powered %d pending_uuid %u", index, powered,
944 if (info->pending_uuid) {
945 info->pending_powered = TRUE;
949 info->pending_powered = FALSE;
952 return mgmt_set_mode(index, MGMT_OP_SET_POWERED, powered);
955 static int mgmt_set_name(int index, const char *name)
957 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_local_name)];
958 struct mgmt_hdr *hdr = (void *) buf;
959 struct mgmt_cp_set_local_name *cp = (void *) &buf[sizeof(*hdr)];
961 DBG("index %d, name %s", index, name);
963 memset(buf, 0, sizeof(buf));
964 hdr->opcode = htobs(MGMT_OP_SET_LOCAL_NAME);
965 hdr->len = htobs(sizeof(*cp));
966 hdr->index = htobs(index);
968 strncpy((char *) cp->name, name, sizeof(cp->name) - 1);
970 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
976 static int mgmt_set_dev_class(int index, uint8_t major, uint8_t minor)
978 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_dev_class)];
979 struct mgmt_hdr *hdr = (void *) buf;
980 struct mgmt_cp_set_dev_class *cp = (void *) &buf[sizeof(*hdr)];
981 struct controller_info *info = &controllers[index];
983 DBG("index %d major %u minor %u", index, major, minor);
985 if (info->pending_uuid) {
988 info->pending_class = TRUE;
992 memset(buf, 0, sizeof(buf));
993 hdr->opcode = htobs(MGMT_OP_SET_DEV_CLASS);
994 hdr->len = htobs(sizeof(*cp));
995 hdr->index = htobs(index);
1000 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1006 static void read_info_complete(int sk, uint16_t index, void *buf, size_t len)
1008 struct mgmt_rp_read_info *rp = buf;
1009 struct controller_info *info;
1010 struct btd_adapter *adapter;
1012 uint8_t mode, major, minor;
1015 if (len < sizeof(*rp)) {
1016 error("Too small read info complete event");
1020 if (index > max_index) {
1021 error("Unexpected index %u in read info complete", index);
1025 info = &controllers[index];
1027 bacpy(&info->bdaddr, &rp->bdaddr);
1028 info->version = rp->version;
1029 info->manufacturer = btohs(bt_get_unaligned(&rp->manufacturer));
1031 memcpy(&info->supported_settings, &rp->supported_settings,
1032 sizeof(info->supported_settings));
1033 memcpy(&info->current_settings, &rp->current_settings,
1034 sizeof(info->current_settings));
1036 memcpy(info->dev_class, rp->dev_class, sizeof(info->dev_class));
1038 ba2str(&info->bdaddr, addr);
1039 DBG("hci%u addr %s version %u manufacturer %u class 0x%02x%02x%02x\n",
1040 index, addr, info->version, info->manufacturer,
1041 info->dev_class[2], info->dev_class[1], info->dev_class[0]);
1042 DBG("hci%u settings", index);
1043 DBG("hci%u name %s", index, (char *) rp->name);
1044 DBG("hci%u short name %s", index, (char *) rp->short_name);
1048 adapter = btd_manager_register_adapter(index,
1049 mgmt_powered(info->current_settings));
1050 if (adapter == NULL) {
1051 error("mgmtops: unable to register adapter");
1055 update_settings(adapter, info->current_settings);
1057 name = btd_adapter_get_name(adapter);
1059 DBG("mgmtops setting name %s", name);
1062 mgmt_set_name(index, name);
1064 adapter_name_changed(adapter, (char *) rp->name);
1066 btd_adapter_get_class(adapter, &major, &minor);
1067 mgmt_set_dev_class(index, major, minor);
1069 btd_adapter_get_mode(adapter, &mode, NULL, NULL, NULL);
1070 if (mode == MODE_OFF && mgmt_powered(info->current_settings)) {
1071 mgmt_set_powered(index, FALSE);
1075 if (mode != MODE_OFF && !mgmt_powered(info->current_settings))
1076 mgmt_set_powered(index, TRUE);
1078 get_connections(sk, index);
1079 btd_adapter_start(adapter);
1082 btd_adapter_unref(adapter);
1085 static void disconnect_complete(int sk, uint16_t index, uint8_t status,
1086 void *buf, size_t len)
1088 struct mgmt_rp_disconnect *rp = buf;
1089 struct controller_info *info;
1092 if (len < sizeof(*rp)) {
1093 error("Too small disconnect complete event");
1097 ba2str(&rp->addr.bdaddr, addr);
1100 error("Disconnecting %s failed with status %u", addr, status);
1104 DBG("hci%d %s disconnected", index, addr);
1106 if (index > max_index) {
1107 error("Unexpected index %u in disconnect complete", index);
1111 info = &controllers[index];
1113 btd_event_disconn_complete(&info->bdaddr, &rp->addr.bdaddr);
1115 bonding_complete(info, &rp->addr.bdaddr, HCI_CONNECTION_TERMINATED);
1118 static void pair_device_complete(int sk, uint16_t index, uint8_t status,
1119 void *buf, size_t len)
1121 struct mgmt_rp_pair_device *rp = buf;
1122 struct controller_info *info;
1125 if (len < sizeof(*rp)) {
1126 error("Too small pair_device complete event");
1130 ba2str(&rp->addr.bdaddr, addr);
1132 DBG("hci%d %s pairing complete status %u", index, addr, status);
1134 if (index > max_index) {
1135 error("Unexpected index %u in pair_device complete", index);
1139 info = &controllers[index];
1141 bonding_complete(info, &rp->addr.bdaddr, status);
1144 static void get_connections_complete(int sk, uint16_t index, void *buf,
1147 struct mgmt_rp_get_connections *rp = buf;
1148 struct controller_info *info;
1151 if (len < sizeof(*rp)) {
1152 error("Too small get_connections complete event");
1156 if (len < (sizeof(*rp) + (rp->conn_count * sizeof(bdaddr_t)))) {
1157 error("Too small get_connections complete event");
1161 if (index > max_index) {
1162 error("Unexpected index %u in get_connections complete",
1167 info = &controllers[index];
1169 for (i = 0; i < rp->conn_count; i++) {
1170 bdaddr_t *bdaddr = g_memdup(&rp->addr[i], sizeof(bdaddr_t));
1171 info->connections = g_slist_append(info->connections, bdaddr);
1175 static void set_local_name_complete(int sk, uint16_t index, void *buf,
1178 struct mgmt_cp_set_local_name *rp = buf;
1179 struct controller_info *info;
1180 struct btd_adapter *adapter;
1182 if (len < sizeof(*rp)) {
1183 error("Too small set_local_name complete event");
1187 DBG("hci%d name %s", index, (char *) rp->name);
1189 if (index > max_index) {
1190 error("Unexpected index %u in set_local_name complete", index);
1194 info = &controllers[index];
1196 adapter = manager_find_adapter(&info->bdaddr);
1197 if (adapter == NULL) {
1198 DBG("Adapter not found");
1202 adapter_name_changed(adapter, (char *) rp->name);
1205 static void read_local_oob_data_complete(int sk, uint16_t index, void *buf,
1208 struct mgmt_rp_read_local_oob_data *rp = buf;
1209 struct btd_adapter *adapter;
1211 if (len != sizeof(*rp)) {
1212 error("read_local_oob_data_complete event size mismatch "
1213 "(%zu != %zu)", len, sizeof(*rp));
1217 if (index > max_index) {
1218 error("Unexpected index %u in read_local_oob_data_complete",
1223 DBG("hci%u", index);
1225 adapter = manager_find_adapter_by_id(index);
1228 oob_read_local_data_complete(adapter, rp->hash, rp->randomizer);
1231 static void start_discovery_complete(int sk, uint16_t index, uint8_t status,
1232 void *buf, size_t len)
1234 uint8_t *type = buf;
1235 struct btd_adapter *adapter;
1237 if (len != sizeof(*type)) {
1238 error("start_discovery_complete event size mismatch "
1239 "(%zu != %zu)", len, sizeof(*type));
1243 DBG("hci%u type %u status %u", index, *type, status);
1245 if (index > max_index) {
1246 error("Invalid index %u in start_discovery_complete", index);
1253 adapter = manager_find_adapter_by_id(index);
1255 /* Start discovery failed, inform upper layers. */
1256 adapter_set_discovering(adapter, FALSE);
1259 static void read_local_oob_data_failed(int sk, uint16_t index)
1261 struct btd_adapter *adapter;
1263 if (index > max_index) {
1264 error("Unexpected index %u in read_local_oob_data_failed",
1269 DBG("hci%u", index);
1271 adapter = manager_find_adapter_by_id(index);
1274 oob_read_local_data_complete(adapter, NULL, NULL);
1277 static void handle_pending_uuids(uint16_t index)
1279 struct controller_info *info;
1280 struct pending_uuid *pending;
1282 DBG("index %d", index);
1284 info = &controllers[index];
1286 info->pending_uuid = FALSE;
1288 if (g_slist_length(info->pending_uuids) == 0) {
1289 if (info->pending_class) {
1290 info->pending_class = FALSE;
1291 mgmt_set_dev_class(index, info->major, info->minor);
1294 if (info->pending_powered) {
1295 info->pending_powered = FALSE;
1296 mgmt_set_powered(index, TRUE);
1302 pending = info->pending_uuids->data;
1304 mgmt_add_uuid(index, &pending->uuid, pending->svc_hint);
1306 info->pending_uuids = g_slist_remove(info->pending_uuids, pending);
1310 static void mgmt_add_uuid_complete(int sk, uint16_t index, void *buf,
1313 DBG("index %d", index);
1315 if (index > max_index) {
1316 error("Unexpected index %u in add_uuid_complete event", index);
1320 handle_pending_uuids(index);
1323 static void mgmt_cmd_complete(int sk, uint16_t index, void *buf, size_t len)
1325 struct mgmt_ev_cmd_complete *ev = buf;
1330 if (len < sizeof(*ev)) {
1331 error("Too small management command complete event packet");
1335 opcode = btohs(bt_get_unaligned(&ev->opcode));
1340 case MGMT_OP_READ_VERSION:
1341 read_version_complete(sk, ev->data, len);
1343 case MGMT_OP_READ_INDEX_LIST:
1344 read_index_list_complete(sk, ev->data, len);
1346 case MGMT_OP_READ_INFO:
1347 read_info_complete(sk, index, ev->data, len);
1349 case MGMT_OP_SET_POWERED:
1350 mgmt_new_settings(sk, index, ev->data, len);
1352 case MGMT_OP_SET_DISCOVERABLE:
1353 mgmt_new_settings(sk, index, ev->data, len);
1355 case MGMT_OP_SET_CONNECTABLE:
1356 mgmt_new_settings(sk, index, ev->data, len);
1358 case MGMT_OP_SET_PAIRABLE:
1359 mgmt_new_settings(sk, index, ev->data, len);
1361 case MGMT_OP_SET_SSP:
1362 DBG("set_ssp complete");
1364 case MGMT_OP_SET_LE:
1365 DBG("set_le complete");
1367 case MGMT_OP_ADD_UUID:
1368 mgmt_add_uuid_complete(sk, index, ev->data, len);
1370 case MGMT_OP_REMOVE_UUID:
1371 DBG("remove_uuid complete");
1373 case MGMT_OP_SET_DEV_CLASS:
1374 DBG("set_dev_class complete");
1376 case MGMT_OP_LOAD_LINK_KEYS:
1377 DBG("load_link_keys complete");
1379 case MGMT_OP_CANCEL_PAIR_DEVICE:
1380 DBG("cancel_pair_device complete");
1382 case MGMT_OP_UNPAIR_DEVICE:
1383 DBG("unpair_device complete");
1385 case MGMT_OP_DISCONNECT:
1386 DBG("disconnect complete");
1387 disconnect_complete(sk, index, ev->status, ev->data, len);
1389 case MGMT_OP_GET_CONNECTIONS:
1390 get_connections_complete(sk, index, ev->data, len);
1392 case MGMT_OP_PIN_CODE_REPLY:
1393 DBG("pin_code_reply complete");
1395 case MGMT_OP_PIN_CODE_NEG_REPLY:
1396 DBG("pin_code_neg_reply complete");
1398 case MGMT_OP_SET_IO_CAPABILITY:
1399 DBG("set_io_capability complete");
1401 case MGMT_OP_PAIR_DEVICE:
1402 pair_device_complete(sk, index, ev->status, ev->data, len);
1404 case MGMT_OP_USER_CONFIRM_REPLY:
1405 DBG("user_confirm_reply complete");
1407 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1408 DBG("user_confirm_net_reply complete");
1410 case MGMT_OP_SET_LOCAL_NAME:
1411 set_local_name_complete(sk, index, ev->data, len);
1413 case MGMT_OP_READ_LOCAL_OOB_DATA:
1414 read_local_oob_data_complete(sk, index, ev->data, len);
1416 case MGMT_OP_ADD_REMOTE_OOB_DATA:
1417 DBG("add_remote_oob_data complete");
1419 case MGMT_OP_REMOVE_REMOTE_OOB_DATA:
1420 DBG("remove_remote_oob_data complete");
1422 case MGMT_OP_BLOCK_DEVICE:
1423 DBG("block_device complete");
1425 case MGMT_OP_UNBLOCK_DEVICE:
1426 DBG("unblock_device complete");
1428 case MGMT_OP_SET_FAST_CONNECTABLE:
1429 DBG("set_fast_connectable complete");
1431 case MGMT_OP_START_DISCOVERY:
1432 start_discovery_complete(sk, index, ev->status, ev->data, len);
1434 case MGMT_OP_STOP_DISCOVERY:
1435 DBG("stop_discovery complete");
1437 case MGMT_OP_SET_DEVICE_ID:
1438 DBG("set_did complete");
1441 error("Unknown command complete for opcode %u", opcode);
1446 static void mgmt_add_uuid_busy(int sk, uint16_t index)
1448 struct controller_info *info;
1450 DBG("index %d", index);
1452 info = &controllers[index];
1453 info->pending_cod_change = TRUE;
1456 static void mgmt_cmd_status(int sk, uint16_t index, void *buf, size_t len)
1458 struct mgmt_ev_cmd_status *ev = buf;
1461 if (len < sizeof(*ev)) {
1462 error("Too small management command status event packet");
1466 opcode = btohs(bt_get_unaligned(&ev->opcode));
1469 DBG("%s (0x%04x) cmd_status %u", mgmt_opstr(opcode), opcode,
1475 case MGMT_OP_READ_LOCAL_OOB_DATA:
1476 read_local_oob_data_failed(sk, index);
1478 case MGMT_OP_ADD_UUID:
1479 if (ev->status == MGMT_STATUS_BUSY) {
1480 mgmt_add_uuid_busy(sk, index);
1486 error("hci%u: %s (0x%04x) failed: %s (0x%02x)", index,
1487 mgmt_opstr(opcode), opcode, mgmt_errstr(ev->status),
1491 static void mgmt_controller_error(int sk, uint16_t index, void *buf, size_t len)
1493 struct mgmt_ev_controller_error *ev = buf;
1495 if (len < sizeof(*ev)) {
1496 error("Too small management controller error event packet");
1500 DBG("index %u error_code %u", index, ev->error_code);
1503 static void mgmt_auth_failed(int sk, uint16_t index, void *buf, size_t len)
1505 struct controller_info *info;
1506 struct mgmt_ev_auth_failed *ev = buf;
1508 if (len < sizeof(*ev)) {
1509 error("Too small mgmt_auth_failed event packet");
1513 DBG("hci%u auth failed status %u", index, ev->status);
1515 if (index > max_index) {
1516 error("Unexpected index %u in auth_failed event", index);
1520 info = &controllers[index];
1522 bonding_complete(info, &ev->addr.bdaddr, ev->status);
1525 static void mgmt_local_name_changed(int sk, uint16_t index, void *buf, size_t len)
1527 struct mgmt_cp_set_local_name *ev = buf;
1528 struct controller_info *info;
1529 struct btd_adapter *adapter;
1531 if (len < sizeof(*ev)) {
1532 error("Too small mgmt_local_name_changed event packet");
1536 DBG("hci%u local name changed: %s", index, (char *) ev->name);
1538 if (index > max_index) {
1539 error("Unexpected index %u in name_changed event", index);
1543 info = &controllers[index];
1545 adapter = manager_find_adapter(&info->bdaddr);
1547 adapter_name_changed(adapter, (char *) ev->name);
1550 static void mgmt_device_found(int sk, uint16_t index, void *buf, size_t len)
1552 struct mgmt_ev_device_found *ev = buf;
1553 struct controller_info *info;
1558 gboolean confirm_name;
1560 if (len < sizeof(*ev)) {
1561 error("mgmt_device_found too short (%zu bytes)", len);
1565 eir_len = bt_get_le16(&ev->eir_len);
1566 if (len != sizeof(*ev) + eir_len) {
1567 error("mgmt_device_found event size mismatch (%zu != %zu)",
1568 len, sizeof(*ev) + eir_len);
1572 if (index > max_index) {
1573 error("Unexpected index %u in device_found event", index);
1577 info = &controllers[index];
1584 flags = btohl(ev->flags);
1586 ba2str(&ev->addr.bdaddr, addr);
1587 DBG("hci%u addr %s, rssi %d flags 0x%04x eir_len %u",
1588 index, addr, ev->rssi, flags, eir_len);
1590 if (flags & MGMT_DEV_FOUND_LEGACY_PAIRING)
1591 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1594 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1597 confirm_name = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
1599 btd_event_device_found(&info->bdaddr, &ev->addr.bdaddr,
1601 ev->rssi, confirm_name,
1605 static void mgmt_discovering(int sk, uint16_t index, void *buf, size_t len)
1607 struct mgmt_ev_discovering *ev = buf;
1608 struct controller_info *info;
1609 struct btd_adapter *adapter;
1611 if (len < sizeof(*ev)) {
1612 error("Too small discovering event");
1616 DBG("Controller %u type %u discovering %u", index,
1617 ev->type, ev->discovering);
1619 if (index > max_index) {
1620 error("Unexpected index %u in discovering event", index);
1624 info = &controllers[index];
1626 adapter = manager_find_adapter(&info->bdaddr);
1630 adapter_set_discovering(adapter, ev->discovering);
1633 static void mgmt_device_blocked(int sk, uint16_t index, void *buf, size_t len)
1635 struct controller_info *info;
1636 struct mgmt_ev_device_blocked *ev = buf;
1639 if (len < sizeof(*ev)) {
1640 error("Too small mgmt_device_blocked event packet");
1644 ba2str(&ev->addr.bdaddr, addr);
1645 DBG("Device blocked, index %u, addr %s", index, addr);
1647 if (index > max_index) {
1648 error("Unexpected index %u in device_blocked event", index);
1652 info = &controllers[index];
1654 btd_event_device_blocked(&info->bdaddr, &ev->addr.bdaddr);
1657 static void mgmt_device_unblocked(int sk, uint16_t index, void *buf, size_t len)
1659 struct controller_info *info;
1660 struct mgmt_ev_device_unblocked *ev = buf;
1663 if (len < sizeof(*ev)) {
1664 error("Too small mgmt_device_unblocked event packet");
1668 ba2str(&ev->addr.bdaddr, addr);
1669 DBG("Device unblocked, index %u, addr %s", index, addr);
1671 if (index > max_index) {
1672 error("Unexpected index %u in device_unblocked event", index);
1676 info = &controllers[index];
1678 btd_event_device_unblocked(&info->bdaddr, &ev->addr.bdaddr);
1681 static void mgmt_device_unpaired(int sk, uint16_t index, void *buf, size_t len)
1683 struct controller_info *info;
1684 struct mgmt_ev_device_unpaired *ev = buf;
1687 if (len < sizeof(*ev)) {
1688 error("Too small mgmt_device_unpaired event packet");
1692 ba2str(&ev->addr.bdaddr, addr);
1693 DBG("Device upaired, index %u, addr %s", index, addr);
1695 if (index > max_index) {
1696 error("Unexpected index %u in device_unpaired event", index);
1700 info = &controllers[index];
1702 btd_event_device_unpaired(&info->bdaddr, &ev->addr.bdaddr);
1705 static void mgmt_new_ltk(int sk, uint16_t index, void *buf, size_t len)
1707 struct mgmt_ev_new_long_term_key *ev = buf;
1708 struct controller_info *info;
1710 if (len != sizeof(*ev)) {
1711 error("mgmt_new_ltk event size mismatch (%zu != %zu)",
1716 DBG("Controller %u new LTK authenticated %u enc_size %u", index,
1717 ev->key.authenticated, ev->key.enc_size);
1719 if (index > max_index) {
1720 error("Unexpected index %u in new_key event", index);
1724 info = &controllers[index];
1726 if (ev->store_hint) {
1727 btd_event_ltk_notify(&info->bdaddr, &ev->key.addr.bdaddr,
1728 ev->key.addr.type, ev->key.val, ev->key.master,
1729 ev->key.authenticated, ev->key.enc_size,
1730 ev->key.ediv, ev->key.rand);
1734 bonding_complete(info, &ev->key.addr.bdaddr, 0);
1737 static void mgmt_cod_changed(int sk, uint16_t index)
1739 struct controller_info *info;
1741 DBG("index %d", index);
1743 if (index > max_index) {
1744 error("Unexpected index %u in mgmt_cod_changed event", index);
1748 info = &controllers[index];
1750 if (info->pending_cod_change) {
1751 info->pending_cod_change = FALSE;
1752 handle_pending_uuids(index);
1756 static gboolean mgmt_event(GIOChannel *io, GIOCondition cond, gpointer user_data)
1758 char buf[MGMT_BUF_SIZE];
1759 struct mgmt_hdr *hdr = (void *) buf;
1762 uint16_t len, opcode, index;
1764 DBG("cond %d", cond);
1766 if (cond & G_IO_NVAL)
1769 sk = g_io_channel_unix_get_fd(io);
1771 if (cond & (G_IO_ERR | G_IO_HUP)) {
1772 error("Error on management socket");
1776 ret = read(sk, buf, sizeof(buf));
1778 error("Unable to read from management socket: %s (%d)",
1779 strerror(errno), errno);
1783 DBG("Received %zd bytes from management socket", ret);
1785 if (ret < MGMT_HDR_SIZE) {
1786 error("Too small Management packet");
1790 opcode = btohs(bt_get_unaligned(&hdr->opcode));
1791 len = btohs(bt_get_unaligned(&hdr->len));
1792 index = btohs(bt_get_unaligned(&hdr->index));
1794 if (ret != MGMT_HDR_SIZE + len) {
1795 error("Packet length mismatch. ret %zd len %u", ret, len);
1800 case MGMT_EV_CMD_COMPLETE:
1801 mgmt_cmd_complete(sk, index, buf + MGMT_HDR_SIZE, len);
1803 case MGMT_EV_CMD_STATUS:
1804 mgmt_cmd_status(sk, index, buf + MGMT_HDR_SIZE, len);
1806 case MGMT_EV_CONTROLLER_ERROR:
1807 mgmt_controller_error(sk, index, buf + MGMT_HDR_SIZE, len);
1809 case MGMT_EV_INDEX_ADDED:
1810 mgmt_index_added(sk, index);
1812 case MGMT_EV_INDEX_REMOVED:
1813 mgmt_index_removed(sk, index);
1815 case MGMT_EV_NEW_SETTINGS:
1816 mgmt_new_settings(sk, index, buf + MGMT_HDR_SIZE, len);
1818 case MGMT_EV_CLASS_OF_DEV_CHANGED:
1819 mgmt_cod_changed(sk, index);
1821 case MGMT_EV_NEW_LINK_KEY:
1822 mgmt_new_link_key(sk, index, buf + MGMT_HDR_SIZE, len);
1824 case MGMT_EV_DEVICE_CONNECTED:
1825 mgmt_device_connected(sk, index, buf + MGMT_HDR_SIZE, len);
1827 case MGMT_EV_DEVICE_DISCONNECTED:
1828 mgmt_device_disconnected(sk, index, buf + MGMT_HDR_SIZE, len);
1830 case MGMT_EV_CONNECT_FAILED:
1831 mgmt_connect_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1833 case MGMT_EV_PIN_CODE_REQUEST:
1834 mgmt_pin_code_request(sk, index, buf + MGMT_HDR_SIZE, len);
1836 case MGMT_EV_USER_CONFIRM_REQUEST:
1837 mgmt_user_confirm_request(sk, index, buf + MGMT_HDR_SIZE, len);
1839 case MGMT_EV_AUTH_FAILED:
1840 mgmt_auth_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1842 case MGMT_EV_LOCAL_NAME_CHANGED:
1843 mgmt_local_name_changed(sk, index, buf + MGMT_HDR_SIZE, len);
1845 case MGMT_EV_DEVICE_FOUND:
1846 mgmt_device_found(sk, index, buf + MGMT_HDR_SIZE, len);
1848 case MGMT_EV_DISCOVERING:
1849 mgmt_discovering(sk, index, buf + MGMT_HDR_SIZE, len);
1851 case MGMT_EV_DEVICE_BLOCKED:
1852 mgmt_device_blocked(sk, index, buf + MGMT_HDR_SIZE, len);
1854 case MGMT_EV_DEVICE_UNBLOCKED:
1855 mgmt_device_unblocked(sk, index, buf + MGMT_HDR_SIZE, len);
1857 case MGMT_EV_DEVICE_UNPAIRED:
1858 mgmt_device_unpaired(sk, index, buf + MGMT_HDR_SIZE, len);
1860 case MGMT_EV_USER_PASSKEY_REQUEST:
1861 mgmt_passkey_request(sk, index, buf + MGMT_HDR_SIZE, len);
1863 case MGMT_EV_NEW_LONG_TERM_KEY:
1864 mgmt_new_ltk(sk, index, buf + MGMT_HDR_SIZE, len);
1867 error("Unknown Management opcode %u (index %u)", opcode, index);
1874 static int mgmt_setup(void)
1876 struct mgmt_hdr hdr;
1877 struct sockaddr_hci addr;
1879 GIOCondition condition;
1882 dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
1886 memset(&addr, 0, sizeof(addr));
1887 addr.hci_family = AF_BLUETOOTH;
1888 addr.hci_dev = HCI_DEV_NONE;
1889 addr.hci_channel = HCI_CHANNEL_CONTROL;
1891 if (bind(dd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1896 memset(&hdr, 0, sizeof(hdr));
1897 hdr.opcode = htobs(MGMT_OP_READ_VERSION);
1898 hdr.index = htobs(MGMT_INDEX_NONE);
1899 if (write(dd, &hdr, sizeof(hdr)) < 0) {
1904 io = g_io_channel_unix_new(dd);
1905 condition = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
1906 mgmt_watch = g_io_add_watch(io, condition, mgmt_event, NULL);
1907 g_io_channel_unref(io);
1911 info("Bluetooth Management interface initialized");
1920 static void mgmt_cleanup(void)
1922 g_free(controllers);
1926 if (mgmt_sock >= 0) {
1931 if (mgmt_watch > 0) {
1932 g_source_remove(mgmt_watch);
1937 static int mgmt_start_discovery(int index)
1939 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
1940 struct mgmt_hdr *hdr = (void *) buf;
1941 struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
1942 struct controller_info *info = &controllers[index];
1944 DBG("index %d", index);
1946 info->discov_type = 0;
1948 if (mgmt_bredr(info->current_settings))
1949 hci_set_bit(BDADDR_BREDR, &info->discov_type);
1951 if (mgmt_low_energy(info->current_settings)) {
1952 hci_set_bit(BDADDR_LE_PUBLIC, &info->discov_type);
1953 hci_set_bit(BDADDR_LE_RANDOM, &info->discov_type);
1956 memset(buf, 0, sizeof(buf));
1957 hdr->opcode = htobs(MGMT_OP_START_DISCOVERY);
1958 hdr->len = htobs(sizeof(*cp));
1959 hdr->index = htobs(index);
1961 cp->type = info->discov_type;
1963 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1969 static int mgmt_stop_discovery(int index)
1971 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
1972 struct mgmt_hdr *hdr = (void *) buf;
1973 struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
1974 struct controller_info *info = &controllers[index];
1976 DBG("index %d", index);
1978 memset(buf, 0, sizeof(buf));
1979 hdr->opcode = htobs(MGMT_OP_STOP_DISCOVERY);
1980 hdr->len = htobs(sizeof(*cp));
1981 hdr->index = htobs(index);
1983 cp->type = info->discov_type;
1985 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1991 static int mgmt_set_fast_connectable(int index, gboolean enable)
1993 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_mode)];
1994 struct mgmt_hdr *hdr = (void *) buf;
1995 struct mgmt_mode *cp = (void *) &buf[sizeof(*hdr)];
1997 DBG("index %d enable %d", index, enable);
1999 memset(buf, 0, sizeof(buf));
2000 hdr->opcode = htobs(MGMT_OP_SET_FAST_CONNECTABLE);
2001 hdr->len = htobs(sizeof(*cp));
2002 hdr->index = htobs(index);
2006 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2012 static int mgmt_read_clock(int index, bdaddr_t *bdaddr, int which, int timeout,
2013 uint32_t *clock, uint16_t *accuracy)
2017 ba2str(bdaddr, addr);
2018 DBG("index %d addr %s which %d timeout %d", index, addr, which,
2024 static int mgmt_read_bdaddr(int index, bdaddr_t *bdaddr)
2027 struct controller_info *info = &controllers[index];
2029 ba2str(&info->bdaddr, addr);
2030 DBG("index %d addr %s", index, addr);
2035 bacpy(bdaddr, &info->bdaddr);
2040 static int mgmt_block_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2042 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_block_device)];
2043 struct mgmt_hdr *hdr = (void *) buf;
2044 struct mgmt_cp_block_device *cp;
2048 ba2str(bdaddr, addr);
2049 DBG("index %d addr %s", index, addr);
2051 memset(buf, 0, sizeof(buf));
2053 hdr->opcode = htobs(MGMT_OP_BLOCK_DEVICE);
2054 hdr->len = htobs(sizeof(*cp));
2055 hdr->index = htobs(index);
2057 cp = (void *) &buf[sizeof(*hdr)];
2058 bacpy(&cp->addr.bdaddr, bdaddr);
2059 cp->addr.type = bdaddr_type;
2061 buf_len = sizeof(*hdr) + sizeof(*cp);
2063 if (write(mgmt_sock, buf, buf_len) < 0)
2069 static int mgmt_unblock_device(int index, bdaddr_t *bdaddr,
2070 uint8_t bdaddr_type)
2072 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_unblock_device)];
2073 struct mgmt_hdr *hdr = (void *) buf;
2074 struct mgmt_cp_unblock_device *cp;
2078 ba2str(bdaddr, addr);
2079 DBG("index %d addr %s", index, addr);
2081 memset(buf, 0, sizeof(buf));
2083 hdr->opcode = htobs(MGMT_OP_UNBLOCK_DEVICE);
2084 hdr->len = htobs(sizeof(*cp));
2085 hdr->index = htobs(index);
2087 cp = (void *) &buf[sizeof(*hdr)];
2088 bacpy(&cp->addr.bdaddr, bdaddr);
2089 cp->addr.type = bdaddr_type;
2091 buf_len = sizeof(*hdr) + sizeof(*cp);
2093 if (write(mgmt_sock, buf, buf_len) < 0)
2099 static int mgmt_get_conn_list(int index, GSList **conns)
2101 struct controller_info *info = &controllers[index];
2103 DBG("index %d", index);
2105 *conns = info->connections;
2106 info->connections = NULL;
2111 static int mgmt_disconnect(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2113 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_disconnect)];
2114 struct mgmt_hdr *hdr = (void *) buf;
2115 struct mgmt_cp_disconnect *cp = (void *) &buf[sizeof(*hdr)];
2118 ba2str(bdaddr, addr);
2119 DBG("index %d %s", index, addr);
2121 memset(buf, 0, sizeof(buf));
2122 hdr->opcode = htobs(MGMT_OP_DISCONNECT);
2123 hdr->len = htobs(sizeof(*cp));
2124 hdr->index = htobs(index);
2126 bacpy(&cp->addr.bdaddr, bdaddr);
2127 cp->addr.type = bdaddr_type;
2129 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2130 error("write: %s (%d)", strerror(errno), errno);
2135 static int mgmt_unpair_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2137 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_unpair_device)];
2138 struct mgmt_hdr *hdr = (void *) buf;
2139 struct mgmt_cp_unpair_device *cp = (void *) &buf[sizeof(*hdr)];
2142 ba2str(bdaddr, addr);
2143 DBG("index %d addr %s", index, addr);
2145 memset(buf, 0, sizeof(buf));
2146 hdr->opcode = htobs(MGMT_OP_UNPAIR_DEVICE);
2147 hdr->len = htobs(sizeof(*cp));
2148 hdr->index = htobs(index);
2150 bacpy(&cp->addr.bdaddr, bdaddr);
2151 cp->addr.type = bdaddr_type;
2154 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2160 static int mgmt_encrypt_link(int index, bdaddr_t *dst, bt_hci_result_t cb,
2166 DBG("index %d addr %s", index, addr);
2171 static int mgmt_set_did(int index, uint16_t vendor, uint16_t product,
2172 uint16_t version, uint16_t source)
2174 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_device_id)];
2175 struct mgmt_hdr *hdr = (void *) buf;
2176 struct mgmt_cp_set_device_id *cp = (void *) &buf[sizeof(*hdr)];
2178 DBG("index %d source %x vendor %x product %x version %x",
2179 index, source, vendor, product, version);
2181 memset(buf, 0, sizeof(buf));
2182 hdr->opcode = htobs(MGMT_OP_SET_DEVICE_ID);
2183 hdr->len = htobs(sizeof(*cp));
2184 hdr->index = htobs(index);
2186 cp->source = htobs(source);
2187 cp->vendor = htobs(vendor);
2188 cp->product = htobs(product);
2189 cp->version = htobs(version);
2191 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2197 static int mgmt_disable_cod_cache(int index)
2199 DBG("index %d", index);
2201 /* The cache control is handled automatically for mgmt */
2205 static int mgmt_restore_powered(int index)
2207 DBG("index %d", index);
2211 static int mgmt_load_link_keys(int index, GSList *keys, gboolean debug_keys)
2214 struct mgmt_hdr *hdr;
2215 struct mgmt_cp_load_link_keys *cp;
2216 struct mgmt_link_key_info *key;
2217 size_t key_count, cp_size;
2221 key_count = g_slist_length(keys);
2223 DBG("index %d keys %zu debug_keys %d", index, key_count, debug_keys);
2225 cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2227 buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2232 hdr->opcode = htobs(MGMT_OP_LOAD_LINK_KEYS);
2233 hdr->len = htobs(cp_size);
2234 hdr->index = htobs(index);
2236 cp = (void *) (buf + sizeof(*hdr));
2237 cp->debug_keys = debug_keys;
2238 cp->key_count = htobs(key_count);
2240 for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2241 struct link_key_info *info = l->data;
2243 bacpy(&key->addr.bdaddr, &info->bdaddr);
2244 key->addr.type = BDADDR_BREDR;
2245 key->type = info->type;
2246 memcpy(key->val, info->key, 16);
2247 key->pin_len = info->pin_len;
2250 if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2260 static int mgmt_set_io_capability(int index, uint8_t io_capability)
2262 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_io_capability)];
2263 struct mgmt_hdr *hdr = (void *) buf;
2264 struct mgmt_cp_set_io_capability *cp = (void *) &buf[sizeof(*hdr)];
2266 DBG("hci%d io_capability 0x%02x", index, io_capability);
2268 memset(buf, 0, sizeof(buf));
2269 hdr->opcode = htobs(MGMT_OP_SET_IO_CAPABILITY);
2270 hdr->len = htobs(sizeof(*cp));
2271 hdr->index = htobs(index);
2273 cp->io_capability = io_capability;
2275 if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2281 static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t addr_type, uint8_t io_cap)
2283 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pair_device)];
2284 struct mgmt_hdr *hdr = (void *) buf;
2285 struct mgmt_cp_pair_device *cp = (void *) &buf[sizeof(*hdr)];
2288 ba2str(bdaddr, addr);
2289 DBG("hci%d bdaddr %s io_cap 0x%02x", index, addr, io_cap);
2291 memset(buf, 0, sizeof(buf));
2292 hdr->opcode = htobs(MGMT_OP_PAIR_DEVICE);
2293 hdr->len = htobs(sizeof(*cp));
2294 hdr->index = htobs(index);
2296 bacpy(&cp->addr.bdaddr, bdaddr);
2297 cp->addr.type = addr_type;
2298 cp->io_cap = io_cap;
2300 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2306 static int mgmt_cancel_bonding(int index, bdaddr_t *bdaddr)
2308 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_addr_info)];
2309 struct mgmt_hdr *hdr = (void *) buf;
2310 struct mgmt_addr_info *cp = (void *) &buf[sizeof(*hdr)];
2313 ba2str(bdaddr, addr);
2314 DBG("hci%d bdaddr %s", index, addr);
2316 memset(buf, 0, sizeof(buf));
2317 hdr->opcode = htobs(MGMT_OP_CANCEL_PAIR_DEVICE);
2318 hdr->len = htobs(sizeof(*cp));
2319 hdr->index = htobs(index);
2321 bacpy(&cp->bdaddr, bdaddr);
2323 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2329 static int mgmt_read_local_oob_data(int index)
2331 struct mgmt_hdr hdr;
2333 DBG("hci%d", index);
2335 hdr.opcode = htobs(MGMT_OP_READ_LOCAL_OOB_DATA);
2337 hdr.index = htobs(index);
2339 if (write(mgmt_sock, &hdr, sizeof(hdr)) < 0)
2345 static int mgmt_add_remote_oob_data(int index, bdaddr_t *bdaddr,
2346 uint8_t *hash, uint8_t *randomizer)
2348 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_add_remote_oob_data)];
2349 struct mgmt_hdr *hdr = (void *) buf;
2350 struct mgmt_cp_add_remote_oob_data *cp = (void *) &buf[sizeof(*hdr)];
2353 ba2str(bdaddr, addr);
2354 DBG("hci%d bdaddr %s", index, addr);
2356 memset(buf, 0, sizeof(buf));
2358 hdr->opcode = htobs(MGMT_OP_ADD_REMOTE_OOB_DATA);
2359 hdr->index = htobs(index);
2360 hdr->len = htobs(sizeof(*cp));
2362 bacpy(&cp->addr.bdaddr, bdaddr);
2363 memcpy(cp->hash, hash, 16);
2364 memcpy(cp->randomizer, randomizer, 16);
2366 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2372 static int mgmt_remove_remote_oob_data(int index, bdaddr_t *bdaddr)
2374 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_remove_remote_oob_data)];
2375 struct mgmt_hdr *hdr = (void *) buf;
2376 struct mgmt_cp_remove_remote_oob_data *cp = (void *) &buf[sizeof(*hdr)];
2379 ba2str(bdaddr, addr);
2380 DBG("hci%d bdaddr %s", index, addr);
2382 memset(buf, 0, sizeof(buf));
2384 hdr->opcode = htobs(MGMT_OP_REMOVE_REMOTE_OOB_DATA);
2385 hdr->index = htobs(index);
2386 hdr->len = htobs(sizeof(*cp));
2388 bacpy(&cp->addr.bdaddr, bdaddr);
2390 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2396 static int mgmt_confirm_name(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
2397 gboolean name_known)
2399 char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_confirm_name)];
2400 struct mgmt_hdr *hdr = (void *) buf;
2401 struct mgmt_cp_confirm_name *cp = (void *) &buf[sizeof(*hdr)];
2404 ba2str(bdaddr, addr);
2405 DBG("hci%d bdaddr %s name_known %u", index, addr, name_known);
2407 memset(buf, 0, sizeof(buf));
2409 hdr->opcode = htobs(MGMT_OP_CONFIRM_NAME);
2410 hdr->index = htobs(index);
2411 hdr->len = htobs(sizeof(*cp));
2413 bacpy(&cp->addr.bdaddr, bdaddr);
2414 cp->addr.type = bdaddr_type;
2415 cp->name_known = name_known;
2417 if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2423 static int mgmtops_load_ltks(int index, GSList *keys)
2426 struct mgmt_hdr *hdr;
2427 struct mgmt_cp_load_long_term_keys *cp;
2428 struct mgmt_ltk_info *key;
2429 size_t key_count, cp_size;
2433 key_count = g_slist_length(keys);
2435 DBG("index %d keys %zu", index, key_count);
2437 cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2439 buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2444 hdr->opcode = htobs(MGMT_OP_LOAD_LONG_TERM_KEYS);
2445 hdr->len = htobs(cp_size);
2446 hdr->index = htobs(index);
2448 cp = (void *) (buf + sizeof(*hdr));
2449 cp->key_count = htobs(key_count);
2451 for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2452 struct smp_ltk_info *info = l->data;
2454 bacpy(&key->addr.bdaddr, &info->bdaddr);
2455 key->addr.type = info->bdaddr_type;
2456 memcpy(key->val, info->val, sizeof(info->val));
2457 memcpy(key->rand, info->rand, sizeof(info->rand));
2458 memcpy(&key->ediv, &info->ediv, sizeof(key->ediv));
2459 key->authenticated = info->authenticated;
2460 key->master = info->master;
2461 key->enc_size = info->enc_size;
2464 if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2474 static struct btd_adapter_ops mgmt_ops = {
2475 .setup = mgmt_setup,
2476 .cleanup = mgmt_cleanup,
2477 .set_powered = mgmt_set_powered,
2478 .set_discoverable = mgmt_set_discoverable,
2479 .set_pairable = mgmt_set_pairable,
2480 .start_discovery = mgmt_start_discovery,
2481 .stop_discovery = mgmt_stop_discovery,
2482 .set_name = mgmt_set_name,
2483 .set_dev_class = mgmt_set_dev_class,
2484 .set_fast_connectable = mgmt_set_fast_connectable,
2485 .read_clock = mgmt_read_clock,
2486 .read_bdaddr = mgmt_read_bdaddr,
2487 .block_device = mgmt_block_device,
2488 .unblock_device = mgmt_unblock_device,
2489 .get_conn_list = mgmt_get_conn_list,
2490 .disconnect = mgmt_disconnect,
2491 .remove_bonding = mgmt_unpair_device,
2492 .pincode_reply = mgmt_pincode_reply,
2493 .confirm_reply = mgmt_confirm_reply,
2494 .passkey_reply = mgmt_passkey_reply,
2495 .encrypt_link = mgmt_encrypt_link,
2496 .set_did = mgmt_set_did,
2497 .add_uuid = mgmt_add_uuid,
2498 .remove_uuid = mgmt_remove_uuid,
2499 .disable_cod_cache = mgmt_disable_cod_cache,
2500 .restore_powered = mgmt_restore_powered,
2501 .load_keys = mgmt_load_link_keys,
2502 .set_io_capability = mgmt_set_io_capability,
2503 .create_bonding = mgmt_create_bonding,
2504 .cancel_bonding = mgmt_cancel_bonding,
2505 .read_local_oob_data = mgmt_read_local_oob_data,
2506 .add_remote_oob_data = mgmt_add_remote_oob_data,
2507 .remove_remote_oob_data = mgmt_remove_remote_oob_data,
2508 .confirm_name = mgmt_confirm_name,
2509 .load_ltks = mgmtops_load_ltks,
2512 static int mgmt_init(void)
2514 return btd_register_adapter_ops(&mgmt_ops, TRUE);
2517 static void mgmt_exit(void)
2519 btd_adapter_cleanup_ops(&mgmt_ops);
2522 BLUETOOTH_PLUGIN_DEFINE(mgmtops, VERSION,
2523 BLUETOOTH_PLUGIN_PRIORITY_LOW, mgmt_init, mgmt_exit)