Fix the bug : TIVI-204 Fail to connect to bluetooth network
[profile/ivi/bluez.git] / plugins / mgmtops.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2010  Nokia Corporation
6  *  Copyright (C) 2010  Marcel Holtmann <marcel@holtmann.org>
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/wait.h>
35
36 #include <glib.h>
37
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>
43
44 #include "plugin.h"
45 #include "log.h"
46 #include "adapter.h"
47 #include "manager.h"
48 #include "device.h"
49 #include "event.h"
50 #include "oob.h"
51 #include "eir.h"
52
53 #define MGMT_BUF_SIZE 1024
54
55 struct pending_uuid {
56         uuid_t uuid;
57         uint8_t svc_hint;
58 };
59
60 static int max_index = -1;
61 static struct controller_info {
62         gboolean valid;
63         gboolean notified;
64         bdaddr_t bdaddr;
65         uint8_t version;
66         uint16_t manufacturer;
67         uint32_t supported_settings;
68         uint32_t current_settings;
69         uint8_t dev_class[3];
70         GSList *connections;
71         uint8_t discov_type;
72
73         gboolean pending_uuid;
74         GSList *pending_uuids;
75
76         gboolean pending_class;
77         uint8_t major;
78         uint8_t minor;
79
80         gboolean pending_powered;
81         gboolean pending_cod_change;
82 } *controllers = NULL;
83
84 static int mgmt_sock = -1;
85 static guint mgmt_watch = 0;
86
87 static uint8_t mgmt_version = 0;
88 static uint16_t mgmt_revision = 0;
89
90 static void read_version_complete(int sk, void *buf, size_t len)
91 {
92         struct mgmt_hdr hdr;
93         struct mgmt_rp_read_version *rp = buf;
94
95         if (len < sizeof(*rp)) {
96                 error("Too small read version complete event"
97                                 " (probably an old kernel)");
98                 abort();
99         }
100
101         mgmt_revision = btohs(bt_get_unaligned(&rp->revision));
102         mgmt_version = rp->version;
103
104         DBG("version %u revision %u", mgmt_version, mgmt_revision);
105
106         if (mgmt_version < 1) {
107                 error("Version 1 of mgmt needed (kernel has version %u)",
108                                                                 mgmt_version);
109                 abort();
110         }
111
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);
118 }
119
120 static void add_controller(uint16_t index)
121 {
122         struct controller_info *info;
123
124         if (index > max_index) {
125                 size_t size = sizeof(struct controller_info) * (index + 1);
126                 max_index = index;
127                 controllers = g_realloc(controllers, size);
128         }
129
130         info = &controllers[index];
131
132         memset(info, 0, sizeof(*info));
133
134         info->valid = TRUE;
135
136         DBG("Added controller %u", index);
137 }
138
139 static void read_info(int sk, uint16_t index)
140 {
141         struct mgmt_hdr hdr;
142
143         memset(&hdr, 0, sizeof(hdr));
144         hdr.opcode = htobs(MGMT_OP_READ_INFO);
145         hdr.index = htobs(index);
146
147         if (write(sk, &hdr, sizeof(hdr)) < 0)
148                 error("Unable to send read_info command: %s (%d)",
149                                                 strerror(errno), errno);
150 }
151
152 static void get_connections(int sk, uint16_t index)
153 {
154         struct mgmt_hdr hdr;
155
156         memset(&hdr, 0, sizeof(hdr));
157         hdr.opcode = htobs(MGMT_OP_GET_CONNECTIONS);
158         hdr.index = htobs(index);
159
160         if (write(sk, &hdr, sizeof(hdr)) < 0)
161                 error("Unable to send get_connections command: %s (%d)",
162                                                 strerror(errno), errno);
163 }
164
165 static void mgmt_index_added(int sk, uint16_t index)
166 {
167         add_controller(index);
168         read_info(sk, index);
169 }
170
171 static void remove_controller(uint16_t index)
172 {
173         if (index > max_index)
174                 return;
175
176         if (!controllers[index].valid)
177                 return;
178
179         btd_manager_unregister_adapter(index);
180
181         g_slist_free_full(controllers[index].pending_uuids, g_free);
182         controllers[index].pending_uuids = NULL;
183
184         memset(&controllers[index], 0, sizeof(struct controller_info));
185
186         DBG("Removed controller %u", index);
187 }
188
189 static void mgmt_index_removed(int sk, uint16_t index)
190 {
191         remove_controller(index);
192 }
193
194 static int mgmt_set_mode(int index, uint16_t opcode, uint8_t val)
195 {
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)];
199
200         memset(buf, 0, sizeof(buf));
201         hdr->opcode = htobs(opcode);
202         hdr->index = htobs(index);
203         hdr->len = htobs(sizeof(*cp));
204
205         cp->val = val;
206
207         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
208                 return -errno;
209
210         return 0;
211 }
212
213 static int mgmt_set_connectable(int index, gboolean connectable)
214 {
215         DBG("index %d connectable %d", index, connectable);
216         return mgmt_set_mode(index, MGMT_OP_SET_CONNECTABLE, connectable);
217 }
218
219 static int mgmt_set_discoverable(int index, gboolean discoverable,
220                                                         uint16_t timeout)
221 {
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)];
225
226         DBG("index %d discoverable %d", index, discoverable);
227
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));
232
233         cp->val = discoverable;
234         cp->timeout = timeout;
235
236         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
237                 return -errno;
238
239         return 0;
240 }
241
242 static int mgmt_set_pairable(int index, gboolean pairable)
243 {
244         DBG("index %d pairable %d", index, pairable);
245         return mgmt_set_mode(index, MGMT_OP_SET_PAIRABLE, pairable);
246 }
247
248 static inline int mgmt_powered(uint32_t settings)
249 {
250         return (settings & MGMT_SETTING_POWERED) != 0;
251 }
252
253 static inline int mgmt_connectable(uint32_t settings)
254 {
255         return (settings & MGMT_SETTING_CONNECTABLE) != 0;
256 }
257
258 static inline int mgmt_fast_connectable(uint32_t settings)
259 {
260         return (settings & MGMT_SETTING_FAST_CONNECTABLE) != 0;
261 }
262
263 static inline int mgmt_discoverable(uint32_t settings)
264 {
265         return (settings & MGMT_SETTING_DISCOVERABLE) != 0;
266 }
267
268 static inline int mgmt_pairable(uint32_t settings)
269 {
270         return (settings & MGMT_SETTING_PAIRABLE) != 0;
271 }
272
273 static inline int mgmt_ssp(uint32_t settings)
274 {
275         return (settings & MGMT_SETTING_SSP) != 0;
276 }
277
278 static inline int mgmt_bredr(uint32_t settings)
279 {
280         return (settings & MGMT_SETTING_BREDR) != 0;
281 }
282
283 static inline int mgmt_high_speed(uint32_t settings)
284 {
285         return (settings & MGMT_SETTING_HS) != 0;
286 }
287
288 static inline int mgmt_low_energy(uint32_t settings)
289 {
290         return (settings & MGMT_SETTING_LE) != 0;
291 }
292
293 static uint8_t create_mode(uint32_t settings)
294 {
295         uint8_t mode = 0;
296
297         if (mgmt_connectable(settings))
298                 mode |= SCAN_PAGE;
299
300         if (mgmt_discoverable(settings))
301                 mode |= SCAN_INQUIRY;
302
303         return mode;
304 }
305
306 static void update_settings(struct btd_adapter *adapter, uint32_t settings)
307 {
308         struct controller_info *info;
309         gboolean pairable;
310         uint8_t on_mode;
311         uint16_t index, discoverable_timeout;
312
313         DBG("new settings %x", settings);
314
315         btd_adapter_get_mode(adapter, NULL, &on_mode, &discoverable_timeout,
316                                                                 &pairable);
317
318         index = adapter_get_dev_id(adapter);
319
320         info = &controllers[index];
321
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));
330         }
331
332         if (mgmt_pairable(settings) != pairable)
333                 mgmt_set_pairable(index, pairable);
334
335         if (mgmt_ssp(info->supported_settings) && !mgmt_ssp(settings))
336                 mgmt_set_mode(index, MGMT_OP_SET_SSP, 1);
337
338         if (mgmt_low_energy(info->supported_settings) &&
339                                                 !mgmt_low_energy(settings))
340                 mgmt_set_mode(index, MGMT_OP_SET_LE, 1);
341 }
342
343 static int mgmt_update_powered(struct btd_adapter *adapter,
344                                                 struct controller_info *info,
345                                                 uint32_t settings)
346 {
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;
354                 return 0;
355         }
356
357         btd_adapter_start(adapter);
358
359         update_settings(adapter, settings);
360
361         return 0;
362 }
363
364 static int mode_changed(uint32_t s1, uint32_t s2)
365 {
366         if (mgmt_connectable(s1) != mgmt_connectable(s2))
367                 return 1;
368
369         if (mgmt_discoverable(s1) != mgmt_discoverable(s2))
370                 return 1;
371
372         return 0;
373 }
374
375 static void mgmt_new_settings(int sk, uint16_t index, void *buf, size_t len)
376 {
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;
381
382         if (len < sizeof(*ev)) {
383                 error("Too small new settings event");
384                 return;
385         }
386
387         DBG("hci%u new settings", index);
388
389         if (index > max_index) {
390                 error("Unexpected index %u in new_settings event", index);
391                 return;
392         }
393
394         info = &controllers[index];
395
396         adapter = manager_find_adapter(&info->bdaddr);
397         if (adapter == NULL) {
398                 DBG("Adapter not found");
399                 return;
400         }
401
402         settings = bt_get_le32(ev);
403
404         old_power = mgmt_powered(info->current_settings);
405         new_power = mgmt_powered(settings);
406
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));
411
412         old_pairable = mgmt_pairable(info->current_settings);
413         new_pairable = mgmt_pairable(settings);
414
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));
419
420         info->current_settings = settings;
421 }
422
423 static void bonding_complete(struct controller_info *info, bdaddr_t *bdaddr,
424                                                                 uint8_t status)
425 {
426         struct btd_adapter *adapter;
427
428         adapter = manager_find_adapter(&info->bdaddr);
429         if (adapter != NULL)
430                 adapter_bonding_complete(adapter, bdaddr, status);
431 }
432
433 static void mgmt_new_link_key(int sk, uint16_t index, void *buf, size_t len)
434 {
435         struct mgmt_ev_new_link_key *ev = buf;
436         struct controller_info *info;
437
438         if (len != sizeof(*ev)) {
439                 error("mgmt_new_link_key event size mismatch (%zu != %zu)",
440                                                         len, sizeof(*ev));
441                 return;
442         }
443
444         DBG("Controller %u new key of type %u pin_len %u", index,
445                                         ev->key.type, ev->key.pin_len);
446
447         if (index > max_index) {
448                 error("Unexpected index %u in new_key event", index);
449                 return;
450         }
451
452         if (ev->key.pin_len > 16) {
453                 error("Invalid PIN length (%u) in new_key event",
454                                                         ev->key.pin_len);
455                 return;
456         }
457
458         info = &controllers[index];
459
460         if (ev->store_hint)
461                 btd_event_link_key_notify(&info->bdaddr, &ev->key.addr.bdaddr,
462                                                 ev->key.val, ev->key.type,
463                                                 ev->key.pin_len);
464
465         bonding_complete(info, &ev->key.addr.bdaddr, 0);
466 }
467
468 static void mgmt_device_connected(int sk, uint16_t index, void *buf, size_t len)
469 {
470         struct mgmt_ev_device_connected *ev = buf;
471         struct eir_data eir_data;
472         struct controller_info *info;
473         uint16_t eir_len;
474         char addr[18];
475
476         if (len < sizeof(*ev)) {
477                 error("Too small device_connected event");
478                 return;
479         }
480
481         eir_len = bt_get_le16(&ev->eir_len);
482         if (len < sizeof(*ev) + eir_len) {
483                 error("Too small device_connected event");
484                 return;
485         }
486
487         ba2str(&ev->addr.bdaddr, addr);
488
489         DBG("hci%u device %s connected eir_len %u", index, addr, eir_len);
490
491         if (index > max_index) {
492                 error("Unexpected index %u in device_connected event", index);
493                 return;
494         }
495
496         info = &controllers[index];
497
498         memset(&eir_data, 0, sizeof(eir_data));
499         if (eir_len > 0)
500                 eir_parse(&eir_data, ev->eir, eir_len);
501
502         btd_event_conn_complete(&info->bdaddr, &ev->addr.bdaddr,
503                                                 ev->addr.type,
504                                                 eir_data.name,
505                                                 eir_data.dev_class);
506
507         eir_data_free(&eir_data);
508 }
509
510 static void mgmt_device_disconnected(int sk, uint16_t index, void *buf,
511                                                                 size_t len)
512 {
513         struct mgmt_addr_info *ev = buf;
514         struct controller_info *info;
515         char addr[18];
516
517         if (len < sizeof(*ev)) {
518                 error("Too small device_disconnected event");
519                 return;
520         }
521
522         ba2str(&ev->bdaddr, addr);
523
524         DBG("hci%u device %s disconnected", index, addr);
525
526         if (index > max_index) {
527                 error("Unexpected index %u in device_disconnected event", index);
528                 return;
529         }
530
531         info = &controllers[index];
532
533         btd_event_disconn_complete(&info->bdaddr, &ev->bdaddr);
534 }
535
536 static void mgmt_connect_failed(int sk, uint16_t index, void *buf, size_t len)
537 {
538         struct mgmt_ev_connect_failed *ev = buf;
539         struct controller_info *info;
540         char addr[18];
541
542         if (len < sizeof(*ev)) {
543                 error("Too small connect_failed event");
544                 return;
545         }
546
547         ba2str(&ev->addr.bdaddr, addr);
548
549         DBG("hci%u %s status %u", index, addr, ev->status);
550
551         if (index > max_index) {
552                 error("Unexpected index %u in connect_failed event", index);
553                 return;
554         }
555
556         info = &controllers[index];
557
558         btd_event_conn_failed(&info->bdaddr, &ev->addr.bdaddr, ev->status);
559
560         /* In the case of security mode 3 devices */
561         bonding_complete(info, &ev->addr.bdaddr, ev->status);
562 }
563
564 static int mgmt_pincode_reply(int index, bdaddr_t *bdaddr, const char *pin,
565                                                                 size_t pin_len)
566 {
567         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pin_code_reply)];
568         struct mgmt_hdr *hdr = (void *) buf;
569         size_t buf_len;
570         char addr[18];
571
572         ba2str(bdaddr, addr);
573         DBG("index %d addr %s pinlen %zu", index, addr, pin_len);
574
575         memset(buf, 0, sizeof(buf));
576
577         if (pin == NULL) {
578                 struct mgmt_cp_pin_code_neg_reply *cp;
579
580                 hdr->opcode = htobs(MGMT_OP_PIN_CODE_NEG_REPLY);
581                 hdr->len = htobs(sizeof(*cp));
582                 hdr->index = htobs(index);
583
584                 cp = (void *) &buf[sizeof(*hdr)];
585                 bacpy(&cp->addr.bdaddr, bdaddr);
586                 cp->addr.type = BDADDR_BREDR;
587
588                 buf_len = sizeof(*hdr) + sizeof(*cp);
589         } else {
590                 struct mgmt_cp_pin_code_reply *cp;
591
592                 if (pin_len > 16)
593                         return -EINVAL;
594
595                 hdr->opcode = htobs(MGMT_OP_PIN_CODE_REPLY);
596                 hdr->len = htobs(sizeof(*cp));
597                 hdr->index = htobs(index);
598
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);
604
605                 buf_len = sizeof(*hdr) + sizeof(*cp);
606         }
607
608         if (write(mgmt_sock, buf, buf_len) < 0)
609                 return -errno;
610
611         return 0;
612 }
613
614 static void mgmt_pin_code_request(int sk, uint16_t index, void *buf, size_t len)
615 {
616         struct mgmt_ev_pin_code_request *ev = buf;
617         struct controller_info *info;
618         char addr[18];
619         int err;
620
621         if (len < sizeof(*ev)) {
622                 error("Too small pin_code_request event");
623                 return;
624         }
625
626         ba2str(&ev->addr.bdaddr, addr);
627
628         DBG("hci%u %s", index, addr);
629
630         if (index > max_index) {
631                 error("Unexpected index %u in pin_code_request event", index);
632                 return;
633         }
634
635         info = &controllers[index];
636
637         err = btd_event_request_pin(&info->bdaddr, &ev->addr.bdaddr,
638                                                                 ev->secure);
639         if (err < 0) {
640                 error("btd_event_request_pin: %s", strerror(-err));
641                 mgmt_pincode_reply(index, &ev->addr.bdaddr, NULL, 0);
642         }
643 }
644
645 static int mgmt_confirm_reply(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
646                                                         gboolean success)
647 {
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;
651         char addr[18];
652
653         ba2str(bdaddr, addr);
654         DBG("index %d addr %s success %d", index, addr, success);
655
656         memset(buf, 0, sizeof(buf));
657
658         if (success)
659                 hdr->opcode = htobs(MGMT_OP_USER_CONFIRM_REPLY);
660         else
661                 hdr->opcode = htobs(MGMT_OP_USER_CONFIRM_NEG_REPLY);
662
663         hdr->len = htobs(sizeof(*cp));
664         hdr->index = htobs(index);
665
666         cp = (void *) &buf[sizeof(*hdr)];
667         bacpy(&cp->addr.bdaddr, bdaddr);
668         cp->addr.type = bdaddr_type;
669
670         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
671                 return -errno;
672
673         return 0;
674 }
675
676 static int mgmt_passkey_reply(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
677                                                         uint32_t passkey)
678 {
679         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_user_passkey_reply)];
680         struct mgmt_hdr *hdr = (void *) buf;
681         size_t buf_len;
682         char addr[18];
683
684         ba2str(bdaddr, addr);
685         DBG("index %d addr %s passkey %06u", index, addr, passkey);
686
687         memset(buf, 0, sizeof(buf));
688
689         hdr->index = htobs(index);
690         if (passkey == INVALID_PASSKEY) {
691                 struct mgmt_cp_user_passkey_neg_reply *cp;
692
693                 hdr->opcode = htobs(MGMT_OP_USER_PASSKEY_NEG_REPLY);
694                 hdr->len = htobs(sizeof(*cp));
695
696                 cp = (void *) &buf[sizeof(*hdr)];
697                 bacpy(&cp->addr.bdaddr, bdaddr);
698                 cp->addr.type = bdaddr_type;
699
700                 buf_len = sizeof(*hdr) + sizeof(*cp);
701         } else {
702                 struct mgmt_cp_user_passkey_reply *cp;
703
704                 hdr->opcode = htobs(MGMT_OP_USER_PASSKEY_REPLY);
705                 hdr->len = htobs(sizeof(*cp));
706
707                 cp = (void *) &buf[sizeof(*hdr)];
708                 bacpy(&cp->addr.bdaddr, bdaddr);
709                 cp->addr.type = bdaddr_type;
710                 cp->passkey = htobl(passkey);
711
712                 buf_len = sizeof(*hdr) + sizeof(*cp);
713         }
714
715         if (write(mgmt_sock, buf, buf_len) < 0)
716                 return -errno;
717
718         return 0;
719 }
720
721 static void mgmt_passkey_request(int sk, uint16_t index, void *buf, size_t len)
722 {
723         struct mgmt_ev_user_passkey_request *ev = buf;
724         struct controller_info *info;
725         char addr[18];
726         int err;
727
728         if (len < sizeof(*ev)) {
729                 error("Too small passkey_request event");
730                 return;
731         }
732
733         ba2str(&ev->addr.bdaddr, addr);
734
735         DBG("hci%u %s", index, addr);
736
737         if (index > max_index) {
738                 error("Unexpected index %u in passkey_request event", index);
739                 return;
740         }
741
742         info = &controllers[index];
743
744         err = btd_event_user_passkey(&info->bdaddr, &ev->addr.bdaddr);
745         if (err < 0) {
746                 error("btd_event_user_passkey: %s", strerror(-err));
747                 mgmt_passkey_reply(index, &ev->addr.bdaddr, ev->addr.type,
748                                                         INVALID_PASSKEY);
749         }
750 }
751
752 struct confirm_data {
753         int index;
754         bdaddr_t bdaddr;
755         uint8_t type;
756 };
757
758 static gboolean confirm_accept(gpointer user_data)
759 {
760         struct confirm_data *data = user_data;
761         struct controller_info *info = &controllers[data->index];
762
763         DBG("auto-accepting incoming pairing request");
764
765         if (data->index > max_index || !info->valid)
766                 return FALSE;
767
768         mgmt_confirm_reply(data->index, &data->bdaddr, data->type, TRUE);
769
770         return FALSE;
771 }
772
773 static void mgmt_user_confirm_request(int sk, uint16_t index, void *buf,
774                                                                 size_t len)
775 {
776         struct mgmt_ev_user_confirm_request *ev = buf;
777         struct controller_info *info;
778         char addr[18];
779         int err;
780
781         if (len < sizeof(*ev)) {
782                 error("Too small user_confirm_request event");
783                 return;
784         }
785
786         ba2str(&ev->addr.bdaddr, addr);
787
788         DBG("hci%u %s confirm_hint %u", index, addr, ev->confirm_hint);
789
790         if (index > max_index) {
791                 error("Unexpected index %u in user_confirm_request event",
792                                                                         index);
793                 return;
794         }
795
796         if (ev->confirm_hint) {
797                 struct confirm_data *data;
798
799                 data = g_new0(struct confirm_data, 1);
800                 data->index = index;
801                 bacpy(&data->bdaddr, &ev->addr.bdaddr);
802                 data->type = ev->addr.type;
803
804                 g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 1,
805                                                 confirm_accept, data, g_free);
806                 return;
807         }
808
809         info = &controllers[index];
810
811         err = btd_event_user_confirm(&info->bdaddr, &ev->addr.bdaddr,
812                                                         btohl(ev->value));
813         if (err < 0) {
814                 error("btd_event_user_confirm: %s", strerror(-err));
815                 mgmt_confirm_reply(index, &ev->addr.bdaddr, ev->addr.type,
816                                                                         FALSE);
817         }
818 }
819
820 static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
821 {
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);
826         else
827                 memcpy(uuid128, uuid, sizeof(*uuid));
828 }
829
830 static int mgmt_add_uuid(int index, uuid_t *uuid, uint8_t svc_hint)
831 {
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];
836         uuid_t uuid128;
837         uint128_t uint128;
838
839         DBG("index %d", index);
840
841         if (info->pending_uuid) {
842                 struct pending_uuid *pending = g_new0(struct pending_uuid, 1);
843
844                 memcpy(&pending->uuid, uuid, sizeof(*uuid));
845                 pending->svc_hint = svc_hint;
846
847                 info->pending_uuids = g_slist_append(info->pending_uuids,
848                                                                 pending);
849                 return 0;
850         }
851
852         uuid_to_uuid128(&uuid128, uuid);
853
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);
858
859         ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
860         htob128(&uint128, (uint128_t *) cp->uuid);
861
862         cp->svc_hint = svc_hint;
863
864         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
865                 return -errno;
866
867         info->pending_uuid = TRUE;
868
869         return 0;
870 }
871
872 static int mgmt_remove_uuid(int index, uuid_t *uuid)
873 {
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)];
877         uuid_t uuid128;
878         uint128_t uint128;
879
880         DBG("index %d", index);
881
882         uuid_to_uuid128(&uuid128, uuid);
883
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);
888
889         ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
890         htob128(&uint128, (uint128_t *) cp->uuid);
891
892         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
893                 return -errno;
894
895         return 0;
896 }
897
898 static int clear_uuids(int index)
899 {
900         uuid_t uuid_any;
901
902         memset(&uuid_any, 0, sizeof(uuid_any));
903         uuid_any.type = SDP_UUID128;
904
905         return mgmt_remove_uuid(index, &uuid_any);
906 }
907
908 static void read_index_list_complete(int sk, void *buf, size_t len)
909 {
910         struct mgmt_rp_read_index_list *rp = buf;
911         uint16_t num;
912         int i;
913
914         if (len < sizeof(*rp)) {
915                 error("Too small read index list complete event");
916                 return;
917         }
918
919         num = btohs(bt_get_unaligned(&rp->num_controllers));
920
921         if (num * sizeof(uint16_t) + sizeof(*rp) != len) {
922                 error("Incorrect packet size for index list event");
923                 return;
924         }
925
926         for (i = 0; i < num; i++) {
927                 uint16_t index;
928
929                 index = btohs(bt_get_unaligned(&rp->index[i]));
930
931                 add_controller(index);
932                 read_info(sk, index);
933         }
934 }
935
936 static int mgmt_set_powered(int index, gboolean powered)
937 {
938         struct controller_info *info = &controllers[index];
939
940         DBG("index %d powered %d pending_uuid %u", index, powered,
941                                                         info->pending_uuid);
942
943         if (powered) {
944                 if (info->pending_uuid) {
945                         info->pending_powered = TRUE;
946                         return 0;
947                 }
948         } else {
949                 info->pending_powered = FALSE;
950         }
951
952         return mgmt_set_mode(index, MGMT_OP_SET_POWERED, powered);
953 }
954
955 static int mgmt_set_name(int index, const char *name)
956 {
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)];
960
961         DBG("index %d, name %s", index, name);
962
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);
967
968         strncpy((char *) cp->name, name, sizeof(cp->name) - 1);
969
970         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
971                 return -errno;
972
973         return 0;
974 }
975
976 static int mgmt_set_dev_class(int index, uint8_t major, uint8_t minor)
977 {
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];
982
983         DBG("index %d major %u minor %u", index, major, minor);
984
985         if (info->pending_uuid) {
986                 info->major = major;
987                 info->minor = minor;
988                 info->pending_class = TRUE;
989                 return 0;
990         }
991
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);
996
997         cp->major = major;
998         cp->minor = minor;
999
1000         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1001                 return -errno;
1002
1003         return 0;
1004 }
1005
1006 static void read_info_complete(int sk, uint16_t index, void *buf, size_t len)
1007 {
1008         struct mgmt_rp_read_info *rp = buf;
1009         struct controller_info *info;
1010         struct btd_adapter *adapter;
1011         const char *name;
1012         uint8_t mode, major, minor;
1013         char addr[18];
1014
1015         if (len < sizeof(*rp)) {
1016                 error("Too small read info complete event");
1017                 return;
1018         }
1019
1020         if (index > max_index) {
1021                 error("Unexpected index %u in read info complete", index);
1022                 return;
1023         }
1024
1025         info = &controllers[index];
1026
1027         bacpy(&info->bdaddr, &rp->bdaddr);
1028         info->version = rp->version;
1029         info->manufacturer = btohs(bt_get_unaligned(&rp->manufacturer));
1030
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));
1035
1036         memcpy(info->dev_class, rp->dev_class, sizeof(info->dev_class));
1037
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);
1045
1046         clear_uuids(index);
1047
1048         adapter = btd_manager_register_adapter(index,
1049                                         mgmt_powered(info->current_settings));
1050         if (adapter == NULL) {
1051                 error("mgmtops: unable to register adapter");
1052                 return;
1053         }
1054
1055         update_settings(adapter, info->current_settings);
1056
1057         name = btd_adapter_get_name(adapter);
1058
1059         DBG("mgmtops setting name %s", name);
1060
1061         if (name)
1062                 mgmt_set_name(index, name);
1063         else
1064                 adapter_name_changed(adapter, (char *) rp->name);
1065
1066         btd_adapter_get_class(adapter, &major, &minor);
1067         mgmt_set_dev_class(index, major, minor);
1068
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);
1072                 return;
1073         }
1074
1075         if (mode != MODE_OFF && !mgmt_powered(info->current_settings))
1076                 mgmt_set_powered(index, TRUE);
1077         else {
1078                 get_connections(sk, index);
1079                 btd_adapter_start(adapter);
1080         }
1081
1082         btd_adapter_unref(adapter);
1083 }
1084
1085 static void disconnect_complete(int sk, uint16_t index, uint8_t status,
1086                                                         void *buf, size_t len)
1087 {
1088         struct mgmt_rp_disconnect *rp = buf;
1089         struct controller_info *info;
1090         char addr[18];
1091
1092         if (len < sizeof(*rp)) {
1093                 error("Too small disconnect complete event");
1094                 return;
1095         }
1096
1097         ba2str(&rp->addr.bdaddr, addr);
1098
1099         if (status != 0) {
1100                 error("Disconnecting %s failed with status %u", addr, status);
1101                 return;
1102         }
1103
1104         DBG("hci%d %s disconnected", index, addr);
1105
1106         if (index > max_index) {
1107                 error("Unexpected index %u in disconnect complete", index);
1108                 return;
1109         }
1110
1111         info = &controllers[index];
1112
1113         btd_event_disconn_complete(&info->bdaddr, &rp->addr.bdaddr);
1114
1115         bonding_complete(info, &rp->addr.bdaddr, HCI_CONNECTION_TERMINATED);
1116 }
1117
1118 static void pair_device_complete(int sk, uint16_t index, uint8_t status,
1119                                                         void *buf, size_t len)
1120 {
1121         struct mgmt_rp_pair_device *rp = buf;
1122         struct controller_info *info;
1123         char addr[18];
1124
1125         if (len < sizeof(*rp)) {
1126                 error("Too small pair_device complete event");
1127                 return;
1128         }
1129
1130         ba2str(&rp->addr.bdaddr, addr);
1131
1132         DBG("hci%d %s pairing complete status %u", index, addr, status);
1133
1134         if (index > max_index) {
1135                 error("Unexpected index %u in pair_device complete", index);
1136                 return;
1137         }
1138
1139         info = &controllers[index];
1140
1141         bonding_complete(info, &rp->addr.bdaddr, status);
1142 }
1143
1144 static void get_connections_complete(int sk, uint16_t index, void *buf,
1145                                                                 size_t len)
1146 {
1147         struct mgmt_rp_get_connections *rp = buf;
1148         struct controller_info *info;
1149         int i;
1150
1151         if (len < sizeof(*rp)) {
1152                 error("Too small get_connections complete event");
1153                 return;
1154         }
1155
1156         if (len < (sizeof(*rp) + (rp->conn_count * sizeof(bdaddr_t)))) {
1157                 error("Too small get_connections complete event");
1158                 return;
1159         }
1160
1161         if (index > max_index) {
1162                 error("Unexpected index %u in get_connections complete",
1163                                                                 index);
1164                 return;
1165         }
1166
1167         info = &controllers[index];
1168
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);
1172         }
1173 }
1174
1175 static void set_local_name_complete(int sk, uint16_t index, void *buf,
1176                                                                 size_t len)
1177 {
1178         struct mgmt_cp_set_local_name *rp = buf;
1179         struct controller_info *info;
1180         struct btd_adapter *adapter;
1181
1182         if (len < sizeof(*rp)) {
1183                 error("Too small set_local_name complete event");
1184                 return;
1185         }
1186
1187         DBG("hci%d name %s", index, (char *) rp->name);
1188
1189         if (index > max_index) {
1190                 error("Unexpected index %u in set_local_name complete", index);
1191                 return;
1192         }
1193
1194         info = &controllers[index];
1195
1196         adapter = manager_find_adapter(&info->bdaddr);
1197         if (adapter == NULL) {
1198                 DBG("Adapter not found");
1199                 return;
1200         }
1201
1202         adapter_name_changed(adapter, (char *) rp->name);
1203 }
1204
1205 static void read_local_oob_data_complete(int sk, uint16_t index, void *buf,
1206                                                                 size_t len)
1207 {
1208         struct mgmt_rp_read_local_oob_data *rp = buf;
1209         struct btd_adapter *adapter;
1210
1211         if (len != sizeof(*rp)) {
1212                 error("read_local_oob_data_complete event size mismatch "
1213                                         "(%zu != %zu)", len, sizeof(*rp));
1214                 return;
1215         }
1216
1217         if (index > max_index) {
1218                 error("Unexpected index %u in read_local_oob_data_complete",
1219                                                                 index);
1220                 return;
1221         }
1222
1223         DBG("hci%u", index);
1224
1225         adapter = manager_find_adapter_by_id(index);
1226
1227         if (adapter)
1228                 oob_read_local_data_complete(adapter, rp->hash, rp->randomizer);
1229 }
1230
1231 static void start_discovery_complete(int sk, uint16_t index, uint8_t status,
1232                                                      void *buf, size_t len)
1233 {
1234         uint8_t *type = buf;
1235         struct btd_adapter *adapter;
1236
1237         if (len != sizeof(*type)) {
1238                 error("start_discovery_complete event size mismatch "
1239                                         "(%zu != %zu)", len, sizeof(*type));
1240                 return;
1241         }
1242
1243         DBG("hci%u type %u status %u", index, *type, status);
1244
1245         if (index > max_index) {
1246                 error("Invalid index %u in start_discovery_complete", index);
1247                 return;
1248         }
1249
1250         if (!status)
1251                 return;
1252
1253         adapter = manager_find_adapter_by_id(index);
1254         if (adapter)
1255                 /* Start discovery failed, inform upper layers. */
1256                 adapter_set_discovering(adapter, FALSE);
1257 }
1258
1259 static void read_local_oob_data_failed(int sk, uint16_t index)
1260 {
1261         struct btd_adapter *adapter;
1262
1263         if (index > max_index) {
1264                 error("Unexpected index %u in read_local_oob_data_failed",
1265                                                                 index);
1266                 return;
1267         }
1268
1269         DBG("hci%u", index);
1270
1271         adapter = manager_find_adapter_by_id(index);
1272
1273         if (adapter)
1274                 oob_read_local_data_complete(adapter, NULL, NULL);
1275 }
1276
1277 static void handle_pending_uuids(uint16_t index)
1278 {
1279         struct controller_info *info;
1280         struct pending_uuid *pending;
1281
1282         DBG("index %d", index);
1283
1284         info = &controllers[index];
1285
1286         info->pending_uuid = FALSE;
1287
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);
1292                 }
1293
1294                 if (info->pending_powered) {
1295                         info->pending_powered = FALSE;
1296                         mgmt_set_powered(index, TRUE);
1297                 }
1298
1299                 return;
1300         }
1301
1302         pending = info->pending_uuids->data;
1303
1304         mgmt_add_uuid(index, &pending->uuid, pending->svc_hint);
1305
1306         info->pending_uuids = g_slist_remove(info->pending_uuids, pending);
1307         g_free(pending);
1308 }
1309
1310 static void mgmt_add_uuid_complete(int sk, uint16_t index, void *buf,
1311                                                                 size_t len)
1312 {
1313         DBG("index %d", index);
1314
1315         if (index > max_index) {
1316                 error("Unexpected index %u in add_uuid_complete event", index);
1317                 return;
1318         }
1319
1320         handle_pending_uuids(index);
1321 }
1322
1323 static void mgmt_cmd_complete(int sk, uint16_t index, void *buf, size_t len)
1324 {
1325         struct mgmt_ev_cmd_complete *ev = buf;
1326         uint16_t opcode;
1327
1328         DBG("");
1329
1330         if (len < sizeof(*ev)) {
1331                 error("Too small management command complete event packet");
1332                 return;
1333         }
1334
1335         opcode = btohs(bt_get_unaligned(&ev->opcode));
1336
1337         len -= sizeof(*ev);
1338
1339         switch (opcode) {
1340         case MGMT_OP_READ_VERSION:
1341                 read_version_complete(sk, ev->data, len);
1342                 break;
1343         case MGMT_OP_READ_INDEX_LIST:
1344                 read_index_list_complete(sk, ev->data, len);
1345                 break;
1346         case MGMT_OP_READ_INFO:
1347                 read_info_complete(sk, index, ev->data, len);
1348                 break;
1349         case MGMT_OP_SET_POWERED:
1350                 mgmt_new_settings(sk, index, ev->data, len);
1351                 break;
1352         case MGMT_OP_SET_DISCOVERABLE:
1353                 mgmt_new_settings(sk, index, ev->data, len);
1354                 break;
1355         case MGMT_OP_SET_CONNECTABLE:
1356                 mgmt_new_settings(sk, index, ev->data, len);
1357                 break;
1358         case MGMT_OP_SET_PAIRABLE:
1359                 mgmt_new_settings(sk, index, ev->data, len);
1360                 break;
1361         case MGMT_OP_SET_SSP:
1362                 DBG("set_ssp complete");
1363                 break;
1364         case MGMT_OP_SET_LE:
1365                 DBG("set_le complete");
1366                 break;
1367         case MGMT_OP_ADD_UUID:
1368                 mgmt_add_uuid_complete(sk, index, ev->data, len);
1369                 break;
1370         case MGMT_OP_REMOVE_UUID:
1371                 DBG("remove_uuid complete");
1372                 break;
1373         case MGMT_OP_SET_DEV_CLASS:
1374                 DBG("set_dev_class complete");
1375                 break;
1376         case MGMT_OP_LOAD_LINK_KEYS:
1377                 DBG("load_link_keys complete");
1378                 break;
1379         case MGMT_OP_CANCEL_PAIR_DEVICE:
1380                 DBG("cancel_pair_device complete");
1381                 break;
1382         case MGMT_OP_UNPAIR_DEVICE:
1383                 DBG("unpair_device complete");
1384                 break;
1385         case MGMT_OP_DISCONNECT:
1386                 DBG("disconnect complete");
1387                 disconnect_complete(sk, index, ev->status, ev->data, len);
1388                 break;
1389         case MGMT_OP_GET_CONNECTIONS:
1390                 get_connections_complete(sk, index, ev->data, len);
1391                 break;
1392         case MGMT_OP_PIN_CODE_REPLY:
1393                 DBG("pin_code_reply complete");
1394                 break;
1395         case MGMT_OP_PIN_CODE_NEG_REPLY:
1396                 DBG("pin_code_neg_reply complete");
1397                 break;
1398         case MGMT_OP_SET_IO_CAPABILITY:
1399                 DBG("set_io_capability complete");
1400                 break;
1401         case MGMT_OP_PAIR_DEVICE:
1402                 pair_device_complete(sk, index, ev->status, ev->data, len);
1403                 break;
1404         case MGMT_OP_USER_CONFIRM_REPLY:
1405                 DBG("user_confirm_reply complete");
1406                 break;
1407         case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1408                 DBG("user_confirm_net_reply complete");
1409                 break;
1410         case MGMT_OP_SET_LOCAL_NAME:
1411                 set_local_name_complete(sk, index, ev->data, len);
1412                 break;
1413         case MGMT_OP_READ_LOCAL_OOB_DATA:
1414                 read_local_oob_data_complete(sk, index, ev->data, len);
1415                 break;
1416         case MGMT_OP_ADD_REMOTE_OOB_DATA:
1417                 DBG("add_remote_oob_data complete");
1418                 break;
1419         case MGMT_OP_REMOVE_REMOTE_OOB_DATA:
1420                 DBG("remove_remote_oob_data complete");
1421                 break;
1422         case MGMT_OP_BLOCK_DEVICE:
1423                 DBG("block_device complete");
1424                 break;
1425         case MGMT_OP_UNBLOCK_DEVICE:
1426                 DBG("unblock_device complete");
1427                 break;
1428         case MGMT_OP_SET_FAST_CONNECTABLE:
1429                 DBG("set_fast_connectable complete");
1430                 break;
1431         case MGMT_OP_START_DISCOVERY:
1432                 start_discovery_complete(sk, index, ev->status, ev->data, len);
1433                 break;
1434         case MGMT_OP_STOP_DISCOVERY:
1435                 DBG("stop_discovery complete");
1436                 break;
1437         case MGMT_OP_SET_DEVICE_ID:
1438                 DBG("set_did complete");
1439                 break;
1440         default:
1441                 error("Unknown command complete for opcode %u", opcode);
1442                 break;
1443         }
1444 }
1445
1446 static void mgmt_add_uuid_busy(int sk, uint16_t index)
1447 {
1448         struct controller_info *info;
1449
1450         DBG("index %d", index);
1451
1452         info = &controllers[index];
1453         info->pending_cod_change = TRUE;
1454 }
1455
1456 static void mgmt_cmd_status(int sk, uint16_t index, void *buf, size_t len)
1457 {
1458         struct mgmt_ev_cmd_status *ev = buf;
1459         uint16_t opcode;
1460
1461         if (len < sizeof(*ev)) {
1462                 error("Too small management command status event packet");
1463                 return;
1464         }
1465
1466         opcode = btohs(bt_get_unaligned(&ev->opcode));
1467
1468         if (!ev->status) {
1469                 DBG("%s (0x%04x) cmd_status %u", mgmt_opstr(opcode), opcode,
1470                                                                 ev->status);
1471                 return;
1472         }
1473
1474         switch (opcode) {
1475         case MGMT_OP_READ_LOCAL_OOB_DATA:
1476                 read_local_oob_data_failed(sk, index);
1477                 break;
1478         case MGMT_OP_ADD_UUID:
1479                 if (ev->status == MGMT_STATUS_BUSY) {
1480                         mgmt_add_uuid_busy(sk, index);
1481                         return;
1482                 }
1483                 break;
1484         }
1485
1486         error("hci%u: %s (0x%04x) failed: %s (0x%02x)", index,
1487                         mgmt_opstr(opcode), opcode, mgmt_errstr(ev->status),
1488                         ev->status);
1489 }
1490
1491 static void mgmt_controller_error(int sk, uint16_t index, void *buf, size_t len)
1492 {
1493         struct mgmt_ev_controller_error *ev = buf;
1494
1495         if (len < sizeof(*ev)) {
1496                 error("Too small management controller error event packet");
1497                 return;
1498         }
1499
1500         DBG("index %u error_code %u", index, ev->error_code);
1501 }
1502
1503 static void mgmt_auth_failed(int sk, uint16_t index, void *buf, size_t len)
1504 {
1505         struct controller_info *info;
1506         struct mgmt_ev_auth_failed *ev = buf;
1507
1508         if (len < sizeof(*ev)) {
1509                 error("Too small mgmt_auth_failed event packet");
1510                 return;
1511         }
1512
1513         DBG("hci%u auth failed status %u", index, ev->status);
1514
1515         if (index > max_index) {
1516                 error("Unexpected index %u in auth_failed event", index);
1517                 return;
1518         }
1519
1520         info = &controllers[index];
1521
1522         bonding_complete(info, &ev->addr.bdaddr, ev->status);
1523 }
1524
1525 static void mgmt_local_name_changed(int sk, uint16_t index, void *buf, size_t len)
1526 {
1527         struct mgmt_cp_set_local_name *ev = buf;
1528         struct controller_info *info;
1529         struct btd_adapter *adapter;
1530
1531         if (len < sizeof(*ev)) {
1532                 error("Too small mgmt_local_name_changed event packet");
1533                 return;
1534         }
1535
1536         DBG("hci%u local name changed: %s", index, (char *) ev->name);
1537
1538         if (index > max_index) {
1539                 error("Unexpected index %u in name_changed event", index);
1540                 return;
1541         }
1542
1543         info = &controllers[index];
1544
1545         adapter = manager_find_adapter(&info->bdaddr);
1546         if (adapter)
1547                 adapter_name_changed(adapter, (char *) ev->name);
1548 }
1549
1550 static void mgmt_device_found(int sk, uint16_t index, void *buf, size_t len)
1551 {
1552         struct mgmt_ev_device_found *ev = buf;
1553         struct controller_info *info;
1554         char addr[18];
1555         uint32_t flags;
1556         uint16_t eir_len;
1557         uint8_t *eir;
1558         gboolean confirm_name;
1559
1560         if (len < sizeof(*ev)) {
1561                 error("mgmt_device_found too short (%zu bytes)", len);
1562                 return;
1563         }
1564
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);
1569                 return;
1570         }
1571
1572         if (index > max_index) {
1573                 error("Unexpected index %u in device_found event", index);
1574                 return;
1575         }
1576
1577         info = &controllers[index];
1578
1579         if (eir_len == 0)
1580                 eir = NULL;
1581         else
1582                 eir = ev->eir;
1583
1584         flags = btohl(ev->flags);
1585
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);
1589
1590         if (flags & MGMT_DEV_FOUND_LEGACY_PAIRING)
1591                 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1592                                                                         TRUE);
1593         else
1594                 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1595                                                                         FALSE);
1596
1597         confirm_name = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
1598
1599         btd_event_device_found(&info->bdaddr, &ev->addr.bdaddr,
1600                                                 ev->addr.type,
1601                                                 ev->rssi, confirm_name,
1602                                                 eir, eir_len);
1603 }
1604
1605 static void mgmt_discovering(int sk, uint16_t index, void *buf, size_t len)
1606 {
1607         struct mgmt_ev_discovering *ev = buf;
1608         struct controller_info *info;
1609         struct btd_adapter *adapter;
1610
1611         if (len < sizeof(*ev)) {
1612                 error("Too small discovering event");
1613                 return;
1614         }
1615
1616         DBG("Controller %u type %u discovering %u", index,
1617                                         ev->type, ev->discovering);
1618
1619         if (index > max_index) {
1620                 error("Unexpected index %u in discovering event", index);
1621                 return;
1622         }
1623
1624         info = &controllers[index];
1625
1626         adapter = manager_find_adapter(&info->bdaddr);
1627         if (!adapter)
1628                 return;
1629
1630         adapter_set_discovering(adapter, ev->discovering);
1631 }
1632
1633 static void mgmt_device_blocked(int sk, uint16_t index, void *buf, size_t len)
1634 {
1635         struct controller_info *info;
1636         struct mgmt_ev_device_blocked *ev = buf;
1637         char addr[18];
1638
1639         if (len < sizeof(*ev)) {
1640                 error("Too small mgmt_device_blocked event packet");
1641                 return;
1642         }
1643
1644         ba2str(&ev->addr.bdaddr, addr);
1645         DBG("Device blocked, index %u, addr %s", index, addr);
1646
1647         if (index > max_index) {
1648                 error("Unexpected index %u in device_blocked event", index);
1649                 return;
1650         }
1651
1652         info = &controllers[index];
1653
1654         btd_event_device_blocked(&info->bdaddr, &ev->addr.bdaddr);
1655 }
1656
1657 static void mgmt_device_unblocked(int sk, uint16_t index, void *buf, size_t len)
1658 {
1659         struct controller_info *info;
1660         struct mgmt_ev_device_unblocked *ev = buf;
1661         char addr[18];
1662
1663         if (len < sizeof(*ev)) {
1664                 error("Too small mgmt_device_unblocked event packet");
1665                 return;
1666         }
1667
1668         ba2str(&ev->addr.bdaddr, addr);
1669         DBG("Device unblocked, index %u, addr %s", index, addr);
1670
1671         if (index > max_index) {
1672                 error("Unexpected index %u in device_unblocked event", index);
1673                 return;
1674         }
1675
1676         info = &controllers[index];
1677
1678         btd_event_device_unblocked(&info->bdaddr, &ev->addr.bdaddr);
1679 }
1680
1681 static void mgmt_device_unpaired(int sk, uint16_t index, void *buf, size_t len)
1682 {
1683         struct controller_info *info;
1684         struct mgmt_ev_device_unpaired *ev = buf;
1685         char addr[18];
1686
1687         if (len < sizeof(*ev)) {
1688                 error("Too small mgmt_device_unpaired event packet");
1689                 return;
1690         }
1691
1692         ba2str(&ev->addr.bdaddr, addr);
1693         DBG("Device upaired, index %u, addr %s", index, addr);
1694
1695         if (index > max_index) {
1696                 error("Unexpected index %u in device_unpaired event", index);
1697                 return;
1698         }
1699
1700         info = &controllers[index];
1701
1702         btd_event_device_unpaired(&info->bdaddr, &ev->addr.bdaddr);
1703 }
1704
1705 static void mgmt_new_ltk(int sk, uint16_t index, void *buf, size_t len)
1706 {
1707         struct mgmt_ev_new_long_term_key *ev = buf;
1708         struct controller_info *info;
1709
1710         if (len != sizeof(*ev)) {
1711                 error("mgmt_new_ltk event size mismatch (%zu != %zu)",
1712                                                         len, sizeof(*ev));
1713                 return;
1714         }
1715
1716         DBG("Controller %u new LTK authenticated %u enc_size %u", index,
1717                                 ev->key.authenticated, ev->key.enc_size);
1718
1719         if (index > max_index) {
1720                 error("Unexpected index %u in new_key event", index);
1721                 return;
1722         }
1723
1724         info = &controllers[index];
1725
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);
1731         }
1732
1733         if (ev->key.master)
1734                 bonding_complete(info, &ev->key.addr.bdaddr, 0);
1735 }
1736
1737 static void mgmt_cod_changed(int sk, uint16_t index)
1738 {
1739         struct controller_info *info;
1740
1741         DBG("index %d", index);
1742
1743         if (index > max_index) {
1744                 error("Unexpected index %u in mgmt_cod_changed event", index);
1745                 return;
1746         }
1747
1748         info = &controllers[index];
1749
1750         if (info->pending_cod_change) {
1751                 info->pending_cod_change = FALSE;
1752                 handle_pending_uuids(index);
1753         }
1754 }
1755
1756 static gboolean mgmt_event(GIOChannel *io, GIOCondition cond, gpointer user_data)
1757 {
1758         char buf[MGMT_BUF_SIZE];
1759         struct mgmt_hdr *hdr = (void *) buf;
1760         int sk;
1761         ssize_t ret;
1762         uint16_t len, opcode, index;
1763
1764         DBG("cond %d", cond);
1765
1766         if (cond & G_IO_NVAL)
1767                 return FALSE;
1768
1769         sk = g_io_channel_unix_get_fd(io);
1770
1771         if (cond & (G_IO_ERR | G_IO_HUP)) {
1772                 error("Error on management socket");
1773                 return FALSE;
1774         }
1775
1776         ret = read(sk, buf, sizeof(buf));
1777         if (ret < 0) {
1778                 error("Unable to read from management socket: %s (%d)",
1779                                                 strerror(errno), errno);
1780                 return TRUE;
1781         }
1782
1783         DBG("Received %zd bytes from management socket", ret);
1784
1785         if (ret < MGMT_HDR_SIZE) {
1786                 error("Too small Management packet");
1787                 return TRUE;
1788         }
1789
1790         opcode = btohs(bt_get_unaligned(&hdr->opcode));
1791         len = btohs(bt_get_unaligned(&hdr->len));
1792         index = btohs(bt_get_unaligned(&hdr->index));
1793
1794         if (ret != MGMT_HDR_SIZE + len) {
1795                 error("Packet length mismatch. ret %zd len %u", ret, len);
1796                 return TRUE;
1797         }
1798
1799         switch (opcode) {
1800         case MGMT_EV_CMD_COMPLETE:
1801                 mgmt_cmd_complete(sk, index, buf + MGMT_HDR_SIZE, len);
1802                 break;
1803         case MGMT_EV_CMD_STATUS:
1804                 mgmt_cmd_status(sk, index, buf + MGMT_HDR_SIZE, len);
1805                 break;
1806         case MGMT_EV_CONTROLLER_ERROR:
1807                 mgmt_controller_error(sk, index, buf + MGMT_HDR_SIZE, len);
1808                 break;
1809         case MGMT_EV_INDEX_ADDED:
1810                 mgmt_index_added(sk, index);
1811                 break;
1812         case MGMT_EV_INDEX_REMOVED:
1813                 mgmt_index_removed(sk, index);
1814                 break;
1815         case MGMT_EV_NEW_SETTINGS:
1816                 mgmt_new_settings(sk, index, buf + MGMT_HDR_SIZE, len);
1817                 break;
1818         case MGMT_EV_CLASS_OF_DEV_CHANGED:
1819                 mgmt_cod_changed(sk, index);
1820                 break;
1821         case MGMT_EV_NEW_LINK_KEY:
1822                 mgmt_new_link_key(sk, index, buf + MGMT_HDR_SIZE, len);
1823                 break;
1824         case MGMT_EV_DEVICE_CONNECTED:
1825                 mgmt_device_connected(sk, index, buf + MGMT_HDR_SIZE, len);
1826                 break;
1827         case MGMT_EV_DEVICE_DISCONNECTED:
1828                 mgmt_device_disconnected(sk, index, buf + MGMT_HDR_SIZE, len);
1829                 break;
1830         case MGMT_EV_CONNECT_FAILED:
1831                 mgmt_connect_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1832                 break;
1833         case MGMT_EV_PIN_CODE_REQUEST:
1834                 mgmt_pin_code_request(sk, index, buf + MGMT_HDR_SIZE, len);
1835                 break;
1836         case MGMT_EV_USER_CONFIRM_REQUEST:
1837                 mgmt_user_confirm_request(sk, index, buf + MGMT_HDR_SIZE, len);
1838                 break;
1839         case MGMT_EV_AUTH_FAILED:
1840                 mgmt_auth_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1841                 break;
1842         case MGMT_EV_LOCAL_NAME_CHANGED:
1843                 mgmt_local_name_changed(sk, index, buf + MGMT_HDR_SIZE, len);
1844                 break;
1845         case MGMT_EV_DEVICE_FOUND:
1846                 mgmt_device_found(sk, index, buf + MGMT_HDR_SIZE, len);
1847                 break;
1848         case MGMT_EV_DISCOVERING:
1849                 mgmt_discovering(sk, index, buf + MGMT_HDR_SIZE, len);
1850                 break;
1851         case MGMT_EV_DEVICE_BLOCKED:
1852                 mgmt_device_blocked(sk, index, buf + MGMT_HDR_SIZE, len);
1853                 break;
1854         case MGMT_EV_DEVICE_UNBLOCKED:
1855                 mgmt_device_unblocked(sk, index, buf + MGMT_HDR_SIZE, len);
1856                 break;
1857         case MGMT_EV_DEVICE_UNPAIRED:
1858                 mgmt_device_unpaired(sk, index, buf + MGMT_HDR_SIZE, len);
1859                 break;
1860         case MGMT_EV_USER_PASSKEY_REQUEST:
1861                 mgmt_passkey_request(sk, index, buf + MGMT_HDR_SIZE, len);
1862                 break;
1863         case MGMT_EV_NEW_LONG_TERM_KEY:
1864                 mgmt_new_ltk(sk, index, buf + MGMT_HDR_SIZE, len);
1865                 break;
1866         default:
1867                 error("Unknown Management opcode %u (index %u)", opcode, index);
1868                 break;
1869         }
1870
1871         return TRUE;
1872 }
1873
1874 static int mgmt_setup(void)
1875 {
1876         struct mgmt_hdr hdr;
1877         struct sockaddr_hci addr;
1878         GIOChannel *io;
1879         GIOCondition condition;
1880         int dd, err;
1881
1882         dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
1883         if (dd < 0)
1884                 return -errno;
1885
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;
1890
1891         if (bind(dd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1892                 err = -errno;
1893                 goto fail;
1894         }
1895
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) {
1900                 err = -errno;
1901                 goto fail;
1902         }
1903
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);
1908
1909         mgmt_sock = dd;
1910
1911         info("Bluetooth Management interface initialized");
1912
1913         return 0;
1914
1915 fail:
1916         close(dd);
1917         return err;
1918 }
1919
1920 static void mgmt_cleanup(void)
1921 {
1922         g_free(controllers);
1923         controllers = NULL;
1924         max_index = -1;
1925
1926         if (mgmt_sock >= 0) {
1927                 close(mgmt_sock);
1928                 mgmt_sock = -1;
1929         }
1930
1931         if (mgmt_watch > 0) {
1932                 g_source_remove(mgmt_watch);
1933                 mgmt_watch = 0;
1934         }
1935 }
1936
1937 static int mgmt_start_discovery(int index)
1938 {
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];
1943
1944         DBG("index %d", index);
1945
1946         info->discov_type = 0;
1947
1948         if (mgmt_bredr(info->current_settings))
1949                 hci_set_bit(BDADDR_BREDR, &info->discov_type);
1950
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);
1954         }
1955
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);
1960
1961         cp->type = info->discov_type;
1962
1963         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1964                 return -errno;
1965
1966         return 0;
1967 }
1968
1969 static int mgmt_stop_discovery(int index)
1970 {
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];
1975
1976         DBG("index %d", index);
1977
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);
1982
1983         cp->type = info->discov_type;
1984
1985         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1986                 return -errno;
1987
1988         return 0;
1989 }
1990
1991 static int mgmt_set_fast_connectable(int index, gboolean enable)
1992 {
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)];
1996
1997         DBG("index %d enable %d", index, enable);
1998
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);
2003
2004         cp->val = enable;
2005
2006         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2007                 return -errno;
2008
2009         return 0;
2010 }
2011
2012 static int mgmt_read_clock(int index, bdaddr_t *bdaddr, int which, int timeout,
2013                                         uint32_t *clock, uint16_t *accuracy)
2014 {
2015         char addr[18];
2016
2017         ba2str(bdaddr, addr);
2018         DBG("index %d addr %s which %d timeout %d", index, addr, which,
2019                                                                 timeout);
2020
2021         return -ENOSYS;
2022 }
2023
2024 static int mgmt_read_bdaddr(int index, bdaddr_t *bdaddr)
2025 {
2026         char addr[18];
2027         struct controller_info *info = &controllers[index];
2028
2029         ba2str(&info->bdaddr, addr);
2030         DBG("index %d addr %s", index, addr);
2031
2032         if (!info->valid)
2033                 return -ENODEV;
2034
2035         bacpy(bdaddr, &info->bdaddr);
2036
2037         return 0;
2038 }
2039
2040 static int mgmt_block_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2041 {
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;
2045         size_t buf_len;
2046         char addr[18];
2047
2048         ba2str(bdaddr, addr);
2049         DBG("index %d addr %s", index, addr);
2050
2051         memset(buf, 0, sizeof(buf));
2052
2053         hdr->opcode = htobs(MGMT_OP_BLOCK_DEVICE);
2054         hdr->len = htobs(sizeof(*cp));
2055         hdr->index = htobs(index);
2056
2057         cp = (void *) &buf[sizeof(*hdr)];
2058         bacpy(&cp->addr.bdaddr, bdaddr);
2059         cp->addr.type = bdaddr_type;
2060
2061         buf_len = sizeof(*hdr) + sizeof(*cp);
2062
2063         if (write(mgmt_sock, buf, buf_len) < 0)
2064                 return -errno;
2065
2066         return 0;
2067 }
2068
2069 static int mgmt_unblock_device(int index, bdaddr_t *bdaddr,
2070                                                         uint8_t bdaddr_type)
2071 {
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;
2075         size_t buf_len;
2076         char addr[18];
2077
2078         ba2str(bdaddr, addr);
2079         DBG("index %d addr %s", index, addr);
2080
2081         memset(buf, 0, sizeof(buf));
2082
2083         hdr->opcode = htobs(MGMT_OP_UNBLOCK_DEVICE);
2084         hdr->len = htobs(sizeof(*cp));
2085         hdr->index = htobs(index);
2086
2087         cp = (void *) &buf[sizeof(*hdr)];
2088         bacpy(&cp->addr.bdaddr, bdaddr);
2089         cp->addr.type = bdaddr_type;
2090
2091         buf_len = sizeof(*hdr) + sizeof(*cp);
2092
2093         if (write(mgmt_sock, buf, buf_len) < 0)
2094                 return -errno;
2095
2096         return 0;
2097 }
2098
2099 static int mgmt_get_conn_list(int index, GSList **conns)
2100 {
2101         struct controller_info *info = &controllers[index];
2102
2103         DBG("index %d", index);
2104
2105         *conns = info->connections;
2106         info->connections = NULL;
2107
2108         return 0;
2109 }
2110
2111 static int mgmt_disconnect(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2112 {
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)];
2116         char addr[18];
2117
2118         ba2str(bdaddr, addr);
2119         DBG("index %d %s", index, addr);
2120
2121         memset(buf, 0, sizeof(buf));
2122         hdr->opcode = htobs(MGMT_OP_DISCONNECT);
2123         hdr->len = htobs(sizeof(*cp));
2124         hdr->index = htobs(index);
2125
2126         bacpy(&cp->addr.bdaddr, bdaddr);
2127         cp->addr.type = bdaddr_type;
2128
2129         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2130                 error("write: %s (%d)", strerror(errno), errno);
2131
2132         return 0;
2133 }
2134
2135 static int mgmt_unpair_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2136 {
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)];
2140         char addr[18];
2141
2142         ba2str(bdaddr, addr);
2143         DBG("index %d addr %s", index, addr);
2144
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);
2149
2150         bacpy(&cp->addr.bdaddr, bdaddr);
2151         cp->addr.type = bdaddr_type;
2152         cp->disconnect = 1;
2153
2154         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2155                 return -errno;
2156
2157         return 0;
2158 }
2159
2160 static int mgmt_encrypt_link(int index, bdaddr_t *dst, bt_hci_result_t cb,
2161                                                         gpointer user_data)
2162 {
2163         char addr[18];
2164
2165         ba2str(dst, addr);
2166         DBG("index %d addr %s", index, addr);
2167
2168         return -ENOSYS;
2169 }
2170
2171 static int mgmt_set_did(int index, uint16_t vendor, uint16_t product,
2172                                         uint16_t version, uint16_t source)
2173 {
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)];
2177
2178         DBG("index %d source %x vendor %x product %x version %x",
2179                                 index, source, vendor, product, version);
2180
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);
2185
2186         cp->source = htobs(source);
2187         cp->vendor = htobs(vendor);
2188         cp->product = htobs(product);
2189         cp->version = htobs(version);
2190
2191         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2192                 return -errno;
2193
2194         return 0;
2195 }
2196
2197 static int mgmt_disable_cod_cache(int index)
2198 {
2199         DBG("index %d", index);
2200
2201         /* The cache control is handled automatically for mgmt */
2202         return 0;
2203 }
2204
2205 static int mgmt_restore_powered(int index)
2206 {
2207         DBG("index %d", index);
2208         return -ENOSYS;
2209 }
2210
2211 static int mgmt_load_link_keys(int index, GSList *keys, gboolean debug_keys)
2212 {
2213         char *buf;
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;
2218         GSList *l;
2219         int err;
2220
2221         key_count = g_slist_length(keys);
2222
2223         DBG("index %d keys %zu debug_keys %d", index, key_count, debug_keys);
2224
2225         cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2226
2227         buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2228         if (buf == NULL)
2229                 return -ENOMEM;
2230
2231         hdr = (void *) buf;
2232         hdr->opcode = htobs(MGMT_OP_LOAD_LINK_KEYS);
2233         hdr->len = htobs(cp_size);
2234         hdr->index = htobs(index);
2235
2236         cp = (void *) (buf + sizeof(*hdr));
2237         cp->debug_keys = debug_keys;
2238         cp->key_count = htobs(key_count);
2239
2240         for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2241                 struct link_key_info *info = l->data;
2242
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;
2248         }
2249
2250         if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2251                 err = -errno;
2252         else
2253                 err = 0;
2254
2255         g_free(buf);
2256
2257         return err;
2258 }
2259
2260 static int mgmt_set_io_capability(int index, uint8_t io_capability)
2261 {
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)];
2265
2266         DBG("hci%d io_capability 0x%02x", index, io_capability);
2267
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);
2272
2273         cp->io_capability = io_capability;
2274
2275         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2276                 return -errno;
2277
2278         return 0;
2279 }
2280
2281 static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t addr_type, uint8_t io_cap)
2282 {
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)];
2286         char addr[18];
2287
2288         ba2str(bdaddr, addr);
2289         DBG("hci%d bdaddr %s io_cap 0x%02x", index, addr, io_cap);
2290
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);
2295
2296         bacpy(&cp->addr.bdaddr, bdaddr);
2297         cp->addr.type = addr_type;
2298         cp->io_cap = io_cap;
2299
2300         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2301                 return -errno;
2302
2303         return 0;
2304 }
2305
2306 static int mgmt_cancel_bonding(int index, bdaddr_t *bdaddr)
2307 {
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)];
2311         char addr[18];
2312
2313         ba2str(bdaddr, addr);
2314         DBG("hci%d bdaddr %s", index, addr);
2315
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);
2320
2321         bacpy(&cp->bdaddr, bdaddr);
2322
2323         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2324                 return -errno;
2325
2326         return 0;
2327 }
2328
2329 static int mgmt_read_local_oob_data(int index)
2330 {
2331         struct mgmt_hdr hdr;
2332
2333         DBG("hci%d", index);
2334
2335         hdr.opcode = htobs(MGMT_OP_READ_LOCAL_OOB_DATA);
2336         hdr.len = 0;
2337         hdr.index = htobs(index);
2338
2339         if (write(mgmt_sock, &hdr, sizeof(hdr)) < 0)
2340                 return -errno;
2341
2342         return 0;
2343 }
2344
2345 static int mgmt_add_remote_oob_data(int index, bdaddr_t *bdaddr,
2346                                         uint8_t *hash, uint8_t *randomizer)
2347 {
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)];
2351         char addr[18];
2352
2353         ba2str(bdaddr, addr);
2354         DBG("hci%d bdaddr %s", index, addr);
2355
2356         memset(buf, 0, sizeof(buf));
2357
2358         hdr->opcode = htobs(MGMT_OP_ADD_REMOTE_OOB_DATA);
2359         hdr->index = htobs(index);
2360         hdr->len = htobs(sizeof(*cp));
2361
2362         bacpy(&cp->addr.bdaddr, bdaddr);
2363         memcpy(cp->hash, hash, 16);
2364         memcpy(cp->randomizer, randomizer, 16);
2365
2366         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2367                 return -errno;
2368
2369         return 0;
2370 }
2371
2372 static int mgmt_remove_remote_oob_data(int index, bdaddr_t *bdaddr)
2373 {
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)];
2377         char addr[18];
2378
2379         ba2str(bdaddr, addr);
2380         DBG("hci%d bdaddr %s", index, addr);
2381
2382         memset(buf, 0, sizeof(buf));
2383
2384         hdr->opcode = htobs(MGMT_OP_REMOVE_REMOTE_OOB_DATA);
2385         hdr->index = htobs(index);
2386         hdr->len = htobs(sizeof(*cp));
2387
2388         bacpy(&cp->addr.bdaddr, bdaddr);
2389
2390         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2391                 return -errno;
2392
2393         return 0;
2394 }
2395
2396 static int mgmt_confirm_name(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
2397                                                         gboolean name_known)
2398 {
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)];
2402         char addr[18];
2403
2404         ba2str(bdaddr, addr);
2405         DBG("hci%d bdaddr %s name_known %u", index, addr, name_known);
2406
2407         memset(buf, 0, sizeof(buf));
2408
2409         hdr->opcode = htobs(MGMT_OP_CONFIRM_NAME);
2410         hdr->index = htobs(index);
2411         hdr->len = htobs(sizeof(*cp));
2412
2413         bacpy(&cp->addr.bdaddr, bdaddr);
2414         cp->addr.type = bdaddr_type;
2415         cp->name_known = name_known;
2416
2417         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2418                 return -errno;
2419
2420         return 0;
2421 }
2422
2423 static int mgmtops_load_ltks(int index, GSList *keys)
2424 {
2425         char *buf;
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;
2430         GSList *l;
2431         int err;
2432
2433         key_count = g_slist_length(keys);
2434
2435         DBG("index %d keys %zu", index, key_count);
2436
2437         cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2438
2439         buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2440         if (buf == NULL)
2441                 return -ENOMEM;
2442
2443         hdr = (void *) buf;
2444         hdr->opcode = htobs(MGMT_OP_LOAD_LONG_TERM_KEYS);
2445         hdr->len = htobs(cp_size);
2446         hdr->index = htobs(index);
2447
2448         cp = (void *) (buf + sizeof(*hdr));
2449         cp->key_count = htobs(key_count);
2450
2451         for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2452                 struct smp_ltk_info *info = l->data;
2453
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;
2462         }
2463
2464         if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2465                 err = -errno;
2466         else
2467                 err = 0;
2468
2469         g_free(buf);
2470
2471         return err;
2472 }
2473
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,
2510 };
2511
2512 static int mgmt_init(void)
2513 {
2514         return btd_register_adapter_ops(&mgmt_ops, TRUE);
2515 }
2516
2517 static void mgmt_exit(void)
2518 {
2519         btd_adapter_cleanup_ops(&mgmt_ops);
2520 }
2521
2522 BLUETOOTH_PLUGIN_DEFINE(mgmtops, VERSION,
2523                 BLUETOOTH_PLUGIN_PRIORITY_LOW, mgmt_init, mgmt_exit)