3a99b56265f36f29b6420921371661318bbd60b6
[framework/connectivity/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 static void mgmt_passkey_notify(int sk, uint16_t index, void *buf, size_t len)
753 {
754         struct mgmt_ev_passkey_notify *ev = buf;
755         struct controller_info *info;
756         uint32_t passkey;
757         char addr[18];
758         int err;
759
760         if (len < sizeof(*ev)) {
761                 error("Too small passkey_notify event");
762                 return;
763         }
764
765         ba2str(&ev->addr.bdaddr, addr);
766
767         DBG("hci%u %s", index, addr);
768
769         if (index > max_index) {
770                 error("Unexpected index %u in passkey_notify event", index);
771                 return;
772         }
773
774         info = &controllers[index];
775
776         passkey = bt_get_le32(&ev->passkey);
777
778         DBG("passkey %06u entered %u", passkey, ev->entered);
779
780         err = btd_event_user_notify(&info->bdaddr, &ev->addr.bdaddr,
781                                                       passkey);
782         if (err < 0)
783                 error("btd_event_user_notify: %s", strerror(-err));
784 }
785
786 struct confirm_data {
787         int index;
788         bdaddr_t bdaddr;
789         uint8_t type;
790 };
791
792 static gboolean confirm_accept(gpointer user_data)
793 {
794         struct confirm_data *data = user_data;
795         struct controller_info *info = &controllers[data->index];
796
797         DBG("auto-accepting incoming pairing request");
798
799         if (data->index > max_index || !info->valid)
800                 return FALSE;
801
802         mgmt_confirm_reply(data->index, &data->bdaddr, data->type, TRUE);
803
804         return FALSE;
805 }
806
807 static void mgmt_user_confirm_request(int sk, uint16_t index, void *buf,
808                                                                 size_t len)
809 {
810         struct mgmt_ev_user_confirm_request *ev = buf;
811         struct controller_info *info;
812         char addr[18];
813         int err;
814
815         if (len < sizeof(*ev)) {
816                 error("Too small user_confirm_request event");
817                 return;
818         }
819
820         ba2str(&ev->addr.bdaddr, addr);
821
822         DBG("hci%u %s confirm_hint %u", index, addr, ev->confirm_hint);
823
824         if (index > max_index) {
825                 error("Unexpected index %u in user_confirm_request event",
826                                                                         index);
827                 return;
828         }
829
830         if (ev->confirm_hint) {
831                 struct confirm_data *data;
832
833                 data = g_new0(struct confirm_data, 1);
834                 data->index = index;
835                 bacpy(&data->bdaddr, &ev->addr.bdaddr);
836                 data->type = ev->addr.type;
837
838                 g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 1,
839                                                 confirm_accept, data, g_free);
840                 return;
841         }
842
843         info = &controllers[index];
844
845         err = btd_event_user_confirm(&info->bdaddr, &ev->addr.bdaddr,
846                                                         btohl(ev->value));
847         if (err < 0) {
848                 error("btd_event_user_confirm: %s", strerror(-err));
849                 mgmt_confirm_reply(index, &ev->addr.bdaddr, ev->addr.type,
850                                                                         FALSE);
851         }
852 }
853
854 static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
855 {
856         if (uuid->type == SDP_UUID16)
857                 sdp_uuid16_to_uuid128(uuid128, uuid);
858         else if (uuid->type == SDP_UUID32)
859                 sdp_uuid32_to_uuid128(uuid128, uuid);
860         else
861                 memcpy(uuid128, uuid, sizeof(*uuid));
862 }
863
864 static int mgmt_add_uuid(int index, uuid_t *uuid, uint8_t svc_hint)
865 {
866         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_add_uuid)];
867         struct mgmt_hdr *hdr = (void *) buf;
868         struct mgmt_cp_add_uuid *cp = (void *) &buf[sizeof(*hdr)];
869         struct controller_info *info = &controllers[index];
870         uuid_t uuid128;
871         uint128_t uint128;
872
873         DBG("index %d", index);
874
875         if (info->pending_uuid) {
876                 struct pending_uuid *pending = g_new0(struct pending_uuid, 1);
877
878                 memcpy(&pending->uuid, uuid, sizeof(*uuid));
879                 pending->svc_hint = svc_hint;
880
881                 info->pending_uuids = g_slist_append(info->pending_uuids,
882                                                                 pending);
883                 return 0;
884         }
885
886         uuid_to_uuid128(&uuid128, uuid);
887
888         memset(buf, 0, sizeof(buf));
889         hdr->opcode = htobs(MGMT_OP_ADD_UUID);
890         hdr->len = htobs(sizeof(*cp));
891         hdr->index = htobs(index);
892
893         ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
894         htob128(&uint128, (uint128_t *) cp->uuid);
895
896         cp->svc_hint = svc_hint;
897
898         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
899                 return -errno;
900
901         info->pending_uuid = TRUE;
902
903         return 0;
904 }
905
906 static int mgmt_remove_uuid(int index, uuid_t *uuid)
907 {
908         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_remove_uuid)];
909         struct mgmt_hdr *hdr = (void *) buf;
910         struct mgmt_cp_remove_uuid *cp = (void *) &buf[sizeof(*hdr)];
911         uuid_t uuid128;
912         uint128_t uint128;
913
914         DBG("index %d", index);
915
916         uuid_to_uuid128(&uuid128, uuid);
917
918         memset(buf, 0, sizeof(buf));
919         hdr->opcode = htobs(MGMT_OP_REMOVE_UUID);
920         hdr->len = htobs(sizeof(*cp));
921         hdr->index = htobs(index);
922
923         ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
924         htob128(&uint128, (uint128_t *) cp->uuid);
925
926         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
927                 return -errno;
928
929         return 0;
930 }
931
932 static int clear_uuids(int index)
933 {
934         uuid_t uuid_any;
935
936         memset(&uuid_any, 0, sizeof(uuid_any));
937         uuid_any.type = SDP_UUID128;
938
939         return mgmt_remove_uuid(index, &uuid_any);
940 }
941
942 static void read_index_list_complete(int sk, void *buf, size_t len)
943 {
944         struct mgmt_rp_read_index_list *rp = buf;
945         uint16_t num;
946         int i;
947
948         if (len < sizeof(*rp)) {
949                 error("Too small read index list complete event");
950                 return;
951         }
952
953         num = btohs(bt_get_unaligned(&rp->num_controllers));
954
955         if (num * sizeof(uint16_t) + sizeof(*rp) != len) {
956                 error("Incorrect packet size for index list event");
957                 return;
958         }
959
960         for (i = 0; i < num; i++) {
961                 uint16_t index;
962
963                 index = btohs(bt_get_unaligned(&rp->index[i]));
964
965                 add_controller(index);
966                 read_info(sk, index);
967         }
968 }
969
970 static int mgmt_set_powered(int index, gboolean powered)
971 {
972         struct controller_info *info = &controllers[index];
973
974         DBG("index %d powered %d pending_uuid %u", index, powered,
975                                                         info->pending_uuid);
976
977         if (powered) {
978                 if (info->pending_uuid) {
979                         info->pending_powered = TRUE;
980                         return 0;
981                 }
982         } else {
983                 info->pending_powered = FALSE;
984         }
985
986         return mgmt_set_mode(index, MGMT_OP_SET_POWERED, powered);
987 }
988
989 static int mgmt_set_name(int index, const char *name)
990 {
991         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_local_name)];
992         struct mgmt_hdr *hdr = (void *) buf;
993         struct mgmt_cp_set_local_name *cp = (void *) &buf[sizeof(*hdr)];
994
995         DBG("index %d, name %s", index, name);
996
997         memset(buf, 0, sizeof(buf));
998         hdr->opcode = htobs(MGMT_OP_SET_LOCAL_NAME);
999         hdr->len = htobs(sizeof(*cp));
1000         hdr->index = htobs(index);
1001
1002         strncpy((char *) cp->name, name, sizeof(cp->name) - 1);
1003
1004         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1005                 return -errno;
1006
1007         return 0;
1008 }
1009
1010 static int mgmt_set_dev_class(int index, uint8_t major, uint8_t minor)
1011 {
1012         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_dev_class)];
1013         struct mgmt_hdr *hdr = (void *) buf;
1014         struct mgmt_cp_set_dev_class *cp = (void *) &buf[sizeof(*hdr)];
1015         struct controller_info *info = &controllers[index];
1016
1017         DBG("index %d major %u minor %u", index, major, minor);
1018
1019         if (info->pending_uuid) {
1020                 info->major = major;
1021                 info->minor = minor;
1022                 info->pending_class = TRUE;
1023                 return 0;
1024         }
1025
1026         memset(buf, 0, sizeof(buf));
1027         hdr->opcode = htobs(MGMT_OP_SET_DEV_CLASS);
1028         hdr->len = htobs(sizeof(*cp));
1029         hdr->index = htobs(index);
1030
1031         cp->major = major;
1032         cp->minor = minor;
1033
1034         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
1035                 return -errno;
1036
1037         return 0;
1038 }
1039
1040 static void read_info_complete(int sk, uint16_t index, void *buf, size_t len)
1041 {
1042         struct mgmt_rp_read_info *rp = buf;
1043         struct controller_info *info;
1044         struct btd_adapter *adapter;
1045         const char *name;
1046         uint8_t mode, major, minor;
1047         char addr[18];
1048
1049         if (len < sizeof(*rp)) {
1050                 error("Too small read info complete event");
1051                 return;
1052         }
1053
1054         if (index > max_index) {
1055                 error("Unexpected index %u in read info complete", index);
1056                 return;
1057         }
1058
1059         info = &controllers[index];
1060
1061         bacpy(&info->bdaddr, &rp->bdaddr);
1062         info->version = rp->version;
1063         info->manufacturer = btohs(bt_get_unaligned(&rp->manufacturer));
1064
1065         memcpy(&info->supported_settings, &rp->supported_settings,
1066                                         sizeof(info->supported_settings));
1067         memcpy(&info->current_settings, &rp->current_settings,
1068                                         sizeof(info->current_settings));
1069
1070         memcpy(info->dev_class, rp->dev_class, sizeof(info->dev_class));
1071
1072         ba2str(&info->bdaddr, addr);
1073         DBG("hci%u addr %s version %u manufacturer %u class 0x%02x%02x%02x\n",
1074                 index, addr, info->version, info->manufacturer,
1075                 info->dev_class[2], info->dev_class[1], info->dev_class[0]);
1076         DBG("hci%u settings", index);
1077         DBG("hci%u name %s", index, (char *) rp->name);
1078         DBG("hci%u short name %s", index, (char *) rp->short_name);
1079
1080         clear_uuids(index);
1081
1082         adapter = btd_manager_register_adapter(index,
1083                                         mgmt_powered(info->current_settings));
1084         if (adapter == NULL) {
1085                 error("mgmtops: unable to register adapter");
1086                 return;
1087         }
1088
1089         update_settings(adapter, info->current_settings);
1090
1091         name = btd_adapter_get_name(adapter);
1092
1093         DBG("mgmtops setting name %s", name);
1094
1095         if (name)
1096                 mgmt_set_name(index, name);
1097         else
1098                 adapter_name_changed(adapter, (char *) rp->name);
1099
1100         btd_adapter_get_class(adapter, &major, &minor);
1101         mgmt_set_dev_class(index, major, minor);
1102
1103         btd_adapter_get_mode(adapter, &mode, NULL, NULL, NULL);
1104         if (mode == MODE_OFF && mgmt_powered(info->current_settings)) {
1105                 mgmt_set_powered(index, FALSE);
1106                 return;
1107         }
1108
1109         if (mode != MODE_OFF && !mgmt_powered(info->current_settings))
1110                 mgmt_set_powered(index, TRUE);
1111         else {
1112                 get_connections(sk, index);
1113                 btd_adapter_start(adapter);
1114         }
1115
1116         btd_adapter_unref(adapter);
1117 }
1118
1119 static void disconnect_complete(int sk, uint16_t index, uint8_t status,
1120                                                         void *buf, size_t len)
1121 {
1122         struct mgmt_rp_disconnect *rp = buf;
1123         struct controller_info *info;
1124         char addr[18];
1125
1126         if (len < sizeof(*rp)) {
1127                 error("Too small disconnect complete event");
1128                 return;
1129         }
1130
1131         ba2str(&rp->addr.bdaddr, addr);
1132
1133         if (status != 0) {
1134                 error("Disconnecting %s failed with status %u", addr, status);
1135                 return;
1136         }
1137
1138         DBG("hci%d %s disconnected", index, addr);
1139
1140         if (index > max_index) {
1141                 error("Unexpected index %u in disconnect complete", index);
1142                 return;
1143         }
1144
1145         info = &controllers[index];
1146
1147         btd_event_disconn_complete(&info->bdaddr, &rp->addr.bdaddr);
1148
1149         bonding_complete(info, &rp->addr.bdaddr, HCI_CONNECTION_TERMINATED);
1150 }
1151
1152 static void pair_device_complete(int sk, uint16_t index, uint8_t status,
1153                                                         void *buf, size_t len)
1154 {
1155         struct mgmt_rp_pair_device *rp = buf;
1156         struct controller_info *info;
1157         char addr[18];
1158
1159         if (len < sizeof(*rp)) {
1160                 error("Too small pair_device complete event");
1161                 return;
1162         }
1163
1164         ba2str(&rp->addr.bdaddr, addr);
1165
1166         DBG("hci%d %s pairing complete status %u", index, addr, status);
1167
1168         if (index > max_index) {
1169                 error("Unexpected index %u in pair_device complete", index);
1170                 return;
1171         }
1172
1173         info = &controllers[index];
1174
1175         bonding_complete(info, &rp->addr.bdaddr, status);
1176 }
1177
1178 static void get_connections_complete(int sk, uint16_t index, void *buf,
1179                                                                 size_t len)
1180 {
1181         struct mgmt_rp_get_connections *rp = buf;
1182         struct controller_info *info;
1183         int i;
1184
1185         if (len < sizeof(*rp)) {
1186                 error("Too small get_connections complete event");
1187                 return;
1188         }
1189
1190         if (len < (sizeof(*rp) + (rp->conn_count * sizeof(bdaddr_t)))) {
1191                 error("Too small get_connections complete event");
1192                 return;
1193         }
1194
1195         if (index > max_index) {
1196                 error("Unexpected index %u in get_connections complete",
1197                                                                 index);
1198                 return;
1199         }
1200
1201         info = &controllers[index];
1202
1203         for (i = 0; i < rp->conn_count; i++) {
1204                 bdaddr_t *bdaddr = g_memdup(&rp->addr[i], sizeof(bdaddr_t));
1205                 info->connections = g_slist_append(info->connections, bdaddr);
1206         }
1207 }
1208
1209 static void set_local_name_complete(int sk, uint16_t index, void *buf,
1210                                                                 size_t len)
1211 {
1212         struct mgmt_cp_set_local_name *rp = buf;
1213         struct controller_info *info;
1214         struct btd_adapter *adapter;
1215
1216         if (len < sizeof(*rp)) {
1217                 error("Too small set_local_name complete event");
1218                 return;
1219         }
1220
1221         DBG("hci%d name %s", index, (char *) rp->name);
1222
1223         if (index > max_index) {
1224                 error("Unexpected index %u in set_local_name complete", index);
1225                 return;
1226         }
1227
1228         info = &controllers[index];
1229
1230         adapter = manager_find_adapter(&info->bdaddr);
1231         if (adapter == NULL) {
1232                 DBG("Adapter not found");
1233                 return;
1234         }
1235
1236         adapter_name_changed(adapter, (char *) rp->name);
1237 }
1238
1239 static void read_local_oob_data_complete(int sk, uint16_t index, void *buf,
1240                                                                 size_t len)
1241 {
1242         struct mgmt_rp_read_local_oob_data *rp = buf;
1243         struct btd_adapter *adapter;
1244
1245         if (len != sizeof(*rp)) {
1246                 error("read_local_oob_data_complete event size mismatch "
1247                                         "(%zu != %zu)", len, sizeof(*rp));
1248                 return;
1249         }
1250
1251         if (index > max_index) {
1252                 error("Unexpected index %u in read_local_oob_data_complete",
1253                                                                 index);
1254                 return;
1255         }
1256
1257         DBG("hci%u", index);
1258
1259         adapter = manager_find_adapter_by_id(index);
1260
1261         if (adapter)
1262                 oob_read_local_data_complete(adapter, rp->hash, rp->randomizer);
1263 }
1264
1265 static void start_discovery_complete(int sk, uint16_t index, uint8_t status,
1266                                                      void *buf, size_t len)
1267 {
1268         uint8_t *type = buf;
1269         struct btd_adapter *adapter;
1270
1271         if (len != sizeof(*type)) {
1272                 error("start_discovery_complete event size mismatch "
1273                                         "(%zu != %zu)", len, sizeof(*type));
1274                 return;
1275         }
1276
1277         DBG("hci%u type %u status %u", index, *type, status);
1278
1279         if (index > max_index) {
1280                 error("Invalid index %u in start_discovery_complete", index);
1281                 return;
1282         }
1283
1284         if (!status)
1285                 return;
1286
1287         adapter = manager_find_adapter_by_id(index);
1288         if (adapter)
1289                 /* Start discovery failed, inform upper layers. */
1290                 adapter_set_discovering(adapter, FALSE);
1291 }
1292
1293 static void read_local_oob_data_failed(int sk, uint16_t index)
1294 {
1295         struct btd_adapter *adapter;
1296
1297         if (index > max_index) {
1298                 error("Unexpected index %u in read_local_oob_data_failed",
1299                                                                 index);
1300                 return;
1301         }
1302
1303         DBG("hci%u", index);
1304
1305         adapter = manager_find_adapter_by_id(index);
1306
1307         if (adapter)
1308                 oob_read_local_data_complete(adapter, NULL, NULL);
1309 }
1310
1311 static void handle_pending_uuids(uint16_t index)
1312 {
1313         struct controller_info *info;
1314         struct pending_uuid *pending;
1315
1316         DBG("index %d", index);
1317
1318         info = &controllers[index];
1319
1320         info->pending_uuid = FALSE;
1321
1322         if (g_slist_length(info->pending_uuids) == 0) {
1323                 if (info->pending_class) {
1324                         info->pending_class = FALSE;
1325                         mgmt_set_dev_class(index, info->major, info->minor);
1326                 }
1327
1328                 if (info->pending_powered) {
1329                         info->pending_powered = FALSE;
1330                         mgmt_set_powered(index, TRUE);
1331                 }
1332
1333                 return;
1334         }
1335
1336         pending = info->pending_uuids->data;
1337
1338         mgmt_add_uuid(index, &pending->uuid, pending->svc_hint);
1339
1340         info->pending_uuids = g_slist_remove(info->pending_uuids, pending);
1341         g_free(pending);
1342 }
1343
1344 static void mgmt_add_uuid_complete(int sk, uint16_t index, void *buf,
1345                                                                 size_t len)
1346 {
1347         DBG("index %d", index);
1348
1349         if (index > max_index) {
1350                 error("Unexpected index %u in add_uuid_complete event", index);
1351                 return;
1352         }
1353
1354         handle_pending_uuids(index);
1355 }
1356
1357 static void mgmt_cmd_complete(int sk, uint16_t index, void *buf, size_t len)
1358 {
1359         struct mgmt_ev_cmd_complete *ev = buf;
1360         uint16_t opcode;
1361
1362         DBG("");
1363
1364         if (len < sizeof(*ev)) {
1365                 error("Too small management command complete event packet");
1366                 return;
1367         }
1368
1369         opcode = btohs(bt_get_unaligned(&ev->opcode));
1370
1371         len -= sizeof(*ev);
1372
1373         switch (opcode) {
1374         case MGMT_OP_READ_VERSION:
1375                 read_version_complete(sk, ev->data, len);
1376                 break;
1377         case MGMT_OP_READ_INDEX_LIST:
1378                 read_index_list_complete(sk, ev->data, len);
1379                 break;
1380         case MGMT_OP_READ_INFO:
1381                 read_info_complete(sk, index, ev->data, len);
1382                 break;
1383         case MGMT_OP_SET_POWERED:
1384                 mgmt_new_settings(sk, index, ev->data, len);
1385                 break;
1386         case MGMT_OP_SET_DISCOVERABLE:
1387                 mgmt_new_settings(sk, index, ev->data, len);
1388                 break;
1389         case MGMT_OP_SET_CONNECTABLE:
1390                 mgmt_new_settings(sk, index, ev->data, len);
1391                 break;
1392         case MGMT_OP_SET_PAIRABLE:
1393                 mgmt_new_settings(sk, index, ev->data, len);
1394                 break;
1395         case MGMT_OP_SET_SSP:
1396                 DBG("set_ssp complete");
1397 #ifdef __TIZEN_PATCH__
1398                 mgmt_new_settings(sk, index, ev->data, len);
1399 #endif
1400                 break;
1401         case MGMT_OP_SET_LE:
1402                 DBG("set_le complete");
1403 #ifdef __TIZEN_PATCH__
1404                 mgmt_new_settings(sk, index, ev->data, len);
1405 #endif
1406                 break;
1407         case MGMT_OP_ADD_UUID:
1408                 mgmt_add_uuid_complete(sk, index, ev->data, len);
1409                 break;
1410         case MGMT_OP_REMOVE_UUID:
1411                 DBG("remove_uuid complete");
1412                 break;
1413         case MGMT_OP_SET_DEV_CLASS:
1414                 DBG("set_dev_class complete");
1415                 break;
1416         case MGMT_OP_LOAD_LINK_KEYS:
1417                 DBG("load_link_keys complete");
1418                 break;
1419         case MGMT_OP_CANCEL_PAIR_DEVICE:
1420                 DBG("cancel_pair_device complete");
1421                 break;
1422         case MGMT_OP_UNPAIR_DEVICE:
1423                 DBG("unpair_device complete");
1424                 break;
1425         case MGMT_OP_DISCONNECT:
1426                 DBG("disconnect complete");
1427                 disconnect_complete(sk, index, ev->status, ev->data, len);
1428                 break;
1429         case MGMT_OP_GET_CONNECTIONS:
1430                 get_connections_complete(sk, index, ev->data, len);
1431                 break;
1432         case MGMT_OP_PIN_CODE_REPLY:
1433                 DBG("pin_code_reply complete");
1434                 break;
1435         case MGMT_OP_PIN_CODE_NEG_REPLY:
1436                 DBG("pin_code_neg_reply complete");
1437                 break;
1438         case MGMT_OP_SET_IO_CAPABILITY:
1439                 DBG("set_io_capability complete");
1440                 break;
1441         case MGMT_OP_PAIR_DEVICE:
1442                 pair_device_complete(sk, index, ev->status, ev->data, len);
1443                 break;
1444         case MGMT_OP_USER_CONFIRM_REPLY:
1445                 DBG("user_confirm_reply complete");
1446                 break;
1447         case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1448                 DBG("user_confirm_net_reply complete");
1449                 break;
1450         case MGMT_OP_SET_LOCAL_NAME:
1451                 set_local_name_complete(sk, index, ev->data, len);
1452                 break;
1453         case MGMT_OP_READ_LOCAL_OOB_DATA:
1454                 read_local_oob_data_complete(sk, index, ev->data, len);
1455                 break;
1456         case MGMT_OP_ADD_REMOTE_OOB_DATA:
1457                 DBG("add_remote_oob_data complete");
1458                 break;
1459         case MGMT_OP_REMOVE_REMOTE_OOB_DATA:
1460                 DBG("remove_remote_oob_data complete");
1461                 break;
1462         case MGMT_OP_BLOCK_DEVICE:
1463                 DBG("block_device complete");
1464                 break;
1465         case MGMT_OP_UNBLOCK_DEVICE:
1466                 DBG("unblock_device complete");
1467                 break;
1468         case MGMT_OP_SET_FAST_CONNECTABLE:
1469                 DBG("set_fast_connectable complete");
1470                 break;
1471         case MGMT_OP_START_DISCOVERY:
1472                 start_discovery_complete(sk, index, ev->status, ev->data, len);
1473                 break;
1474         case MGMT_OP_STOP_DISCOVERY:
1475                 DBG("stop_discovery complete");
1476                 break;
1477         case MGMT_OP_SET_DEVICE_ID:
1478                 DBG("set_did complete");
1479                 break;
1480         default:
1481                 error("Unknown command complete for opcode %u", opcode);
1482                 break;
1483         }
1484 }
1485
1486 static void mgmt_add_uuid_busy(int sk, uint16_t index)
1487 {
1488         struct controller_info *info;
1489
1490         DBG("index %d", index);
1491
1492         info = &controllers[index];
1493         info->pending_cod_change = TRUE;
1494 }
1495
1496 static void mgmt_cmd_status(int sk, uint16_t index, void *buf, size_t len)
1497 {
1498         struct mgmt_ev_cmd_status *ev = buf;
1499         uint16_t opcode;
1500
1501         if (len < sizeof(*ev)) {
1502                 error("Too small management command status event packet");
1503                 return;
1504         }
1505
1506         opcode = btohs(bt_get_unaligned(&ev->opcode));
1507
1508         if (!ev->status) {
1509                 DBG("%s (0x%04x) cmd_status %u", mgmt_opstr(opcode), opcode,
1510                                                                 ev->status);
1511                 return;
1512         }
1513
1514         switch (opcode) {
1515         case MGMT_OP_READ_LOCAL_OOB_DATA:
1516                 read_local_oob_data_failed(sk, index);
1517                 break;
1518         case MGMT_OP_ADD_UUID:
1519                 if (ev->status == MGMT_STATUS_BUSY) {
1520                         mgmt_add_uuid_busy(sk, index);
1521                         return;
1522                 }
1523                 break;
1524         }
1525
1526         error("hci%u: %s (0x%04x) failed: %s (0x%02x)", index,
1527                         mgmt_opstr(opcode), opcode, mgmt_errstr(ev->status),
1528                         ev->status);
1529 }
1530
1531 static void mgmt_controller_error(int sk, uint16_t index, void *buf, size_t len)
1532 {
1533         struct mgmt_ev_controller_error *ev = buf;
1534
1535         if (len < sizeof(*ev)) {
1536                 error("Too small management controller error event packet");
1537                 return;
1538         }
1539
1540         DBG("index %u error_code %u", index, ev->error_code);
1541 }
1542
1543 static void mgmt_auth_failed(int sk, uint16_t index, void *buf, size_t len)
1544 {
1545         struct controller_info *info;
1546         struct mgmt_ev_auth_failed *ev = buf;
1547
1548         if (len < sizeof(*ev)) {
1549                 error("Too small mgmt_auth_failed event packet");
1550                 return;
1551         }
1552
1553         DBG("hci%u auth failed status %u", index, ev->status);
1554
1555         if (index > max_index) {
1556                 error("Unexpected index %u in auth_failed event", index);
1557                 return;
1558         }
1559
1560         info = &controllers[index];
1561
1562         bonding_complete(info, &ev->addr.bdaddr, ev->status);
1563 }
1564
1565 static void mgmt_local_name_changed(int sk, uint16_t index, void *buf, size_t len)
1566 {
1567         struct mgmt_cp_set_local_name *ev = buf;
1568         struct controller_info *info;
1569         struct btd_adapter *adapter;
1570
1571         if (len < sizeof(*ev)) {
1572                 error("Too small mgmt_local_name_changed event packet");
1573                 return;
1574         }
1575
1576         DBG("hci%u local name changed: %s", index, (char *) ev->name);
1577
1578         if (index > max_index) {
1579                 error("Unexpected index %u in name_changed event", index);
1580                 return;
1581         }
1582
1583         info = &controllers[index];
1584
1585         adapter = manager_find_adapter(&info->bdaddr);
1586         if (adapter)
1587                 adapter_name_changed(adapter, (char *) ev->name);
1588 }
1589
1590 static void mgmt_device_found(int sk, uint16_t index, void *buf, size_t len)
1591 {
1592         struct mgmt_ev_device_found *ev = buf;
1593         struct controller_info *info;
1594         char addr[18];
1595         uint32_t flags;
1596         uint16_t eir_len;
1597         uint8_t *eir;
1598         gboolean confirm_name;
1599
1600         if (len < sizeof(*ev)) {
1601                 error("mgmt_device_found too short (%zu bytes)", len);
1602                 return;
1603         }
1604
1605         eir_len = bt_get_le16(&ev->eir_len);
1606         if (len != sizeof(*ev) + eir_len) {
1607                 error("mgmt_device_found event size mismatch (%zu != %zu)",
1608                                                 len, sizeof(*ev) + eir_len);
1609                 return;
1610         }
1611
1612         if (index > max_index) {
1613                 error("Unexpected index %u in device_found event", index);
1614                 return;
1615         }
1616
1617         info = &controllers[index];
1618
1619         if (eir_len == 0)
1620                 eir = NULL;
1621         else
1622                 eir = ev->eir;
1623
1624         flags = btohl(ev->flags);
1625
1626         ba2str(&ev->addr.bdaddr, addr);
1627         DBG("hci%u addr %s, rssi %d flags 0x%04x eir_len %u",
1628                         index, addr, ev->rssi, flags, eir_len);
1629
1630         if (flags & MGMT_DEV_FOUND_LEGACY_PAIRING)
1631                 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1632                                                                         TRUE);
1633         else
1634                 btd_event_set_legacy_pairing(&info->bdaddr, &ev->addr.bdaddr,
1635                                                                         FALSE);
1636
1637         confirm_name = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
1638
1639         btd_event_device_found(&info->bdaddr, &ev->addr.bdaddr,
1640                                                 ev->addr.type,
1641                                                 ev->rssi, confirm_name,
1642                                                 eir, eir_len);
1643 }
1644
1645 static void mgmt_discovering(int sk, uint16_t index, void *buf, size_t len)
1646 {
1647         struct mgmt_ev_discovering *ev = buf;
1648         struct controller_info *info;
1649         struct btd_adapter *adapter;
1650
1651         if (len < sizeof(*ev)) {
1652                 error("Too small discovering event");
1653                 return;
1654         }
1655
1656         DBG("Controller %u type %u discovering %u", index,
1657                                         ev->type, ev->discovering);
1658
1659         if (index > max_index) {
1660                 error("Unexpected index %u in discovering event", index);
1661                 return;
1662         }
1663
1664         info = &controllers[index];
1665
1666         adapter = manager_find_adapter(&info->bdaddr);
1667         if (!adapter)
1668                 return;
1669
1670         adapter_set_discovering(adapter, ev->discovering);
1671 }
1672
1673 static void mgmt_device_blocked(int sk, uint16_t index, void *buf, size_t len)
1674 {
1675         struct controller_info *info;
1676         struct mgmt_ev_device_blocked *ev = buf;
1677         char addr[18];
1678
1679         if (len < sizeof(*ev)) {
1680                 error("Too small mgmt_device_blocked event packet");
1681                 return;
1682         }
1683
1684         ba2str(&ev->addr.bdaddr, addr);
1685         DBG("Device blocked, index %u, addr %s", index, addr);
1686
1687         if (index > max_index) {
1688                 error("Unexpected index %u in device_blocked event", index);
1689                 return;
1690         }
1691
1692         info = &controllers[index];
1693
1694         btd_event_device_blocked(&info->bdaddr, &ev->addr.bdaddr);
1695 }
1696
1697 static void mgmt_device_unblocked(int sk, uint16_t index, void *buf, size_t len)
1698 {
1699         struct controller_info *info;
1700         struct mgmt_ev_device_unblocked *ev = buf;
1701         char addr[18];
1702
1703         if (len < sizeof(*ev)) {
1704                 error("Too small mgmt_device_unblocked event packet");
1705                 return;
1706         }
1707
1708         ba2str(&ev->addr.bdaddr, addr);
1709         DBG("Device unblocked, index %u, addr %s", index, addr);
1710
1711         if (index > max_index) {
1712                 error("Unexpected index %u in device_unblocked event", index);
1713                 return;
1714         }
1715
1716         info = &controllers[index];
1717
1718         btd_event_device_unblocked(&info->bdaddr, &ev->addr.bdaddr);
1719 }
1720
1721 static void mgmt_device_unpaired(int sk, uint16_t index, void *buf, size_t len)
1722 {
1723         struct controller_info *info;
1724         struct mgmt_ev_device_unpaired *ev = buf;
1725         char addr[18];
1726
1727         if (len < sizeof(*ev)) {
1728                 error("Too small mgmt_device_unpaired event packet");
1729                 return;
1730         }
1731
1732         ba2str(&ev->addr.bdaddr, addr);
1733         DBG("Device upaired, index %u, addr %s", index, addr);
1734
1735         if (index > max_index) {
1736                 error("Unexpected index %u in device_unpaired event", index);
1737                 return;
1738         }
1739
1740         info = &controllers[index];
1741
1742         btd_event_device_unpaired(&info->bdaddr, &ev->addr.bdaddr);
1743 }
1744
1745 static void mgmt_new_ltk(int sk, uint16_t index, void *buf, size_t len)
1746 {
1747         struct mgmt_ev_new_long_term_key *ev = buf;
1748         struct controller_info *info;
1749
1750         if (len != sizeof(*ev)) {
1751                 error("mgmt_new_ltk event size mismatch (%zu != %zu)",
1752                                                         len, sizeof(*ev));
1753                 return;
1754         }
1755
1756         DBG("Controller %u new LTK authenticated %u enc_size %u", index,
1757                                 ev->key.authenticated, ev->key.enc_size);
1758
1759         if (index > max_index) {
1760                 error("Unexpected index %u in new_key event", index);
1761                 return;
1762         }
1763
1764         info = &controllers[index];
1765
1766         if (ev->store_hint) {
1767                 btd_event_ltk_notify(&info->bdaddr, &ev->key.addr.bdaddr,
1768                                 ev->key.addr.type, ev->key.val, ev->key.master,
1769                                 ev->key.authenticated, ev->key.enc_size,
1770                                 ev->key.ediv, ev->key.rand);
1771         }
1772
1773         if (ev->key.master)
1774                 bonding_complete(info, &ev->key.addr.bdaddr, 0);
1775 }
1776
1777 static void mgmt_cod_changed(int sk, uint16_t index)
1778 {
1779         struct controller_info *info;
1780
1781         DBG("index %d", index);
1782
1783         if (index > max_index) {
1784                 error("Unexpected index %u in mgmt_cod_changed event", index);
1785                 return;
1786         }
1787
1788         info = &controllers[index];
1789
1790         if (info->pending_cod_change) {
1791                 info->pending_cod_change = FALSE;
1792                 handle_pending_uuids(index);
1793         }
1794 }
1795
1796 static gboolean mgmt_event(GIOChannel *io, GIOCondition cond, gpointer user_data)
1797 {
1798         char buf[MGMT_BUF_SIZE];
1799         struct mgmt_hdr *hdr = (void *) buf;
1800         int sk;
1801         ssize_t ret;
1802         uint16_t len, opcode, index;
1803
1804         DBG("cond %d", cond);
1805
1806         if (cond & G_IO_NVAL)
1807                 return FALSE;
1808
1809         sk = g_io_channel_unix_get_fd(io);
1810
1811         if (cond & (G_IO_ERR | G_IO_HUP)) {
1812                 error("Error on management socket");
1813                 return FALSE;
1814         }
1815
1816         ret = read(sk, buf, sizeof(buf));
1817         if (ret < 0) {
1818                 error("Unable to read from management socket: %s (%d)",
1819                                                 strerror(errno), errno);
1820                 return TRUE;
1821         }
1822
1823         DBG("Received %zd bytes from management socket", ret);
1824
1825         if (ret < MGMT_HDR_SIZE) {
1826                 error("Too small Management packet");
1827                 return TRUE;
1828         }
1829
1830         opcode = btohs(bt_get_unaligned(&hdr->opcode));
1831         len = btohs(bt_get_unaligned(&hdr->len));
1832         index = btohs(bt_get_unaligned(&hdr->index));
1833
1834         if (ret != MGMT_HDR_SIZE + len) {
1835                 error("Packet length mismatch. ret %zd len %u", ret, len);
1836                 return TRUE;
1837         }
1838
1839         switch (opcode) {
1840         case MGMT_EV_CMD_COMPLETE:
1841                 mgmt_cmd_complete(sk, index, buf + MGMT_HDR_SIZE, len);
1842                 break;
1843         case MGMT_EV_CMD_STATUS:
1844                 mgmt_cmd_status(sk, index, buf + MGMT_HDR_SIZE, len);
1845                 break;
1846         case MGMT_EV_CONTROLLER_ERROR:
1847                 mgmt_controller_error(sk, index, buf + MGMT_HDR_SIZE, len);
1848                 break;
1849         case MGMT_EV_INDEX_ADDED:
1850                 mgmt_index_added(sk, index);
1851                 break;
1852         case MGMT_EV_INDEX_REMOVED:
1853                 mgmt_index_removed(sk, index);
1854                 break;
1855         case MGMT_EV_NEW_SETTINGS:
1856                 mgmt_new_settings(sk, index, buf + MGMT_HDR_SIZE, len);
1857                 break;
1858         case MGMT_EV_CLASS_OF_DEV_CHANGED:
1859                 mgmt_cod_changed(sk, index);
1860                 break;
1861         case MGMT_EV_NEW_LINK_KEY:
1862                 mgmt_new_link_key(sk, index, buf + MGMT_HDR_SIZE, len);
1863                 break;
1864         case MGMT_EV_DEVICE_CONNECTED:
1865                 mgmt_device_connected(sk, index, buf + MGMT_HDR_SIZE, len);
1866                 break;
1867         case MGMT_EV_DEVICE_DISCONNECTED:
1868                 mgmt_device_disconnected(sk, index, buf + MGMT_HDR_SIZE, len);
1869                 break;
1870         case MGMT_EV_CONNECT_FAILED:
1871                 mgmt_connect_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1872                 break;
1873         case MGMT_EV_PIN_CODE_REQUEST:
1874                 mgmt_pin_code_request(sk, index, buf + MGMT_HDR_SIZE, len);
1875                 break;
1876         case MGMT_EV_USER_CONFIRM_REQUEST:
1877                 mgmt_user_confirm_request(sk, index, buf + MGMT_HDR_SIZE, len);
1878                 break;
1879         case MGMT_EV_AUTH_FAILED:
1880                 mgmt_auth_failed(sk, index, buf + MGMT_HDR_SIZE, len);
1881                 break;
1882         case MGMT_EV_LOCAL_NAME_CHANGED:
1883                 mgmt_local_name_changed(sk, index, buf + MGMT_HDR_SIZE, len);
1884                 break;
1885         case MGMT_EV_DEVICE_FOUND:
1886                 mgmt_device_found(sk, index, buf + MGMT_HDR_SIZE, len);
1887                 break;
1888         case MGMT_EV_DISCOVERING:
1889                 mgmt_discovering(sk, index, buf + MGMT_HDR_SIZE, len);
1890                 break;
1891         case MGMT_EV_DEVICE_BLOCKED:
1892                 mgmt_device_blocked(sk, index, buf + MGMT_HDR_SIZE, len);
1893                 break;
1894         case MGMT_EV_DEVICE_UNBLOCKED:
1895                 mgmt_device_unblocked(sk, index, buf + MGMT_HDR_SIZE, len);
1896                 break;
1897         case MGMT_EV_DEVICE_UNPAIRED:
1898                 mgmt_device_unpaired(sk, index, buf + MGMT_HDR_SIZE, len);
1899                 break;
1900         case MGMT_EV_USER_PASSKEY_REQUEST:
1901                 mgmt_passkey_request(sk, index, buf + MGMT_HDR_SIZE, len);
1902                 break;
1903         case MGMT_EV_PASSKEY_NOTIFY:
1904                 mgmt_passkey_notify(sk, index, buf + MGMT_HDR_SIZE, len);
1905                 break;
1906         case MGMT_EV_NEW_LONG_TERM_KEY:
1907                 mgmt_new_ltk(sk, index, buf + MGMT_HDR_SIZE, len);
1908                 break;
1909         default:
1910                 error("Unknown Management opcode %u (index %u)", opcode, index);
1911                 break;
1912         }
1913
1914         return TRUE;
1915 }
1916
1917 static int mgmt_setup(void)
1918 {
1919         struct mgmt_hdr hdr;
1920         struct sockaddr_hci addr;
1921         GIOChannel *io;
1922         GIOCondition condition;
1923         int dd, err;
1924
1925         dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
1926         if (dd < 0)
1927                 return -errno;
1928
1929         memset(&addr, 0, sizeof(addr));
1930         addr.hci_family = AF_BLUETOOTH;
1931         addr.hci_dev = HCI_DEV_NONE;
1932         addr.hci_channel = HCI_CHANNEL_CONTROL;
1933
1934         if (bind(dd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1935                 err = -errno;
1936                 goto fail;
1937         }
1938
1939         memset(&hdr, 0, sizeof(hdr));
1940         hdr.opcode = htobs(MGMT_OP_READ_VERSION);
1941         hdr.index = htobs(MGMT_INDEX_NONE);
1942         if (write(dd, &hdr, sizeof(hdr)) < 0) {
1943                 err = -errno;
1944                 goto fail;
1945         }
1946
1947         io = g_io_channel_unix_new(dd);
1948         condition = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
1949         mgmt_watch = g_io_add_watch(io, condition, mgmt_event, NULL);
1950         g_io_channel_unref(io);
1951
1952         mgmt_sock = dd;
1953
1954         info("Bluetooth Management interface initialized");
1955
1956         return 0;
1957
1958 fail:
1959         close(dd);
1960         return err;
1961 }
1962
1963 static void mgmt_cleanup(void)
1964 {
1965         g_free(controllers);
1966         controllers = NULL;
1967         max_index = -1;
1968
1969         if (mgmt_sock >= 0) {
1970                 close(mgmt_sock);
1971                 mgmt_sock = -1;
1972         }
1973
1974         if (mgmt_watch > 0) {
1975                 g_source_remove(mgmt_watch);
1976                 mgmt_watch = 0;
1977         }
1978 }
1979
1980 static int mgmt_start_discovery(int index)
1981 {
1982         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
1983         struct mgmt_hdr *hdr = (void *) buf;
1984         struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
1985         struct controller_info *info = &controllers[index];
1986
1987         DBG("index %d", index);
1988
1989         info->discov_type = 0;
1990
1991         if (mgmt_bredr(info->current_settings))
1992                 hci_set_bit(BDADDR_BREDR, &info->discov_type);
1993
1994         if (mgmt_low_energy(info->current_settings)) {
1995                 hci_set_bit(BDADDR_LE_PUBLIC, &info->discov_type);
1996                 hci_set_bit(BDADDR_LE_RANDOM, &info->discov_type);
1997         }
1998
1999         memset(buf, 0, sizeof(buf));
2000         hdr->opcode = htobs(MGMT_OP_START_DISCOVERY);
2001         hdr->len = htobs(sizeof(*cp));
2002         hdr->index = htobs(index);
2003
2004         cp->type = info->discov_type;
2005
2006         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2007                 return -errno;
2008
2009         return 0;
2010 }
2011
2012 static int mgmt_stop_discovery(int index)
2013 {
2014         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)];
2015         struct mgmt_hdr *hdr = (void *) buf;
2016         struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)];
2017         struct controller_info *info = &controllers[index];
2018
2019         DBG("index %d", index);
2020
2021         memset(buf, 0, sizeof(buf));
2022         hdr->opcode = htobs(MGMT_OP_STOP_DISCOVERY);
2023         hdr->len = htobs(sizeof(*cp));
2024         hdr->index = htobs(index);
2025
2026         cp->type = info->discov_type;
2027
2028         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2029                 return -errno;
2030
2031         return 0;
2032 }
2033
2034 static int mgmt_set_fast_connectable(int index, gboolean enable)
2035 {
2036         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_mode)];
2037         struct mgmt_hdr *hdr = (void *) buf;
2038         struct mgmt_mode *cp = (void *) &buf[sizeof(*hdr)];
2039
2040         DBG("index %d enable %d", index, enable);
2041
2042         memset(buf, 0, sizeof(buf));
2043         hdr->opcode = htobs(MGMT_OP_SET_FAST_CONNECTABLE);
2044         hdr->len = htobs(sizeof(*cp));
2045         hdr->index = htobs(index);
2046
2047         cp->val = enable;
2048
2049         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2050                 return -errno;
2051
2052         return 0;
2053 }
2054
2055 static int mgmt_read_clock(int index, bdaddr_t *bdaddr, int which, int timeout,
2056                                         uint32_t *clock, uint16_t *accuracy)
2057 {
2058         char addr[18];
2059
2060         ba2str(bdaddr, addr);
2061         DBG("index %d addr %s which %d timeout %d", index, addr, which,
2062                                                                 timeout);
2063
2064         return -ENOSYS;
2065 }
2066
2067 static int mgmt_read_bdaddr(int index, bdaddr_t *bdaddr)
2068 {
2069         char addr[18];
2070         struct controller_info *info = &controllers[index];
2071
2072         ba2str(&info->bdaddr, addr);
2073         DBG("index %d addr %s", index, addr);
2074
2075         if (!info->valid)
2076                 return -ENODEV;
2077
2078         bacpy(bdaddr, &info->bdaddr);
2079
2080         return 0;
2081 }
2082
2083 static int mgmt_block_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2084 {
2085         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_block_device)];
2086         struct mgmt_hdr *hdr = (void *) buf;
2087         struct mgmt_cp_block_device *cp;
2088         size_t buf_len;
2089         char addr[18];
2090
2091         ba2str(bdaddr, addr);
2092         DBG("index %d addr %s", index, addr);
2093
2094         memset(buf, 0, sizeof(buf));
2095
2096         hdr->opcode = htobs(MGMT_OP_BLOCK_DEVICE);
2097         hdr->len = htobs(sizeof(*cp));
2098         hdr->index = htobs(index);
2099
2100         cp = (void *) &buf[sizeof(*hdr)];
2101         bacpy(&cp->addr.bdaddr, bdaddr);
2102         cp->addr.type = bdaddr_type;
2103
2104         buf_len = sizeof(*hdr) + sizeof(*cp);
2105
2106         if (write(mgmt_sock, buf, buf_len) < 0)
2107                 return -errno;
2108
2109         return 0;
2110 }
2111
2112 static int mgmt_unblock_device(int index, bdaddr_t *bdaddr,
2113                                                         uint8_t bdaddr_type)
2114 {
2115         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_unblock_device)];
2116         struct mgmt_hdr *hdr = (void *) buf;
2117         struct mgmt_cp_unblock_device *cp;
2118         size_t buf_len;
2119         char addr[18];
2120
2121         ba2str(bdaddr, addr);
2122         DBG("index %d addr %s", index, addr);
2123
2124         memset(buf, 0, sizeof(buf));
2125
2126         hdr->opcode = htobs(MGMT_OP_UNBLOCK_DEVICE);
2127         hdr->len = htobs(sizeof(*cp));
2128         hdr->index = htobs(index);
2129
2130         cp = (void *) &buf[sizeof(*hdr)];
2131         bacpy(&cp->addr.bdaddr, bdaddr);
2132         cp->addr.type = bdaddr_type;
2133
2134         buf_len = sizeof(*hdr) + sizeof(*cp);
2135
2136         if (write(mgmt_sock, buf, buf_len) < 0)
2137                 return -errno;
2138
2139         return 0;
2140 }
2141
2142 static int mgmt_get_conn_list(int index, GSList **conns)
2143 {
2144         struct controller_info *info = &controllers[index];
2145
2146         DBG("index %d", index);
2147
2148         *conns = info->connections;
2149         info->connections = NULL;
2150
2151         return 0;
2152 }
2153
2154 static int mgmt_disconnect(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2155 {
2156         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_disconnect)];
2157         struct mgmt_hdr *hdr = (void *) buf;
2158         struct mgmt_cp_disconnect *cp = (void *) &buf[sizeof(*hdr)];
2159         char addr[18];
2160
2161         ba2str(bdaddr, addr);
2162         DBG("index %d %s", index, addr);
2163
2164         memset(buf, 0, sizeof(buf));
2165         hdr->opcode = htobs(MGMT_OP_DISCONNECT);
2166         hdr->len = htobs(sizeof(*cp));
2167         hdr->index = htobs(index);
2168
2169         bacpy(&cp->addr.bdaddr, bdaddr);
2170         cp->addr.type = bdaddr_type;
2171
2172         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2173                 error("write: %s (%d)", strerror(errno), errno);
2174
2175         return 0;
2176 }
2177
2178 static int mgmt_unpair_device(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type)
2179 {
2180         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_unpair_device)];
2181         struct mgmt_hdr *hdr = (void *) buf;
2182         struct mgmt_cp_unpair_device *cp = (void *) &buf[sizeof(*hdr)];
2183         char addr[18];
2184
2185         ba2str(bdaddr, addr);
2186         DBG("index %d addr %s", index, addr);
2187
2188         memset(buf, 0, sizeof(buf));
2189         hdr->opcode = htobs(MGMT_OP_UNPAIR_DEVICE);
2190         hdr->len = htobs(sizeof(*cp));
2191         hdr->index = htobs(index);
2192
2193         bacpy(&cp->addr.bdaddr, bdaddr);
2194         cp->addr.type = bdaddr_type;
2195         cp->disconnect = 1;
2196
2197         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2198                 return -errno;
2199
2200         return 0;
2201 }
2202
2203 static int mgmt_encrypt_link(int index, bdaddr_t *dst, bt_hci_result_t cb,
2204                                                         gpointer user_data)
2205 {
2206         char addr[18];
2207
2208         ba2str(dst, addr);
2209         DBG("index %d addr %s", index, addr);
2210
2211         return -ENOSYS;
2212 }
2213
2214 static int mgmt_set_did(int index, uint16_t vendor, uint16_t product,
2215                                         uint16_t version, uint16_t source)
2216 {
2217         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_device_id)];
2218         struct mgmt_hdr *hdr = (void *) buf;
2219         struct mgmt_cp_set_device_id *cp = (void *) &buf[sizeof(*hdr)];
2220
2221         DBG("index %d source %x vendor %x product %x version %x",
2222                                 index, source, vendor, product, version);
2223
2224         memset(buf, 0, sizeof(buf));
2225         hdr->opcode = htobs(MGMT_OP_SET_DEVICE_ID);
2226         hdr->len = htobs(sizeof(*cp));
2227         hdr->index = htobs(index);
2228
2229         cp->source = htobs(source);
2230         cp->vendor = htobs(vendor);
2231         cp->product = htobs(product);
2232         cp->version = htobs(version);
2233
2234         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2235                 return -errno;
2236
2237         return 0;
2238 }
2239
2240 static int mgmt_disable_cod_cache(int index)
2241 {
2242         DBG("index %d", index);
2243
2244         /* The cache control is handled automatically for mgmt */
2245         return 0;
2246 }
2247
2248 static int mgmt_restore_powered(int index)
2249 {
2250         DBG("index %d", index);
2251         return -ENOSYS;
2252 }
2253
2254 static int mgmt_load_link_keys(int index, GSList *keys, gboolean debug_keys)
2255 {
2256         char *buf;
2257         struct mgmt_hdr *hdr;
2258         struct mgmt_cp_load_link_keys *cp;
2259         struct mgmt_link_key_info *key;
2260         size_t key_count, cp_size;
2261         GSList *l;
2262         int err;
2263
2264         key_count = g_slist_length(keys);
2265
2266         DBG("index %d keys %zu debug_keys %d", index, key_count, debug_keys);
2267
2268         cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2269
2270         buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2271         if (buf == NULL)
2272                 return -ENOMEM;
2273
2274         hdr = (void *) buf;
2275         hdr->opcode = htobs(MGMT_OP_LOAD_LINK_KEYS);
2276         hdr->len = htobs(cp_size);
2277         hdr->index = htobs(index);
2278
2279         cp = (void *) (buf + sizeof(*hdr));
2280         cp->debug_keys = debug_keys;
2281         cp->key_count = htobs(key_count);
2282
2283         for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2284                 struct link_key_info *info = l->data;
2285
2286                 bacpy(&key->addr.bdaddr, &info->bdaddr);
2287                 key->addr.type = BDADDR_BREDR;
2288                 key->type = info->type;
2289                 memcpy(key->val, info->key, 16);
2290                 key->pin_len = info->pin_len;
2291         }
2292
2293         if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2294                 err = -errno;
2295         else
2296                 err = 0;
2297
2298         g_free(buf);
2299
2300         return err;
2301 }
2302
2303 static int mgmt_set_io_capability(int index, uint8_t io_capability)
2304 {
2305         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_io_capability)];
2306         struct mgmt_hdr *hdr = (void *) buf;
2307         struct mgmt_cp_set_io_capability *cp = (void *) &buf[sizeof(*hdr)];
2308
2309         DBG("hci%d io_capability 0x%02x", index, io_capability);
2310
2311         memset(buf, 0, sizeof(buf));
2312         hdr->opcode = htobs(MGMT_OP_SET_IO_CAPABILITY);
2313         hdr->len = htobs(sizeof(*cp));
2314         hdr->index = htobs(index);
2315
2316         cp->io_capability = io_capability;
2317
2318         if (write(mgmt_sock, buf, sizeof(buf)) < 0)
2319                 return -errno;
2320
2321         return 0;
2322 }
2323
2324 static int mgmt_create_bonding(int index, bdaddr_t *bdaddr, uint8_t addr_type, uint8_t io_cap)
2325 {
2326         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_pair_device)];
2327         struct mgmt_hdr *hdr = (void *) buf;
2328         struct mgmt_cp_pair_device *cp = (void *) &buf[sizeof(*hdr)];
2329         char addr[18];
2330
2331         ba2str(bdaddr, addr);
2332         DBG("hci%d bdaddr %s io_cap 0x%02x", index, addr, io_cap);
2333
2334         memset(buf, 0, sizeof(buf));
2335         hdr->opcode = htobs(MGMT_OP_PAIR_DEVICE);
2336         hdr->len = htobs(sizeof(*cp));
2337         hdr->index = htobs(index);
2338
2339         bacpy(&cp->addr.bdaddr, bdaddr);
2340         cp->addr.type = addr_type;
2341         cp->io_cap = io_cap;
2342
2343         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2344                 return -errno;
2345
2346         return 0;
2347 }
2348
2349 static int mgmt_cancel_bonding(int index, bdaddr_t *bdaddr)
2350 {
2351         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_addr_info)];
2352         struct mgmt_hdr *hdr = (void *) buf;
2353         struct mgmt_addr_info *cp = (void *) &buf[sizeof(*hdr)];
2354         char addr[18];
2355
2356         ba2str(bdaddr, addr);
2357         DBG("hci%d bdaddr %s", index, addr);
2358
2359         memset(buf, 0, sizeof(buf));
2360         hdr->opcode = htobs(MGMT_OP_CANCEL_PAIR_DEVICE);
2361         hdr->len = htobs(sizeof(*cp));
2362         hdr->index = htobs(index);
2363
2364         bacpy(&cp->bdaddr, bdaddr);
2365
2366         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2367                 return -errno;
2368
2369         return 0;
2370 }
2371
2372 static int mgmt_read_local_oob_data(int index)
2373 {
2374         struct mgmt_hdr hdr;
2375
2376         DBG("hci%d", index);
2377
2378         hdr.opcode = htobs(MGMT_OP_READ_LOCAL_OOB_DATA);
2379         hdr.len = 0;
2380         hdr.index = htobs(index);
2381
2382         if (write(mgmt_sock, &hdr, sizeof(hdr)) < 0)
2383                 return -errno;
2384
2385         return 0;
2386 }
2387
2388 static int mgmt_add_remote_oob_data(int index, bdaddr_t *bdaddr,
2389                                         uint8_t *hash, uint8_t *randomizer)
2390 {
2391         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_add_remote_oob_data)];
2392         struct mgmt_hdr *hdr = (void *) buf;
2393         struct mgmt_cp_add_remote_oob_data *cp = (void *) &buf[sizeof(*hdr)];
2394         char addr[18];
2395
2396         ba2str(bdaddr, addr);
2397         DBG("hci%d bdaddr %s", index, addr);
2398
2399         memset(buf, 0, sizeof(buf));
2400
2401         hdr->opcode = htobs(MGMT_OP_ADD_REMOTE_OOB_DATA);
2402         hdr->index = htobs(index);
2403         hdr->len = htobs(sizeof(*cp));
2404
2405         bacpy(&cp->addr.bdaddr, bdaddr);
2406         memcpy(cp->hash, hash, 16);
2407         memcpy(cp->randomizer, randomizer, 16);
2408
2409         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2410                 return -errno;
2411
2412         return 0;
2413 }
2414
2415 static int mgmt_remove_remote_oob_data(int index, bdaddr_t *bdaddr)
2416 {
2417         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_remove_remote_oob_data)];
2418         struct mgmt_hdr *hdr = (void *) buf;
2419         struct mgmt_cp_remove_remote_oob_data *cp = (void *) &buf[sizeof(*hdr)];
2420         char addr[18];
2421
2422         ba2str(bdaddr, addr);
2423         DBG("hci%d bdaddr %s", index, addr);
2424
2425         memset(buf, 0, sizeof(buf));
2426
2427         hdr->opcode = htobs(MGMT_OP_REMOVE_REMOTE_OOB_DATA);
2428         hdr->index = htobs(index);
2429         hdr->len = htobs(sizeof(*cp));
2430
2431         bacpy(&cp->addr.bdaddr, bdaddr);
2432
2433         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2434                 return -errno;
2435
2436         return 0;
2437 }
2438
2439 static int mgmt_confirm_name(int index, bdaddr_t *bdaddr, uint8_t bdaddr_type,
2440                                                         gboolean name_known)
2441 {
2442         char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_confirm_name)];
2443         struct mgmt_hdr *hdr = (void *) buf;
2444         struct mgmt_cp_confirm_name *cp = (void *) &buf[sizeof(*hdr)];
2445         char addr[18];
2446
2447         ba2str(bdaddr, addr);
2448         DBG("hci%d bdaddr %s name_known %u", index, addr, name_known);
2449
2450         memset(buf, 0, sizeof(buf));
2451
2452         hdr->opcode = htobs(MGMT_OP_CONFIRM_NAME);
2453         hdr->index = htobs(index);
2454         hdr->len = htobs(sizeof(*cp));
2455
2456         bacpy(&cp->addr.bdaddr, bdaddr);
2457         cp->addr.type = bdaddr_type;
2458         cp->name_known = name_known;
2459
2460         if (write(mgmt_sock, &buf, sizeof(buf)) < 0)
2461                 return -errno;
2462
2463         return 0;
2464 }
2465
2466 static int mgmtops_load_ltks(int index, GSList *keys)
2467 {
2468         char *buf;
2469         struct mgmt_hdr *hdr;
2470         struct mgmt_cp_load_long_term_keys *cp;
2471         struct mgmt_ltk_info *key;
2472         size_t key_count, cp_size;
2473         GSList *l;
2474         int err;
2475
2476         key_count = g_slist_length(keys);
2477
2478         DBG("index %d keys %zu", index, key_count);
2479
2480         cp_size = sizeof(*cp) + (key_count * sizeof(*key));
2481
2482         buf = g_try_malloc0(sizeof(*hdr) + cp_size);
2483         if (buf == NULL)
2484                 return -ENOMEM;
2485
2486         hdr = (void *) buf;
2487         hdr->opcode = htobs(MGMT_OP_LOAD_LONG_TERM_KEYS);
2488         hdr->len = htobs(cp_size);
2489         hdr->index = htobs(index);
2490
2491         cp = (void *) (buf + sizeof(*hdr));
2492         cp->key_count = htobs(key_count);
2493
2494         for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
2495                 struct smp_ltk_info *info = l->data;
2496
2497                 bacpy(&key->addr.bdaddr, &info->bdaddr);
2498                 key->addr.type = info->bdaddr_type;
2499                 memcpy(key->val, info->val, sizeof(info->val));
2500                 memcpy(key->rand, info->rand, sizeof(info->rand));
2501                 memcpy(&key->ediv, &info->ediv, sizeof(key->ediv));
2502                 key->authenticated = info->authenticated;
2503                 key->master = info->master;
2504                 key->enc_size = info->enc_size;
2505         }
2506
2507         if (write(mgmt_sock, buf, sizeof(*hdr) + cp_size) < 0)
2508                 err = -errno;
2509         else
2510                 err = 0;
2511
2512         g_free(buf);
2513
2514         return err;
2515 }
2516
2517 static struct btd_adapter_ops mgmt_ops = {
2518         .setup = mgmt_setup,
2519         .cleanup = mgmt_cleanup,
2520         .set_powered = mgmt_set_powered,
2521         .set_discoverable = mgmt_set_discoverable,
2522         .set_pairable = mgmt_set_pairable,
2523         .start_discovery = mgmt_start_discovery,
2524         .stop_discovery = mgmt_stop_discovery,
2525         .set_name = mgmt_set_name,
2526         .set_dev_class = mgmt_set_dev_class,
2527         .set_fast_connectable = mgmt_set_fast_connectable,
2528         .read_clock = mgmt_read_clock,
2529         .read_bdaddr = mgmt_read_bdaddr,
2530         .block_device = mgmt_block_device,
2531         .unblock_device = mgmt_unblock_device,
2532         .get_conn_list = mgmt_get_conn_list,
2533         .disconnect = mgmt_disconnect,
2534         .remove_bonding = mgmt_unpair_device,
2535         .pincode_reply = mgmt_pincode_reply,
2536         .confirm_reply = mgmt_confirm_reply,
2537         .passkey_reply = mgmt_passkey_reply,
2538         .encrypt_link = mgmt_encrypt_link,
2539         .set_did = mgmt_set_did,
2540         .add_uuid = mgmt_add_uuid,
2541         .remove_uuid = mgmt_remove_uuid,
2542         .disable_cod_cache = mgmt_disable_cod_cache,
2543         .restore_powered = mgmt_restore_powered,
2544         .load_keys = mgmt_load_link_keys,
2545         .set_io_capability = mgmt_set_io_capability,
2546         .create_bonding = mgmt_create_bonding,
2547         .cancel_bonding = mgmt_cancel_bonding,
2548         .read_local_oob_data = mgmt_read_local_oob_data,
2549         .add_remote_oob_data = mgmt_add_remote_oob_data,
2550         .remove_remote_oob_data = mgmt_remove_remote_oob_data,
2551         .confirm_name = mgmt_confirm_name,
2552         .load_ltks = mgmtops_load_ltks,
2553 };
2554
2555 static int mgmt_init(void)
2556 {
2557 #ifdef __TIZEN_PATCH__
2558         return btd_register_adapter_ops(&mgmt_ops, FALSE);
2559 #else
2560         return btd_register_adapter_ops(&mgmt_ops, TRUE);
2561 #endif
2562 }
2563
2564 static void mgmt_exit(void)
2565 {
2566         btd_adapter_cleanup_ops(&mgmt_ops);
2567 }
2568
2569 BLUETOOTH_PLUGIN_DEFINE(mgmtops, VERSION,
2570                 BLUETOOTH_PLUGIN_PRIORITY_LOW, mgmt_init, mgmt_exit)