Blame tools/bnep-tester.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2014  Intel Corporation. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <stdint.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <errno.h>
Packit 34410b
#include <stdbool.h>
Packit 34410b
Packit 34410b
#include <glib.h>
Packit 34410b
Packit 34410b
#include "lib/bluetooth.h"
Packit 34410b
#include "lib/bnep.h"
Packit 34410b
#include "lib/mgmt.h"
Packit 34410b
Packit 34410b
#include "monitor/bt.h"
Packit 34410b
#include "emulator/bthost.h"
Packit 34410b
#include "emulator/hciemu.h"
Packit 34410b
Packit 34410b
#include "src/shared/tester.h"
Packit 34410b
#include "src/shared/mgmt.h"
Packit 34410b
Packit 34410b
struct test_data {
Packit 34410b
	struct mgmt *mgmt;
Packit 34410b
	uint16_t mgmt_index;
Packit 34410b
	struct hciemu *hciemu;
Packit 34410b
	enum hciemu_type hciemu_type;
Packit 34410b
	const void *test_data;
Packit 34410b
	unsigned int io_id;
Packit 34410b
	uint16_t conn_handle;
Packit 34410b
};
Packit 34410b
Packit 34410b
struct rfcomm_client_data {
Packit 34410b
	uint8_t server_channel;
Packit 34410b
	uint8_t client_channel;
Packit 34410b
	int expected_connect_err;
Packit 34410b
	const uint8_t *send_data;
Packit 34410b
	const uint8_t *read_data;
Packit 34410b
	uint16_t data_len;
Packit 34410b
};
Packit 34410b
Packit 34410b
struct rfcomm_server_data {
Packit 34410b
	uint8_t server_channel;
Packit 34410b
	uint8_t client_channel;
Packit 34410b
	bool expected_status;
Packit 34410b
	const uint8_t *send_data;
Packit 34410b
	const uint8_t *read_data;
Packit 34410b
	uint16_t data_len;
Packit 34410b
};
Packit 34410b
Packit 34410b
static void mgmt_debug(const char *str, void *user_data)
Packit 34410b
{
Packit 34410b
	const char *prefix = user_data;
Packit 34410b
Packit 34410b
	tester_print("%s%s", prefix, str);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void read_info_callback(uint8_t status, uint16_t length,
Packit 34410b
					const void *param, void *user_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
	const struct mgmt_rp_read_info *rp = param;
Packit 34410b
	char addr[18];
Packit 34410b
	uint16_t manufacturer;
Packit 34410b
	uint32_t supported_settings, current_settings;
Packit 34410b
Packit 34410b
	tester_print("Read Info callback");
Packit 34410b
	tester_print("  Status: 0x%02x", status);
Packit 34410b
Packit 34410b
	if (status || !param) {
Packit 34410b
		tester_pre_setup_failed();
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	ba2str(&rp->bdaddr, addr);
Packit 34410b
	manufacturer = btohs(rp->manufacturer);
Packit 34410b
	supported_settings = btohl(rp->supported_settings);
Packit 34410b
	current_settings = btohl(rp->current_settings);
Packit 34410b
Packit 34410b
	tester_print("  Address: %s", addr);
Packit 34410b
	tester_print("  Version: 0x%02x", rp->version);
Packit 34410b
	tester_print("  Manufacturer: 0x%04x", manufacturer);
Packit 34410b
	tester_print("  Supported settings: 0x%08x", supported_settings);
Packit 34410b
	tester_print("  Current settings: 0x%08x", current_settings);
Packit 34410b
	tester_print("  Class: 0x%02x%02x%02x",
Packit 34410b
			rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
Packit 34410b
	tester_print("  Name: %s", rp->name);
Packit 34410b
	tester_print("  Short name: %s", rp->short_name);
Packit 34410b
Packit 34410b
	if (strcmp(hciemu_get_address(data->hciemu), addr)) {
Packit 34410b
		tester_pre_setup_failed();
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	tester_pre_setup_complete();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void index_added_callback(uint16_t index, uint16_t length,
Packit 34410b
					const void *param, void *user_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
Packit 34410b
	tester_print("Index Added callback");
Packit 34410b
	tester_print("  Index: 0x%04x", index);
Packit 34410b
Packit 34410b
	data->mgmt_index = index;
Packit 34410b
Packit 34410b
	mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
Packit 34410b
					read_info_callback, NULL, NULL);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void index_removed_callback(uint16_t index, uint16_t length,
Packit 34410b
					const void *param, void *user_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
Packit 34410b
	tester_print("Index Removed callback");
Packit 34410b
	tester_print("  Index: 0x%04x", index);
Packit 34410b
Packit 34410b
	if (index != data->mgmt_index)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	mgmt_unregister_index(data->mgmt, data->mgmt_index);
Packit 34410b
Packit 34410b
	mgmt_unref(data->mgmt);
Packit 34410b
	data->mgmt = NULL;
Packit 34410b
Packit 34410b
	tester_post_teardown_complete();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void read_index_list_callback(uint8_t status, uint16_t length,
Packit 34410b
					const void *param, void *user_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
Packit 34410b
	tester_print("Read Index List callback");
Packit 34410b
	tester_print("  Status: 0x%02x", status);
Packit 34410b
Packit 34410b
	if (status || !param) {
Packit 34410b
		tester_pre_setup_failed();
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
Packit 34410b
					index_added_callback, NULL, NULL);
Packit 34410b
Packit 34410b
	mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
Packit 34410b
					index_removed_callback, NULL, NULL);
Packit 34410b
Packit 34410b
	data->hciemu = hciemu_new(data->hciemu_type);
Packit 34410b
	if (!data->hciemu) {
Packit 34410b
		tester_warn("Failed to setup HCI emulation");
Packit 34410b
		tester_pre_setup_failed();
Packit 34410b
	}
Packit 34410b
Packit 34410b
	tester_print("New hciemu instance created");
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_pre_setup(const void *test_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
Packit 34410b
	data->mgmt = mgmt_new_default();
Packit 34410b
	if (!data->mgmt) {
Packit 34410b
		tester_warn("Failed to setup management interface");
Packit 34410b
		tester_pre_setup_failed();
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (tester_use_debug())
Packit 34410b
		mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
Packit 34410b
Packit 34410b
	mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
Packit 34410b
					read_index_list_callback, NULL, NULL);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_post_teardown(const void *test_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
Packit 34410b
	if (data->io_id > 0) {
Packit 34410b
		g_source_remove(data->io_id);
Packit 34410b
		data->io_id = 0;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	hciemu_unref(data->hciemu);
Packit 34410b
	data->hciemu = NULL;
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_data_free(void *test_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = test_data;
Packit 34410b
Packit 34410b
	free(data);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void client_connectable_complete(uint16_t opcode, uint8_t status,
Packit 34410b
					const void *param, uint8_t len,
Packit 34410b
					void *user_data)
Packit 34410b
{
Packit 34410b
	switch (opcode) {
Packit 34410b
	case BT_HCI_CMD_WRITE_SCAN_ENABLE:
Packit 34410b
		break;
Packit 34410b
	default:
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	tester_print("Client set connectable status 0x%02x", status);
Packit 34410b
Packit 34410b
	if (status)
Packit 34410b
		tester_setup_failed();
Packit 34410b
	else
Packit 34410b
		tester_setup_complete();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void setup_powered_client_callback(uint8_t status, uint16_t length,
Packit 34410b
					const void *param, void *user_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
	struct bthost *bthost;
Packit 34410b
Packit 34410b
	if (status != MGMT_STATUS_SUCCESS) {
Packit 34410b
		tester_setup_failed();
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	tester_print("Controller powered on");
Packit 34410b
Packit 34410b
	bthost = hciemu_client_get_host(data->hciemu);
Packit 34410b
	bthost_set_cmd_complete_cb(bthost, client_connectable_complete, data);
Packit 34410b
	bthost_write_scan_enable(bthost, 0x03);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void setup_powered_client(const void *test_data)
Packit 34410b
{
Packit 34410b
	struct test_data *data = tester_get_data();
Packit 34410b
	unsigned char param[] = { 0x01 };
Packit 34410b
Packit 34410b
	tester_print("Powering on controller");
Packit 34410b
Packit 34410b
	mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
Packit 34410b
			sizeof(param), param, setup_powered_client_callback,
Packit 34410b
			NULL, NULL);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_basic(const void *test_data)
Packit 34410b
{
Packit 34410b
	int sk;
Packit 34410b
Packit 34410b
	sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
Packit 34410b
	if (sk < 0) {
Packit 34410b
		tester_warn("Can't create socket: %s (%d)", strerror(errno),
Packit 34410b
									errno);
Packit 34410b
		tester_test_failed();
Packit 34410b
		return;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	close(sk);
Packit 34410b
Packit 34410b
	tester_test_passed();
Packit 34410b
}
Packit 34410b
Packit 34410b
#define test_bnep(name, data, setup, func) \
Packit 34410b
	do { \
Packit 34410b
		struct test_data *user; \
Packit 34410b
		user = malloc(sizeof(struct test_data)); \
Packit 34410b
		if (!user) \
Packit 34410b
			break; \
Packit 34410b
		user->hciemu_type = HCIEMU_TYPE_BREDR; \
Packit 34410b
		user->test_data = data; \
Packit 34410b
		user->io_id = 0; \
Packit 34410b
		tester_add_full(name, data, \
Packit 34410b
				test_pre_setup, setup, func, NULL, \
Packit 34410b
				test_post_teardown, 2, user, test_data_free); \
Packit 34410b
	} while (0)
Packit 34410b
Packit 34410b
int main(int argc, char *argv[])
Packit 34410b
{
Packit 34410b
	tester_init(&argc, &argv);
Packit 34410b
Packit 34410b
	test_bnep("Basic BNEP Socket - Success", NULL,
Packit 34410b
					setup_powered_client, test_basic);
Packit 34410b
Packit 34410b
	return tester_run();
Packit 34410b
}