1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011-2012 Intel Corporation
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include "monitor/bt.h"
#include "src/shared/mainloop.h"
#include "src/shared/timeout.h"
#include "src/shared/util.h"
#include "src/shared/hci.h"
static int urandom_fd;
static struct bt_hci *hci_dev;
static bool shutdown_timeout(void *user_data)
{
mainloop_quit();
return false;
}
static void shutdown_complete(const void *data, uint8_t size, void *user_data)
{
unsigned int id = PTR_TO_UINT(user_data);
timeout_remove(id);
mainloop_quit();
}
static void shutdown_device(void)
{
uint8_t enable = 0x00;
unsigned int id;
bt_hci_flush(hci_dev);
id = timeout_add(5000, shutdown_timeout, NULL, NULL);
bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_ENABLE,
&enable, 1, NULL, NULL, NULL);
bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
shutdown_complete, UINT_TO_PTR(id), NULL);
}
static void set_random_address(void)
{
struct bt_hci_cmd_le_set_random_address cmd;
ssize_t len;
len = read(urandom_fd, cmd.addr, sizeof(cmd.addr));
if (len < 0 || len != sizeof(cmd.addr)) {
fprintf(stderr, "Failed to read random data\n");
return;
}
/* Clear top most significant bits */
cmd.addr[5] &= 0x3f;
bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
&cmd, sizeof(cmd), NULL, NULL, NULL);
}
static void set_adv_parameters(void)
{
struct bt_hci_cmd_le_set_adv_parameters cmd;
cmd.min_interval = cpu_to_le16(0x0800);
cmd.max_interval = cpu_to_le16(0x0800);
cmd.type = 0x03; /* Non-connectable advertising */
cmd.own_addr_type = 0x01; /* Use random address */
cmd.direct_addr_type = 0x00;
memset(cmd.direct_addr, 0, 6);
cmd.channel_map = 0x07;
cmd.filter_policy = 0x00;
bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
&cmd, sizeof(cmd), NULL, NULL, NULL);
}
static void set_adv_enable(void)
{
uint8_t enable = 0x01;
bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_ENABLE,
&enable, 1, NULL, NULL, NULL);
}
static void adv_data_callback(const void *data, uint8_t size,
void *user_data)
{
uint8_t status = *((uint8_t *) data);
if (status) {
fprintf(stderr, "Failed to set advertising data\n");
shutdown_device();
return;
}
set_random_address();
set_adv_parameters();
set_adv_enable();
}
static void adv_tx_power_callback(const void *data, uint8_t size,
void *user_data)
{
const struct bt_hci_rsp_le_read_adv_tx_power *rsp = data;
struct bt_hci_cmd_le_set_adv_data cmd;
if (rsp->status) {
fprintf(stderr, "Failed to read advertising TX power\n");
shutdown_device();
return;
}
cmd.data[0] = 0x02; /* Field length */
cmd.data[1] = 0x01; /* Flags */
cmd.data[2] = 0x04; /* BR/EDR Not Supported */
cmd.data[3] = 0x1a; /* Field length */
cmd.data[4] = 0xff; /* Vendor field */
cmd.data[5] = 0x4c; /* Apple (76) - LSB */
cmd.data[6] = 0x00; /* Apple (76) - MSB */
cmd.data[7] = 0x02; /* iBeacon type */
cmd.data[8] = 0x15; /* Length */
memset(cmd.data + 9, 0, 16); /* UUID */
cmd.data[25] = 0x00; /* Major - LSB */
cmd.data[26] = 0x00; /* Major - MSB */
cmd.data[27] = 0x00; /* Minor - LSB */
cmd.data[28] = 0x00; /* Minor - MSB */
cmd.data[29] = 0xc5; /* TX power level */
cmd.data[30] = 0x00; /* Field terminator */
cmd.len = 1 + cmd.data[0] + 1 + cmd.data[3];
bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_DATA, &cmd, sizeof(cmd),
adv_data_callback, NULL, NULL);
}
static void local_features_callback(const void *data, uint8_t size,
void *user_data)
{
const struct bt_hci_rsp_read_local_features *rsp = data;
if (rsp->status) {
fprintf(stderr, "Failed to read local features\n");
shutdown_device();
return;
}
if (!(rsp->features[4] & 0x40)) {
fprintf(stderr, "Controller without Low Energy support\n");
shutdown_device();
return;
}
bt_hci_send(hci_dev, BT_HCI_CMD_LE_READ_ADV_TX_POWER, NULL, 0,
adv_tx_power_callback, NULL, NULL);
}
static void start_ibeacon(void)
{
bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0, NULL, NULL, NULL);
bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0,
local_features_callback, NULL, NULL);
}
static void signal_callback(int signum, void *user_data)
{
static bool terminated = false;
switch (signum) {
case SIGINT:
case SIGTERM:
if (!terminated) {
shutdown_device();
terminated = true;
}
break;
}
}
static void usage(void)
{
printf("ibeacon - Low Energy iBeacon testing tool\n"
"Usage:\n");
printf("\tibeacon [options]\n");
printf("Options:\n"
"\t-i, --index <num> Use specified controller\n"
"\t-h, --help Show help options\n");
}
static const struct option main_options[] = {
{ "index", required_argument, NULL, 'i' },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ }
};
int main(int argc, char *argv[])
{
uint16_t index = 0;
const char *str;
int exit_status;
for (;;) {
int opt;
opt = getopt_long(argc, argv, "i:vh", main_options, NULL);
if (opt < 0)
break;
switch (opt) {
case 'i':
if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
str = optarg + 3;
else
str = optarg;
if (!isdigit(*str)) {
usage();
return EXIT_FAILURE;
}
index = atoi(str);
break;
case 'v':
printf("%s\n", VERSION);
return EXIT_SUCCESS;
case 'h':
usage();
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
if (argc - optind > 0) {
fprintf(stderr, "Invalid command line parameters\n");
return EXIT_FAILURE;
}
urandom_fd = open("/dev/urandom", O_RDONLY);
if (urandom_fd < 0) {
fprintf(stderr, "Failed to open /dev/urandom device\n");
return EXIT_FAILURE;
}
mainloop_init();
printf("Low Energy iBeacon utility ver %s\n", VERSION);
hci_dev = bt_hci_new_user_channel(index);
if (!hci_dev) {
fprintf(stderr, "Failed to open HCI user channel\n");
exit_status = EXIT_FAILURE;
goto done;
}
start_ibeacon();
exit_status = mainloop_run_with_signal(signal_callback, NULL);
bt_hci_unref(hci_dev);
done:
close(urandom_fd);
return exit_status;
}
|