static void test_connect(const void *test_data) { struct test_data *data = tester_get_data(); const struct l2cap_client_data *l2data = data->test_data; GIOChannel *io; int sk; if (l2data->server_psm) { struct bthost *bthost = hciemu_client_get_host(data->hciemu); bthost_set_server_psm(bthost, l2data->server_psm); } sk = create_l2cap_sock(data, 0); if (sk < 0) { tester_test_failed(); return; } if (connect_l2cap_sock(data, sk, l2data->client_psm) < 0) { close(sk); tester_test_failed(); return; } io = g_io_channel_unix_new(sk); g_io_channel_set_close_on_unref(io, TRUE); data->io_id = g_io_add_watch(io, G_IO_OUT, l2cap_connect_cb, NULL); g_io_channel_unref(io); tester_print("Connect in progress"); }
static gboolean l2cap_listen_cb(GIOChannel *io, GIOCondition cond, gpointer user_data) { struct test_data *data = tester_get_data(); const struct l2cap_data *l2data = data->test_data; int sk, new_sk; data->io_id = 0; sk = g_io_channel_unix_get_fd(io); new_sk = accept(sk, NULL, NULL); if (new_sk < 0) { tester_warn("accept failed: %s (%u)", strerror(errno), errno); tester_test_failed(); return FALSE; } if (l2data->read_data) { struct bthost *bthost; GIOChannel *new_io; new_io = g_io_channel_unix_new(new_sk); g_io_channel_set_close_on_unref(new_io, TRUE); bthost = hciemu_client_get_host(data->hciemu); g_io_add_watch(new_io, G_IO_IN, server_received_data, NULL); bthost_send_cid(bthost, data->handle, data->dcid, l2data->read_data, l2data->data_len); g_io_channel_unref(new_io); return FALSE; } else if (l2data->write_data) { struct bthost *bthost; ssize_t ret; bthost = hciemu_client_get_host(data->hciemu); bthost_add_cid_hook(bthost, data->handle, data->scid, server_bthost_received_data, NULL); ret = write(new_sk, l2data->write_data, l2data->data_len); close(new_sk); if (ret != l2data->data_len) { tester_warn("Unable to write all data"); tester_test_failed(); } return FALSE; } tester_print("Successfully connected"); close(new_sk); tester_test_passed(); return FALSE; }
static gboolean l2cap_connect_cb(GIOChannel *io, GIOCondition cond, gpointer user_data) { struct test_data *data = tester_get_data(); const struct l2cap_client_data *l2data = data->test_data; int err, sk_err, sk; socklen_t len = sizeof(sk_err); data->io_id = 0; sk = g_io_channel_unix_get_fd(io); if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0) err = -errno; else err = -sk_err; if (err < 0) tester_warn("Connect failed: %s (%d)", strerror(-err), -err); else tester_print("Successfully connected"); if (-err != l2data->expect_err) tester_test_failed(); else tester_test_passed(); return FALSE; }
static void make_pk(struct test_data *data) { if (!ecc_make_key(data->local_pk, data->local_sk)) { tester_print("Failed to general local ECDH keypair"); tester_setup_failed(); return; } }
static void index_removed_callback(uint16_t index, uint16_t length, const void *param, void *user_data) { struct test_data *data = tester_get_data(); tester_print("Index Removed callback"); tester_print(" Index: 0x%04x", index); if (index != data->mgmt_index) return; mgmt_unregister_index(data->mgmt, data->mgmt_index); mgmt_unref(data->mgmt); data->mgmt = NULL; tester_post_teardown_complete(); }
static gboolean l2cap_connect_cb(GIOChannel *io, GIOCondition cond, gpointer user_data) { struct test_data *data = tester_get_data(); const struct l2cap_data *l2data = data->test_data; int err, sk_err, sk; socklen_t len = sizeof(sk_err); data->io_id = 0; sk = g_io_channel_unix_get_fd(io); if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0) err = -errno; else err = -sk_err; if (err < 0) tester_warn("Connect failed: %s (%d)", strerror(-err), -err); else tester_print("Successfully connected"); if (l2data->read_data) { struct bthost *bthost; bthost = hciemu_client_get_host(data->hciemu); g_io_add_watch(io, G_IO_IN, client_received_data, NULL); bthost_send_cid(bthost, data->handle, data->dcid, l2data->read_data, l2data->data_len); return FALSE; } else if (l2data->write_data) { struct bthost *bthost; ssize_t ret; bthost = hciemu_client_get_host(data->hciemu); bthost_add_cid_hook(bthost, data->handle, data->dcid, bthost_received_data, NULL); ret = write(sk, l2data->write_data, l2data->data_len); if (ret != l2data->data_len) { tester_warn("Unable to write all data"); tester_test_failed(); } return FALSE; } if (-err != l2data->expect_err) tester_test_failed(); else tester_test_passed(); return FALSE; }
static void pair_device_complete(uint8_t status, uint16_t length, const void *param, void *user_data) { if (status != MGMT_STATUS_SUCCESS) { tester_warn("Pairing failed: %s", mgmt_errstr(status)); return; } tester_print("Pairing succeedded"); }
static void conn_cb(uint16_t handle, void *user_data) { struct test_data *data = tester_get_data(); struct bthost *bthost = hciemu_client_get_host(data->hciemu); tester_print("New connection with handle 0x%04x", handle); test_conn_handle = handle; bthost_request_auth(bthost, handle); }
static void setup_powered(const void *test_data) { struct test_data *data = tester_get_data(); unsigned char param[] = { 0x01 }; tester_print("Powering on controller"); mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index, sizeof(param), param, setup_powered_client_callback, NULL, NULL); }
static void test_server(const void *test_data) { struct test_data *data = tester_get_data(); const struct l2cap_data *l2data = data->test_data; const uint8_t *master_bdaddr; uint8_t addr_type; struct bthost *bthost; GIOChannel *io; int sk; if (l2data->server_psm || l2data->cid) { sk = create_l2cap_sock(data, l2data->server_psm, l2data->cid, l2data->sec_level); if (sk < 0) { tester_test_failed(); return; } if (listen(sk, 5) < 0) { tester_warn("listening on socket failed: %s (%u)", strerror(errno), errno); tester_test_failed(); close(sk); return; } io = g_io_channel_unix_new(sk); g_io_channel_set_close_on_unref(io, TRUE); data->io_id = g_io_add_watch(io, G_IO_IN, l2cap_listen_cb, NULL); g_io_channel_unref(io); tester_print("Listening for connections"); } master_bdaddr = hciemu_get_master_bdaddr(data->hciemu); if (!master_bdaddr) { tester_warn("No master bdaddr"); tester_test_failed(); return; } bthost = hciemu_client_get_host(data->hciemu); bthost_set_connect_cb(bthost, send_req_new_conn, data); if (data->hciemu_type == HCIEMU_TYPE_BREDR) addr_type = BDADDR_BREDR; else addr_type = BDADDR_LE_PUBLIC; bthost_hci_connect(bthost, master_bdaddr, addr_type); }
static gboolean test_close_socket_1_part_3(gpointer arg) { struct test_data *data = tester_get_data(); tester_print("Checking whether scan was properly stopped..."); if (data->sk != -1) { tester_print("Error - scan was not enabled yet"); tester_test_failed(); return FALSE; } if (hciemu_get_master_le_scan_enable(data->hciemu)) { tester_print("Delayed check whether scann is off failed"); tester_test_failed(); return FALSE; } tester_test_passed(); return FALSE; }
static void test_condition_complete(struct test_data *data) { data->unmet_conditions--; tester_print("Test condition complete, %d left", data->unmet_conditions); if (data->unmet_conditions > 0) return; tester_test_passed(); }
static void setup_powered_server_callback(uint8_t status, uint16_t length, const void *param, void *user_data) { if (status != MGMT_STATUS_SUCCESS) { tester_setup_failed(); return; } tester_print("Controller powered on"); tester_setup_complete(); }
static void read_info_callback(uint8_t status, uint16_t length, const void *param, void *user_data) { struct test_data *data = tester_get_data(); const struct mgmt_rp_read_info *rp = param; char addr[18]; uint16_t manufacturer; uint32_t supported_settings, current_settings; tester_print("Read Info callback"); tester_print(" Status: 0x%02x", status); if (status || !param) { tester_pre_setup_failed(); return; } ba2str(&rp->bdaddr, addr); manufacturer = btohs(rp->manufacturer); supported_settings = btohl(rp->supported_settings); current_settings = btohl(rp->current_settings); tester_print(" Address: %s", addr); tester_print(" Version: 0x%02x", rp->version); tester_print(" Manufacturer: 0x%04x", manufacturer); tester_print(" Supported settings: 0x%08x", supported_settings); tester_print(" Current settings: 0x%08x", current_settings); tester_print(" Class: 0x%02x%02x%02x", rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]); tester_print(" Name: %s", rp->name); tester_print(" Short name: %s", rp->short_name); if (strcmp(hciemu_get_address(data->hciemu), addr)) { tester_pre_setup_failed(); return; } tester_pre_setup_complete(); }
static void client_connectable_complete(uint16_t opcode, uint8_t status, const void *param, uint8_t len, void *user_data) { if (opcode != BT_HCI_CMD_LE_SET_ADV_ENABLE) return; tester_print("Client set connectable status 0x%02x", status); if (status) tester_setup_failed(); else tester_setup_complete(); }
static void toggle_powered(const void *test_data) { struct test_data *data = tester_get_data(); bool power = PTR_TO_INT(test_data); unsigned char param[1]; param[0] = power ? 0x01 : 0x00; tester_print("Powering %s controller", power ? "on" : "off"); mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index, sizeof(param), param, toggle_powered_client_callback, INT_TO_PTR(power), NULL); }
static void read_index_list_callback(uint8_t status, uint16_t length, const void *param, void *user_data) { struct test_data *data = tester_get_data(); tester_print("Read Index List callback"); tester_print(" Status: 0x%02x", status); if (status || !param) { tester_pre_setup_failed(); return; } mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE, index_added_callback, NULL, NULL); mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE, index_removed_callback, NULL, NULL); data->hciemu = hciemu_new(HCIEMU_TYPE_BREDRLE); if (!data->hciemu) { tester_warn("Failed to setup HCI emulation"); tester_pre_setup_failed(); } tester_print("New hciemu instance created"); if (data->disable_esco) { uint8_t *features; tester_print("Disabling eSCO packet type support"); features = hciemu_get_features(data->hciemu); if (features) features[3] &= ~0x80; } }
static void client_l2cap_rsp(uint8_t code, const void *data, uint16_t len, void *user_data) { struct test_data *test_data = user_data; const struct l2cap_data *l2data = test_data->test_data; tester_print("Client received response code 0x%02x", code); if (code != l2data->expect_cmd_code) { tester_warn("Unexpected L2CAP response code (expected 0x%02x)", l2data->expect_cmd_code); goto failed; } if (code == BT_L2CAP_PDU_CONN_RSP) { const struct bt_l2cap_pdu_conn_rsp *rsp = data; if (len == sizeof(rsp) && !rsp->result && !rsp->status) return; test_data->dcid = rsp->dcid; test_data->scid = rsp->scid; if (l2data->data_len) return; } if (!l2data->expect_cmd) { tester_test_passed(); return; } if (l2data->expect_cmd_len != len) { tester_warn("Unexpected L2CAP response length (%u != %u)", len, l2data->expect_cmd_len); goto failed; } if (memcmp(l2data->expect_cmd, data, len) != 0) { tester_warn("Unexpected L2CAP response"); goto failed; } tester_test_passed(); return; failed: tester_test_failed(); }
static void connect_socket(const uint8_t *client_bdaddr, int *sk_holder, GIOFunc connect_cb) { struct test_data *data = tester_get_data(); const struct l2cap_data *l2data = data->test_data; GIOChannel *io; int sk; sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level); if (sk < 0) { tester_print("Error in create_l2cap_sock"); tester_test_failed(); return; } *sk_holder = sk; if (connect_l2cap_impl(sk, client_bdaddr, BDADDR_LE_PUBLIC, l2data->client_psm, l2data->cid) < 0) { tester_print("Error in connect_l2cap_sock"); close(sk); tester_test_failed(); return; } if (connect_cb) { io = g_io_channel_unix_new(sk); g_io_channel_set_close_on_unref(io, TRUE); data->io_id = g_io_add_watch(io, G_IO_OUT, connect_cb, NULL); g_io_channel_unref(io); } tester_print("Connect in progress, sk = %d", sk); }
static void client_new_conn(uint16_t handle, void *user_data) { struct test_data *data = user_data; const struct l2cap_server_data *l2data = data->test_data; struct bthost *bthost; tester_print("New client connection with handle 0x%04x", handle); if (l2data->send_req) { bthost_l2cap_rsp_cb cb; if (l2data->expect_rsp_code) cb = client_l2cap_rsp; else cb = NULL; tester_print("Sending L2CAP Request from client"); bthost = hciemu_client_get_host(data->hciemu); bthost_l2cap_req(bthost, handle, l2data->send_req_code, l2data->send_req, l2data->send_req_len, cb, data); } }
static gboolean test_close_socket_2_part_3(gpointer arg) { struct test_data *data = tester_get_data(); int sk = data->sk; int err; /* Scan should be already over, we're trying to create connection */ if (hciemu_get_master_le_scan_enable(data->hciemu)) { tester_print("Error - should no longer scan"); tester_test_failed(); return FALSE; } /* Calling close() should eventually cause CMD_LE_CREATE_CONN_CANCEL */ err = close(sk); if (err < 0) { tester_print("Error when closing socket"); tester_test_failed(); return FALSE; } /* CMD_LE_CREATE_CONN_CANCEL will trigger test pass. */ return FALSE; }
static void read_index_list_callback(uint8_t status, uint16_t length, const void *param, void *user_data) { struct test_data *data = tester_get_data(); tester_print("Read Index List callback"); tester_print(" Status: 0x%02x", status); if (status || !param) { tester_pre_setup_failed(); return; } mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE, index_added_callback, NULL, NULL); data->hciemu = hciemu_new(data->hciemu_type); if (!data->hciemu) { tester_warn("Failed to setup HCI emulation"); tester_pre_setup_failed(); } tester_print("New hciemu instance created"); }
static void test_connect_two_sockets_router(uint16_t opcode, const void *param, uint8_t length, void *user_data) { const struct bt_hci_cmd_le_set_scan_enable *scan_params = param; tester_print("HCI Command 0x%04x length %u", opcode, length); if (opcode == BT_HCI_CMD_LE_SET_SCAN_ENABLE && scan_params->enable == true) { test_scan_enable_counter++; if (test_scan_enable_counter == 1) test_connect_two_sockets_part_2(); else if (test_scan_enable_counter == 2) g_idle_add(enable_advertising, NULL); } }
static void test_connect_two_sockets_part_2(void) { struct test_data *data = tester_get_data(); const struct l2cap_data *l2data = data->test_data; const uint8_t *client_bdaddr; client_bdaddr = hciemu_get_client_bdaddr(data->hciemu); connect_socket(client_bdaddr, &data->sk2, test_two_sockets_connect_cb); if (l2data->close_one_socket) { tester_print("Closing first socket! %d", data->sk); close(data->sk); } g_idle_add(enable_advertising, NULL); }
static void setup_powered_client_callback(uint8_t status, uint16_t length, const void *param, void *user_data) { struct test_data *data = tester_get_data(); struct bthost *bthost; if (status != MGMT_STATUS_SUCCESS) { tester_setup_failed(); return; } tester_print("Controller powered on"); bthost = hciemu_client_get_host(data->hciemu); bthost_set_cmd_complete_cb(bthost, client_connectable_complete, data); bthost_set_adv_enable(bthost, 0x01, 0x00); }
static void server_cmd_complete(uint16_t opcode, uint8_t status, const void *param, uint8_t len, void *user_data) { switch (opcode) { case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE: tester_print("Server enable SSP status 0x%02x", status); break; default: return; } if (status) tester_setup_failed(); else tester_setup_complete(); }
static void toggle_powered_client_callback(uint8_t status, uint16_t length, const void *param, void *user_data) { bool power = PTR_TO_INT(user_data); if (status != MGMT_STATUS_SUCCESS) { tester_setup_failed(); return; } tester_print("Controller powered %s", power ? "on" : "off"); if (power) toggle_powered(false); else tester_setup_complete(); }
static void map_client_conn_cb(uint16_t handle, void *user_data) { struct test_data *data = tester_get_data(); struct bthost *bthost = hciemu_client_get_host(data->hciemu); tester_print("New connection with handle 0x%04x", handle); if (data->hciemu_type == HCIEMU_TYPE_BREDR) { tester_warn("Not handled device type."); return; } cid_data.cid = 0x0040; cid_data.handle = handle; bthost_add_cid_hook(bthost, handle, cid_data.cid, map_client_cid_hook_cb, &cid_data); }
static void send_rsp_new_conn(uint16_t handle, void *user_data) { struct test_data *data = user_data; struct bthost *bthost; tester_print("New connection with handle 0x%04x", handle); data->handle = handle; if (data->hciemu_type == HCIEMU_TYPE_LE) data->dcid = 0x0005; else data->dcid = 0x0001; bthost = hciemu_client_get_host(data->hciemu); bthost_add_cid_hook(bthost, data->handle, data->dcid, bthost_send_rsp, NULL); }
static void client_connectable_complete(uint16_t opcode, uint8_t status, const void *param, uint8_t len, void *user_data) { switch (opcode) { case BT_HCI_CMD_WRITE_SCAN_ENABLE: break; default: return; } tester_print("Client set connectable status 0x%02x", status); if (status) tester_setup_failed(); else tester_setup_complete(); }