tizen 2.3 release
[framework/connectivity/bluez.git] / android / android-tester.c
1 /*
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21
22 #include <glib.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/un.h>
27 #include <sys/signalfd.h>
28 #include <libgen.h>
29
30 #include "lib/bluetooth.h"
31 #include "lib/mgmt.h"
32
33 #include "src/shared/tester.h"
34 #include "src/shared/mgmt.h"
35 #include "src/shared/hciemu.h"
36
37 #include "emulator/bthost.h"
38 #include "monitor/bt.h"
39
40 #include <hardware/hardware.h>
41 #include <hardware/bluetooth.h>
42 #include <hardware/bt_sock.h>
43 #include <hardware/bt_hh.h>
44
45 #include "utils.h"
46
47 struct priority_property {
48         bt_property_t prop;
49         int prio;
50 };
51
52 struct generic_data {
53         int expected_adapter_status;
54         uint32_t expect_settings_set;
55         int expected_cb_count;
56         bt_property_t set_property;
57         bt_callbacks_t expected_hal_cb;
58         struct priority_property *expected_properties;
59         uint8_t expected_properties_num;
60 };
61
62 struct socket_data {
63         btsock_type_t sock_type;
64         const char *service_name;
65         const uint8_t *service_uuid;
66         const bt_bdaddr_t *bdaddr;
67         int channel;
68         int flags;
69         bt_status_t expected_status;
70         bool test_channel;
71 };
72
73 struct hidhost_generic_data {
74         bthh_status_t expected_status;
75         int expected_conn_state;
76         int expected_cb_count;
77         bthh_protocol_mode_t expected_protocol_mode;
78         int expected_report;
79         bthh_callbacks_t expected_hal_cb;
80         int expected_report_size;
81 };
82
83 #define WAIT_FOR_SIGNAL_TIME 2 /* in seconds */
84 #define EMULATOR_SIGNAL "emulator_started"
85
86 #define BT_STATUS_NOT_EXPECTED  -1
87
88 struct test_data {
89         struct mgmt *mgmt;
90         uint16_t mgmt_index;
91         unsigned int mgmt_settings_id;
92         struct hciemu *hciemu;
93         enum hciemu_type hciemu_type;
94         const void *test_data;
95         pid_t bluetoothd_pid;
96         guint signalfd;
97
98         struct hw_device_t *device;
99         const bt_interface_t *if_bluetooth;
100         const btsock_interface_t *if_sock;
101         const bthh_interface_t *if_hid;
102
103         int conditions_left;
104
105         /* Set to true if test conditions are initialized */
106         bool test_init_done;
107
108         bool test_result_set;
109
110         int cb_count;
111         GSList *expected_properties_list;
112
113         /* hidhost */
114         uint16_t sdp_handle;
115         uint16_t sdp_cid;
116         uint16_t ctrl_handle;
117         uint16_t ctrl_cid;
118         uint16_t intr_handle;
119         uint16_t intr_cid;
120 };
121
122 struct bt_cb_data {
123         bt_state_t state;
124         bt_status_t status;
125
126         bt_bdaddr_t bdaddr;
127         bt_bdname_t bdname;
128         uint32_t cod;
129
130         bt_ssp_variant_t ssp_variant;
131         uint32_t passkey;
132
133         int num;
134         bt_property_t *props;
135 };
136
137 struct hh_cb_data {
138         bt_bdaddr_t bdaddr;
139
140         bthh_status_t status;
141         bthh_hid_info_t hid_info;
142         bthh_protocol_mode_t mode;
143         bthh_connection_state_t state;
144
145         uint8_t *report;
146         int size;
147 };
148
149 static char exec_dir[PATH_MAX + 1];
150
151 static gint scheduled_cbacks_num = 0;
152
153 static gboolean check_callbacks_called(gpointer user_data)
154 {
155         /* Wait for all callbacks scheduled in current test context to execute
156          * in main loop. This will avoid late callback calls after test case has
157          * already failed or timed out.
158          */
159
160         if (g_atomic_int_get(&scheduled_cbacks_num) == 0) {
161                 tester_teardown_complete();
162                 return FALSE;
163         }
164
165         return TRUE;
166 }
167 static void check_daemon_term(void)
168 {
169         int status;
170         pid_t pid;
171         struct test_data *data = tester_get_data();
172
173         if (!data)
174                 return;
175
176         pid = waitpid(data->bluetoothd_pid, &status, WNOHANG);
177         if (pid != data->bluetoothd_pid)
178                 return;
179
180         data->bluetoothd_pid = 0;
181
182         if (WIFEXITED(status) && (WEXITSTATUS(status) == EXIT_SUCCESS)) {
183                 g_idle_add(check_callbacks_called, NULL);
184                 return;
185         }
186
187         tester_warn("Unexpected Daemon shutdown with status %d", status);
188 }
189
190 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
191                                                         gpointer user_data)
192 {
193         struct signalfd_siginfo si;
194         ssize_t result;
195         int fd;
196
197         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
198                 return FALSE;
199
200         fd = g_io_channel_unix_get_fd(channel);
201
202         result = read(fd, &si, sizeof(si));
203         if (result != sizeof(si))
204                 return FALSE;
205
206         switch (si.ssi_signo) {
207         case SIGCHLD:
208                 check_daemon_term();
209                 break;
210         }
211
212         return TRUE;
213 }
214
215 static guint setup_signalfd(void)
216 {
217         GIOChannel *channel;
218         guint source;
219         sigset_t mask;
220         int fd;
221
222         sigemptyset(&mask);
223         sigaddset(&mask, SIGCHLD);
224
225         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
226                 return 0;
227
228         fd = signalfd(-1, &mask, 0);
229         if (fd < 0)
230                 return 0;
231
232         channel = g_io_channel_unix_new(fd);
233
234         g_io_channel_set_close_on_unref(channel, TRUE);
235         g_io_channel_set_encoding(channel, NULL, NULL);
236         g_io_channel_set_buffered(channel, FALSE);
237
238         source = g_io_add_watch(channel,
239                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
240                                 signal_handler, NULL);
241
242         g_io_channel_unref(channel);
243
244         return source;
245 }
246
247 static void mgmt_debug(const char *str, void *user_data)
248 {
249         const char *prefix = user_data;
250
251         tester_print("%s%s", prefix, str);
252 }
253
254 static void test_update_state(void)
255 {
256         struct test_data *data = tester_get_data();
257
258         if (data->conditions_left == 0 && !data->test_result_set) {
259                 data->test_result_set = true;
260                 tester_test_passed();
261         }
262 }
263
264 static void test_mgmt_settings_set(struct test_data *data)
265 {
266         data->conditions_left--;
267
268         test_update_state();
269 }
270
271 static void command_generic_new_settings(uint16_t index, uint16_t length,
272                                         const void *param, void *user_data)
273 {
274         struct test_data *data = tester_get_data();
275         const struct generic_data *test_data = data->test_data;
276         uint32_t settings;
277
278         if (length != 4) {
279                 tester_warn("Invalid parameter size for new settings event");
280                 tester_test_failed();
281                 return;
282         }
283
284         settings = bt_get_le32(param);
285
286         if ((settings & test_data->expect_settings_set) !=
287                                         test_data->expect_settings_set)
288                 return;
289
290         test_mgmt_settings_set(data);
291         mgmt_unregister(data->mgmt, data->mgmt_settings_id);
292 }
293
294 static void check_cb_count(void)
295 {
296         struct test_data *data = tester_get_data();
297
298         if (!data->test_init_done)
299                 return;
300
301         if (data->cb_count == 0) {
302                 data->conditions_left--;
303                 test_update_state();
304         }
305 }
306
307 static void expected_cb_count_init(struct test_data *data)
308 {
309         const struct generic_data *test_data = data->test_data;
310
311         data->cb_count = test_data->expected_cb_count;
312
313         check_cb_count();
314 }
315
316 static void mgmt_cb_init(struct test_data *data)
317 {
318         const struct generic_data *test_data = data->test_data;
319
320         if (!test_data->expect_settings_set)
321                 test_mgmt_settings_set(data);
322         else
323                 data->mgmt_settings_id = mgmt_register(data->mgmt,
324                                 MGMT_EV_NEW_SETTINGS, data->mgmt_index,
325                                 command_generic_new_settings, NULL, NULL);
326 }
327
328 static void expected_status_init(struct test_data *data)
329 {
330         const struct generic_data *test_data = data->test_data;
331
332         if (test_data->expected_adapter_status == BT_STATUS_NOT_EXPECTED)
333                 data->conditions_left--;
334 }
335
336 static void test_property_init(struct test_data *data)
337 {
338         const struct generic_data *test_data = data->test_data;
339         GSList *l = data->expected_properties_list;
340         int i;
341
342         if (!test_data->expected_properties_num) {
343                 data->conditions_left--;
344                 return;
345         }
346
347         for (i = 0; i < test_data->expected_properties_num; i++)
348                 l = g_slist_prepend(l, &(test_data->expected_properties[i]));
349
350         data->expected_properties_list = l;
351 }
352
353 static void init_test_conditions(struct test_data *data)
354 {
355         data->test_init_done = true;
356
357         data->conditions_left = 4;
358
359         expected_cb_count_init(data);
360         mgmt_cb_init(data);
361         expected_status_init(data);
362         test_property_init(data);
363 }
364
365 static void check_expected_status(uint8_t status)
366 {
367         struct test_data *data = tester_get_data();
368         const struct generic_data *test_data = data->test_data;
369
370         if (test_data->expected_adapter_status == status) {
371                 data->conditions_left--;
372                 test_update_state();
373         } else
374                 tester_test_failed();
375 }
376
377 static int locate_property(gconstpointer expected_data,
378                                                 gconstpointer received_prop)
379 {
380         bt_property_t rec_prop = *((bt_property_t *)received_prop);
381         bt_property_t exp_prop =
382                         ((struct priority_property *)expected_data)->prop;
383
384         if (exp_prop.type && (exp_prop.type != rec_prop.type))
385                 return 1;
386         if (exp_prop.len && (exp_prop.len != rec_prop.len))
387                 return 1;
388         if (exp_prop.val && memcmp(exp_prop.val, rec_prop.val, exp_prop.len))
389                 return 1;
390
391         return 0;
392 }
393
394 static int compare_priorities(gconstpointer prop_list, gconstpointer priority)
395 {
396         int prio = GPOINTER_TO_INT(priority);
397         int comp_prio = ((struct priority_property *)prop_list)->prio;
398
399         if (prio > comp_prio)
400                 return 0;
401
402         return 1;
403 }
404
405 static bool check_prop_priority(int rec_prop_prio)
406 {
407         struct test_data *data = tester_get_data();
408         GSList *l = data->expected_properties_list;
409
410         if (!rec_prop_prio || !g_slist_length(l))
411                 return true;
412
413         if (g_slist_find_custom(l, GINT_TO_POINTER(rec_prop_prio),
414                                                         &compare_priorities))
415                 return false;
416
417         return true;
418 }
419
420 static void check_expected_property(bt_property_t received_prop)
421 {
422         struct test_data *data = tester_get_data();
423         int rec_prio;
424         GSList *l = data->expected_properties_list;
425         GSList *found_exp_prop;
426
427         if (!g_slist_length(l))
428                 return;
429
430         found_exp_prop = g_slist_find_custom(l, &received_prop,
431                                                         &locate_property);
432
433         if (found_exp_prop) {
434                 rec_prio = ((struct priority_property *)
435                                                 (found_exp_prop->data))->prio;
436                 if (check_prop_priority(rec_prio))
437                         l = g_slist_remove(l, found_exp_prop->data);
438         }
439
440         data->expected_properties_list = l;
441
442         if (g_slist_length(l))
443                 return;
444
445         data->conditions_left--;
446         test_update_state();
447 }
448
449 static bool check_test_property(bt_property_t received_prop,
450                                                 bt_property_t expected_prop)
451 {
452         if (expected_prop.type && (expected_prop.type != received_prop.type))
453                 return false;
454         if (expected_prop.len && (expected_prop.len != received_prop.len))
455                 return false;
456         if (expected_prop.val && memcmp(expected_prop.val, received_prop.val,
457                                                         expected_prop.len))
458                 return false;
459
460         return true;
461 }
462
463 static void read_info_callback(uint8_t status, uint16_t length,
464                                         const void *param, void *user_data)
465 {
466         struct test_data *data = tester_get_data();
467         const struct mgmt_rp_read_info *rp = param;
468         char addr[18];
469         uint16_t manufacturer;
470         uint32_t supported_settings, current_settings;
471
472         tester_print("Read Info callback");
473         tester_print("  Status: 0x%02x", status);
474
475         if (status || !param) {
476                 tester_pre_setup_failed();
477                 return;
478         }
479
480         ba2str(&rp->bdaddr, addr);
481         manufacturer = btohs(rp->manufacturer);
482         supported_settings = btohl(rp->supported_settings);
483         current_settings = btohl(rp->current_settings);
484
485         tester_print("  Address: %s", addr);
486         tester_print("  Version: 0x%02x", rp->version);
487         tester_print("  Manufacturer: 0x%04x", manufacturer);
488         tester_print("  Supported settings: 0x%08x", supported_settings);
489         tester_print("  Current settings: 0x%08x", current_settings);
490         tester_print("  Class: 0x%02x%02x%02x",
491                         rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
492         tester_print("  Name: %s", rp->name);
493         tester_print("  Short name: %s", rp->short_name);
494
495         if (strcmp(hciemu_get_address(data->hciemu), addr)) {
496                 tester_pre_setup_failed();
497                 return;
498         }
499
500         tester_pre_setup_complete();
501 }
502
503 static void index_added_callback(uint16_t index, uint16_t length,
504                                         const void *param, void *user_data)
505 {
506         struct test_data *data = tester_get_data();
507
508         tester_print("Index Added callback");
509         tester_print("  Index: 0x%04x", index);
510
511         data->mgmt_index = index;
512
513         mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
514                                         read_info_callback, NULL, NULL);
515 }
516
517 static void index_removed_callback(uint16_t index, uint16_t length,
518                                         const void *param, void *user_data)
519 {
520         struct test_data *data = tester_get_data();
521
522         tester_print("Index Removed callback");
523         tester_print("  Index: 0x%04x", index);
524
525         if (index != data->mgmt_index)
526                 return;
527
528         mgmt_unregister_index(data->mgmt, data->mgmt_index);
529
530         mgmt_unref(data->mgmt);
531         data->mgmt = NULL;
532
533         tester_post_teardown_complete();
534 }
535
536 static void read_index_list_callback(uint8_t status, uint16_t length,
537                                         const void *param, void *user_data)
538 {
539         struct test_data *data = tester_get_data();
540
541         tester_print("Read Index List callback");
542         tester_print("  Status: 0x%02x", status);
543
544         if (status || !param) {
545                 tester_pre_setup_failed();
546                 return;
547         }
548
549         mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
550                                         index_added_callback, NULL, NULL);
551
552         mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
553                                         index_removed_callback, NULL, NULL);
554
555         data->hciemu = hciemu_new(data->hciemu_type);
556         if (!data->hciemu) {
557                 tester_warn("Failed to setup HCI emulation");
558                 tester_pre_setup_failed();
559                 return;
560         }
561
562         tester_print("New hciemu instance created");
563 }
564
565 static void test_pre_setup(const void *test_data)
566 {
567         struct test_data *data = tester_get_data();
568
569         data->signalfd = setup_signalfd();
570         if (!data->signalfd) {
571                 tester_warn("Failed to setup signalfd");
572                 tester_pre_setup_failed();
573                 return;
574         }
575
576         data->mgmt = mgmt_new_default();
577         if (!data->mgmt) {
578                 tester_warn("Failed to setup management interface");
579                 tester_pre_setup_failed();
580                 return;
581         }
582
583         if (!tester_use_debug())
584                 fclose(stderr);
585         else
586                 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
587
588         mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0,
589                                 NULL, read_index_list_callback, NULL, NULL);
590 }
591
592 static void test_post_teardown(const void *test_data)
593 {
594         struct test_data *data = tester_get_data();
595
596         hciemu_unref(data->hciemu);
597         data->hciemu = NULL;
598
599         g_source_remove(data->signalfd);
600         data->signalfd = 0;
601 }
602
603 static void bluetoothd_start(int hci_index)
604 {
605         char prg_name[PATH_MAX + 1];
606         char index[8];
607         char *prg_argv[4];
608
609         snprintf(prg_name, sizeof(prg_name), "%s/%s", exec_dir, "bluetoothd");
610         snprintf(index, sizeof(index), "%d", hci_index);
611
612         prg_argv[0] = prg_name;
613         prg_argv[1] = "-i";
614         prg_argv[2] = index;
615         prg_argv[3] = NULL;
616
617         if (!tester_use_debug())
618                 fclose(stderr);
619
620         execve(prg_argv[0], prg_argv, NULL);
621 }
622
623 static void emulator(int pipe, int hci_index)
624 {
625         static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
626         char buf[1024];
627         struct sockaddr_un addr;
628         struct timeval tv;
629         int fd;
630         ssize_t len;
631
632         fd = socket(PF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
633         if (fd < 0)
634                 goto failed;
635
636         tv.tv_sec = WAIT_FOR_SIGNAL_TIME;
637         tv.tv_usec = 0;
638         setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
639
640         memset(&addr, 0, sizeof(addr));
641         addr.sun_family = AF_UNIX;
642         memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
643
644         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
645                 perror("Failed to bind system socket");
646                 goto failed;
647         }
648
649         len = write(pipe, EMULATOR_SIGNAL, sizeof(EMULATOR_SIGNAL));
650         if (len != sizeof(EMULATOR_SIGNAL))
651                 goto failed;
652
653         memset(buf, 0, sizeof(buf));
654
655         len = read(fd, buf, sizeof(buf));
656         if (len <= 0 || strcmp(buf, "bluetooth.start=daemon"))
657                 goto failed;
658
659         close(pipe);
660         close(fd);
661         return bluetoothd_start(hci_index);
662
663 failed:
664         close(pipe);
665
666         if (fd >= 0)
667                 close(fd);
668 }
669
670 static void emu_connectable_complete(uint16_t opcode, uint8_t status,
671                                         const void *param, uint8_t len,
672                                         void *user_data)
673 {
674         switch (opcode) {
675         case BT_HCI_CMD_WRITE_SCAN_ENABLE:
676         case BT_HCI_CMD_LE_SET_ADV_ENABLE:
677                 break;
678         default:
679                 return;
680         }
681
682         tester_print("Emulated remote set connectable status 0x%02x", status);
683
684         if (status)
685                 tester_setup_failed();
686         else
687                 tester_setup_complete();
688 }
689
690 static void setup_powered_emulated_remote(void)
691 {
692         struct test_data *data = tester_get_data();
693         struct bthost *bthost;
694
695         tester_print("Controller powered on");
696
697         bthost = hciemu_client_get_host(data->hciemu);
698         bthost_set_cmd_complete_cb(bthost, emu_connectable_complete, data);
699
700         if (data->hciemu_type == HCIEMU_TYPE_LE)
701                 bthost_set_adv_enable(bthost, 0x01);
702         else
703                 bthost_write_scan_enable(bthost, 0x03);
704 }
705
706 static void enable_success_cb(bt_state_t state)
707 {
708         struct test_data *data = tester_get_data();
709
710         if (state == BT_STATE_ON) {
711                 setup_powered_emulated_remote();
712                 data->cb_count--;
713                 check_cb_count();
714         }
715 }
716
717 static void disable_success_cb(bt_state_t state)
718 {
719         struct test_data *data = tester_get_data();
720
721         if (state == BT_STATE_OFF) {
722                 data->cb_count--;
723                 check_cb_count();
724         }
725 }
726
727 static gboolean adapter_state_changed(gpointer user_data)
728 {
729         struct test_data *data = tester_get_data();
730         const struct generic_data *test = data->test_data;
731         struct bt_cb_data *cb_data = user_data;
732
733         if (data->test_init_done &&
734                         test->expected_hal_cb.adapter_state_changed_cb) {
735                 test->expected_hal_cb.adapter_state_changed_cb(cb_data->state);
736                 goto cleanup;
737         }
738
739         if (!data->test_init_done && cb_data->state == BT_STATE_ON)
740                 setup_powered_emulated_remote();
741
742 cleanup:
743         g_free(cb_data);
744
745         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
746         return FALSE;
747 }
748
749 static void adapter_state_changed_cb(bt_state_t state)
750 {
751         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
752
753         cb_data->state = state;
754
755         g_atomic_int_inc(&scheduled_cbacks_num);
756         g_idle_add(adapter_state_changed, cb_data);
757 }
758
759 static void discovery_start_success_cb(bt_discovery_state_t state)
760 {
761         struct test_data *data = tester_get_data();
762
763         if (state == BT_DISCOVERY_STARTED) {
764                 data->cb_count--;
765                 check_cb_count();
766         }
767 }
768
769 static void discovery_start_done_cb(bt_discovery_state_t state)
770 {
771         struct test_data *data = tester_get_data();
772         bt_status_t status;
773
774         status = data->if_bluetooth->start_discovery();
775         data->cb_count--;
776
777         check_cb_count();
778         check_expected_status(status);
779 }
780
781 static void discovery_stop_success_cb(bt_discovery_state_t state)
782 {
783         struct test_data *data = tester_get_data();
784         bt_status_t status;
785
786         if (state == BT_DISCOVERY_STARTED && data->cb_count == 2) {
787                 status = data->if_bluetooth->cancel_discovery();
788                 check_expected_status(status);
789                 data->cb_count--;
790                 return;
791         }
792         if (state == BT_DISCOVERY_STOPPED && data->cb_count == 1) {
793                 data->cb_count--;
794                 check_cb_count();
795         }
796 }
797
798 static void discovery_device_found_state_changed_cb(bt_discovery_state_t state)
799 {
800         struct test_data *data = tester_get_data();
801
802         if (state == BT_DISCOVERY_STARTED && data->cb_count == 3) {
803                 data->cb_count--;
804                 return;
805         }
806         if (state == BT_DISCOVERY_STOPPED && data->cb_count == 1) {
807                 data->cb_count--;
808                 check_cb_count();
809         }
810 }
811
812 static void remote_discovery_state_changed_cb(bt_discovery_state_t state)
813 {
814         struct test_data *data = tester_get_data();
815
816         if (state == BT_DISCOVERY_STARTED && data->cb_count == 3) {
817                 data->cb_count--;
818                 return;
819         }
820         if (state == BT_DISCOVERY_STOPPED && data->cb_count == 1) {
821                 data->cb_count--;
822                 check_cb_count();
823         }
824 }
825
826 static void remote_setprop_disc_state_changed_cb(bt_discovery_state_t state)
827 {
828         struct test_data *data = tester_get_data();
829
830         if (state == BT_DISCOVERY_STARTED && data->cb_count == 4) {
831                 data->cb_count--;
832                 return;
833         }
834         if (state == BT_DISCOVERY_STOPPED) {
835                 data->cb_count--;
836                 check_cb_count();
837         }
838 }
839
840 static gboolean discovery_state_changed(gpointer user_data)
841 {
842         struct test_data *data = tester_get_data();
843         const struct generic_data *test = data->test_data;
844         struct bt_cb_data *cb_data = user_data;
845
846         if (test && test->expected_hal_cb.discovery_state_changed_cb)
847                 test->expected_hal_cb.discovery_state_changed_cb(
848                                                                 cb_data->state);
849
850         g_free(cb_data);
851
852         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
853         return FALSE;
854 }
855
856 static void discovery_state_changed_cb(bt_discovery_state_t state)
857 {
858         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
859
860         cb_data->state = state;
861         g_atomic_int_inc(&scheduled_cbacks_num);
862         g_idle_add(discovery_state_changed, cb_data);
863 }
864
865 static bt_property_t *copy_properties(int num_properties,
866                                                 bt_property_t *properties)
867 {
868         int i;
869         bt_property_t *props = g_new0(bt_property_t, num_properties);
870
871         for (i = 0; i < num_properties; i++) {
872                 props[i].type = properties[i].type;
873                 props[i].len = properties[i].len;
874                 props[i].val = g_memdup(properties[i].val, properties[i].len);
875         }
876
877         return props;
878 }
879
880 static void free_properties(int num_properties, bt_property_t *properties)
881 {
882         int i;
883
884         for (i = 0; i < num_properties; i++)
885                 g_free(properties[i].val);
886
887         g_free(properties);
888 }
889
890 static void discovery_device_found_cb(int num_properties,
891                                                 bt_property_t *properties)
892 {
893         struct test_data *data = tester_get_data();
894         uint8_t *remote_bdaddr =
895                         (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
896         uint32_t emu_remote_type = BT_DEVICE_DEVTYPE_BREDR;
897         int32_t emu_remote_rssi = -60;
898         bt_bdaddr_t emu_remote_bdaddr;
899         int i;
900         bt_property_t expected_prop;
901         bt_property_t received_prop;
902
903         data->cb_count--;
904         check_cb_count();
905
906         if (num_properties < 1) {
907                 tester_test_failed();
908                 return;
909         }
910
911         bdaddr2android((const bdaddr_t *) remote_bdaddr, &emu_remote_bdaddr);
912
913         for (i = 0; i < num_properties; i++) {
914                 received_prop = properties[i];
915
916                 switch (properties[i].type) {
917                 case BT_PROPERTY_BDADDR:
918                         expected_prop.type = BT_PROPERTY_BDADDR;
919                         expected_prop.len = sizeof(emu_remote_bdaddr);
920                         expected_prop.val = &emu_remote_bdaddr;
921                         break;
922
923                 case BT_PROPERTY_TYPE_OF_DEVICE:
924                         expected_prop.type = BT_PROPERTY_TYPE_OF_DEVICE;
925                         expected_prop.len = sizeof(emu_remote_type);
926                         expected_prop.val = &emu_remote_type;
927                         break;
928
929                 case BT_PROPERTY_REMOTE_RSSI:
930                         expected_prop.type = BT_PROPERTY_REMOTE_RSSI;
931                         expected_prop.len = sizeof(emu_remote_rssi);
932                         expected_prop.val = &emu_remote_rssi;
933                         break;
934
935                 default:
936                         expected_prop.type = 0;
937                         expected_prop.len = 0;
938                         expected_prop.val = NULL;
939                         break;
940                 }
941
942                 if (!check_test_property(received_prop, expected_prop)) {
943                         tester_test_failed();
944                         return;
945                 }
946         }
947 }
948
949 static void remote_getprops_device_found_cb(int num_properties,
950                                                 bt_property_t *properties)
951 {
952         struct test_data *data = tester_get_data();
953         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
954         bt_bdaddr_t remote_addr;
955
956         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
957
958         if (data->cb_count == 2)
959                 data->cb_count--;
960
961         data->if_bluetooth->get_remote_device_properties(&remote_addr);
962 }
963
964 static void remote_get_property_device_found_cb(int num_properties,
965                                                 bt_property_t *properties)
966 {
967         struct test_data *data = tester_get_data();
968         const struct generic_data *test = data->test_data;
969         bt_status_t status;
970         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
971         bt_bdaddr_t remote_addr;
972
973         const bt_property_t prop = test->expected_properties[0].prop;
974
975         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
976
977         if (data->cb_count == 2)
978                 data->cb_count--;
979
980         status = data->if_bluetooth->get_remote_device_property(&remote_addr,
981                                                                 prop.type);
982         check_expected_status(status);
983 }
984
985 static void remote_setprop_device_found_cb(int num_properties,
986                                                 bt_property_t *properties)
987 {
988         struct test_data *data = tester_get_data();
989         const struct generic_data *test = data->test_data;
990         bt_status_t status;
991         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
992         bt_bdaddr_t remote_addr;
993
994         const bt_property_t prop = test->expected_properties[0].prop;
995
996         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
997
998         if (data->cb_count == 3)
999                 data->cb_count--;
1000
1001         status = data->if_bluetooth->set_remote_device_property(&remote_addr,
1002                                                                         &prop);
1003         check_expected_status(status);
1004 }
1005
1006 static void remote_setprop_fail_device_found_cb(int num_properties,
1007                                                 bt_property_t *properties)
1008 {
1009         struct test_data *data = tester_get_data();
1010         const struct generic_data *test = data->test_data;
1011         bt_status_t status;
1012         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
1013         bt_bdaddr_t remote_addr;
1014
1015         const bt_property_t prop = test->expected_properties[0].prop;
1016
1017         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
1018
1019         if (data->cb_count == 2)
1020                 data->cb_count--;
1021
1022         status = data->if_bluetooth->set_remote_device_property(&remote_addr,
1023                                                                         &prop);
1024         check_expected_status(status);
1025 }
1026
1027 static void bond_device_found_cb(int num_properties, bt_property_t *properties)
1028 {
1029         struct test_data *data = tester_get_data();
1030         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
1031         bt_bdaddr_t remote_addr;
1032         bt_status_t status;
1033
1034         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
1035
1036         if (data->cb_count == 4) {
1037                 data->cb_count--;
1038                 status = data->if_bluetooth->create_bond(&remote_addr);
1039                 check_expected_status(status);
1040         }
1041 }
1042
1043 static void bond_nostatus_device_found_cb(int num_properties,
1044                                                 bt_property_t *properties)
1045 {
1046         struct test_data *data = tester_get_data();
1047         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
1048         bt_bdaddr_t remote_addr;
1049
1050         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
1051
1052         if (data->cb_count == 4) {
1053                 data->cb_count--;
1054                 data->if_bluetooth->create_bond(&remote_addr);
1055         }
1056 }
1057
1058 static gboolean device_found(gpointer user_data)
1059 {
1060         struct test_data *data = tester_get_data();
1061         const struct generic_data *test = data->test_data;
1062         struct bt_cb_data *cb_data = user_data;
1063
1064         if (data->test_init_done && test->expected_hal_cb.device_found_cb)
1065                 test->expected_hal_cb.device_found_cb(cb_data->num,
1066                                                                 cb_data->props);
1067
1068         free_properties(cb_data->num, cb_data->props);
1069         g_free(cb_data);
1070
1071         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
1072         return FALSE;
1073 }
1074
1075 static void device_found_cb(int num_properties, bt_property_t *properties)
1076 {
1077         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
1078
1079         cb_data->num = num_properties;
1080         cb_data->props = copy_properties(num_properties, properties);
1081
1082         g_atomic_int_inc(&scheduled_cbacks_num);
1083         g_idle_add(device_found, cb_data);
1084 }
1085
1086 static void check_count_properties_cb(bt_status_t status, int num_properties,
1087                                                 bt_property_t *properties)
1088 {
1089         int i;
1090
1091         for (i = 0; i < num_properties; i++)
1092                 check_expected_property(properties[i]);
1093 }
1094
1095 static gboolean adapter_properties(gpointer user_data)
1096 {
1097         struct test_data *data = tester_get_data();
1098         const struct generic_data *test = data->test_data;
1099         struct bt_cb_data *cb_data = user_data;
1100
1101         if (data->test_init_done && test->expected_hal_cb.adapter_properties_cb)
1102                 test->expected_hal_cb.adapter_properties_cb(cb_data->status,
1103                                                 cb_data->num, cb_data->props);
1104
1105         free_properties(cb_data->num, cb_data->props);
1106         g_free(cb_data);
1107
1108         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
1109         return FALSE;
1110 }
1111
1112 static void adapter_properties_cb(bt_status_t status, int num_properties,
1113                                                 bt_property_t *properties)
1114 {
1115         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
1116
1117         cb_data->status = status;
1118         cb_data->num = num_properties;
1119         cb_data->props = copy_properties(num_properties, properties);
1120
1121         g_atomic_int_inc(&scheduled_cbacks_num);
1122         g_idle_add(adapter_properties, cb_data);
1123 }
1124
1125 static void remote_test_device_properties_cb(bt_status_t status,
1126                                 bt_bdaddr_t *bd_addr, int num_properties,
1127                                 bt_property_t *properties)
1128 {
1129         int i;
1130
1131         for (i = 0; i < num_properties; i++)
1132                 check_expected_property(properties[i]);
1133 }
1134
1135 static void remote_setprop_device_properties_cb(bt_status_t status,
1136                                 bt_bdaddr_t *bd_addr, int num_properties,
1137                                 bt_property_t *properties)
1138 {
1139         int i;
1140         struct test_data *data = tester_get_data();
1141         const struct generic_data *test = data->test_data;
1142         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
1143         bt_bdaddr_t remote_addr;
1144         const bt_property_t prop = test->expected_properties[1].prop;
1145
1146         for (i = 0; i < num_properties; i++)
1147                 check_expected_property(properties[i]);
1148
1149         if (g_slist_length(data->expected_properties_list) == 1) {
1150                 bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
1151                 data->cb_count--;
1152                 check_cb_count();
1153                 data->if_bluetooth->get_remote_device_property(&remote_addr,
1154                                                                 prop.type);
1155         }
1156 }
1157
1158 static gboolean remote_device_properties(gpointer user_data)
1159 {
1160         struct test_data *data = tester_get_data();
1161         const struct generic_data *test = data->test_data;
1162         struct bt_cb_data *cb_data = user_data;
1163
1164         if (data->test_init_done &&
1165                         test->expected_hal_cb.remote_device_properties_cb)
1166                 test->expected_hal_cb.remote_device_properties_cb(
1167                                         cb_data->status, &cb_data->bdaddr,
1168                                         cb_data->num, cb_data->props);
1169
1170         free_properties(cb_data->num, cb_data->props);
1171         g_free(cb_data);
1172
1173         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
1174         return FALSE;
1175 }
1176
1177 static void remote_device_properties_cb(bt_status_t status,
1178                                 bt_bdaddr_t *bd_addr, int num_properties,
1179                                 bt_property_t *properties)
1180 {
1181         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
1182
1183         cb_data->status = status;
1184         cb_data->bdaddr = *bd_addr;
1185         cb_data->num = num_properties;
1186         cb_data->props = copy_properties(num_properties, properties);
1187
1188         g_atomic_int_inc(&scheduled_cbacks_num);
1189         g_idle_add(remote_device_properties, cb_data);
1190 }
1191
1192 static void bond_test_bonded_state_changed_cb(bt_status_t status,
1193                         bt_bdaddr_t *remote_bd_addr, bt_bond_state_t state)
1194 {
1195         struct test_data *data = tester_get_data();
1196
1197         switch (state) {
1198         case BT_BOND_STATE_BONDING:
1199                 data->cb_count--;
1200                 break;
1201         case BT_BOND_STATE_BONDED:
1202                 data->cb_count--;
1203                 check_cb_count();
1204                 break;
1205         default:
1206                 tester_test_failed();
1207                 break;
1208         }
1209 }
1210
1211 static void bond_test_none_state_changed_cb(bt_status_t status,
1212                         bt_bdaddr_t *remote_bd_addr, bt_bond_state_t state)
1213 {
1214         struct test_data *data = tester_get_data();
1215
1216         switch (state) {
1217         case BT_BOND_STATE_BONDING:
1218                 data->cb_count--;
1219                 break;
1220         case BT_BOND_STATE_NONE:
1221                 data->cb_count--;
1222                 check_cb_count();
1223                 break;
1224         default:
1225                 tester_test_failed();
1226                 break;
1227         }
1228 }
1229
1230 static void bond_remove_success_state_changed_cb(bt_status_t status,
1231                         bt_bdaddr_t *remote_bd_addr, bt_bond_state_t state)
1232 {
1233         struct test_data *data = tester_get_data();
1234         bt_status_t remove_status;
1235         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
1236         bt_bdaddr_t remote_addr;
1237
1238         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
1239
1240         if (state == BT_BOND_STATE_BONDED) {
1241                 data->cb_count--;
1242                 remove_status = data->if_bluetooth->remove_bond(&remote_addr);
1243                 check_expected_status(remove_status);
1244                 return;
1245         }
1246
1247         if (state == BT_BOND_STATE_NONE) {
1248                 data->cb_count--;
1249                 check_cb_count();
1250         }
1251 }
1252
1253 static gboolean bond_state_changed(gpointer user_data)
1254 {
1255         struct test_data *data = tester_get_data();
1256         const struct generic_data *test = data->test_data;
1257         struct bt_cb_data *cb_data = user_data;
1258
1259         if (data->test_init_done && test->expected_hal_cb.bond_state_changed_cb)
1260                 test->expected_hal_cb.bond_state_changed_cb(cb_data->status,
1261                                         &cb_data->bdaddr, cb_data->state);
1262
1263         g_free(cb_data);
1264         return FALSE;
1265 }
1266
1267 static void bond_state_changed_cb(bt_status_t status,
1268                         bt_bdaddr_t *remote_bd_addr, bt_bond_state_t state)
1269 {
1270         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
1271
1272         cb_data->status = status;
1273         cb_data->bdaddr = *remote_bd_addr;
1274         cb_data->state = state;
1275
1276         g_idle_add(bond_state_changed, cb_data);
1277 }
1278
1279 static void bond_create_pin_success_request_cb(bt_bdaddr_t *remote_bd_addr,
1280                                         bt_bdname_t *bd_name, uint32_t cod)
1281 {
1282         struct test_data *data = tester_get_data();
1283         const bt_bdaddr_t *bdaddr = remote_bd_addr;
1284         bt_pin_code_t pin_code = {
1285         .pin = { 0x30, 0x30, 0x30, 0x30 },
1286         };
1287         uint8_t pin_len = 4;
1288
1289         data->cb_count--;
1290
1291         data->if_bluetooth->pin_reply(bdaddr, TRUE, pin_len, &pin_code);
1292 }
1293
1294 static void bond_create_pin_fail_request_cb(bt_bdaddr_t *remote_bd_addr,
1295                                         bt_bdname_t *bd_name, uint32_t cod)
1296 {
1297         struct test_data *data = tester_get_data();
1298         const bt_bdaddr_t *bdaddr = remote_bd_addr;
1299         bt_pin_code_t pin_code = {
1300         .pin = { 0x31, 0x31, 0x31, 0x31 },
1301         };
1302         uint8_t pin_len = 4;
1303
1304         data->cb_count--;
1305
1306         data->if_bluetooth->pin_reply(bdaddr, TRUE, pin_len, &pin_code);
1307 }
1308
1309 static gboolean pin_request(gpointer user_data)
1310 {
1311         struct test_data *data = tester_get_data();
1312         const struct generic_data *test = data->test_data;
1313         struct bt_cb_data *cb_data = user_data;
1314
1315         if (data->test_init_done && test->expected_hal_cb.pin_request_cb)
1316                 test->expected_hal_cb.pin_request_cb(&cb_data->bdaddr,
1317                                                 &cb_data->bdname, cb_data->cod);
1318
1319         g_free(cb_data);
1320         return FALSE;
1321 }
1322
1323 static void pin_request_cb(bt_bdaddr_t *remote_bd_addr,
1324                                         bt_bdname_t *bd_name, uint32_t cod)
1325 {
1326         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
1327
1328         cb_data->bdaddr = *remote_bd_addr;
1329         cb_data->bdname = *bd_name;
1330         cb_data->cod = cod;
1331
1332         g_idle_add(pin_request, cb_data);
1333 }
1334
1335 static void bond_create_ssp_request_cb(const bt_bdaddr_t *remote_bd_addr,
1336                                         bt_ssp_variant_t pairing_variant,
1337                                         bool accept, uint32_t pass_key)
1338 {
1339         struct test_data *data = tester_get_data();
1340
1341         data->if_bluetooth->ssp_reply(remote_bd_addr,
1342                                         BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
1343                                         accept, pass_key);
1344
1345         data->cb_count--;
1346 }
1347
1348 static void bond_create_ssp_success_request_cb(bt_bdaddr_t *remote_bd_addr,
1349                                         bt_bdname_t *bd_name, uint32_t cod,
1350                                         bt_ssp_variant_t pairing_variant,
1351                                         uint32_t pass_key)
1352 {
1353         bool accept = true;
1354
1355         bond_create_ssp_request_cb(remote_bd_addr, pairing_variant, accept,
1356                                                                 pass_key);
1357 }
1358
1359 static void bond_create_ssp_fail_request_cb(bt_bdaddr_t *remote_bd_addr,
1360                                         bt_bdname_t *bd_name, uint32_t cod,
1361                                         bt_ssp_variant_t pairing_variant,
1362                                         uint32_t pass_key)
1363 {
1364         bool accept = false;
1365
1366         bond_create_ssp_request_cb(remote_bd_addr, pairing_variant, accept,
1367                                                                 pass_key);
1368 }
1369
1370 static void bond_cancel_success_ssp_request_cb(bt_bdaddr_t *remote_bd_addr,
1371                                         bt_bdname_t *bd_name, uint32_t cod,
1372                                         bt_ssp_variant_t pairing_variant,
1373                                         uint32_t pass_key)
1374 {
1375         struct test_data *data = tester_get_data();
1376         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
1377         bt_bdaddr_t remote_addr;
1378         bt_status_t status;
1379
1380         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
1381
1382         data->cb_count--;
1383
1384         status = data->if_bluetooth->cancel_bond(&remote_addr);
1385         check_expected_status(status);
1386 }
1387
1388 static gboolean ssp_request(gpointer user_data)
1389 {
1390         struct test_data *data = tester_get_data();
1391         const struct generic_data *test = data->test_data;
1392         struct bt_cb_data *cb_data = user_data;
1393
1394         if (data->test_init_done &&
1395                                 test->expected_hal_cb.ssp_request_cb)
1396                 test->expected_hal_cb.ssp_request_cb(&cb_data->bdaddr,
1397                                         &cb_data->bdname, cb_data->cod,
1398                                         cb_data->ssp_variant, cb_data->passkey);
1399
1400         g_free(cb_data);
1401         return FALSE;
1402 }
1403
1404 static void ssp_request_cb(bt_bdaddr_t *remote_bd_addr, bt_bdname_t *bd_name,
1405                                 uint32_t cod, bt_ssp_variant_t pairing_variant,
1406                                 uint32_t pass_key)
1407 {
1408         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
1409
1410         cb_data->bdaddr = *remote_bd_addr;
1411         cb_data->bdname = *bd_name;
1412         cb_data->cod = cod;
1413         cb_data->ssp_variant = pairing_variant;
1414         cb_data->passkey = pass_key;
1415
1416         g_idle_add(ssp_request, cb_data);
1417 }
1418
1419 static bt_bdaddr_t enable_done_bdaddr_val = { {0x00} };
1420 static const char enable_done_bdname_val[] = "BlueZ for Android";
1421 static bt_uuid_t enable_done_uuids_val = {
1422         .uu = { 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
1423                                         0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb},
1424 };
1425 static bt_device_type_t enable_done_tod_val = BT_DEVICE_DEVTYPE_BREDR;
1426 static bt_scan_mode_t enable_done_scanmode_val = BT_SCAN_MODE_NONE;
1427 static uint32_t enable_done_disctimeout_val = 120;
1428
1429 static struct priority_property enable_done_props[] = {
1430         {
1431         .prop.type = BT_PROPERTY_BDADDR,
1432         .prop.len = sizeof(enable_done_bdaddr_val),
1433         .prop.val = &enable_done_bdaddr_val,
1434         .prio = 1,
1435         },
1436         {
1437         .prop.type = BT_PROPERTY_BDNAME,
1438         .prop.len = sizeof(enable_done_bdname_val) - 1,
1439         .prop.val = &enable_done_bdname_val,
1440         .prio = 2,
1441         },
1442         {
1443         .prop.type = BT_PROPERTY_UUIDS,
1444         .prop.len = sizeof(enable_done_uuids_val),
1445         .prop.val = &enable_done_uuids_val,
1446         .prio = 3,
1447         },
1448         {
1449         .prop.type = BT_PROPERTY_CLASS_OF_DEVICE,
1450         .prop.len = sizeof(uint32_t),
1451         .prop.val = NULL,
1452         .prio = 4,
1453         },
1454         {
1455         .prop.type = BT_PROPERTY_TYPE_OF_DEVICE,
1456         .prop.len = sizeof(enable_done_tod_val),
1457         .prop.val = &enable_done_tod_val,
1458         .prio = 5,
1459         },
1460         {
1461         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
1462         .prop.len = sizeof(enable_done_scanmode_val),
1463         .prop.val = &enable_done_scanmode_val,
1464         .prio = 6,
1465         },
1466         {
1467         .prop.type = BT_PROPERTY_ADAPTER_BONDED_DEVICES,
1468         .prop.len = 0,
1469         .prop.val = NULL,
1470         .prio = 7,
1471         },
1472         {
1473         .prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
1474         .prop.len = sizeof(enable_done_disctimeout_val),
1475         .prop.val = &enable_done_disctimeout_val,
1476         .prio = 8,
1477         },
1478 };
1479
1480 static const struct generic_data bluetooth_enable_success_test = {
1481         .expected_hal_cb.adapter_state_changed_cb = enable_success_cb,
1482         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1483         .expected_cb_count = 1,
1484         .expected_properties_num = 8,
1485         .expected_properties = enable_done_props,
1486         .expected_adapter_status = BT_STATUS_SUCCESS,
1487 };
1488
1489 static const struct generic_data bluetooth_enable_success2_test = {
1490         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1491         .expected_adapter_status = BT_STATUS_SUCCESS,
1492         .expected_properties_num = 8,
1493         .expected_properties = enable_done_props,
1494 };
1495
1496 static const struct generic_data bluetooth_disable_success_test = {
1497         .expected_hal_cb.adapter_state_changed_cb = disable_success_cb,
1498         .expected_cb_count = 1,
1499         .expected_adapter_status = BT_STATUS_SUCCESS,
1500 };
1501
1502 static char test_set_bdname[] = "test_bdname_set";
1503
1504 static struct priority_property setprop_bdname_props[] = {
1505         {
1506         .prop.type = BT_PROPERTY_BDNAME,
1507         .prop.val = test_set_bdname,
1508         .prop.len = sizeof(test_set_bdname) - 1,
1509         .prio = 0,
1510         },
1511 };
1512
1513 static const struct generic_data bluetooth_setprop_bdname_success_test = {
1514         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1515         .expected_properties_num = 1,
1516         .expected_properties = setprop_bdname_props,
1517 };
1518
1519 static bt_scan_mode_t test_setprop_scanmode_val =
1520                                         BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE;
1521
1522 static struct priority_property setprop_scanmode_props[] = {
1523         {
1524         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
1525         .prop.val = &test_setprop_scanmode_val,
1526         .prop.len = sizeof(bt_scan_mode_t),
1527         },
1528 };
1529
1530 static const struct generic_data bluetooth_setprop_scanmode_success_test = {
1531         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1532         .expected_properties_num = 1,
1533         .expected_properties = setprop_scanmode_props,
1534         .expected_adapter_status = BT_STATUS_SUCCESS,
1535 };
1536
1537 static uint32_t test_setprop_disctimeout_val = 120;
1538
1539 static struct priority_property setprop_disctimeout_props[] = {
1540         {
1541         .prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
1542         .prop.val = &test_setprop_disctimeout_val,
1543         .prop.len = sizeof(test_setprop_disctimeout_val),
1544         },
1545 };
1546
1547 static const struct generic_data bluetooth_setprop_disctimeout_success_test = {
1548         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1549         .expected_properties_num = 1,
1550         .expected_properties = setprop_disctimeout_props,
1551         .expected_adapter_status = BT_STATUS_SUCCESS,
1552 };
1553
1554 static bt_bdaddr_t test_getprop_bdaddr_val = { {0x00} };
1555
1556 static struct priority_property getprop_bdaddr_props[] = {
1557         {
1558         .prop.type = BT_PROPERTY_BDADDR,
1559         .prop.val = &test_getprop_bdaddr_val,
1560         .prop.len = sizeof(test_getprop_bdaddr_val),
1561         },
1562 };
1563
1564 static const struct generic_data bluetooth_getprop_bdaddr_success_test = {
1565         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1566         .expected_properties_num = 1,
1567         .expected_properties = getprop_bdaddr_props,
1568         .expected_adapter_status = BT_STATUS_SUCCESS,
1569 };
1570
1571 static const char test_bdname[] = "test_bdname_setget";
1572
1573 static struct priority_property getprop_bdname_props[] = {
1574         {
1575         .prop.type = BT_PROPERTY_BDNAME,
1576         .prop.val = &test_bdname,
1577         .prop.len = sizeof(test_bdname) - 1,
1578         },
1579 };
1580
1581 static const struct generic_data bluetooth_getprop_bdname_success_test = {
1582         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1583         .expected_properties_num = 1,
1584         .expected_properties = getprop_bdname_props,
1585         .expected_adapter_status = BT_STATUS_SUCCESS,
1586 };
1587
1588 static unsigned char setprop_uuids[] = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00,
1589                         0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
1590                         0x00, 0x00 };
1591
1592 static struct priority_property setprop_uuid_prop[] = {
1593         {
1594         .prop.type = BT_PROPERTY_UUIDS,
1595         .prop.val = &setprop_uuids,
1596         .prop.len = sizeof(setprop_uuids),
1597         },
1598 };
1599
1600 static const struct generic_data bluetooth_setprop_uuid_invalid_test = {
1601         .expected_adapter_status = BT_STATUS_FAIL,
1602 };
1603
1604 static uint32_t setprop_class_of_device = 0;
1605
1606 static struct priority_property setprop_cod_props[] = {
1607         {
1608         .prop.type = BT_PROPERTY_CLASS_OF_DEVICE,
1609         .prop.val = &setprop_class_of_device,
1610         .prop.len = sizeof(setprop_class_of_device),
1611         },
1612 };
1613
1614 static const struct generic_data bluetooth_setprop_cod_invalid_test = {
1615         .expected_adapter_status = BT_STATUS_FAIL,
1616 };
1617
1618 static bt_device_type_t setprop_type_of_device = BT_DEVICE_DEVTYPE_BREDR;
1619
1620 static struct priority_property setprop_tod_props[] = {
1621         {
1622         .prop.type = BT_PROPERTY_TYPE_OF_DEVICE,
1623         .prop.val = &setprop_type_of_device,
1624         .prop.len = sizeof(setprop_type_of_device),
1625         },
1626 };
1627
1628 static const struct generic_data bluetooth_setprop_tod_invalid_test = {
1629         .expected_adapter_status = BT_STATUS_FAIL,
1630 };
1631
1632 static int32_t setprop_remote_rssi = 0;
1633
1634 static struct priority_property setprop_remote_rssi_props[] = {
1635         {
1636         .prop.type = BT_PROPERTY_REMOTE_RSSI,
1637         .prop.val = &setprop_remote_rssi,
1638         .prop.len = sizeof(setprop_remote_rssi),
1639         },
1640 };
1641
1642 static const struct generic_data bluetooth_setprop_remote_rssi_invalid_test = {
1643         .expected_adapter_status = BT_STATUS_FAIL,
1644 };
1645
1646 static bt_service_record_t setprop_remote_service = {
1647         .uuid = { {0x00} },
1648         .channel = 12,
1649         .name = "bt_name",
1650 };
1651
1652 static struct priority_property setprop_service_record_props[] = {
1653         {
1654         .prop.type = BT_PROPERTY_SERVICE_RECORD,
1655         .prop.val = &setprop_remote_service,
1656         .prop.len = sizeof(setprop_remote_service),
1657         },
1658 };
1659
1660 static const struct generic_data
1661                         bluetooth_setprop_service_record_invalid_test = {
1662         .expected_adapter_status = BT_STATUS_FAIL,
1663 };
1664
1665 static bt_bdaddr_t setprop_bdaddr = {
1666         .address = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
1667 };
1668
1669 static struct priority_property setprop_bdaddr_props[] = {
1670         {
1671         .prop.type = BT_PROPERTY_BDADDR,
1672         .prop.val = &setprop_bdaddr,
1673         .prop.len = sizeof(setprop_bdaddr),
1674         },
1675 };
1676
1677 static const struct generic_data bluetooth_setprop_bdaddr_invalid_test = {
1678         .expected_adapter_status = BT_STATUS_FAIL,
1679 };
1680
1681 static bt_scan_mode_t setprop_scanmode_connectable = BT_SCAN_MODE_CONNECTABLE;
1682
1683 static struct priority_property setprop_scanmode_connectable_props[] = {
1684         {
1685         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
1686         .prop.val = &setprop_scanmode_connectable,
1687         .prop.len = sizeof(setprop_scanmode_connectable),
1688         },
1689 };
1690
1691 static const struct generic_data
1692                         bluetooth_setprop_scanmode_connectable_success_test = {
1693         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1694         .expected_properties_num = 1,
1695         .expected_properties = setprop_scanmode_connectable_props,
1696         .expected_adapter_status = BT_STATUS_SUCCESS,
1697 };
1698
1699 static bt_bdaddr_t setprop_bonded_devices = {
1700         .address = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 },
1701 };
1702
1703 static struct priority_property setprop_bonded_devices_props[] = {
1704         {
1705         .prop.type = BT_PROPERTY_ADAPTER_BONDED_DEVICES,
1706         .prop.val = &setprop_bonded_devices,
1707         .prop.len = sizeof(setprop_bonded_devices),
1708         },
1709 };
1710
1711 static const struct generic_data
1712                         bluetooth_setprop_bonded_devices_invalid_test = {
1713         .expected_adapter_status = BT_STATUS_FAIL,
1714 };
1715
1716 static uint32_t getprop_cod = 0x00020c;
1717
1718 static struct priority_property getprop_cod_props[] = {
1719         {
1720         .prop.type = BT_PROPERTY_CLASS_OF_DEVICE,
1721         .prop.val = &getprop_cod,
1722         .prop.len = sizeof(getprop_cod),
1723         },
1724 };
1725
1726 static const struct generic_data bluetooth_getprop_cod_success_test = {
1727         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1728         .expected_properties_num = 1,
1729         .expected_properties = getprop_cod_props,
1730         .expected_adapter_status = BT_STATUS_SUCCESS,
1731 };
1732
1733 static bt_device_type_t getprop_tod = BT_DEVICE_DEVTYPE_BREDR;
1734
1735 static struct priority_property getprop_tod_props[] = {
1736         {
1737         .prop.type = BT_PROPERTY_TYPE_OF_DEVICE,
1738         .prop.val = &getprop_tod,
1739         .prop.len = sizeof(getprop_tod),
1740         },
1741 };
1742
1743 static const struct generic_data bluetooth_getprop_tod_success_test = {
1744         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1745         .expected_properties_num = 1,
1746         .expected_properties = getprop_tod_props,
1747         .expected_adapter_status = BT_STATUS_SUCCESS,
1748 };
1749
1750 static bt_scan_mode_t getprop_scanmode = BT_SCAN_MODE_NONE;
1751
1752 static struct priority_property getprop_scanmode_props[] = {
1753         {
1754         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
1755         .prop.val = &getprop_scanmode,
1756         .prop.len = sizeof(getprop_scanmode),
1757         },
1758 };
1759
1760 static const struct generic_data bluetooth_getprop_scanmode_success_test = {
1761         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1762         .expected_properties_num = 1,
1763         .expected_properties = getprop_scanmode_props,
1764         .expected_adapter_status = BT_STATUS_SUCCESS,
1765 };
1766
1767 static uint32_t getprop_disctimeout_val = 120;
1768
1769 static struct priority_property getprop_disctimeout_props[] = {
1770         {
1771         .prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
1772         .prop.val = &getprop_disctimeout_val,
1773         .prop.len = sizeof(getprop_disctimeout_val),
1774         },
1775 };
1776
1777 static const struct generic_data bluetooth_getprop_disctimeout_success_test = {
1778         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1779         .expected_properties_num = 1,
1780         .expected_properties = getprop_disctimeout_props,
1781         .expected_adapter_status = BT_STATUS_SUCCESS,
1782 };
1783
1784 static bt_uuid_t getprop_uuids = {
1785         .uu = { 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
1786                                         0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB },
1787 };
1788
1789 static struct priority_property getprop_uuids_props[] = {
1790         {
1791         .prop.type = BT_PROPERTY_UUIDS,
1792         .prop.val = &getprop_uuids,
1793         .prop.len = sizeof(getprop_uuids),
1794         },
1795 };
1796
1797 static const struct generic_data bluetooth_getprop_uuids_success_test = {
1798         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1799         .expected_properties_num = 1,
1800         .expected_properties = getprop_uuids_props,
1801         .expected_adapter_status = BT_STATUS_SUCCESS,
1802 };
1803
1804 static struct priority_property getprop_bondeddev_props[] = {
1805         {
1806         .prop.type = BT_PROPERTY_ADAPTER_BONDED_DEVICES,
1807         .prop.val = NULL,
1808         .prop.len = 0,
1809         },
1810 };
1811
1812 static const struct generic_data bluetooth_getprop_bondeddev_success_test = {
1813         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1814         .expected_properties_num = 1,
1815         .expected_properties = getprop_bondeddev_props,
1816         .expected_adapter_status = BT_STATUS_SUCCESS,
1817 };
1818
1819 static bt_scan_mode_t setprop_scanmode_none = BT_SCAN_MODE_NONE;
1820
1821 static struct priority_property setprop_scanmode_none_props[] = {
1822         {
1823         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
1824         .prop.val = &setprop_scanmode_none,
1825         .prop.len = sizeof(setprop_scanmode_none),
1826         },
1827 };
1828
1829 static const struct generic_data
1830                         bluetooth_setprop_scanmode_none_success2_test = {
1831         .expected_hal_cb.adapter_properties_cb = check_count_properties_cb,
1832         .expected_properties_num = 1,
1833         .expected_properties = setprop_scanmode_none_props,
1834         .expected_adapter_status = BT_STATUS_SUCCESS,
1835 };
1836
1837 static const struct generic_data bluetooth_discovery_start_success_test = {
1838         .expected_hal_cb.discovery_state_changed_cb =
1839                                                 discovery_start_success_cb,
1840         .expected_cb_count = 1,
1841         .expected_adapter_status = BT_STATUS_SUCCESS,
1842 };
1843
1844 static const struct generic_data bluetooth_discovery_start_success2_test = {
1845         .expected_hal_cb.discovery_state_changed_cb = discovery_start_done_cb,
1846         .expected_cb_count = 1,
1847         .expected_adapter_status = BT_STATUS_SUCCESS,
1848 };
1849
1850 static const struct generic_data bluetooth_discovery_stop_success2_test = {
1851         .expected_adapter_status = BT_STATUS_SUCCESS,
1852 };
1853
1854 static const struct generic_data bluetooth_discovery_stop_success_test = {
1855         .expected_hal_cb.discovery_state_changed_cb = discovery_stop_success_cb,
1856         .expected_cb_count = 2,
1857         .expected_adapter_status = BT_STATUS_SUCCESS,
1858 };
1859
1860 static const struct generic_data bluetooth_discovery_device_found_test = {
1861         .expected_hal_cb.discovery_state_changed_cb =
1862                                         discovery_device_found_state_changed_cb,
1863         .expected_hal_cb.device_found_cb = discovery_device_found_cb,
1864         .expected_cb_count = 3,
1865         .expected_adapter_status = BT_STATUS_NOT_EXPECTED,
1866 };
1867
1868 static const char remote_get_properties_bdname_val[] = "00:AA:01:01:00:00";
1869 static uint32_t remote_get_properties_cod_val = 0;
1870 static bt_device_type_t remote_get_properties_tod_val = BT_DEVICE_DEVTYPE_BREDR;
1871 static int32_t remote_get_properties_rssi_val = -60;
1872
1873 static struct priority_property remote_getprops_props[] = {
1874         {
1875         .prop.type = BT_PROPERTY_BDNAME,
1876         .prop.val = &remote_get_properties_bdname_val,
1877         .prop.len = sizeof(remote_get_properties_bdname_val) - 1,
1878         .prio = 1,
1879         },
1880         {
1881         .prop.type = BT_PROPERTY_UUIDS,
1882         .prop.val = NULL,
1883         .prop.len = 0,
1884         .prio = 2,
1885         },
1886         {
1887         .prop.type = BT_PROPERTY_CLASS_OF_DEVICE,
1888         .prop.val = &remote_get_properties_cod_val,
1889         .prop.len = sizeof(remote_get_properties_cod_val),
1890         .prio = 3,
1891         },
1892         {
1893         .prop.type = BT_PROPERTY_TYPE_OF_DEVICE,
1894         .prop.val = &remote_get_properties_tod_val,
1895         .prop.len = sizeof(remote_get_properties_tod_val),
1896         .prio = 4,
1897         },
1898         {
1899         .prop.type = BT_PROPERTY_REMOTE_RSSI,
1900         .prop.val = &remote_get_properties_rssi_val,
1901         .prop.len = sizeof(remote_get_properties_rssi_val),
1902         .prio = 5,
1903         },
1904         {
1905         .prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP,
1906         .prop.val = NULL,
1907         .prop.len = 4,
1908         .prio = 6,
1909         },
1910 };
1911
1912 static const struct generic_data bt_dev_getprops_success_test = {
1913         .expected_hal_cb.discovery_state_changed_cb =
1914                                         remote_discovery_state_changed_cb,
1915         .expected_hal_cb.device_found_cb = remote_getprops_device_found_cb,
1916         .expected_hal_cb.remote_device_properties_cb =
1917                                         remote_test_device_properties_cb,
1918         .expected_cb_count = 3,
1919         .expected_properties_num = 6,
1920         .expected_properties = remote_getprops_props,
1921         .expected_adapter_status = BT_STATUS_NOT_EXPECTED,
1922 };
1923
1924 static const char remote_getprop_bdname_val[] = "00:AA:01:01:00:00";
1925
1926 static struct priority_property remote_getprop_bdname_props[] = {
1927         {
1928         .prop.type = BT_PROPERTY_BDNAME,
1929         .prop.val = &remote_getprop_bdname_val,
1930         .prop.len = sizeof(remote_getprop_bdname_val) - 1,
1931         },
1932 };
1933
1934 static const struct generic_data bt_dev_getprop_bdname_success_test = {
1935         .expected_hal_cb.discovery_state_changed_cb =
1936                                         remote_discovery_state_changed_cb,
1937         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
1938         .expected_hal_cb.remote_device_properties_cb =
1939                                         remote_test_device_properties_cb,
1940         .expected_cb_count = 3,
1941         .expected_properties_num = 1,
1942         .expected_properties = remote_getprop_bdname_props,
1943         .expected_adapter_status = BT_STATUS_SUCCESS,
1944 };
1945
1946 static struct priority_property remote_getprop_uuids_props[] = {
1947         {
1948         .prop.type = BT_PROPERTY_UUIDS,
1949         .prop.val = NULL,
1950         .prop.len = 0,
1951         },
1952 };
1953
1954 static const struct generic_data bt_dev_getprop_uuids_success_test = {
1955         .expected_hal_cb.discovery_state_changed_cb =
1956                                         remote_discovery_state_changed_cb,
1957         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
1958         .expected_hal_cb.remote_device_properties_cb =
1959                                         remote_test_device_properties_cb,
1960         .expected_cb_count = 3,
1961         .expected_properties_num = 1,
1962         .expected_properties = remote_getprop_uuids_props,
1963         .expected_adapter_status = BT_STATUS_SUCCESS,
1964 };
1965
1966 static uint32_t remote_getprop_cod_val = 0;
1967
1968 static struct priority_property remote_getprop_cod_props[] = {
1969         {
1970         .prop.type = BT_PROPERTY_CLASS_OF_DEVICE,
1971         .prop.val = &remote_getprop_cod_val,
1972         .prop.len = sizeof(remote_getprop_cod_val),
1973         },
1974 };
1975
1976 static const struct generic_data bt_dev_getprop_cod_success_test = {
1977         .expected_hal_cb.discovery_state_changed_cb =
1978                                         remote_discovery_state_changed_cb,
1979         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
1980         .expected_hal_cb.remote_device_properties_cb =
1981                                         remote_test_device_properties_cb,
1982         .expected_cb_count = 3,
1983         .expected_properties_num = 1,
1984         .expected_properties = remote_getprop_cod_props,
1985         .expected_adapter_status = BT_STATUS_SUCCESS,
1986 };
1987
1988 static bt_device_type_t remote_getprop_tod_val = BT_DEVICE_DEVTYPE_BREDR;
1989
1990 static struct priority_property remote_getprop_tod_props[] = {
1991         {
1992         .prop.type = BT_PROPERTY_TYPE_OF_DEVICE,
1993         .prop.val = &remote_getprop_tod_val,
1994         .prop.len = sizeof(remote_getprop_tod_val),
1995         },
1996 };
1997
1998 static const struct generic_data bt_dev_getprop_tod_success_test = {
1999         .expected_hal_cb.discovery_state_changed_cb =
2000                                         remote_discovery_state_changed_cb,
2001         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2002         .expected_hal_cb.remote_device_properties_cb =
2003                                         remote_test_device_properties_cb,
2004         .expected_cb_count = 3,
2005         .expected_properties_num = 1,
2006         .expected_properties = remote_getprop_tod_props,
2007         .expected_adapter_status = BT_STATUS_SUCCESS,
2008 };
2009
2010 static int32_t remote_getprop_rssi_val = -60;
2011
2012 static struct priority_property remote_getprop_rssi_props[] = {
2013         {
2014         .prop.type = BT_PROPERTY_REMOTE_RSSI,
2015         .prop.val = &remote_getprop_rssi_val,
2016         .prop.len = sizeof(remote_getprop_rssi_val),
2017         },
2018 };
2019
2020 static const struct generic_data bt_dev_getprop_rssi_success_test = {
2021         .expected_hal_cb.discovery_state_changed_cb =
2022                                         remote_discovery_state_changed_cb,
2023         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2024         .expected_hal_cb.remote_device_properties_cb =
2025                                         remote_test_device_properties_cb,
2026         .expected_cb_count = 3,
2027         .expected_properties_num = 1,
2028         .expected_properties = remote_getprop_rssi_props,
2029         .expected_adapter_status = BT_STATUS_SUCCESS,
2030 };
2031
2032 static struct priority_property remote_getprop_timestamp_props[] = {
2033         {
2034         .prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP,
2035         .prop.val = NULL,
2036         .prop.len = 4,
2037         },
2038 };
2039
2040 static const struct generic_data bt_dev_getprop_timpestamp_success_test = {
2041         .expected_hal_cb.discovery_state_changed_cb =
2042                                         remote_discovery_state_changed_cb,
2043         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2044         .expected_hal_cb.remote_device_properties_cb =
2045                                         remote_test_device_properties_cb,
2046         .expected_cb_count = 3,
2047         .expected_properties_num = 1,
2048         .expected_properties = remote_getprop_timestamp_props,
2049         .expected_adapter_status = BT_STATUS_SUCCESS,
2050 };
2051
2052 static bt_bdaddr_t remote_getprop_bdaddr_val = {
2053         .address = { 0x00, 0xaa, 0x01, 0x00, 0x00, 0x00 }
2054 };
2055
2056 static struct priority_property remote_getprop_bdaddr_props[] = {
2057         {
2058         .prop.type = BT_PROPERTY_BDADDR,
2059         .prop.val = &remote_getprop_bdaddr_val,
2060         .prop.len = sizeof(remote_getprop_bdaddr_val),
2061         },
2062 };
2063
2064 static const struct generic_data bt_dev_getprop_bdaddr_fail_test = {
2065         .expected_hal_cb.discovery_state_changed_cb =
2066                                         remote_discovery_state_changed_cb,
2067         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2068         .expected_hal_cb.remote_device_properties_cb =
2069                                         remote_test_device_properties_cb,
2070         .expected_cb_count = 3,
2071         .expected_properties = remote_getprop_bdaddr_props,
2072         .expected_adapter_status = BT_STATUS_FAIL,
2073 };
2074
2075 static bt_service_record_t remote_getprop_servrec_val = {
2076         .uuid = { {0x00} },
2077         .channel = 12,
2078         .name = "bt_name",
2079 };
2080
2081 static struct priority_property remote_getprop_servrec_props[] = {
2082         {
2083         .prop.type = BT_PROPERTY_SERVICE_RECORD,
2084         .prop.val = &remote_getprop_servrec_val,
2085         .prop.len = sizeof(remote_getprop_servrec_val),
2086         },
2087 };
2088
2089 static const struct generic_data bt_dev_getprop_servrec_fail_test = {
2090         .expected_hal_cb.discovery_state_changed_cb =
2091                                         remote_discovery_state_changed_cb,
2092         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2093         .expected_hal_cb.remote_device_properties_cb =
2094                                         remote_test_device_properties_cb,
2095         .expected_cb_count = 3,
2096         .expected_properties = remote_getprop_servrec_props,
2097         .expected_adapter_status = BT_STATUS_FAIL,
2098 };
2099
2100 static bt_scan_mode_t remote_getprop_scanmode_val = BT_SCAN_MODE_CONNECTABLE;
2101
2102 static struct priority_property remote_getprop_scanmode_props[] = {
2103         {
2104         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
2105         .prop.val = &remote_getprop_scanmode_val,
2106         .prop.len = sizeof(remote_getprop_scanmode_val),
2107         },
2108 };
2109
2110 static const struct generic_data bt_dev_getprop_scanmode_fail_test = {
2111         .expected_hal_cb.discovery_state_changed_cb =
2112                                         remote_discovery_state_changed_cb,
2113         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2114         .expected_hal_cb.remote_device_properties_cb =
2115                                         remote_test_device_properties_cb,
2116         .expected_cb_count = 3,
2117         .expected_properties = remote_getprop_scanmode_props,
2118         .expected_adapter_status = BT_STATUS_FAIL,
2119 };
2120
2121 static struct priority_property remote_getprop_bondeddev_props[] = {
2122         {
2123         .prop.type = BT_PROPERTY_ADAPTER_BONDED_DEVICES,
2124         .prop.val = NULL,
2125         .prop.len = 0,
2126         },
2127 };
2128
2129 static const struct generic_data bt_dev_getprop_bondeddev_fail_test = {
2130         .expected_hal_cb.discovery_state_changed_cb =
2131                                         remote_discovery_state_changed_cb,
2132         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2133         .expected_hal_cb.remote_device_properties_cb =
2134                                         remote_test_device_properties_cb,
2135         .expected_cb_count = 3,
2136         .expected_properties = remote_getprop_bondeddev_props,
2137         .expected_adapter_status = BT_STATUS_FAIL,
2138 };
2139
2140 static uint32_t remote_getprop_disctimeout_val = 120;
2141
2142 static struct priority_property remote_getprop_disctimeout_props[] = {
2143         {
2144         .prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
2145         .prop.val = &remote_getprop_disctimeout_val,
2146         .prop.len = sizeof(remote_getprop_disctimeout_val),
2147         },
2148 };
2149
2150 static const struct generic_data bt_dev_getprop_disctimeout_fail_test = {
2151         .expected_hal_cb.discovery_state_changed_cb =
2152                                         remote_discovery_state_changed_cb,
2153         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2154         .expected_hal_cb.remote_device_properties_cb =
2155                                         remote_test_device_properties_cb,
2156         .expected_cb_count = 3,
2157         .expected_properties = remote_getprop_disctimeout_props,
2158         .expected_adapter_status = BT_STATUS_FAIL,
2159 };
2160
2161 static struct priority_property remote_getprop_verinfo_props[] = {
2162         {
2163         .prop.type = BT_PROPERTY_REMOTE_VERSION_INFO,
2164         .prop.val = NULL,
2165         .prop.len = 0,
2166         },
2167 };
2168
2169 static const struct generic_data bt_dev_getprop_verinfo_fail_test = {
2170         .expected_hal_cb.discovery_state_changed_cb =
2171                                         remote_discovery_state_changed_cb,
2172         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2173         .expected_hal_cb.remote_device_properties_cb =
2174                                         remote_test_device_properties_cb,
2175         .expected_cb_count = 3,
2176         .expected_properties = remote_getprop_verinfo_props,
2177         .expected_adapter_status = BT_STATUS_FAIL,
2178 };
2179
2180 static struct priority_property remote_getprop_fname_props[] = {
2181         {
2182         .prop.type = BT_PROPERTY_REMOTE_VERSION_INFO,
2183         .prop.val = NULL,
2184         .prop.len = 0,
2185         },
2186 };
2187
2188 static const struct generic_data bt_dev_getprop_fname_fail_test = {
2189         .expected_hal_cb.discovery_state_changed_cb =
2190                                         remote_discovery_state_changed_cb,
2191         .expected_hal_cb.device_found_cb = remote_get_property_device_found_cb,
2192         .expected_hal_cb.remote_device_properties_cb =
2193                                         remote_test_device_properties_cb,
2194         .expected_cb_count = 3,
2195         .expected_properties = remote_getprop_fname_props,
2196         .expected_adapter_status = BT_STATUS_FAIL,
2197 };
2198
2199 static const char remote_setprop_fname_val[] = "set_fname_test";
2200
2201 static struct priority_property remote_setprop_fname_props[] = {
2202         {
2203         .prop.type = BT_PROPERTY_REMOTE_FRIENDLY_NAME,
2204         .prop.val = &remote_setprop_fname_val,
2205         .prop.len = sizeof(remote_setprop_fname_val) - 1,
2206         },
2207         {
2208         .prop.type = BT_PROPERTY_REMOTE_FRIENDLY_NAME,
2209         .prop.val = &remote_setprop_fname_val,
2210         .prop.len = sizeof(remote_setprop_fname_val) - 1,
2211         },
2212 };
2213
2214 static const struct generic_data bt_dev_setprop_fname_success_test = {
2215         .expected_hal_cb.discovery_state_changed_cb =
2216                                         remote_setprop_disc_state_changed_cb,
2217         .expected_hal_cb.device_found_cb = remote_setprop_device_found_cb,
2218         .expected_hal_cb.remote_device_properties_cb =
2219                                         remote_setprop_device_properties_cb,
2220         .expected_cb_count = 4,
2221         .expected_properties_num = 2,
2222         .expected_properties = remote_setprop_fname_props,
2223         .expected_adapter_status = BT_STATUS_SUCCESS,
2224 };
2225
2226 static const char remote_setprop_bdname_val[] = "setprop_bdname_fail";
2227
2228 static struct priority_property remote_setprop_bdname_props[] = {
2229         {
2230         .prop.type = BT_PROPERTY_BDNAME,
2231         .prop.val = &remote_setprop_bdname_val,
2232         .prop.len = sizeof(remote_setprop_bdname_val) - 1,
2233         },
2234 };
2235
2236 static const struct generic_data bt_dev_setprop_bdname_fail_test = {
2237         .expected_hal_cb.discovery_state_changed_cb =
2238                                         remote_discovery_state_changed_cb,
2239         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2240         .expected_cb_count = 3,
2241         .expected_properties = remote_setprop_bdname_props,
2242         .expected_adapter_status = BT_STATUS_FAIL,
2243 };
2244
2245 static struct priority_property remote_setprop_uuids_props[] = {
2246         {
2247         .prop.type = BT_PROPERTY_UUIDS,
2248         .prop.val = NULL,
2249         .prop.len = 0,
2250         },
2251 };
2252
2253 static const struct generic_data bt_dev_setprop_uuids_fail_test  = {
2254         .expected_hal_cb.discovery_state_changed_cb =
2255                                         remote_discovery_state_changed_cb,
2256         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2257         .expected_cb_count = 3,
2258         .expected_properties = remote_setprop_uuids_props,
2259         .expected_adapter_status = BT_STATUS_FAIL,
2260 };
2261
2262 static uint32_t remote_setprop_cod_val = 0;
2263
2264 static struct priority_property remote_setprop_cod_props[] = {
2265         {
2266         .prop.type = BT_PROPERTY_CLASS_OF_DEVICE,
2267         .prop.val = &remote_setprop_cod_val,
2268         .prop.len = sizeof(remote_setprop_cod_val),
2269         },
2270 };
2271
2272 static const struct generic_data bt_dev_setprop_cod_fail_test = {
2273         .expected_hal_cb.discovery_state_changed_cb =
2274                                         remote_discovery_state_changed_cb,
2275         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2276         .expected_cb_count = 3,
2277         .expected_properties = remote_setprop_cod_props,
2278         .expected_adapter_status = BT_STATUS_FAIL,
2279 };
2280
2281 static bt_device_type_t remote_setprop_tod_val = BT_DEVICE_DEVTYPE_BREDR;
2282
2283 static struct priority_property remote_setprop_tod_props[] = {
2284         {
2285         .prop.type = BT_PROPERTY_TYPE_OF_DEVICE,
2286         .prop.val = &remote_setprop_tod_val,
2287         .prop.len = sizeof(remote_setprop_tod_val),
2288         },
2289 };
2290
2291 static const struct generic_data bt_dev_setprop_tod_fail_test = {
2292         .expected_hal_cb.discovery_state_changed_cb =
2293                                         remote_discovery_state_changed_cb,
2294         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2295         .expected_cb_count = 3,
2296         .expected_properties = remote_setprop_tod_props,
2297         .expected_adapter_status = BT_STATUS_FAIL,
2298 };
2299
2300 static int32_t remote_setprop_rssi_val = -60;
2301
2302 static struct priority_property remote_setprop_rssi_props[] = {
2303         {
2304         .prop.type = BT_PROPERTY_REMOTE_RSSI,
2305         .prop.val = &remote_setprop_rssi_val,
2306         .prop.len = sizeof(remote_setprop_rssi_val),
2307         },
2308 };
2309
2310 static const struct generic_data bt_dev_setprop_rssi_fail_test = {
2311         .expected_hal_cb.discovery_state_changed_cb =
2312                                         remote_discovery_state_changed_cb,
2313         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2314         .expected_cb_count = 3,
2315         .expected_properties = remote_setprop_rssi_props,
2316         .expected_adapter_status = BT_STATUS_FAIL,
2317 };
2318
2319 static int32_t remote_setprop_timestamp_val = 0xAB;
2320
2321 static struct priority_property remote_setprop_timestamp_props[] = {
2322         {
2323         .prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP,
2324         .prop.val = (&remote_setprop_timestamp_val),
2325         .prop.len = sizeof(remote_setprop_timestamp_val),
2326         },
2327 };
2328
2329 static const struct generic_data bt_dev_setprop_timpestamp_fail_test = {
2330         .expected_hal_cb.discovery_state_changed_cb =
2331                                         remote_discovery_state_changed_cb,
2332         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2333         .expected_cb_count = 3,
2334         .expected_properties = remote_setprop_timestamp_props,
2335         .expected_adapter_status = BT_STATUS_FAIL,
2336 };
2337
2338 static bt_bdaddr_t remote_setprop_bdaddr_val = {
2339         .address = { 0x00, 0xaa, 0x01, 0x00, 0x00, 0x00 }
2340 };
2341
2342 static struct priority_property remote_setprop_bdaddr_props[] = {
2343         {
2344         .prop.type = BT_PROPERTY_BDADDR,
2345         .prop.val = &remote_setprop_bdaddr_val,
2346         .prop.len = sizeof(remote_setprop_bdaddr_val),
2347         },
2348 };
2349
2350 static const struct generic_data bt_dev_setprop_bdaddr_fail_test = {
2351         .expected_hal_cb.discovery_state_changed_cb =
2352                                         remote_discovery_state_changed_cb,
2353         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2354         .expected_cb_count = 3,
2355         .expected_properties = remote_setprop_bdaddr_props,
2356         .expected_adapter_status = BT_STATUS_FAIL,
2357 };
2358
2359 static bt_service_record_t remote_setprop_servrec_val = {
2360         .uuid = { {0x00} },
2361         .channel = 12,
2362         .name = "bt_name",
2363 };
2364
2365 static struct priority_property remote_setprop_servrec_props[] = {
2366         {
2367         .prop.type = BT_PROPERTY_SERVICE_RECORD,
2368         .prop.val = &remote_setprop_servrec_val,
2369         .prop.len = sizeof(remote_setprop_servrec_val),
2370         },
2371 };
2372
2373 static const struct generic_data bt_dev_setprop_servrec_fail_test = {
2374         .expected_hal_cb.discovery_state_changed_cb =
2375                                         remote_discovery_state_changed_cb,
2376         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2377         .expected_cb_count = 3,
2378         .expected_properties = remote_setprop_servrec_props,
2379         .expected_adapter_status = BT_STATUS_FAIL,
2380 };
2381
2382 static bt_scan_mode_t remote_setprop_scanmode_val = BT_SCAN_MODE_CONNECTABLE;
2383
2384 static struct priority_property remote_setprop_scanmode_props[] = {
2385         {
2386         .prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE,
2387         .prop.val = &remote_setprop_scanmode_val,
2388         .prop.len = sizeof(remote_setprop_scanmode_val),
2389         },
2390 };
2391
2392 static const struct generic_data bt_dev_setprop_scanmode_fail_test = {
2393         .expected_hal_cb.discovery_state_changed_cb =
2394                                         remote_discovery_state_changed_cb,
2395         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2396         .expected_cb_count = 3,
2397         .expected_properties = remote_setprop_scanmode_props,
2398         .expected_adapter_status = BT_STATUS_FAIL,
2399 };
2400
2401 static bt_bdaddr_t remote_setprop_bondeddev_val = {
2402         .address = { 0x00, 0xaa, 0x01, 0x00, 0x00, 0x00 }
2403 };
2404
2405 static struct priority_property remote_setprop_bondeddev_props[] = {
2406         {
2407         .prop.type = BT_PROPERTY_ADAPTER_BONDED_DEVICES,
2408         .prop.val = &remote_setprop_bondeddev_val,
2409         .prop.len = sizeof(remote_setprop_bondeddev_val),
2410         },
2411 };
2412
2413 static const struct generic_data bt_dev_setprop_bondeddev_fail_test = {
2414         .expected_hal_cb.discovery_state_changed_cb =
2415                                         remote_discovery_state_changed_cb,
2416         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2417         .expected_cb_count = 3,
2418         .expected_properties = remote_setprop_bondeddev_props,
2419         .expected_adapter_status = BT_STATUS_FAIL,
2420 };
2421
2422 static uint32_t remote_setprop_disctimeout_val = 120;
2423
2424 static struct priority_property remote_setprop_disctimeout_props[] = {
2425         {
2426         .prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
2427         .prop.val = &remote_setprop_disctimeout_val,
2428         .prop.len = sizeof(remote_setprop_disctimeout_val),
2429         },
2430 };
2431
2432 static const struct generic_data bt_dev_setprop_disctimeout_fail_test = {
2433         .expected_hal_cb.discovery_state_changed_cb =
2434                                         remote_discovery_state_changed_cb,
2435         .expected_hal_cb.device_found_cb = remote_setprop_fail_device_found_cb,
2436         .expected_cb_count = 3,
2437         .expected_properties = remote_setprop_disctimeout_props,
2438         .expected_adapter_status = BT_STATUS_FAIL,
2439 };
2440
2441 static const struct generic_data bt_bond_create_pin_success_test = {
2442         .expected_hal_cb.device_found_cb = bond_device_found_cb,
2443         .expected_hal_cb.bond_state_changed_cb =
2444                                         bond_test_bonded_state_changed_cb,
2445         .expected_hal_cb.pin_request_cb = bond_create_pin_success_request_cb,
2446         .expected_cb_count = 4,
2447         .expected_adapter_status = BT_STATUS_SUCCESS,
2448 };
2449
2450 static const struct generic_data bt_bond_create_pin_fail_test = {
2451         .expected_hal_cb.device_found_cb = bond_nostatus_device_found_cb,
2452         .expected_hal_cb.bond_state_changed_cb =
2453                                                 bond_test_none_state_changed_cb,
2454         .expected_hal_cb.pin_request_cb = bond_create_pin_fail_request_cb,
2455         .expected_cb_count = 4,
2456         .expected_adapter_status = MGMT_STATUS_AUTH_FAILED,
2457 };
2458
2459 static const struct generic_data bt_bond_create_ssp_success_test = {
2460         .expected_hal_cb.device_found_cb = bond_device_found_cb,
2461         .expected_hal_cb.bond_state_changed_cb =
2462                                         bond_test_bonded_state_changed_cb,
2463         .expected_hal_cb.ssp_request_cb = bond_create_ssp_success_request_cb,
2464         .expected_cb_count = 4,
2465         .expected_adapter_status = BT_STATUS_SUCCESS,
2466 };
2467
2468 static const struct generic_data bt_bond_create_ssp_fail_test = {
2469         .expected_hal_cb.device_found_cb = bond_nostatus_device_found_cb,
2470         .expected_hal_cb.bond_state_changed_cb =
2471                                                 bond_test_none_state_changed_cb,
2472         .expected_hal_cb.ssp_request_cb = bond_create_ssp_fail_request_cb,
2473         .expected_cb_count = 4,
2474         .expected_adapter_status = MGMT_STATUS_AUTH_FAILED,
2475 };
2476
2477 static const struct generic_data bt_bond_create_no_disc_success_test = {
2478         .expected_hal_cb.bond_state_changed_cb =
2479                                         bond_test_bonded_state_changed_cb,
2480         .expected_hal_cb.ssp_request_cb = bond_create_ssp_success_request_cb,
2481         .expected_cb_count = 3,
2482         .expected_adapter_status = BT_STATUS_SUCCESS,
2483 };
2484
2485 static const struct generic_data bt_bond_create_bad_addr_success_test = {
2486         .expected_adapter_status = MGMT_STATUS_CONNECT_FAILED,
2487 };
2488
2489 static const struct generic_data bt_bond_cancel_success_test = {
2490         .expected_hal_cb.device_found_cb = bond_nostatus_device_found_cb,
2491         .expected_hal_cb.bond_state_changed_cb =
2492                                                 bond_test_none_state_changed_cb,
2493         .expected_hal_cb.ssp_request_cb = bond_cancel_success_ssp_request_cb,
2494         .expected_cb_count = 4,
2495         .expected_adapter_status = BT_STATUS_SUCCESS,
2496 };
2497
2498 static const struct generic_data bt_bond_remove_success_test = {
2499         .expected_hal_cb.device_found_cb = bond_nostatus_device_found_cb,
2500         .expected_hal_cb.bond_state_changed_cb =
2501                                         bond_remove_success_state_changed_cb,
2502         .expected_hal_cb.ssp_request_cb = bond_create_ssp_success_request_cb,
2503         .expected_cb_count = 4,
2504         .expected_adapter_status = BT_STATUS_SUCCESS,
2505 };
2506
2507 static bt_callbacks_t bt_callbacks = {
2508         .size = sizeof(bt_callbacks),
2509         .adapter_state_changed_cb = adapter_state_changed_cb,
2510         .adapter_properties_cb = adapter_properties_cb,
2511         .remote_device_properties_cb = remote_device_properties_cb,
2512         .device_found_cb = device_found_cb,
2513         .discovery_state_changed_cb = discovery_state_changed_cb,
2514         .pin_request_cb = pin_request_cb,
2515         .ssp_request_cb = ssp_request_cb,
2516         .bond_state_changed_cb = bond_state_changed_cb,
2517         .acl_state_changed_cb = NULL,
2518         .thread_evt_cb = NULL,
2519         .dut_mode_recv_cb = NULL,
2520         .le_test_mode_cb = NULL
2521 };
2522
2523 static bool setup(struct test_data *data)
2524 {
2525         const hw_module_t *module;
2526         hw_device_t *device;
2527         int signal_fd[2];
2528         char buf[1024];
2529         pid_t pid;
2530         int len;
2531         int err;
2532
2533         if (pipe(signal_fd))
2534                 return false;
2535
2536         pid = fork();
2537
2538         if (pid < 0) {
2539                 close(signal_fd[0]);
2540                 close(signal_fd[1]);
2541                 return false;
2542         }
2543
2544         if (pid == 0) {
2545                 if (!tester_use_debug())
2546                         fclose(stderr);
2547
2548                 close(signal_fd[0]);
2549                 emulator(signal_fd[1], data->mgmt_index);
2550                 exit(0);
2551         }
2552
2553         close(signal_fd[1]);
2554         data->bluetoothd_pid = pid;
2555
2556         len = read(signal_fd[0], buf, sizeof(buf));
2557         if (len <= 0 || strcmp(buf, EMULATOR_SIGNAL)) {
2558                 close(signal_fd[0]);
2559                 return false;
2560         }
2561
2562         close(signal_fd[0]);
2563
2564         err = hw_get_module(BT_HARDWARE_MODULE_ID, &module);
2565         if (err)
2566                 return false;
2567
2568         err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
2569         if (err)
2570                 return false;
2571
2572         data->device = device;
2573
2574         data->if_bluetooth = ((bluetooth_device_t *)
2575                                         device)->get_bluetooth_interface();
2576         if (!data->if_bluetooth)
2577                 return false;
2578
2579         return true;
2580 }
2581
2582 static void setup_base(const void *test_data)
2583 {
2584         struct test_data *data = tester_get_data();
2585         bt_status_t status;
2586
2587         if (!setup(data)) {
2588                 tester_setup_failed();
2589                 return;
2590         }
2591
2592         status = data->if_bluetooth->init(&bt_callbacks);
2593         if (status != BT_STATUS_SUCCESS) {
2594                 data->if_bluetooth = NULL;
2595                 tester_setup_failed();
2596                 return;
2597         }
2598
2599         tester_setup_complete();
2600 }
2601
2602 static void setup_enabled_adapter(const void *test_data)
2603 {
2604         struct test_data *data = tester_get_data();
2605         bt_status_t status;
2606
2607         if (!setup(data)) {
2608                 tester_setup_failed();
2609                 return;
2610         }
2611
2612         status = data->if_bluetooth->init(&bt_callbacks);
2613         if (status != BT_STATUS_SUCCESS) {
2614                 data->if_bluetooth = NULL;
2615                 tester_setup_failed();
2616                 return;
2617         }
2618
2619         status = data->if_bluetooth->enable();
2620         if (status != BT_STATUS_SUCCESS)
2621                 tester_setup_failed();
2622 }
2623
2624 static void teardown(const void *test_data)
2625 {
2626         struct test_data *data = tester_get_data();
2627
2628         if (data->if_hid) {
2629                 data->if_hid->cleanup();
2630                 data->if_hid = NULL;
2631         }
2632
2633         if (data->if_bluetooth) {
2634                 data->if_bluetooth->cleanup();
2635                 data->if_bluetooth = NULL;
2636         }
2637
2638         if (data->expected_properties_list)
2639                 g_slist_free(data->expected_properties_list);
2640
2641         data->device->close(data->device);
2642
2643         if (!data->bluetoothd_pid)
2644                 tester_teardown_complete();
2645 }
2646
2647 static void test_dummy(const void *test_data)
2648 {
2649         tester_test_passed();
2650 }
2651
2652 static void test_enable(const void *test_data)
2653 {
2654         struct test_data *data = tester_get_data();
2655         bt_status_t adapter_status;
2656
2657         uint8_t *bdaddr = (uint8_t *)hciemu_get_master_bdaddr(data->hciemu);
2658
2659         init_test_conditions(data);
2660
2661         bdaddr2android((const bdaddr_t *)bdaddr,
2662                                         &enable_done_bdaddr_val.address);
2663
2664         adapter_status = data->if_bluetooth->enable();
2665         check_expected_status(adapter_status);
2666 }
2667
2668 static void test_enable_done(const void *test_data)
2669 {
2670         struct test_data *data = tester_get_data();
2671         bt_status_t adapter_status;
2672
2673         uint8_t *bdaddr = (uint8_t *)hciemu_get_master_bdaddr(data->hciemu);
2674
2675         init_test_conditions(data);
2676
2677         bdaddr2android((const bdaddr_t *)bdaddr,
2678                                         &enable_done_bdaddr_val.address);
2679
2680         adapter_status = data->if_bluetooth->enable();
2681         check_expected_status(adapter_status);
2682 }
2683
2684 static void test_disable(const void *test_data)
2685 {
2686         struct test_data *data = tester_get_data();
2687         bt_status_t adapter_status;
2688
2689         init_test_conditions(data);
2690
2691         adapter_status = data->if_bluetooth->disable();
2692         check_expected_status(adapter_status);
2693 }
2694
2695 static void test_setprop_bdname_success(const void *test_data)
2696 {
2697         struct test_data *data = tester_get_data();
2698         const bt_property_t *prop = &(setprop_bdname_props[0].prop);
2699         bt_status_t adapter_status;
2700
2701         init_test_conditions(data);
2702
2703         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2704         check_expected_status(adapter_status);
2705 }
2706
2707 static void test_setprop_scanmode_succes(const void *test_data)
2708 {
2709         struct test_data *data = tester_get_data();
2710         const bt_property_t *prop = &(setprop_scanmode_props[0].prop);
2711         bt_status_t adapter_status;
2712
2713         init_test_conditions(data);
2714
2715         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2716         check_expected_status(adapter_status);
2717 }
2718
2719 static void test_setprop_disctimeout_succes(const void *test_data)
2720 {
2721         struct test_data *data = tester_get_data();
2722         const bt_property_t *prop = &(setprop_disctimeout_props[0].prop);
2723         bt_status_t adapter_status;
2724
2725         init_test_conditions(data);
2726
2727         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2728         check_expected_status(adapter_status);
2729 }
2730
2731 static void test_getprop_bdaddr_success(const void *test_data)
2732 {
2733         struct test_data *data = tester_get_data();
2734         const bt_property_t prop = setprop_bdaddr_props[0].prop;
2735         bt_status_t adapter_status;
2736         uint8_t *bdaddr = (uint8_t *)hciemu_get_master_bdaddr(data->hciemu);
2737
2738         init_test_conditions(data);
2739
2740         bdaddr2android((const bdaddr_t *)bdaddr,
2741                                         &test_getprop_bdaddr_val.address);
2742
2743         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2744         check_expected_status(adapter_status);
2745 }
2746
2747 static void test_getprop_bdname_success(const void *test_data)
2748 {
2749         struct test_data *data = tester_get_data();
2750         const bt_property_t *prop = &(getprop_bdname_props[0].prop);
2751         bt_status_t adapter_status;
2752
2753         init_test_conditions(data);
2754
2755         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2756         if (adapter_status != BT_STATUS_SUCCESS) {
2757                 tester_test_failed();
2758                 return;
2759         }
2760
2761         adapter_status = data->if_bluetooth->get_adapter_property((*prop).type);
2762         check_expected_status(adapter_status);
2763 }
2764 static void test_setprop_uuid_invalid(const void *test_data)
2765 {
2766         struct test_data *data = tester_get_data();
2767         const bt_property_t *prop = &(setprop_uuid_prop[0].prop);
2768         bt_status_t adapter_status;
2769
2770         init_test_conditions(data);
2771
2772         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2773         check_expected_status(adapter_status);
2774 }
2775
2776 static void test_setprop_cod_invalid(const void *test_data)
2777 {
2778         struct test_data *data = tester_get_data();
2779         const bt_property_t *prop = &(setprop_cod_props[0].prop);
2780         bt_status_t adapter_status;
2781
2782         init_test_conditions(data);
2783
2784         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2785         check_expected_status(adapter_status);
2786 }
2787
2788 static void test_setprop_tod_invalid(const void *test_data)
2789 {
2790         struct test_data *data = tester_get_data();
2791         const struct generic_data *test = data->test_data;
2792         const bt_property_t *prop = &test->set_property;
2793         bt_status_t adapter_status;
2794
2795         init_test_conditions(data);
2796
2797         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2798         check_expected_status(adapter_status);
2799 }
2800
2801 static void test_setprop_rssi_invalid(const void *test_data)
2802 {
2803         struct test_data *data = tester_get_data();
2804         const bt_property_t *prop = &(setprop_remote_rssi_props[0].prop);
2805         bt_status_t adapter_status;
2806
2807         init_test_conditions(data);
2808
2809         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2810         check_expected_status(adapter_status);
2811 }
2812
2813 static void test_setprop_service_record_invalid(const void *test_data)
2814 {
2815         struct test_data *data = tester_get_data();
2816         const bt_property_t *prop = &(setprop_service_record_props[0].prop);
2817         bt_status_t adapter_status;
2818
2819         init_test_conditions(data);
2820
2821         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2822         check_expected_status(adapter_status);
2823 }
2824
2825 static void test_setprop_bdaddr_invalid(const void *test_data)
2826 {
2827         struct test_data *data = tester_get_data();
2828         const bt_property_t *prop = &(setprop_bdaddr_props[0].prop);
2829         bt_status_t adapter_status;
2830
2831         init_test_conditions(data);
2832
2833         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2834         check_expected_status(adapter_status);
2835 }
2836
2837 static void test_setprop_scanmode_connectable_success(const void *test_data)
2838 {
2839         struct test_data *data = tester_get_data();
2840         const bt_property_t *prop =
2841                                 &(setprop_scanmode_connectable_props[0].prop);
2842         bt_status_t adapter_status;
2843
2844         init_test_conditions(data);
2845
2846         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2847         check_expected_status(adapter_status);
2848 }
2849
2850 static void test_setprop_bonded_devices_invalid(const void *test_data)
2851 {
2852         struct test_data *data = tester_get_data();
2853         const bt_property_t *prop = &(setprop_bonded_devices_props[0].prop);
2854         bt_status_t adapter_status;
2855
2856         init_test_conditions(data);
2857
2858         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2859         check_expected_status(adapter_status);
2860 }
2861
2862 static void test_getprop_cod_success(const void *test_data)
2863 {
2864         struct test_data *data = tester_get_data();
2865         const bt_property_t prop = setprop_cod_props[0].prop;
2866         bt_status_t adapter_status;
2867
2868         init_test_conditions(data);
2869
2870         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2871         check_expected_status(adapter_status);
2872 }
2873
2874 static void test_getprop_tod_success(const void *test_data)
2875 {
2876         struct test_data *data = tester_get_data();
2877         const bt_property_t prop = setprop_tod_props[0].prop;
2878         bt_status_t adapter_status;
2879
2880         init_test_conditions(data);
2881
2882         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2883         check_expected_status(adapter_status);
2884 }
2885
2886 static void test_getprop_scanmode_success(const void *test_data)
2887 {
2888         struct test_data *data = tester_get_data();
2889         const bt_property_t prop = setprop_scanmode_props[0].prop;
2890         bt_status_t adapter_status;
2891
2892         init_test_conditions(data);
2893
2894         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2895         check_expected_status(adapter_status);
2896 }
2897
2898 static void test_getprop_disctimeout_success(const void *test_data)
2899 {
2900         struct test_data *data = tester_get_data();
2901         const bt_property_t prop = setprop_disctimeout_props[0].prop;
2902         bt_status_t adapter_status;
2903
2904         init_test_conditions(data);
2905
2906         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2907         check_expected_status(adapter_status);
2908 }
2909
2910 static void test_getprop_uuids_success(const void *test_data)
2911 {
2912         struct test_data *data = tester_get_data();
2913         const bt_property_t prop = getprop_uuids_props[0].prop;
2914         bt_status_t adapter_status;
2915
2916         init_test_conditions(data);
2917
2918         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2919         check_expected_status(adapter_status);
2920 }
2921
2922 static void test_getprop_bondeddev_success(const void *test_data)
2923 {
2924         struct test_data *data = tester_get_data();
2925         const bt_property_t prop = getprop_bondeddev_props[0].prop;
2926         bt_status_t adapter_status;
2927
2928         init_test_conditions(data);
2929
2930         adapter_status = data->if_bluetooth->get_adapter_property(prop.type);
2931         check_expected_status(adapter_status);
2932 }
2933
2934 static void test_setprop_scanmode_none_done(const void *test_data)
2935 {
2936         struct test_data *data = tester_get_data();
2937         const bt_property_t *prop = &(setprop_scanmode_none_props[0].prop);
2938         bt_status_t adapter_status;
2939
2940         init_test_conditions(data);
2941
2942         adapter_status = data->if_bluetooth->set_adapter_property(prop);
2943         check_expected_status(adapter_status);
2944 }
2945
2946 static void test_discovery_start_success(const void *test_data)
2947 {
2948         struct test_data *data = tester_get_data();
2949         bt_status_t status;
2950
2951         init_test_conditions(data);
2952
2953         status = data->if_bluetooth->start_discovery();
2954         check_expected_status(status);
2955 }
2956
2957 static void test_discovery_stop_done(const void *test_data)
2958 {
2959         struct test_data *data = tester_get_data();
2960         bt_status_t status;
2961
2962         init_test_conditions(data);
2963
2964         status = data->if_bluetooth->cancel_discovery();
2965         check_expected_status(status);
2966 }
2967
2968 static bool pre_inq_compl_hook(const void *dummy, uint16_t len, void *user_data)
2969 {
2970         struct test_data *data = tester_get_data();
2971
2972         /* Make sure Inquiry Command Complete is not called */
2973
2974         hciemu_del_hook(data->hciemu, HCIEMU_HOOK_PRE_EVT, BT_HCI_CMD_INQUIRY);
2975
2976         return false;
2977 }
2978
2979 static void test_discovery_stop_success(const void *test_data)
2980 {
2981         struct test_data *data = tester_get_data();
2982
2983         init_test_conditions(data);
2984
2985         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_EVT, BT_HCI_CMD_INQUIRY,
2986                                         pre_inq_compl_hook, data);
2987
2988         data->if_bluetooth->start_discovery();
2989 }
2990
2991 static void test_discovery_start_done(const void *test_data)
2992 {
2993         struct test_data *data = tester_get_data();
2994
2995         init_test_conditions(data);
2996
2997         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_EVT, BT_HCI_CMD_INQUIRY,
2998                                         pre_inq_compl_hook, data);
2999
3000         data->if_bluetooth->start_discovery();
3001 }
3002
3003 static void test_discovery_device_found(const void *test_data)
3004 {
3005         struct test_data *data = tester_get_data();
3006
3007         init_test_conditions(data);
3008
3009         data->if_bluetooth->start_discovery();
3010 }
3011
3012 static void test_dev_getprops_success(const void *test_data)
3013 {
3014         struct test_data *data = tester_get_data();
3015
3016         init_test_conditions(data);
3017
3018         data->if_bluetooth->start_discovery();
3019 }
3020
3021 static void test_dev_getprop_bdname_success(const void *test_data)
3022 {
3023         struct test_data *data = tester_get_data();
3024
3025         init_test_conditions(data);
3026
3027         data->if_bluetooth->start_discovery();
3028 }
3029
3030 static void test_dev_getprop_uuids_success(const void *test_data)
3031 {
3032         struct test_data *data = tester_get_data();
3033
3034         init_test_conditions(data);
3035
3036         data->if_bluetooth->start_discovery();
3037 }
3038
3039 static void test_dev_getprop_cod_success(const void *test_data)
3040 {
3041         struct test_data *data = tester_get_data();
3042
3043         init_test_conditions(data);
3044
3045         data->if_bluetooth->start_discovery();
3046 }
3047
3048 static void test_dev_getprop_tod_success(const void *test_data)
3049 {
3050         struct test_data *data = tester_get_data();
3051
3052         init_test_conditions(data);
3053
3054         data->if_bluetooth->start_discovery();
3055 }
3056
3057 static void test_dev_getprop_rssi_success(const void *test_data)
3058 {
3059         struct test_data *data = tester_get_data();
3060
3061         init_test_conditions(data);
3062
3063         data->if_bluetooth->start_discovery();
3064 }
3065
3066 static void test_dev_getprop_timestamp_success(const void *test_data)
3067 {
3068         struct test_data *data = tester_get_data();
3069
3070         init_test_conditions(data);
3071
3072         data->if_bluetooth->start_discovery();
3073 }
3074
3075 static void test_dev_getprop_bdaddr_fail(const void *test_data)
3076 {
3077         struct test_data *data = tester_get_data();
3078
3079         init_test_conditions(data);
3080
3081         data->if_bluetooth->start_discovery();
3082 }
3083
3084 static void test_dev_getprop_servrec_fail(const void *test_data)
3085 {
3086         struct test_data *data = tester_get_data();
3087
3088         init_test_conditions(data);
3089
3090         data->if_bluetooth->start_discovery();
3091 }
3092
3093 static void test_dev_getprop_scanmode_fail(const void *test_data)
3094 {
3095         struct test_data *data = tester_get_data();
3096
3097         init_test_conditions(data);
3098
3099         data->if_bluetooth->start_discovery();
3100 }
3101
3102 static void test_dev_getprop_bondeddev_fail(const void *test_data)
3103 {
3104         struct test_data *data = tester_get_data();
3105
3106         init_test_conditions(data);
3107
3108         data->if_bluetooth->start_discovery();
3109 }
3110
3111 static void test_dev_getprop_disctimeout_fail(const void *test_data)
3112 {
3113         struct test_data *data = tester_get_data();
3114
3115         init_test_conditions(data);
3116
3117         data->if_bluetooth->start_discovery();
3118 }
3119
3120 static void test_dev_getprop_verinfo_fail(const void *test_data)
3121 {
3122         struct test_data *data = tester_get_data();
3123
3124         init_test_conditions(data);
3125
3126         data->if_bluetooth->start_discovery();
3127 }
3128
3129 static void test_dev_getprop_fname_fail(const void *test_data)
3130 {
3131         struct test_data *data = tester_get_data();
3132
3133         init_test_conditions(data);
3134
3135         data->if_bluetooth->start_discovery();
3136 }
3137
3138 static void test_dev_setprop_fname_success(const void *test_data)
3139 {
3140         struct test_data *data = tester_get_data();
3141
3142         init_test_conditions(data);
3143
3144         data->if_bluetooth->start_discovery();
3145 }
3146
3147 static void test_dev_setprop_bdname_fail(const void *test_data)
3148 {
3149         struct test_data *data = tester_get_data();
3150
3151         init_test_conditions(data);
3152
3153         data->if_bluetooth->start_discovery();
3154 }
3155
3156 static void test_dev_setprop_uuids_fail(const void *test_data)
3157 {
3158         struct test_data *data = tester_get_data();
3159
3160         init_test_conditions(data);
3161
3162         data->if_bluetooth->start_discovery();
3163 }
3164
3165 static void test_dev_setprop_cod_fail(const void *test_data)
3166 {
3167         struct test_data *data = tester_get_data();
3168
3169         init_test_conditions(data);
3170
3171         data->if_bluetooth->start_discovery();
3172 }
3173
3174 static void test_dev_setprop_tod_fail(const void *test_data)
3175 {
3176         struct test_data *data = tester_get_data();
3177
3178         init_test_conditions(data);
3179
3180         data->if_bluetooth->start_discovery();
3181 }
3182
3183 static void test_dev_setprop_rssi_fail(const void *test_data)
3184 {
3185         struct test_data *data = tester_get_data();
3186
3187         init_test_conditions(data);
3188
3189         data->if_bluetooth->start_discovery();
3190 }
3191
3192 static void test_dev_setprop_timestamp_fail(const void *test_data)
3193 {
3194         struct test_data *data = tester_get_data();
3195
3196         init_test_conditions(data);
3197
3198         data->if_bluetooth->start_discovery();
3199 }
3200
3201 static void test_dev_setprop_bdaddr_fail(const void *test_data)
3202 {
3203         struct test_data *data = tester_get_data();
3204
3205         init_test_conditions(data);
3206
3207         data->if_bluetooth->start_discovery();
3208 }
3209
3210 static void test_dev_setprop_servrec_fail(const void *test_data)
3211 {
3212         struct test_data *data = tester_get_data();
3213
3214         init_test_conditions(data);
3215
3216         data->if_bluetooth->start_discovery();
3217 }
3218
3219 static void test_dev_setprop_scanmode_fail(const void *test_data)
3220 {
3221         struct test_data *data = tester_get_data();
3222
3223         init_test_conditions(data);
3224
3225         data->if_bluetooth->start_discovery();
3226 }
3227
3228 static void test_dev_setprop_bondeddev_fail(const void *test_data)
3229 {
3230         struct test_data *data = tester_get_data();
3231
3232         init_test_conditions(data);
3233
3234         data->if_bluetooth->start_discovery();
3235 }
3236
3237 static void test_dev_setprop_disctimeout_fail(const void *test_data)
3238 {
3239         struct test_data *data = tester_get_data();
3240
3241         init_test_conditions(data);
3242
3243         data->if_bluetooth->start_discovery();
3244 }
3245
3246 static void bond_device_auth_fail_callback(uint16_t index, uint16_t length,
3247                                                         const void *param,
3248                                                         void *user_data)
3249 {
3250         const struct mgmt_ev_auth_failed *ev = param;
3251
3252         check_expected_status(ev->status);
3253 }
3254
3255 static void test_bond_create_pin_success(const void *test_data)
3256 {
3257         struct test_data *data = tester_get_data();
3258         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3259
3260         static uint8_t pair_device_pin[] = { 0x30, 0x30, 0x30, 0x30 };
3261         const void *pin = pair_device_pin;
3262         uint8_t pin_len = 4;
3263
3264         init_test_conditions(data);
3265
3266         bthost_set_pin_code(bthost, pin, pin_len);
3267
3268         data->if_bluetooth->start_discovery();
3269 }
3270
3271 static void test_bond_create_pin_fail(const void *test_data)
3272 {
3273         struct test_data *data = tester_get_data();
3274         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3275
3276         static uint8_t pair_device_pin[] = { 0x30, 0x30, 0x30, 0x30 };
3277         const void *pin = pair_device_pin;
3278         uint8_t pin_len = 4;
3279
3280         init_test_conditions(data);
3281
3282         mgmt_register(data->mgmt, MGMT_EV_AUTH_FAILED, data->mgmt_index,
3283                                         bond_device_auth_fail_callback, data,
3284                                         NULL);
3285
3286         bthost_set_pin_code(bthost, pin, pin_len);
3287
3288         data->if_bluetooth->start_discovery();
3289 }
3290
3291 static void test_bond_create_ssp_success(const void *test_data)
3292 {
3293         struct test_data *data = tester_get_data();
3294         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3295
3296         init_test_conditions(data);
3297
3298         bthost_write_ssp_mode(bthost, 0x01);
3299
3300         data->if_bluetooth->start_discovery();
3301 }
3302
3303 static void test_bond_create_ssp_fail(const void *test_data)
3304 {
3305         struct test_data *data = tester_get_data();
3306         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3307
3308         init_test_conditions(data);
3309
3310         mgmt_register(data->mgmt, MGMT_EV_AUTH_FAILED, data->mgmt_index,
3311                                         bond_device_auth_fail_callback, data,
3312                                         NULL);
3313
3314         bthost_write_ssp_mode(bthost, 0x01);
3315
3316         data->if_bluetooth->start_discovery();
3317 }
3318
3319 static void test_bond_create_no_disc_success(const void *test_data)
3320 {
3321         struct test_data *data = tester_get_data();
3322         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3323
3324         uint8_t *bdaddr = (uint8_t *)hciemu_get_client_bdaddr(data->hciemu);
3325         bt_bdaddr_t remote_addr;
3326         bt_status_t status;
3327
3328         init_test_conditions(data);
3329
3330         bdaddr2android((const bdaddr_t *)bdaddr, &remote_addr.address);
3331
3332         bthost_write_ssp_mode(bthost, 0x01);
3333
3334         status = data->if_bluetooth->create_bond(&remote_addr);
3335         check_expected_status(status);
3336 }
3337
3338 static void test_bond_create_bad_addr_success(const void *test_data)
3339 {
3340         struct test_data *data = tester_get_data();
3341         bt_bdaddr_t bad_addr = {
3342                 .address = { 0x12, 0x34, 0x56, 0x78, 0x90, 0x12 }
3343         };
3344
3345         init_test_conditions(data);
3346
3347         mgmt_register(data->mgmt, MGMT_EV_CONNECT_FAILED, data->mgmt_index,
3348                                         bond_device_auth_fail_callback, data,
3349                                         NULL);
3350
3351         data->if_bluetooth->create_bond(&bad_addr);
3352 }
3353
3354 static void test_bond_cancel_success(const void *test_data)
3355 {
3356         struct test_data *data = tester_get_data();
3357         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3358
3359         init_test_conditions(data);
3360
3361         bthost_write_ssp_mode(bthost, 0x01);
3362
3363         data->if_bluetooth->start_discovery();
3364 }
3365
3366 static void test_bond_remove_success(const void *test_data)
3367 {
3368         struct test_data *data = tester_get_data();
3369         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3370
3371         init_test_conditions(data);
3372
3373         bthost_write_ssp_mode(bthost, 0x01);
3374
3375         data->if_bluetooth->start_discovery();
3376 }
3377
3378 /* Test Socket HAL */
3379
3380 static gboolean adapter_socket_state_changed(gpointer user_data)
3381 {
3382         struct bt_cb_data *cb_data = user_data;
3383
3384         switch (cb_data->state) {
3385         case BT_STATE_ON:
3386                 setup_powered_emulated_remote();
3387                 break;
3388         case BT_STATE_OFF:
3389                 tester_setup_failed();
3390                 break;
3391         default:
3392                 break;
3393         }
3394
3395         g_free(cb_data);
3396
3397         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
3398         return FALSE;
3399 }
3400
3401 static void adapter_socket_state_changed_cb(bt_state_t state)
3402 {
3403         struct bt_cb_data *cb_data = g_new0(struct bt_cb_data, 1);
3404
3405         cb_data->state = state;
3406
3407         g_atomic_int_inc(&scheduled_cbacks_num);
3408         g_idle_add(adapter_socket_state_changed, cb_data);
3409 }
3410
3411 const bt_bdaddr_t bdaddr_dummy = {
3412         .address = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
3413 };
3414
3415 static const struct socket_data btsock_inv_param_socktype = {
3416         .bdaddr = &bdaddr_dummy,
3417         .sock_type = 0,
3418         .channel = 1,
3419         .service_uuid = NULL,
3420         .service_name = "Test service",
3421         .flags = 0,
3422         .expected_status = BT_STATUS_PARM_INVALID,
3423 };
3424
3425 static const struct socket_data btsock_inv_param_socktype_l2cap = {
3426         .bdaddr = &bdaddr_dummy,
3427         .sock_type = BTSOCK_L2CAP,
3428         .channel = 1,
3429         .service_uuid = NULL,
3430         .service_name = "Test service",
3431         .flags = 0,
3432         .expected_status = BT_STATUS_UNSUPPORTED,
3433 };
3434
3435 /* Test invalid: channel & uuid are both zeroes */
3436 static const struct socket_data btsock_inv_params_chan_uuid = {
3437         .bdaddr = &bdaddr_dummy,
3438         .sock_type = BTSOCK_RFCOMM,
3439         .channel = 0,
3440         .service_uuid = NULL,
3441         .service_name = "Test service",
3442         .flags = 0,
3443         .expected_status = BT_STATUS_PARM_INVALID,
3444 };
3445
3446 static const struct socket_data btsock_success = {
3447         .bdaddr = &bdaddr_dummy,
3448         .sock_type = BTSOCK_RFCOMM,
3449         .channel = 1,
3450         .service_uuid = NULL,
3451         .service_name = "Test service",
3452         .flags = 0,
3453         .expected_status = BT_STATUS_SUCCESS,
3454         .test_channel = false
3455 };
3456
3457 static const struct socket_data btsock_success_check_chan = {
3458         .sock_type = BTSOCK_RFCOMM,
3459         .channel = 1,
3460         .service_uuid = NULL,
3461         .service_name = "Test service",
3462         .flags = 0,
3463         .expected_status = BT_STATUS_SUCCESS,
3464         .test_channel = true,
3465 };
3466
3467 static const struct socket_data btsock_inv_param_bdaddr = {
3468         .bdaddr = NULL,
3469         .sock_type = BTSOCK_RFCOMM,
3470         .channel = 1,
3471         .service_uuid = NULL,
3472         .service_name = "Test service",
3473         .flags = 0,
3474         .expected_status = BT_STATUS_PARM_INVALID,
3475 };
3476
3477 static const struct socket_data btsock_inv_listen_listen = {
3478         .sock_type = BTSOCK_RFCOMM,
3479         .channel = 1,
3480         .service_uuid = NULL,
3481         .service_name = "Test service",
3482         .flags = 0,
3483         .expected_status = BT_STATUS_BUSY,
3484         .test_channel = true,
3485 };
3486
3487 static bt_callbacks_t bt_socket_callbacks = {
3488         .size = sizeof(bt_callbacks),
3489         .adapter_state_changed_cb = adapter_socket_state_changed_cb,
3490         .adapter_properties_cb = NULL,
3491         .remote_device_properties_cb = NULL,
3492         .device_found_cb = NULL,
3493         .discovery_state_changed_cb = NULL,
3494         .pin_request_cb = NULL,
3495         .ssp_request_cb = NULL,
3496         .bond_state_changed_cb = NULL,
3497         .acl_state_changed_cb = NULL,
3498         .thread_evt_cb = NULL,
3499         .dut_mode_recv_cb = NULL,
3500         .le_test_mode_cb = NULL
3501 };
3502
3503 static void setup_socket_interface(const void *test_data)
3504 {
3505         struct test_data *data = tester_get_data();
3506         bt_status_t status;
3507         const void *sock;
3508
3509         if (!setup(data)) {
3510                 tester_setup_failed();
3511                 return;
3512         }
3513
3514         status = data->if_bluetooth->init(&bt_socket_callbacks);
3515         if (status != BT_STATUS_SUCCESS) {
3516                 data->if_bluetooth = NULL;
3517                 tester_setup_failed();
3518                 return;
3519         }
3520
3521         sock = data->if_bluetooth->get_profile_interface(BT_PROFILE_SOCKETS_ID);
3522         if (!sock) {
3523                 tester_setup_failed();
3524                 return;
3525         }
3526
3527         data->if_sock = sock;
3528
3529         tester_setup_complete();
3530 }
3531
3532 static void setup_socket_interface_enabled(const void *test_data)
3533 {
3534         struct test_data *data = tester_get_data();
3535         bt_status_t status;
3536         const void *sock;
3537
3538         if (!setup(data)) {
3539                 tester_setup_failed();
3540                 return;
3541         }
3542
3543         status = data->if_bluetooth->init(&bt_socket_callbacks);
3544         if (status != BT_STATUS_SUCCESS) {
3545                 data->if_bluetooth = NULL;
3546                 tester_setup_failed();
3547                 return;
3548         }
3549
3550         sock = data->if_bluetooth->get_profile_interface(BT_PROFILE_SOCKETS_ID);
3551         if (!sock) {
3552                 tester_setup_failed();
3553                 return;
3554         }
3555
3556         data->if_sock = sock;
3557
3558         status = data->if_bluetooth->enable();
3559         if (status != BT_STATUS_SUCCESS)
3560                 tester_setup_failed();
3561 }
3562
3563 static void test_generic_listen(const void *test_data)
3564 {
3565         struct test_data *data = tester_get_data();
3566         const struct socket_data *test = data->test_data;
3567         bt_status_t status;
3568         int sock_fd = -1;
3569
3570         status = data->if_sock->listen(test->sock_type,
3571                                         test->service_name, test->service_uuid,
3572                                         test->channel, &sock_fd, test->flags);
3573         if (status != test->expected_status) {
3574                 tester_test_failed();
3575                 goto clean;
3576         }
3577
3578         /* Check that file descriptor is valid */
3579         if (status == BT_STATUS_SUCCESS && fcntl(sock_fd, F_GETFD) < 0) {
3580                 tester_test_failed();
3581                 return;
3582         }
3583
3584         if (status == BT_STATUS_SUCCESS && test->test_channel) {
3585                 int channel, len;
3586
3587                 len = read(sock_fd, &channel, sizeof(channel));
3588                 if (len != sizeof(channel) || channel != test->channel) {
3589                         tester_test_failed();
3590                         goto clean;
3591                 }
3592
3593                 tester_print("read correct channel: %d", channel);
3594         }
3595
3596         tester_test_passed();
3597
3598 clean:
3599         if (sock_fd >= 0)
3600                 close(sock_fd);
3601 }
3602
3603 static void test_listen_close(const void *test_data)
3604 {
3605         struct test_data *data = tester_get_data();
3606         const struct socket_data *test = data->test_data;
3607         bt_status_t status;
3608         int sock_fd = -1;
3609
3610         status = data->if_sock->listen(test->sock_type,
3611                                         test->service_name, test->service_uuid,
3612                                         test->channel, &sock_fd, test->flags);
3613         if (status != test->expected_status) {
3614                 tester_warn("sock->listen() failed");
3615                 tester_test_failed();
3616                 goto clean;
3617         }
3618
3619         /* Check that file descriptor is valid */
3620         if (status == BT_STATUS_SUCCESS && fcntl(sock_fd, F_GETFD) < 0) {
3621                 tester_warn("sock_fd %d is not valid", sock_fd);
3622                 tester_test_failed();
3623                 return;
3624         }
3625
3626         tester_print("Got valid sock_fd: %d", sock_fd);
3627
3628         /* Now close sock_fd */
3629         close(sock_fd);
3630         sock_fd = -1;
3631
3632         /* Try to listen again */
3633         status = data->if_sock->listen(test->sock_type,
3634                                         test->service_name, test->service_uuid,
3635                                         test->channel, &sock_fd, test->flags);
3636         if (status != test->expected_status) {
3637                 tester_warn("sock->listen() failed");
3638                 tester_test_failed();
3639                 goto clean;
3640         }
3641
3642         /* Check that file descriptor is valid */
3643         if (status == BT_STATUS_SUCCESS && fcntl(sock_fd, F_GETFD) < 0) {
3644                 tester_warn("sock_fd %d is not valid", sock_fd);
3645                 tester_test_failed();
3646                 return;
3647         }
3648
3649         tester_print("Got valid sock_fd: %d", sock_fd);
3650
3651         tester_test_passed();
3652
3653 clean:
3654         if (sock_fd >= 0)
3655                 close(sock_fd);
3656 }
3657
3658 static void test_listen_listen(const void *test_data)
3659 {
3660         struct test_data *data = tester_get_data();
3661         const struct socket_data *test = data->test_data;
3662         bt_status_t status;
3663         int sock_fd1 = -1, sock_fd2 = -1;
3664
3665         status = data->if_sock->listen(test->sock_type,
3666                                         test->service_name, test->service_uuid,
3667                                         test->channel, &sock_fd1, test->flags);
3668         if (status != BT_STATUS_SUCCESS) {
3669                 tester_warn("sock->listen() failed");
3670                 tester_test_failed();
3671                 goto clean;
3672         }
3673
3674         status = data->if_sock->listen(test->sock_type,
3675                                         test->service_name, test->service_uuid,
3676                                         test->channel, &sock_fd2, test->flags);
3677         if (status != test->expected_status) {
3678                 tester_warn("sock->listen() failed, status %d", status);
3679                 tester_test_failed();
3680                 goto clean;
3681         }
3682
3683         tester_print("status after second listen(): %d", status);
3684
3685         tester_test_passed();
3686
3687 clean:
3688         if (sock_fd1 >= 0)
3689                 close(sock_fd1);
3690
3691         if (sock_fd2 >= 0)
3692                 close(sock_fd2);
3693 }
3694
3695 static void test_generic_connect(const void *test_data)
3696 {
3697         struct test_data *data = tester_get_data();
3698         const struct socket_data *test = data->test_data;
3699         bt_status_t status;
3700         int sock_fd = -1;
3701
3702         status = data->if_sock->connect(test->bdaddr, test->sock_type,
3703                                         test->service_uuid, test->channel,
3704                                         &sock_fd, test->flags);
3705         if (status != test->expected_status) {
3706                 tester_test_failed();
3707                 goto clean;
3708         }
3709
3710         /* Check that file descriptor is valid */
3711         if (status == BT_STATUS_SUCCESS && fcntl(sock_fd, F_GETFD) < 0) {
3712                 tester_test_failed();
3713                 return;
3714         }
3715
3716         tester_test_passed();
3717
3718 clean:
3719         if (sock_fd >= 0)
3720                 close(sock_fd);
3721 }
3722
3723 static gboolean socket_chan_cb(GIOChannel *io, GIOCondition cond,
3724                                                         gpointer user_data)
3725 {
3726         int sock_fd = g_io_channel_unix_get_fd(io);
3727         struct test_data *data = tester_get_data();
3728         const struct socket_data *test = data->test_data;
3729         int channel, len;
3730
3731         tester_print("%s", __func__);
3732
3733         if (cond & G_IO_HUP) {
3734                 tester_warn("Socket %d hang up", sock_fd);
3735                 goto failed;
3736         }
3737
3738         if (cond & (G_IO_ERR | G_IO_NVAL)) {
3739                 tester_warn("Socket error: sock %d cond %d", sock_fd, cond);
3740                 goto failed;
3741         }
3742
3743         if (test->test_channel) {
3744                 len = read(sock_fd, &channel, sizeof(channel));
3745                 if (len != sizeof(channel) || channel != test->channel)
3746                         goto failed;
3747
3748                 tester_print("read correct channel: %d", channel);
3749                 tester_test_passed();
3750                 return FALSE;
3751         }
3752
3753 failed:
3754         tester_test_failed();
3755         return FALSE;
3756 }
3757
3758 static void test_socket_real_connect(const void *test_data)
3759 {
3760         struct test_data *data = tester_get_data();
3761         const struct socket_data *test = data->test_data;
3762         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
3763         const uint8_t *client_bdaddr;
3764         bt_bdaddr_t emu_bdaddr;
3765         bt_status_t status;
3766         int sock_fd = -1;
3767
3768         client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
3769         if (!client_bdaddr) {
3770                 tester_warn("No client bdaddr");
3771                 tester_test_failed();
3772                 return;
3773         }
3774
3775         bdaddr2android((bdaddr_t *) client_bdaddr, &emu_bdaddr);
3776
3777         bthost_add_l2cap_server(bthost, 0x0003, NULL, NULL);
3778
3779         status = data->if_sock->connect(&emu_bdaddr, test->sock_type,
3780                                         test->service_uuid, test->channel,
3781                                         &sock_fd, test->flags);
3782         if (status != test->expected_status) {
3783                 tester_test_failed();
3784                 goto clean;
3785         }
3786
3787         /* Check that file descriptor is valid */
3788         if (status == BT_STATUS_SUCCESS && fcntl(sock_fd, F_GETFD) < 0) {
3789                 tester_test_failed();
3790                 return;
3791         }
3792
3793         tester_print("status %d sock_fd %d", status, sock_fd);
3794
3795         if (status == BT_STATUS_SUCCESS) {
3796                 GIOChannel *io;
3797
3798                 io = g_io_channel_unix_new(sock_fd);
3799                 g_io_channel_set_close_on_unref(io, TRUE);
3800
3801                 g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
3802                                 socket_chan_cb, NULL);
3803
3804                 g_io_channel_unref(io);
3805         }
3806
3807         return;
3808
3809 clean:
3810         if (sock_fd >= 0)
3811                 close(sock_fd);
3812 }
3813
3814 static gboolean hidhost_connection_state(gpointer user_data)
3815 {
3816         struct test_data *data = tester_get_data();
3817         const struct hidhost_generic_data *test = data->test_data;
3818         struct hh_cb_data *cb_data = user_data;
3819
3820         data->cb_count++;
3821
3822         if (cb_data->state == BTHH_CONN_STATE_CONNECTED)
3823                 tester_setup_complete();
3824
3825         if (test && test->expected_hal_cb.connection_state_cb)
3826                 test->expected_hal_cb.connection_state_cb(&cb_data->bdaddr,
3827                                                                 cb_data->state);
3828
3829         g_free(cb_data);
3830
3831         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
3832         return FALSE;
3833 }
3834
3835 static void hidhost_connection_state_cb(bt_bdaddr_t *bd_addr,
3836                                                 bthh_connection_state_t state)
3837 {
3838         struct hh_cb_data *cb_data = g_new0(struct hh_cb_data, 1);
3839
3840         cb_data->state = state;
3841         cb_data->bdaddr = *bd_addr;
3842
3843         g_atomic_int_inc(&scheduled_cbacks_num);
3844         g_idle_add(hidhost_connection_state, cb_data);
3845 }
3846
3847 static gboolean hidhost_virual_unplug(gpointer user_data)
3848 {
3849         struct test_data *data = tester_get_data();
3850         const struct hidhost_generic_data *test = data->test_data;
3851         struct hh_cb_data *cb_data = user_data;
3852
3853         data->cb_count++;
3854
3855         if (test && test->expected_hal_cb.virtual_unplug_cb)
3856                 test->expected_hal_cb.virtual_unplug_cb(&cb_data->bdaddr,
3857                                                         cb_data->status);
3858
3859         g_free(cb_data);
3860
3861         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
3862         return FALSE;
3863 }
3864
3865 static void hidhost_virual_unplug_cb(bt_bdaddr_t *bd_addr, bthh_status_t status)
3866 {
3867         struct hh_cb_data *cb_data = g_new0(struct hh_cb_data, 1);
3868
3869         cb_data->bdaddr = *bd_addr;
3870         cb_data->status = status;
3871
3872         g_atomic_int_inc(&scheduled_cbacks_num);
3873         g_idle_add(hidhost_virual_unplug, cb_data);
3874 }
3875
3876 static gboolean hidhost_hid_info(gpointer user_data)
3877 {
3878         struct test_data *data = tester_get_data();
3879         const struct hidhost_generic_data *test = data->test_data;
3880         struct hh_cb_data *cb_data = user_data;
3881
3882         data->cb_count++;
3883
3884         if (test && test->expected_hal_cb.hid_info_cb)
3885                 test->expected_hal_cb.hid_info_cb(&cb_data->bdaddr,
3886                                                         cb_data->hid_info);
3887
3888         g_free(cb_data);
3889
3890         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
3891         return FALSE;
3892 }
3893
3894 static void hidhost_hid_info_cb(bt_bdaddr_t *bd_addr, bthh_hid_info_t hid)
3895 {
3896         struct hh_cb_data *cb_data = g_new0(struct hh_cb_data, 1);
3897
3898         cb_data->bdaddr = *bd_addr;
3899         cb_data->hid_info = hid;
3900
3901         g_atomic_int_inc(&scheduled_cbacks_num);
3902         g_idle_add(hidhost_hid_info, cb_data);
3903 }
3904
3905 static gboolean hidhost_protocol_mode(gpointer user_data)
3906 {
3907         struct test_data *data = tester_get_data();
3908         const struct hidhost_generic_data *test = data->test_data;
3909         struct hh_cb_data *cb_data = user_data;
3910
3911         data->cb_count++;
3912
3913         if (test && test->expected_hal_cb.protocol_mode_cb)
3914                 test->expected_hal_cb.protocol_mode_cb(&cb_data->bdaddr,
3915                                                 cb_data->status, cb_data->mode);
3916
3917         g_free(cb_data);
3918
3919         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
3920         return FALSE;
3921 }
3922
3923 static void hidhost_protocol_mode_cb(bt_bdaddr_t *bd_addr,
3924                                                 bthh_status_t status,
3925                                                 bthh_protocol_mode_t mode)
3926 {
3927         struct hh_cb_data *cb_data = g_new0(struct hh_cb_data, 1);
3928
3929         cb_data->bdaddr = *bd_addr;
3930         cb_data->status = status;
3931         cb_data->mode = mode;
3932
3933         g_atomic_int_inc(&scheduled_cbacks_num);
3934         g_idle_add(hidhost_protocol_mode, cb_data);
3935 }
3936
3937 static gboolean hidhost_get_report(gpointer user_data)
3938 {
3939         struct test_data *data = tester_get_data();
3940         const struct hidhost_generic_data *test = data->test_data;
3941         struct hh_cb_data *cb_data = user_data;
3942
3943         data->cb_count++;
3944
3945         if (test && test->expected_hal_cb.get_report_cb)
3946                 test->expected_hal_cb.get_report_cb(&cb_data->bdaddr,
3947                         cb_data->status, cb_data->report, cb_data->size);
3948
3949         g_free(cb_data->report);
3950         g_free(cb_data);
3951
3952         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
3953         return FALSE;
3954 }
3955
3956 static void hidhost_get_report_cb(bt_bdaddr_t *bd_addr, bthh_status_t status,
3957                                                 uint8_t *report, int size)
3958 {
3959         struct hh_cb_data *cb_data = g_new0(struct hh_cb_data, 1);
3960
3961         cb_data->bdaddr = *bd_addr;
3962         cb_data->status = status;
3963         cb_data->report = g_memdup(report, size);
3964         cb_data->size = size;
3965
3966         g_atomic_int_inc(&scheduled_cbacks_num);
3967         g_idle_add(hidhost_get_report, cb_data);
3968 }
3969
3970 static bthh_callbacks_t bthh_callbacks = {
3971         .size = sizeof(bthh_callbacks),
3972         .connection_state_cb = hidhost_connection_state_cb,
3973         .hid_info_cb = hidhost_hid_info_cb,
3974         .protocol_mode_cb = hidhost_protocol_mode_cb,
3975         .idle_time_cb = NULL,
3976         .get_report_cb = hidhost_get_report_cb,
3977         .virtual_unplug_cb = hidhost_virual_unplug_cb
3978 };
3979
3980 static bool setup_hidhost(const void *test_data)
3981 {
3982         struct test_data *data = tester_get_data();
3983         bt_status_t status;
3984         const void *hid;
3985
3986         if (!setup(data))
3987                 return false;
3988
3989         status = data->if_bluetooth->init(&bt_callbacks);
3990         if (status != BT_STATUS_SUCCESS) {
3991                 data->if_bluetooth = NULL;
3992                 return false;
3993         }
3994
3995         hid = data->if_bluetooth->get_profile_interface(BT_PROFILE_HIDHOST_ID);
3996         if (!hid)
3997                 return false;
3998
3999         data->if_hid = hid;
4000
4001         status = data->if_hid->init(&bthh_callbacks);
4002         if (status != BT_STATUS_SUCCESS) {
4003                 data->if_hid = NULL;
4004                 return false;
4005         }
4006
4007         return true;
4008 }
4009
4010 static void setup_hidhost_interface(const void *test_data)
4011 {
4012         if (setup_hidhost(test_data))
4013                 tester_setup_complete();
4014         else
4015                 tester_setup_failed();
4016 }
4017
4018 #define HID_GET_REPORT_PROTOCOL         0x60
4019 #define HID_GET_BOOT_PROTOCOL           0x61
4020 #define HID_SET_REPORT_PROTOCOL         0x70
4021 #define HID_SET_BOOT_PROTOCOL           0x71
4022
4023 #define HID_SET_INPUT_REPORT            0x51
4024 #define HID_SET_OUTPUT_REPORT           0x52
4025 #define HID_SET_FEATURE_REPORT          0x53
4026
4027 #define HID_SEND_DATA                   0xa2
4028
4029 #define HID_GET_INPUT_REPORT            0x49
4030 #define HID_GET_OUTPUT_REPORT           0x4a
4031 #define HID_GET_FEATURE_REPORT          0x4b
4032
4033 static void hid_prepare_reply_protocol_mode(const void *data, uint16_t len)
4034 {
4035         struct test_data *t_data = tester_get_data();
4036         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
4037         uint8_t pdu[2] = { 0, 0 };
4038         uint16_t pdu_len = 0;
4039
4040         pdu_len = 2;
4041         pdu[0] = 0xa0;
4042         pdu[1] = 0x00;
4043
4044         bthost_send_cid(bthost, t_data->ctrl_handle, t_data->ctrl_cid,
4045                                                 (void *)pdu, pdu_len);
4046 }
4047
4048 static void hid_prepare_reply_report(const void *data, uint16_t len)
4049 {
4050         struct test_data *t_data = tester_get_data();
4051         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
4052         uint8_t pdu[3] = { 0, 0, 0 };
4053         uint16_t pdu_len = 0;
4054
4055         pdu_len = 3;
4056         pdu[0] = 0xa2;
4057         pdu[1] = 0x01;
4058         pdu[2] = 0x00;
4059
4060         bthost_send_cid(bthost, t_data->ctrl_handle, t_data->ctrl_cid,
4061                                                 (void *)pdu, pdu_len);
4062 }
4063
4064 static void hid_intr_cid_hook_cb(const void *data, uint16_t len,
4065                                                         void *user_data)
4066 {
4067         uint8_t header = ((uint8_t *) data)[0];
4068
4069         switch (header) {
4070         case HID_SEND_DATA:
4071                 tester_test_passed();
4072                 break;
4073         }
4074 }
4075
4076 static void hid_intr_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
4077 {
4078         struct test_data *data = tester_get_data();
4079         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
4080
4081         data->intr_handle = handle;
4082         data->intr_cid = cid;
4083
4084         bthost_add_cid_hook(bthost, handle, cid, hid_intr_cid_hook_cb, NULL);
4085 }
4086
4087 static void hid_ctrl_cid_hook_cb(const void *data, uint16_t len,
4088                                                         void *user_data)
4089 {
4090         uint8_t header = ((uint8_t *) data)[0];
4091
4092         switch (header) {
4093         case HID_GET_REPORT_PROTOCOL:
4094         case HID_GET_BOOT_PROTOCOL:
4095         case HID_SET_REPORT_PROTOCOL:
4096         case HID_SET_BOOT_PROTOCOL:
4097                 hid_prepare_reply_protocol_mode(data, len);
4098                 break;
4099         case HID_GET_INPUT_REPORT:
4100         case HID_GET_OUTPUT_REPORT:
4101         case HID_GET_FEATURE_REPORT:
4102                 hid_prepare_reply_report(data, len);
4103                 break;
4104         /* HID device doesnot reply for this commads, so reaching pdu's
4105          * to hid device means assuming test passed */
4106         case HID_SET_INPUT_REPORT:
4107         case HID_SET_OUTPUT_REPORT:
4108         case HID_SET_FEATURE_REPORT:
4109         case HID_SEND_DATA:
4110                 tester_test_passed();
4111                 break;
4112         }
4113 }
4114
4115 static void hid_ctrl_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
4116 {
4117         struct test_data *data = tester_get_data();
4118         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
4119
4120         data->ctrl_handle = handle;
4121         data->ctrl_cid = cid;
4122
4123         bthost_add_cid_hook(bthost, handle, cid, hid_ctrl_cid_hook_cb, NULL);
4124 }
4125
4126 static const uint8_t did_req_pdu[] = { 0x06, /* PDU id */
4127                         0x00, 0x00, /* Transaction id */
4128                         0x00, 0x0f, /* Req length */
4129                         0x35, 0x03, /* Attributes length */
4130                         0x19, 0x12, 0x00, 0xff, 0xff, 0x35, 0x05, 0x0a, 0x00,
4131                         0x00, 0xff, 0xff, 0x00 }; /* no continuation */
4132
4133 static const uint8_t did_rsp_pdu[] = { 0x07, /* PDU id */
4134                         0x00, 0x00, /* Transaction id */
4135                         0x00, 0x4f, /* Response length */
4136                         0x00, 0x4c, /* Attributes length */
4137                         0x35, 0x4a, 0x35, 0x48, 0x09, 0x00, 0x00, 0x0a, 0x00,
4138                         0x01, 0x00, 0x00, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
4139                         0x12, 0x00, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10,
4140                         0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19,
4141                         0x12, 0x00, 0x09, 0x01, 0x03, 0x09, 0x02, 0x00, 0x09,
4142                         0x01, 0x03, 0x09, 0x02, 0x01, 0x09, 0x1d, 0x6b, 0x09,
4143                         0x02, 0x02, 0x09, 0x02, 0x46, 0x09, 0x02, 0x03, 0x09,
4144                         0x05, 0x0e, 0x09, 0x02, 0x04, 0x28, 0x01, 0x09, 0x02,
4145                         0x05, 0x09, 0x00, 0x02,
4146                         0x00 }; /* no continuation */
4147
4148 static const uint8_t hid_rsp_pdu[] = { 0x07, /* PDU id */
4149                         0x00, 0x01, /* Transaction id */
4150                         0x01, 0x71, /* Response length */
4151                         0x01, 0x6E, /* Attributes length */
4152                         0x36, 0x01, 0x6b, 0x36, 0x01, 0x68, 0x09, 0x00, 0x00,
4153                         0x0a, 0x00, 0x01, 0x00, 0x00, 0x09, 0x00, 0x01, 0x35,
4154                         0x03, 0x19, 0x11, 0x24, 0x09, 0x00, 0x04, 0x35, 0x0d,
4155                         0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x11, 0x35,
4156                         0x03, 0x19, 0x00, 0x11, 0x09, 0x00, 0x05, 0x35, 0x03,
4157                         0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09,
4158                         0x65, 0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01, 0x00, 0x09,
4159                         0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x24,
4160                         0x09, 0x01, 0x00, 0x09, 0x00, 0x0d, 0x35, 0x0f, 0x35,
4161                         0x0d, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x13,
4162                         0x35, 0x03, 0x19, 0x00, 0x11, 0x09, 0x01, 0x00, 0x25,
4163                         0x1e, 0x4c, 0x6f, 0x67, 0x69, 0x74, 0x65, 0x63, 0x68,
4164                         0x20, 0x42, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74,
4165                         0x68, 0x20, 0x4d, 0x6f, 0x75, 0x73, 0x65, 0x20, 0x4d,
4166                         0x35, 0x35, 0x35, 0x62, 0x09, 0x01, 0x01, 0x25, 0x0f,
4167                         0x42, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74, 0x68,
4168                         0x20, 0x4d, 0x6f, 0x75, 0x73, 0x65, 0x09, 0x01, 0x02,
4169                         0x25, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x74, 0x65, 0x63,
4170                         0x68, 0x09, 0x02, 0x00, 0x09, 0x01, 0x00, 0x09, 0x02,
4171                         0x01, 0x09, 0x01, 0x11, 0x09, 0x02, 0x02, 0x08, 0x80,
4172                         0x09, 0x02, 0x03, 0x08, 0x21, 0x09, 0x02, 0x04, 0x28,
4173                         0x01, 0x09, 0x02, 0x05, 0x28, 0x01, 0x09, 0x02, 0x06,
4174                         0x35, 0x74, 0x35, 0x72, 0x08, 0x22, 0x25, 0x6e, 0x05,
4175                         0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x02, 0x09, 0x01,
4176                         0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x08, 0x15,
4177                         0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02,
4178                         0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x16, 0x01, 0xf8,
4179                         0x26, 0xff, 0x07, 0x75, 0x0c, 0x95, 0x02, 0x81, 0x06,
4180                         0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95,
4181                         0x01, 0x81, 0x06, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x81,
4182                         0x06, 0x05, 0x09, 0x19, 0x09, 0x29, 0x10, 0x15, 0x00,
4183                         0x25, 0x01, 0x95, 0x08, 0x75, 0x01, 0x81, 0x02, 0xc0,
4184                         0xc0, 0x06, 0x00, 0xff, 0x09, 0x01, 0xa1, 0x01, 0x85,
4185                         0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 0x00, 0x26, 0xff,
4186                         0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 0x91, 0x00,
4187                         0xc0, 0x09, 0x02, 0x07, 0x35, 0x08, 0x35, 0x06, 0x09,
4188                         0x04, 0x09, 0x09, 0x01, 0x00, 0x09, 0x02, 0x08, 0x28,
4189                         0x00, 0x09, 0x02, 0x09, 0x28, 0x01, 0x09, 0x02, 0x0a,
4190                         0x28, 0x01, 0x09, 0x02, 0x0b, 0x09, 0x01, 0x00, 0x09,
4191                         0x02, 0x0c, 0x09, 0x0c, 0x80, 0x09, 0x02, 0x0d, 0x28,
4192                         0x00, 0x09, 0x02, 0x0e, 0x28, 0x01,
4193                         0x00 }; /* no continuation */
4194
4195 static void hid_sdp_cid_hook_cb(const void *data, uint16_t len, void *user_data)
4196 {
4197         struct test_data *t_data = tester_get_data();
4198         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
4199
4200         if (!memcmp(did_req_pdu, data, len)) {
4201                 bthost_send_cid(bthost, t_data->sdp_handle, t_data->sdp_cid,
4202                                         did_rsp_pdu, sizeof(did_rsp_pdu));
4203                 return;
4204         }
4205
4206         bthost_send_cid(bthost, t_data->sdp_handle, t_data->sdp_cid,
4207                                         hid_rsp_pdu, sizeof(hid_rsp_pdu));
4208 }
4209
4210 static void hid_sdp_search_cb(uint16_t handle, uint16_t cid, void *user_data)
4211 {
4212         struct test_data *data = tester_get_data();
4213         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
4214
4215         data->sdp_handle = handle;
4216         data->sdp_cid = cid;
4217
4218         bthost_add_cid_hook(bthost, handle, cid, hid_sdp_cid_hook_cb, NULL);
4219 }
4220
4221 static void emu_powered_complete(uint16_t opcode, uint8_t status,
4222                                         const void *param, uint8_t len,
4223                                         void *user_data)
4224 {
4225         struct test_data *data = tester_get_data();
4226         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4227         bt_bdaddr_t bdaddr;
4228         bt_status_t bt_status;
4229
4230         switch (opcode) {
4231         case BT_HCI_CMD_WRITE_SCAN_ENABLE:
4232         case BT_HCI_CMD_LE_SET_ADV_ENABLE:
4233                 break;
4234         default:
4235                 return;
4236         }
4237
4238         if (status) {
4239                 tester_setup_failed();
4240                 return;
4241         }
4242
4243         data->cb_count = 0;
4244         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4245         bt_status = data->if_hid->connect(&bdaddr);
4246         if (bt_status != BT_STATUS_SUCCESS)
4247                 tester_setup_failed();
4248 }
4249
4250 static void setup_hidhost_connect(const void *test_data)
4251 {
4252         struct test_data *data = tester_get_data();
4253         struct bthost *bthost;
4254
4255         if (!setup_hidhost(test_data)) {
4256                 tester_setup_failed();
4257                 return;
4258         }
4259
4260         bthost = hciemu_client_get_host(data->hciemu);
4261
4262         /* Emulate SDP (PSM = 1) */
4263         bthost_add_l2cap_server(bthost, 1, hid_sdp_search_cb, NULL);
4264         /* Emulate Control Channel (PSM = 17) */
4265         bthost_add_l2cap_server(bthost, 17, hid_ctrl_connect_cb, NULL);
4266         /* Emulate Interrupt Channel (PSM = 19) */
4267         bthost_add_l2cap_server(bthost, 19, hid_intr_connect_cb, NULL);
4268
4269         bthost_set_cmd_complete_cb(bthost, emu_powered_complete, data);
4270         bthost_write_scan_enable(bthost, 0x03);
4271 }
4272
4273 static void hid_discon_cb(bt_bdaddr_t *bd_addr, bthh_connection_state_t state)
4274 {
4275         if (state == BTHH_CONN_STATE_DISCONNECTED)
4276                 tester_test_passed();
4277 }
4278
4279 static const struct hidhost_generic_data hidhost_test_disconnect = {
4280         .expected_hal_cb.connection_state_cb = hid_discon_cb,
4281 };
4282
4283 static void test_hidhost_disconnect(const void *test_data)
4284 {
4285         struct test_data *data = tester_get_data();
4286         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4287         bt_bdaddr_t bdaddr;
4288         bt_status_t bt_status;
4289
4290         data->cb_count = 0;
4291         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4292         bt_status = data->if_hid->disconnect(&bdaddr);
4293         if (bt_status != BT_STATUS_SUCCESS)
4294                 tester_test_failed();
4295 }
4296
4297 static void test_hidhost_virtual_unplug(const void *test_data)
4298 {
4299         struct test_data *data = tester_get_data();
4300         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4301         bt_bdaddr_t bdaddr;
4302         bt_status_t bt_status;
4303
4304         data->cb_count = 0;
4305         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4306         bt_status = data->if_hid->virtual_unplug(&bdaddr);
4307         if (bt_status != BT_STATUS_SUCCESS)
4308                 tester_test_failed();
4309 }
4310
4311 static void hid_protocol_mode_cb(bt_bdaddr_t *bd_addr, bthh_status_t status,
4312                                                 bthh_protocol_mode_t mode)
4313 {
4314         struct test_data *data = tester_get_data();
4315         const struct hidhost_generic_data *test = data->test_data;
4316
4317         if (data->cb_count == test->expected_cb_count &&
4318                                         status == test->expected_status &&
4319                                         mode == test->expected_protocol_mode)
4320                 tester_test_passed();
4321         else
4322                 tester_test_failed();
4323 }
4324
4325 static const struct hidhost_generic_data hidhost_test_get_protocol = {
4326         .expected_hal_cb.protocol_mode_cb = hid_protocol_mode_cb,
4327         .expected_cb_count = 1,
4328         .expected_protocol_mode = BTHH_BOOT_MODE,
4329         .expected_status = BTHH_OK,
4330 };
4331
4332 static void test_hidhost_get_protocol(const void *test_data)
4333 {
4334         struct test_data *data = tester_get_data();
4335         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4336         bt_bdaddr_t bdaddr;
4337         bt_status_t bt_status;
4338
4339         data->cb_count = 0;
4340         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4341         bt_status = data->if_hid->get_protocol(&bdaddr, BTHH_REPORT_MODE);
4342         if (bt_status != BT_STATUS_SUCCESS)
4343                 tester_test_failed();
4344 }
4345
4346 static void test_hidhost_set_protocol(const void *test_data)
4347 {
4348         struct test_data *data = tester_get_data();
4349         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4350         bt_bdaddr_t bdaddr;
4351         bt_status_t bt_status;
4352
4353         data->cb_count = 0;
4354         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4355         bt_status = data->if_hid->set_protocol(&bdaddr, BTHH_REPORT_MODE);
4356         if (bt_status != BT_STATUS_SUCCESS)
4357                 tester_test_failed();
4358 }
4359
4360 static void test_hidhost_set_report(const void *test_data)
4361 {
4362         struct test_data *data = tester_get_data();
4363         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4364         bt_bdaddr_t bdaddr;
4365         bt_status_t bt_status;
4366         char *buf = "010101";
4367
4368         data->cb_count = 0;
4369         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4370         bt_status = data->if_hid->set_report(&bdaddr, BTHH_INPUT_REPORT, buf);
4371         if (bt_status != BT_STATUS_SUCCESS)
4372                 tester_test_failed();
4373 }
4374
4375 static void test_hidhost_send_data(const void *test_data)
4376 {
4377         struct test_data *data = tester_get_data();
4378         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4379         bt_bdaddr_t bdaddr;
4380         bt_status_t bt_status;
4381         char *buf = "fe0201";
4382
4383         data->cb_count = 0;
4384         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4385         bt_status = data->if_hid->send_data(&bdaddr, buf);
4386         if (bt_status != BT_STATUS_SUCCESS)
4387                 tester_test_failed();
4388 }
4389
4390 static void hid_get_report_cb(bt_bdaddr_t *bd_addr, bthh_status_t status,
4391                                                 uint8_t *report, int size)
4392 {
4393         struct test_data *data = tester_get_data();
4394         const struct hidhost_generic_data *test = data->test_data;
4395
4396         if (data->cb_count == test->expected_cb_count &&
4397                                         status == test->expected_status &&
4398                                         size == test->expected_report_size)
4399                 tester_test_passed();
4400         else
4401                 tester_test_failed();
4402 }
4403
4404 static const struct hidhost_generic_data hidhost_test_get_report = {
4405         .expected_hal_cb.get_report_cb = hid_get_report_cb,
4406         .expected_cb_count = 1,
4407         .expected_status = BTHH_OK,
4408         .expected_report_size = 2,
4409 };
4410
4411 static void test_hidhost_get_report(const void *test_data)
4412 {
4413         struct test_data *data = tester_get_data();
4414         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
4415         bt_bdaddr_t bdaddr;
4416         bt_status_t bt_status;
4417
4418         data->cb_count = 0;
4419         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
4420         bt_status = data->if_hid->get_report(&bdaddr, BTHH_INPUT_REPORT, 1, 20);
4421         if (bt_status != BT_STATUS_SUCCESS)
4422                 tester_test_failed();
4423 }
4424
4425 #define test_bredrle(name, data, test_setup, test, test_teardown) \
4426         do { \
4427                 struct test_data *user; \
4428                 user = g_malloc0(sizeof(struct test_data)); \
4429                 if (!user) \
4430                         break; \
4431                 user->hciemu_type = HCIEMU_TYPE_BREDRLE; \
4432                 user->test_data = data; \
4433                 tester_add_full(name, data, test_pre_setup, test_setup, \
4434                                 test, test_teardown, test_post_teardown, \
4435                                                         3, user, g_free); \
4436         } while (0)
4437
4438 int main(int argc, char *argv[])
4439 {
4440         snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));
4441
4442         tester_init(&argc, &argv);
4443
4444         test_bredrle("Bluetooth Init", NULL, setup_base, test_dummy, teardown);
4445
4446         test_bredrle("Bluetooth Enable - Success",
4447                                                 &bluetooth_enable_success_test,
4448                                                 setup_base, test_enable,
4449                                                 teardown);
4450
4451         test_bredrle("Bluetooth Enable - Success 2",
4452                                                 &bluetooth_enable_success2_test,
4453                                                 setup_enabled_adapter,
4454                                                 test_enable_done, teardown);
4455
4456         test_bredrle("Bluetooth Disable - Success",
4457                                                 &bluetooth_disable_success_test,
4458                                                 setup_enabled_adapter,
4459                                                 test_disable, teardown);
4460
4461         test_bredrle("Bluetooth Set BDNAME - Success",
4462                                         &bluetooth_setprop_bdname_success_test,
4463                                         setup_enabled_adapter,
4464                                         test_setprop_bdname_success, teardown);
4465
4466         test_bredrle("Bluetooth Set SCAN_MODE - Success",
4467                                 &bluetooth_setprop_scanmode_success_test,
4468                                 setup_enabled_adapter,
4469                                 test_setprop_scanmode_succes, teardown);
4470
4471         test_bredrle("Bluetooth Set DISCOVERY_TIMEOUT - Success",
4472                                 &bluetooth_setprop_disctimeout_success_test,
4473                                 setup_enabled_adapter,
4474                                 test_setprop_disctimeout_succes, teardown);
4475
4476         test_bredrle("Bluetooth Get BDADDR - Success",
4477                                         &bluetooth_getprop_bdaddr_success_test,
4478                                         setup_enabled_adapter,
4479                                         test_getprop_bdaddr_success, teardown);
4480
4481         test_bredrle("Bluetooth Get BDNAME - Success",
4482                                         &bluetooth_getprop_bdname_success_test,
4483                                         setup_enabled_adapter,
4484                                         test_getprop_bdname_success, teardown);
4485
4486         test_bredrle("Bluetooth Set UUID - Invalid",
4487                                         &bluetooth_setprop_uuid_invalid_test,
4488                                         setup_enabled_adapter,
4489                                         test_setprop_uuid_invalid, teardown);
4490
4491         test_bredrle("Bluetooth Set CLASS_OF_DEVICE - Invalid",
4492                                         &bluetooth_setprop_cod_invalid_test,
4493                                         setup_enabled_adapter,
4494                                         test_setprop_cod_invalid, teardown);
4495
4496         test_bredrle("Bluetooth Set TYPE_OF_DEVICE - Invalid",
4497                                         &bluetooth_setprop_tod_invalid_test,
4498                                         setup_enabled_adapter,
4499                                         test_setprop_tod_invalid, teardown);
4500
4501         test_bredrle("Bluetooth Set REMOTE_RSSI - Invalid",
4502                                 &bluetooth_setprop_remote_rssi_invalid_test,
4503                                 setup_enabled_adapter,
4504                                 test_setprop_rssi_invalid, teardown);
4505
4506         test_bredrle("Bluetooth Set SERVICE_RECORD - Invalid",
4507                                 &bluetooth_setprop_service_record_invalid_test,
4508                                 setup_enabled_adapter,
4509                                 test_setprop_service_record_invalid, teardown);
4510
4511         test_bredrle("Bluetooth Set BDADDR - Invalid",
4512                                         &bluetooth_setprop_bdaddr_invalid_test,
4513                                         setup_enabled_adapter,
4514                                         test_setprop_bdaddr_invalid, teardown);
4515
4516         test_bredrle("Bluetooth Set SCAN_MODE CONNECTABLE - Success",
4517                         &bluetooth_setprop_scanmode_connectable_success_test,
4518                         setup_enabled_adapter,
4519                         test_setprop_scanmode_connectable_success, teardown);
4520
4521         test_bredrle("Bluetooth Set BONDED_DEVICES - Invalid",
4522                                 &bluetooth_setprop_bonded_devices_invalid_test,
4523                                 setup_enabled_adapter,
4524                                 test_setprop_bonded_devices_invalid, teardown);
4525
4526         test_bredrle("Bluetooth Get CLASS_OF_DEVICE - Success",
4527                                         &bluetooth_getprop_cod_success_test,
4528                                         setup_enabled_adapter,
4529                                         test_getprop_cod_success, teardown);
4530
4531         test_bredrle("Bluetooth Get TYPE_OF_DEVICE - Success",
4532                                         &bluetooth_getprop_tod_success_test,
4533                                         setup_enabled_adapter,
4534                                         test_getprop_tod_success, teardown);
4535
4536         test_bredrle("Bluetooth Get SCAN_MODE - Success",
4537                                 &bluetooth_getprop_scanmode_success_test,
4538                                 setup_enabled_adapter,
4539                                 test_getprop_scanmode_success, teardown);
4540
4541         test_bredrle("Bluetooth Get DISCOVERY_TIMEOUT - Success",
4542                                 &bluetooth_getprop_disctimeout_success_test,
4543                                 setup_enabled_adapter,
4544                                 test_getprop_disctimeout_success, teardown);
4545
4546         test_bredrle("Bluetooth Get UUIDS - Success",
4547                                         &bluetooth_getprop_uuids_success_test,
4548                                         setup_enabled_adapter,
4549                                         test_getprop_uuids_success, teardown);
4550
4551         test_bredrle("Bluetooth Get BONDED_DEVICES - Success",
4552                                 &bluetooth_getprop_bondeddev_success_test,
4553                                 setup_enabled_adapter,
4554                                 test_getprop_bondeddev_success, teardown);
4555
4556         test_bredrle("Bluetooth Set SCAN_MODE NONE - Success 2",
4557                                 &bluetooth_setprop_scanmode_none_success2_test,
4558                                 setup_enabled_adapter,
4559                                 test_setprop_scanmode_none_done, teardown);
4560
4561         test_bredrle("Bluetooth BR/EDR Discovery Start - Success",
4562                                 &bluetooth_discovery_start_success_test,
4563                                 setup_enabled_adapter,
4564                                 test_discovery_start_success, teardown);
4565
4566         test_bredrle("Bluetooth BR/EDR Discovery Start - Success 2",
4567                                 &bluetooth_discovery_start_success2_test,
4568                                 setup_enabled_adapter,
4569                                 test_discovery_start_done, teardown);
4570
4571         test_bredrle("Bluetooth BR/EDR Discovery Stop - Success",
4572                                 &bluetooth_discovery_stop_success_test,
4573                                 setup_enabled_adapter,
4574                                 test_discovery_stop_success, teardown);
4575
4576         test_bredrle("Bluetooth BR/EDR Discovery Stop - Success 2",
4577                                 &bluetooth_discovery_stop_success2_test,
4578                                 setup_enabled_adapter,
4579                                 test_discovery_stop_done, teardown);
4580
4581         test_bredrle("Bluetooth BR/EDR Discovery Device Found",
4582                                 &bluetooth_discovery_device_found_test,
4583                                 setup_enabled_adapter,
4584                                 test_discovery_device_found, teardown);
4585
4586         test_bredrle("Bluetooth Device Get Props - Success",
4587                                         &bt_dev_getprops_success_test,
4588                                         setup_enabled_adapter,
4589                                         test_dev_getprops_success, teardown);
4590
4591         test_bredrle("Bluetooth Device Get BDNAME - Success",
4592                                 &bt_dev_getprop_bdname_success_test,
4593                                 setup_enabled_adapter,
4594                                 test_dev_getprop_bdname_success, teardown);
4595
4596         test_bredrle("Bluetooth Device Get UUIDS - Success",
4597                                 &bt_dev_getprop_uuids_success_test,
4598                                 setup_enabled_adapter,
4599                                 test_dev_getprop_uuids_success, teardown);
4600
4601         test_bredrle("Bluetooth Device Get COD - Success",
4602                                         &bt_dev_getprop_cod_success_test,
4603                                         setup_enabled_adapter,
4604                                         test_dev_getprop_cod_success, teardown);
4605
4606         test_bredrle("Bluetooth Device Get TOD - Success",
4607                                         &bt_dev_getprop_tod_success_test,
4608                                         setup_enabled_adapter,
4609                                         test_dev_getprop_tod_success, teardown);
4610
4611         test_bredrle("Bluetooth Device Get RSSI - Success",
4612                                 &bt_dev_getprop_rssi_success_test,
4613                                 setup_enabled_adapter,
4614                                 test_dev_getprop_rssi_success, teardown);
4615
4616         test_bredrle("Bluetooth Device Get TIMESTAMP - Success",
4617                                 &bt_dev_getprop_timpestamp_success_test,
4618                                 setup_enabled_adapter,
4619                                 test_dev_getprop_timestamp_success, teardown);
4620
4621         test_bredrle("Bluetooth Device Get BDADDR - Fail",
4622                                 &bt_dev_getprop_bdaddr_fail_test,
4623                                 setup_enabled_adapter,
4624                                 test_dev_getprop_bdaddr_fail, teardown);
4625
4626         test_bredrle("Bluetooth Device Get SERVICE_RECORD - Fail",
4627                                 &bt_dev_getprop_servrec_fail_test,
4628                                 setup_enabled_adapter,
4629                                 test_dev_getprop_servrec_fail, teardown);
4630
4631         test_bredrle("Bluetooth Device Get SCAN_MODE - Fail",
4632                                 &bt_dev_getprop_scanmode_fail_test,
4633                                 setup_enabled_adapter,
4634                                 test_dev_getprop_scanmode_fail, teardown);
4635
4636         test_bredrle("Bluetooth Device Get BONDED_DEVICES - Fail",
4637                                 &bt_dev_getprop_bondeddev_fail_test,
4638                                 setup_enabled_adapter,
4639                                 test_dev_getprop_bondeddev_fail, teardown);
4640
4641         test_bredrle("Bluetooth Device Get DISCOVERY_TIMEOUT - Fail",
4642                                 &bt_dev_getprop_disctimeout_fail_test,
4643                                 setup_enabled_adapter,
4644                                 test_dev_getprop_disctimeout_fail, teardown);
4645
4646         test_bredrle("Bluetooth Device Get VERSION_INFO - Fail",
4647                                 &bt_dev_getprop_verinfo_fail_test,
4648                                 setup_enabled_adapter,
4649                                 test_dev_getprop_verinfo_fail, teardown);
4650
4651         test_bredrle("Bluetooth Device Get FRIENDLY_NAME - Fail",
4652                                         &bt_dev_getprop_fname_fail_test,
4653                                         setup_enabled_adapter,
4654                                         test_dev_getprop_fname_fail, teardown);
4655
4656         test_bredrle("Bluetooth Device Set FRIENDLY_NAME - Success",
4657                                 &bt_dev_setprop_fname_success_test,
4658                                 setup_enabled_adapter,
4659                                 test_dev_setprop_fname_success, teardown);
4660
4661         test_bredrle("Bluetooth Device Set BDNAME - Fail",
4662                                         &bt_dev_setprop_bdname_fail_test,
4663                                         setup_enabled_adapter,
4664                                         test_dev_setprop_bdname_fail, teardown);
4665
4666         test_bredrle("Bluetooth Device Set UUIDS - Fail",
4667                                         &bt_dev_setprop_uuids_fail_test,
4668                                         setup_enabled_adapter,
4669                                         test_dev_setprop_uuids_fail, teardown);
4670
4671         test_bredrle("Bluetooth Device Set COD - Fail",
4672                                         &bt_dev_setprop_cod_fail_test,
4673                                         setup_enabled_adapter,
4674                                         test_dev_setprop_cod_fail, teardown);
4675
4676         test_bredrle("Bluetooth Device Set TOD - Fail",
4677                                         &bt_dev_setprop_tod_fail_test,
4678                                         setup_enabled_adapter,
4679                                         test_dev_setprop_tod_fail, teardown);
4680
4681         test_bredrle("Bluetooth Device Set RSSI - Fail",
4682                                 &bt_dev_setprop_rssi_fail_test,
4683                                 setup_enabled_adapter,
4684                                 test_dev_setprop_rssi_fail, teardown);
4685
4686         test_bredrle("Bluetooth Device Set TIMESTAMP - Fail",
4687                                 &bt_dev_setprop_timpestamp_fail_test,
4688                                 setup_enabled_adapter,
4689                                 test_dev_setprop_timestamp_fail, teardown);
4690
4691         test_bredrle("Bluetooth Device Set BDADDR - Fail",
4692                                 &bt_dev_setprop_bdaddr_fail_test,
4693                                 setup_enabled_adapter,
4694                                 test_dev_setprop_bdaddr_fail, teardown);
4695
4696         test_bredrle("Bluetooth Device Set SERVICE_RECORD - Fail",
4697                                 &bt_dev_setprop_servrec_fail_test,
4698                                 setup_enabled_adapter,
4699                                 test_dev_setprop_servrec_fail, teardown);
4700
4701         test_bredrle("Bluetooth Device Set SCAN_MODE - Fail",
4702                                 &bt_dev_setprop_scanmode_fail_test,
4703                                 setup_enabled_adapter,
4704                                 test_dev_setprop_scanmode_fail, teardown);
4705
4706         test_bredrle("Bluetooth Device Set BONDED_DEVICES - Fail",
4707                                 &bt_dev_setprop_bondeddev_fail_test,
4708                                 setup_enabled_adapter,
4709                                 test_dev_setprop_bondeddev_fail, teardown);
4710
4711         test_bredrle("Bluetooth Device Set DISCOVERY_TIMEOUT - Fail",
4712                                 &bt_dev_setprop_disctimeout_fail_test,
4713                                 setup_enabled_adapter,
4714                                 test_dev_setprop_disctimeout_fail, teardown);
4715
4716         test_bredrle("Bluetooth Create Bond PIN - Success",
4717                                         &bt_bond_create_pin_success_test,
4718                                         setup_enabled_adapter,
4719                                         test_bond_create_pin_success, teardown);
4720
4721         test_bredrle("Bluetooth Create Bond PIN - Bad PIN",
4722                                         &bt_bond_create_pin_fail_test,
4723                                         setup_enabled_adapter,
4724                                         test_bond_create_pin_fail, teardown);
4725
4726         test_bredrle("Bluetooth Create Bond SSP - Success",
4727                                         &bt_bond_create_ssp_success_test,
4728                                         setup_enabled_adapter,
4729                                         test_bond_create_ssp_success, teardown);
4730
4731         test_bredrle("Bluetooth Create Bond SSP - Negative reply",
4732                                         &bt_bond_create_ssp_fail_test,
4733                                         setup_enabled_adapter,
4734                                         test_bond_create_ssp_fail, teardown);
4735
4736         test_bredrle("Bluetooth Create Bond - No Discovery",
4737                                 &bt_bond_create_no_disc_success_test,
4738                                 setup_enabled_adapter,
4739                                 test_bond_create_no_disc_success, teardown);
4740
4741         test_bredrle("Bluetooth Create Bond - Bad Address",
4742                                 &bt_bond_create_bad_addr_success_test,
4743                                 setup_enabled_adapter,
4744                                 test_bond_create_bad_addr_success, teardown);
4745
4746         test_bredrle("Bluetooth Cancel Bonding - Success",
4747                                         &bt_bond_cancel_success_test,
4748                                         setup_enabled_adapter,
4749                                         test_bond_cancel_success, teardown);
4750
4751         test_bredrle("Bluetooth Remove Bond - Success",
4752                                         &bt_bond_remove_success_test,
4753                                         setup_enabled_adapter,
4754                                         test_bond_remove_success, teardown);
4755
4756         test_bredrle("Socket Init", NULL, setup_socket_interface,
4757                                                 test_dummy, teardown);
4758
4759         test_bredrle("Socket Listen - Invalid: sock_type 0",
4760                         &btsock_inv_param_socktype, setup_socket_interface,
4761                         test_generic_listen, teardown);
4762
4763         test_bredrle("Socket Listen - Invalid: sock_type L2CAP",
4764                         &btsock_inv_param_socktype_l2cap,
4765                         setup_socket_interface, test_generic_listen, teardown);
4766
4767         test_bredrle("Socket Listen - Invalid: chan, uuid",
4768                         &btsock_inv_params_chan_uuid,
4769                         setup_socket_interface, test_generic_listen, teardown);
4770
4771         test_bredrle("Socket Listen - Check returned fd valid",
4772                         &btsock_success,
4773                         setup_socket_interface, test_generic_listen, teardown);
4774
4775         test_bredrle("Socket Listen - Check returned channel",
4776                         &btsock_success_check_chan,
4777                         setup_socket_interface, test_generic_listen, teardown);
4778
4779         test_bredrle("Socket Listen - Close and Listen again",
4780                         &btsock_success_check_chan,
4781                         setup_socket_interface, test_listen_close, teardown);
4782
4783         test_bredrle("Socket Listen - Invalid: double Listen",
4784                         &btsock_inv_listen_listen,
4785                         setup_socket_interface, test_listen_listen, teardown);
4786
4787         test_bredrle("Socket Connect - Check returned fd valid",
4788                         &btsock_success, setup_socket_interface,
4789                         test_generic_connect, teardown);
4790
4791         test_bredrle("Socket Connect - Invalid: sock_type 0",
4792                         &btsock_inv_param_socktype, setup_socket_interface,
4793                         test_generic_connect, teardown);
4794
4795         test_bredrle("Socket Connect - Invalid: sock_type L2CAP",
4796                         &btsock_inv_param_socktype_l2cap,
4797                         setup_socket_interface, test_generic_connect, teardown);
4798
4799         test_bredrle("Socket Connect - Invalid: chan, uuid",
4800                         &btsock_inv_params_chan_uuid,
4801                         setup_socket_interface, test_generic_connect, teardown);
4802
4803         test_bredrle("Socket Connect - Invalid: bdaddr",
4804                         &btsock_inv_param_bdaddr,
4805                         setup_socket_interface, test_generic_connect, teardown);
4806
4807         test_bredrle("Socket Connect - Check returned chan",
4808                         &btsock_success_check_chan,
4809                         setup_socket_interface_enabled,
4810                         test_socket_real_connect, teardown);
4811
4812         test_bredrle("HIDHost Init", NULL, setup_hidhost_interface,
4813                                                 test_dummy, teardown);
4814
4815         test_bredrle("HIDHost Connect Success",
4816                                 NULL, setup_hidhost_connect,
4817                                 test_dummy, teardown);
4818
4819         test_bredrle("HIDHost Disconnect Success",
4820                                 &hidhost_test_disconnect, setup_hidhost_connect,
4821                                 test_hidhost_disconnect, teardown);
4822
4823         test_bredrle("HIDHost VirtualUnplug Success",
4824                                 &hidhost_test_disconnect, setup_hidhost_connect,
4825                                 test_hidhost_virtual_unplug, teardown);
4826
4827         test_bredrle("HIDHost GetProtocol Success",
4828                         &hidhost_test_get_protocol, setup_hidhost_connect,
4829                                 test_hidhost_get_protocol, teardown);
4830
4831         test_bredrle("HIDHost SetProtocol Success",
4832                         &hidhost_test_get_protocol, setup_hidhost_connect,
4833                                 test_hidhost_set_protocol, teardown);
4834
4835         test_bredrle("HIDHost GetReport Success",
4836                         &hidhost_test_get_report, setup_hidhost_connect,
4837                                 test_hidhost_get_report, teardown);
4838
4839         test_bredrle("HIDHost SetReport Success",
4840                                 NULL, setup_hidhost_connect,
4841                                 test_hidhost_set_report, teardown);
4842
4843         test_bredrle("HIDHost SendData Success",
4844                                 NULL, setup_hidhost_connect,
4845                                 test_hidhost_send_data, teardown);
4846         return tester_run();
4847 }