3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2014 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"
50 struct hciemu *hciemu;
51 enum hciemu_type hciemu_type;
52 const void *test_data;
57 struct rfcomm_client_data {
58 uint8_t server_channel;
59 uint8_t client_channel;
60 int expected_connect_err;
61 const uint8_t *send_data;
62 const uint8_t *read_data;
66 struct rfcomm_server_data {
67 uint8_t server_channel;
68 uint8_t client_channel;
70 const uint8_t *send_data;
71 const uint8_t *read_data;
75 static void mgmt_debug(const char *str, void *user_data)
77 const char *prefix = user_data;
79 tester_print("%s%s", prefix, str);
82 static void read_info_callback(uint8_t status, uint16_t length,
83 const void *param, void *user_data)
85 struct test_data *data = tester_get_data();
86 const struct mgmt_rp_read_info *rp = param;
88 uint16_t manufacturer;
89 uint32_t supported_settings, current_settings;
91 tester_print("Read Info callback");
92 tester_print(" Status: 0x%02x", status);
94 if (status || !param) {
95 tester_pre_setup_failed();
99 ba2str(&rp->bdaddr, addr);
100 manufacturer = btohs(rp->manufacturer);
101 supported_settings = btohl(rp->supported_settings);
102 current_settings = btohl(rp->current_settings);
104 tester_print(" Address: %s", addr);
105 tester_print(" Version: 0x%02x", rp->version);
106 tester_print(" Manufacturer: 0x%04x", manufacturer);
107 tester_print(" Supported settings: 0x%08x", supported_settings);
108 tester_print(" Current settings: 0x%08x", current_settings);
109 tester_print(" Class: 0x%02x%02x%02x",
110 rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
111 tester_print(" Name: %s", rp->name);
112 tester_print(" Short name: %s", rp->short_name);
114 if (strcmp(hciemu_get_address(data->hciemu), addr)) {
115 tester_pre_setup_failed();
119 tester_pre_setup_complete();
122 static void index_added_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 Added callback");
128 tester_print(" Index: 0x%04x", index);
130 data->mgmt_index = index;
132 mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
133 read_info_callback, NULL, NULL);
136 static void index_removed_callback(uint16_t index, uint16_t length,
137 const void *param, void *user_data)
139 struct test_data *data = tester_get_data();
141 tester_print("Index Removed callback");
142 tester_print(" Index: 0x%04x", index);
144 if (index != data->mgmt_index)
147 mgmt_unregister_index(data->mgmt, data->mgmt_index);
149 mgmt_unref(data->mgmt);
152 tester_post_teardown_complete();
155 static void read_index_list_callback(uint8_t status, uint16_t length,
156 const void *param, void *user_data)
158 struct test_data *data = tester_get_data();
160 tester_print("Read Index List callback");
161 tester_print(" Status: 0x%02x", status);
163 if (status || !param) {
164 tester_pre_setup_failed();
168 mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
169 index_added_callback, NULL, NULL);
171 mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
172 index_removed_callback, NULL, NULL);
174 data->hciemu = hciemu_new(data->hciemu_type);
176 tester_warn("Failed to setup HCI emulation");
177 tester_pre_setup_failed();
180 tester_print("New hciemu instance created");
183 static void test_pre_setup(const void *test_data)
185 struct test_data *data = tester_get_data();
187 data->mgmt = mgmt_new_default();
189 tester_warn("Failed to setup management interface");
190 tester_pre_setup_failed();
194 if (tester_use_debug())
195 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
197 mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
198 read_index_list_callback, NULL, NULL);
201 static void test_post_teardown(const void *test_data)
203 struct test_data *data = tester_get_data();
205 if (data->io_id > 0) {
206 g_source_remove(data->io_id);
210 hciemu_unref(data->hciemu);
214 static void test_data_free(void *test_data)
216 struct test_data *data = test_data;
221 static void client_connectable_complete(uint16_t opcode, uint8_t status,
222 const void *param, uint8_t len,
226 case BT_HCI_CMD_WRITE_SCAN_ENABLE:
232 tester_print("Client set connectable status 0x%02x", status);
235 tester_setup_failed();
237 tester_setup_complete();
240 static void setup_powered_client_callback(uint8_t status, uint16_t length,
241 const void *param, void *user_data)
243 struct test_data *data = tester_get_data();
244 struct bthost *bthost;
246 if (status != MGMT_STATUS_SUCCESS) {
247 tester_setup_failed();
251 tester_print("Controller powered on");
253 bthost = hciemu_client_get_host(data->hciemu);
254 bthost_set_cmd_complete_cb(bthost, client_connectable_complete, data);
255 bthost_write_scan_enable(bthost, 0x03);
258 static void setup_powered_client(const void *test_data)
260 struct test_data *data = tester_get_data();
261 unsigned char param[] = { 0x01 };
263 tester_print("Powering on controller");
265 mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
266 sizeof(param), param, setup_powered_client_callback,
270 static void test_basic(const void *test_data)
274 sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
276 tester_warn("Can't create socket: %s (%d)", strerror(errno),
278 tester_test_failed();
284 tester_test_passed();
287 #define test_bnep(name, data, setup, func) \
289 struct test_data *user; \
290 user = malloc(sizeof(struct test_data)); \
293 user->hciemu_type = HCIEMU_TYPE_BREDR; \
294 user->test_data = data; \
296 tester_add_full(name, data, \
297 test_pre_setup, setup, func, NULL, \
298 test_post_teardown, 2, user, test_data_free); \
301 int main(int argc, char *argv[])
303 tester_init(&argc, &argv);
305 test_bnep("Basic BNEP Socket - Success", NULL,
306 setup_powered_client, test_basic);