Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / tester-main.c
1 /*
2  * Copyright (C) 2014 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 #include <stdbool.h>
18 #include <unistd.h>
19 #include <libgen.h>
20
21 #include <sys/un.h>
22 #include <sys/wait.h>
23 #include <sys/signalfd.h>
24
25 #include "lib/bluetooth.h"
26 #include "lib/mgmt.h"
27 #include "src/shared/util.h"
28 #include "src/shared/tester.h"
29 #include "src/shared/mgmt.h"
30 #include "src/shared/queue.h"
31 #include "emulator/bthost.h"
32 #include "monitor/bt.h"
33 #include "tester-main.h"
34
35 static char exec_dir[PATH_MAX + 1];
36
37 static gint scheduled_cbacks_num;
38
39 #define EMULATOR_SIGNAL_TIMEOUT 2 /* in seconds */
40 #define EMULATOR_SIGNAL "emulator_started"
41
42 #define BT_TRANSPORT_UNKNOWN    0x00
43
44 static struct {
45         uint16_t cb_num;
46         const char *str;
47 } cb_table[] = {
48         DBG_CB(CB_BT_NONE),
49         DBG_CB(CB_BT_ADAPTER_STATE_CHANGED),
50         DBG_CB(CB_BT_ADAPTER_PROPERTIES),
51         DBG_CB(CB_BT_REMOTE_DEVICE_PROPERTIES),
52         DBG_CB(CB_BT_DEVICE_FOUND),
53         DBG_CB(CB_BT_DISCOVERY_STATE_CHANGED),
54         DBG_CB(CB_BT_PIN_REQUEST),
55         DBG_CB(CB_BT_SSP_REQUEST),
56         DBG_CB(CB_BT_BOND_STATE_CHANGED),
57         DBG_CB(CB_BT_ACL_STATE_CHANGED),
58         DBG_CB(CB_BT_THREAD_EVT),
59         DBG_CB(CB_BT_DUT_MODE_RECV),
60         DBG_CB(CB_BT_LE_TEST_MODE),
61
62         /* Hidhost cb */
63         DBG_CB(CB_HH_CONNECTION_STATE),
64         DBG_CB(CB_HH_HID_INFO),
65         DBG_CB(CB_HH_PROTOCOL_MODE),
66         DBG_CB(CB_HH_IDLE_TIME),
67         DBG_CB(CB_HH_GET_REPORT),
68         DBG_CB(CB_HH_VIRTUAL_UNPLUG),
69
70         /* PAN cb */
71         DBG_CB(CB_PAN_CONTROL_STATE),
72         DBG_CB(CB_PAN_CONNECTION_STATE),
73
74         /* HDP cb */
75         DBG_CB(CB_HDP_APP_REG_STATE),
76         DBG_CB(CB_HDP_CHANNEL_STATE),
77
78         /* A2DP cb */
79         DBG_CB(CB_A2DP_CONN_STATE),
80         DBG_CB(CB_A2DP_AUDIO_STATE),
81
82         /* AVRCP */
83         DBG_CB(CB_AVRCP_PLAY_STATUS_REQ),
84         DBG_CB(CB_AVRCP_PLAY_STATUS_RSP),
85         DBG_CB(CB_AVRCP_REG_NOTIF_REQ),
86         DBG_CB(CB_AVRCP_REG_NOTIF_RSP),
87         DBG_CB(CB_AVRCP_GET_ATTR_REQ),
88         DBG_CB(CB_AVRCP_GET_ATTR_RSP),
89
90         /* Gatt client */
91         DBG_CB(CB_GATTC_REGISTER_CLIENT),
92         DBG_CB(CB_GATTC_SCAN_RESULT),
93         DBG_CB(CB_GATTC_OPEN),
94         DBG_CB(CB_GATTC_CLOSE),
95         DBG_CB(CB_GATTC_SEARCH_COMPLETE),
96         DBG_CB(CB_GATTC_SEARCH_RESULT),
97         DBG_CB(CB_GATTC_GET_CHARACTERISTIC),
98         DBG_CB(CB_GATTC_GET_DESCRIPTOR),
99         DBG_CB(CB_GATTC_GET_INCLUDED_SERVICE),
100         DBG_CB(CB_GATTC_REGISTER_FOR_NOTIFICATION),
101         DBG_CB(CB_GATTC_NOTIFY),
102         DBG_CB(CB_GATTC_READ_CHARACTERISTIC),
103         DBG_CB(CB_GATTC_WRITE_CHARACTERISTIC),
104         DBG_CB(CB_GATTC_READ_DESCRIPTOR),
105         DBG_CB(CB_GATTC_WRITE_DESCRIPTOR),
106         DBG_CB(CB_GATTC_EXECUTE_WRITE),
107         DBG_CB(CB_GATTC_READ_REMOTE_RSSI),
108         DBG_CB(CB_GATTC_LISTEN),
109
110         /* Gatt server */
111         DBG_CB(CB_GATTS_REGISTER_SERVER),
112         DBG_CB(CB_GATTS_CONNECTION),
113         DBG_CB(CB_GATTS_SERVICE_ADDED),
114         DBG_CB(CB_GATTS_INCLUDED_SERVICE_ADDED),
115         DBG_CB(CB_GATTS_CHARACTERISTIC_ADDED),
116         DBG_CB(CB_GATTS_DESCRIPTOR_ADDED),
117         DBG_CB(CB_GATTS_SERVICE_STARTED),
118         DBG_CB(CB_GATTS_SERVICE_STOPPED),
119         DBG_CB(CB_GATTS_SERVICE_DELETED),
120         DBG_CB(CB_GATTS_REQUEST_READ),
121         DBG_CB(CB_GATTS_REQUEST_WRITE),
122         DBG_CB(CB_GATTS_REQUEST_EXEC_WRITE),
123         DBG_CB(CB_GATTS_RESPONSE_CONFIRMATION),
124         DBG_CB(CB_GATTS_INDICATION_SEND),
125
126         /* Map client */
127         DBG_CB(CB_MAP_CLIENT_REMOTE_MAS_INSTANCES),
128
129         /* Emulator callbacks */
130         DBG_CB(CB_EMU_CONFIRM_SEND_DATA),
131         DBG_CB(CB_EMU_ENCRYPTION_ENABLED),
132         DBG_CB(CB_EMU_ENCRYPTION_DISABLED),
133         DBG_CB(CB_EMU_CONNECTION_REJECTED),
134         DBG_CB(CB_EMU_VALUE_INDICATION),
135         DBG_CB(CB_EMU_VALUE_NOTIFICATION),
136         DBG_CB(CB_EMU_READ_RESPONSE),
137         DBG_CB(CB_EMU_WRITE_RESPONSE),
138         DBG_CB(CB_EMU_ATT_ERROR),
139 };
140
141 static gboolean check_callbacks_called(gpointer user_data)
142 {
143         /*
144          * Wait for all callbacks scheduled in current test context to execute
145          * in main loop. This will avoid late callback calls after test case has
146          * already failed or timed out.
147          */
148
149         if (g_atomic_int_get(&scheduled_cbacks_num) == 0) {
150                 tester_teardown_complete();
151                 return FALSE;
152         } else if (scheduled_cbacks_num < 0) {
153                 tester_warn("Unscheduled callback called!");
154                 return FALSE;
155         }
156
157         return TRUE;
158 }
159
160 static void check_daemon_term(void)
161 {
162         int status;
163         pid_t pid;
164         struct test_data *data = tester_get_data();
165
166         if (!data)
167                 return;
168
169         pid = waitpid(data->bluetoothd_pid, &status, WNOHANG);
170         if (pid != data->bluetoothd_pid)
171                 return;
172
173         data->bluetoothd_pid = 0;
174
175         if (WIFEXITED(status) && (WEXITSTATUS(status) == EXIT_SUCCESS)) {
176                 g_idle_add(check_callbacks_called, NULL);
177                 return;
178         }
179
180         tester_warn("Unexpected Daemon shutdown with status %d", status);
181 }
182
183 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
184                                                         gpointer user_data)
185 {
186         struct signalfd_siginfo si;
187         ssize_t result;
188         int fd;
189
190         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
191                 return FALSE;
192
193         fd = g_io_channel_unix_get_fd(channel);
194
195         result = read(fd, &si, sizeof(si));
196         if (result != sizeof(si))
197                 return FALSE;
198
199         switch (si.ssi_signo) {
200         case SIGCHLD:
201                 check_daemon_term();
202                 break;
203         }
204
205         return TRUE;
206 }
207
208 static guint setup_signalfd(void)
209 {
210         GIOChannel *channel;
211         guint source;
212         sigset_t mask;
213         int fd;
214
215         sigemptyset(&mask);
216         sigaddset(&mask, SIGCHLD);
217
218         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
219                 return 0;
220
221         fd = signalfd(-1, &mask, 0);
222         if (fd < 0)
223                 return 0;
224
225         channel = g_io_channel_unix_new(fd);
226
227         g_io_channel_set_close_on_unref(channel, TRUE);
228         g_io_channel_set_encoding(channel, NULL, NULL);
229         g_io_channel_set_buffered(channel, FALSE);
230
231         source = g_io_add_watch(channel,
232                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
233                                 signal_handler, NULL);
234
235         g_io_channel_unref(channel);
236
237         return source;
238 }
239
240 static void test_post_teardown(const void *test_data)
241 {
242         struct test_data *data = tester_get_data();
243
244         /* remove hook for encryption change */
245         hciemu_del_hook(data->hciemu, HCIEMU_HOOK_POST_EVT, 0x08);
246
247         hciemu_unref(data->hciemu);
248         data->hciemu = NULL;
249
250         g_source_remove(data->signalfd);
251         data->signalfd = 0;
252 }
253
254 static void bluetoothd_start(int hci_index)
255 {
256         char prg_name[PATH_MAX + 1];
257         char index[8];
258         char *prg_argv[5];
259
260         snprintf(prg_name, sizeof(prg_name), "%s/%s", exec_dir, "bluetoothd");
261         snprintf(index, sizeof(index), "%d", hci_index);
262
263         prg_argv[0] = prg_name;
264         prg_argv[1] = "-i";
265         prg_argv[2] = index;
266         prg_argv[3] = "-d";
267         prg_argv[4] = NULL;
268
269         if (!tester_use_debug())
270                 fclose(stderr);
271
272         execve(prg_argv[0], prg_argv, NULL);
273 }
274
275 static void emulator(int pipe, int hci_index)
276 {
277         static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
278         char buf[1024];
279         struct sockaddr_un addr;
280         struct timeval tv;
281         int fd;
282         ssize_t len;
283
284         fd = socket(PF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
285         if (fd < 0)
286                 goto failed;
287
288         tv.tv_sec = EMULATOR_SIGNAL_TIMEOUT;
289         tv.tv_usec = 0;
290         setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
291
292         memset(&addr, 0, sizeof(addr));
293         addr.sun_family = AF_UNIX;
294         memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
295
296         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
297                 perror("Failed to bind system socket");
298                 goto failed;
299         }
300
301         len = write(pipe, EMULATOR_SIGNAL, sizeof(EMULATOR_SIGNAL));
302         if (len != sizeof(EMULATOR_SIGNAL))
303                 goto failed;
304
305         memset(buf, 0, sizeof(buf));
306
307         len = read(fd, buf, sizeof(buf));
308         if (len <= 0 || strcmp(buf, "bluetooth.start=daemon"))
309                 goto failed;
310
311         close(pipe);
312         close(fd);
313         return bluetoothd_start(hci_index);
314
315 failed:
316         close(pipe);
317
318         if (fd >= 0)
319                 close(fd);
320 }
321
322 static void mgmt_debug(const char *str, void *user_data)
323 {
324         const char *prefix = user_data;
325
326         tester_print("%s%s", prefix, str);
327 }
328
329 static bool hciemu_post_encr_hook(const void *data, uint16_t len,
330                                                         void *user_data)
331 {
332         struct step *step;
333
334         /*
335          * Expected data: status (1 octet) + conn. handle (2 octets) +
336          * encryption flag (1 octet)
337          */
338         if (len < 4)
339                 return true;
340
341         step = g_new0(struct step, 1);
342
343         step->callback = ((uint8_t *)data)[3] ? CB_EMU_ENCRYPTION_ENABLED :
344                                                 CB_EMU_ENCRYPTION_DISABLED;
345
346         schedule_callback_verification(step);
347         return true;
348 }
349
350 static void read_info_callback(uint8_t status, uint16_t length,
351                                         const void *param, void *user_data)
352 {
353         struct test_data *data = tester_get_data();
354         const struct mgmt_rp_read_info *rp = param;
355         char addr[18];
356         uint16_t manufacturer;
357         uint32_t supported_settings, current_settings;
358
359         tester_print("Read Info callback");
360         tester_print("  Status: 0x%02x", status);
361
362         if (status || !param) {
363                 tester_pre_setup_failed();
364                 return;
365         }
366
367         ba2str(&rp->bdaddr, addr);
368         manufacturer = btohs(rp->manufacturer);
369         supported_settings = btohl(rp->supported_settings);
370         current_settings = btohl(rp->current_settings);
371
372         tester_print("  Address: %s", addr);
373         tester_print("  Version: 0x%02x", rp->version);
374         tester_print("  Manufacturer: 0x%04x", manufacturer);
375         tester_print("  Supported settings: 0x%08x", supported_settings);
376         tester_print("  Current settings: 0x%08x", current_settings);
377         tester_print("  Class: 0x%02x%02x%02x",
378                         rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
379         tester_print("  Name: %s", rp->name);
380         tester_print("  Short name: %s", rp->short_name);
381
382         if (strcmp(hciemu_get_address(data->hciemu), addr)) {
383                 tester_pre_setup_failed();
384                 return;
385         }
386
387         /* set hook for encryption change */
388         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_POST_EVT, 0x08,
389                                                 hciemu_post_encr_hook, NULL);
390
391         tester_pre_setup_complete();
392 }
393
394 static void index_added_callback(uint16_t index, uint16_t length,
395                                         const void *param, void *user_data)
396 {
397         struct test_data *data = tester_get_data();
398
399         tester_print("Index Added callback");
400         tester_print("  Index: 0x%04x", index);
401
402         data->mgmt_index = index;
403
404         mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
405                                         read_info_callback, NULL, NULL);
406 }
407
408 static void index_removed_callback(uint16_t index, uint16_t length,
409                                         const void *param, void *user_data)
410 {
411         struct test_data *data = tester_get_data();
412
413         tester_print("Index Removed callback");
414         tester_print("  Index: 0x%04x", index);
415
416         if (index != data->mgmt_index)
417                 return;
418
419         mgmt_unregister_index(data->mgmt, data->mgmt_index);
420
421         mgmt_unref(data->mgmt);
422         data->mgmt = NULL;
423
424         tester_post_teardown_complete();
425 }
426
427 static void read_index_list_callback(uint8_t status, uint16_t length,
428                                         const void *param, void *user_data)
429 {
430         struct test_data *data = tester_get_data();
431
432         tester_print("Read Index List callback");
433         tester_print("  Status: 0x%02x", status);
434
435         if (status || !param) {
436                 tester_pre_setup_failed();
437                 return;
438         }
439
440         mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
441                                         index_added_callback, NULL, NULL);
442
443         mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
444                                         index_removed_callback, NULL, NULL);
445
446         data->hciemu = hciemu_new(data->hciemu_type);
447         if (!data->hciemu) {
448                 tester_warn("Failed to setup HCI emulation");
449                 tester_pre_setup_failed();
450                 return;
451         }
452
453         tester_print("New hciemu instance created");
454 }
455
456 static void test_pre_setup(const void *test_data)
457 {
458         struct test_data *data = tester_get_data();
459
460         data->signalfd = setup_signalfd();
461         if (!data->signalfd) {
462                 tester_warn("Failed to setup signalfd");
463                 tester_pre_setup_failed();
464                 return;
465         }
466
467         data->mgmt = mgmt_new_default();
468         if (!data->mgmt) {
469                 tester_warn("Failed to setup management interface");
470                 tester_pre_setup_failed();
471                 return;
472         }
473
474         if (!tester_use_debug())
475                 fclose(stderr);
476         else
477                 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
478
479         mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0,
480                                 NULL, read_index_list_callback, NULL, NULL);
481 }
482
483 static bool match_property(bt_property_t *exp_prop, bt_property_t *rec_prop,
484                                                                 int prop_num)
485 {
486         if (exp_prop->type && (exp_prop->type != rec_prop->type))
487                 return 0;
488
489         if (exp_prop->len && (exp_prop->len != rec_prop->len)) {
490                 tester_debug("Property [%d] len don't match! received=%d, "
491                                         "expected=%d", prop_num, rec_prop->len,
492                                         exp_prop->len);
493                 return 0;
494         }
495
496         if (exp_prop->val && memcmp(exp_prop->val, rec_prop->val,
497                                                         exp_prop->len)) {
498                 tester_debug("Property [%d] value don't match!", prop_num);
499                 return 0;
500         }
501
502         return 1;
503 }
504
505 static bool match_mas_inst(btmce_mas_instance_t *exp_inst,
506                                 btmce_mas_instance_t *rec_inst, int inst_num)
507 {
508         if (exp_inst->id && (exp_inst->id != rec_inst->id)) {
509                 tester_debug("MAS inst. [%d] id missmatch %d vs %d", inst_num,
510                                                 rec_inst->id, exp_inst->id);
511                 return 0;
512         }
513
514         if (exp_inst->scn && (exp_inst->scn != rec_inst->scn)) {
515                 tester_debug("MAS inst. [%d] scn missmatch %d vs %d", inst_num,
516                                                 rec_inst->scn, exp_inst->scn);
517                 return 0;
518         }
519
520         if (exp_inst->msg_types &&
521                         (exp_inst->msg_types != rec_inst->msg_types)) {
522                 tester_debug("Mas inst. [%d] mesg type missmatch %d vs %d",
523                                         inst_num, rec_inst->scn, exp_inst->scn);
524                 return 0;
525         }
526
527         if (exp_inst->p_name && memcmp(exp_inst->p_name, rec_inst->p_name,
528                                                 strlen(exp_inst->p_name))) {
529                 tester_debug("Mas inst. [%d] name don't match!", inst_num);
530                 return 0;
531         }
532
533         return 1;
534 }
535
536 static int verify_property(bt_property_t *exp_props, int exp_num_props,
537                                 bt_property_t *rec_props, int rec_num_props)
538 {
539         int i, j;
540         int exp_prop_to_find = exp_num_props;
541
542         if (rec_num_props == 0)
543                 return 1;
544
545         if (exp_num_props == 0) {
546                 tester_debug("Wrong number of expected properties given");
547                 tester_test_failed();
548                 return 1;
549         }
550
551         /* Get first exp prop to match and search for it */
552         for (i = 0; i < exp_num_props; i++) {
553                 for (j = 0; j < rec_num_props; j++) {
554                         if (match_property(&exp_props[i], &rec_props[j], i)) {
555                                 exp_prop_to_find--;
556                                 break;
557                         }
558                 }
559         }
560
561         return exp_prop_to_find;
562 }
563
564 static int verify_mas_inst(btmce_mas_instance_t *exp_inst, int exp_num_inst,
565                                                 btmce_mas_instance_t *rec_inst,
566                                                 int rec_num_inst)
567 {
568         int i, j;
569         int exp_inst_to_find = exp_num_inst;
570
571         if (rec_num_inst == 0)
572                 return 1;
573
574         if (exp_num_inst == 0) {
575                 tester_debug("Wrong number of expected MAS instances given");
576                 tester_test_failed();
577                 return 1;
578         }
579
580         for (i = 0; i < exp_num_inst; i++) {
581                 for (j = 0; j < rec_num_inst; j++) {
582                         if (match_mas_inst(&exp_inst[i], &rec_inst[i], i)) {
583                                 exp_inst_to_find--;
584                                 break;
585                         }
586                 }
587         }
588
589         return exp_inst_to_find;
590 }
591
592 /*
593  * Check each test case step if test case expected
594  * data is set and match it with expected result.
595  */
596
597 static bool verify_gatt_ids(btgatt_gatt_id_t *a, btgatt_gatt_id_t *b)
598 {
599
600         if (memcmp(&a->uuid, &b->uuid, sizeof(bt_uuid_t)))
601                 return false;
602
603         if (a->inst_id != b->inst_id)
604                 return false;
605
606         return true;
607 }
608
609 static bool verify_services(btgatt_srvc_id_t *a, btgatt_srvc_id_t *b)
610 {
611         if (a->is_primary != b->is_primary)
612                 return false;
613
614         return verify_gatt_ids(&a->id, &b->id);
615 }
616
617 static bool match_data(struct step *step)
618 {
619         struct test_data *data = tester_get_data();
620         const struct step *exp;
621
622         exp = queue_peek_head(data->steps);
623
624         if (!exp) {
625                 /* Can occure while test passed already */
626                 tester_debug("Cannot get step to match");
627                 return false;
628         }
629
630         if (exp->action_status != step->action_status) {
631                 tester_debug("Action status don't match");
632                 return false;
633         }
634
635         if (!exp->callback && !step->callback)
636                 return true;
637
638         if (exp->callback != step->callback) {
639                 tester_debug("Callback type mismatch: %s vs %s",
640                                                 cb_table[step->callback].str,
641                                                 cb_table[exp->callback].str);
642                 return false;
643         }
644
645         if (exp->callback_result.state != step->callback_result.state) {
646                 tester_debug("Callback state mismatch: %d vs %d",
647                                                 step->callback_result.state,
648                                                 exp->callback_result.state);
649                 return false;
650         }
651
652         if (exp->callback_result.status != step->callback_result.status) {
653                 tester_debug("Callback status mismatch: %d vs %d",
654                                                 step->callback_result.status,
655                                                 exp->callback_result.status);
656                 return false;
657         }
658
659         if (exp->callback_result.mode != step->callback_result.mode) {
660                 tester_debug("Callback mode mismatch: %02x vs %02x",
661                                                 step->callback_result.mode,
662                                                 exp->callback_result.mode);
663                 return false;
664         }
665
666         if (exp->callback_result.report_size !=
667                                         step->callback_result.report_size) {
668                 tester_debug("Callback report size mismatch: %d vs %d",
669                                         step->callback_result.report_size,
670                                         exp->callback_result.report_size);
671                 return false;
672         }
673
674         if (exp->callback_result.ctrl_state !=
675                                         step->callback_result.ctrl_state) {
676                 tester_debug("Callback ctrl state mismatch: %d vs %d",
677                                         step->callback_result.ctrl_state,
678                                         exp->callback_result.ctrl_state);
679                 return false;
680         }
681
682         if (exp->callback_result.conn_state !=
683                                         step->callback_result.conn_state) {
684                 tester_debug("Callback connection state mismatch: %d vs %d",
685                                         step->callback_result.conn_state,
686                                         exp->callback_result.conn_state);
687                 return false;
688         }
689
690         if (exp->callback_result.local_role !=
691                                         step->callback_result.local_role) {
692                 tester_debug("Callback local_role mismatch: %d vs %d",
693                                         step->callback_result.local_role,
694                                         exp->callback_result.local_role);
695                 return false;
696         }
697
698         if (exp->callback_result.remote_role !=
699                                         step->callback_result.remote_role) {
700                 tester_debug("Callback remote_role mismatch: %d vs %d",
701                                         step->callback_result.remote_role,
702                                         exp->callback_result.remote_role);
703                 return false;
704         }
705
706         if (exp->callback_result.app_id != step->callback_result.app_id) {
707                 tester_debug("Callback app_id mismatch: %d vs %d",
708                                                 step->callback_result.app_id,
709                                                 exp->callback_result.app_id);
710                 return false;
711         }
712
713         if (exp->callback_result.channel_id !=
714                                         step->callback_result.channel_id) {
715                 tester_debug("Callback channel_id mismatch: %d vs %d",
716                                         step->callback_result.channel_id,
717                                         exp->callback_result.channel_id);
718                 return false;
719         }
720
721         if (exp->callback_result.mdep_cfg_index !=
722                                         step->callback_result.mdep_cfg_index) {
723                 tester_debug("Callback mdep_cfg_index mismatch: %d vs %d",
724                                         step->callback_result.mdep_cfg_index,
725                                         exp->callback_result.mdep_cfg_index);
726                 return false;
727         }
728
729         if (exp->callback_result.app_state != step->callback_result.app_state) {
730                 tester_debug("Callback app_state mismatch: %d vs %d",
731                                                 step->callback_result.app_state,
732                                                 exp->callback_result.app_state);
733                 return false;
734         }
735
736         if (exp->callback_result.channel_state !=
737                                         step->callback_result.channel_state) {
738                 tester_debug("Callback channel_state mismatch: %d vs %d",
739                                         step->callback_result.channel_state,
740                                         exp->callback_result.channel_state);
741                 return false;
742         }
743
744         if (exp->callback_result.av_conn_state !=
745                                         step->callback_result.av_conn_state) {
746                 tester_debug("Callback av conn state mismatch: 0x%x vs 0x%x",
747                                         step->callback_result.av_conn_state,
748                                         exp->callback_result.av_conn_state);
749                 return false;
750         }
751
752         if (exp->callback_result.av_audio_state !=
753                                         step->callback_result.av_audio_state) {
754                 tester_debug("Callback av audio state mismatch: 0x%x vs 0x%x",
755                                         step->callback_result.av_audio_state,
756                                         exp->callback_result.av_audio_state);
757                 return false;
758         }
759
760         if (exp->callback_result.song_length !=
761                                         step->callback_result.song_length) {
762                 tester_debug("Callback song_length mismatch: 0x%x vs 0x%x",
763                                         step->callback_result.song_length,
764                                         exp->callback_result.song_length);
765                 return false;
766         }
767
768         if (exp->callback_result.song_position !=
769                                         step->callback_result.song_position) {
770                 tester_debug("Callback song_position mismatch: 0x%x vs 0x%x",
771                                         step->callback_result.song_position,
772                                         exp->callback_result.song_position);
773                 return false;
774         }
775
776         if (exp->callback_result.play_status !=
777                                         step->callback_result.play_status) {
778                 tester_debug("Callback play_status mismatch: 0x%x vs 0x%x",
779                                         step->callback_result.play_status,
780                                         exp->callback_result.play_status);
781                 return false;
782         }
783
784         if (exp->callback_result.rc_index !=
785                                         step->callback_result.rc_index) {
786                 tester_debug("Callback rc_index mismatch");
787                 return false;
788         }
789
790         if (exp->callback_result.num_of_attrs !=
791                                         step->callback_result.num_of_attrs) {
792                 tester_debug("Callback rc num of attrs mismatch");
793                 return false;
794         }
795
796         if (exp->callback_result.attrs) {
797                 if (memcmp(step->callback_result.attrs,
798                                 exp->callback_result.attrs,
799                                 exp->callback_result.num_of_attrs *
800                                 sizeof(btrc_element_attr_val_t))) {
801                         tester_debug("Callback rc element attributes doesn't match");
802                         return false;
803                 }
804         }
805
806         if (exp->callback_result.pairing_variant !=
807                                         step->callback_result.pairing_variant) {
808                 tester_debug("Callback pairing result mismatch: %d vs %d",
809                                         step->callback_result.pairing_variant,
810                                         exp->callback_result.pairing_variant);
811                 return false;
812         }
813
814         if (exp->callback_result.adv_data != step->callback_result.adv_data) {
815                 tester_debug("Callback adv. data status mismatch: %d vs %d",
816                                                 step->callback_result.adv_data,
817                                                 exp->callback_result.adv_data);
818                 return false;
819         }
820
821         if (exp->callback_result.conn_id != step->callback_result.conn_id) {
822                 tester_debug("Callback conn_id mismatch: %d vs %d",
823                                                 step->callback_result.conn_id,
824                                                 exp->callback_result.conn_id);
825                 return false;
826         }
827
828         if (exp->callback_result.gatt_app_id !=
829                                         step->callback_result.gatt_app_id) {
830                 tester_debug("Callback gatt_app_id mismatch: %d vs %d",
831                                         step->callback_result.gatt_app_id,
832                                         exp->callback_result.gatt_app_id);
833                 return false;
834         }
835
836         if (exp->callback_result.properties &&
837                         verify_property(exp->callback_result.properties,
838                                         exp->callback_result.num_properties,
839                                         step->callback_result.properties,
840                                         step->callback_result.num_properties)) {
841                 tester_debug("Gatt properties don't match");
842                 return false;
843         }
844
845         if (exp->callback_result.service &&
846                         !verify_services(step->callback_result.service,
847                                                 exp->callback_result.service)) {
848                 tester_debug("Gatt service doesn't match");
849                 return false;
850         }
851
852         if (exp->callback_result.characteristic) {
853                 btgatt_gatt_id_t *a;
854                 btgatt_gatt_id_t *b;
855                 a = step->callback_result.characteristic;
856                 b = exp->callback_result.characteristic;
857
858                 if (!verify_gatt_ids(a, b)) {
859                         tester_debug("Gatt char doesn't match");
860                         return false;
861                 }
862         }
863
864         if (exp->callback_result.char_prop != step->callback_result.char_prop) {
865                 tester_debug("Gatt char prop mismatch: %d vs %d",
866                                                 step->callback_result.char_prop,
867                                                 exp->callback_result.char_prop);
868                 return false;
869         }
870
871         if (exp->callback_result.descriptor) {
872                 btgatt_gatt_id_t *a;
873                 btgatt_gatt_id_t *b;
874                 a = step->callback_result.descriptor;
875                 b = exp->callback_result.descriptor;
876
877                 if (!verify_gatt_ids(a, b)) {
878                         tester_debug("Gatt desc doesn't match");
879                         return false;
880                 }
881         }
882
883         if (exp->callback_result.included) {
884                 if (!verify_services(step->callback_result.included,
885                                         exp->callback_result.included)) {
886                         tester_debug("Gatt include srvc doesn't match");
887                         return false;
888                 }
889         }
890
891         if (exp->callback_result.read_params) {
892                 if (memcmp(step->callback_result.read_params,
893                                 exp->callback_result.read_params,
894                                 sizeof(btgatt_read_params_t))) {
895                         tester_debug("Gatt read_param doesn't match");
896                         return false;
897                 }
898         }
899
900         if (exp->callback_result.write_params) {
901                 if (memcmp(step->callback_result.write_params,
902                                 exp->callback_result.write_params,
903                                 sizeof(btgatt_write_params_t))) {
904                         tester_debug("Gatt write_param doesn't match");
905                         return false;
906                 }
907
908                 if (exp->callback_result.notification_registered !=
909                                 step->callback_result.notification_registered) {
910                         tester_debug("Gatt registered flag mismatch");
911                         return false;
912                 }
913
914                 if (exp->callback_result.notify_params) {
915                         if (memcmp(step->callback_result.notify_params,
916                                         exp->callback_result.notify_params,
917                                         sizeof(btgatt_notify_params_t))) {
918                                 tester_debug("Gatt notify_param doesn't match");
919                                 return false;
920                         }
921                 }
922         }
923
924         if (exp->callback_result.connected !=
925                                 step->callback_result.connected) {
926                 tester_debug("Gatt server conn status mismatch: %d vs %d",
927                                                 step->callback_result.connected,
928                                                 exp->callback_result.connected);
929                 return false;
930         }
931
932         if (exp->callback_result.attr_handle &&
933                                         step->callback_result.attr_handle)
934                 if (*exp->callback_result.attr_handle !=
935                                         *step->callback_result.attr_handle) {
936                         tester_debug("Gatt attribute handle mismatch: %d vs %d",
937                                         *step->callback_result.attr_handle,
938                                         *exp->callback_result.attr_handle);
939                         return false;
940                 }
941
942         if (exp->callback_result.srvc_handle &&
943                                         step->callback_result.srvc_handle)
944                 if (*exp->callback_result.srvc_handle !=
945                                         *step->callback_result.srvc_handle) {
946                         tester_debug("Gatt service handle mismatch: %d vs %d",
947                                         *step->callback_result.srvc_handle,
948                                         *exp->callback_result.srvc_handle);
949                         return false;
950                 }
951
952         if (exp->callback_result.inc_srvc_handle &&
953                                         step->callback_result.inc_srvc_handle)
954                 if (*exp->callback_result.inc_srvc_handle !=
955                                 *step->callback_result.inc_srvc_handle) {
956                         tester_debug("Gatt inc. srvc handle mismatch: %d vs %d",
957                                         *step->callback_result.inc_srvc_handle,
958                                         *exp->callback_result.inc_srvc_handle);
959                         return false;
960                 }
961
962         if (exp->callback_result.uuid && step->callback_result.uuid)
963                 if (memcmp(exp->callback_result.uuid,
964                                         step->callback_result.uuid,
965                                         sizeof(*exp->callback_result.uuid))) {
966                         tester_debug("Uuid mismatch");
967                         return false;
968                 }
969
970         if (exp->callback_result.trans_id != step->callback_result.trans_id) {
971                 tester_debug("Gatt trans id mismatch: %d vs %d",
972                                                 exp->callback_result.trans_id,
973                                                 step->callback_result.trans_id);
974                 return false;
975         }
976
977         if (exp->callback_result.offset != step->callback_result.offset) {
978                 tester_debug("Gatt offset mismatch: %d vs %d",
979                                                 exp->callback_result.offset,
980                                                 step->callback_result.offset);
981                 return false;
982         }
983
984         if (exp->callback_result.is_long != step->callback_result.is_long) {
985                 tester_debug("Gatt is long attr value flag mismatch: %d vs %d",
986                                                 exp->callback_result.is_long,
987                                                 step->callback_result.is_long);
988                 return false;
989         }
990
991         if (exp->callback_result.length > 0) {
992                 if (exp->callback_result.length !=
993                                                 step->callback_result.length) {
994                         tester_debug("Gatt attr length mismatch: %d vs %d",
995                                                 exp->callback_result.length,
996                                                 step->callback_result.length);
997                         return false;
998                 }
999                 if (!exp->callback_result.value ||
1000                                                 !step->callback_result.value) {
1001                         tester_debug("Gatt attr values are wrong set");
1002                         return false;
1003                 }
1004                 if (!memcmp(exp->callback_result.value,
1005                                                 step->callback_result.value,
1006                                                 exp->callback_result.length)) {
1007                         tester_debug("Gatt attr value mismatch");
1008                         return false;
1009                 }
1010         }
1011
1012         if (exp->callback_result.need_rsp != step->callback_result.need_rsp) {
1013                 tester_debug("Gatt need response value flag mismatch: %d vs %d",
1014                                                 exp->callback_result.need_rsp,
1015                                                 step->callback_result.need_rsp);
1016                 return false;
1017         }
1018
1019         if (exp->callback_result.is_prep != step->callback_result.is_prep) {
1020                 tester_debug("Gatt is prepared value flag mismatch: %d vs %d",
1021                                                 exp->callback_result.is_prep,
1022                                                 step->callback_result.is_prep);
1023                 return false;
1024         }
1025
1026         if (exp->callback_result.num_mas_instances !=
1027                                 step->callback_result.num_mas_instances) {
1028                 tester_debug("Mas instance count mismatch: %d vs %d",
1029                                 exp->callback_result.num_mas_instances,
1030                                 step->callback_result.num_mas_instances);
1031                 return false;
1032         }
1033
1034         if (exp->callback_result.mas_instances &&
1035                 verify_mas_inst(exp->callback_result.mas_instances,
1036                                 exp->callback_result.num_mas_instances,
1037                                 step->callback_result.mas_instances,
1038                                 step->callback_result.num_mas_instances)) {
1039                 tester_debug("Mas instances don't match");
1040                 return false;
1041         }
1042
1043         if (exp->callback_result.error != step->callback_result.error) {
1044                 tester_debug("Err mismatch: %d vs %d",
1045                                 exp->callback_result.error,
1046                                 step->callback_result.error);
1047                 return false;
1048         }
1049
1050         if (exp->store_srvc_handle)
1051                 memcpy(exp->store_srvc_handle,
1052                                         step->callback_result.srvc_handle,
1053                                         sizeof(*exp->store_srvc_handle));
1054
1055         if (exp->store_char_handle)
1056                 memcpy(exp->store_char_handle,
1057                                         step->callback_result.char_handle,
1058                                         sizeof(*exp->store_char_handle));
1059
1060         return true;
1061 }
1062
1063 static void init_test_steps(struct test_data *data)
1064 {
1065         const struct test_case *test_steps = data->test_data;
1066         int i = 0;
1067
1068         for (i = 0; i < test_steps->step_num; i++)
1069                 queue_push_tail(data->steps, (void *) &(test_steps->step[i]));
1070
1071         tester_print("tester: Number of test steps=%d",
1072                                                 queue_length(data->steps));
1073 }
1074
1075 /*
1076  * Each test case step should be verified, if match with
1077  * expected result tester should go to next test step.
1078  */
1079 static void verify_step(struct step *step, queue_destroy_func_t cleanup_cb)
1080 {
1081         struct test_data *data = tester_get_data();
1082         const struct test_case *test_steps = data->test_data;
1083         struct step *next_step;
1084
1085         tester_debug("tester: STEP[%d] check",
1086                         test_steps->step_num-queue_length(data->steps) + 1);
1087
1088         if (step && !match_data(step)) {
1089                 if (cleanup_cb)
1090                         cleanup_cb(step);
1091
1092                 return;
1093         }
1094
1095         queue_pop_head(data->steps);
1096
1097         if (cleanup_cb)
1098                 cleanup_cb(step);
1099
1100         tester_debug("tester: STEP[%d] pass",
1101                         test_steps->step_num-queue_length(data->steps));
1102
1103         if (queue_isempty(data->steps)) {
1104                 tester_print("tester: All steps done, passing");
1105                 tester_test_passed();
1106
1107                 return;
1108         }
1109
1110         /* goto next step action if declared in step */
1111         next_step = queue_peek_head(data->steps);
1112
1113         if (next_step->action)
1114                 next_step->action();
1115 }
1116
1117 /*
1118  * NOTICE:
1119  * Its mandatory for callback to set proper step.callback value so that
1120  * step verification could pass and move to next test step
1121  */
1122
1123 static void free_properties(struct step *step)
1124 {
1125         bt_property_t *properties = step->callback_result.properties;
1126         int num_properties = step->callback_result.num_properties;
1127         int i;
1128
1129         for (i = 0; i < num_properties; i++)
1130                 g_free(properties[i].val);
1131
1132         g_free(properties);
1133 }
1134
1135 static void free_mas_instances(struct step *step)
1136 {
1137         btmce_mas_instance_t *mas_instances;
1138         int num_instances;
1139         int i;
1140
1141         mas_instances = step->callback_result.mas_instances;
1142         num_instances = step->callback_result.num_mas_instances;
1143
1144         for (i = 0; i < num_instances; i++)
1145                 g_free(mas_instances[i].p_name);
1146
1147         g_free(mas_instances);
1148 }
1149
1150 static void destroy_callback_step(void *data)
1151 {
1152         struct step *step = data;
1153
1154         if (step->callback_result.properties)
1155                 free_properties(step);
1156
1157         if (step->callback_result.service)
1158                 free(step->callback_result.service);
1159
1160         if (step->callback_result.characteristic)
1161                 free(step->callback_result.characteristic);
1162
1163         if (step->callback_result.descriptor)
1164                 free(step->callback_result.descriptor);
1165
1166         if (step->callback_result.included)
1167                 free(step->callback_result.included);
1168
1169         if (step->callback_result.read_params)
1170                 free(step->callback_result.read_params);
1171
1172         if (step->callback_result.write_params)
1173                 free(step->callback_result.write_params);
1174
1175         if (step->callback_result.notify_params)
1176                 free(step->callback_result.notify_params);
1177
1178         if (step->callback_result.srvc_handle)
1179                 free(step->callback_result.srvc_handle);
1180
1181         if (step->callback_result.inc_srvc_handle)
1182                 free(step->callback_result.inc_srvc_handle);
1183
1184         if (step->callback_result.uuid)
1185                 free(step->callback_result.uuid);
1186
1187         if (step->callback_result.char_handle)
1188                 free(step->callback_result.char_handle);
1189
1190         if (step->callback_result.desc_handle)
1191                 free(step->callback_result.desc_handle);
1192
1193         if (step->callback_result.attr_handle)
1194                 free(step->callback_result.attr_handle);
1195
1196         if (step->callback_result.value)
1197                 free(step->callback_result.value);
1198
1199         if (step->callback_result.mas_instances)
1200                 free_mas_instances(step);
1201
1202         g_free(step);
1203         g_atomic_int_dec_and_test(&scheduled_cbacks_num);
1204 }
1205
1206 static gboolean verify_action(gpointer user_data)
1207 {
1208         struct step *step = user_data;
1209
1210         verify_step(step, g_free);
1211
1212         return FALSE;
1213 }
1214
1215 static gboolean verify_callback(gpointer user_data)
1216 {
1217         struct test_data *data = tester_get_data();
1218         struct step *step = user_data;
1219
1220         /* Return if callback came when all steps are already verified */
1221         if (queue_isempty(data->steps)) {
1222                 destroy_callback_step(step);
1223                 return FALSE;
1224         }
1225
1226         /*
1227          * TODO: This may call action from next step before callback data
1228          * from previous step was freed.
1229          */
1230         verify_step(step, destroy_callback_step);
1231
1232         return FALSE;
1233 }
1234
1235 void schedule_callback_verification(struct step *step)
1236 {
1237         g_atomic_int_inc(&scheduled_cbacks_num);
1238         g_idle_add(verify_callback, step);
1239 }
1240
1241 void schedule_action_verification(struct step *step)
1242 {
1243         g_idle_add_full(G_PRIORITY_HIGH_IDLE, verify_action, step, NULL);
1244 }
1245
1246 static void adapter_state_changed_cb(bt_state_t state)
1247 {
1248         struct step *step = g_new0(struct step, 1);
1249
1250         step->callback_result.state = state;
1251         step->callback = CB_BT_ADAPTER_STATE_CHANGED;
1252
1253         schedule_callback_verification(step);
1254 }
1255
1256 static bt_property_t *copy_properties(int num_properties,
1257                                                 bt_property_t *properties)
1258 {
1259         int i;
1260         bt_property_t *props = g_new0(bt_property_t, num_properties);
1261
1262         for (i = 0; i < num_properties; i++) {
1263                 props[i].type = properties[i].type;
1264                 props[i].len = properties[i].len;
1265                 props[i].val = g_memdup(properties[i].val, properties[i].len);
1266         }
1267
1268         return props;
1269 }
1270
1271 static bt_property_t *repack_properties(int num_properties,
1272                                                 bt_property_t **properties)
1273 {
1274         int i;
1275         bt_property_t *props = g_new0(bt_property_t, num_properties);
1276
1277         for (i = 0; i < num_properties; i++) {
1278                 props[i].type = properties[i]->type;
1279                 props[i].len = properties[i]->len;
1280                 props[i].val = g_memdup(properties[i]->val, properties[i]->len);
1281         }
1282
1283         return props;
1284 }
1285
1286 static bt_property_t *create_property(bt_property_type_t type, void *val,
1287                                                                 int len)
1288 {
1289         bt_property_t *prop = g_new0(bt_property_t, 1);
1290
1291         prop->type = type;
1292         prop->len = len;
1293         prop->val = g_memdup(val, len);
1294
1295         return prop;
1296 }
1297
1298 static void adapter_properties_cb(bt_status_t status, int num_properties,
1299                                                 bt_property_t *properties)
1300 {
1301         struct step *step = g_new0(struct step, 1);
1302
1303         step->callback_result.status = status;
1304         step->callback_result.num_properties = num_properties;
1305         step->callback_result.properties = copy_properties(num_properties,
1306                                                                 properties);
1307         step->callback = CB_BT_ADAPTER_PROPERTIES;
1308
1309         schedule_callback_verification(step);
1310 }
1311
1312 static void discovery_state_changed_cb(bt_discovery_state_t state)
1313 {
1314         struct step *step = g_new0(struct step, 1);
1315
1316         step->callback = CB_BT_DISCOVERY_STATE_CHANGED;
1317         step->callback_result.state = state;
1318
1319         schedule_callback_verification(step);
1320 }
1321
1322 static void device_found_cb(int num_properties, bt_property_t *properties)
1323 {
1324         struct step *step = g_new0(struct step, 1);
1325
1326         step->callback_result.num_properties = num_properties;
1327         step->callback_result.properties = copy_properties(num_properties,
1328                                                                 properties);
1329
1330         step->callback = CB_BT_DEVICE_FOUND;
1331
1332         schedule_callback_verification(step);
1333 }
1334
1335 static void remote_device_properties_cb(bt_status_t status,
1336                                 bt_bdaddr_t *bd_addr, int num_properties,
1337                                 bt_property_t *properties)
1338 {
1339         struct step *step = g_new0(struct step, 1);
1340
1341         step->callback_result.num_properties = num_properties;
1342         step->callback_result.properties = copy_properties(num_properties,
1343                                                                 properties);
1344
1345         step->callback = CB_BT_REMOTE_DEVICE_PROPERTIES;
1346
1347         schedule_callback_verification(step);
1348 }
1349
1350 static void bond_state_changed_cb(bt_status_t status,
1351                                                 bt_bdaddr_t *remote_bd_addr,
1352                                                 bt_bond_state_t state)
1353 {
1354         struct step *step = g_new0(struct step, 1);
1355
1356         step->callback_result.status = status;
1357         step->callback_result.state = state;
1358
1359         /* Utilize property verification mechanism for bdaddr */
1360         step->callback_result.num_properties = 1;
1361         step->callback_result.properties =
1362                         create_property(BT_PROPERTY_BDADDR, remote_bd_addr,
1363                                                 sizeof(*remote_bd_addr));
1364
1365         step->callback = CB_BT_BOND_STATE_CHANGED;
1366
1367         schedule_callback_verification(step);
1368 }
1369
1370 static void pin_request_cb(bt_bdaddr_t *remote_bd_addr,
1371                                         bt_bdname_t *bd_name, uint32_t cod)
1372 {
1373         struct step *step = g_new0(struct step, 1);
1374         bt_property_t *props[3];
1375
1376         step->callback = CB_BT_PIN_REQUEST;
1377
1378         /* Utilize property verification mechanism for those */
1379         props[0] = create_property(BT_PROPERTY_BDADDR, remote_bd_addr,
1380                                                 sizeof(*remote_bd_addr));
1381         props[1] = create_property(BT_PROPERTY_BDNAME, bd_name->name,
1382                                                 strlen((char *) bd_name->name));
1383         props[2] = create_property(BT_PROPERTY_CLASS_OF_DEVICE, &cod,
1384                                                                 sizeof(cod));
1385
1386         step->callback_result.num_properties = 3;
1387         step->callback_result.properties = repack_properties(3, props);
1388
1389         g_free(props[0]->val);
1390         g_free(props[0]);
1391         g_free(props[1]->val);
1392         g_free(props[1]);
1393         g_free(props[2]->val);
1394         g_free(props[2]);
1395
1396         schedule_callback_verification(step);
1397 }
1398
1399 static void ssp_request_cb(bt_bdaddr_t *remote_bd_addr,
1400                                         bt_bdname_t *bd_name, uint32_t cod,
1401                                         bt_ssp_variant_t pairing_variant,
1402                                         uint32_t pass_key)
1403 {
1404         struct step *step = g_new0(struct step, 1);
1405         bt_property_t *props[3];
1406
1407         step->callback = CB_BT_SSP_REQUEST;
1408
1409         /* Utilize property verification mechanism for those */
1410         props[0] = create_property(BT_PROPERTY_BDADDR, remote_bd_addr,
1411                                                 sizeof(*remote_bd_addr));
1412         props[1] = create_property(BT_PROPERTY_BDNAME, bd_name->name,
1413                                                 strlen((char *) bd_name->name));
1414         props[2] = create_property(BT_PROPERTY_CLASS_OF_DEVICE, &cod,
1415                                                                 sizeof(cod));
1416
1417         step->callback_result.num_properties = 3;
1418         step->callback_result.properties = repack_properties(3, props);
1419
1420         g_free(props[0]->val);
1421         g_free(props[0]);
1422         g_free(props[1]->val);
1423         g_free(props[1]);
1424         g_free(props[2]->val);
1425         g_free(props[2]);
1426
1427         schedule_callback_verification(step);
1428 }
1429
1430 static void acl_state_changed_cb(bt_status_t status,
1431                                         bt_bdaddr_t *remote_bd_addr,
1432                                         bt_acl_state_t state) {
1433         struct step *step = g_new0(struct step, 1);
1434
1435         step->callback = CB_BT_ACL_STATE_CHANGED;
1436
1437         step->callback_result.status = status;
1438         step->callback_result.state = state;
1439
1440         schedule_callback_verification(step);
1441 }
1442
1443 static bt_callbacks_t bt_callbacks = {
1444         .size = sizeof(bt_callbacks),
1445         .adapter_state_changed_cb = adapter_state_changed_cb,
1446         .adapter_properties_cb = adapter_properties_cb,
1447         .remote_device_properties_cb = remote_device_properties_cb,
1448         .device_found_cb = device_found_cb,
1449         .discovery_state_changed_cb = discovery_state_changed_cb,
1450         .pin_request_cb = pin_request_cb,
1451         .ssp_request_cb = ssp_request_cb,
1452         .bond_state_changed_cb = bond_state_changed_cb,
1453         .acl_state_changed_cb = acl_state_changed_cb,
1454         .thread_evt_cb = NULL,
1455         .dut_mode_recv_cb = NULL,
1456         .le_test_mode_cb = NULL,
1457         .energy_info_cb = NULL,
1458 };
1459
1460 static void hidhost_connection_state_cb(bt_bdaddr_t *bd_addr,
1461                                                 bthh_connection_state_t state)
1462 {
1463         struct step *step = g_new0(struct step, 1);
1464
1465         step->callback = CB_HH_CONNECTION_STATE;
1466         step->callback_result.state = state;
1467
1468         schedule_callback_verification(step);
1469 }
1470
1471 static void hidhost_virtual_unplug_cb(bt_bdaddr_t *bd_addr, bthh_status_t status)
1472 {
1473         struct step *step = g_new0(struct step, 1);
1474
1475         step->callback = CB_HH_VIRTUAL_UNPLUG;
1476         step->callback_result.status = status;
1477
1478         schedule_callback_verification(step);
1479 }
1480
1481 static void hidhost_protocol_mode_cb(bt_bdaddr_t *bd_addr,
1482                                                 bthh_status_t status,
1483                                                 bthh_protocol_mode_t mode)
1484 {
1485         struct step *step = g_new0(struct step, 1);
1486
1487         step->callback = CB_HH_PROTOCOL_MODE;
1488         step->callback_result.status = status;
1489         step->callback_result.mode = mode;
1490
1491         /* TODO: add bdaddr to verify? */
1492
1493         schedule_callback_verification(step);
1494 }
1495
1496 static void hidhost_hid_info_cb(bt_bdaddr_t *bd_addr, bthh_hid_info_t hid)
1497 {
1498         struct step *step = g_new0(struct step, 1);
1499
1500         step->callback = CB_HH_HID_INFO;
1501
1502         schedule_callback_verification(step);
1503 }
1504
1505 static void hidhost_get_report_cb(bt_bdaddr_t *bd_addr, bthh_status_t status,
1506                                                 uint8_t *report, int size)
1507 {
1508         struct step *step = g_new0(struct step, 1);
1509
1510         step->callback = CB_HH_GET_REPORT;
1511
1512         step->callback_result.status = status;
1513         step->callback_result.report_size = size;
1514
1515         schedule_callback_verification(step);
1516 }
1517
1518 static bthh_callbacks_t bthh_callbacks = {
1519         .size = sizeof(bthh_callbacks),
1520         .connection_state_cb = hidhost_connection_state_cb,
1521         .hid_info_cb = hidhost_hid_info_cb,
1522         .protocol_mode_cb = hidhost_protocol_mode_cb,
1523         .idle_time_cb = NULL,
1524         .get_report_cb = hidhost_get_report_cb,
1525         .virtual_unplug_cb = hidhost_virtual_unplug_cb,
1526         .handshake_cb = NULL,
1527 };
1528
1529 static void gattc_register_client_cb(int status, int client_if,
1530                                                         bt_uuid_t *app_uuid)
1531 {
1532         struct step *step = g_new0(struct step, 1);
1533
1534         step->callback = CB_GATTC_REGISTER_CLIENT;
1535
1536         step->callback_result.status = status;
1537
1538         schedule_callback_verification(step);
1539 }
1540
1541 static void gattc_scan_result_cb(bt_bdaddr_t *bda, int rssi, uint8_t *adv_data)
1542 {
1543         struct step *step = g_new0(struct step, 1);
1544         bt_property_t *props[2];
1545
1546         step->callback = CB_GATTC_SCAN_RESULT;
1547         step->callback_result.adv_data = adv_data ? TRUE : FALSE;
1548
1549         /* Utilize property verification mechanism for those */
1550         props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1551         props[1] = create_property(BT_PROPERTY_REMOTE_RSSI, &rssi,
1552                                                                 sizeof(rssi));
1553
1554         step->callback_result.num_properties = 2;
1555         step->callback_result.properties = repack_properties(2, props);
1556
1557         g_free(props[0]->val);
1558         g_free(props[0]);
1559         g_free(props[1]->val);
1560         g_free(props[1]);
1561
1562         schedule_callback_verification(step);
1563 }
1564
1565 static void gattc_connect_cb(int conn_id, int status, int client_if,
1566                                                         bt_bdaddr_t *bda)
1567 {
1568         struct step *step = g_new0(struct step, 1);
1569         bt_property_t *props[1];
1570
1571         step->callback = CB_GATTC_OPEN;
1572         step->callback_result.status = status;
1573         step->callback_result.conn_id = conn_id;
1574         step->callback_result.gatt_app_id = client_if;
1575
1576         /* Utilize property verification mechanism for bdaddr */
1577         props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1578
1579         step->callback_result.num_properties = 1;
1580         step->callback_result.properties = repack_properties(1, props);
1581
1582         g_free(props[0]->val);
1583         g_free(props[0]);
1584
1585         schedule_callback_verification(step);
1586 }
1587
1588 static void gattc_disconnect_cb(int conn_id, int status, int client_if,
1589                                                         bt_bdaddr_t *bda)
1590 {
1591         struct step *step = g_new0(struct step, 1);
1592         bt_property_t *props[1];
1593
1594         step->callback = CB_GATTC_CLOSE;
1595         step->callback_result.status = status;
1596         step->callback_result.conn_id = conn_id;
1597         step->callback_result.gatt_app_id = client_if;
1598
1599         /* Utilize property verification mechanism for bdaddr */
1600         props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1601
1602         step->callback_result.num_properties = 1;
1603         step->callback_result.properties = repack_properties(1, props);
1604
1605         g_free(props[0]->val);
1606         g_free(props[0]);
1607
1608         schedule_callback_verification(step);
1609 }
1610
1611 static void gattc_listen_cb(int status, int server_if)
1612 {
1613         struct step *step = g_new0(struct step, 1);
1614
1615         step->callback = CB_GATTC_LISTEN;
1616         step->callback_result.status = status;
1617
1618         schedule_callback_verification(step);
1619 }
1620
1621 static void gattc_search_result_cb(int conn_id, btgatt_srvc_id_t *srvc_id)
1622 {
1623         struct step *step = g_new0(struct step, 1);
1624
1625         step->callback = CB_GATTC_SEARCH_RESULT;
1626         step->callback_result.conn_id = conn_id;
1627         step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
1628
1629         schedule_callback_verification(step);
1630 }
1631
1632 static void gattc_search_complete_cb(int conn_id, int status)
1633 {
1634         struct step *step = g_new0(struct step, 1);
1635
1636         step->callback = CB_GATTC_SEARCH_COMPLETE;
1637         step->callback_result.conn_id = conn_id;
1638
1639         schedule_callback_verification(step);
1640 }
1641
1642 static void gattc_get_characteristic_cb(int conn_id, int status,
1643                         btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1644                         int char_prop)
1645 {
1646         struct step *step = g_new0(struct step, 1);
1647
1648         step->callback = CB_GATTC_GET_CHARACTERISTIC;
1649         step->callback_result.status = status;
1650         step->callback_result.conn_id = conn_id;
1651         step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
1652         step->callback_result.characteristic = g_memdup(char_id,
1653                                                         sizeof(*char_id));
1654         step->callback_result.char_prop = char_prop;
1655
1656         schedule_callback_verification(step);
1657 }
1658
1659 static void gattc_get_descriptor_cb(int conn_id, int status,
1660                         btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1661                         btgatt_gatt_id_t *descr_id)
1662 {
1663         struct step *step = g_new0(struct step, 1);
1664
1665         step->callback = CB_GATTC_GET_DESCRIPTOR;
1666         step->callback_result.status = status;
1667         step->callback_result.conn_id = conn_id;
1668         step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
1669         step->callback_result.characteristic = g_memdup(char_id,
1670                                                         sizeof(*char_id));
1671         step->callback_result.descriptor = g_memdup(descr_id,
1672                                                         sizeof(*descr_id));
1673
1674         schedule_callback_verification(step);
1675 }
1676
1677 static void gattc_get_included_service_cb(int conn_id, int status,
1678                 btgatt_srvc_id_t *srvc_id, btgatt_srvc_id_t *incl_srvc_id)
1679 {
1680         struct step *step = g_new0(struct step, 1);
1681
1682         step->callback = CB_GATTC_GET_INCLUDED_SERVICE;
1683         step->callback_result.status = status;
1684         step->callback_result.conn_id = conn_id;
1685         step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
1686         step->callback_result.included = g_memdup(incl_srvc_id,
1687                                                         sizeof(*srvc_id));
1688
1689         schedule_callback_verification(step);
1690 }
1691
1692 static void gattc_read_characteristic_cb(int conn_id, int status,
1693                                                 btgatt_read_params_t *p_data)
1694 {
1695         struct step *step = g_new0(struct step, 1);
1696
1697         step->callback = CB_GATTC_READ_CHARACTERISTIC;
1698         step->callback_result.status = status;
1699         step->callback_result.conn_id = conn_id;
1700         step->callback_result.read_params = g_memdup(p_data, sizeof(*p_data));
1701
1702         schedule_callback_verification(step);
1703 }
1704
1705 static void gattc_read_descriptor_cb(int conn_id, int status,
1706                                                 btgatt_read_params_t *p_data)
1707 {
1708         struct step *step = g_new0(struct step, 1);
1709
1710         step->callback = CB_GATTC_READ_DESCRIPTOR;
1711         step->callback_result.status = status;
1712         step->callback_result.conn_id = conn_id;
1713         step->callback_result.read_params = g_memdup(p_data, sizeof(*p_data));
1714
1715         schedule_callback_verification(step);
1716 }
1717
1718 static void gattc_write_characteristic_cb(int conn_id, int status,
1719                                                 btgatt_write_params_t *p_data)
1720 {
1721         struct step *step = g_new0(struct step, 1);
1722
1723         step->callback = CB_GATTC_WRITE_CHARACTERISTIC;
1724         step->callback_result.status = status;
1725         step->callback_result.conn_id = conn_id;
1726         step->callback_result.write_params = g_memdup(p_data, sizeof(*p_data));
1727
1728         schedule_callback_verification(step);
1729 }
1730
1731 static void gattc_write_descriptor_cb(int conn_id, int status,
1732                                                 btgatt_write_params_t *p_data)
1733 {
1734         struct step *step = g_new0(struct step, 1);
1735
1736         step->callback = CB_GATTC_WRITE_DESCRIPTOR;
1737         step->callback_result.status = status;
1738         step->callback_result.conn_id = conn_id;
1739         step->callback_result.write_params = g_memdup(p_data, sizeof(*p_data));
1740
1741         schedule_callback_verification(step);
1742 }
1743
1744 static void gattc_register_for_notification_cb(int conn_id, int registered,
1745                                                 int status,
1746                                                 btgatt_srvc_id_t *srvc_id,
1747                                                 btgatt_gatt_id_t *char_id)
1748 {
1749         struct step *step = g_new0(struct step, 1);
1750
1751         step->callback = CB_GATTC_REGISTER_FOR_NOTIFICATION;
1752         step->callback_result.status = status;
1753         step->callback_result.conn_id = conn_id;
1754         step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
1755         step->callback_result.characteristic = g_memdup(char_id,
1756                                                         sizeof(*char_id));
1757         step->callback_result.notification_registered = registered;
1758
1759         schedule_callback_verification(step);
1760 }
1761
1762 static void gattc_notif_cb(int conn_id, btgatt_notify_params_t *p_data)
1763 {
1764         struct step *step = g_new0(struct step, 1);
1765
1766         step->callback = CB_GATTC_NOTIFY;
1767         step->callback_result.conn_id = conn_id;
1768         step->callback_result.notify_params = g_memdup(p_data, sizeof(*p_data));
1769
1770         schedule_callback_verification(step);
1771 }
1772
1773 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
1774         .register_client_cb = gattc_register_client_cb,
1775         .scan_result_cb = gattc_scan_result_cb,
1776         .open_cb = gattc_connect_cb,
1777         .close_cb = gattc_disconnect_cb,
1778         .search_complete_cb = gattc_search_complete_cb,
1779         .search_result_cb = gattc_search_result_cb,
1780         .get_characteristic_cb = gattc_get_characteristic_cb,
1781         .get_descriptor_cb = gattc_get_descriptor_cb,
1782         .get_included_service_cb = gattc_get_included_service_cb,
1783         .register_for_notification_cb = gattc_register_for_notification_cb,
1784         .notify_cb = gattc_notif_cb,
1785         .read_characteristic_cb = gattc_read_characteristic_cb,
1786         .write_characteristic_cb = gattc_write_characteristic_cb,
1787         .read_descriptor_cb = gattc_read_descriptor_cb,
1788         .write_descriptor_cb = gattc_write_descriptor_cb,
1789         .execute_write_cb = NULL,
1790         .read_remote_rssi_cb = NULL,
1791         .listen_cb = gattc_listen_cb
1792 };
1793
1794 static void gatts_register_server_cb(int status, int server_if,
1795                                                         bt_uuid_t *app_uuid)
1796 {
1797         struct step *step = g_new0(struct step, 1);
1798
1799         step->callback = CB_GATTS_REGISTER_SERVER;
1800
1801         step->callback_result.status = status;
1802
1803         schedule_callback_verification(step);
1804 }
1805
1806 static void gatts_connection_cb(int conn_id, int server_if, int connected,
1807                                                         bt_bdaddr_t *bda)
1808 {
1809         struct step *step = g_new0(struct step, 1);
1810         bt_property_t *props[1];
1811
1812         step->callback = CB_GATTS_CONNECTION;
1813         step->callback_result.conn_id = conn_id;
1814         step->callback_result.gatt_app_id = server_if;
1815         step->callback_result.connected = connected;
1816
1817         /* Utilize property verification mechanism for bdaddr */
1818         props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1819
1820         step->callback_result.num_properties = 1;
1821         step->callback_result.properties = repack_properties(1, props);
1822
1823         g_free(props[0]->val);
1824         g_free(props[0]);
1825
1826         schedule_callback_verification(step);
1827 }
1828
1829 static void gatts_service_added_cb(int status, int server_if,
1830                                                 btgatt_srvc_id_t *srvc_id,
1831                                                 int srvc_handle)
1832 {
1833         struct step *step = g_new0(struct step, 1);
1834
1835         step->callback = CB_GATTS_SERVICE_ADDED;
1836
1837         step->callback_result.status = status;
1838         step->callback_result.gatt_app_id = server_if;
1839         step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
1840         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1841                                                         sizeof(srvc_handle));
1842
1843         schedule_callback_verification(step);
1844 }
1845
1846 static void gatts_included_service_added_cb(int status, int server_if,
1847                                                         int srvc_handle,
1848                                                         int inc_srvc_handle)
1849 {
1850         struct step *step = g_new0(struct step, 1);
1851
1852         step->callback = CB_GATTS_INCLUDED_SERVICE_ADDED;
1853
1854         step->callback_result.status = status;
1855         step->callback_result.gatt_app_id = server_if;
1856         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1857                                                         sizeof(srvc_handle));
1858         step->callback_result.inc_srvc_handle = g_memdup(&inc_srvc_handle,
1859                                                 sizeof(inc_srvc_handle));
1860
1861         schedule_callback_verification(step);
1862 }
1863
1864 static void gatts_characteristic_added_cb(int status, int server_if,
1865                                                                 bt_uuid_t *uuid,
1866                                                                 int srvc_handle,
1867                                                                 int char_handle)
1868 {
1869         struct step *step = g_new0(struct step, 1);
1870
1871         step->callback = CB_GATTS_CHARACTERISTIC_ADDED;
1872
1873         step->callback_result.status = status;
1874         step->callback_result.gatt_app_id = server_if;
1875         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1876                                                         sizeof(srvc_handle));
1877         step->callback_result.uuid = g_memdup(uuid, sizeof(*uuid));
1878         step->callback_result.char_handle = g_memdup(&char_handle,
1879                                                         sizeof(char_handle));
1880
1881         schedule_callback_verification(step);
1882 }
1883
1884 static void gatts_descriptor_added_cb(int status, int server_if,
1885                                                                 bt_uuid_t *uuid,
1886                                                                 int srvc_handle,
1887                                                                 int desc_handle)
1888 {
1889         struct step *step = g_new0(struct step, 1);
1890
1891         step->callback = CB_GATTS_DESCRIPTOR_ADDED;
1892
1893         step->callback_result.status = status;
1894         step->callback_result.gatt_app_id = server_if;
1895         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1896                                                         sizeof(srvc_handle));
1897         step->callback_result.uuid = g_memdup(uuid, sizeof(*uuid));
1898         step->callback_result.desc_handle = g_memdup(&desc_handle,
1899                                                         sizeof(desc_handle));
1900
1901         schedule_callback_verification(step);
1902 }
1903
1904 static void gatts_service_started_cb(int status, int server_if, int srvc_handle)
1905 {
1906         struct step *step = g_new0(struct step, 1);
1907
1908         step->callback = CB_GATTS_SERVICE_STARTED;
1909
1910         step->callback_result.status = status;
1911         step->callback_result.gatt_app_id = server_if;
1912         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1913                                                         sizeof(srvc_handle));
1914
1915         schedule_callback_verification(step);
1916 }
1917
1918 static void gatts_service_stopped_cb(int status, int server_if, int srvc_handle)
1919 {
1920         struct step *step = g_new0(struct step, 1);
1921
1922         step->callback = CB_GATTS_SERVICE_STOPPED;
1923
1924         step->callback_result.status = status;
1925         step->callback_result.gatt_app_id = server_if;
1926         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1927                                                         sizeof(srvc_handle));
1928
1929         schedule_callback_verification(step);
1930 }
1931
1932 static void gatts_service_deleted_cb(int status, int server_if, int srvc_handle)
1933 {
1934         struct step *step = g_new0(struct step, 1);
1935
1936         step->callback = CB_GATTS_SERVICE_DELETED;
1937
1938         step->callback_result.status = status;
1939         step->callback_result.gatt_app_id = server_if;
1940         step->callback_result.srvc_handle = g_memdup(&srvc_handle,
1941                                                         sizeof(srvc_handle));
1942
1943         schedule_callback_verification(step);
1944 }
1945
1946 static void gatts_request_read_cb(int conn_id, int trans_id, bt_bdaddr_t *bda,
1947                                                 int attr_handle, int offset,
1948                                                 bool is_long)
1949 {
1950         struct step *step = g_new0(struct step, 1);
1951         bt_property_t *props[1];
1952
1953         step->callback = CB_GATTS_REQUEST_READ;
1954
1955         step->callback_result.conn_id = conn_id;
1956         step->callback_result.trans_id = trans_id;
1957         step->callback_result.attr_handle = g_memdup(&attr_handle,
1958                                                         sizeof(attr_handle));
1959         step->callback_result.offset = offset;
1960         step->callback_result.is_long = is_long;
1961
1962         /* Utilize property verification mechanism for bdaddr */
1963         props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1964
1965         step->callback_result.num_properties = 1;
1966         step->callback_result.properties = repack_properties(1, props);
1967
1968         g_free(props[0]->val);
1969         g_free(props[0]);
1970
1971         schedule_callback_verification(step);
1972 }
1973
1974 static void gatts_request_write_cb(int conn_id, int trans_id, bt_bdaddr_t *bda,
1975                                                 int attr_handle, int offset,
1976                                                 int length, bool need_rsp,
1977                                                 bool is_prep, uint8_t *value)
1978 {
1979         struct step *step = g_new0(struct step, 1);
1980         bt_property_t *props[1];
1981
1982         step->callback = CB_GATTS_REQUEST_WRITE;
1983
1984         step->callback_result.conn_id = conn_id;
1985         step->callback_result.trans_id = trans_id;
1986         step->callback_result.attr_handle = g_memdup(&attr_handle,
1987                                                         sizeof(attr_handle));
1988         step->callback_result.offset = offset;
1989         step->callback_result.length = length;
1990         step->callback_result.need_rsp = need_rsp;
1991         step->callback_result.is_prep = is_prep;
1992         step->callback_result.value = g_memdup(&value, length);
1993
1994         /* Utilize property verification mechanism for bdaddr */
1995         props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1996
1997         step->callback_result.num_properties = 1;
1998         step->callback_result.properties = repack_properties(1, props);
1999
2000         g_free(props[0]->val);
2001         g_free(props[0]);
2002
2003         schedule_callback_verification(step);
2004 }
2005
2006 static void gatts_indication_send_cb(int conn_id, int status)
2007 {
2008         struct step *step = g_new0(struct step, 1);
2009
2010         step->callback = CB_GATTS_INDICATION_SEND;
2011
2012         step->callback_result.conn_id = conn_id;
2013         step->callback_result.status = status;
2014
2015         schedule_callback_verification(step);
2016 }
2017
2018 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
2019         .register_server_cb = gatts_register_server_cb,
2020         .connection_cb = gatts_connection_cb,
2021         .service_added_cb = gatts_service_added_cb,
2022         .included_service_added_cb = gatts_included_service_added_cb,
2023         .characteristic_added_cb = gatts_characteristic_added_cb,
2024         .descriptor_added_cb = gatts_descriptor_added_cb,
2025         .service_started_cb = gatts_service_started_cb,
2026         .service_stopped_cb = gatts_service_stopped_cb,
2027         .service_deleted_cb = gatts_service_deleted_cb,
2028         .request_read_cb = gatts_request_read_cb,
2029         .request_write_cb = gatts_request_write_cb,
2030         .request_exec_write_cb = NULL,
2031         .response_confirmation_cb = NULL,
2032         .indication_sent_cb = gatts_indication_send_cb,
2033         .congestion_cb = NULL,
2034 };
2035
2036 static const btgatt_callbacks_t btgatt_callbacks = {
2037         .size = sizeof(btgatt_callbacks),
2038         .client = &btgatt_client_callbacks,
2039         .server = &btgatt_server_callbacks
2040 };
2041
2042 static void pan_control_state_cb(btpan_control_state_t state, int local_role,
2043                                         bt_status_t error, const char *ifname)
2044 {
2045         struct step *step = g_new0(struct step, 1);
2046
2047         step->callback = CB_PAN_CONTROL_STATE;
2048         step->callback_result.state = error;
2049         step->callback_result.ctrl_state = state;
2050         step->callback_result.local_role = local_role;
2051
2052         schedule_callback_verification(step);
2053 }
2054
2055 static void pan_connection_state_cb(btpan_connection_state_t state,
2056                                         bt_status_t error,
2057                                         const bt_bdaddr_t *bd_addr,
2058                                         int local_role, int remote_role)
2059 {
2060         struct step *step = g_new0(struct step, 1);
2061
2062         step->callback = CB_PAN_CONNECTION_STATE;
2063         step->callback_result.state = error;
2064         step->callback_result.conn_state = state;
2065         step->callback_result.local_role = local_role;
2066         step->callback_result.remote_role = remote_role;
2067
2068         schedule_callback_verification(step);
2069 }
2070
2071 static btpan_callbacks_t btpan_callbacks = {
2072         .size = sizeof(btpan_callbacks),
2073         .control_state_cb = pan_control_state_cb,
2074         .connection_state_cb = pan_connection_state_cb,
2075 };
2076
2077 static void hdp_app_reg_state_cb(int app_id, bthl_app_reg_state_t state)
2078 {
2079         struct step *step = g_new0(struct step, 1);
2080
2081         step->callback = CB_HDP_APP_REG_STATE;
2082         step->callback_result.app_id = app_id;
2083         step->callback_result.app_state = state;
2084
2085         schedule_callback_verification(step);
2086 }
2087
2088 static void hdp_channel_state_cb(int app_id, bt_bdaddr_t *bd_addr,
2089                                 int mdep_cfg_index, int channel_id,
2090                                 bthl_channel_state_t state, int fd)
2091 {
2092         struct step *step = g_new0(struct step, 1);
2093
2094         step->callback = CB_HDP_CHANNEL_STATE;
2095         step->callback_result.app_id = app_id;
2096         step->callback_result.channel_id = channel_id;
2097         step->callback_result.mdep_cfg_index = mdep_cfg_index;
2098         step->callback_result.channel_state = state;
2099
2100         schedule_callback_verification(step);
2101 }
2102
2103 static bthl_callbacks_t bthl_callbacks = {
2104         .size = sizeof(bthl_callbacks),
2105         .app_reg_state_cb = hdp_app_reg_state_cb,
2106         .channel_state_cb = hdp_channel_state_cb,
2107 };
2108
2109 static void a2dp_connection_state_cb(btav_connection_state_t state,
2110                                                         bt_bdaddr_t *bd_addr)
2111 {
2112         struct step *step = g_new0(struct step, 1);
2113
2114         step->callback = CB_A2DP_CONN_STATE;
2115         step->callback_result.av_conn_state = state;
2116
2117         schedule_callback_verification(step);
2118 }
2119
2120 static void a2dp_audio_state_cb(btav_audio_state_t state, bt_bdaddr_t *bd_addr)
2121 {
2122         struct step *step = g_new0(struct step, 1);
2123
2124         step->callback = CB_A2DP_AUDIO_STATE;
2125         step->callback_result.av_audio_state = state;
2126
2127         schedule_callback_verification(step);
2128 }
2129
2130 static btav_callbacks_t bta2dp_callbacks = {
2131         .size = sizeof(bta2dp_callbacks),
2132         .connection_state_cb = a2dp_connection_state_cb,
2133         .audio_state_cb = a2dp_audio_state_cb,
2134 };
2135
2136 static void avrcp_get_play_status_cb(void)
2137 {
2138         struct step *step = g_new0(struct step, 1);
2139
2140         step->callback = CB_AVRCP_PLAY_STATUS_REQ;
2141         schedule_callback_verification(step);
2142 }
2143
2144 static void avrcp_register_notification_cb(btrc_event_id_t event_id,
2145                                                                 uint32_t param)
2146 {
2147         struct step *step = g_new0(struct step, 1);
2148
2149         step->callback = CB_AVRCP_REG_NOTIF_REQ;
2150         schedule_callback_verification(step);
2151 }
2152
2153 static void avrcp_get_element_attr_cb(uint8_t num_attr,
2154                                                 btrc_media_attr_t *p_attrs)
2155 {
2156         struct step *step = g_new0(struct step, 1);
2157
2158         step->callback = CB_AVRCP_GET_ATTR_REQ;
2159         schedule_callback_verification(step);
2160 }
2161
2162 static btrc_callbacks_t btavrcp_callbacks = {
2163         .size = sizeof(btavrcp_callbacks),
2164         .get_play_status_cb = avrcp_get_play_status_cb,
2165         .register_notification_cb = avrcp_register_notification_cb,
2166         .get_element_attr_cb = avrcp_get_element_attr_cb,
2167 };
2168
2169 static btmce_mas_instance_t *copy_mas_instances(int num_instances,
2170                                                 btmce_mas_instance_t *instances)
2171 {
2172         int i;
2173         btmce_mas_instance_t *inst;
2174
2175         inst = g_new0(btmce_mas_instance_t, num_instances);
2176
2177         for (i = 0; i < num_instances; i++) {
2178                 inst[i].id = instances[i].id;
2179                 inst[i].scn = instances[i].scn;
2180                 inst[i].msg_types = instances[i].msg_types;
2181                 inst[i].p_name = g_memdup(instances[i].p_name,
2182                                                 strlen(instances[i].p_name));
2183         }
2184
2185         return inst;
2186 }
2187
2188 static void map_client_get_remote_mas_instances_cb(bt_status_t status,
2189                                                         bt_bdaddr_t *bd_addr,
2190                                                         int num_instances,
2191                                                         btmce_mas_instance_t
2192                                                         *instances)
2193 {
2194         struct step *step = g_new0(struct step, 1);
2195
2196         step->callback_result.status = status;
2197         step->callback_result.num_mas_instances = num_instances;
2198         step->callback_result.mas_instances = copy_mas_instances(num_instances,
2199                                                                 instances);
2200
2201         step->callback = CB_MAP_CLIENT_REMOTE_MAS_INSTANCES;
2202
2203         schedule_callback_verification(step);
2204 }
2205
2206 static btmce_callbacks_t btmap_client_callbacks = {
2207         .size = sizeof(btmap_client_callbacks),
2208         .remote_mas_instances_cb = map_client_get_remote_mas_instances_cb,
2209 };
2210
2211 static bool setup_base(struct test_data *data)
2212 {
2213         const hw_module_t *module;
2214         hw_device_t *device;
2215         int signal_fd[2];
2216         char buf[1024];
2217         pid_t pid;
2218         int len;
2219         int err;
2220
2221         if (pipe(signal_fd))
2222                 return false;
2223
2224         pid = fork();
2225
2226         if (pid < 0) {
2227                 close(signal_fd[0]);
2228                 close(signal_fd[1]);
2229                 return false;
2230         }
2231
2232         if (pid == 0) {
2233                 if (!tester_use_debug())
2234                         fclose(stderr);
2235
2236                 close(signal_fd[0]);
2237                 emulator(signal_fd[1], data->mgmt_index);
2238                 exit(0);
2239         }
2240
2241         close(signal_fd[1]);
2242         data->bluetoothd_pid = pid;
2243
2244         len = read(signal_fd[0], buf, sizeof(buf));
2245         if (len <= 0 || strcmp(buf, EMULATOR_SIGNAL)) {
2246                 close(signal_fd[0]);
2247                 return false;
2248         }
2249
2250         close(signal_fd[0]);
2251
2252         err = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID,
2253                                         AUDIO_HARDWARE_MODULE_ID_A2DP, &module);
2254         if (err)
2255                 return false;
2256
2257         err = audio_hw_device_open(module, &data->audio);
2258         if (err)
2259                 return false;
2260
2261         err = hw_get_module(BT_HARDWARE_MODULE_ID, &module);
2262         if (err)
2263                 return false;
2264
2265         err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
2266         if (err)
2267                 return false;
2268
2269         data->device = device;
2270
2271         data->if_bluetooth = ((bluetooth_device_t *)
2272                                         device)->get_bluetooth_interface();
2273         if (!data->if_bluetooth)
2274                 return false;
2275
2276         if (!(data->steps = queue_new()))
2277                 return false;
2278
2279         data->pdus = queue_new();
2280         if (!data->pdus) {
2281                 queue_destroy(data->steps, NULL);
2282                 return false;
2283         }
2284
2285         return true;
2286 }
2287
2288 static void setup(const void *test_data)
2289 {
2290         struct test_data *data = tester_get_data();
2291         bt_status_t status;
2292
2293         if (!setup_base(data)) {
2294                 tester_setup_failed();
2295                 return;
2296         }
2297
2298         status = data->if_bluetooth->init(&bt_callbacks);
2299         if (status != BT_STATUS_SUCCESS) {
2300                 data->if_bluetooth = NULL;
2301                 tester_setup_failed();
2302                 return;
2303         }
2304
2305         tester_setup_complete();
2306 }
2307
2308 static void setup_socket(const void *test_data)
2309 {
2310         struct test_data *data = tester_get_data();
2311         bt_status_t status;
2312         const void *sock;
2313
2314         if (!setup_base(data)) {
2315                 tester_setup_failed();
2316                 return;
2317         }
2318
2319         status = data->if_bluetooth->init(&bt_callbacks);
2320         if (status != BT_STATUS_SUCCESS) {
2321                 data->if_bluetooth = NULL;
2322                 tester_setup_failed();
2323                 return;
2324         }
2325
2326         sock = data->if_bluetooth->get_profile_interface(BT_PROFILE_SOCKETS_ID);
2327         if (!sock) {
2328                 tester_setup_failed();
2329                 return;
2330         }
2331
2332         data->if_sock = sock;
2333
2334         tester_setup_complete();
2335 }
2336
2337 static void setup_hidhost(const void *test_data)
2338 {
2339         struct test_data *data = tester_get_data();
2340         bt_status_t status;
2341         const void *hid;
2342
2343         if (!setup_base(data)) {
2344                 tester_setup_failed();
2345                 return;
2346         }
2347
2348         status = data->if_bluetooth->init(&bt_callbacks);
2349         if (status != BT_STATUS_SUCCESS) {
2350                 data->if_bluetooth = NULL;
2351                 tester_setup_failed();
2352                 return;
2353         }
2354
2355         hid = data->if_bluetooth->get_profile_interface(BT_PROFILE_HIDHOST_ID);
2356         if (!hid) {
2357                 tester_setup_failed();
2358                 return;
2359         }
2360
2361         data->if_hid = hid;
2362
2363         status = data->if_hid->init(&bthh_callbacks);
2364         if (status != BT_STATUS_SUCCESS) {
2365                 data->if_hid = NULL;
2366                 tester_setup_failed();
2367                 return;
2368         }
2369
2370         tester_setup_complete();
2371 }
2372
2373 static void setup_pan(const void *test_data)
2374 {
2375         struct test_data *data = tester_get_data();
2376         bt_status_t status;
2377         const void *pan;
2378
2379         if (!setup_base(data)) {
2380                 tester_setup_failed();
2381                 return;
2382         }
2383
2384         status = data->if_bluetooth->init(&bt_callbacks);
2385         if (status != BT_STATUS_SUCCESS) {
2386                 data->if_bluetooth = NULL;
2387                 tester_setup_failed();
2388                 return;
2389         }
2390
2391         pan = data->if_bluetooth->get_profile_interface(BT_PROFILE_PAN_ID);
2392         if (!pan) {
2393                 tester_setup_failed();
2394                 return;
2395         }
2396
2397         data->if_pan = pan;
2398
2399         status = data->if_pan->init(&btpan_callbacks);
2400         if (status != BT_STATUS_SUCCESS) {
2401                 data->if_pan = NULL;
2402                 tester_setup_failed();
2403                 return;
2404         }
2405
2406         tester_setup_complete();
2407 }
2408
2409 static void setup_hdp(const void *test_data)
2410 {
2411         struct test_data *data = tester_get_data();
2412         bt_status_t status;
2413         const void *hdp;
2414
2415         if (!setup_base(data)) {
2416                 tester_setup_failed();
2417                 return;
2418         }
2419
2420         status = data->if_bluetooth->init(&bt_callbacks);
2421         if (status != BT_STATUS_SUCCESS) {
2422                 data->if_bluetooth = NULL;
2423                 tester_setup_failed();
2424                 return;
2425         }
2426
2427         hdp = data->if_bluetooth->get_profile_interface(BT_PROFILE_HEALTH_ID);
2428         if (!hdp) {
2429                 tester_setup_failed();
2430                 return;
2431         }
2432
2433         data->if_hdp = hdp;
2434
2435         status = data->if_hdp->init(&bthl_callbacks);
2436         if (status != BT_STATUS_SUCCESS) {
2437                 data->if_hdp = NULL;
2438                 tester_setup_failed();
2439                 return;
2440         }
2441
2442         tester_setup_complete();
2443 }
2444
2445 static void setup_a2dp(const void *test_data)
2446 {
2447         struct test_data *data = tester_get_data();
2448         const bt_interface_t *if_bt;
2449         bt_status_t status;
2450         const void *a2dp;
2451
2452         if (!setup_base(data)) {
2453                 tester_setup_failed();
2454                 return;
2455         }
2456
2457         if_bt = data->if_bluetooth;
2458
2459         status = if_bt->init(&bt_callbacks);
2460         if (status != BT_STATUS_SUCCESS) {
2461                 data->if_bluetooth = NULL;
2462                 tester_setup_failed();
2463                 return;
2464         }
2465
2466         a2dp = if_bt->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID);
2467         if (!a2dp) {
2468                 tester_setup_failed();
2469                 return;
2470         }
2471
2472         data->if_a2dp = a2dp;
2473
2474         status = data->if_a2dp->init(&bta2dp_callbacks);
2475         if (status != BT_STATUS_SUCCESS) {
2476                 data->if_a2dp = NULL;
2477                 tester_setup_failed();
2478                 return;
2479         }
2480
2481         tester_setup_complete();
2482 }
2483
2484 static void setup_avrcp(const void *test_data)
2485 {
2486         struct test_data *data = tester_get_data();
2487         const bt_interface_t *if_bt;
2488         bt_status_t status;
2489         const void *a2dp, *avrcp;
2490
2491         if (!setup_base(data)) {
2492                 tester_setup_failed();
2493                 return;
2494         }
2495
2496         if_bt = data->if_bluetooth;
2497
2498         status = if_bt->init(&bt_callbacks);
2499         if (status != BT_STATUS_SUCCESS) {
2500                 data->if_bluetooth = NULL;
2501                 tester_setup_failed();
2502                 return;
2503         }
2504
2505         a2dp = if_bt->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID);
2506         if (!a2dp) {
2507                 tester_setup_failed();
2508                 return;
2509         }
2510
2511         data->if_a2dp = a2dp;
2512
2513         status = data->if_a2dp->init(&bta2dp_callbacks);
2514         if (status != BT_STATUS_SUCCESS) {
2515                 data->if_a2dp = NULL;
2516                 tester_setup_failed();
2517                 return;
2518         }
2519
2520         avrcp = if_bt->get_profile_interface(BT_PROFILE_AV_RC_ID);
2521         if (!avrcp) {
2522                 tester_setup_failed();
2523                 return;
2524         }
2525
2526         data->if_avrcp = avrcp;
2527
2528         status = data->if_avrcp->init(&btavrcp_callbacks);
2529         if (status != BT_STATUS_SUCCESS) {
2530                 data->if_avrcp = NULL;
2531                 tester_setup_failed();
2532                 return;
2533         }
2534
2535         tester_setup_complete();
2536 }
2537
2538 static void setup_gatt(const void *test_data)
2539 {
2540         struct test_data *data = tester_get_data();
2541         bt_status_t status;
2542         const void *gatt;
2543
2544         if (!setup_base(data)) {
2545                 tester_setup_failed();
2546                 return;
2547         }
2548
2549         status = data->if_bluetooth->init(&bt_callbacks);
2550         if (status != BT_STATUS_SUCCESS) {
2551                 data->if_bluetooth = NULL;
2552                 tester_setup_failed();
2553                 return;
2554         }
2555
2556         gatt = data->if_bluetooth->get_profile_interface(BT_PROFILE_GATT_ID);
2557         if (!gatt) {
2558                 tester_setup_failed();
2559                 return;
2560         }
2561
2562         data->if_gatt = gatt;
2563
2564         status = data->if_gatt->init(&btgatt_callbacks);
2565         if (status != BT_STATUS_SUCCESS) {
2566                 data->if_gatt = NULL;
2567
2568                 tester_setup_failed();
2569                 return;
2570         }
2571
2572         tester_setup_complete();
2573 }
2574
2575 static void setup_map_client(const void *test_data)
2576 {
2577         struct test_data *data = tester_get_data();
2578         bt_status_t status;
2579         const void *map_client;
2580
2581         if (!setup_base(data)) {
2582                 tester_setup_failed();
2583                 return;
2584         }
2585
2586         status = data->if_bluetooth->init(&bt_callbacks);
2587         if (status != BT_STATUS_SUCCESS) {
2588                 data->if_bluetooth = NULL;
2589                 tester_setup_failed();
2590                 return;
2591         }
2592
2593         map_client = data->if_bluetooth->get_profile_interface(
2594                                                 BT_PROFILE_MAP_CLIENT_ID);
2595         if (!map_client) {
2596                 tester_setup_failed();
2597                 return;
2598         }
2599
2600         data->if_map_client = map_client;
2601
2602         status = data->if_map_client->init(&btmap_client_callbacks);
2603         if (status != BT_STATUS_SUCCESS) {
2604                 data->if_map_client = NULL;
2605
2606                 tester_setup_failed();
2607                 return;
2608         }
2609
2610         tester_setup_complete();
2611 }
2612
2613 static void teardown(const void *test_data)
2614 {
2615         struct test_data *data = tester_get_data();
2616
2617         queue_destroy(data->steps, NULL);
2618         data->steps = NULL;
2619
2620         queue_destroy(data->pdus, NULL);
2621         data->pdus = NULL;
2622
2623         /* no cleanup for map_client */
2624         data->if_map_client = NULL;
2625
2626         if (data->if_gatt) {
2627                 data->if_gatt->cleanup();
2628                 data->if_gatt = NULL;
2629         }
2630
2631         if (data->if_hid) {
2632                 data->if_hid->cleanup();
2633                 data->if_hid = NULL;
2634         }
2635
2636         if (data->if_pan) {
2637                 data->if_pan->cleanup();
2638                 data->if_pan = NULL;
2639         }
2640
2641         if (data->if_hdp) {
2642                 data->if_hdp->cleanup();
2643                 data->if_hdp = NULL;
2644         }
2645
2646         if (data->if_stream) {
2647                 data->audio->close_output_stream(data->audio, data->if_stream);
2648                 data->if_stream = NULL;
2649         }
2650
2651         if (data->if_a2dp) {
2652                 data->if_a2dp->cleanup();
2653                 data->if_a2dp = NULL;
2654         }
2655
2656         if (data->if_avrcp) {
2657                 data->if_avrcp->cleanup();
2658                 data->if_avrcp = NULL;
2659         }
2660
2661         if (data->if_bluetooth) {
2662                 data->if_bluetooth->cleanup();
2663                 data->if_bluetooth = NULL;
2664         }
2665
2666         data->device->close(data->device);
2667         audio_hw_device_close(data->audio);
2668
2669         /*
2670          * Ssp_request_cb pointer can be set do default_ssp_req_cb.
2671          * Set it back to ssp_request_cb
2672          */
2673         bt_callbacks.ssp_request_cb = ssp_request_cb;
2674
2675         if (!data->bluetoothd_pid)
2676                 tester_teardown_complete();
2677 }
2678
2679 static void emu_connectable_complete(uint16_t opcode, uint8_t status,
2680                                         const void *param, uint8_t len,
2681                                         void *user_data)
2682 {
2683         struct step *step;
2684         struct test_data *data = user_data;
2685
2686         switch (opcode) {
2687         case BT_HCI_CMD_WRITE_SCAN_ENABLE:
2688                 break;
2689         case BT_HCI_CMD_LE_SET_ADV_ENABLE:
2690                 /*
2691                  * For BREDRLE emulator we want to verify step after scan
2692                  * enable and not after le_set_adv_enable
2693                  */
2694                 if (data->hciemu_type == HCIEMU_TYPE_BREDRLE)
2695                         return;
2696
2697                 break;
2698         default:
2699                 return;
2700         }
2701
2702         step = g_new0(struct step, 1);
2703
2704         if (status) {
2705                 tester_warn("Emulated remote setup failed.");
2706                 step->action_status = BT_STATUS_FAIL;
2707         } else {
2708                 tester_debug("Emulated remote setup done.");
2709                 step->action_status = BT_STATUS_SUCCESS;
2710         }
2711
2712         schedule_action_verification(step);
2713 }
2714
2715 void emu_setup_powered_remote_action(void)
2716 {
2717         struct test_data *data = tester_get_data();
2718         struct bthost *bthost;
2719
2720         bthost = hciemu_client_get_host(data->hciemu);
2721         bthost_set_cmd_complete_cb(bthost, emu_connectable_complete, data);
2722
2723         if ((data->hciemu_type == HCIEMU_TYPE_LE) ||
2724                                 (data->hciemu_type == HCIEMU_TYPE_BREDRLE)) {
2725                 uint8_t adv[4];
2726
2727                 adv[0] = 0x02;  /* Field length */
2728                 adv[1] = 0x01;  /* Flags */
2729                 adv[2] = 0x02;  /* Flags value */
2730                 adv[3] = 0x00;  /* Field terminator */
2731
2732                 bthost_set_adv_data(bthost, adv, sizeof(adv));
2733                 bthost_set_adv_enable(bthost, 0x01);
2734         }
2735
2736         if (data->hciemu_type != HCIEMU_TYPE_LE)
2737                 bthost_write_scan_enable(bthost, 0x03);
2738 }
2739
2740 void emu_set_pin_code_action(void)
2741 {
2742         struct test_data *data = tester_get_data();
2743         struct step *current_data_step = queue_peek_head(data->steps);
2744         struct bt_action_data *action_data = current_data_step->set_data;
2745         struct bthost *bthost;
2746         struct step *step = g_new0(struct step, 1);
2747
2748         bthost = hciemu_client_get_host(data->hciemu);
2749
2750         bthost_set_pin_code(bthost, action_data->pin->pin,
2751                                                         action_data->pin_len);
2752
2753         step->action_status = BT_STATUS_SUCCESS;
2754
2755         tester_print("Setting emu pin done.");
2756
2757         schedule_action_verification(step);
2758 }
2759
2760 void emu_set_ssp_mode_action(void)
2761 {
2762         struct test_data *data = tester_get_data();
2763         struct bthost *bthost;
2764         struct step *step = g_new0(struct step, 1);
2765
2766         bthost = hciemu_client_get_host(data->hciemu);
2767
2768         bthost_write_ssp_mode(bthost, 0x01);
2769
2770         step->action_status = BT_STATUS_SUCCESS;
2771
2772         schedule_action_verification(step);
2773 }
2774
2775 void emu_set_connect_cb_action(void)
2776 {
2777         struct test_data *data = tester_get_data();
2778         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
2779         struct step *current_data_step = queue_peek_head(data->steps);
2780         void *cb = current_data_step->set_data;
2781         struct step *step = g_new0(struct step, 1);
2782
2783         bthost_set_connect_cb(bthost, cb, data);
2784
2785         step->action_status = BT_STATUS_SUCCESS;
2786
2787         schedule_action_verification(step);
2788 }
2789
2790 void emu_remote_connect_hci_action(void)
2791 {
2792         struct test_data *data = tester_get_data();
2793         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
2794         struct step *current_data_step = queue_peek_head(data->steps);
2795         struct bt_action_data *action_data = current_data_step->set_data;
2796         struct step *step = g_new0(struct step, 1);
2797         const uint8_t *master_addr;
2798
2799         master_addr = hciemu_get_master_bdaddr(data->hciemu);
2800
2801         tester_print("Trying to connect hci");
2802
2803         if (action_data)
2804                 bthost_hci_connect(bthost, master_addr,
2805                                                 action_data->bearer_type);
2806         else
2807                 bthost_hci_connect(bthost, master_addr, BDADDR_BREDR);
2808
2809         step->action_status = BT_STATUS_SUCCESS;
2810
2811         schedule_action_verification(step);
2812 }
2813
2814 void emu_remote_disconnect_hci_action(void)
2815 {
2816         struct test_data *data = tester_get_data();
2817         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
2818         struct step *current_data_step = queue_peek_head(data->steps);
2819         uint16_t *handle = current_data_step->set_data;
2820         struct step *step = g_new0(struct step, 1);
2821
2822         if (handle) {
2823                 bthost_hci_disconnect(bthost, *handle, 0x13);
2824                 step->action_status = BT_STATUS_SUCCESS;
2825         } else {
2826                 step->action_status = BT_STATUS_FAIL;
2827         }
2828
2829         schedule_action_verification(step);
2830 }
2831
2832 void emu_set_io_cap(void)
2833 {
2834         struct test_data *data = tester_get_data();
2835         struct bthost *bthost;
2836         struct step *current_data_step = queue_peek_head(data->steps);
2837         struct bt_action_data *action_data = current_data_step->set_data;
2838         struct step *step = g_new0(struct step, 1);
2839
2840         bthost = hciemu_client_get_host(data->hciemu);
2841
2842         if (action_data)
2843                 bthost_set_io_capability(bthost, action_data->io_cap);
2844         else
2845                 bthost_set_io_capability(bthost, 0x01);
2846
2847         step->action_status = BT_STATUS_SUCCESS;
2848
2849         schedule_action_verification(step);
2850 }
2851
2852 void emu_add_l2cap_server_action(void)
2853 {
2854         struct test_data *data = tester_get_data();
2855         struct step *current_data_step = queue_peek_head(data->steps);
2856         struct emu_set_l2cap_data *l2cap_data = current_data_step->set_data;
2857         struct bthost *bthost;
2858         struct step *step = g_new0(struct step, 1);
2859
2860         if (!l2cap_data) {
2861                 tester_warn("Invalid l2cap_data params");
2862                 step->action_status = BT_STATUS_FAIL;
2863                 goto done;
2864         }
2865
2866         bthost = hciemu_client_get_host(data->hciemu);
2867
2868         bthost_add_l2cap_server(bthost, l2cap_data->psm, l2cap_data->func,
2869                                                         l2cap_data->user_data);
2870
2871         step->action_status = BT_STATUS_SUCCESS;
2872
2873 done:
2874         schedule_action_verification(step);
2875 }
2876
2877 static void print_data(const char *str, void *user_data)
2878 {
2879         tester_debug("tester: %s", str);
2880 }
2881
2882 static void emu_generic_cid_hook_cb(const void *data, uint16_t len,
2883                                                                 void *user_data)
2884 {
2885         struct test_data *t_data = tester_get_data();
2886         struct emu_l2cap_cid_data *cid_data = user_data;
2887         const struct pdu_set *pdus = cid_data->pdu;
2888         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
2889         int i;
2890
2891         for (i = 0; pdus[i].rsp.iov_base; i++) {
2892                 if (pdus[i].req.iov_base) {
2893                         if (pdus[i].req.iov_len != len)
2894                                 continue;
2895
2896                         if (memcmp(pdus[i].req.iov_base, data, len))
2897                                 continue;
2898                 }
2899
2900                 if (pdus[i].rsp.iov_base) {
2901                         util_hexdump('>', pdus[i].rsp.iov_base,
2902                                         pdus[i].rsp.iov_len, print_data, NULL);
2903
2904                         /* if its sdp pdu use transaction ID from request */
2905                         if (cid_data->is_sdp) {
2906                                 struct iovec rsp[3];
2907
2908                                 rsp[0].iov_base = pdus[i].rsp.iov_base;
2909                                 rsp[0].iov_len = 1;
2910
2911                                 rsp[1].iov_base = ((uint8_t *) data) + 1;
2912                                 rsp[1].iov_len = 2;
2913
2914                                 rsp[2].iov_base = pdus[i].rsp.iov_base + 3;
2915                                 rsp[2].iov_len = pdus[i].rsp.iov_len - 3;
2916
2917                                 bthost_send_cid_v(bthost, cid_data->handle,
2918                                                         cid_data->cid, rsp, 3);
2919                         } else {
2920                                 bthost_send_cid_v(bthost, cid_data->handle,
2921                                                 cid_data->cid, &pdus[i].rsp, 1);
2922                         }
2923
2924                 }
2925         }
2926 }
2927
2928 void tester_handle_l2cap_data_exchange(struct emu_l2cap_cid_data *cid_data)
2929 {
2930         struct test_data *t_data = tester_get_data();
2931         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
2932
2933         bthost_add_cid_hook(bthost, cid_data->handle, cid_data->cid,
2934                                         emu_generic_cid_hook_cb, cid_data);
2935 }
2936
2937 void tester_generic_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
2938 {
2939         struct emu_l2cap_cid_data *cid_data = user_data;
2940
2941         cid_data->handle = handle;
2942         cid_data->cid = cid;
2943
2944         tester_handle_l2cap_data_exchange(cid_data);
2945 }
2946
2947 static void rfcomm_connect_cb(uint16_t handle, uint16_t cid, void *user_data,
2948                                                                 bool status)
2949 {
2950         struct step *step = g_new0(struct step, 1);
2951
2952         tester_print("Connect handle %d, cid %d cb status: %d", handle, cid,
2953                                                                         status);
2954
2955         step->action_status = BT_STATUS_SUCCESS;
2956
2957         schedule_action_verification(step);
2958 }
2959
2960 void emu_add_rfcomm_server_action(void)
2961 {
2962         struct test_data *data = tester_get_data();
2963         struct step *current_data_step = queue_peek_head(data->steps);
2964         struct bt_action_data *rfcomm_data = current_data_step->set_data;
2965         struct bthost *bthost;
2966         struct step *step;
2967
2968         if (!rfcomm_data) {
2969                 tester_warn("Invalid l2cap_data params");
2970                 return;
2971         }
2972
2973         step = g_new0(struct step, 1);
2974
2975         bthost = hciemu_client_get_host(data->hciemu);
2976
2977         bthost_add_rfcomm_server(bthost, rfcomm_data->channel,
2978                                                 rfcomm_connect_cb, data);
2979
2980         step->action_status = BT_STATUS_SUCCESS;
2981
2982         schedule_action_verification(step);
2983 }
2984
2985 void dummy_action(void)
2986 {
2987         struct step *step = g_new0(struct step, 1);
2988
2989         step->action = dummy_action;
2990
2991         schedule_action_verification(step);
2992 }
2993
2994 void bluetooth_enable_action(void)
2995 {
2996         struct test_data *data = tester_get_data();
2997         struct step *step = g_new0(struct step, 1);
2998
2999         step->action_status = data->if_bluetooth->enable();
3000
3001         schedule_action_verification(step);
3002 }
3003
3004 void bluetooth_disable_action(void)
3005 {
3006         struct test_data *data = tester_get_data();
3007         struct step *step = g_new0(struct step, 1);
3008
3009         step->action_status = data->if_bluetooth->disable();
3010
3011         schedule_action_verification(step);
3012 }
3013
3014 void bt_set_property_action(void)
3015 {
3016         struct test_data *data = tester_get_data();
3017         struct step *step;
3018         struct step *current_data_step = queue_peek_head(data->steps);
3019         bt_property_t *prop;
3020
3021         if (!current_data_step->set_data) {
3022                 tester_debug("BT property not set for step");
3023                 tester_test_failed();
3024                 return;
3025         }
3026
3027         step = g_new0(struct step, 1);
3028
3029         prop = (bt_property_t *)current_data_step->set_data;
3030
3031         step->action_status = data->if_bluetooth->set_adapter_property(prop);
3032
3033         schedule_action_verification(step);
3034 }
3035
3036 void bt_get_property_action(void)
3037 {
3038         struct test_data *data = tester_get_data();
3039         struct step *step;
3040         struct step *current_data_step = queue_peek_head(data->steps);
3041         bt_property_t *prop;
3042
3043         if (!current_data_step->set_data) {
3044                 tester_debug("BT property to get not defined");
3045                 tester_test_failed();
3046                 return;
3047         }
3048
3049         step = g_new0(struct step, 1);
3050
3051         prop = (bt_property_t *)current_data_step->set_data;
3052
3053         step->action_status = data->if_bluetooth->get_adapter_property(
3054                                                                 prop->type);
3055
3056         schedule_action_verification(step);
3057 }
3058
3059 void bt_start_discovery_action(void)
3060 {
3061         struct test_data *data = tester_get_data();
3062         struct step *step = g_new0(struct step, 1);
3063
3064         step->action_status = data->if_bluetooth->start_discovery();
3065
3066         schedule_action_verification(step);
3067 }
3068
3069 void bt_cancel_discovery_action(void)
3070 {
3071         struct test_data *data = tester_get_data();
3072         struct step *step = g_new0(struct step, 1);
3073
3074         step->action_status = data->if_bluetooth->cancel_discovery();
3075
3076         schedule_action_verification(step);
3077 }
3078
3079 void bt_get_device_props_action(void)
3080 {
3081         struct test_data *data = tester_get_data();
3082         struct step *current_data_step = queue_peek_head(data->steps);
3083         struct step *step;
3084
3085         if (!current_data_step->set_data) {
3086                 tester_debug("bdaddr not defined");
3087                 tester_test_failed();
3088                 return;
3089         }
3090
3091         step = g_new0(struct step, 1);
3092
3093         step->action_status =
3094                 data->if_bluetooth->get_remote_device_properties(
3095                                                 current_data_step->set_data);
3096
3097         schedule_action_verification(step);
3098 }
3099
3100 void bt_get_device_prop_action(void)
3101 {
3102         struct test_data *data = tester_get_data();
3103         struct step *current_data_step = queue_peek_head(data->steps);
3104         struct bt_action_data *action_data = current_data_step->set_data;
3105         struct step *step;
3106
3107         if (!action_data) {
3108                 tester_warn("No arguments for 'get remote device prop' req.");
3109                 tester_test_failed();
3110                 return;
3111         }
3112
3113         step = g_new0(struct step, 1);
3114
3115         step->action_status = data->if_bluetooth->get_remote_device_property(
3116                                                         action_data->addr,
3117                                                         action_data->prop_type);
3118
3119         schedule_action_verification(step);
3120 }
3121
3122 void bt_set_device_prop_action(void)
3123 {
3124         struct test_data *data = tester_get_data();
3125         struct step *current_data_step = queue_peek_head(data->steps);
3126         struct bt_action_data *action_data = current_data_step->set_data;
3127         struct step *step;
3128
3129         if (!action_data) {
3130                 tester_warn("No arguments for 'set remote device prop' req.");
3131                 tester_test_failed();
3132                 return;
3133         }
3134
3135         step = g_new0(struct step, 1);
3136
3137         step->action_status = data->if_bluetooth->set_remote_device_property(
3138                                                         action_data->addr,
3139                                                         action_data->prop);
3140
3141         schedule_action_verification(step);
3142 }
3143
3144 void bt_create_bond_action(void)
3145 {
3146         struct test_data *data = tester_get_data();
3147         struct step *current_data_step = queue_peek_head(data->steps);
3148         struct bt_action_data *action_data = current_data_step->set_data;
3149         struct step *step;
3150
3151         if (!action_data || !action_data->addr) {
3152                 tester_warn("Bad arguments for 'create bond' req.");
3153                 tester_test_failed();
3154                 return;
3155         }
3156
3157         step = g_new0(struct step, 1);
3158
3159         step->action_status =
3160                         data->if_bluetooth->create_bond(action_data->addr,
3161                                                 action_data->transport_type ?
3162                                                 action_data->transport_type :
3163                                                 BT_TRANSPORT_UNKNOWN);
3164
3165         schedule_action_verification(step);
3166 }
3167
3168 void bt_pin_reply_accept_action(void)
3169 {
3170         struct test_data *data = tester_get_data();
3171         struct step *current_data_step = queue_peek_head(data->steps);
3172         struct bt_action_data *action_data = current_data_step->set_data;
3173         struct step *step;
3174
3175         if (!action_data || !action_data->addr || !action_data->pin) {
3176                 tester_warn("Bad arguments for 'pin reply' req.");
3177                 tester_test_failed();
3178                 return;
3179         }
3180
3181         step = g_new0(struct step, 1);
3182
3183         step->action_status = data->if_bluetooth->pin_reply(action_data->addr,
3184                                                         TRUE,
3185                                                         action_data->pin_len,
3186                                                         action_data->pin);
3187
3188         schedule_action_verification(step);
3189 }
3190
3191 void bt_ssp_reply_accept_action(void)
3192 {
3193         struct test_data *data = tester_get_data();
3194         struct step *current_data_step = queue_peek_head(data->steps);
3195         struct bt_action_data *action_data = current_data_step->set_data;
3196         struct step *step = g_new0(struct step, 1);
3197
3198         step->action_status = data->if_bluetooth->ssp_reply(action_data->addr,
3199                                                 action_data->ssp_variant,
3200                                                 action_data->accept, 0);
3201
3202         schedule_action_verification(step);
3203 }
3204
3205 void bt_cancel_bond_action(void)
3206 {
3207         struct test_data *data = tester_get_data();
3208         struct step *current_data_step = queue_peek_head(data->steps);
3209         bt_bdaddr_t *addr = current_data_step->set_data;
3210         struct step *step = g_new0(struct step, 1);
3211
3212         step->action_status = data->if_bluetooth->cancel_bond(addr);
3213
3214         schedule_action_verification(step);
3215 }
3216
3217 void bt_remove_bond_action(void)
3218 {
3219         struct test_data *data = tester_get_data();
3220         struct step *current_data_step = queue_peek_head(data->steps);
3221         bt_bdaddr_t *addr = current_data_step->set_data;
3222         struct step *step = g_new0(struct step, 1);
3223
3224         step->action_status = data->if_bluetooth->remove_bond(addr);
3225
3226         schedule_action_verification(step);
3227 }
3228
3229 static void default_ssp_req_cb(bt_bdaddr_t *remote_bd_addr, bt_bdname_t *name,
3230                                 uint32_t cod, bt_ssp_variant_t pairing_variant,
3231                                 uint32_t pass_key)
3232 {
3233         struct test_data *t_data = tester_get_data();
3234
3235         t_data->if_bluetooth->ssp_reply(remote_bd_addr, pairing_variant, true,
3236                                                                 pass_key);
3237 }
3238
3239 void set_default_ssp_request_handler(void)
3240 {
3241         struct step *step = g_new0(struct step, 1);
3242
3243         bt_callbacks.ssp_request_cb = default_ssp_req_cb;
3244
3245         step->action_status = BT_STATUS_SUCCESS;
3246
3247         schedule_action_verification(step);
3248 }
3249
3250 static void generic_test_function(const void *test_data)
3251 {
3252         struct test_data *data = tester_get_data();
3253         struct step *first_step;
3254
3255         init_test_steps(data);
3256
3257         /* first step action */
3258         first_step = queue_peek_head(data->steps);
3259         if (!first_step->action) {
3260                 tester_print("tester: No initial action declared");
3261                 tester_test_failed();
3262                 return;
3263         }
3264         first_step->action();
3265 }
3266
3267 #define test(data, test_setup, test, test_teardown) \
3268         do { \
3269                 struct test_data *user; \
3270                 user = g_malloc0(sizeof(struct test_data)); \
3271                 if (!user) \
3272                         break; \
3273                 user->hciemu_type = data->emu_type; \
3274                 user->test_data = data; \
3275                 tester_add_full(data->title, data, test_pre_setup, \
3276                                         test_setup, test, test_teardown, \
3277                                         test_post_teardown, 3, user, g_free); \
3278         } while (0)
3279
3280 static void tester_testcases_cleanup(void)
3281 {
3282         remove_bluetooth_tests();
3283         remove_socket_tests();
3284         remove_hidhost_tests();
3285         remove_gatt_tests();
3286         remove_a2dp_tests();
3287         remove_avrcp_tests();
3288         remove_hdp_tests();
3289         remove_pan_tests();
3290 }
3291
3292 static void add_bluetooth_tests(void *data, void *user_data)
3293 {
3294         struct test_case *tc = data;
3295
3296         test(tc, setup, generic_test_function, teardown);
3297 }
3298
3299 static void add_socket_tests(void *data, void *user_data)
3300 {
3301         struct test_case *tc = data;
3302
3303         test(tc, setup_socket, generic_test_function, teardown);
3304 }
3305
3306 static void add_hidhost_tests(void *data, void *user_data)
3307 {
3308         struct test_case *tc = data;
3309
3310         test(tc, setup_hidhost, generic_test_function, teardown);
3311 }
3312
3313 static void add_pan_tests(void *data, void *user_data)
3314 {
3315         struct test_case *tc = data;
3316
3317         test(tc, setup_pan, generic_test_function, teardown);
3318 }
3319
3320 static void add_hdp_tests(void *data, void *user_data)
3321 {
3322         struct test_case *tc = data;
3323
3324         test(tc, setup_hdp, generic_test_function, teardown);
3325 }
3326
3327 static void add_a2dp_tests(void *data, void *user_data)
3328 {
3329         struct test_case *tc = data;
3330
3331         test(tc, setup_a2dp, generic_test_function, teardown);
3332 }
3333
3334 static void add_avrcp_tests(void *data, void *user_data)
3335 {
3336         struct test_case *tc = data;
3337
3338         test(tc, setup_avrcp, generic_test_function, teardown);
3339 }
3340
3341 static void add_gatt_tests(void *data, void *user_data)
3342 {
3343         struct test_case *tc = data;
3344
3345         test(tc, setup_gatt, generic_test_function, teardown);
3346 }
3347
3348 static void add_map_client_tests(void *data, void *user_data)
3349 {
3350         struct test_case *tc = data;
3351
3352         test(tc, setup_map_client, generic_test_function, teardown);
3353 }
3354
3355 int main(int argc, char *argv[])
3356 {
3357         snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));
3358
3359         tester_init(&argc, &argv);
3360
3361         queue_foreach(get_bluetooth_tests(), add_bluetooth_tests, NULL);
3362         queue_foreach(get_socket_tests(), add_socket_tests, NULL);
3363         queue_foreach(get_hidhost_tests(), add_hidhost_tests, NULL);
3364         queue_foreach(get_pan_tests(), add_pan_tests, NULL);
3365         queue_foreach(get_hdp_tests(), add_hdp_tests, NULL);
3366         queue_foreach(get_a2dp_tests(), add_a2dp_tests, NULL);
3367         queue_foreach(get_avrcp_tests(), add_avrcp_tests, NULL);
3368         queue_foreach(get_gatt_tests(), add_gatt_tests, NULL);
3369         queue_foreach(get_map_client_tests(), add_map_client_tests, NULL);
3370
3371         if (tester_run())
3372                 return 1;
3373
3374         tester_testcases_cleanup();
3375
3376         return 0;
3377 }