3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2014-2015 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "lib/bluetooth.h"
40 #include "monitor/bt.h"
41 #include "emulator/bthost.h"
42 #include "emulator/hciemu.h"
44 #include "src/shared/tester.h"
45 #include "src/shared/mgmt.h"
46 #include "src/shared/hci.h"
47 #include "src/shared/util.h"
52 struct hciemu *hciemu;
53 enum hciemu_type hciemu_type;
54 const void *test_data;
55 unsigned int remove_id;
58 static void mgmt_debug(const char *str, void *user_data)
60 const char *prefix = user_data;
62 tester_print("%s%s", prefix, str);
65 static void read_info_callback(uint8_t status, uint16_t length,
66 const void *param, void *user_data)
68 struct test_data *data = tester_get_data();
69 const struct mgmt_rp_read_info *rp = param;
71 uint16_t manufacturer;
72 uint32_t supported_settings, current_settings;
74 tester_print("Read Info callback");
75 tester_print(" Status: 0x%02x", status);
77 if (status || !param) {
78 tester_pre_setup_failed();
82 ba2str(&rp->bdaddr, addr);
83 manufacturer = btohs(rp->manufacturer);
84 supported_settings = btohl(rp->supported_settings);
85 current_settings = btohl(rp->current_settings);
87 tester_print(" Address: %s", addr);
88 tester_print(" Version: 0x%02x", rp->version);
89 tester_print(" Manufacturer: 0x%04x", manufacturer);
90 tester_print(" Supported settings: 0x%08x", supported_settings);
91 tester_print(" Current settings: 0x%08x", current_settings);
92 tester_print(" Class: 0x%02x%02x%02x",
93 rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
94 tester_print(" Name: %s", rp->name);
95 tester_print(" Short name: %s", rp->short_name);
97 if (strcmp(hciemu_get_address(data->hciemu), addr)) {
98 tester_pre_setup_failed();
102 tester_pre_setup_complete();
105 static void index_added_callback(uint16_t index, uint16_t length,
106 const void *param, void *user_data)
108 struct test_data *data = tester_get_data();
110 tester_print("Index Added callback");
111 tester_print(" Index: 0x%04x", index);
113 if (data->mgmt_index != MGMT_INDEX_NONE)
116 data->mgmt_index = index;
118 mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
119 read_info_callback, NULL, NULL);
122 static void index_removed_callback(uint16_t index, uint16_t length,
123 const void *param, void *user_data)
125 struct test_data *data = tester_get_data();
127 tester_print("Index Removed callback");
128 tester_print(" Index: 0x%04x", index);
130 if (index != data->mgmt_index)
133 if (data->remove_id) {
134 mgmt_unregister(data->mgmt, data->remove_id);
136 tester_test_passed();
140 mgmt_unregister_index(data->mgmt, data->mgmt_index);
142 mgmt_unref(data->mgmt);
145 tester_post_teardown_complete();
148 static void read_index_list_callback(uint8_t status, uint16_t length,
149 const void *param, void *user_data)
151 struct test_data *data = tester_get_data();
153 tester_print("Read Index List callback");
154 tester_print(" Status: 0x%02x", status);
156 if (status || !param) {
157 tester_pre_setup_failed();
161 mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
162 index_added_callback, NULL, NULL);
164 data->hciemu = hciemu_new(data->hciemu_type);
166 tester_warn("Failed to setup HCI emulation");
167 tester_pre_setup_failed();
170 tester_print("New hciemu instance created");
173 static void test_pre_setup(const void *test_data)
175 struct test_data *data = tester_get_data();
177 data->mgmt = mgmt_new_default();
179 tester_warn("Failed to setup management interface");
180 tester_pre_setup_failed();
184 if (tester_use_debug())
185 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
187 mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
188 read_index_list_callback, NULL, NULL);
191 static void test_post_teardown(const void *test_data)
193 struct test_data *data = tester_get_data();
195 mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, data->mgmt_index,
196 index_removed_callback,
199 hciemu_unref(data->hciemu);
203 static void test_data_free(void *test_data)
205 struct test_data *data = test_data;
210 static void setup_powered_client_callback(uint8_t status, uint16_t length,
211 const void *param, void *user_data)
213 if (status != MGMT_STATUS_SUCCESS) {
214 tester_setup_failed();
218 tester_print("Controller powered on");
220 tester_setup_complete();
223 static void setup_powered(const void *test_data)
225 struct test_data *data = tester_get_data();
226 unsigned char param[] = { 0x01 };
228 tester_print("Powering on controller");
230 mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
231 sizeof(param), param, setup_powered_client_callback,
235 static void toggle_powered(const void *test_data);
237 static void toggle_powered_client_callback(uint8_t status, uint16_t length,
238 const void *param, void *user_data)
240 bool power = PTR_TO_INT(user_data);
242 if (status != MGMT_STATUS_SUCCESS) {
243 tester_setup_failed();
247 tester_print("Controller powered %s", power ? "on" : "off");
250 toggle_powered(false);
252 tester_setup_complete();
255 static void toggle_powered(const void *test_data)
257 struct test_data *data = tester_get_data();
258 bool power = PTR_TO_INT(test_data);
259 unsigned char param[1];
261 param[0] = power ? 0x01 : 0x00;
263 tester_print("Powering %s controller", power ? "on" : "off");
265 mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
266 sizeof(param), param, toggle_powered_client_callback,
267 INT_TO_PTR(power), NULL);
270 static void test_open_success(const void *test_data)
272 struct test_data *data = tester_get_data();
275 data->remove_id = mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED,
277 index_removed_callback,
280 hci = bt_hci_new_user_channel(data->mgmt_index);
286 mgmt_unregister(data->mgmt, data->remove_id);
289 tester_test_failed();
292 static void test_open_failed(const void *test_data)
294 struct test_data *data = tester_get_data();
297 hci = bt_hci_new_user_channel(data->mgmt_index);
299 tester_test_passed();
304 tester_test_failed();
307 #define test_user(name, data, setup, func) \
309 struct test_data *user; \
310 user = malloc(sizeof(struct test_data)); \
313 user->hciemu_type = HCIEMU_TYPE_BREDR; \
314 user->mgmt_index = MGMT_INDEX_NONE; \
315 user->test_data = data; \
316 user->remove_id = 0; \
317 tester_add_full(name, data, \
318 test_pre_setup, setup, func, NULL, \
319 test_post_teardown, 2, user, test_data_free); \
322 int main(int argc, char *argv[])
324 tester_init(&argc, &argv);
326 test_user("User channel open - Success", NULL,
327 NULL, test_open_success);
328 test_user("User channel open - Failed", NULL,
329 setup_powered, test_open_failed);
330 test_user("User channel open - Power Toggle Success", INT_TO_PTR(true),
331 toggle_powered, test_open_success);