3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2011-2012 Intel Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
39 #include "monitor/bt.h"
40 #include "src/shared/mainloop.h"
41 #include "src/shared/timeout.h"
42 #include "src/shared/util.h"
43 #include "src/shared/hci.h"
45 static int urandom_fd;
46 static struct bt_hci *hci_dev;
48 static bool shutdown_timeout(void *user_data)
55 static void shutdown_complete(const void *data, uint8_t size, void *user_data)
57 unsigned int id = PTR_TO_UINT(user_data);
63 static void shutdown_device(void)
65 uint8_t enable = 0x00;
68 bt_hci_flush(hci_dev);
70 id = timeout_add(5000, shutdown_timeout, NULL, NULL);
72 bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_ENABLE,
73 &enable, 1, NULL, NULL, NULL);
75 bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
76 shutdown_complete, UINT_TO_PTR(id), NULL);
79 static void set_random_address(void)
81 struct bt_hci_cmd_le_set_random_address cmd;
84 len = read(urandom_fd, cmd.addr, sizeof(cmd.addr));
85 if (len < 0 || len != sizeof(cmd.addr)) {
86 fprintf(stderr, "Failed to read random data\n");
90 /* Clear top most significant bits */
93 bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
94 &cmd, sizeof(cmd), NULL, NULL, NULL);
97 static void set_adv_parameters(void)
99 struct bt_hci_cmd_le_set_adv_parameters cmd;
101 cmd.min_interval = cpu_to_le16(0x0800);
102 cmd.max_interval = cpu_to_le16(0x0800);
103 cmd.type = 0x03; /* Non-connectable advertising */
104 cmd.own_addr_type = 0x01; /* Use random address */
105 cmd.direct_addr_type = 0x00;
106 memset(cmd.direct_addr, 0, 6);
107 cmd.channel_map = 0x07;
108 cmd.filter_policy = 0x00;
110 bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
111 &cmd, sizeof(cmd), NULL, NULL, NULL);
114 static void set_adv_enable(void)
116 uint8_t enable = 0x01;
118 bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_ENABLE,
119 &enable, 1, NULL, NULL, NULL);
122 static void adv_data_callback(const void *data, uint8_t size,
125 uint8_t status = *((uint8_t *) data);
128 fprintf(stderr, "Failed to set advertising data\n");
133 set_random_address();
134 set_adv_parameters();
138 static void adv_tx_power_callback(const void *data, uint8_t size,
141 const struct bt_hci_rsp_le_read_adv_tx_power *rsp = data;
142 struct bt_hci_cmd_le_set_adv_data cmd;
145 fprintf(stderr, "Failed to read advertising TX power\n");
150 cmd.data[0] = 0x02; /* Field length */
151 cmd.data[1] = 0x01; /* Flags */
152 cmd.data[2] = 0x02; /* LE General Discoverable Mode */
153 cmd.data[2] |= 0x04; /* BR/EDR Not Supported */
155 cmd.data[3] = 0x03; /* Field length */
156 cmd.data[4] = 0x03; /* 16-bit Service UUID list */
157 cmd.data[5] = 0xaa; /* Eddystone UUID */
160 cmd.data[7] = 0x0c; /* Field length */
161 cmd.data[8] = 0x16; /* 16-bit Service UUID data */
162 cmd.data[9] = 0xaa; /* Eddystone UUID */
164 cmd.data[11] = 0x10; /* Eddystone-URL frame type */
165 cmd.data[12] = 0x00; /* Calibrated Tx power at 0m */
166 cmd.data[13] = 0x00; /* URL Scheme Prefix http://www. */
172 cmd.data[19] = 0x01; /* .org/ */
174 cmd.data[20] = 0x00; /* Field terminator */
175 memset(cmd.data + 21, 0, 9);
177 cmd.len = 1 + cmd.data[0] + 1 + cmd.data[3] + 1 + cmd.data[7];
179 bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_DATA, &cmd, sizeof(cmd),
180 adv_data_callback, NULL, NULL);
183 static void local_features_callback(const void *data, uint8_t size,
186 const struct bt_hci_rsp_read_local_features *rsp = data;
189 fprintf(stderr, "Failed to read local features\n");
194 if (!(rsp->features[4] & 0x40)) {
195 fprintf(stderr, "Controller without Low Energy support\n");
200 bt_hci_send(hci_dev, BT_HCI_CMD_LE_READ_ADV_TX_POWER, NULL, 0,
201 adv_tx_power_callback, NULL, NULL);
204 static void start_eddystone(void)
206 bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0, NULL, NULL, NULL);
208 bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0,
209 local_features_callback, NULL, NULL);
212 static void signal_callback(int signum, void *user_data)
214 static bool terminated = false;
227 static void usage(void)
229 printf("eddystone - Low Energy Eddystone testing tool\n"
231 printf("\teddystone [options]\n");
233 "\t-i, --index <num> Use specified controller\n"
234 "\t-h, --help Show help options\n");
237 static const struct option main_options[] = {
238 { "index", required_argument, NULL, 'i' },
239 { "version", no_argument, NULL, 'v' },
240 { "help", no_argument, NULL, 'h' },
244 int main(int argc, char *argv[])
254 opt = getopt_long(argc, argv, "i:vh", main_options, NULL);
260 if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
264 if (!isdigit(*str)) {
271 printf("%s\n", VERSION);
281 if (argc - optind > 0) {
282 fprintf(stderr, "Invalid command line parameters\n");
286 urandom_fd = open("/dev/urandom", O_RDONLY);
287 if (urandom_fd < 0) {
288 fprintf(stderr, "Failed to open /dev/urandom device\n");
295 sigaddset(&mask, SIGINT);
296 sigaddset(&mask, SIGTERM);
298 mainloop_set_signal(&mask, signal_callback, NULL, NULL);
300 printf("Low Energy Eddystone utility ver %s\n", VERSION);
302 hci_dev = bt_hci_new_user_channel(index);
304 fprintf(stderr, "Failed to open HCI user channel\n");
305 exit_status = EXIT_FAILURE;
311 exit_status = mainloop_run();
313 bt_hci_unref(hci_dev);