tizen 2.4 release
[framework/connectivity/bluez.git] / monitor / control.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011-2014  Intel Corporation
6  *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 2.1 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdbool.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/time.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38
39 #include "lib/bluetooth.h"
40 #include "lib/hci.h"
41 #include "lib/mgmt.h"
42
43 #include "src/shared/util.h"
44 #include "src/shared/btsnoop.h"
45 #include "src/shared/mainloop.h"
46
47 #include "display.h"
48 #include "packet.h"
49 #include "hcidump.h"
50 #include "ellisys.h"
51 #include "control.h"
52
53 static struct btsnoop *btsnoop_file = NULL;
54 static bool hcidump_fallback = false;
55
56 struct control_data {
57         uint16_t channel;
58         int fd;
59         unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
60         uint16_t offset;
61 };
62
63 static void free_data(void *user_data)
64 {
65         struct control_data *data = user_data;
66
67         close(data->fd);
68
69         free(data);
70 }
71
72 static void mgmt_index_added(uint16_t len, const void *buf)
73 {
74         printf("@ Index Added\n");
75
76         packet_hexdump(buf, len);
77 }
78
79 static void mgmt_index_removed(uint16_t len, const void *buf)
80 {
81         printf("@ Index Removed\n");
82
83         packet_hexdump(buf, len);
84 }
85
86 static void mgmt_unconf_index_added(uint16_t len, const void *buf)
87 {
88         printf("@ Unconfigured Index Added\n");
89
90         packet_hexdump(buf, len);
91 }
92
93 static void mgmt_unconf_index_removed(uint16_t len, const void *buf)
94 {
95         printf("@ Unconfigured Index Removed\n");
96
97         packet_hexdump(buf, len);
98 }
99
100 static void mgmt_ext_index_added(uint16_t len, const void *buf)
101 {
102         const struct mgmt_ev_ext_index_added *ev = buf;
103
104         if (len < sizeof(*ev)) {
105                 printf("* Malformed Extended Index Added control\n");
106                 return;
107         }
108
109         printf("@ Extended Index Added: %u (%u)\n", ev->type, ev->bus);
110
111         buf += sizeof(*ev);
112         len -= sizeof(*ev);
113
114         packet_hexdump(buf, len);
115 }
116
117 static void mgmt_ext_index_removed(uint16_t len, const void *buf)
118 {
119         const struct mgmt_ev_ext_index_removed *ev = buf;
120
121         if (len < sizeof(*ev)) {
122                 printf("* Malformed Extended Index Removed control\n");
123                 return;
124         }
125
126         printf("@ Extended Index Removed: %u (%u)\n", ev->type, ev->bus);
127
128         buf += sizeof(*ev);
129         len -= sizeof(*ev);
130
131         packet_hexdump(buf, len);
132 }
133
134 static void mgmt_controller_error(uint16_t len, const void *buf)
135 {
136         const struct mgmt_ev_controller_error *ev = buf;
137
138         if (len < sizeof(*ev)) {
139                 printf("* Malformed Controller Error control\n");
140                 return;
141         }
142
143         printf("@ Controller Error: 0x%2.2x\n", ev->error_code);
144
145         buf += sizeof(*ev);
146         len -= sizeof(*ev);
147
148         packet_hexdump(buf, len);
149 }
150
151 #ifndef NELEM
152 #define NELEM(x) (sizeof(x) / sizeof((x)[0]))
153 #endif
154
155 static const char *config_options_str[] = {
156         "external", "public-address",
157 };
158
159 static void mgmt_new_config_options(uint16_t len, const void *buf)
160 {
161         uint32_t options;
162         unsigned int i;
163
164         if (len < 4) {
165                 printf("* Malformed New Configuration Options control\n");
166                 return;
167         }
168
169         options = get_le32(buf);
170
171         printf("@ New Configuration Options: 0x%4.4x\n", options);
172
173         if (options) {
174                 printf("%-12c", ' ');
175                 for (i = 0; i < NELEM(config_options_str); i++) {
176                         if (options & (1 << i))
177                                 printf("%s ", config_options_str[i]);
178                 }
179                 printf("\n");
180         }
181
182         buf += 4;
183         len -= 4;
184
185         packet_hexdump(buf, len);
186 }
187
188 static const char *settings_str[] = {
189         "powered", "connectable", "fast-connectable", "discoverable",
190         "bondable", "link-security", "ssp", "br/edr", "hs", "le",
191         "advertising", "secure-conn", "debug-keys", "privacy",
192         "configuration", "static-addr",
193 };
194
195 static void mgmt_new_settings(uint16_t len, const void *buf)
196 {
197         uint32_t settings;
198         unsigned int i;
199
200         if (len < 4) {
201                 printf("* Malformed New Settings control\n");
202                 return;
203         }
204
205         settings = get_le32(buf);
206
207         printf("@ New Settings: 0x%4.4x\n", settings);
208
209         if (settings) {
210                 printf("%-12c", ' ');
211                 for (i = 0; i < NELEM(settings_str); i++) {
212                         if (settings & (1 << i))
213                                 printf("%s ", settings_str[i]);
214                 }
215                 printf("\n");
216         }
217
218         buf += 4;
219         len -= 4;
220
221         packet_hexdump(buf, len);
222 }
223
224 static void mgmt_class_of_dev_changed(uint16_t len, const void *buf)
225 {
226         const struct mgmt_ev_class_of_dev_changed *ev = buf;
227
228         if (len < sizeof(*ev)) {
229                 printf("* Malformed Class of Device Changed control\n");
230                 return;
231         }
232
233         printf("@ Class of Device Changed: 0x%2.2x%2.2x%2.2x\n",
234                                                 ev->dev_class[2],
235                                                 ev->dev_class[1],
236                                                 ev->dev_class[0]);
237
238         buf += sizeof(*ev);
239         len -= sizeof(*ev);
240
241         packet_hexdump(buf, len);
242 }
243
244 static void mgmt_local_name_changed(uint16_t len, const void *buf)
245 {
246         const struct mgmt_ev_local_name_changed *ev = buf;
247
248         if (len < sizeof(*ev)) {
249                 printf("* Malformed Local Name Changed control\n");
250                 return;
251         }
252
253         printf("@ Local Name Changed: %s (%s)\n", ev->name, ev->short_name);
254
255         buf += sizeof(*ev);
256         len -= sizeof(*ev);
257
258         packet_hexdump(buf, len);
259 }
260
261 static void mgmt_new_link_key(uint16_t len, const void *buf)
262 {
263         const struct mgmt_ev_new_link_key *ev = buf;
264         char str[18];
265
266         if (len < sizeof(*ev)) {
267                 printf("* Malformed New Link Key control\n");
268                 return;
269         }
270
271         ba2str(&ev->key.addr.bdaddr, str);
272
273         printf("@ New Link Key: %s (%d)\n", str, ev->key.addr.type);
274
275         buf += sizeof(*ev);
276         len -= sizeof(*ev);
277
278         packet_hexdump(buf, len);
279 }
280
281 static void mgmt_new_long_term_key(uint16_t len, const void *buf)
282 {
283         const struct mgmt_ev_new_long_term_key *ev = buf;
284         const char *type;
285         char str[18];
286
287         if (len < sizeof(*ev)) {
288                 printf("* Malformed New Long Term Key control\n");
289                 return;
290         }
291
292         /* LE SC keys are both for master and slave */
293         switch (ev->key.type) {
294         case 0x00:
295                 if (ev->key.master)
296                         type = "Master (Unauthenticated)";
297                 else
298                         type = "Slave (Unauthenticated)";
299                 break;
300         case 0x01:
301                 if (ev->key.master)
302                         type = "Master (Authenticated)";
303                 else
304                         type = "Slave (Authenticated)";
305                 break;
306         case 0x02:
307                 type = "SC (Unauthenticated)";
308                 break;
309         case 0x03:
310                 type = "SC (Authenticated)";
311                 break;
312         case 0x04:
313                 type = "SC (Debug)";
314                 break;
315         default:
316                 type = "<unknown>";
317                 break;
318         }
319
320         ba2str(&ev->key.addr.bdaddr, str);
321
322         printf("@ New Long Term Key: %s (%d) %s 0x%02x\n", str,
323                         ev->key.addr.type, type, ev->key.type);
324
325         buf += sizeof(*ev);
326         len -= sizeof(*ev);
327
328         packet_hexdump(buf, len);
329 }
330
331 static void mgmt_device_connected(uint16_t len, const void *buf)
332 {
333         const struct mgmt_ev_device_connected *ev = buf;
334         uint32_t flags;
335         char str[18];
336
337         if (len < sizeof(*ev)) {
338                 printf("* Malformed Device Connected control\n");
339                 return;
340         }
341
342         flags = le32_to_cpu(ev->flags);
343         ba2str(&ev->addr.bdaddr, str);
344
345         printf("@ Device Connected: %s (%d) flags 0x%4.4x\n",
346                                                 str, ev->addr.type, flags);
347
348         buf += sizeof(*ev);
349         len -= sizeof(*ev);
350
351         packet_hexdump(buf, len);
352 }
353
354 static void mgmt_device_disconnected(uint16_t len, const void *buf)
355 {
356         const struct mgmt_ev_device_disconnected *ev = buf;
357         char str[18];
358         uint8_t reason;
359         uint16_t consumed_len;
360
361         if (len < sizeof(struct mgmt_addr_info)) {
362                 printf("* Malformed Device Disconnected control\n");
363                 return;
364         }
365
366         if (len < sizeof(*ev)) {
367                 reason = MGMT_DEV_DISCONN_UNKNOWN;
368                 consumed_len = len;
369         } else {
370                 reason = ev->reason;
371                 consumed_len = sizeof(*ev);
372         }
373
374         ba2str(&ev->addr.bdaddr, str);
375
376         printf("@ Device Disconnected: %s (%d) reason %u\n", str, ev->addr.type,
377                                                                         reason);
378
379         buf += consumed_len;
380         len -= consumed_len;
381
382         packet_hexdump(buf, len);
383 }
384
385 static void mgmt_connect_failed(uint16_t len, const void *buf)
386 {
387         const struct mgmt_ev_connect_failed *ev = buf;
388         char str[18];
389
390         if (len < sizeof(*ev)) {
391                 printf("* Malformed Connect Failed control\n");
392                 return;
393         }
394
395         ba2str(&ev->addr.bdaddr, str);
396
397         printf("@ Connect Failed: %s (%d) status 0x%2.2x\n",
398                                         str, ev->addr.type, ev->status);
399
400         buf += sizeof(*ev);
401         len -= sizeof(*ev);
402
403         packet_hexdump(buf, len);
404 }
405
406 static void mgmt_pin_code_request(uint16_t len, const void *buf)
407 {
408         const struct mgmt_ev_pin_code_request *ev = buf;
409         char str[18];
410
411         if (len < sizeof(*ev)) {
412                 printf("* Malformed PIN Code Request control\n");
413                 return;
414         }
415
416         ba2str(&ev->addr.bdaddr, str);
417
418         printf("@ PIN Code Request: %s (%d) secure 0x%2.2x\n",
419                                         str, ev->addr.type, ev->secure);
420
421         buf += sizeof(*ev);
422         len -= sizeof(*ev);
423
424         packet_hexdump(buf, len);
425 }
426
427 static void mgmt_user_confirm_request(uint16_t len, const void *buf)
428 {
429         const struct mgmt_ev_user_confirm_request *ev = buf;
430         char str[18];
431
432         if (len < sizeof(*ev)) {
433                 printf("* Malformed User Confirmation Request control\n");
434                 return;
435         }
436
437         ba2str(&ev->addr.bdaddr, str);
438
439         printf("@ User Confirmation Request: %s (%d) hint %d value %d\n",
440                         str, ev->addr.type, ev->confirm_hint, ev->value);
441
442         buf += sizeof(*ev);
443         len -= sizeof(*ev);
444
445         packet_hexdump(buf, len);
446 }
447
448 static void mgmt_user_passkey_request(uint16_t len, const void *buf)
449 {
450         const struct mgmt_ev_user_passkey_request *ev = buf;
451         char str[18];
452
453         if (len < sizeof(*ev)) {
454                 printf("* Malformed User Passkey Request control\n");
455                 return;
456         }
457
458         ba2str(&ev->addr.bdaddr, str);
459
460         printf("@ User Passkey Request: %s (%d)\n", str, ev->addr.type);
461
462         buf += sizeof(*ev);
463         len -= sizeof(*ev);
464
465         packet_hexdump(buf, len);
466 }
467
468 static void mgmt_auth_failed(uint16_t len, const void *buf)
469 {
470         const struct mgmt_ev_auth_failed *ev = buf;
471         char str[18];
472
473         if (len < sizeof(*ev)) {
474                 printf("* Malformed Authentication Failed control\n");
475                 return;
476         }
477
478         ba2str(&ev->addr.bdaddr, str);
479
480         printf("@ Authentication Failed: %s (%d) status 0x%2.2x\n",
481                                         str, ev->addr.type, ev->status);
482
483         buf += sizeof(*ev);
484         len -= sizeof(*ev);
485
486         packet_hexdump(buf, len);
487 }
488
489 static void mgmt_device_found(uint16_t len, const void *buf)
490 {
491         const struct mgmt_ev_device_found *ev = buf;
492         uint32_t flags;
493         char str[18];
494
495         if (len < sizeof(*ev)) {
496                 printf("* Malformed Device Found control\n");
497                 return;
498         }
499
500         flags = le32_to_cpu(ev->flags);
501         ba2str(&ev->addr.bdaddr, str);
502
503         printf("@ Device Found: %s (%d) rssi %d flags 0x%4.4x\n",
504                                         str, ev->addr.type, ev->rssi, flags);
505
506         buf += sizeof(*ev);
507         len -= sizeof(*ev);
508
509         packet_hexdump(buf, len);
510 }
511
512 static void mgmt_discovering(uint16_t len, const void *buf)
513 {
514         const struct mgmt_ev_discovering *ev = buf;
515
516         if (len < sizeof(*ev)) {
517                 printf("* Malformed Discovering control\n");
518                 return;
519         }
520
521         printf("@ Discovering: 0x%2.2x (%d)\n", ev->discovering, ev->type);
522
523         buf += sizeof(*ev);
524         len -= sizeof(*ev);
525
526         packet_hexdump(buf, len);
527 }
528
529 static void mgmt_device_blocked(uint16_t len, const void *buf)
530 {
531         const struct mgmt_ev_device_blocked *ev = buf;
532         char str[18];
533
534         if (len < sizeof(*ev)) {
535                 printf("* Malformed Device Blocked control\n");
536                 return;
537         }
538
539         ba2str(&ev->addr.bdaddr, str);
540
541         printf("@ Device Blocked: %s (%d)\n", str, ev->addr.type);
542
543         buf += sizeof(*ev);
544         len -= sizeof(*ev);
545
546         packet_hexdump(buf, len);
547 }
548
549 static void mgmt_device_unblocked(uint16_t len, const void *buf)
550 {
551         const struct mgmt_ev_device_unblocked *ev = buf;
552         char str[18];
553
554         if (len < sizeof(*ev)) {
555                 printf("* Malformed Device Unblocked control\n");
556                 return;
557         }
558
559         ba2str(&ev->addr.bdaddr, str);
560
561         printf("@ Device Unblocked: %s (%d)\n", str, ev->addr.type);
562
563         buf += sizeof(*ev);
564         len -= sizeof(*ev);
565
566         packet_hexdump(buf, len);
567 }
568
569 static void mgmt_device_unpaired(uint16_t len, const void *buf)
570 {
571         const struct mgmt_ev_device_unpaired *ev = buf;
572         char str[18];
573
574         if (len < sizeof(*ev)) {
575                 printf("* Malformed Device Unpaired control\n");
576                 return;
577         }
578
579         ba2str(&ev->addr.bdaddr, str);
580
581         printf("@ Device Unpaired: %s (%d)\n", str, ev->addr.type);
582
583         buf += sizeof(*ev);
584         len -= sizeof(*ev);
585
586         packet_hexdump(buf, len);
587 }
588
589 static void mgmt_passkey_notify(uint16_t len, const void *buf)
590 {
591         const struct mgmt_ev_passkey_notify *ev = buf;
592         uint32_t passkey;
593         char str[18];
594
595         if (len < sizeof(*ev)) {
596                 printf("* Malformed Passkey Notify control\n");
597                 return;
598         }
599
600         ba2str(&ev->addr.bdaddr, str);
601
602         passkey = le32_to_cpu(ev->passkey);
603
604         printf("@ Passkey Notify: %s (%d) passkey %06u entered %u\n",
605                                 str, ev->addr.type, passkey, ev->entered);
606
607         buf += sizeof(*ev);
608         len -= sizeof(*ev);
609
610         packet_hexdump(buf, len);
611 }
612
613 static void mgmt_new_irk(uint16_t len, const void *buf)
614 {
615         const struct mgmt_ev_new_irk *ev = buf;
616         char addr[18], rpa[18];
617
618         if (len < sizeof(*ev)) {
619                 printf("* Malformed New IRK control\n");
620                 return;
621         }
622
623         ba2str(&ev->rpa, rpa);
624         ba2str(&ev->key.addr.bdaddr, addr);
625
626         printf("@ New IRK: %s (%d) %s\n", addr, ev->key.addr.type, rpa);
627
628         buf += sizeof(*ev);
629         len -= sizeof(*ev);
630
631         packet_hexdump(buf, len);
632 }
633
634 static void mgmt_new_csrk(uint16_t len, const void *buf)
635 {
636         const struct mgmt_ev_new_csrk *ev = buf;
637         const char *type;
638         char addr[18];
639
640         if (len < sizeof(*ev)) {
641                 printf("* Malformed New CSRK control\n");
642                 return;
643         }
644
645         ba2str(&ev->key.addr.bdaddr, addr);
646
647         switch (ev->key.type) {
648         case 0x00:
649                 type = "Local Unauthenticated";
650                 break;
651         case 0x01:
652                 type = "Remote Unauthenticated";
653                 break;
654         case 0x02:
655                 type = "Local Authenticated";
656                 break;
657         case 0x03:
658                 type = "Remote Authenticated";
659                 break;
660         default:
661                 type = "<unknown>";
662                 break;
663         }
664
665         printf("@ New CSRK: %s (%d) %s (%u)\n", addr, ev->key.addr.type,
666                                                         type, ev->key.type);
667
668         buf += sizeof(*ev);
669         len -= sizeof(*ev);
670
671         packet_hexdump(buf, len);
672 }
673
674 static void mgmt_device_added(uint16_t len, const void *buf)
675 {
676         const struct mgmt_ev_device_added *ev = buf;
677         char str[18];
678
679         if (len < sizeof(*ev)) {
680                 printf("* Malformed Device Added control\n");
681                 return;
682         }
683
684         ba2str(&ev->addr.bdaddr, str);
685
686         printf("@ Device Added: %s (%d) %d\n", str, ev->addr.type, ev->action);
687
688         buf += sizeof(*ev);
689         len -= sizeof(*ev);
690
691         packet_hexdump(buf, len);
692 }
693
694 static void mgmt_device_removed(uint16_t len, const void *buf)
695 {
696         const struct mgmt_ev_device_removed *ev = buf;
697         char str[18];
698
699         if (len < sizeof(*ev)) {
700                 printf("* Malformed Device Removed control\n");
701                 return;
702         }
703
704         ba2str(&ev->addr.bdaddr, str);
705
706         printf("@ Device Removed: %s (%d)\n", str, ev->addr.type);
707
708         buf += sizeof(*ev);
709         len -= sizeof(*ev);
710
711         packet_hexdump(buf, len);
712 }
713
714 static void mgmt_new_conn_param(uint16_t len, const void *buf)
715 {
716         const struct mgmt_ev_new_conn_param *ev = buf;
717         char addr[18];
718         uint16_t min, max, latency, timeout;
719
720         if (len < sizeof(*ev)) {
721                 printf("* Malformed New Connection Parameter control\n");
722                 return;
723         }
724
725         ba2str(&ev->addr.bdaddr, addr);
726         min = le16_to_cpu(ev->min_interval);
727         max = le16_to_cpu(ev->max_interval);
728         latency = le16_to_cpu(ev->latency);
729         timeout = le16_to_cpu(ev->timeout);
730
731         printf("@ New Conn Param: %s (%d) hint %d min 0x%4.4x max 0x%4.4x "
732                 "latency 0x%4.4x timeout 0x%4.4x\n", addr, ev->addr.type,
733                 ev->store_hint, min, max, latency, timeout);
734
735         buf += sizeof(*ev);
736         len -= sizeof(*ev);
737
738         packet_hexdump(buf, len);
739 }
740
741 static void mgmt_advertising_added(uint16_t len, const void *buf)
742 {
743         const struct mgmt_ev_advertising_added *ev = buf;
744
745         if (len < sizeof(*ev)) {
746                 printf("* Malformed Advertising Added control\n");
747                 return;
748         }
749
750         printf("@ Advertising Added: %u\n", ev->instance);
751
752         buf += sizeof(*ev);
753         len -= sizeof(*ev);
754
755         packet_hexdump(buf, len);
756 }
757
758 static void mgmt_advertising_removed(uint16_t len, const void *buf)
759 {
760         const struct mgmt_ev_advertising_removed *ev = buf;
761
762         if (len < sizeof(*ev)) {
763                 printf("* Malformed Advertising Removed control\n");
764                 return;
765         }
766
767         printf("@ Advertising Removed: %u\n", ev->instance);
768
769         buf += sizeof(*ev);
770         len -= sizeof(*ev);
771
772         packet_hexdump(buf, len);
773 }
774
775 void control_message(uint16_t opcode, const void *data, uint16_t size)
776 {
777         switch (opcode) {
778         case MGMT_EV_INDEX_ADDED:
779                 mgmt_index_added(size, data);
780                 break;
781         case MGMT_EV_INDEX_REMOVED:
782                 mgmt_index_removed(size, data);
783                 break;
784         case MGMT_EV_CONTROLLER_ERROR:
785                 mgmt_controller_error(size, data);
786                 break;
787         case MGMT_EV_NEW_SETTINGS:
788                 mgmt_new_settings(size, data);
789                 break;
790         case MGMT_EV_CLASS_OF_DEV_CHANGED:
791                 mgmt_class_of_dev_changed(size, data);
792                 break;
793         case MGMT_EV_LOCAL_NAME_CHANGED:
794                 mgmt_local_name_changed(size, data);
795                 break;
796         case MGMT_EV_NEW_LINK_KEY:
797                 mgmt_new_link_key(size, data);
798                 break;
799         case MGMT_EV_NEW_LONG_TERM_KEY:
800                 mgmt_new_long_term_key(size, data);
801                 break;
802         case MGMT_EV_DEVICE_CONNECTED:
803                 mgmt_device_connected(size, data);
804                 break;
805         case MGMT_EV_DEVICE_DISCONNECTED:
806                 mgmt_device_disconnected(size, data);
807                 break;
808         case MGMT_EV_CONNECT_FAILED:
809                 mgmt_connect_failed(size, data);
810                 break;
811         case MGMT_EV_PIN_CODE_REQUEST:
812                 mgmt_pin_code_request(size, data);
813                 break;
814         case MGMT_EV_USER_CONFIRM_REQUEST:
815                 mgmt_user_confirm_request(size, data);
816                 break;
817         case MGMT_EV_USER_PASSKEY_REQUEST:
818                 mgmt_user_passkey_request(size, data);
819                 break;
820         case MGMT_EV_AUTH_FAILED:
821                 mgmt_auth_failed(size, data);
822                 break;
823         case MGMT_EV_DEVICE_FOUND:
824                 mgmt_device_found(size, data);
825                 break;
826         case MGMT_EV_DISCOVERING:
827                 mgmt_discovering(size, data);
828                 break;
829         case MGMT_EV_DEVICE_BLOCKED:
830                 mgmt_device_blocked(size, data);
831                 break;
832         case MGMT_EV_DEVICE_UNBLOCKED:
833                 mgmt_device_unblocked(size, data);
834                 break;
835         case MGMT_EV_DEVICE_UNPAIRED:
836                 mgmt_device_unpaired(size, data);
837                 break;
838         case MGMT_EV_PASSKEY_NOTIFY:
839                 mgmt_passkey_notify(size, data);
840                 break;
841         case MGMT_EV_NEW_IRK:
842                 mgmt_new_irk(size, data);
843                 break;
844         case MGMT_EV_NEW_CSRK:
845                 mgmt_new_csrk(size, data);
846                 break;
847         case MGMT_EV_DEVICE_ADDED:
848                 mgmt_device_added(size, data);
849                 break;
850         case MGMT_EV_DEVICE_REMOVED:
851                 mgmt_device_removed(size, data);
852                 break;
853         case MGMT_EV_NEW_CONN_PARAM:
854                 mgmt_new_conn_param(size, data);
855                 break;
856         case MGMT_EV_UNCONF_INDEX_ADDED:
857                 mgmt_unconf_index_added(size, data);
858                 break;
859         case MGMT_EV_UNCONF_INDEX_REMOVED:
860                 mgmt_unconf_index_removed(size, data);
861                 break;
862         case MGMT_EV_NEW_CONFIG_OPTIONS:
863                 mgmt_new_config_options(size, data);
864                 break;
865         case MGMT_EV_EXT_INDEX_ADDED:
866                 mgmt_ext_index_added(size, data);
867                 break;
868         case MGMT_EV_EXT_INDEX_REMOVED:
869                 mgmt_ext_index_removed(size, data);
870                 break;
871         case MGMT_EV_ADVERTISING_ADDED:
872                 mgmt_advertising_added(size, data);
873                 break;
874         case MGMT_EV_ADVERTISING_REMOVED:
875                 mgmt_advertising_removed(size, data);
876                 break;
877         default:
878                 printf("* Unknown control (code %d len %d)\n", opcode, size);
879                 packet_hexdump(data, size);
880                 break;
881         }
882 }
883
884 static void data_callback(int fd, uint32_t events, void *user_data)
885 {
886         struct control_data *data = user_data;
887         unsigned char control[32];
888         struct mgmt_hdr hdr;
889         struct msghdr msg;
890         struct iovec iov[2];
891
892         if (events & (EPOLLERR | EPOLLHUP)) {
893                 mainloop_remove_fd(data->fd);
894                 return;
895         }
896
897         iov[0].iov_base = &hdr;
898         iov[0].iov_len = MGMT_HDR_SIZE;
899         iov[1].iov_base = data->buf;
900         iov[1].iov_len = sizeof(data->buf);
901
902         memset(&msg, 0, sizeof(msg));
903         msg.msg_iov = iov;
904         msg.msg_iovlen = 2;
905         msg.msg_control = control;
906         msg.msg_controllen = sizeof(control);
907
908         while (1) {
909                 struct cmsghdr *cmsg;
910                 struct timeval *tv = NULL;
911                 struct timeval ctv;
912                 uint16_t opcode, index, pktlen;
913                 ssize_t len;
914
915                 len = recvmsg(data->fd, &msg, MSG_DONTWAIT);
916                 if (len < 0)
917                         break;
918
919                 if (len < MGMT_HDR_SIZE)
920                         break;
921
922                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
923                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
924                         if (cmsg->cmsg_level != SOL_SOCKET)
925                                 continue;
926
927                         if (cmsg->cmsg_type == SCM_TIMESTAMP) {
928                                 memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
929                                 tv = &ctv;
930                         }
931                 }
932
933                 opcode = le16_to_cpu(hdr.opcode);
934                 index  = le16_to_cpu(hdr.index);
935                 pktlen = le16_to_cpu(hdr.len);
936
937                 switch (data->channel) {
938                 case HCI_CHANNEL_CONTROL:
939                         packet_control(tv, index, opcode, data->buf, pktlen);
940                         break;
941                 case HCI_CHANNEL_MONITOR:
942                         btsnoop_write_hci(btsnoop_file, tv, index, opcode,
943                                                         data->buf, pktlen);
944                         ellisys_inject_hci(tv, index, opcode,
945                                                         data->buf, pktlen);
946                         packet_monitor(tv, index, opcode, data->buf, pktlen);
947                         break;
948                 }
949         }
950 }
951
952 static int open_socket(uint16_t channel)
953 {
954         struct sockaddr_hci addr;
955         int fd, opt = 1;
956
957         fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
958         if (fd < 0) {
959                 perror("Failed to open channel");
960                 return -1;
961         }
962
963         memset(&addr, 0, sizeof(addr));
964         addr.hci_family = AF_BLUETOOTH;
965         addr.hci_dev = HCI_DEV_NONE;
966         addr.hci_channel = channel;
967
968         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
969                 if (errno == EINVAL) {
970                         /* Fallback to hcidump support */
971                         hcidump_fallback = true;
972                         close(fd);
973                         return -1;
974                 }
975                 perror("Failed to bind channel");
976                 close(fd);
977                 return -1;
978         }
979
980         if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
981                 perror("Failed to enable timestamps");
982                 close(fd);
983                 return -1;
984         }
985
986         return fd;
987 }
988
989 static int open_channel(uint16_t channel)
990 {
991         struct control_data *data;
992
993         data = malloc(sizeof(*data));
994         if (!data)
995                 return -1;
996
997         memset(data, 0, sizeof(*data));
998         data->channel = channel;
999
1000         data->fd = open_socket(channel);
1001         if (data->fd < 0) {
1002                 free(data);
1003                 return -1;
1004         }
1005
1006         mainloop_add_fd(data->fd, EPOLLIN, data_callback, data, free_data);
1007
1008         return 0;
1009 }
1010
1011 static void client_callback(int fd, uint32_t events, void *user_data)
1012 {
1013         struct control_data *data = user_data;
1014         ssize_t len;
1015
1016         if (events & (EPOLLERR | EPOLLHUP)) {
1017                 mainloop_remove_fd(data->fd);
1018                 return;
1019         }
1020
1021         len = recv(data->fd, data->buf + data->offset,
1022                         sizeof(data->buf) - data->offset, MSG_DONTWAIT);
1023         if (len < 0)
1024                 return;
1025
1026         data->offset += len;
1027
1028         if (data->offset > MGMT_HDR_SIZE) {
1029                 struct mgmt_hdr *hdr = (struct mgmt_hdr *) data->buf;
1030                 uint16_t pktlen = le16_to_cpu(hdr->len);
1031
1032                 if (data->offset > pktlen + MGMT_HDR_SIZE) {
1033                         uint16_t opcode = le16_to_cpu(hdr->opcode);
1034                         uint16_t index = le16_to_cpu(hdr->index);
1035
1036                         packet_monitor(NULL, index, opcode,
1037                                         data->buf + MGMT_HDR_SIZE, pktlen);
1038
1039                         data->offset -= pktlen + MGMT_HDR_SIZE;
1040
1041                         if (data->offset > 0)
1042                                 memmove(data->buf, data->buf +
1043                                          MGMT_HDR_SIZE + pktlen, data->offset);
1044                 }
1045         }
1046 }
1047
1048 static void server_accept_callback(int fd, uint32_t events, void *user_data)
1049 {
1050         struct control_data *data;
1051         struct sockaddr_un addr;
1052         socklen_t len;
1053         int nfd;
1054
1055         if (events & (EPOLLERR | EPOLLHUP)) {
1056                 mainloop_remove_fd(fd);
1057                 return;
1058         }
1059
1060         memset(&addr, 0, sizeof(addr));
1061         len = sizeof(addr);
1062
1063         nfd = accept(fd, (struct sockaddr *) &addr, &len);
1064         if (nfd < 0) {
1065                 perror("Failed to accept client socket");
1066                 return;
1067         }
1068
1069         printf("--- New monitor connection ---\n");
1070
1071         data = malloc(sizeof(*data));
1072         if (!data) {
1073                 close(nfd);
1074                 return;
1075         }
1076
1077         memset(data, 0, sizeof(*data));
1078         data->channel = HCI_CHANNEL_MONITOR;
1079         data->fd = nfd;
1080
1081         mainloop_add_fd(data->fd, EPOLLIN, client_callback, data, free_data);
1082 }
1083
1084 static int server_fd = -1;
1085
1086 void control_server(const char *path)
1087 {
1088         struct sockaddr_un addr;
1089         int fd;
1090
1091         if (server_fd >= 0)
1092                 return;
1093
1094         unlink(path);
1095
1096         fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
1097         if (fd < 0) {
1098                 perror("Failed to open server socket");
1099                 return;
1100         }
1101
1102         memset(&addr, 0, sizeof(addr));
1103         addr.sun_family = AF_UNIX;
1104         strcpy(addr.sun_path, path);
1105
1106         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1107                 perror("Failed to bind server socket");
1108                 close(fd);
1109                 return;
1110         }
1111
1112         if (listen(fd, 5) < 0) {
1113                 perror("Failed to listen server socket");
1114                 close(fd);
1115                 return;
1116         }
1117
1118         if (mainloop_add_fd(fd, EPOLLIN, server_accept_callback,
1119                                                 NULL, NULL) < 0) {
1120                 close(fd);
1121                 return;
1122         }
1123
1124         server_fd = fd;
1125 }
1126
1127 #ifdef __TIZEN_PATCH__
1128 bool control_writer(const char *path, int16_t rotate_count, ssize_t file_size)
1129 #else
1130 bool control_writer(const char *path)
1131 #endif
1132 {
1133 #ifdef __TIZEN_PATCH__
1134         btsnoop_file = btsnoop_create(path, BTSNOOP_TYPE_MONITOR,
1135                         rotate_count, file_size);
1136 #else
1137         btsnoop_file = btsnoop_create(path, BTSNOOP_TYPE_MONITOR);
1138 #endif
1139
1140         return !!btsnoop_file;
1141 }
1142
1143 void control_reader(const char *path)
1144 {
1145         unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
1146         uint16_t pktlen;
1147         uint32_t type;
1148         struct timeval tv;
1149
1150         btsnoop_file = btsnoop_open(path, BTSNOOP_FLAG_PKLG_SUPPORT);
1151         if (!btsnoop_file)
1152                 return;
1153
1154         type = btsnoop_get_type(btsnoop_file);
1155
1156         switch (type) {
1157         case BTSNOOP_TYPE_HCI:
1158         case BTSNOOP_TYPE_UART:
1159         case BTSNOOP_TYPE_SIMULATOR:
1160                 packet_del_filter(PACKET_FILTER_SHOW_INDEX);
1161                 break;
1162
1163         case BTSNOOP_TYPE_MONITOR:
1164                 packet_add_filter(PACKET_FILTER_SHOW_INDEX);
1165                 break;
1166         }
1167
1168         open_pager();
1169
1170         switch (type) {
1171         case BTSNOOP_TYPE_HCI:
1172         case BTSNOOP_TYPE_UART:
1173         case BTSNOOP_TYPE_MONITOR:
1174                 while (1) {
1175                         uint16_t index, opcode;
1176
1177                         if (!btsnoop_read_hci(btsnoop_file, &tv, &index,
1178                                                         &opcode, buf, &pktlen))
1179                                 break;
1180
1181                         if (opcode == 0xffff)
1182                                 continue;
1183
1184                         packet_monitor(&tv, index, opcode, buf, pktlen);
1185                         ellisys_inject_hci(&tv, index, opcode, buf, pktlen);
1186                 }
1187                 break;
1188
1189         case BTSNOOP_TYPE_SIMULATOR:
1190                 while (1) {
1191                         uint16_t frequency;
1192
1193                         if (!btsnoop_read_phy(btsnoop_file, &tv, &frequency,
1194                                                                 buf, &pktlen))
1195                                 break;
1196
1197                         packet_simulator(&tv, frequency, buf, pktlen);
1198                 }
1199                 break;
1200         }
1201
1202         close_pager();
1203
1204         btsnoop_unref(btsnoop_file);
1205 }
1206
1207 int control_tracing(void)
1208 {
1209         packet_add_filter(PACKET_FILTER_SHOW_INDEX);
1210
1211         if (server_fd >= 0)
1212                 return 0;
1213
1214         if (open_channel(HCI_CHANNEL_MONITOR) < 0) {
1215                 if (!hcidump_fallback)
1216                         return -1;
1217                 if (hcidump_tracing() < 0)
1218                         return -1;
1219                 return 0;
1220         }
1221
1222         open_channel(HCI_CHANNEL_CONTROL);
1223
1224         return 0;
1225 }