2 * Copyright (C) 2014 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/signalfd.h>
25 #include "lib/bluetooth.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"
35 static char exec_dir[PATH_MAX + 1];
37 static gint scheduled_cbacks_num;
39 #define EMULATOR_SIGNAL_TIMEOUT 2 /* in seconds */
40 #define EMULATOR_SIGNAL "emulator_started"
42 #define BT_TRANSPORT_UNKNOWN 0x00
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),
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),
71 DBG_CB(CB_PAN_CONTROL_STATE),
72 DBG_CB(CB_PAN_CONNECTION_STATE),
75 DBG_CB(CB_HDP_APP_REG_STATE),
76 DBG_CB(CB_HDP_CHANNEL_STATE),
79 DBG_CB(CB_A2DP_CONN_STATE),
80 DBG_CB(CB_A2DP_AUDIO_STATE),
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),
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),
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),
127 DBG_CB(CB_MAP_CLIENT_REMOTE_MAS_INSTANCES),
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),
141 static gboolean check_callbacks_called(gpointer user_data)
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.
149 if (g_atomic_int_get(&scheduled_cbacks_num) == 0) {
150 tester_teardown_complete();
152 } else if (scheduled_cbacks_num < 0) {
153 tester_warn("Unscheduled callback called!");
160 static void check_daemon_term(void)
164 struct test_data *data = tester_get_data();
169 pid = waitpid(data->bluetoothd_pid, &status, WNOHANG);
170 if (pid != data->bluetoothd_pid)
173 data->bluetoothd_pid = 0;
175 if (WIFEXITED(status) && (WEXITSTATUS(status) == EXIT_SUCCESS)) {
176 g_idle_add(check_callbacks_called, NULL);
180 tester_warn("Unexpected Daemon shutdown with status %d", status);
183 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
186 struct signalfd_siginfo si;
190 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
193 fd = g_io_channel_unix_get_fd(channel);
195 result = read(fd, &si, sizeof(si));
196 if (result != sizeof(si))
199 switch (si.ssi_signo) {
208 static guint setup_signalfd(void)
216 sigaddset(&mask, SIGCHLD);
218 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
221 fd = signalfd(-1, &mask, 0);
225 channel = g_io_channel_unix_new(fd);
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);
231 source = g_io_add_watch(channel,
232 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
233 signal_handler, NULL);
235 g_io_channel_unref(channel);
240 static void test_post_teardown(const void *test_data)
242 struct test_data *data = tester_get_data();
244 /* remove hook for encryption change */
245 hciemu_del_hook(data->hciemu, HCIEMU_HOOK_POST_EVT, 0x08);
247 hciemu_unref(data->hciemu);
250 g_source_remove(data->signalfd);
254 static void bluetoothd_start(int hci_index)
256 char prg_name[PATH_MAX + 1];
260 snprintf(prg_name, sizeof(prg_name), "%s/%s", exec_dir, "bluetoothd");
261 snprintf(index, sizeof(index), "%d", hci_index);
263 prg_argv[0] = prg_name;
269 if (!tester_use_debug())
272 execve(prg_argv[0], prg_argv, NULL);
275 static void emulator(int pipe, int hci_index)
277 static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
279 struct sockaddr_un addr;
284 fd = socket(PF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
288 tv.tv_sec = EMULATOR_SIGNAL_TIMEOUT;
290 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
292 memset(&addr, 0, sizeof(addr));
293 addr.sun_family = AF_UNIX;
294 memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
296 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
297 perror("Failed to bind system socket");
301 len = write(pipe, EMULATOR_SIGNAL, sizeof(EMULATOR_SIGNAL));
302 if (len != sizeof(EMULATOR_SIGNAL))
305 memset(buf, 0, sizeof(buf));
307 len = read(fd, buf, sizeof(buf));
308 if (len <= 0 || strcmp(buf, "bluetooth.start=daemon"))
313 return bluetoothd_start(hci_index);
322 static void mgmt_debug(const char *str, void *user_data)
324 const char *prefix = user_data;
326 tester_print("%s%s", prefix, str);
329 static bool hciemu_post_encr_hook(const void *data, uint16_t len,
335 * Expected data: status (1 octet) + conn. handle (2 octets) +
336 * encryption flag (1 octet)
341 step = g_new0(struct step, 1);
343 step->callback = ((uint8_t *)data)[3] ? CB_EMU_ENCRYPTION_ENABLED :
344 CB_EMU_ENCRYPTION_DISABLED;
346 schedule_callback_verification(step);
350 static void read_info_callback(uint8_t status, uint16_t length,
351 const void *param, void *user_data)
353 struct test_data *data = tester_get_data();
354 const struct mgmt_rp_read_info *rp = param;
356 uint16_t manufacturer;
357 uint32_t supported_settings, current_settings;
359 tester_print("Read Info callback");
360 tester_print(" Status: 0x%02x", status);
362 if (status || !param) {
363 tester_pre_setup_failed();
367 ba2str(&rp->bdaddr, addr);
368 manufacturer = btohs(rp->manufacturer);
369 supported_settings = btohl(rp->supported_settings);
370 current_settings = btohl(rp->current_settings);
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);
382 if (strcmp(hciemu_get_address(data->hciemu), addr)) {
383 tester_pre_setup_failed();
387 /* set hook for encryption change */
388 hciemu_add_hook(data->hciemu, HCIEMU_HOOK_POST_EVT, 0x08,
389 hciemu_post_encr_hook, NULL);
391 tester_pre_setup_complete();
394 static void index_added_callback(uint16_t index, uint16_t length,
395 const void *param, void *user_data)
397 struct test_data *data = tester_get_data();
399 tester_print("Index Added callback");
400 tester_print(" Index: 0x%04x", index);
402 data->mgmt_index = index;
404 mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
405 read_info_callback, NULL, NULL);
408 static void index_removed_callback(uint16_t index, uint16_t length,
409 const void *param, void *user_data)
411 struct test_data *data = tester_get_data();
413 tester_print("Index Removed callback");
414 tester_print(" Index: 0x%04x", index);
416 if (index != data->mgmt_index)
419 mgmt_unregister_index(data->mgmt, data->mgmt_index);
421 mgmt_unref(data->mgmt);
424 tester_post_teardown_complete();
427 static void read_index_list_callback(uint8_t status, uint16_t length,
428 const void *param, void *user_data)
430 struct test_data *data = tester_get_data();
432 tester_print("Read Index List callback");
433 tester_print(" Status: 0x%02x", status);
435 if (status || !param) {
436 tester_pre_setup_failed();
440 mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
441 index_added_callback, NULL, NULL);
443 mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
444 index_removed_callback, NULL, NULL);
446 data->hciemu = hciemu_new(data->hciemu_type);
448 tester_warn("Failed to setup HCI emulation");
449 tester_pre_setup_failed();
453 tester_print("New hciemu instance created");
456 static void test_pre_setup(const void *test_data)
458 struct test_data *data = tester_get_data();
460 data->signalfd = setup_signalfd();
461 if (!data->signalfd) {
462 tester_warn("Failed to setup signalfd");
463 tester_pre_setup_failed();
467 data->mgmt = mgmt_new_default();
469 tester_warn("Failed to setup management interface");
470 tester_pre_setup_failed();
474 if (!tester_use_debug())
477 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
479 mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0,
480 NULL, read_index_list_callback, NULL, NULL);
483 static bool match_property(bt_property_t *exp_prop, bt_property_t *rec_prop,
486 if (exp_prop->type && (exp_prop->type != rec_prop->type))
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,
496 if (exp_prop->val && memcmp(exp_prop->val, rec_prop->val,
498 tester_debug("Property [%d] value don't match!", prop_num);
505 static bool match_mas_inst(btmce_mas_instance_t *exp_inst,
506 btmce_mas_instance_t *rec_inst, int inst_num)
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);
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);
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);
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);
536 static int verify_property(bt_property_t *exp_props, int exp_num_props,
537 bt_property_t *rec_props, int rec_num_props)
540 int exp_prop_to_find = exp_num_props;
542 if (rec_num_props == 0)
545 if (exp_num_props == 0) {
546 tester_debug("Wrong number of expected properties given");
547 tester_test_failed();
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)) {
561 return exp_prop_to_find;
564 static int verify_mas_inst(btmce_mas_instance_t *exp_inst, int exp_num_inst,
565 btmce_mas_instance_t *rec_inst,
569 int exp_inst_to_find = exp_num_inst;
571 if (rec_num_inst == 0)
574 if (exp_num_inst == 0) {
575 tester_debug("Wrong number of expected MAS instances given");
576 tester_test_failed();
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)) {
589 return exp_inst_to_find;
593 * Check each test case step if test case expected
594 * data is set and match it with expected result.
597 static bool verify_gatt_ids(btgatt_gatt_id_t *a, btgatt_gatt_id_t *b)
600 if (memcmp(&a->uuid, &b->uuid, sizeof(bt_uuid_t)))
603 if (a->inst_id != b->inst_id)
609 static bool verify_services(btgatt_srvc_id_t *a, btgatt_srvc_id_t *b)
611 if (a->is_primary != b->is_primary)
614 return verify_gatt_ids(&a->id, &b->id);
617 static bool match_data(struct step *step)
619 struct test_data *data = tester_get_data();
620 const struct step *exp;
622 exp = queue_peek_head(data->steps);
625 /* Can occure while test passed already */
626 tester_debug("Cannot get step to match");
630 if (exp->action_status != step->action_status) {
631 tester_debug("Action status don't match");
635 if (!exp->callback && !step->callback)
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
784 if (exp->callback_result.rc_index !=
785 step->callback_result.rc_index) {
786 tester_debug("Callback rc_index mismatch");
790 if (exp->callback_result.num_of_attrs !=
791 step->callback_result.num_of_attrs) {
792 tester_debug("Callback rc num of attrs mismatch");
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");
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);
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);
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);
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);
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");
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");
852 if (exp->callback_result.characteristic) {
855 a = step->callback_result.characteristic;
856 b = exp->callback_result.characteristic;
858 if (!verify_gatt_ids(a, b)) {
859 tester_debug("Gatt char doesn't match");
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);
871 if (exp->callback_result.descriptor) {
874 a = step->callback_result.descriptor;
875 b = exp->callback_result.descriptor;
877 if (!verify_gatt_ids(a, b)) {
878 tester_debug("Gatt desc doesn't match");
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");
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");
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");
908 if (exp->callback_result.notification_registered !=
909 step->callback_result.notification_registered) {
910 tester_debug("Gatt registered flag mismatch");
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");
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);
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);
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);
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);
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");
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);
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);
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);
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);
999 if (!exp->callback_result.value ||
1000 !step->callback_result.value) {
1001 tester_debug("Gatt attr values are wrong set");
1004 if (!memcmp(exp->callback_result.value,
1005 step->callback_result.value,
1006 exp->callback_result.length)) {
1007 tester_debug("Gatt attr value mismatch");
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);
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);
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);
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");
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);
1050 if (exp->store_srvc_handle)
1051 memcpy(exp->store_srvc_handle,
1052 step->callback_result.srvc_handle,
1053 sizeof(*exp->store_srvc_handle));
1055 if (exp->store_char_handle)
1056 memcpy(exp->store_char_handle,
1057 step->callback_result.char_handle,
1058 sizeof(*exp->store_char_handle));
1063 static void init_test_steps(struct test_data *data)
1065 const struct test_case *test_steps = data->test_data;
1068 for (i = 0; i < test_steps->step_num; i++)
1069 queue_push_tail(data->steps, (void *) &(test_steps->step[i]));
1071 tester_print("tester: Number of test steps=%d",
1072 queue_length(data->steps));
1076 * Each test case step should be verified, if match with
1077 * expected result tester should go to next test step.
1079 static void verify_step(struct step *step, queue_destroy_func_t cleanup_cb)
1081 struct test_data *data = tester_get_data();
1082 const struct test_case *test_steps = data->test_data;
1083 struct step *next_step;
1085 tester_debug("tester: STEP[%d] check",
1086 test_steps->step_num-queue_length(data->steps) + 1);
1088 if (step && !match_data(step)) {
1095 queue_pop_head(data->steps);
1100 tester_debug("tester: STEP[%d] pass",
1101 test_steps->step_num-queue_length(data->steps));
1103 if (queue_isempty(data->steps)) {
1104 tester_print("tester: All steps done, passing");
1105 tester_test_passed();
1110 /* goto next step action if declared in step */
1111 next_step = queue_peek_head(data->steps);
1113 if (next_step->action)
1114 next_step->action();
1119 * Its mandatory for callback to set proper step.callback value so that
1120 * step verification could pass and move to next test step
1123 static void free_properties(struct step *step)
1125 bt_property_t *properties = step->callback_result.properties;
1126 int num_properties = step->callback_result.num_properties;
1129 for (i = 0; i < num_properties; i++)
1130 g_free(properties[i].val);
1135 static void free_mas_instances(struct step *step)
1137 btmce_mas_instance_t *mas_instances;
1141 mas_instances = step->callback_result.mas_instances;
1142 num_instances = step->callback_result.num_mas_instances;
1144 for (i = 0; i < num_instances; i++)
1145 g_free(mas_instances[i].p_name);
1147 g_free(mas_instances);
1150 static void destroy_callback_step(void *data)
1152 struct step *step = data;
1154 if (step->callback_result.properties)
1155 free_properties(step);
1157 if (step->callback_result.service)
1158 free(step->callback_result.service);
1160 if (step->callback_result.characteristic)
1161 free(step->callback_result.characteristic);
1163 if (step->callback_result.descriptor)
1164 free(step->callback_result.descriptor);
1166 if (step->callback_result.included)
1167 free(step->callback_result.included);
1169 if (step->callback_result.read_params)
1170 free(step->callback_result.read_params);
1172 if (step->callback_result.write_params)
1173 free(step->callback_result.write_params);
1175 if (step->callback_result.notify_params)
1176 free(step->callback_result.notify_params);
1178 if (step->callback_result.srvc_handle)
1179 free(step->callback_result.srvc_handle);
1181 if (step->callback_result.inc_srvc_handle)
1182 free(step->callback_result.inc_srvc_handle);
1184 if (step->callback_result.uuid)
1185 free(step->callback_result.uuid);
1187 if (step->callback_result.char_handle)
1188 free(step->callback_result.char_handle);
1190 if (step->callback_result.desc_handle)
1191 free(step->callback_result.desc_handle);
1193 if (step->callback_result.attr_handle)
1194 free(step->callback_result.attr_handle);
1196 if (step->callback_result.value)
1197 free(step->callback_result.value);
1199 if (step->callback_result.mas_instances)
1200 free_mas_instances(step);
1203 g_atomic_int_dec_and_test(&scheduled_cbacks_num);
1206 static gboolean verify_action(gpointer user_data)
1208 struct step *step = user_data;
1210 verify_step(step, g_free);
1215 static gboolean verify_callback(gpointer user_data)
1217 struct test_data *data = tester_get_data();
1218 struct step *step = user_data;
1220 /* Return if callback came when all steps are already verified */
1221 if (queue_isempty(data->steps)) {
1222 destroy_callback_step(step);
1227 * TODO: This may call action from next step before callback data
1228 * from previous step was freed.
1230 verify_step(step, destroy_callback_step);
1235 void schedule_callback_verification(struct step *step)
1237 g_atomic_int_inc(&scheduled_cbacks_num);
1238 g_idle_add(verify_callback, step);
1241 void schedule_action_verification(struct step *step)
1243 g_idle_add_full(G_PRIORITY_HIGH_IDLE, verify_action, step, NULL);
1246 static void adapter_state_changed_cb(bt_state_t state)
1248 struct step *step = g_new0(struct step, 1);
1250 step->callback_result.state = state;
1251 step->callback = CB_BT_ADAPTER_STATE_CHANGED;
1253 schedule_callback_verification(step);
1256 static bt_property_t *copy_properties(int num_properties,
1257 bt_property_t *properties)
1260 bt_property_t *props = g_new0(bt_property_t, num_properties);
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);
1271 static bt_property_t *repack_properties(int num_properties,
1272 bt_property_t **properties)
1275 bt_property_t *props = g_new0(bt_property_t, num_properties);
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);
1286 static bt_property_t *create_property(bt_property_type_t type, void *val,
1289 bt_property_t *prop = g_new0(bt_property_t, 1);
1293 prop->val = g_memdup(val, len);
1298 static void adapter_properties_cb(bt_status_t status, int num_properties,
1299 bt_property_t *properties)
1301 struct step *step = g_new0(struct step, 1);
1303 step->callback_result.status = status;
1304 step->callback_result.num_properties = num_properties;
1305 step->callback_result.properties = copy_properties(num_properties,
1307 step->callback = CB_BT_ADAPTER_PROPERTIES;
1309 schedule_callback_verification(step);
1312 static void discovery_state_changed_cb(bt_discovery_state_t state)
1314 struct step *step = g_new0(struct step, 1);
1316 step->callback = CB_BT_DISCOVERY_STATE_CHANGED;
1317 step->callback_result.state = state;
1319 schedule_callback_verification(step);
1322 static void device_found_cb(int num_properties, bt_property_t *properties)
1324 struct step *step = g_new0(struct step, 1);
1326 step->callback_result.num_properties = num_properties;
1327 step->callback_result.properties = copy_properties(num_properties,
1330 step->callback = CB_BT_DEVICE_FOUND;
1332 schedule_callback_verification(step);
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)
1339 struct step *step = g_new0(struct step, 1);
1341 step->callback_result.num_properties = num_properties;
1342 step->callback_result.properties = copy_properties(num_properties,
1345 step->callback = CB_BT_REMOTE_DEVICE_PROPERTIES;
1347 schedule_callback_verification(step);
1350 static void bond_state_changed_cb(bt_status_t status,
1351 bt_bdaddr_t *remote_bd_addr,
1352 bt_bond_state_t state)
1354 struct step *step = g_new0(struct step, 1);
1356 step->callback_result.status = status;
1357 step->callback_result.state = state;
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));
1365 step->callback = CB_BT_BOND_STATE_CHANGED;
1367 schedule_callback_verification(step);
1370 static void pin_request_cb(bt_bdaddr_t *remote_bd_addr,
1371 bt_bdname_t *bd_name, uint32_t cod)
1373 struct step *step = g_new0(struct step, 1);
1374 bt_property_t *props[3];
1376 step->callback = CB_BT_PIN_REQUEST;
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,
1386 step->callback_result.num_properties = 3;
1387 step->callback_result.properties = repack_properties(3, props);
1389 g_free(props[0]->val);
1391 g_free(props[1]->val);
1393 g_free(props[2]->val);
1396 schedule_callback_verification(step);
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,
1404 struct step *step = g_new0(struct step, 1);
1405 bt_property_t *props[3];
1407 step->callback = CB_BT_SSP_REQUEST;
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,
1417 step->callback_result.num_properties = 3;
1418 step->callback_result.properties = repack_properties(3, props);
1420 g_free(props[0]->val);
1422 g_free(props[1]->val);
1424 g_free(props[2]->val);
1427 schedule_callback_verification(step);
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);
1435 step->callback = CB_BT_ACL_STATE_CHANGED;
1437 step->callback_result.status = status;
1438 step->callback_result.state = state;
1440 schedule_callback_verification(step);
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,
1460 static void hidhost_connection_state_cb(bt_bdaddr_t *bd_addr,
1461 bthh_connection_state_t state)
1463 struct step *step = g_new0(struct step, 1);
1465 step->callback = CB_HH_CONNECTION_STATE;
1466 step->callback_result.state = state;
1468 schedule_callback_verification(step);
1471 static void hidhost_virtual_unplug_cb(bt_bdaddr_t *bd_addr, bthh_status_t status)
1473 struct step *step = g_new0(struct step, 1);
1475 step->callback = CB_HH_VIRTUAL_UNPLUG;
1476 step->callback_result.status = status;
1478 schedule_callback_verification(step);
1481 static void hidhost_protocol_mode_cb(bt_bdaddr_t *bd_addr,
1482 bthh_status_t status,
1483 bthh_protocol_mode_t mode)
1485 struct step *step = g_new0(struct step, 1);
1487 step->callback = CB_HH_PROTOCOL_MODE;
1488 step->callback_result.status = status;
1489 step->callback_result.mode = mode;
1491 /* TODO: add bdaddr to verify? */
1493 schedule_callback_verification(step);
1496 static void hidhost_hid_info_cb(bt_bdaddr_t *bd_addr, bthh_hid_info_t hid)
1498 struct step *step = g_new0(struct step, 1);
1500 step->callback = CB_HH_HID_INFO;
1502 schedule_callback_verification(step);
1505 static void hidhost_get_report_cb(bt_bdaddr_t *bd_addr, bthh_status_t status,
1506 uint8_t *report, int size)
1508 struct step *step = g_new0(struct step, 1);
1510 step->callback = CB_HH_GET_REPORT;
1512 step->callback_result.status = status;
1513 step->callback_result.report_size = size;
1515 schedule_callback_verification(step);
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,
1529 static void gattc_register_client_cb(int status, int client_if,
1530 bt_uuid_t *app_uuid)
1532 struct step *step = g_new0(struct step, 1);
1534 step->callback = CB_GATTC_REGISTER_CLIENT;
1536 step->callback_result.status = status;
1538 schedule_callback_verification(step);
1541 static void gattc_scan_result_cb(bt_bdaddr_t *bda, int rssi, uint8_t *adv_data)
1543 struct step *step = g_new0(struct step, 1);
1544 bt_property_t *props[2];
1546 step->callback = CB_GATTC_SCAN_RESULT;
1547 step->callback_result.adv_data = adv_data ? TRUE : FALSE;
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,
1554 step->callback_result.num_properties = 2;
1555 step->callback_result.properties = repack_properties(2, props);
1557 g_free(props[0]->val);
1559 g_free(props[1]->val);
1562 schedule_callback_verification(step);
1565 static void gattc_connect_cb(int conn_id, int status, int client_if,
1568 struct step *step = g_new0(struct step, 1);
1569 bt_property_t *props[1];
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;
1576 /* Utilize property verification mechanism for bdaddr */
1577 props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1579 step->callback_result.num_properties = 1;
1580 step->callback_result.properties = repack_properties(1, props);
1582 g_free(props[0]->val);
1585 schedule_callback_verification(step);
1588 static void gattc_disconnect_cb(int conn_id, int status, int client_if,
1591 struct step *step = g_new0(struct step, 1);
1592 bt_property_t *props[1];
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;
1599 /* Utilize property verification mechanism for bdaddr */
1600 props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1602 step->callback_result.num_properties = 1;
1603 step->callback_result.properties = repack_properties(1, props);
1605 g_free(props[0]->val);
1608 schedule_callback_verification(step);
1611 static void gattc_listen_cb(int status, int server_if)
1613 struct step *step = g_new0(struct step, 1);
1615 step->callback = CB_GATTC_LISTEN;
1616 step->callback_result.status = status;
1618 schedule_callback_verification(step);
1621 static void gattc_search_result_cb(int conn_id, btgatt_srvc_id_t *srvc_id)
1623 struct step *step = g_new0(struct step, 1);
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));
1629 schedule_callback_verification(step);
1632 static void gattc_search_complete_cb(int conn_id, int status)
1634 struct step *step = g_new0(struct step, 1);
1636 step->callback = CB_GATTC_SEARCH_COMPLETE;
1637 step->callback_result.conn_id = conn_id;
1639 schedule_callback_verification(step);
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,
1646 struct step *step = g_new0(struct step, 1);
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,
1654 step->callback_result.char_prop = char_prop;
1656 schedule_callback_verification(step);
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)
1663 struct step *step = g_new0(struct step, 1);
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,
1671 step->callback_result.descriptor = g_memdup(descr_id,
1674 schedule_callback_verification(step);
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)
1680 struct step *step = g_new0(struct step, 1);
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,
1689 schedule_callback_verification(step);
1692 static void gattc_read_characteristic_cb(int conn_id, int status,
1693 btgatt_read_params_t *p_data)
1695 struct step *step = g_new0(struct step, 1);
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));
1702 schedule_callback_verification(step);
1705 static void gattc_read_descriptor_cb(int conn_id, int status,
1706 btgatt_read_params_t *p_data)
1708 struct step *step = g_new0(struct step, 1);
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));
1715 schedule_callback_verification(step);
1718 static void gattc_write_characteristic_cb(int conn_id, int status,
1719 btgatt_write_params_t *p_data)
1721 struct step *step = g_new0(struct step, 1);
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));
1728 schedule_callback_verification(step);
1731 static void gattc_write_descriptor_cb(int conn_id, int status,
1732 btgatt_write_params_t *p_data)
1734 struct step *step = g_new0(struct step, 1);
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));
1741 schedule_callback_verification(step);
1744 static void gattc_register_for_notification_cb(int conn_id, int registered,
1746 btgatt_srvc_id_t *srvc_id,
1747 btgatt_gatt_id_t *char_id)
1749 struct step *step = g_new0(struct step, 1);
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,
1757 step->callback_result.notification_registered = registered;
1759 schedule_callback_verification(step);
1762 static void gattc_notif_cb(int conn_id, btgatt_notify_params_t *p_data)
1764 struct step *step = g_new0(struct step, 1);
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));
1770 schedule_callback_verification(step);
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
1794 static void gatts_register_server_cb(int status, int server_if,
1795 bt_uuid_t *app_uuid)
1797 struct step *step = g_new0(struct step, 1);
1799 step->callback = CB_GATTS_REGISTER_SERVER;
1801 step->callback_result.status = status;
1803 schedule_callback_verification(step);
1806 static void gatts_connection_cb(int conn_id, int server_if, int connected,
1809 struct step *step = g_new0(struct step, 1);
1810 bt_property_t *props[1];
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;
1817 /* Utilize property verification mechanism for bdaddr */
1818 props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1820 step->callback_result.num_properties = 1;
1821 step->callback_result.properties = repack_properties(1, props);
1823 g_free(props[0]->val);
1826 schedule_callback_verification(step);
1829 static void gatts_service_added_cb(int status, int server_if,
1830 btgatt_srvc_id_t *srvc_id,
1833 struct step *step = g_new0(struct step, 1);
1835 step->callback = CB_GATTS_SERVICE_ADDED;
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));
1843 schedule_callback_verification(step);
1846 static void gatts_included_service_added_cb(int status, int server_if,
1848 int inc_srvc_handle)
1850 struct step *step = g_new0(struct step, 1);
1852 step->callback = CB_GATTS_INCLUDED_SERVICE_ADDED;
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));
1861 schedule_callback_verification(step);
1864 static void gatts_characteristic_added_cb(int status, int server_if,
1869 struct step *step = g_new0(struct step, 1);
1871 step->callback = CB_GATTS_CHARACTERISTIC_ADDED;
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));
1881 schedule_callback_verification(step);
1884 static void gatts_descriptor_added_cb(int status, int server_if,
1889 struct step *step = g_new0(struct step, 1);
1891 step->callback = CB_GATTS_DESCRIPTOR_ADDED;
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));
1901 schedule_callback_verification(step);
1904 static void gatts_service_started_cb(int status, int server_if, int srvc_handle)
1906 struct step *step = g_new0(struct step, 1);
1908 step->callback = CB_GATTS_SERVICE_STARTED;
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));
1915 schedule_callback_verification(step);
1918 static void gatts_service_stopped_cb(int status, int server_if, int srvc_handle)
1920 struct step *step = g_new0(struct step, 1);
1922 step->callback = CB_GATTS_SERVICE_STOPPED;
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));
1929 schedule_callback_verification(step);
1932 static void gatts_service_deleted_cb(int status, int server_if, int srvc_handle)
1934 struct step *step = g_new0(struct step, 1);
1936 step->callback = CB_GATTS_SERVICE_DELETED;
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));
1943 schedule_callback_verification(step);
1946 static void gatts_request_read_cb(int conn_id, int trans_id, bt_bdaddr_t *bda,
1947 int attr_handle, int offset,
1950 struct step *step = g_new0(struct step, 1);
1951 bt_property_t *props[1];
1953 step->callback = CB_GATTS_REQUEST_READ;
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;
1962 /* Utilize property verification mechanism for bdaddr */
1963 props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1965 step->callback_result.num_properties = 1;
1966 step->callback_result.properties = repack_properties(1, props);
1968 g_free(props[0]->val);
1971 schedule_callback_verification(step);
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)
1979 struct step *step = g_new0(struct step, 1);
1980 bt_property_t *props[1];
1982 step->callback = CB_GATTS_REQUEST_WRITE;
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);
1994 /* Utilize property verification mechanism for bdaddr */
1995 props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
1997 step->callback_result.num_properties = 1;
1998 step->callback_result.properties = repack_properties(1, props);
2000 g_free(props[0]->val);
2003 schedule_callback_verification(step);
2006 static void gatts_indication_send_cb(int conn_id, int status)
2008 struct step *step = g_new0(struct step, 1);
2010 step->callback = CB_GATTS_INDICATION_SEND;
2012 step->callback_result.conn_id = conn_id;
2013 step->callback_result.status = status;
2015 schedule_callback_verification(step);
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,
2036 static const btgatt_callbacks_t btgatt_callbacks = {
2037 .size = sizeof(btgatt_callbacks),
2038 .client = &btgatt_client_callbacks,
2039 .server = &btgatt_server_callbacks
2042 static void pan_control_state_cb(btpan_control_state_t state, int local_role,
2043 bt_status_t error, const char *ifname)
2045 struct step *step = g_new0(struct step, 1);
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;
2052 schedule_callback_verification(step);
2055 static void pan_connection_state_cb(btpan_connection_state_t state,
2057 const bt_bdaddr_t *bd_addr,
2058 int local_role, int remote_role)
2060 struct step *step = g_new0(struct step, 1);
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;
2068 schedule_callback_verification(step);
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,
2077 static void hdp_app_reg_state_cb(int app_id, bthl_app_reg_state_t state)
2079 struct step *step = g_new0(struct step, 1);
2081 step->callback = CB_HDP_APP_REG_STATE;
2082 step->callback_result.app_id = app_id;
2083 step->callback_result.app_state = state;
2085 schedule_callback_verification(step);
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)
2092 struct step *step = g_new0(struct step, 1);
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;
2100 schedule_callback_verification(step);
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,
2109 static void a2dp_connection_state_cb(btav_connection_state_t state,
2110 bt_bdaddr_t *bd_addr)
2112 struct step *step = g_new0(struct step, 1);
2114 step->callback = CB_A2DP_CONN_STATE;
2115 step->callback_result.av_conn_state = state;
2117 schedule_callback_verification(step);
2120 static void a2dp_audio_state_cb(btav_audio_state_t state, bt_bdaddr_t *bd_addr)
2122 struct step *step = g_new0(struct step, 1);
2124 step->callback = CB_A2DP_AUDIO_STATE;
2125 step->callback_result.av_audio_state = state;
2127 schedule_callback_verification(step);
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,
2136 static void avrcp_get_play_status_cb(void)
2138 struct step *step = g_new0(struct step, 1);
2140 step->callback = CB_AVRCP_PLAY_STATUS_REQ;
2141 schedule_callback_verification(step);
2144 static void avrcp_register_notification_cb(btrc_event_id_t event_id,
2147 struct step *step = g_new0(struct step, 1);
2149 step->callback = CB_AVRCP_REG_NOTIF_REQ;
2150 schedule_callback_verification(step);
2153 static void avrcp_get_element_attr_cb(uint8_t num_attr,
2154 btrc_media_attr_t *p_attrs)
2156 struct step *step = g_new0(struct step, 1);
2158 step->callback = CB_AVRCP_GET_ATTR_REQ;
2159 schedule_callback_verification(step);
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,
2169 static btmce_mas_instance_t *copy_mas_instances(int num_instances,
2170 btmce_mas_instance_t *instances)
2173 btmce_mas_instance_t *inst;
2175 inst = g_new0(btmce_mas_instance_t, num_instances);
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));
2188 static void map_client_get_remote_mas_instances_cb(bt_status_t status,
2189 bt_bdaddr_t *bd_addr,
2191 btmce_mas_instance_t
2194 struct step *step = g_new0(struct step, 1);
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,
2201 step->callback = CB_MAP_CLIENT_REMOTE_MAS_INSTANCES;
2203 schedule_callback_verification(step);
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,
2211 static bool setup_base(struct test_data *data)
2213 const hw_module_t *module;
2214 hw_device_t *device;
2221 if (pipe(signal_fd))
2227 close(signal_fd[0]);
2228 close(signal_fd[1]);
2233 if (!tester_use_debug())
2236 close(signal_fd[0]);
2237 emulator(signal_fd[1], data->mgmt_index);
2241 close(signal_fd[1]);
2242 data->bluetoothd_pid = pid;
2244 len = read(signal_fd[0], buf, sizeof(buf));
2245 if (len <= 0 || strcmp(buf, EMULATOR_SIGNAL)) {
2246 close(signal_fd[0]);
2250 close(signal_fd[0]);
2252 err = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID,
2253 AUDIO_HARDWARE_MODULE_ID_A2DP, &module);
2257 err = audio_hw_device_open(module, &data->audio);
2261 err = hw_get_module(BT_HARDWARE_MODULE_ID, &module);
2265 err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
2269 data->device = device;
2271 data->if_bluetooth = ((bluetooth_device_t *)
2272 device)->get_bluetooth_interface();
2273 if (!data->if_bluetooth)
2276 if (!(data->steps = queue_new()))
2279 data->pdus = queue_new();
2281 queue_destroy(data->steps, NULL);
2288 static void setup(const void *test_data)
2290 struct test_data *data = tester_get_data();
2293 if (!setup_base(data)) {
2294 tester_setup_failed();
2298 status = data->if_bluetooth->init(&bt_callbacks);
2299 if (status != BT_STATUS_SUCCESS) {
2300 data->if_bluetooth = NULL;
2301 tester_setup_failed();
2305 tester_setup_complete();
2308 static void setup_socket(const void *test_data)
2310 struct test_data *data = tester_get_data();
2314 if (!setup_base(data)) {
2315 tester_setup_failed();
2319 status = data->if_bluetooth->init(&bt_callbacks);
2320 if (status != BT_STATUS_SUCCESS) {
2321 data->if_bluetooth = NULL;
2322 tester_setup_failed();
2326 sock = data->if_bluetooth->get_profile_interface(BT_PROFILE_SOCKETS_ID);
2328 tester_setup_failed();
2332 data->if_sock = sock;
2334 tester_setup_complete();
2337 static void setup_hidhost(const void *test_data)
2339 struct test_data *data = tester_get_data();
2343 if (!setup_base(data)) {
2344 tester_setup_failed();
2348 status = data->if_bluetooth->init(&bt_callbacks);
2349 if (status != BT_STATUS_SUCCESS) {
2350 data->if_bluetooth = NULL;
2351 tester_setup_failed();
2355 hid = data->if_bluetooth->get_profile_interface(BT_PROFILE_HIDHOST_ID);
2357 tester_setup_failed();
2363 status = data->if_hid->init(&bthh_callbacks);
2364 if (status != BT_STATUS_SUCCESS) {
2365 data->if_hid = NULL;
2366 tester_setup_failed();
2370 tester_setup_complete();
2373 static void setup_pan(const void *test_data)
2375 struct test_data *data = tester_get_data();
2379 if (!setup_base(data)) {
2380 tester_setup_failed();
2384 status = data->if_bluetooth->init(&bt_callbacks);
2385 if (status != BT_STATUS_SUCCESS) {
2386 data->if_bluetooth = NULL;
2387 tester_setup_failed();
2391 pan = data->if_bluetooth->get_profile_interface(BT_PROFILE_PAN_ID);
2393 tester_setup_failed();
2399 status = data->if_pan->init(&btpan_callbacks);
2400 if (status != BT_STATUS_SUCCESS) {
2401 data->if_pan = NULL;
2402 tester_setup_failed();
2406 tester_setup_complete();
2409 static void setup_hdp(const void *test_data)
2411 struct test_data *data = tester_get_data();
2415 if (!setup_base(data)) {
2416 tester_setup_failed();
2420 status = data->if_bluetooth->init(&bt_callbacks);
2421 if (status != BT_STATUS_SUCCESS) {
2422 data->if_bluetooth = NULL;
2423 tester_setup_failed();
2427 hdp = data->if_bluetooth->get_profile_interface(BT_PROFILE_HEALTH_ID);
2429 tester_setup_failed();
2435 status = data->if_hdp->init(&bthl_callbacks);
2436 if (status != BT_STATUS_SUCCESS) {
2437 data->if_hdp = NULL;
2438 tester_setup_failed();
2442 tester_setup_complete();
2445 static void setup_a2dp(const void *test_data)
2447 struct test_data *data = tester_get_data();
2448 const bt_interface_t *if_bt;
2452 if (!setup_base(data)) {
2453 tester_setup_failed();
2457 if_bt = data->if_bluetooth;
2459 status = if_bt->init(&bt_callbacks);
2460 if (status != BT_STATUS_SUCCESS) {
2461 data->if_bluetooth = NULL;
2462 tester_setup_failed();
2466 a2dp = if_bt->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID);
2468 tester_setup_failed();
2472 data->if_a2dp = a2dp;
2474 status = data->if_a2dp->init(&bta2dp_callbacks);
2475 if (status != BT_STATUS_SUCCESS) {
2476 data->if_a2dp = NULL;
2477 tester_setup_failed();
2481 tester_setup_complete();
2484 static void setup_avrcp(const void *test_data)
2486 struct test_data *data = tester_get_data();
2487 const bt_interface_t *if_bt;
2489 const void *a2dp, *avrcp;
2491 if (!setup_base(data)) {
2492 tester_setup_failed();
2496 if_bt = data->if_bluetooth;
2498 status = if_bt->init(&bt_callbacks);
2499 if (status != BT_STATUS_SUCCESS) {
2500 data->if_bluetooth = NULL;
2501 tester_setup_failed();
2505 a2dp = if_bt->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID);
2507 tester_setup_failed();
2511 data->if_a2dp = a2dp;
2513 status = data->if_a2dp->init(&bta2dp_callbacks);
2514 if (status != BT_STATUS_SUCCESS) {
2515 data->if_a2dp = NULL;
2516 tester_setup_failed();
2520 avrcp = if_bt->get_profile_interface(BT_PROFILE_AV_RC_ID);
2522 tester_setup_failed();
2526 data->if_avrcp = avrcp;
2528 status = data->if_avrcp->init(&btavrcp_callbacks);
2529 if (status != BT_STATUS_SUCCESS) {
2530 data->if_avrcp = NULL;
2531 tester_setup_failed();
2535 tester_setup_complete();
2538 static void setup_gatt(const void *test_data)
2540 struct test_data *data = tester_get_data();
2544 if (!setup_base(data)) {
2545 tester_setup_failed();
2549 status = data->if_bluetooth->init(&bt_callbacks);
2550 if (status != BT_STATUS_SUCCESS) {
2551 data->if_bluetooth = NULL;
2552 tester_setup_failed();
2556 gatt = data->if_bluetooth->get_profile_interface(BT_PROFILE_GATT_ID);
2558 tester_setup_failed();
2562 data->if_gatt = gatt;
2564 status = data->if_gatt->init(&btgatt_callbacks);
2565 if (status != BT_STATUS_SUCCESS) {
2566 data->if_gatt = NULL;
2568 tester_setup_failed();
2572 tester_setup_complete();
2575 static void setup_map_client(const void *test_data)
2577 struct test_data *data = tester_get_data();
2579 const void *map_client;
2581 if (!setup_base(data)) {
2582 tester_setup_failed();
2586 status = data->if_bluetooth->init(&bt_callbacks);
2587 if (status != BT_STATUS_SUCCESS) {
2588 data->if_bluetooth = NULL;
2589 tester_setup_failed();
2593 map_client = data->if_bluetooth->get_profile_interface(
2594 BT_PROFILE_MAP_CLIENT_ID);
2596 tester_setup_failed();
2600 data->if_map_client = map_client;
2602 status = data->if_map_client->init(&btmap_client_callbacks);
2603 if (status != BT_STATUS_SUCCESS) {
2604 data->if_map_client = NULL;
2606 tester_setup_failed();
2610 tester_setup_complete();
2613 static void teardown(const void *test_data)
2615 struct test_data *data = tester_get_data();
2617 queue_destroy(data->steps, NULL);
2620 queue_destroy(data->pdus, NULL);
2623 /* no cleanup for map_client */
2624 data->if_map_client = NULL;
2626 if (data->if_gatt) {
2627 data->if_gatt->cleanup();
2628 data->if_gatt = NULL;
2632 data->if_hid->cleanup();
2633 data->if_hid = NULL;
2637 data->if_pan->cleanup();
2638 data->if_pan = NULL;
2642 data->if_hdp->cleanup();
2643 data->if_hdp = NULL;
2646 if (data->if_stream) {
2647 data->audio->close_output_stream(data->audio, data->if_stream);
2648 data->if_stream = NULL;
2651 if (data->if_a2dp) {
2652 data->if_a2dp->cleanup();
2653 data->if_a2dp = NULL;
2656 if (data->if_avrcp) {
2657 data->if_avrcp->cleanup();
2658 data->if_avrcp = NULL;
2661 if (data->if_bluetooth) {
2662 data->if_bluetooth->cleanup();
2663 data->if_bluetooth = NULL;
2666 data->device->close(data->device);
2667 audio_hw_device_close(data->audio);
2670 * Ssp_request_cb pointer can be set do default_ssp_req_cb.
2671 * Set it back to ssp_request_cb
2673 bt_callbacks.ssp_request_cb = ssp_request_cb;
2675 if (!data->bluetoothd_pid)
2676 tester_teardown_complete();
2679 static void emu_connectable_complete(uint16_t opcode, uint8_t status,
2680 const void *param, uint8_t len,
2684 struct test_data *data = user_data;
2687 case BT_HCI_CMD_WRITE_SCAN_ENABLE:
2689 case BT_HCI_CMD_LE_SET_ADV_ENABLE:
2691 * For BREDRLE emulator we want to verify step after scan
2692 * enable and not after le_set_adv_enable
2694 if (data->hciemu_type == HCIEMU_TYPE_BREDRLE)
2702 step = g_new0(struct step, 1);
2705 tester_warn("Emulated remote setup failed.");
2706 step->action_status = BT_STATUS_FAIL;
2708 tester_debug("Emulated remote setup done.");
2709 step->action_status = BT_STATUS_SUCCESS;
2712 schedule_action_verification(step);
2715 void emu_setup_powered_remote_action(void)
2717 struct test_data *data = tester_get_data();
2718 struct bthost *bthost;
2720 bthost = hciemu_client_get_host(data->hciemu);
2721 bthost_set_cmd_complete_cb(bthost, emu_connectable_complete, data);
2723 if ((data->hciemu_type == HCIEMU_TYPE_LE) ||
2724 (data->hciemu_type == HCIEMU_TYPE_BREDRLE)) {
2727 adv[0] = 0x02; /* Field length */
2728 adv[1] = 0x01; /* Flags */
2729 adv[2] = 0x02; /* Flags value */
2730 adv[3] = 0x00; /* Field terminator */
2732 bthost_set_adv_data(bthost, adv, sizeof(adv));
2733 bthost_set_adv_enable(bthost, 0x01);
2736 if (data->hciemu_type != HCIEMU_TYPE_LE)
2737 bthost_write_scan_enable(bthost, 0x03);
2740 void emu_set_pin_code_action(void)
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);
2748 bthost = hciemu_client_get_host(data->hciemu);
2750 bthost_set_pin_code(bthost, action_data->pin->pin,
2751 action_data->pin_len);
2753 step->action_status = BT_STATUS_SUCCESS;
2755 tester_print("Setting emu pin done.");
2757 schedule_action_verification(step);
2760 void emu_set_ssp_mode_action(void)
2762 struct test_data *data = tester_get_data();
2763 struct bthost *bthost;
2764 struct step *step = g_new0(struct step, 1);
2766 bthost = hciemu_client_get_host(data->hciemu);
2768 bthost_write_ssp_mode(bthost, 0x01);
2770 step->action_status = BT_STATUS_SUCCESS;
2772 schedule_action_verification(step);
2775 void emu_set_connect_cb_action(void)
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);
2783 bthost_set_connect_cb(bthost, cb, data);
2785 step->action_status = BT_STATUS_SUCCESS;
2787 schedule_action_verification(step);
2790 void emu_remote_connect_hci_action(void)
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;
2799 master_addr = hciemu_get_master_bdaddr(data->hciemu);
2801 tester_print("Trying to connect hci");
2804 bthost_hci_connect(bthost, master_addr,
2805 action_data->bearer_type);
2807 bthost_hci_connect(bthost, master_addr, BDADDR_BREDR);
2809 step->action_status = BT_STATUS_SUCCESS;
2811 schedule_action_verification(step);
2814 void emu_remote_disconnect_hci_action(void)
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);
2823 bthost_hci_disconnect(bthost, *handle, 0x13);
2824 step->action_status = BT_STATUS_SUCCESS;
2826 step->action_status = BT_STATUS_FAIL;
2829 schedule_action_verification(step);
2832 void emu_set_io_cap(void)
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);
2840 bthost = hciemu_client_get_host(data->hciemu);
2843 bthost_set_io_capability(bthost, action_data->io_cap);
2845 bthost_set_io_capability(bthost, 0x01);
2847 step->action_status = BT_STATUS_SUCCESS;
2849 schedule_action_verification(step);
2852 void emu_add_l2cap_server_action(void)
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);
2861 tester_warn("Invalid l2cap_data params");
2862 step->action_status = BT_STATUS_FAIL;
2866 bthost = hciemu_client_get_host(data->hciemu);
2868 bthost_add_l2cap_server(bthost, l2cap_data->psm, l2cap_data->func,
2869 l2cap_data->user_data);
2871 step->action_status = BT_STATUS_SUCCESS;
2874 schedule_action_verification(step);
2877 static void print_data(const char *str, void *user_data)
2879 tester_debug("tester: %s", str);
2882 static void emu_generic_cid_hook_cb(const void *data, uint16_t len,
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);
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)
2896 if (memcmp(pdus[i].req.iov_base, data, len))
2900 if (pdus[i].rsp.iov_base) {
2901 util_hexdump('>', pdus[i].rsp.iov_base,
2902 pdus[i].rsp.iov_len, print_data, NULL);
2904 /* if its sdp pdu use transaction ID from request */
2905 if (cid_data->is_sdp) {
2906 struct iovec rsp[3];
2908 rsp[0].iov_base = pdus[i].rsp.iov_base;
2911 rsp[1].iov_base = ((uint8_t *) data) + 1;
2914 rsp[2].iov_base = pdus[i].rsp.iov_base + 3;
2915 rsp[2].iov_len = pdus[i].rsp.iov_len - 3;
2917 bthost_send_cid_v(bthost, cid_data->handle,
2918 cid_data->cid, rsp, 3);
2920 bthost_send_cid_v(bthost, cid_data->handle,
2921 cid_data->cid, &pdus[i].rsp, 1);
2928 void tester_handle_l2cap_data_exchange(struct emu_l2cap_cid_data *cid_data)
2930 struct test_data *t_data = tester_get_data();
2931 struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
2933 bthost_add_cid_hook(bthost, cid_data->handle, cid_data->cid,
2934 emu_generic_cid_hook_cb, cid_data);
2937 void tester_generic_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
2939 struct emu_l2cap_cid_data *cid_data = user_data;
2941 cid_data->handle = handle;
2942 cid_data->cid = cid;
2944 tester_handle_l2cap_data_exchange(cid_data);
2947 static void rfcomm_connect_cb(uint16_t handle, uint16_t cid, void *user_data,
2950 struct step *step = g_new0(struct step, 1);
2952 tester_print("Connect handle %d, cid %d cb status: %d", handle, cid,
2955 step->action_status = BT_STATUS_SUCCESS;
2957 schedule_action_verification(step);
2960 void emu_add_rfcomm_server_action(void)
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;
2969 tester_warn("Invalid l2cap_data params");
2973 step = g_new0(struct step, 1);
2975 bthost = hciemu_client_get_host(data->hciemu);
2977 bthost_add_rfcomm_server(bthost, rfcomm_data->channel,
2978 rfcomm_connect_cb, data);
2980 step->action_status = BT_STATUS_SUCCESS;
2982 schedule_action_verification(step);
2985 void dummy_action(void)
2987 struct step *step = g_new0(struct step, 1);
2989 step->action = dummy_action;
2991 schedule_action_verification(step);
2994 void bluetooth_enable_action(void)
2996 struct test_data *data = tester_get_data();
2997 struct step *step = g_new0(struct step, 1);
2999 step->action_status = data->if_bluetooth->enable();
3001 schedule_action_verification(step);
3004 void bluetooth_disable_action(void)
3006 struct test_data *data = tester_get_data();
3007 struct step *step = g_new0(struct step, 1);
3009 step->action_status = data->if_bluetooth->disable();
3011 schedule_action_verification(step);
3014 void bt_set_property_action(void)
3016 struct test_data *data = tester_get_data();
3018 struct step *current_data_step = queue_peek_head(data->steps);
3019 bt_property_t *prop;
3021 if (!current_data_step->set_data) {
3022 tester_debug("BT property not set for step");
3023 tester_test_failed();
3027 step = g_new0(struct step, 1);
3029 prop = (bt_property_t *)current_data_step->set_data;
3031 step->action_status = data->if_bluetooth->set_adapter_property(prop);
3033 schedule_action_verification(step);
3036 void bt_get_property_action(void)
3038 struct test_data *data = tester_get_data();
3040 struct step *current_data_step = queue_peek_head(data->steps);
3041 bt_property_t *prop;
3043 if (!current_data_step->set_data) {
3044 tester_debug("BT property to get not defined");
3045 tester_test_failed();
3049 step = g_new0(struct step, 1);
3051 prop = (bt_property_t *)current_data_step->set_data;
3053 step->action_status = data->if_bluetooth->get_adapter_property(
3056 schedule_action_verification(step);
3059 void bt_start_discovery_action(void)
3061 struct test_data *data = tester_get_data();
3062 struct step *step = g_new0(struct step, 1);
3064 step->action_status = data->if_bluetooth->start_discovery();
3066 schedule_action_verification(step);
3069 void bt_cancel_discovery_action(void)
3071 struct test_data *data = tester_get_data();
3072 struct step *step = g_new0(struct step, 1);
3074 step->action_status = data->if_bluetooth->cancel_discovery();
3076 schedule_action_verification(step);
3079 void bt_get_device_props_action(void)
3081 struct test_data *data = tester_get_data();
3082 struct step *current_data_step = queue_peek_head(data->steps);
3085 if (!current_data_step->set_data) {
3086 tester_debug("bdaddr not defined");
3087 tester_test_failed();
3091 step = g_new0(struct step, 1);
3093 step->action_status =
3094 data->if_bluetooth->get_remote_device_properties(
3095 current_data_step->set_data);
3097 schedule_action_verification(step);
3100 void bt_get_device_prop_action(void)
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;
3108 tester_warn("No arguments for 'get remote device prop' req.");
3109 tester_test_failed();
3113 step = g_new0(struct step, 1);
3115 step->action_status = data->if_bluetooth->get_remote_device_property(
3117 action_data->prop_type);
3119 schedule_action_verification(step);
3122 void bt_set_device_prop_action(void)
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;
3130 tester_warn("No arguments for 'set remote device prop' req.");
3131 tester_test_failed();
3135 step = g_new0(struct step, 1);
3137 step->action_status = data->if_bluetooth->set_remote_device_property(
3141 schedule_action_verification(step);
3144 void bt_create_bond_action(void)
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;
3151 if (!action_data || !action_data->addr) {
3152 tester_warn("Bad arguments for 'create bond' req.");
3153 tester_test_failed();
3157 step = g_new0(struct step, 1);
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);
3165 schedule_action_verification(step);
3168 void bt_pin_reply_accept_action(void)
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;
3175 if (!action_data || !action_data->addr || !action_data->pin) {
3176 tester_warn("Bad arguments for 'pin reply' req.");
3177 tester_test_failed();
3181 step = g_new0(struct step, 1);
3183 step->action_status = data->if_bluetooth->pin_reply(action_data->addr,
3185 action_data->pin_len,
3188 schedule_action_verification(step);
3191 void bt_ssp_reply_accept_action(void)
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);
3198 step->action_status = data->if_bluetooth->ssp_reply(action_data->addr,
3199 action_data->ssp_variant,
3200 action_data->accept, 0);
3202 schedule_action_verification(step);
3205 void bt_cancel_bond_action(void)
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);
3212 step->action_status = data->if_bluetooth->cancel_bond(addr);
3214 schedule_action_verification(step);
3217 void bt_remove_bond_action(void)
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);
3224 step->action_status = data->if_bluetooth->remove_bond(addr);
3226 schedule_action_verification(step);
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,
3233 struct test_data *t_data = tester_get_data();
3235 t_data->if_bluetooth->ssp_reply(remote_bd_addr, pairing_variant, true,
3239 void set_default_ssp_request_handler(void)
3241 struct step *step = g_new0(struct step, 1);
3243 bt_callbacks.ssp_request_cb = default_ssp_req_cb;
3245 step->action_status = BT_STATUS_SUCCESS;
3247 schedule_action_verification(step);
3250 static void generic_test_function(const void *test_data)
3252 struct test_data *data = tester_get_data();
3253 struct step *first_step;
3255 init_test_steps(data);
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();
3264 first_step->action();
3267 #define test(data, test_setup, test, test_teardown) \
3269 struct test_data *user; \
3270 user = g_malloc0(sizeof(struct test_data)); \
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); \
3280 static void tester_testcases_cleanup(void)
3282 remove_bluetooth_tests();
3283 remove_socket_tests();
3284 remove_hidhost_tests();
3285 remove_gatt_tests();
3286 remove_a2dp_tests();
3287 remove_avrcp_tests();
3292 static void add_bluetooth_tests(void *data, void *user_data)
3294 struct test_case *tc = data;
3296 test(tc, setup, generic_test_function, teardown);
3299 static void add_socket_tests(void *data, void *user_data)
3301 struct test_case *tc = data;
3303 test(tc, setup_socket, generic_test_function, teardown);
3306 static void add_hidhost_tests(void *data, void *user_data)
3308 struct test_case *tc = data;
3310 test(tc, setup_hidhost, generic_test_function, teardown);
3313 static void add_pan_tests(void *data, void *user_data)
3315 struct test_case *tc = data;
3317 test(tc, setup_pan, generic_test_function, teardown);
3320 static void add_hdp_tests(void *data, void *user_data)
3322 struct test_case *tc = data;
3324 test(tc, setup_hdp, generic_test_function, teardown);
3327 static void add_a2dp_tests(void *data, void *user_data)
3329 struct test_case *tc = data;
3331 test(tc, setup_a2dp, generic_test_function, teardown);
3334 static void add_avrcp_tests(void *data, void *user_data)
3336 struct test_case *tc = data;
3338 test(tc, setup_avrcp, generic_test_function, teardown);
3341 static void add_gatt_tests(void *data, void *user_data)
3343 struct test_case *tc = data;
3345 test(tc, setup_gatt, generic_test_function, teardown);
3348 static void add_map_client_tests(void *data, void *user_data)
3350 struct test_case *tc = data;
3352 test(tc, setup_map_client, generic_test_function, teardown);
3355 int main(int argc, char *argv[])
3357 snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));
3359 tester_init(&argc, &argv);
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);
3374 tester_testcases_cleanup();