2011-10-25 21:36:00 +08:00
|
|
|
/*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-07 18:46:04 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-26 18:01:47 +08:00
|
|
|
#include <stdlib.h>
|
2011-10-25 23:07:08 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2014-02-22 22:36:53 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2011-10-25 23:07:08 +08:00
|
|
|
#include <poll.h>
|
2011-10-26 18:01:47 +08:00
|
|
|
#include <getopt.h>
|
2011-10-27 04:50:47 +08:00
|
|
|
#include <stdbool.h>
|
2011-10-25 23:07:08 +08:00
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/hci.h>
|
2011-11-10 16:45:11 +08:00
|
|
|
#include <bluetooth/hci_lib.h>
|
2012-02-24 03:53:17 +08:00
|
|
|
#include <bluetooth/sdp.h>
|
|
|
|
#include <bluetooth/sdp_lib.h>
|
2011-10-25 23:07:08 +08:00
|
|
|
|
2014-01-26 08:58:24 +08:00
|
|
|
#include "src/uuid-helper.h"
|
|
|
|
#include "lib/mgmt.h"
|
2013-01-10 12:31:07 +08:00
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
#include "monitor/mainloop.h"
|
|
|
|
#include "src/shared/util.h"
|
2013-02-18 17:32:50 +08:00
|
|
|
#include "src/shared/mgmt.h"
|
2012-02-24 03:53:17 +08:00
|
|
|
|
2011-10-27 04:50:47 +08:00
|
|
|
static bool monitor = false;
|
2011-11-01 21:05:03 +08:00
|
|
|
static bool discovery = false;
|
2011-12-19 20:15:53 +08:00
|
|
|
static bool resolve_names = true;
|
2011-10-27 04:50:47 +08:00
|
|
|
|
2013-02-18 20:38:29 +08:00
|
|
|
static int pending = 0;
|
2011-10-26 05:33:11 +08:00
|
|
|
|
2013-02-18 17:38:51 +08:00
|
|
|
static void controller_error(uint16_t index, uint16_t len,
|
|
|
|
const void *param, void *user_data)
|
2011-10-26 05:33:11 +08:00
|
|
|
{
|
2013-02-18 17:38:51 +08:00
|
|
|
const struct mgmt_ev_controller_error *ev = param;
|
|
|
|
|
2011-10-26 05:33:11 +08:00
|
|
|
if (len < sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Too short (%u bytes) controller error event\n", len);
|
2013-02-18 17:38:51 +08:00
|
|
|
return;
|
2011-10-26 05:33:11 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor)
|
|
|
|
printf("hci%u error 0x%02x\n", index, ev->error_code);
|
2011-10-26 05:33:11 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 17:42:41 +08:00
|
|
|
static void index_added(uint16_t index, uint16_t len,
|
|
|
|
const void *param, void *user_data)
|
2011-10-26 05:33:11 +08:00
|
|
|
{
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor)
|
|
|
|
printf("hci%u added\n", index);
|
2011-10-26 05:33:11 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 17:42:41 +08:00
|
|
|
static void index_removed(uint16_t index, uint16_t len,
|
|
|
|
const void *param, void *user_data)
|
2011-10-26 05:33:11 +08:00
|
|
|
{
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor)
|
|
|
|
printf("hci%u removed\n", index);
|
2011-10-26 05:33:11 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 17:20:06 +08:00
|
|
|
static const char *settings_str[] = {
|
|
|
|
"powered",
|
|
|
|
"connectable",
|
|
|
|
"fast-connectable",
|
|
|
|
"discoverable",
|
|
|
|
"pairable",
|
|
|
|
"link-security",
|
|
|
|
"ssp",
|
|
|
|
"br/edr",
|
|
|
|
"hs",
|
2013-10-02 15:41:30 +08:00
|
|
|
"le",
|
|
|
|
"advertising",
|
2014-01-10 11:47:24 +08:00
|
|
|
"secure-conn",
|
2014-02-02 15:53:42 +08:00
|
|
|
"debug-keys",
|
2014-02-22 13:27:09 +08:00
|
|
|
"privacy",
|
2011-12-13 17:20:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void print_settings(uint32_t settings)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < NELEM(settings_str); i++) {
|
|
|
|
if ((settings & (1 << i)) != 0)
|
|
|
|
printf("%s ", settings_str[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 17:53:56 +08:00
|
|
|
static void new_settings(uint16_t index, uint16_t len,
|
|
|
|
const void *param, void *user_data)
|
2011-12-13 17:20:06 +08:00
|
|
|
{
|
2013-02-18 17:53:56 +08:00
|
|
|
const uint32_t *ev = param;
|
|
|
|
|
2011-12-13 17:20:06 +08:00
|
|
|
if (len < sizeof(*ev)) {
|
|
|
|
fprintf(stderr, "Too short new_settings event (%u)\n", len);
|
2013-02-18 17:53:56 +08:00
|
|
|
return;
|
2011-12-13 17:20:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (monitor) {
|
|
|
|
printf("hci%u new_settings: ", index);
|
2014-03-20 22:42:37 +08:00
|
|
|
print_settings(get_le32(ev));
|
2011-12-13 17:20:06 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 17:56:22 +08:00
|
|
|
static void discovering(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-10-26 05:33:11 +08:00
|
|
|
{
|
2013-02-18 17:56:22 +08:00
|
|
|
const struct mgmt_ev_discovering *ev = param;
|
|
|
|
|
2011-10-26 05:33:11 +08:00
|
|
|
if (len < sizeof(*ev)) {
|
2011-12-13 17:20:06 +08:00
|
|
|
fprintf(stderr, "Too short (%u bytes) discovering event\n",
|
|
|
|
len);
|
2013-02-18 17:56:22 +08:00
|
|
|
return;
|
2011-10-26 05:33:11 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 05:00:03 +08:00
|
|
|
if (ev->discovering == 0 && discovery) {
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-02-20 05:00:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-11-01 21:05:03 +08:00
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor)
|
2012-02-21 06:20:27 +08:00
|
|
|
printf("hci%u type %u discovering %s\n", index,
|
|
|
|
ev->type, ev->discovering ? "on" : "off");
|
2011-10-26 05:33:11 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 18:37:45 +08:00
|
|
|
static void new_link_key(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:20:54 +08:00
|
|
|
{
|
2013-02-18 18:37:45 +08:00
|
|
|
const struct mgmt_ev_new_link_key *ev = param;
|
2011-11-01 19:20:54 +08:00
|
|
|
|
|
|
|
if (len != sizeof(*ev)) {
|
2011-11-08 02:09:03 +08:00
|
|
|
fprintf(stderr, "Invalid new_link_key length (%u bytes)\n",
|
2011-11-01 19:20:54 +08:00
|
|
|
len);
|
2013-02-18 18:37:45 +08:00
|
|
|
return;
|
2011-11-01 19:20:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor) {
|
|
|
|
char addr[18];
|
2012-02-17 20:30:31 +08:00
|
|
|
ba2str(&ev->key.addr.bdaddr, addr);
|
2011-11-08 02:09:03 +08:00
|
|
|
printf("hci%u new_link_key %s type 0x%02x pin_len %d "
|
2011-11-01 21:10:32 +08:00
|
|
|
"store_hint %u\n", index, addr, ev->key.type,
|
|
|
|
ev->key.pin_len, ev->store_hint);
|
|
|
|
}
|
2011-11-01 19:20:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 06:21:46 +08:00
|
|
|
static const char *typestr(uint8_t type)
|
2011-11-07 19:55:30 +08:00
|
|
|
{
|
2011-11-11 06:21:46 +08:00
|
|
|
const char *str[] = { "BR/EDR", "LE Public", "LE Random" };
|
|
|
|
|
2012-04-25 09:42:30 +08:00
|
|
|
if (type <= BDADDR_LE_RANDOM)
|
2011-11-11 06:21:46 +08:00
|
|
|
return str[type];
|
|
|
|
|
|
|
|
return "(unknown)";
|
2011-11-07 19:55:30 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 18:41:37 +08:00
|
|
|
static void connected(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:29:13 +08:00
|
|
|
{
|
2013-02-18 18:41:37 +08:00
|
|
|
const struct mgmt_ev_device_connected *ev = param;
|
2012-01-17 23:18:41 +08:00
|
|
|
uint16_t eir_len;
|
|
|
|
|
2012-01-18 03:37:55 +08:00
|
|
|
if (len < sizeof(*ev)) {
|
2012-01-17 23:18:41 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid connected event length (%u bytes)\n", len);
|
2013-02-18 18:41:37 +08:00
|
|
|
return;
|
2012-01-17 23:18:41 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 22:42:36 +08:00
|
|
|
eir_len = get_le16(&ev->eir_len);
|
2012-01-17 23:18:41 +08:00
|
|
|
if (len != sizeof(*ev) + eir_len) {
|
|
|
|
fprintf(stderr, "Invalid connected event length "
|
|
|
|
"(%u bytes, eir_len %u bytes)\n", len, eir_len);
|
2013-02-18 18:41:37 +08:00
|
|
|
return;
|
2012-01-17 23:18:41 +08:00
|
|
|
}
|
2011-11-01 19:29:13 +08:00
|
|
|
|
2012-01-17 23:18:41 +08:00
|
|
|
if (monitor) {
|
|
|
|
char addr[18];
|
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
|
|
|
printf("hci%u %s type %s connected eir_len %u\n", index, addr,
|
|
|
|
typestr(ev->addr.type), eir_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 18:41:37 +08:00
|
|
|
static void disconnected(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2012-01-17 23:18:41 +08:00
|
|
|
{
|
2013-02-18 18:41:37 +08:00
|
|
|
const struct mgmt_ev_device_disconnected *ev = param;
|
|
|
|
|
2012-08-17 17:10:55 +08:00
|
|
|
if (len < sizeof(struct mgmt_addr_info)) {
|
2011-11-01 19:29:13 +08:00
|
|
|
fprintf(stderr,
|
2012-01-17 23:18:41 +08:00
|
|
|
"Invalid disconnected event length (%u bytes)\n", len);
|
2013-02-18 18:41:37 +08:00
|
|
|
return;
|
2011-11-01 19:29:13 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor) {
|
2011-11-11 06:21:46 +08:00
|
|
|
char addr[18];
|
2012-08-17 17:10:55 +08:00
|
|
|
uint8_t reason;
|
|
|
|
|
|
|
|
if (len < sizeof(*ev))
|
|
|
|
reason = MGMT_DEV_DISCONN_UNKNOWN;
|
|
|
|
else
|
|
|
|
reason = ev->reason;
|
|
|
|
|
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
|
|
|
printf("hci%u %s type %s disconnected with reason %u\n",
|
|
|
|
index, addr, typestr(ev->addr.type), reason);
|
2011-11-01 21:10:32 +08:00
|
|
|
}
|
2011-11-01 19:29:13 +08:00
|
|
|
}
|
2011-11-01 19:33:02 +08:00
|
|
|
|
2013-02-18 18:41:37 +08:00
|
|
|
static void conn_failed(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:33:02 +08:00
|
|
|
{
|
2013-02-18 18:41:37 +08:00
|
|
|
const struct mgmt_ev_connect_failed *ev = param;
|
|
|
|
|
2011-11-01 19:33:02 +08:00
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid connect_failed event length (%u bytes)\n", len);
|
2013-02-18 18:41:37 +08:00
|
|
|
return;
|
2011-11-01 19:33:02 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor) {
|
2011-11-11 06:21:46 +08:00
|
|
|
char addr[18];
|
2011-11-07 19:55:30 +08:00
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
2011-11-11 21:54:31 +08:00
|
|
|
printf("hci%u %s type %s connect failed (status 0x%02x, %s)\n",
|
|
|
|
index, addr, typestr(ev->addr.type), ev->status,
|
|
|
|
mgmt_errstr(ev->status));
|
2011-11-01 21:10:32 +08:00
|
|
|
}
|
2011-11-01 19:33:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 18:47:00 +08:00
|
|
|
static void auth_failed(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:34:45 +08:00
|
|
|
{
|
2013-02-18 18:47:00 +08:00
|
|
|
const struct mgmt_ev_auth_failed *ev = param;
|
|
|
|
|
2011-11-01 19:34:45 +08:00
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid auth_failed event length (%u bytes)\n", len);
|
2013-02-18 18:47:00 +08:00
|
|
|
return;
|
2011-11-01 19:34:45 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor) {
|
|
|
|
char addr[18];
|
2012-02-09 19:33:54 +08:00
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
2011-11-11 21:54:31 +08:00
|
|
|
printf("hci%u %s auth failed with status 0x%02x (%s)\n",
|
|
|
|
index, addr, ev->status, mgmt_errstr(ev->status));
|
2011-11-01 21:10:32 +08:00
|
|
|
}
|
2011-11-01 19:34:45 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 18:48:58 +08:00
|
|
|
static void local_name_changed(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:42:13 +08:00
|
|
|
{
|
2013-02-18 18:48:58 +08:00
|
|
|
const struct mgmt_ev_local_name_changed *ev = param;
|
|
|
|
|
2011-11-01 19:42:13 +08:00
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid local_name_changed length (%u bytes)\n", len);
|
2013-02-18 18:48:58 +08:00
|
|
|
return;
|
2011-11-01 19:42:13 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor)
|
|
|
|
printf("hci%u name changed: %s\n", index, ev->name);
|
2011-11-01 19:42:13 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:23:58 +08:00
|
|
|
static void confirm_name_rsp(uint8_t status, uint16_t len,
|
|
|
|
const void *param, void *user_data)
|
2011-12-19 20:15:53 +08:00
|
|
|
{
|
2013-02-18 20:23:58 +08:00
|
|
|
const struct mgmt_rp_confirm_name *rp = param;
|
2011-12-19 20:15:53 +08:00
|
|
|
char addr[18];
|
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (len == 0 && status != 0) {
|
2011-12-19 20:15:53 +08:00
|
|
|
fprintf(stderr,
|
2013-02-18 20:23:58 +08:00
|
|
|
"confirm_name failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2011-12-19 20:15:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(*rp)) {
|
2013-02-18 20:23:58 +08:00
|
|
|
fprintf(stderr, "confirm_name rsp length %u instead of %zu\n",
|
|
|
|
len, sizeof(*rp));
|
2011-12-19 20:15:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:30:31 +08:00
|
|
|
ba2str(&rp->addr.bdaddr, addr);
|
2011-12-19 20:15:53 +08:00
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (status != 0)
|
2013-02-18 20:23:58 +08:00
|
|
|
fprintf(stderr, "confirm_name for %s failed: 0x%02x (%s)\n",
|
|
|
|
addr, status, mgmt_errstr(status));
|
2011-12-19 20:15:53 +08:00
|
|
|
else
|
2013-02-18 20:23:58 +08:00
|
|
|
printf("confirm_name succeeded for %s\n", addr);
|
2011-12-19 20:15:53 +08:00
|
|
|
}
|
|
|
|
|
2014-01-21 19:54:30 +08:00
|
|
|
static char *eir_get_name(const uint8_t *eir, uint16_t eir_len)
|
|
|
|
{
|
|
|
|
uint8_t parsed = 0;
|
|
|
|
|
|
|
|
if (eir_len < 2)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (parsed < eir_len - 1) {
|
|
|
|
uint8_t field_len = eir[0];
|
|
|
|
|
|
|
|
if (field_len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
parsed += field_len + 1;
|
|
|
|
|
|
|
|
if (parsed > eir_len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Check for short of complete name */
|
|
|
|
if (eir[1] == 0x09 || eir[1] == 0x08)
|
|
|
|
return strndup((char *) &eir[2], field_len - 1);
|
|
|
|
|
|
|
|
eir += field_len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:29:45 +08:00
|
|
|
static unsigned int eir_get_flags(const uint8_t *eir, uint16_t eir_len)
|
|
|
|
{
|
|
|
|
uint8_t parsed = 0;
|
|
|
|
|
|
|
|
if (eir_len < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (parsed < eir_len - 1) {
|
|
|
|
uint8_t field_len = eir[0];
|
|
|
|
|
|
|
|
if (field_len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
parsed += field_len + 1;
|
|
|
|
|
|
|
|
if (parsed > eir_len)
|
|
|
|
break;
|
|
|
|
|
2014-04-02 18:29:58 +08:00
|
|
|
/* Check for flags */
|
2014-04-01 21:29:45 +08:00
|
|
|
if (eir[1] == 0x01)
|
|
|
|
return eir[2];
|
|
|
|
|
|
|
|
eir += field_len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-18 18:51:51 +08:00
|
|
|
static void device_found(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:52:14 +08:00
|
|
|
{
|
2013-02-18 18:51:51 +08:00
|
|
|
const struct mgmt_ev_device_found *ev = param;
|
2013-02-18 20:23:58 +08:00
|
|
|
struct mgmt *mgmt = user_data;
|
2012-01-18 19:27:44 +08:00
|
|
|
uint16_t eir_len;
|
2014-01-21 19:54:30 +08:00
|
|
|
uint32_t flags;
|
2012-01-18 19:27:44 +08:00
|
|
|
|
|
|
|
if (len < sizeof(*ev)) {
|
2011-11-01 19:52:14 +08:00
|
|
|
fprintf(stderr,
|
2012-01-18 19:27:44 +08:00
|
|
|
"Too short device_found length (%u bytes)\n", len);
|
2013-02-18 18:51:51 +08:00
|
|
|
return;
|
2012-01-18 19:27:44 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 21:58:01 +08:00
|
|
|
flags = btohl(ev->flags);
|
2012-02-23 06:14:06 +08:00
|
|
|
|
2014-03-20 22:42:36 +08:00
|
|
|
eir_len = get_le16(&ev->eir_len);
|
2012-01-18 19:27:44 +08:00
|
|
|
if (len != sizeof(*ev) + eir_len) {
|
2012-12-14 23:42:27 +08:00
|
|
|
fprintf(stderr, "dev_found: expected %zu bytes, got %u bytes\n",
|
2012-01-18 19:27:44 +08:00
|
|
|
sizeof(*ev) + eir_len, len);
|
2013-02-18 18:51:51 +08:00
|
|
|
return;
|
2011-11-01 19:52:14 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 21:10:32 +08:00
|
|
|
if (monitor || discovery) {
|
2014-01-21 19:54:30 +08:00
|
|
|
char addr[18], *name;
|
|
|
|
|
2011-11-07 19:55:30 +08:00
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
2012-01-18 19:27:44 +08:00
|
|
|
printf("hci%u dev_found: %s type %s rssi %d "
|
2012-10-22 18:01:12 +08:00
|
|
|
"flags 0x%04x ", index, addr,
|
|
|
|
typestr(ev->addr.type), ev->rssi, flags);
|
|
|
|
|
2014-04-01 21:29:45 +08:00
|
|
|
if (ev->addr.type != BDADDR_BREDR)
|
|
|
|
printf("AD flags 0x%02x ",
|
|
|
|
eir_get_flags(ev->eir, eir_len));
|
|
|
|
|
2014-01-21 19:54:30 +08:00
|
|
|
name = eir_get_name(ev->eir, eir_len);
|
|
|
|
if (name)
|
|
|
|
printf("name %s\n", name);
|
2012-10-22 18:01:12 +08:00
|
|
|
else
|
|
|
|
printf("eir_len %u\n", eir_len);
|
2011-11-01 19:52:14 +08:00
|
|
|
|
2014-01-21 19:54:30 +08:00
|
|
|
free(name);
|
|
|
|
}
|
2012-10-22 18:01:12 +08:00
|
|
|
|
2012-02-23 06:14:06 +08:00
|
|
|
if (discovery && (flags & MGMT_DEV_FOUND_CONFIRM_NAME)) {
|
2011-12-19 20:15:53 +08:00
|
|
|
struct mgmt_cp_confirm_name cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2012-02-17 20:30:31 +08:00
|
|
|
memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
|
2011-12-19 20:15:53 +08:00
|
|
|
if (resolve_names)
|
|
|
|
cp.name_known = 0;
|
|
|
|
else
|
|
|
|
cp.name_known = 1;
|
|
|
|
|
2013-02-18 20:23:58 +08:00
|
|
|
mgmt_reply(mgmt, MGMT_OP_CONFIRM_NAME, index, sizeof(cp), &cp,
|
|
|
|
confirm_name_rsp, NULL, NULL);
|
2011-12-19 20:15:53 +08:00
|
|
|
}
|
2011-11-01 19:52:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
static void pin_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 21:22:50 +08:00
|
|
|
{
|
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
2013-02-18 20:28:59 +08:00
|
|
|
"PIN Code reply failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-02-18 18:35:47 +08:00
|
|
|
return;
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
printf("PIN Reply successful\n");
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
static int mgmt_pin_reply(struct mgmt *mgmt, uint16_t index,
|
2013-02-18 18:58:38 +08:00
|
|
|
const struct mgmt_addr_info *addr,
|
|
|
|
const char *pin, size_t len)
|
2011-11-10 21:22:50 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_pin_code_reply cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2012-02-17 20:30:31 +08:00
|
|
|
memcpy(&cp.addr, addr, sizeof(cp.addr));
|
2011-11-10 21:22:50 +08:00
|
|
|
cp.pin_len = len;
|
|
|
|
memcpy(cp.pin_code, pin, len);
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
return mgmt_reply(mgmt, MGMT_OP_PIN_CODE_REPLY, index, sizeof(cp), &cp,
|
|
|
|
pin_rsp, NULL, NULL);
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
static void pin_neg_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 21:22:50 +08:00
|
|
|
{
|
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
2013-02-18 20:28:59 +08:00
|
|
|
"PIN Neg reply failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-02-18 18:35:47 +08:00
|
|
|
return;
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
printf("PIN Negative Reply successful\n");
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
static int mgmt_pin_neg_reply(struct mgmt *mgmt, uint16_t index,
|
2013-02-18 18:58:38 +08:00
|
|
|
const struct mgmt_addr_info *addr)
|
2011-11-10 21:22:50 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_pin_code_neg_reply cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2012-02-17 20:30:31 +08:00
|
|
|
memcpy(&cp.addr, addr, sizeof(cp.addr));
|
2011-11-10 21:22:50 +08:00
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
return mgmt_reply(mgmt, MGMT_OP_PIN_CODE_NEG_REPLY, index,
|
|
|
|
sizeof(cp), &cp, pin_neg_rsp, NULL, NULL);
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 18:58:38 +08:00
|
|
|
static void request_pin(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 21:22:50 +08:00
|
|
|
{
|
2013-02-18 18:58:38 +08:00
|
|
|
const struct mgmt_ev_pin_code_request *ev = param;
|
2013-02-18 20:28:59 +08:00
|
|
|
struct mgmt *mgmt = user_data;
|
2011-11-10 21:22:50 +08:00
|
|
|
char pin[18];
|
|
|
|
size_t pin_len;
|
|
|
|
|
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid pin_code request length (%u bytes)\n", len);
|
2013-02-18 18:58:38 +08:00
|
|
|
return;
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (monitor) {
|
|
|
|
char addr[18];
|
2012-02-17 20:30:31 +08:00
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
2011-11-10 21:22:50 +08:00
|
|
|
printf("hci%u %s request PIN\n", index, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PIN Request (press enter to reject) >> ");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
memset(pin, 0, sizeof(pin));
|
|
|
|
|
2013-02-18 18:58:38 +08:00
|
|
|
if (fgets(pin, sizeof(pin), stdin) == NULL || pin[0] == '\n') {
|
2013-02-18 20:28:59 +08:00
|
|
|
mgmt_pin_neg_reply(mgmt, index, &ev->addr);
|
2013-02-18 18:58:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-11-10 21:22:50 +08:00
|
|
|
|
|
|
|
pin_len = strlen(pin);
|
|
|
|
if (pin[pin_len - 1] == '\n') {
|
|
|
|
pin[pin_len - 1] = '\0';
|
|
|
|
pin_len--;
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:28:59 +08:00
|
|
|
mgmt_pin_reply(mgmt, index, &ev->addr, pin, pin_len);
|
2011-11-10 21:22:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
static void confirm_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 21:45:15 +08:00
|
|
|
{
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr,
|
2013-02-18 20:33:03 +08:00
|
|
|
"User Confirm reply failed. status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-02-18 18:35:47 +08:00
|
|
|
return;
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
printf("User Confirm Reply successful\n");
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
static int mgmt_confirm_reply(struct mgmt *mgmt, uint16_t index,
|
2014-05-28 01:58:28 +08:00
|
|
|
const struct mgmt_addr_info *addr)
|
2011-11-10 21:45:15 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_user_confirm_reply cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2014-05-28 01:58:28 +08:00
|
|
|
memcpy(&cp.addr, addr, sizeof(*addr));
|
2011-11-10 21:45:15 +08:00
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
return mgmt_reply(mgmt, MGMT_OP_USER_CONFIRM_REPLY, index,
|
|
|
|
sizeof(cp), &cp, confirm_rsp, NULL, NULL);
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
static void confirm_neg_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 21:45:15 +08:00
|
|
|
{
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr,
|
2013-02-18 20:33:03 +08:00
|
|
|
"Confirm Neg reply failed. status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-02-18 18:35:47 +08:00
|
|
|
return;
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
printf("User Confirm Negative Reply successful\n");
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
static int mgmt_confirm_neg_reply(struct mgmt *mgmt, uint16_t index,
|
2014-05-28 01:58:28 +08:00
|
|
|
const struct mgmt_addr_info *addr)
|
2011-11-10 21:45:15 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_user_confirm_reply cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2014-05-28 01:58:28 +08:00
|
|
|
memcpy(&cp.addr, addr, sizeof(*addr));
|
2011-11-10 21:45:15 +08:00
|
|
|
|
2013-02-18 20:33:03 +08:00
|
|
|
return mgmt_reply(mgmt, MGMT_OP_USER_CONFIRM_NEG_REPLY, index,
|
|
|
|
sizeof(cp), &cp, confirm_neg_rsp, NULL, NULL);
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 18:58:38 +08:00
|
|
|
static void user_confirm(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 21:45:15 +08:00
|
|
|
{
|
2013-02-18 18:58:38 +08:00
|
|
|
const struct mgmt_ev_user_confirm_request *ev = param;
|
2013-02-18 20:33:03 +08:00
|
|
|
struct mgmt *mgmt = user_data;
|
2011-11-10 21:45:15 +08:00
|
|
|
char rsp[5];
|
|
|
|
size_t rsp_len;
|
|
|
|
uint32_t val;
|
|
|
|
char addr[18];
|
|
|
|
|
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid user_confirm request length (%u)\n", len);
|
2013-02-18 18:58:38 +08:00
|
|
|
return;
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 19:33:54 +08:00
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
2014-03-20 22:42:37 +08:00
|
|
|
val = get_le32(&ev->value);
|
2011-11-10 21:45:15 +08:00
|
|
|
|
|
|
|
if (monitor)
|
|
|
|
printf("hci%u %s User Confirm %06u hint %u\n", index, addr,
|
|
|
|
val, ev->confirm_hint);
|
|
|
|
|
|
|
|
if (ev->confirm_hint)
|
|
|
|
printf("Accept pairing with %s (yes/no) >> ", addr);
|
|
|
|
else
|
|
|
|
printf("Confirm value %06u for %s (yes/no) >> ", val, addr);
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
memset(rsp, 0, sizeof(rsp));
|
|
|
|
|
2013-02-18 18:58:38 +08:00
|
|
|
if (fgets(rsp, sizeof(rsp), stdin) == NULL || rsp[0] == '\n') {
|
2014-05-28 01:58:28 +08:00
|
|
|
mgmt_confirm_neg_reply(mgmt, index, &ev->addr);
|
2013-02-18 18:58:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-11-10 21:45:15 +08:00
|
|
|
|
|
|
|
rsp_len = strlen(rsp);
|
2013-12-16 16:57:38 +08:00
|
|
|
if (rsp[rsp_len - 1] == '\n')
|
2011-11-10 21:45:15 +08:00
|
|
|
rsp[rsp_len - 1] = '\0';
|
|
|
|
|
|
|
|
if (rsp[0] == 'y' || rsp[0] == 'Y')
|
2014-05-28 01:58:28 +08:00
|
|
|
mgmt_confirm_reply(mgmt, index, &ev->addr);
|
2011-11-10 21:45:15 +08:00
|
|
|
else
|
2014-05-28 01:58:28 +08:00
|
|
|
mgmt_confirm_neg_reply(mgmt, index, &ev->addr);
|
2011-11-10 21:45:15 +08:00
|
|
|
}
|
|
|
|
|
2014-06-02 02:48:00 +08:00
|
|
|
static void passkey_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"User Passkey reply failed. status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
mainloop_quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("User Passkey Reply successful\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mgmt_passkey_reply(struct mgmt *mgmt, uint16_t index,
|
|
|
|
const struct mgmt_addr_info *addr,
|
|
|
|
uint32_t passkey)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_user_passkey_reply cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
memcpy(&cp.addr, addr, sizeof(*addr));
|
|
|
|
put_le32(passkey, &cp.passkey);
|
|
|
|
|
|
|
|
return mgmt_reply(mgmt, MGMT_OP_USER_PASSKEY_REPLY, index,
|
|
|
|
sizeof(cp), &cp, passkey_rsp, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void passkey_neg_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Passkey Neg reply failed. status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
mainloop_quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("User Passkey Negative Reply successful\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mgmt_passkey_neg_reply(struct mgmt *mgmt, uint16_t index,
|
|
|
|
const struct mgmt_addr_info *addr)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_user_passkey_reply cp;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
memcpy(&cp.addr, addr, sizeof(*addr));
|
|
|
|
|
|
|
|
return mgmt_reply(mgmt, MGMT_OP_USER_PASSKEY_NEG_REPLY, index,
|
|
|
|
sizeof(cp), &cp, passkey_neg_rsp, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void request_passkey(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const struct mgmt_ev_user_passkey_request *ev = param;
|
|
|
|
struct mgmt *mgmt = user_data;
|
|
|
|
char passkey[7];
|
|
|
|
|
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid passkey request length (%u bytes)\n", len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monitor) {
|
|
|
|
char addr[18];
|
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
|
|
|
printf("hci%u %s request passkey\n", index, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Passkey Request (press enter to reject) >> ");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
memset(passkey, 0, sizeof(passkey));
|
|
|
|
|
|
|
|
if (fgets(passkey, sizeof(passkey), stdin) == NULL ||
|
|
|
|
passkey[0] == '\n') {
|
|
|
|
mgmt_passkey_neg_reply(mgmt, index, &ev->addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(passkey);
|
|
|
|
if (passkey[len - 1] == '\n') {
|
|
|
|
passkey[len - 1] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mgmt_passkey_reply(mgmt, index, &ev->addr, atoi(passkey));
|
|
|
|
}
|
|
|
|
|
2014-06-02 02:51:48 +08:00
|
|
|
static void passkey_notify(uint16_t index, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const struct mgmt_ev_passkey_notify *ev = param;
|
|
|
|
|
|
|
|
if (len != sizeof(*ev)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid passkey request length (%u bytes)\n", len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monitor) {
|
|
|
|
char addr[18];
|
|
|
|
ba2str(&ev->addr.bdaddr, addr);
|
|
|
|
printf("hci%u %s request passkey\n", index, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Passkey Notify: %06u (entered %u)\n", get_le32(&ev->passkey),
|
|
|
|
ev->entered);
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_monitor(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2011-10-26 18:01:47 +08:00
|
|
|
{
|
|
|
|
printf("Monitoring mgmt events...\n");
|
2011-10-27 04:50:47 +08:00
|
|
|
monitor = true;
|
2011-10-26 18:01:47 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void version_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2012-02-20 20:11:06 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_read_version *rp = param;
|
2012-02-20 20:11:06 +08:00
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr, "Reading mgmt version failed with status"
|
|
|
|
" 0x%02x (%s)\n", status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-20 20:11:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Too small version reply (%u bytes)\n", len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-20 20:11:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("MGMT Version %u, revision %u\n", rp->version,
|
2014-03-20 22:42:36 +08:00
|
|
|
get_le16(&rp->revision));
|
2012-02-20 20:11:06 +08:00
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2012-02-20 20:11:06 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_version(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-20 20:11:06 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_READ_VERSION, MGMT_INDEX_NONE,
|
|
|
|
0, NULL, version_rsp, NULL, NULL) == 0) {
|
2012-02-20 20:11:06 +08:00
|
|
|
fprintf(stderr, "Unable to send read_version cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void commands_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2012-02-15 19:52:53 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_read_commands *rp = param;
|
|
|
|
uint16_t num_commands, num_events;
|
|
|
|
const uint16_t *opcode;
|
2012-02-15 19:52:53 +08:00
|
|
|
size_t expected_len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr, "Reading supported commands failed with status"
|
|
|
|
" 0x%02x (%s)\n", status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-15 19:52:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Too small commands reply (%u bytes)\n", len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-15 19:52:53 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 22:42:36 +08:00
|
|
|
num_commands = get_le16(&rp->num_commands);
|
|
|
|
num_events = get_le16(&rp->num_events);
|
2012-02-15 19:52:53 +08:00
|
|
|
|
|
|
|
expected_len = sizeof(*rp) + num_commands * sizeof(uint16_t) +
|
|
|
|
num_events * sizeof(uint16_t);
|
|
|
|
|
|
|
|
if (len < expected_len) {
|
|
|
|
fprintf(stderr, "Too small commands reply (%u != %zu)\n",
|
|
|
|
len, expected_len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-15 19:52:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
opcode = rp->opcodes;
|
|
|
|
|
|
|
|
printf("%u commands:\n", num_commands);
|
|
|
|
for (i = 0; i < num_commands; i++) {
|
2014-03-20 22:42:36 +08:00
|
|
|
uint16_t op = get_le16(opcode++);
|
2012-02-15 19:52:53 +08:00
|
|
|
printf("\t%s (0x%04x)\n", mgmt_opstr(op), op);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%u events:\n", num_events);
|
|
|
|
for (i = 0; i < num_events; i++) {
|
2014-03-20 22:42:36 +08:00
|
|
|
uint16_t ev = get_le16(opcode++);
|
2012-02-15 19:52:53 +08:00
|
|
|
printf("\t%s (0x%04x)\n", mgmt_evstr(ev), ev);
|
|
|
|
}
|
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2012-02-15 19:52:53 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_commands(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-15 19:52:53 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_READ_COMMANDS, MGMT_INDEX_NONE,
|
|
|
|
0, NULL, commands_rsp, NULL, NULL) == 0) {
|
2012-02-15 19:52:53 +08:00
|
|
|
fprintf(stderr, "Unable to send read_commands cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void info_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-10-26 23:41:41 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_read_info *rp = param;
|
2014-01-12 18:23:01 +08:00
|
|
|
uint16_t index = PTR_TO_UINT(user_data);
|
2011-10-26 23:41:41 +08:00
|
|
|
char addr[18];
|
|
|
|
|
2013-02-18 20:38:29 +08:00
|
|
|
pending--;
|
|
|
|
|
2011-10-26 23:41:41 +08:00
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Reading hci%u info failed with status 0x%02x (%s)\n",
|
2014-01-12 18:23:01 +08:00
|
|
|
index, status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-26 23:41:41 +08:00
|
|
|
}
|
|
|
|
|
2011-10-27 22:27:34 +08:00
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Too small info reply (%u bytes)\n", len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 22:27:34 +08:00
|
|
|
}
|
|
|
|
|
2011-10-26 23:41:41 +08:00
|
|
|
ba2str(&rp->bdaddr, addr);
|
2011-12-13 17:20:06 +08:00
|
|
|
printf("hci%u:\taddr %s version %u manufacturer %u"
|
2014-01-12 18:23:01 +08:00
|
|
|
" class 0x%02x%02x%02x\n", index,
|
2014-03-20 22:42:36 +08:00
|
|
|
addr, rp->version, get_le16(&rp->manufacturer),
|
2011-12-13 17:20:06 +08:00
|
|
|
rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
|
|
|
|
|
|
|
|
printf("\tsupported settings: ");
|
2014-03-20 22:42:37 +08:00
|
|
|
print_settings(get_le32(&rp->supported_settings));
|
2011-12-13 17:20:06 +08:00
|
|
|
|
|
|
|
printf("\n\tcurrent settings: ");
|
2014-03-20 22:42:37 +08:00
|
|
|
print_settings(get_le32(&rp->current_settings));
|
2011-12-13 17:20:06 +08:00
|
|
|
|
|
|
|
printf("\n\tname %s\n", rp->name);
|
|
|
|
printf("\tshort name %s\n", rp->short_name);
|
2011-10-27 23:05:56 +08:00
|
|
|
|
2013-02-18 20:38:29 +08:00
|
|
|
if (pending > 0)
|
2013-02-18 18:35:47 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-10-26 23:41:41 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void index_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-10-27 22:21:30 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_read_index_list *rp = param;
|
|
|
|
struct mgmt *mgmt = user_data;
|
2011-10-27 22:21:30 +08:00
|
|
|
uint16_t count;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Reading index list failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 22:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Too small index list reply (%u bytes)\n",
|
|
|
|
len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 22:21:30 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 22:42:36 +08:00
|
|
|
count = get_le16(&rp->num_controllers);
|
2011-10-27 22:21:30 +08:00
|
|
|
|
|
|
|
if (len < sizeof(*rp) + count * sizeof(uint16_t)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Index count (%u) doesn't match reply length (%u)\n",
|
|
|
|
count, len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 22:21:30 +08:00
|
|
|
}
|
|
|
|
|
2011-10-27 23:06:26 +08:00
|
|
|
if (monitor)
|
|
|
|
printf("Index list with %u item%s\n",
|
|
|
|
count, count > 1 ? "s" : "");
|
|
|
|
|
2011-10-28 02:55:57 +08:00
|
|
|
if (count == 0)
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-28 02:55:57 +08:00
|
|
|
|
2011-10-27 23:06:26 +08:00
|
|
|
if (monitor && count > 0)
|
|
|
|
printf("\t");
|
|
|
|
|
2011-10-27 22:21:30 +08:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
uint16_t index;
|
2013-02-18 20:19:54 +08:00
|
|
|
void *data;
|
2011-10-27 22:21:30 +08:00
|
|
|
|
2014-03-20 22:42:36 +08:00
|
|
|
index = get_le16(&rp->index[i]);
|
2011-10-27 22:21:30 +08:00
|
|
|
|
2011-10-27 23:06:26 +08:00
|
|
|
if (monitor)
|
|
|
|
printf("hci%u ", index);
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
data = UINT_TO_PTR(index);
|
2013-02-18 20:19:54 +08:00
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
|
|
|
|
info_rsp, data, NULL) == 0) {
|
2011-10-27 22:30:16 +08:00
|
|
|
fprintf(stderr, "Unable to send read_info cmd\n");
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 22:21:30 +08:00
|
|
|
}
|
2013-02-18 20:38:29 +08:00
|
|
|
|
|
|
|
pending++;
|
2011-10-27 22:21:30 +08:00
|
|
|
}
|
2011-10-27 23:06:26 +08:00
|
|
|
|
|
|
|
if (monitor && count > 0)
|
|
|
|
printf("\n");
|
2013-02-18 18:35:47 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-10-27 22:21:30 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_info(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-10-26 23:41:41 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
void *data;
|
|
|
|
|
2011-10-28 02:56:42 +08:00
|
|
|
if (index == MGMT_INDEX_NONE) {
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_READ_INDEX_LIST,
|
|
|
|
MGMT_INDEX_NONE, 0, NULL,
|
|
|
|
index_rsp, mgmt, NULL) == 0) {
|
2011-10-27 22:30:16 +08:00
|
|
|
fprintf(stderr, "Unable to send index_list cmd\n");
|
2011-10-27 22:21:30 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2011-10-26 23:41:41 +08:00
|
|
|
}
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
data = UINT_TO_PTR(index);
|
2013-02-18 20:19:54 +08:00
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL, info_rsp,
|
|
|
|
data, NULL) == 0) {
|
2011-10-27 22:30:16 +08:00
|
|
|
fprintf(stderr, "Unable to send read_info cmd\n");
|
2011-10-26 23:41:41 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
/* Wrapper to get the index and opcode to the response callback */
|
|
|
|
struct command_data {
|
|
|
|
uint16_t id;
|
|
|
|
uint16_t op;
|
|
|
|
void (*callback) (uint16_t id, uint16_t op, uint8_t status,
|
|
|
|
uint16_t len, const void *param);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void cmd_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct command_data *data = user_data;
|
|
|
|
|
|
|
|
data->callback(data->op, data->id, status, len, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int send_cmd(struct mgmt *mgmt, uint16_t op, uint16_t id,
|
|
|
|
uint16_t len, const void *param,
|
|
|
|
void (*cb)(uint16_t id, uint16_t op,
|
|
|
|
uint8_t status, uint16_t len,
|
|
|
|
const void *param))
|
2011-10-27 16:43:06 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
struct command_data *data;
|
|
|
|
unsigned int send_id;
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
data = new0(struct command_data, 1);
|
|
|
|
if (!data)
|
|
|
|
return 0;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
data->id = id;
|
|
|
|
data->op = op;
|
|
|
|
data->callback = cb;
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
send_id = mgmt_send(mgmt, op, id, len, param, cmd_rsp, data, free);
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_id == 0)
|
2014-01-12 18:23:01 +08:00
|
|
|
free(data);
|
2013-02-18 20:19:54 +08:00
|
|
|
|
2013-02-21 00:26:34 +08:00
|
|
|
return send_id;
|
2013-02-18 20:19:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setting_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
|
|
|
|
const void *param)
|
|
|
|
{
|
|
|
|
const uint32_t *rp = param;
|
2011-10-27 16:43:06 +08:00
|
|
|
|
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s for hci%u failed with status 0x%02x (%s)\n",
|
|
|
|
mgmt_opstr(op), id, status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 16:43:06 +08:00
|
|
|
}
|
|
|
|
|
2011-10-27 22:27:34 +08:00
|
|
|
if (len < sizeof(*rp)) {
|
2011-11-01 16:45:00 +08:00
|
|
|
fprintf(stderr, "Too small %s response (%u bytes)\n",
|
|
|
|
mgmt_opstr(op), len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-10-27 22:27:34 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 17:20:06 +08:00
|
|
|
printf("hci%u %s complete, settings: ", id, mgmt_opstr(op));
|
2014-03-20 22:42:37 +08:00
|
|
|
print_settings(get_le32(rp));
|
2011-12-13 17:20:06 +08:00
|
|
|
printf("\n");
|
2011-10-27 16:43:06 +08:00
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-10-27 16:43:06 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_setting(struct mgmt *mgmt, uint16_t index, uint16_t op,
|
2011-11-01 16:49:20 +08:00
|
|
|
int argc, char **argv)
|
2011-10-27 16:43:06 +08:00
|
|
|
{
|
2011-11-01 16:49:20 +08:00
|
|
|
uint8_t val;
|
2011-10-27 16:43:06 +08:00
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("Specify \"on\" or \"off\"\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:01:20 +08:00
|
|
|
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
|
2011-11-01 16:49:20 +08:00
|
|
|
val = 1;
|
2011-10-27 16:43:06 +08:00
|
|
|
else if (strcasecmp(argv[1], "off") == 0)
|
2011-11-01 16:49:20 +08:00
|
|
|
val = 0;
|
2011-10-27 16:43:06 +08:00
|
|
|
else
|
2011-11-01 16:49:20 +08:00
|
|
|
val = atoi(argv[1]);
|
2011-10-27 16:43:06 +08:00
|
|
|
|
2011-10-28 02:56:42 +08:00
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, op, index, sizeof(val), &val, setting_rsp) == 0) {
|
2011-11-01 16:49:20 +08:00
|
|
|
fprintf(stderr, "Unable to send %s cmd\n", mgmt_opstr(op));
|
2011-10-27 16:43:06 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_power(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-10-28 03:06:04 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_POWERED, argc, argv);
|
2011-11-01 16:49:20 +08:00
|
|
|
}
|
2011-10-28 03:06:04 +08:00
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_discov(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2011-11-01 16:49:20 +08:00
|
|
|
{
|
2011-11-03 14:17:23 +08:00
|
|
|
struct mgmt_cp_set_discoverable cp;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
2013-10-16 05:30:02 +08:00
|
|
|
printf("Usage: btmgmt %s <yes/no/limited> [timeout]\n", argv[0]);
|
2011-11-03 14:17:23 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
|
2011-11-03 20:01:20 +08:00
|
|
|
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
|
2011-11-03 14:17:23 +08:00
|
|
|
cp.val = 1;
|
|
|
|
else if (strcasecmp(argv[1], "off") == 0)
|
|
|
|
cp.val = 0;
|
2013-10-16 05:30:02 +08:00
|
|
|
else if (strcasecmp(argv[1], "limited") == 0)
|
|
|
|
cp.val = 2;
|
2011-11-03 14:17:23 +08:00
|
|
|
else
|
|
|
|
cp.val = atoi(argv[1]);
|
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
cp.timeout = htobs(atoi(argv[2]));
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, MGMT_OP_SET_DISCOVERABLE, index, sizeof(cp), &cp,
|
|
|
|
setting_rsp) == 0) {
|
2011-11-03 14:17:23 +08:00
|
|
|
fprintf(stderr, "Unable to send set_discoverable cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2011-10-28 03:06:04 +08:00
|
|
|
}
|
2011-11-01 16:49:20 +08:00
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_connectable(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2011-11-01 16:52:12 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_CONNECTABLE, argc, argv);
|
2011-11-01 16:52:12 +08:00
|
|
|
}
|
|
|
|
|
2013-03-04 16:36:39 +08:00
|
|
|
static void cmd_fast_conn(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_FAST_CONNECTABLE, argc, argv);
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_pairable(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2011-11-01 16:53:16 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_PAIRABLE, argc, argv);
|
2011-11-01 16:53:16 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_linksec(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-17 05:55:33 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_LINK_SECURITY, argc, argv);
|
2012-02-17 05:55:33 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_ssp(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2012-02-17 07:02:39 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_SSP, argc, argv);
|
2012-02-17 07:02:39 +08:00
|
|
|
}
|
|
|
|
|
2014-01-10 11:47:24 +08:00
|
|
|
static void cmd_sc(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
|
|
|
{
|
2014-01-12 08:02:48 +08:00
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("Specify \"on\" or \"off\" or \"only\"\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
|
|
|
|
val = 1;
|
|
|
|
else if (strcasecmp(argv[1], "off") == 0)
|
|
|
|
val = 0;
|
|
|
|
else if (strcasecmp(argv[1], "only") == 0)
|
|
|
|
val = 2;
|
|
|
|
else
|
|
|
|
val = atoi(argv[1]);
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
if (send_cmd(mgmt, MGMT_OP_SET_SECURE_CONN, index,
|
|
|
|
sizeof(val), &val, setting_rsp) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send set_secure_conn cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-01-10 11:47:24 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_hs(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2012-02-21 06:17:58 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_HS, argc, argv);
|
2012-02-21 06:17:58 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_le(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2012-02-22 22:43:43 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_LE, argc, argv);
|
2012-02-22 22:43:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 20:24:34 +08:00
|
|
|
static void cmd_advertising(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_ADVERTISING, argc, argv);
|
|
|
|
}
|
|
|
|
|
2013-10-02 15:41:48 +08:00
|
|
|
static void cmd_bredr(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
|
|
|
{
|
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_BREDR, argc, argv);
|
|
|
|
}
|
|
|
|
|
2014-02-22 22:36:53 +08:00
|
|
|
static void cmd_privacy(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_set_privacy cp;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("Specify \"on\" or \"off\"\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
|
|
|
|
cp.privacy = 0x01;
|
|
|
|
else if (strcasecmp(argv[1], "off") == 0)
|
|
|
|
cp.privacy = 0x00;
|
|
|
|
else
|
|
|
|
cp.privacy = atoi(argv[1]);
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "open(/dev/urandom): %s\n", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read(fd, cp.irk, sizeof(cp.irk)) != sizeof(cp.irk)) {
|
|
|
|
fprintf(stderr, "Reading from urandom failed\n");
|
|
|
|
close(fd);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (send_cmd(mgmt, MGMT_OP_SET_PRIVACY, index, sizeof(cp), &cp,
|
|
|
|
setting_rsp) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send Set Privacy command\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void class_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
|
|
|
|
const void *param)
|
2011-11-01 18:29:05 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_ev_class_of_dev_changed *rp = param;
|
2012-02-24 03:53:17 +08:00
|
|
|
|
|
|
|
if (len == 0 && status != 0) {
|
|
|
|
fprintf(stderr, "%s failed, status 0x%02x (%s)\n",
|
|
|
|
mgmt_opstr(op), status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-01 18:29:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-24 03:53:17 +08:00
|
|
|
if (len != sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Unexpected %s len %u\n", mgmt_opstr(op), len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-24 03:53:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s succeeded. Class 0x%02x%02x%02x\n", mgmt_opstr(op),
|
|
|
|
rp->class_of_dev[2], rp->class_of_dev[1], rp->class_of_dev[0]);
|
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-01 18:29:05 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_class(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-11-01 18:29:05 +08:00
|
|
|
{
|
|
|
|
uint8_t class[2];
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("Usage: btmgmt %s <major> <minor>\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
class[0] = atoi(argv[1]);
|
|
|
|
class[1] = atoi(argv[2]);
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, MGMT_OP_SET_DEV_CLASS, index, sizeof(class), class,
|
|
|
|
class_rsp) == 0) {
|
2011-11-01 18:29:05 +08:00
|
|
|
fprintf(stderr, "Unable to send set_dev_class cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void disconnect_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 18:53:25 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_disconnect *rp = param;
|
2011-11-01 18:53:25 +08:00
|
|
|
char addr[18];
|
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (len == 0 && status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr, "Disconnect failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 20:17:24 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 18:53:25 +08:00
|
|
|
if (len != sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Invalid disconnect response length (%u)\n",
|
|
|
|
len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-01 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 19:33:54 +08:00
|
|
|
ba2str(&rp->addr.bdaddr, addr);
|
2011-11-01 18:53:25 +08:00
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
if (status == 0)
|
2011-11-01 18:53:25 +08:00
|
|
|
printf("%s disconnected\n", addr);
|
2013-02-18 18:35:47 +08:00
|
|
|
else
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Disconnecting %s failed with status 0x%02x (%s)\n",
|
2012-02-18 20:44:08 +08:00
|
|
|
addr, status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
|
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-01 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:29:57 +08:00
|
|
|
static void disconnect_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt disconnect [-t type] <remote address>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option disconnect_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "type", 1, 0, 't' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_disconnect(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2011-11-01 18:53:25 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_disconnect cp;
|
2014-04-02 18:29:57 +08:00
|
|
|
uint8_t type = BDADDR_BREDR;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "+t:h", disconnect_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
disconnect_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
2011-11-01 18:53:25 +08:00
|
|
|
|
2014-04-02 18:29:57 +08:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
disconnect_usage();
|
2011-11-01 18:53:25 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
2014-04-02 18:29:57 +08:00
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
str2ba(argv[0], &cp.addr.bdaddr);
|
|
|
|
cp.addr.type = type;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_DISCONNECT, index, sizeof(cp), &cp,
|
|
|
|
disconnect_rsp, NULL, NULL) == 0) {
|
2011-11-01 18:53:25 +08:00
|
|
|
fprintf(stderr, "Unable to send disconnect cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void con_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 19:06:12 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_get_connections *rp = param;
|
2011-11-01 19:06:12 +08:00
|
|
|
uint16_t count, i;
|
|
|
|
|
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Too small (%u bytes) get_connections rsp\n",
|
|
|
|
len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-01 19:06:12 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 22:42:36 +08:00
|
|
|
count = get_le16(&rp->conn_count);
|
2011-11-07 19:55:30 +08:00
|
|
|
if (len != sizeof(*rp) + count * sizeof(struct mgmt_addr_info)) {
|
2011-11-01 19:06:12 +08:00
|
|
|
fprintf(stderr, "Invalid get_connections length "
|
|
|
|
" (count=%u, len=%u)\n", count, len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-01 19:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2011-11-11 06:21:46 +08:00
|
|
|
char addr[18];
|
2011-11-07 19:55:30 +08:00
|
|
|
|
|
|
|
ba2str(&rp->addr[i].bdaddr, addr);
|
2011-11-01 19:06:12 +08:00
|
|
|
|
2011-11-11 06:21:46 +08:00
|
|
|
printf("%s type %s\n", addr, typestr(rp->addr[i].type));
|
2011-11-01 19:06:12 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-01 19:06:12 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_con(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-11-01 19:06:12 +08:00
|
|
|
{
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_GET_CONNECTIONS, index, 0, NULL,
|
|
|
|
con_rsp, NULL, NULL) == 0) {
|
2011-11-01 19:06:12 +08:00
|
|
|
fprintf(stderr, "Unable to send get_connections cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void find_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-01 21:05:03 +08:00
|
|
|
{
|
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Unable to start discovery. status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-02-18 18:35:47 +08:00
|
|
|
return;
|
2011-11-01 21:05:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Discovery started\n");
|
|
|
|
discovery = true;
|
|
|
|
}
|
|
|
|
|
2012-02-04 06:28:08 +08:00
|
|
|
static void find_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt find [-l|-b]>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option find_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "le-only", 1, 0, 'l' },
|
|
|
|
{ "bredr-only", 1, 0, 'b' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_find(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-11-01 21:05:03 +08:00
|
|
|
{
|
2011-11-10 16:52:42 +08:00
|
|
|
struct mgmt_cp_start_discovery cp;
|
2011-11-10 23:25:15 +08:00
|
|
|
uint8_t type;
|
2012-02-04 06:28:08 +08:00
|
|
|
int opt;
|
2011-11-10 16:45:11 +08:00
|
|
|
|
2011-11-01 21:05:03 +08:00
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
2011-11-10 23:25:15 +08:00
|
|
|
type = 0;
|
2012-04-25 09:42:30 +08:00
|
|
|
hci_set_bit(BDADDR_BREDR, &type);
|
|
|
|
hci_set_bit(BDADDR_LE_PUBLIC, &type);
|
|
|
|
hci_set_bit(BDADDR_LE_RANDOM, &type);
|
2011-11-10 23:25:15 +08:00
|
|
|
|
2012-02-04 06:28:08 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, "+lbh", find_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'l':
|
2012-04-25 09:42:30 +08:00
|
|
|
hci_clear_bit(BDADDR_BREDR, &type);
|
|
|
|
hci_set_bit(BDADDR_LE_PUBLIC, &type);
|
|
|
|
hci_set_bit(BDADDR_LE_RANDOM, &type);
|
2012-02-04 06:28:08 +08:00
|
|
|
break;
|
|
|
|
case 'b':
|
2012-04-25 09:42:30 +08:00
|
|
|
hci_set_bit(BDADDR_BREDR, &type);
|
|
|
|
hci_clear_bit(BDADDR_LE_PUBLIC, &type);
|
|
|
|
hci_clear_bit(BDADDR_LE_RANDOM, &type);
|
2012-02-04 06:28:08 +08:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
find_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
2011-11-10 16:52:42 +08:00
|
|
|
memset(&cp, 0, sizeof(cp));
|
2011-11-10 23:25:15 +08:00
|
|
|
cp.type = type;
|
2011-11-10 16:45:11 +08:00
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_START_DISCOVERY, index, sizeof(cp), &cp,
|
|
|
|
find_rsp, NULL, NULL) == 0) {
|
2011-11-10 16:45:11 +08:00
|
|
|
fprintf(stderr, "Unable to send start_discovery cmd\n");
|
2011-11-01 21:05:03 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void name_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-02 15:02:19 +08:00
|
|
|
{
|
2013-02-18 18:35:47 +08:00
|
|
|
if (status != 0)
|
2012-12-14 23:42:27 +08:00
|
|
|
fprintf(stderr, "Unable to set local name "
|
|
|
|
"with status 0x%02x (%s)\n",
|
2011-11-11 21:54:31 +08:00
|
|
|
status, mgmt_errstr(status));
|
2011-11-02 15:02:19 +08:00
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-02 15:02:19 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_name(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-11-02 15:02:19 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_set_local_name cp;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
2012-02-22 02:08:19 +08:00
|
|
|
printf("Usage: btmgmt %s <name> [shortname]\n", argv[0]);
|
2011-11-02 15:02:19 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
strncpy((char *) cp.name, argv[1], HCI_MAX_NAME_LENGTH);
|
2012-02-22 02:08:19 +08:00
|
|
|
if (argc > 2)
|
|
|
|
strncpy((char *) cp.short_name, argv[2],
|
|
|
|
MGMT_MAX_SHORT_NAME_LENGTH);
|
2011-11-02 15:02:19 +08:00
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_SET_LOCAL_NAME, index, sizeof(cp), &cp,
|
|
|
|
name_rsp, NULL, NULL) == 0) {
|
2011-11-02 15:02:19 +08:00
|
|
|
fprintf(stderr, "Unable to send set_name cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void pair_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 19:11:36 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_pair_device *rp = param;
|
2011-11-10 19:11:36 +08:00
|
|
|
char addr[18];
|
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (len == 0 && status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr, "Pairing failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 19:11:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Unexpected pair_rsp len %u\n", len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 19:11:36 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 05:22:32 +08:00
|
|
|
ba2str(&rp->addr.bdaddr, addr);
|
2011-11-10 19:11:36 +08:00
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Pairing with %s (%s) failed. status 0x%02x (%s)\n",
|
2012-02-18 20:44:08 +08:00
|
|
|
addr, typestr(rp->addr.type), status,
|
|
|
|
mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 19:11:36 +08:00
|
|
|
}
|
|
|
|
|
2014-02-18 19:54:28 +08:00
|
|
|
printf("Paired with %s (%s)\n", addr, typestr(rp->addr.type));
|
2011-11-10 19:11:36 +08:00
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-10 19:11:36 +08:00
|
|
|
}
|
|
|
|
|
2011-11-10 21:52:27 +08:00
|
|
|
static void pair_usage(void)
|
|
|
|
{
|
2011-11-11 05:22:32 +08:00
|
|
|
printf("Usage: btmgmt pair [-c cap] [-t type] <remote address>\n");
|
2011-11-10 21:52:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct option pair_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "capability", 1, 0, 'c' },
|
2011-11-11 05:22:32 +08:00
|
|
|
{ "type", 1, 0, 't' },
|
2011-11-10 21:52:27 +08:00
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_pair(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-11-10 19:11:36 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_pair_device cp;
|
2011-11-10 21:52:27 +08:00
|
|
|
uint8_t cap = 0x01;
|
2012-04-25 09:42:30 +08:00
|
|
|
uint8_t type = BDADDR_BREDR;
|
2014-02-18 19:54:28 +08:00
|
|
|
char addr[18];
|
2011-11-10 21:52:27 +08:00
|
|
|
int opt;
|
|
|
|
|
2011-11-11 05:22:32 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, "+c:t:h", pair_options,
|
2011-11-10 21:52:27 +08:00
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'c':
|
|
|
|
cap = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
2011-11-11 05:22:32 +08:00
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
2011-11-10 21:52:27 +08:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
pair_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
2011-11-10 19:11:36 +08:00
|
|
|
|
2011-11-10 21:52:27 +08:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
pair_usage();
|
2011-11-10 19:11:36 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2011-11-11 05:22:32 +08:00
|
|
|
str2ba(argv[0], &cp.addr.bdaddr);
|
|
|
|
cp.addr.type = type;
|
2011-11-10 21:52:27 +08:00
|
|
|
cp.io_cap = cap;
|
2011-11-10 19:11:36 +08:00
|
|
|
|
2014-02-18 19:54:28 +08:00
|
|
|
ba2str(&cp.addr.bdaddr, addr);
|
|
|
|
printf("Pairing with %s (%s)\n", addr, typestr(cp.addr.type));
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_PAIR_DEVICE, index, sizeof(cp), &cp,
|
|
|
|
pair_rsp, NULL, NULL) == 0) {
|
2011-11-10 19:11:36 +08:00
|
|
|
fprintf(stderr, "Unable to send pair_device cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cancel_pair_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2013-01-22 20:50:02 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_addr_info *rp = param;
|
2013-01-22 20:50:02 +08:00
|
|
|
char addr[18];
|
|
|
|
|
|
|
|
if (len == 0 && status != 0) {
|
|
|
|
fprintf(stderr, "Cancel Pairing failed with 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2013-01-22 20:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Unexpected cancel_pair_rsp len %u\n", len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2013-01-22 20:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ba2str(&rp->bdaddr, addr);
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Cancel Pairing with %s (%s) failed. 0x%02x (%s)\n",
|
|
|
|
addr, typestr(rp->type), status,
|
|
|
|
mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2013-01-22 20:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Pairing Cancelled with %s\n", addr);
|
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-01-22 20:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cancel_pair_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt cancelpair [-t type] <remote address>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option cancel_pair_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "type", 1, 0, 't' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_cancel_pair(struct mgmt *mgmt, uint16_t index, int argc,
|
2013-01-22 20:50:02 +08:00
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_addr_info cp;
|
|
|
|
uint8_t type = BDADDR_BREDR;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "+t:h", cancel_pair_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
cancel_pair_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
cancel_pair_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
str2ba(argv[0], &cp.bdaddr);
|
|
|
|
cp.type = type;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_CANCEL_PAIR_DEVICE, index, sizeof(cp), &cp,
|
|
|
|
cancel_pair_rsp, NULL, NULL) == 0) {
|
2013-01-22 20:50:02 +08:00
|
|
|
fprintf(stderr, "Unable to send cancel_pair_device cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void unpair_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-10 20:04:52 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_rp_unpair_device *rp = param;
|
2011-11-10 20:04:52 +08:00
|
|
|
char addr[18];
|
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (len == 0 && status != 0) {
|
2012-02-09 19:33:54 +08:00
|
|
|
fprintf(stderr, "Unpair device failed. status 0x%02x (%s)\n",
|
2011-11-11 21:54:31 +08:00
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 20:04:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(*rp)) {
|
2012-02-09 19:33:54 +08:00
|
|
|
fprintf(stderr, "Unexpected unpair_device_rsp len %u\n", len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 20:04:52 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 19:33:54 +08:00
|
|
|
ba2str(&rp->addr.bdaddr, addr);
|
2011-11-10 20:04:52 +08:00
|
|
|
|
2012-02-18 20:44:08 +08:00
|
|
|
if (status != 0) {
|
2011-11-11 21:54:31 +08:00
|
|
|
fprintf(stderr,
|
2012-02-09 19:33:54 +08:00
|
|
|
"Unpairing %s failed. status 0x%02x (%s)\n",
|
2012-02-18 20:44:08 +08:00
|
|
|
addr, status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2011-11-10 20:04:52 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 19:33:54 +08:00
|
|
|
printf("%s unpaired\n", addr);
|
2011-11-10 20:04:52 +08:00
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-10 20:04:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-18 20:16:19 +08:00
|
|
|
static void unpair_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt unpair [-t type] <remote address>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option unpair_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "type", 1, 0, 't' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_unpair(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2011-11-10 20:04:52 +08:00
|
|
|
{
|
2012-02-09 19:33:54 +08:00
|
|
|
struct mgmt_cp_unpair_device cp;
|
2014-02-18 20:16:19 +08:00
|
|
|
uint8_t type = BDADDR_BREDR;
|
|
|
|
int opt;
|
2011-11-10 20:04:52 +08:00
|
|
|
|
2014-02-18 20:16:19 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, "+t:h", unpair_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
unpair_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
unpair_usage();
|
2011-11-10 20:04:52 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2014-02-18 20:16:19 +08:00
|
|
|
str2ba(argv[0], &cp.addr.bdaddr);
|
|
|
|
cp.addr.type = type;
|
2011-11-10 20:04:52 +08:00
|
|
|
cp.disconnect = 1;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_UNPAIR_DEVICE, index, sizeof(cp), &cp,
|
|
|
|
unpair_rsp, NULL, NULL) == 0) {
|
2012-02-09 19:33:54 +08:00
|
|
|
fprintf(stderr, "Unable to send unpair_device cmd\n");
|
2011-11-10 20:04:52 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void keys_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2011-11-11 22:14:35 +08:00
|
|
|
{
|
2013-02-18 18:35:47 +08:00
|
|
|
if (status != 0)
|
2011-11-11 22:14:35 +08:00
|
|
|
fprintf(stderr, "Load keys failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
else
|
|
|
|
printf("Keys successfully loaded\n");
|
2011-11-11 22:14:35 +08:00
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2011-11-11 22:14:35 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_keys(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2011-11-11 22:14:35 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_load_link_keys cp;
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_LOAD_LINK_KEYS, index, sizeof(cp), &cp,
|
|
|
|
keys_rsp, NULL, NULL) == 0) {
|
2011-11-11 22:14:35 +08:00
|
|
|
fprintf(stderr, "Unable to send load_keys cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 07:42:45 +08:00
|
|
|
static void ltks_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (status != 0)
|
|
|
|
fprintf(stderr, "Load keys failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
else
|
|
|
|
printf("Long term keys successfully loaded\n");
|
|
|
|
|
|
|
|
mainloop_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_ltks(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_load_long_term_keys cp;
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_LOAD_LONG_TERM_KEYS, index, sizeof(cp), &cp,
|
|
|
|
ltks_rsp, NULL, NULL) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send load_ltks cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 16:15:16 +08:00
|
|
|
|
|
|
|
static void irks_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (status != 0)
|
|
|
|
fprintf(stderr, "Load IRKs failed with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
else
|
|
|
|
printf("Identity Resolving Keys successfully loaded\n");
|
|
|
|
|
|
|
|
mainloop_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_irks(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_load_irks cp;
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_LOAD_IRKS, index, sizeof(cp), &cp,
|
|
|
|
irks_rsp, NULL, NULL) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send load_irks cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void block_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
|
|
|
|
const void *param)
|
2012-02-19 19:53:59 +08:00
|
|
|
{
|
2013-02-18 20:19:54 +08:00
|
|
|
const struct mgmt_addr_info *rp = param;
|
2012-02-19 19:53:59 +08:00
|
|
|
char addr[18];
|
|
|
|
|
|
|
|
if (len == 0 && status != 0) {
|
|
|
|
fprintf(stderr, "%s failed, status 0x%02x (%s)\n",
|
|
|
|
mgmt_opstr(op), status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-19 19:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Unexpected %s len %u\n", mgmt_opstr(op), len);
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-19 19:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ba2str(&rp->bdaddr, addr);
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr, "%s %s (%s) failed. status 0x%02x (%s)\n",
|
|
|
|
mgmt_opstr(op), addr, typestr(rp->type),
|
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
goto done;
|
2012-02-19 19:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s %s succeeded\n", mgmt_opstr(op), addr);
|
|
|
|
|
2013-02-18 18:35:47 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2012-02-19 19:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void block_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt block [-t type] <remote address>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option block_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "type", 1, 0, 't' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_block(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2012-02-19 19:53:59 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_block_device cp;
|
2012-04-25 09:42:30 +08:00
|
|
|
uint8_t type = BDADDR_BREDR;
|
2012-02-19 19:53:59 +08:00
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "+t:h", block_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
block_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
block_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
str2ba(argv[0], &cp.addr.bdaddr);
|
|
|
|
cp.addr.type = type;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, MGMT_OP_BLOCK_DEVICE, index, sizeof(cp), &cp,
|
|
|
|
block_rsp) == 0) {
|
2012-02-19 19:53:59 +08:00
|
|
|
fprintf(stderr, "Unable to send block_device cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unblock_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt unblock [-t type] <remote address>\n");
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_unblock(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-19 19:53:59 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_unblock_device cp;
|
2012-04-25 09:42:30 +08:00
|
|
|
uint8_t type = BDADDR_BREDR;
|
2012-02-19 19:53:59 +08:00
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "+t:h", block_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
unblock_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
unblock_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
str2ba(argv[0], &cp.addr.bdaddr);
|
|
|
|
cp.addr.type = type;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, MGMT_OP_UNBLOCK_DEVICE, index, sizeof(cp), &cp,
|
|
|
|
block_rsp) == 0) {
|
2012-02-19 19:53:59 +08:00
|
|
|
fprintf(stderr, "Unable to send unblock_device cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 03:53:17 +08:00
|
|
|
static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
|
|
|
|
{
|
|
|
|
if (uuid->type == SDP_UUID16)
|
|
|
|
sdp_uuid16_to_uuid128(uuid128, uuid);
|
|
|
|
else if (uuid->type == SDP_UUID32)
|
|
|
|
sdp_uuid32_to_uuid128(uuid128, uuid);
|
|
|
|
else
|
|
|
|
memcpy(uuid128, uuid, sizeof(*uuid));
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_add_uuid(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-24 03:53:17 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_add_uuid cp;
|
|
|
|
uint128_t uint128;
|
|
|
|
uuid_t uuid, uuid128;
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("UUID and service hint needed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
if (bt_string2uuid(&uuid, argv[1]) < 0) {
|
|
|
|
printf("Invalid UUID: %s\n", argv[1]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
|
|
|
|
uuid_to_uuid128(&uuid128, &uuid);
|
|
|
|
ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
|
|
|
|
htob128(&uint128, (uint128_t *) cp.uuid);
|
|
|
|
|
|
|
|
cp.svc_hint = atoi(argv[2]);
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, MGMT_OP_ADD_UUID, index, sizeof(cp), &cp,
|
|
|
|
class_rsp) == 0) {
|
2012-02-24 03:53:17 +08:00
|
|
|
fprintf(stderr, "Unable to send add_uuid cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_remove_uuid(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-24 03:53:17 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_remove_uuid cp;
|
|
|
|
uint128_t uint128;
|
|
|
|
uuid_t uuid, uuid128;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("UUID needed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
if (bt_string2uuid(&uuid, argv[1]) < 0) {
|
|
|
|
printf("Invalid UUID: %s\n", argv[1]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
|
|
|
|
uuid_to_uuid128(&uuid128, &uuid);
|
|
|
|
ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
|
|
|
|
htob128(&uint128, (uint128_t *) cp.uuid);
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (send_cmd(mgmt, MGMT_OP_REMOVE_UUID, index, sizeof(cp), &cp,
|
|
|
|
class_rsp) == 0) {
|
2012-02-24 03:53:17 +08:00
|
|
|
fprintf(stderr, "Unable to send remove_uuid cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_clr_uuids(struct mgmt *mgmt, uint16_t index, int argc,
|
|
|
|
char **argv)
|
2012-02-24 03:53:17 +08:00
|
|
|
{
|
|
|
|
char *uuid_any = "00000000-0000-0000-0000-000000000000";
|
|
|
|
char *rm_argv[] = { "rm-uuid", uuid_any, NULL };
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
cmd_remove_uuid(mgmt, index, 2, rm_argv);
|
2012-02-24 03:53:17 +08:00
|
|
|
}
|
|
|
|
|
2014-01-10 11:39:59 +08:00
|
|
|
static void local_oob_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const struct mgmt_rp_read_local_oob_data *rp = param;
|
2014-01-10 11:53:59 +08:00
|
|
|
const struct mgmt_rp_read_local_oob_ext_data *rp_ext = param;
|
2014-01-10 11:39:59 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr, "Read Local OOB Data failed "
|
|
|
|
"with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Too small (%u bytes) read_local_oob rsp\n",
|
|
|
|
len);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Hash C from P-192: ");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf("%02x", rp->hash[i]);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Randomizer R with P-192: ");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf("%02x", rp->randomizer[i]);
|
|
|
|
printf("\n");
|
|
|
|
|
2014-01-10 11:53:59 +08:00
|
|
|
if (len < sizeof(*rp_ext))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
printf("Hash C from P-256: ");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf("%02x", rp_ext->hash256[i]);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Randomizer R with P-256: ");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf("%02x", rp_ext->randomizer256[i]);
|
|
|
|
printf("\n");
|
|
|
|
|
2014-01-10 11:39:59 +08:00
|
|
|
done:
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2014-01-10 11:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_local_oob(struct mgmt *mgmt, uint16_t index,
|
|
|
|
int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_READ_LOCAL_OOB_DATA, index, 0, NULL,
|
|
|
|
local_oob_rsp, NULL, NULL) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send read_local_oob cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void did_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
2012-03-28 18:03:17 +08:00
|
|
|
{
|
2013-02-18 18:35:47 +08:00
|
|
|
if (status != 0)
|
2012-12-14 23:42:27 +08:00
|
|
|
fprintf(stderr, "Set Device ID failed "
|
|
|
|
"with status 0x%02x (%s)\n",
|
2012-03-28 18:03:17 +08:00
|
|
|
status, mgmt_errstr(status));
|
2013-02-18 18:35:47 +08:00
|
|
|
else
|
|
|
|
printf("Device ID successfully set\n");
|
2012-03-28 18:03:17 +08:00
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2012-03-28 18:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void did_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt did <source>:<vendor>:<product>:<version>\n");
|
|
|
|
printf(" possible source values: bluetooth, usb\n");
|
|
|
|
}
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
static void cmd_did(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
|
2012-03-28 18:03:17 +08:00
|
|
|
{
|
|
|
|
struct mgmt_cp_set_device_id cp;
|
|
|
|
uint16_t vendor, product, version , source;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
did_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = sscanf(argv[1], "bluetooth:%4hx:%4hx:%4hx", &vendor, &product,
|
|
|
|
&version);
|
|
|
|
if (result == 3) {
|
|
|
|
source = 0x0001;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = sscanf(argv[1], "usb:%4hx:%4hx:%4hx", &vendor, &product,
|
|
|
|
&version);
|
|
|
|
if (result == 3) {
|
|
|
|
source = 0x0002;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
did_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
cp.source = htobs(source);
|
|
|
|
cp.vendor = htobs(vendor);
|
|
|
|
cp.product = htobs(product);
|
|
|
|
cp.version = htobs(version);
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
if (mgmt_send(mgmt, MGMT_OP_SET_DEVICE_ID, index, sizeof(cp), &cp,
|
|
|
|
did_rsp, NULL, NULL) == 0) {
|
2013-10-02 21:14:43 +08:00
|
|
|
fprintf(stderr, "Unable to send set_device_id cmd\n");
|
2012-03-28 18:03:17 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 21:13:55 +08:00
|
|
|
static void static_addr_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (status != 0)
|
|
|
|
fprintf(stderr, "Set static address failed "
|
|
|
|
"with status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
else
|
|
|
|
printf("Static address successfully set\n");
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_quit();
|
2013-10-02 21:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void static_addr_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt static-addr <address>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_static_addr(struct mgmt *mgmt, uint16_t index,
|
|
|
|
int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_set_static_address cp;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
static_addr_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
str2ba(argv[1], &cp.bdaddr);
|
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_SET_STATIC_ADDRESS, index, sizeof(cp), &cp,
|
|
|
|
static_addr_rsp, NULL, NULL) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send set_static_address cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-02 16:02:34 +08:00
|
|
|
static void cmd_debug_keys(struct mgmt *mgmt, uint16_t index,
|
|
|
|
int argc, char **argv)
|
|
|
|
{
|
|
|
|
cmd_setting(mgmt, index, MGMT_OP_SET_DEBUG_KEYS, argc, argv);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:22:47 +08:00
|
|
|
static void conn_info_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const struct mgmt_rp_get_conn_info *rp = param; char addr[18];
|
|
|
|
|
|
|
|
if (len == 0 && status != 0) {
|
|
|
|
fprintf(stderr, "Get Conn Info failed, status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(*rp)) {
|
|
|
|
fprintf(stderr, "Unexpected Get Conn Info len %u\n", len);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ba2str(&rp->addr.bdaddr, addr);
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr, "Get Conn Info for %s (%s) failed. status 0x%02x (%s)\n",
|
|
|
|
addr, typestr(rp->addr.type),
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Connection Information for %s (%s)\n",
|
|
|
|
addr, typestr(rp->addr.type));
|
|
|
|
printf("\tRSSI %d\n\tTX power %d\n\tmaximum TX power %d\n",
|
|
|
|
rp->rssi, rp->tx_power, rp->max_tx_power);
|
|
|
|
|
|
|
|
done:
|
|
|
|
mainloop_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void conn_info_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt conn-info [-t type] <remote address>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option conn_info_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "type", 1, 0, 't' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void cmd_conn_info(struct mgmt *mgmt, uint16_t index,
|
|
|
|
int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_get_conn_info cp;
|
|
|
|
uint8_t type = BDADDR_BREDR;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "+t:h", conn_info_options,
|
|
|
|
NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
|
|
|
type = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
conn_info_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
conn_info_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
str2ba(argv[0], &cp.addr.bdaddr);
|
|
|
|
cp.addr.type = type;
|
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_GET_CONN_INFO, index, sizeof(cp), &cp,
|
|
|
|
conn_info_rsp, NULL, NULL) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send get_conn_info cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:45 +08:00
|
|
|
static void set_io_cap_rsp(uint8_t status, uint16_t len, const void *param,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (status != 0)
|
|
|
|
fprintf(stderr, "Could not set IO Capability with "
|
|
|
|
"status 0x%02x (%s)\n",
|
|
|
|
status, mgmt_errstr(status));
|
|
|
|
else
|
|
|
|
printf("IO Capabilities successfully set\n");
|
|
|
|
|
|
|
|
mainloop_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_io_cap_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: btmgmt [cap]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_set_io_cap(struct mgmt *mgmt, uint16_t index,
|
|
|
|
int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct mgmt_cp_set_io_capability cp;
|
|
|
|
uint8_t cap;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
set_io_cap_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == MGMT_INDEX_NONE)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
cap = strtol(argv[1], NULL, 0);
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
cp.io_capability = cap;
|
|
|
|
|
|
|
|
if (mgmt_send(mgmt, MGMT_OP_SET_IO_CAPABILITY, index, sizeof(cp), &cp,
|
|
|
|
set_io_cap_rsp, NULL, NULL) == 0) {
|
|
|
|
fprintf(stderr, "Unable to send set-io-cap cmd\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 18:01:47 +08:00
|
|
|
static struct {
|
|
|
|
char *cmd;
|
2013-02-18 20:19:54 +08:00
|
|
|
void (*func)(struct mgmt *mgmt, uint16_t index, int argc, char **argv);
|
2011-10-26 18:01:47 +08:00
|
|
|
char *doc;
|
|
|
|
} command[] = {
|
|
|
|
{ "monitor", cmd_monitor, "Monitor events" },
|
2012-02-20 20:11:06 +08:00
|
|
|
{ "version", cmd_version, "Get the MGMT Version" },
|
2012-02-15 19:52:53 +08:00
|
|
|
{ "commands", cmd_commands, "List supported commands" },
|
2011-10-26 23:41:41 +08:00
|
|
|
{ "info", cmd_info, "Show controller info" },
|
2011-10-27 16:43:06 +08:00
|
|
|
{ "power", cmd_power, "Toggle powered state" },
|
2011-10-28 03:06:04 +08:00
|
|
|
{ "discov", cmd_discov, "Toggle discoverable state" },
|
2011-11-01 16:52:12 +08:00
|
|
|
{ "connectable",cmd_connectable,"Toggle connectable state" },
|
2013-03-04 16:36:39 +08:00
|
|
|
{ "fast-conn", cmd_fast_conn, "Toggle fast connectable state" },
|
2011-11-01 18:29:05 +08:00
|
|
|
{ "pairable", cmd_pairable, "Toggle pairable state" },
|
2012-02-17 05:55:33 +08:00
|
|
|
{ "linksec", cmd_linksec, "Toggle link level security" },
|
2012-02-17 07:02:39 +08:00
|
|
|
{ "ssp", cmd_ssp, "Toggle SSP mode" },
|
2014-01-10 11:47:24 +08:00
|
|
|
{ "sc", cmd_sc, "Toogle SC support" },
|
|
|
|
{ "hs", cmd_hs, "Toggle HS support" },
|
|
|
|
{ "le", cmd_le, "Toggle LE support" },
|
2013-09-24 20:24:34 +08:00
|
|
|
{ "advertising",cmd_advertising,"Toggle LE advertising", },
|
2013-10-02 15:41:48 +08:00
|
|
|
{ "bredr", cmd_bredr, "Toggle BR/EDR support", },
|
2014-02-22 22:36:53 +08:00
|
|
|
{ "privacy", cmd_privacy, "Toggle privacy support" },
|
2011-11-01 18:29:05 +08:00
|
|
|
{ "class", cmd_class, "Set device major/minor class" },
|
2011-11-01 18:53:25 +08:00
|
|
|
{ "disconnect", cmd_disconnect, "Disconnect device" },
|
2011-11-01 19:06:12 +08:00
|
|
|
{ "con", cmd_con, "List connections" },
|
2011-11-01 21:05:03 +08:00
|
|
|
{ "find", cmd_find, "Discover nearby devices" },
|
2011-11-02 15:02:19 +08:00
|
|
|
{ "name", cmd_name, "Set local name" },
|
2011-11-10 19:11:36 +08:00
|
|
|
{ "pair", cmd_pair, "Pair with a remote device" },
|
2013-01-22 20:50:02 +08:00
|
|
|
{ "cancelpair", cmd_cancel_pair,"Cancel pairing" },
|
2012-02-09 19:33:54 +08:00
|
|
|
{ "unpair", cmd_unpair, "Unpair device" },
|
2014-02-16 07:42:45 +08:00
|
|
|
{ "keys", cmd_keys, "Load Link Keys" },
|
|
|
|
{ "ltks", cmd_ltks, "Load Long Term Keys" },
|
2014-02-18 16:15:16 +08:00
|
|
|
{ "irks", cmd_irks, "Load Identity Resolving Keys" },
|
2012-02-19 19:53:59 +08:00
|
|
|
{ "block", cmd_block, "Block Device" },
|
2012-02-24 03:53:17 +08:00
|
|
|
{ "unblock", cmd_unblock, "Unblock Device" },
|
|
|
|
{ "add-uuid", cmd_add_uuid, "Add UUID" },
|
2014-01-10 11:39:59 +08:00
|
|
|
{ "rm-uuid", cmd_remove_uuid,"Remove UUID" },
|
|
|
|
{ "clr-uuids", cmd_clr_uuids, "Clear UUIDs" },
|
2014-01-10 11:47:24 +08:00
|
|
|
{ "local-oob", cmd_local_oob, "Local OOB data" },
|
2014-01-10 11:39:59 +08:00
|
|
|
{ "did", cmd_did, "Set Device ID" },
|
|
|
|
{ "static-addr",cmd_static_addr,"Set static address" },
|
2014-02-02 16:02:34 +08:00
|
|
|
{ "debug-keys", cmd_debug_keys, "Toogle debug keys" },
|
2014-05-17 02:22:47 +08:00
|
|
|
{ "conn-info", cmd_conn_info, "Get connection information" },
|
2014-05-27 23:17:45 +08:00
|
|
|
{ "set-io-cap", cmd_set_io_cap, "Set IO Capability" },
|
2014-01-10 11:39:59 +08:00
|
|
|
{ }
|
2011-10-26 18:01:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("btmgmt ver %s\n", VERSION);
|
|
|
|
printf("Usage:\n"
|
|
|
|
"\tbtmgmt [options] <command> [command parameters]\n");
|
|
|
|
|
|
|
|
printf("Options:\n"
|
2011-10-28 02:56:42 +08:00
|
|
|
"\t--index <id>\tSpecify adapter index\n"
|
2011-10-27 22:30:29 +08:00
|
|
|
"\t--verbose\tEnable extra logging\n"
|
2011-10-26 18:01:47 +08:00
|
|
|
"\t--help\tDisplay help\n");
|
|
|
|
|
|
|
|
printf("Commands:\n");
|
|
|
|
for (i = 0; command[i].cmd; i++)
|
2011-11-01 20:49:38 +08:00
|
|
|
printf("\t%-15s\t%s\n", command[i].cmd, command[i].doc);
|
2011-10-26 18:01:47 +08:00
|
|
|
|
|
|
|
printf("\n"
|
|
|
|
"For more information on the usage of each command use:\n"
|
|
|
|
"\tbtmgmt <command> --help\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option main_options[] = {
|
2011-10-28 02:56:42 +08:00
|
|
|
{ "index", 1, 0, 'i' },
|
2011-10-27 22:30:29 +08:00
|
|
|
{ "verbose", 0, 0, 'v' },
|
2011-10-26 18:01:47 +08:00
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2011-10-25 21:36:00 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-02-18 20:38:29 +08:00
|
|
|
int opt, i;
|
2011-10-28 02:56:42 +08:00
|
|
|
uint16_t index = MGMT_INDEX_NONE;
|
2013-02-18 17:32:50 +08:00
|
|
|
struct mgmt *mgmt;
|
2014-01-12 18:23:01 +08:00
|
|
|
int exit_status;
|
2011-10-25 23:07:08 +08:00
|
|
|
|
2011-10-28 02:56:42 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, "+hvi:",
|
|
|
|
main_options, NULL)) != -1) {
|
2011-10-26 18:01:47 +08:00
|
|
|
switch (opt) {
|
2011-10-28 02:56:42 +08:00
|
|
|
case 'i':
|
|
|
|
if (strlen(optarg) > 3 &&
|
|
|
|
strncasecmp(optarg, "hci", 3) == 0)
|
|
|
|
index = atoi(&optarg[4]);
|
|
|
|
else
|
|
|
|
index = atoi(optarg);
|
|
|
|
break;
|
2011-10-27 22:30:29 +08:00
|
|
|
case 'v':
|
|
|
|
monitor = true;
|
|
|
|
break;
|
2011-10-26 18:01:47 +08:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
mainloop_init();
|
2011-10-25 23:07:08 +08:00
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
mgmt = mgmt_new_default();
|
|
|
|
if (!mgmt) {
|
|
|
|
fprintf(stderr, "Unable to open mgmt_socket\n");
|
2014-01-12 18:23:01 +08:00
|
|
|
return EXIT_FAILURE;
|
2013-02-18 20:19:54 +08:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:01:47 +08:00
|
|
|
for (i = 0; command[i].cmd; i++) {
|
|
|
|
if (strcmp(command[i].cmd, argv[0]) != 0)
|
|
|
|
continue;
|
|
|
|
|
2013-02-18 20:19:54 +08:00
|
|
|
command[i].func(mgmt, index, argc, argv);
|
2011-10-26 18:01:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-01 18:15:35 +08:00
|
|
|
if (command[i].cmd == NULL) {
|
|
|
|
fprintf(stderr, "Unknown command: %s\n", argv[0]);
|
2013-02-18 20:19:54 +08:00
|
|
|
mgmt_unref(mgmt);
|
2014-01-12 18:23:01 +08:00
|
|
|
return EXIT_FAILURE;
|
2013-02-18 17:32:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-18 17:38:51 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error,
|
|
|
|
NULL, NULL);
|
2013-02-18 17:42:41 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_INDEX_ADDED, index, index_added,
|
|
|
|
NULL, NULL);
|
|
|
|
mgmt_register(mgmt, MGMT_EV_INDEX_REMOVED, index, index_removed,
|
|
|
|
NULL, NULL);
|
2013-02-18 17:53:56 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_NEW_SETTINGS, index, new_settings,
|
|
|
|
NULL, NULL);
|
2013-02-18 17:56:22 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_DISCOVERING, index, discovering,
|
|
|
|
NULL, NULL);
|
2013-02-18 18:37:45 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_NEW_LINK_KEY, index, new_link_key,
|
|
|
|
NULL, NULL);
|
2013-02-18 18:41:37 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_DEVICE_CONNECTED, index, connected,
|
|
|
|
NULL, NULL);
|
|
|
|
mgmt_register(mgmt, MGMT_EV_DEVICE_DISCONNECTED, index, disconnected,
|
|
|
|
NULL, NULL);
|
|
|
|
mgmt_register(mgmt, MGMT_EV_CONNECT_FAILED, index, conn_failed,
|
|
|
|
NULL, NULL);
|
2013-02-18 18:47:00 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_AUTH_FAILED, index, auth_failed,
|
|
|
|
NULL, NULL);
|
2013-02-18 18:48:58 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_LOCAL_NAME_CHANGED, index,
|
|
|
|
local_name_changed, NULL, NULL);
|
2013-02-18 18:51:51 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_DEVICE_FOUND, index, device_found,
|
2013-02-18 20:23:58 +08:00
|
|
|
mgmt, NULL);
|
2013-02-18 18:58:38 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_PIN_CODE_REQUEST, index, request_pin,
|
2013-02-18 20:28:59 +08:00
|
|
|
mgmt, NULL);
|
2013-02-18 18:58:38 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_USER_CONFIRM_REQUEST, index, user_confirm,
|
2013-02-18 20:33:03 +08:00
|
|
|
mgmt, NULL);
|
2014-06-02 02:48:00 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_USER_PASSKEY_REQUEST, index,
|
|
|
|
request_passkey, mgmt, NULL);
|
2014-06-02 02:51:48 +08:00
|
|
|
mgmt_register(mgmt, MGMT_EV_PASSKEY_NOTIFY, index,
|
|
|
|
passkey_notify, mgmt, NULL);
|
2013-02-18 17:38:51 +08:00
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
exit_status = mainloop_run();
|
2011-10-25 23:07:08 +08:00
|
|
|
|
2013-02-18 17:32:50 +08:00
|
|
|
mgmt_cancel_all(mgmt);
|
|
|
|
mgmt_unregister_all(mgmt);
|
|
|
|
mgmt_unref(mgmt);
|
|
|
|
|
2014-01-12 18:23:01 +08:00
|
|
|
return exit_status;
|
2011-10-25 21:36:00 +08:00
|
|
|
}
|