Blame tools/btinfo.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2011-2012  Intel Corporation
Packit 34410b
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
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 <ctype.h>
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <string.h>
Packit 34410b
#include <getopt.h>
Packit 34410b
#include <sys/ioctl.h>
Packit 34410b
#include <sys/socket.h>
Packit 34410b
Packit 34410b
#include "monitor/bt.h"
Packit 34410b
#include "src/shared/mainloop.h"
Packit 34410b
#include "src/shared/timeout.h"
Packit 34410b
#include "src/shared/util.h"
Packit 34410b
#include "src/shared/hci.h"
Packit 34410b
Packit 34410b
#define BTPROTO_HCI	1
Packit 34410b
Packit 34410b
struct hci_dev_stats {
Packit 34410b
	uint32_t err_rx;
Packit 34410b
	uint32_t err_tx;
Packit 34410b
	uint32_t cmd_tx;
Packit 34410b
	uint32_t evt_rx;
Packit 34410b
	uint32_t acl_tx;
Packit 34410b
	uint32_t acl_rx;
Packit 34410b
	uint32_t sco_tx;
Packit 34410b
	uint32_t sco_rx;
Packit 34410b
	uint32_t byte_rx;
Packit 34410b
	uint32_t byte_tx;
Packit 34410b
};
Packit 34410b
Packit 34410b
struct hci_dev_info {
Packit 34410b
	uint16_t dev_id;
Packit 34410b
	char     name[8];
Packit 34410b
	uint8_t  bdaddr[6];
Packit 34410b
	uint32_t flags;
Packit 34410b
	uint8_t  type;
Packit 34410b
	uint8_t  features[8];
Packit 34410b
	uint32_t pkt_type;
Packit 34410b
	uint32_t link_policy;
Packit 34410b
	uint32_t link_mode;
Packit 34410b
	uint16_t acl_mtu;
Packit 34410b
	uint16_t acl_pkts;
Packit 34410b
	uint16_t sco_mtu;
Packit 34410b
	uint16_t sco_pkts;
Packit 34410b
	struct   hci_dev_stats stat;
Packit 34410b
};
Packit 34410b
Packit 34410b
#define HCIDEVUP	_IOW('H', 201, int)
Packit 34410b
#define HCIDEVDOWN	_IOW('H', 202, int)
Packit 34410b
#define HCIGETDEVINFO	_IOR('H', 211, int)
Packit 34410b
Packit 34410b
#define HCI_UP		(1 << 0)
Packit 34410b
Packit 34410b
#define HCI_PRIMARY	0x00
Packit 34410b
#define HCI_AMP		0x01
Packit 34410b
Packit 34410b
static struct hci_dev_info hci_info;
Packit 34410b
static uint8_t hci_type;
Packit 34410b
static struct bt_hci *hci_dev;
Packit 34410b
Packit 34410b
static bool reset_on_init = false;
Packit 34410b
static bool reset_on_shutdown = false;
Packit 34410b
Packit 34410b
static bool shutdown_timeout(void *user_data)
Packit 34410b
{
Packit 34410b
	mainloop_quit();
Packit 34410b
Packit 34410b
	return false;
Packit 34410b
}
Packit 34410b
Packit 34410b
static void shutdown_complete(const void *data, uint8_t size, void *user_data)
Packit 34410b
{
Packit 34410b
	unsigned int id = PTR_TO_UINT(user_data);
Packit 34410b
Packit 34410b
	timeout_remove(id);
Packit 34410b
	mainloop_quit();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void shutdown_device(void)
Packit 34410b
{
Packit 34410b
	unsigned int id;
Packit 34410b
Packit 34410b
	bt_hci_flush(hci_dev);
Packit 34410b
Packit 34410b
	if (reset_on_shutdown) {
Packit 34410b
		id = timeout_add(5000, shutdown_timeout, NULL, NULL);
Packit 34410b
Packit 34410b
		bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
Packit 34410b
				shutdown_complete, UINT_TO_PTR(id), NULL);
Packit 34410b
	} else
Packit 34410b
		mainloop_quit();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void local_version_callback(const void *data, uint8_t size,
Packit 34410b
							void *user_data)
Packit 34410b
{
Packit 34410b
	const struct bt_hci_rsp_read_local_version *rsp = data;
Packit 34410b
Packit 34410b
	printf("HCI version: %u\n", rsp->hci_ver);
Packit 34410b
	printf("HCI revision: %u\n", le16_to_cpu(rsp->hci_rev));
Packit 34410b
Packit 34410b
	switch (hci_type) {
Packit 34410b
	case HCI_PRIMARY:
Packit 34410b
		printf("LMP version: %u\n", rsp->lmp_ver);
Packit 34410b
		printf("LMP subversion: %u\n", le16_to_cpu(rsp->lmp_subver));
Packit 34410b
		break;
Packit 34410b
	case HCI_AMP:
Packit 34410b
		printf("PAL version: %u\n", rsp->lmp_ver);
Packit 34410b
		printf("PAL subversion: %u\n", le16_to_cpu(rsp->lmp_subver));
Packit 34410b
		break;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	printf("Manufacturer: %u\n", le16_to_cpu(rsp->manufacturer));
Packit 34410b
}
Packit 34410b
Packit 34410b
static void local_commands_callback(const void *data, uint8_t size,
Packit 34410b
							void *user_data)
Packit 34410b
{
Packit 34410b
	shutdown_device();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void local_features_callback(const void *data, uint8_t size,
Packit 34410b
							void *user_data)
Packit 34410b
{
Packit 34410b
	bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_COMMANDS, NULL, 0,
Packit 34410b
					local_commands_callback, NULL, NULL);
Packit 34410b
}
Packit 34410b
Packit 34410b
static bool cmd_local(int argc, char *argv[])
Packit 34410b
{
Packit 34410b
	if (reset_on_init)
Packit 34410b
		bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
Packit 34410b
						NULL, NULL, NULL);
Packit 34410b
Packit 34410b
	bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_VERSION, NULL, 0,
Packit 34410b
					local_version_callback, NULL, NULL);
Packit 34410b
Packit 34410b
	bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0,
Packit 34410b
					local_features_callback, NULL, NULL);
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
typedef bool (*cmd_func_t)(int argc, char *argv[]);
Packit 34410b
Packit 34410b
static const struct {
Packit 34410b
	const char *name;
Packit 34410b
	cmd_func_t func;
Packit 34410b
	const char *help;
Packit 34410b
} cmd_table[] = {
Packit 34410b
	{ "local", cmd_local, "Print local controller details" },
Packit 34410b
	{ }
Packit 34410b
};
Packit 34410b
Packit 34410b
static void signal_callback(int signum, void *user_data)
Packit 34410b
{
Packit 34410b
	static bool terminated = false;
Packit 34410b
Packit 34410b
	switch (signum) {
Packit 34410b
	case SIGINT:
Packit 34410b
	case SIGTERM:
Packit 34410b
		if (!terminated) {
Packit 34410b
			shutdown_device();
Packit 34410b
			terminated = true;
Packit 34410b
		}
Packit 34410b
		break;
Packit 34410b
	}
Packit 34410b
}
Packit 34410b
Packit 34410b
static void usage(void)
Packit 34410b
{
Packit 34410b
	int i;
Packit 34410b
Packit 34410b
	printf("btinfo - Bluetooth device testing tool\n"
Packit 34410b
		"Usage:\n");
Packit 34410b
	printf("\tbtinfo [options] <command>\n");
Packit 34410b
	printf("options:\n"
Packit 34410b
		"\t-i, --index <num>      Use specified controller\n"
Packit 34410b
		"\t-h, --help             Show help options\n");
Packit 34410b
	printf("commands:\n");
Packit 34410b
	for (i = 0; cmd_table[i].name; i++)
Packit 34410b
		printf("\t%-25s%s\n", cmd_table[i].name, cmd_table[i].help);
Packit 34410b
}
Packit 34410b
Packit 34410b
static const struct option main_options[] = {
Packit 34410b
	{ "index",   required_argument, NULL, 'i' },
Packit 34410b
	{ "reset",   no_argument,       NULL, 'r' },
Packit 34410b
	{ "raw",     no_argument,       NULL, 'R' },
Packit 34410b
	{ "version", no_argument,       NULL, 'v' },
Packit 34410b
	{ "help",    no_argument,       NULL, 'h' },
Packit 34410b
	{ }
Packit 34410b
};
Packit 34410b
Packit 34410b
int main(int argc, char *argv[])
Packit 34410b
{
Packit 34410b
	cmd_func_t func = NULL;
Packit 34410b
	uint16_t index = 0;
Packit 34410b
	const char *str;
Packit 34410b
	bool use_raw = false;
Packit 34410b
	bool power_down = false;
Packit 34410b
	int fd, i, exit_status;
Packit 34410b
Packit 34410b
	for (;;) {
Packit 34410b
		int opt;
Packit 34410b
Packit 34410b
		opt = getopt_long(argc, argv, "i:rRvh", main_options, NULL);
Packit 34410b
		if (opt < 0)
Packit 34410b
			break;
Packit 34410b
Packit 34410b
		switch (opt) {
Packit 34410b
		case 'i':
Packit 34410b
			if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
Packit 34410b
				str = optarg + 3;
Packit 34410b
			else
Packit 34410b
				str = optarg;
Packit 34410b
			if (!isdigit(*str)) {
Packit 34410b
				usage();
Packit 34410b
				return EXIT_FAILURE;
Packit 34410b
			}
Packit 34410b
			index = atoi(str);
Packit 34410b
			break;
Packit 34410b
		case 'r':
Packit 34410b
			reset_on_init = true;
Packit 34410b
			break;
Packit 34410b
		case 'R':
Packit 34410b
			use_raw = true;
Packit 34410b
			break;
Packit 34410b
		case 'v':
Packit 34410b
			printf("%s\n", VERSION);
Packit 34410b
			return EXIT_SUCCESS;
Packit 34410b
		case 'h':
Packit 34410b
			usage();
Packit 34410b
			return EXIT_SUCCESS;
Packit 34410b
		default:
Packit 34410b
			return EXIT_FAILURE;
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (argc - optind < 1) {
Packit 34410b
		fprintf(stderr, "Missing command argument\n");
Packit 34410b
		return EXIT_FAILURE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	for (i = 0; cmd_table[i].name; i++) {
Packit 34410b
		if (!strcmp(cmd_table[i].name, argv[optind])) {
Packit 34410b
			func = cmd_table[i].func;
Packit 34410b
			break;
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (!func) {
Packit 34410b
		fprintf(stderr, "Unsupported command specified\n");
Packit 34410b
		return EXIT_FAILURE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	mainloop_init();
Packit 34410b
Packit 34410b
	printf("Bluetooth information utility ver %s\n", VERSION);
Packit 34410b
Packit 34410b
	fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
Packit 34410b
	if (fd < 0) {
Packit 34410b
		perror("Failed to open HCI raw socket");
Packit 34410b
		return EXIT_FAILURE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	memset(&hci_info, 0, sizeof(hci_info));
Packit 34410b
	hci_info.dev_id = index;
Packit 34410b
Packit 34410b
	if (ioctl(fd, HCIGETDEVINFO, (void *) &hci_info) < 0) {
Packit 34410b
		perror("Failed to get HCI device information");
Packit 34410b
		close(fd);
Packit 34410b
		return EXIT_FAILURE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (use_raw && !(hci_info.flags & HCI_UP)) {
Packit 34410b
		printf("Powering on controller\n");
Packit 34410b
Packit 34410b
		if (ioctl(fd, HCIDEVUP, hci_info.dev_id) < 0) {
Packit 34410b
			perror("Failed to power on controller");
Packit 34410b
			close(fd);
Packit 34410b
			return EXIT_FAILURE;
Packit 34410b
		}
Packit 34410b
Packit 34410b
		power_down = true;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	close(fd);
Packit 34410b
Packit 34410b
	hci_type = (hci_info.type & 0x30) >> 4;
Packit 34410b
Packit 34410b
	if (use_raw) {
Packit 34410b
		hci_dev = bt_hci_new_raw_device(index);
Packit 34410b
		if (!hci_dev) {
Packit 34410b
			fprintf(stderr, "Failed to open HCI raw device\n");
Packit 34410b
			return EXIT_FAILURE;
Packit 34410b
		}
Packit 34410b
	} else {
Packit 34410b
		hci_dev = bt_hci_new_user_channel(index);
Packit 34410b
		if (!hci_dev) {
Packit 34410b
			fprintf(stderr, "Failed to open HCI user channel\n");
Packit 34410b
			return EXIT_FAILURE;
Packit 34410b
		}
Packit 34410b
Packit 34410b
		reset_on_init = true;
Packit 34410b
		reset_on_shutdown = true;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (!func(argc - optind - 1, argv + optind + 1)) {
Packit 34410b
		bt_hci_unref(hci_dev);
Packit 34410b
		return EXIT_FAILURE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	exit_status = mainloop_run_with_signal(signal_callback, NULL);
Packit 34410b
Packit 34410b
	bt_hci_unref(hci_dev);
Packit 34410b
Packit 34410b
	if (use_raw && power_down) {
Packit 34410b
		fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
Packit 34410b
		if (fd >= 0) {
Packit 34410b
			printf("Powering down controller\n");
Packit 34410b
Packit 34410b
			if (ioctl(fd, HCIDEVDOWN, hci_info.dev_id) < 0)
Packit 34410b
				perror("Failed to power down controller");
Packit 34410b
Packit 34410b
			close(fd);
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return exit_status;
Packit 34410b
}