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 library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library 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 GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/socket.h>
36 #include <bluetooth/bluetooth.h>
37 #include <bluetooth/hci.h>
39 #include "src/shared/util.h"
40 #include "src/shared/crypto.h"
41 #include "monitor/mainloop.h"
42 #include "monitor/bt.h"
46 #define WHITE_LIST_SIZE 16
49 volatile int ref_count;
51 struct bt_crypto *crypto;
53 uint8_t event_mask[16];
54 uint16_t manufacturer;
59 uint8_t le_event_mask[8];
62 uint8_t le_features[8];
63 uint8_t le_random_addr[6];
64 uint16_t le_adv_min_interval;
65 uint16_t le_adv_max_interval;
67 uint8_t le_adv_own_addr_type;
68 uint8_t le_adv_direct_addr_type;
69 uint8_t le_adv_direct_addr[6];
70 uint8_t le_adv_channel_map;
71 uint8_t le_adv_filter_policy;
72 int8_t le_adv_tx_power;
73 uint8_t le_adv_data_len;
74 uint8_t le_adv_data[31];
75 uint8_t le_scan_rsp_data_len;
76 uint8_t le_scan_rsp_data[31];
77 uint8_t le_adv_enable;
79 uint8_t le_white_list_size;
83 static void reset_defaults(struct bt_le *hci)
85 memset(hci->event_mask, 0, sizeof(hci->event_mask));
86 hci->event_mask[0] |= 0x10; /* Disconnection Complete */
87 hci->event_mask[0] |= 0x80; /* Encryption Change */
88 hci->event_mask[1] |= 0x08; /* Read Remote Version Information Complete */
89 hci->event_mask[1] |= 0x20; /* Command Complete */
90 hci->event_mask[1] |= 0x40; /* Command Status */
91 hci->event_mask[1] |= 0x80; /* Hardware Error */
92 hci->event_mask[2] |= 0x04; /* Number of Completed Packets */
93 hci->event_mask[3] |= 0x02; /* Data Buffer Overflow */
94 hci->event_mask[5] |= 0x80; /* Encryption Key Refresh Complete */
96 hci->manufacturer = 0x003f; /* Bluetooth SIG (63) */
98 memset(hci->commands, 0, sizeof(hci->commands));
99 //hci->commands[0] |= 0x20; /* Disconnect */
100 //hci->commands[2] |= 0x80; /* Read Remote Version Information */
101 hci->commands[5] |= 0x40; /* Set Event Mask */
102 hci->commands[5] |= 0x80; /* Reset */
103 //hci->commands[10] |= 0x04; /* Read Transmit Power Level */
104 hci->commands[14] |= 0x08; /* Read Local Version Information */
105 hci->commands[14] |= 0x10; /* Read Local Supported Commands */
106 hci->commands[14] |= 0x20; /* Read Local Supported Features */
107 hci->commands[14] |= 0x80; /* Read Buffer Size */
108 hci->commands[15] |= 0x02; /* Read BD ADDR */
109 //hci->commands[15] |= 0x20; /* Read RSSI */
110 hci->commands[25] |= 0x01; /* LE Set Event Mask */
111 hci->commands[25] |= 0x02; /* LE Read Buffer Size */
112 hci->commands[25] |= 0x04; /* LE Read Local Supported Features */
113 hci->commands[25] |= 0x10; /* LE Set Random Address */
114 hci->commands[25] |= 0x20; /* LE Set Advertising Parameters */
115 hci->commands[25] |= 0x40; /* LE Read Advertising Channel TX Power */
116 hci->commands[25] |= 0x80; /* LE Set Advertising Data */
117 hci->commands[26] |= 0x01; /* LE Set Scan Response Data */
118 hci->commands[26] |= 0x02; /* LE Set Advertise Enable */
119 //hci->commands[26] |= 0x04; /* LE Set Scan Parameters */
120 //hci->commands[26] |= 0x08; /* LE Set Scan Enable */
121 //hci->commands[26] |= 0x10; /* LE Create Connection */
122 //hci->commands[26] |= 0x20; /* LE Create Connection Cancel */
123 hci->commands[26] |= 0x40; /* LE Read White List Size */
124 hci->commands[26] |= 0x80; /* LE Clear White List */
125 //hci->commands[27] |= 0x01; /* LE Add Device To White List */
126 //hci->commands[27] |= 0x02; /* LE Remove Device From White List */
127 //hci->commands[27] |= 0x04; /* LE Connection Update */
128 //hci->commands[27] |= 0x08; /* LE Set Host Channel Classification */
129 //hci->commands[27] |= 0x10; /* LE Read Channel Map */
130 //hci->commands[27] |= 0x20; /* LE Read Remote Used Features */
131 hci->commands[27] |= 0x40; /* LE Encrypt */
132 hci->commands[27] |= 0x80; /* LE Rand */
133 //hci->commands[28] |= 0x01; /* LE Start Encryption */
134 //hci->commands[28] |= 0x02; /* LE Long Term Key Request Reply */
135 //hci->commands[28] |= 0x04; /* LE Long Term Key Request Negative Reply */
136 hci->commands[28] |= 0x08; /* LE Read Supported States */
137 //hci->commands[28] |= 0x10; /* LE Receiver Test */
138 //hci->commands[28] |= 0x20; /* LE Transmitter Test */
139 //hci->commands[28] |= 0x40; /* LE Test End */
140 //hci->commands[33] |= 0x10; /* LE Remote Connection Parameter Request Reply */
141 //hci->commands[33] |= 0x20; /* LE Remote Connection Parameter Request Negative Reply */
143 memset(hci->features, 0, sizeof(hci->features));
144 hci->features[4] |= 0x20; /* BR/EDR Not Supported */
145 hci->features[4] |= 0x40; /* LE Supported */
147 memset(hci->bdaddr, 0, sizeof(hci->bdaddr));
149 memset(hci->le_event_mask, 0, sizeof(hci->le_event_mask));
150 hci->le_event_mask[0] |= 0x01; /* LE Connection Complete */
151 hci->le_event_mask[0] |= 0x02; /* LE Advertising Report */
152 hci->le_event_mask[0] |= 0x04; /* LE Connection Update Complete */
153 hci->le_event_mask[0] |= 0x08; /* LE Read Remote Used Features Complete */
154 hci->le_event_mask[0] |= 0x10; /* LE Long Term Key Request */
155 //hci->le_event_mask[0] |= 0x20; /* LE Remote Connection Parameter Request */
160 memset(hci->le_features, 0, sizeof(hci->le_features));
161 hci->le_features[0] |= 0x01; /* LE Encryption */
162 //hci->le_features[0] |= 0x02; /* Connection Parameter Request Procedure */
163 //hci->le_features[0] |= 0x04; /* Extended Reject Indication */
164 //hci->le_features[0] |= 0x08; /* Slave-initiated Features Exchange */
165 //hci->le_features[0] |= 0x10; /* LE Ping */
167 memset(hci->le_random_addr, 0, sizeof(hci->le_random_addr));
169 hci->le_adv_min_interval = 0x0800;
170 hci->le_adv_max_interval = 0x0800;
171 hci->le_adv_type = 0x00;
172 hci->le_adv_own_addr_type = 0x00;
173 hci->le_adv_direct_addr_type = 0x00;
174 memset(hci->le_adv_direct_addr, 0, 6);
175 hci->le_adv_channel_map = 0x07;
176 hci->le_adv_filter_policy = 0x00;
178 hci->le_adv_tx_power = 0;
180 memset(hci->le_adv_data, 0, sizeof(hci->le_adv_data));
181 hci->le_adv_data_len = 0;
183 memset(hci->le_scan_rsp_data, 0, sizeof(hci->le_scan_rsp_data));
184 hci->le_scan_rsp_data_len = 0;
186 hci->le_adv_enable = 0x00;
188 hci->le_white_list_size = WHITE_LIST_SIZE;
190 memset(hci->le_states, 0, sizeof(hci->le_states));
191 hci->le_states[0] |= 0x01; /* Non-connectable Advertising */
192 hci->le_states[0] |= 0x02; /* Scannable Advertising */
193 hci->le_states[0] |= 0x04; /* Connectable Advertising */
194 hci->le_states[0] |= 0x08; /* Directed Advertising */
195 hci->le_states[0] |= 0x10; /* Passive Scanning */
196 hci->le_states[0] |= 0x20; /* Active Scanning */
197 hci->le_states[0] |= 0x40; /* Initiating */
198 hci->le_states[0] |= 0x80; /* Connection */
201 static void send_event(struct bt_le *hci, uint8_t event,
202 void *data, uint8_t size)
204 uint8_t type = BT_H4_EVT_PKT;
205 struct bt_hci_evt_hdr hdr;
212 iov[0].iov_base = &type;
214 iov[1].iov_base = &hdr;
215 iov[1].iov_len = sizeof(hdr);
218 iov[2].iov_base = data;
219 iov[2].iov_len = size;
224 if (writev(hci->vhci_fd, iov, iovcnt) < 0)
225 fprintf(stderr, "Write to /dev/vhci failed (%m)\n");
228 static void cmd_complete(struct bt_le *hci, uint16_t opcode,
229 const void *data, uint8_t len)
231 struct bt_hci_evt_cmd_complete *cc;
234 pkt_data = alloca(sizeof(*cc) + len);
240 cc->opcode = cpu_to_le16(opcode);
243 memcpy(pkt_data + sizeof(*cc), data, len);
245 send_event(hci, BT_HCI_EVT_CMD_COMPLETE, pkt_data, sizeof(*cc) + len);
248 static void cmd_status(struct bt_le *hci, uint8_t status, uint16_t opcode)
250 struct bt_hci_evt_cmd_status cs;
254 cs.opcode = cpu_to_le16(opcode);
256 send_event(hci, BT_HCI_EVT_CMD_STATUS, &cs, sizeof(cs));
259 static void cmd_set_event_mask(struct bt_le *hci,
260 const void *data, uint8_t size)
262 const struct bt_hci_cmd_set_event_mask *cmd = data;
265 memcpy(hci->event_mask, cmd->mask, 8);
267 status = BT_HCI_ERR_SUCCESS;
268 cmd_complete(hci, BT_HCI_CMD_SET_EVENT_MASK, &status, sizeof(status));
271 static void cmd_reset(struct bt_le *hci, const void *data, uint8_t size)
277 status = BT_HCI_ERR_SUCCESS;
278 cmd_complete(hci, BT_HCI_CMD_RESET, &status, sizeof(status));
281 static void cmd_read_local_version(struct bt_le *hci,
282 const void *data, uint8_t size)
284 struct bt_hci_rsp_read_local_version rsp;
286 rsp.status = BT_HCI_ERR_SUCCESS;
288 rsp.hci_rev = cpu_to_le16(0x0000);
290 rsp.manufacturer = cpu_to_le16(hci->manufacturer);
291 rsp.lmp_subver = cpu_to_le16(0x0000);
293 cmd_complete(hci, BT_HCI_CMD_READ_LOCAL_VERSION, &rsp, sizeof(rsp));
296 static void cmd_read_local_commands(struct bt_le *hci,
297 const void *data, uint8_t size)
299 struct bt_hci_rsp_read_local_commands rsp;
301 rsp.status = BT_HCI_ERR_SUCCESS;
302 memcpy(rsp.commands, hci->commands, 64);
304 cmd_complete(hci, BT_HCI_CMD_READ_LOCAL_COMMANDS, &rsp, sizeof(rsp));
307 static void cmd_read_local_features(struct bt_le *hci,
308 const void *data, uint8_t size)
310 struct bt_hci_rsp_read_local_features rsp;
312 rsp.status = BT_HCI_ERR_SUCCESS;
313 memcpy(rsp.features, hci->features, 8);
315 cmd_complete(hci, BT_HCI_CMD_READ_LOCAL_FEATURES, &rsp, sizeof(rsp));
318 static void cmd_read_buffer_size(struct bt_le *hci,
319 const void *data, uint8_t size)
321 struct bt_hci_rsp_read_buffer_size rsp;
323 rsp.status = BT_HCI_ERR_SUCCESS;
324 rsp.acl_mtu = cpu_to_le16(0x0000);
326 rsp.acl_max_pkt = cpu_to_le16(0x0000);
327 rsp.sco_max_pkt = cpu_to_le16(0x0000);
329 cmd_complete(hci, BT_HCI_CMD_READ_BUFFER_SIZE, &rsp, sizeof(rsp));
332 static void cmd_read_bd_addr(struct bt_le *hci, const void *data, uint8_t size)
334 struct bt_hci_rsp_read_bd_addr rsp;
336 rsp.status = BT_HCI_ERR_SUCCESS;
337 memcpy(rsp.bdaddr, hci->bdaddr, 6);
339 cmd_complete(hci, BT_HCI_CMD_READ_BD_ADDR, &rsp, sizeof(rsp));
342 static void cmd_le_set_event_mask(struct bt_le *hci,
343 const void *data, uint8_t size)
345 const struct bt_hci_cmd_le_set_event_mask *cmd = data;
348 memcpy(hci->le_event_mask, cmd->mask, 8);
350 status = BT_HCI_ERR_SUCCESS;
351 cmd_complete(hci, BT_HCI_CMD_LE_SET_EVENT_MASK,
352 &status, sizeof(status));
355 static void cmd_le_read_buffer_size(struct bt_le *hci,
356 const void *data, uint8_t size)
358 struct bt_hci_rsp_le_read_buffer_size rsp;
360 rsp.status = BT_HCI_ERR_SUCCESS;
361 rsp.le_mtu = cpu_to_le16(hci->le_mtu);
362 rsp.le_max_pkt = hci->le_max_pkt;
364 cmd_complete(hci, BT_HCI_CMD_LE_READ_BUFFER_SIZE, &rsp, sizeof(rsp));
367 static void cmd_le_read_local_features(struct bt_le *hci,
368 const void *data, uint8_t size)
370 struct bt_hci_rsp_le_read_local_features rsp;
372 rsp.status = BT_HCI_ERR_SUCCESS;
373 memcpy(rsp.features, hci->le_features, 8);
375 cmd_complete(hci, BT_HCI_CMD_LE_READ_LOCAL_FEATURES,
379 static void cmd_le_set_random_address(struct bt_le *hci,
380 const void *data, uint8_t size)
382 const struct bt_hci_cmd_le_set_random_address *cmd = data;
385 memcpy(hci->le_random_addr, cmd->addr, 6);
387 status = BT_HCI_ERR_SUCCESS;
388 cmd_complete(hci, BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
389 &status, sizeof(status));
392 static void cmd_le_set_adv_parameters(struct bt_le *hci,
393 const void *data, uint8_t size)
395 const struct bt_hci_cmd_le_set_adv_parameters *cmd = data;
396 uint16_t min_interval, max_interval;
399 if (hci->le_adv_enable == 0x01) {
400 cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
401 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
405 min_interval = le16_to_cpu(cmd->min_interval);
406 max_interval = le16_to_cpu(cmd->max_interval);
408 /* Valid range for advertising type is 0x00 to 0x03 */
410 case 0x00: /* ADV_IND */
411 /* Range for advertising interval min is 0x0020 to 0x4000 */
412 if (min_interval < 0x0020 || min_interval > 0x4000) {
413 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
414 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
417 /* Range for advertising interval max is 0x0020 to 0x4000 */
418 if (max_interval < 0x0020 || max_interval > 0x4000) {
419 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
420 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
423 /* Advertising interval max shall be less or equal */
424 if (min_interval > max_interval) {
425 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
426 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
431 case 0x01: /* ADV_DIRECT_IND */
432 /* Range for direct address type is 0x00 to 0x01 */
433 if (cmd->direct_addr_type > 0x01) {
434 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
435 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
440 case 0x02: /* ADV_SCAN_IND */
441 case 0x03: /* ADV_NONCONN_IND */
442 /* Range for advertising interval min is 0x00a0 to 0x4000 */
443 if (min_interval < 0x00a0 || min_interval > 0x4000) {
444 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
445 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
448 /* Range for advertising interval max is 0x00a0 to 0x4000 */
449 if (max_interval < 0x00a0 || max_interval > 0x4000) {
450 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
451 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
454 /* Advertising interval min shall be less or equal */
455 if (min_interval > max_interval) {
456 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
457 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
463 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
464 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
468 /* Valid range for own address type is 0x00 to 0x01 */
469 if (cmd->own_addr_type > 0x01) {
470 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
471 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
475 /* Valid range for advertising channel map is 0x01 to 0x07 */
476 if (cmd->channel_map < 0x01 || cmd->channel_map > 0x07) {
477 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
478 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
482 /* Valid range for advertising filter policy is 0x00 to 0x03 */
483 if (cmd->filter_policy > 0x03) {
484 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
485 BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
489 hci->le_adv_min_interval = min_interval;
490 hci->le_adv_max_interval = max_interval;
491 hci->le_adv_type = cmd->type;
492 hci->le_adv_own_addr_type = cmd->own_addr_type;
493 hci->le_adv_direct_addr_type = cmd->direct_addr_type;
494 memcpy(hci->le_adv_direct_addr, cmd->direct_addr, 6);
495 hci->le_adv_channel_map = cmd->channel_map;
496 hci->le_adv_filter_policy = cmd->filter_policy;
498 status = BT_HCI_ERR_SUCCESS;
499 cmd_complete(hci, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
500 &status, sizeof(status));
503 static void cmd_le_read_adv_tx_power(struct bt_le *hci,
504 const void *data, uint8_t size)
506 struct bt_hci_rsp_le_read_adv_tx_power rsp;
508 rsp.status = BT_HCI_ERR_SUCCESS;
509 rsp.level = hci->le_adv_tx_power;
511 cmd_complete(hci, BT_HCI_CMD_LE_READ_ADV_TX_POWER, &rsp, sizeof(rsp));
514 static void cmd_le_set_adv_data(struct bt_le *hci,
515 const void *data, uint8_t size)
517 const struct bt_hci_cmd_le_set_adv_data *cmd = data;
520 /* Valid range for advertising data length is 0x00 to 0x1f */
521 if (cmd->len > 0x1f) {
522 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
523 BT_HCI_CMD_LE_SET_ADV_DATA);
527 hci->le_adv_data_len = cmd->len;
528 memcpy(hci->le_adv_data, cmd->data, 31);
530 status = BT_HCI_ERR_SUCCESS;
531 cmd_complete(hci, BT_HCI_CMD_LE_SET_ADV_DATA, &status, sizeof(status));
534 static void cmd_le_set_scan_rsp_data(struct bt_le *hci,
535 const void *data, uint8_t size)
537 const struct bt_hci_cmd_le_set_scan_rsp_data *cmd = data;
540 /* Valid range for scan response data length is 0x00 to 0x1f */
541 if (cmd->len > 0x1f) {
542 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
543 BT_HCI_CMD_LE_SET_SCAN_RSP_DATA);
547 hci->le_scan_rsp_data_len = cmd->len;
548 memcpy(hci->le_scan_rsp_data, cmd->data, 31);
550 status = BT_HCI_ERR_SUCCESS;
551 cmd_complete(hci, BT_HCI_CMD_LE_SET_SCAN_RSP_DATA,
552 &status, sizeof(status));
555 static void cmd_le_set_adv_enable(struct bt_le *hci,
556 const void *data, uint8_t size)
558 const struct bt_hci_cmd_le_set_adv_enable *cmd = data;
561 /* Valid range for advertising enable is 0x00 to 0x01 */
562 if (cmd->enable > 0x01) {
563 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
564 BT_HCI_CMD_LE_SET_ADV_ENABLE);
568 if (cmd->enable == hci->le_adv_enable) {
569 cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
570 BT_HCI_CMD_LE_SET_ADV_ENABLE);
574 hci->le_adv_enable = cmd->enable;
576 status = BT_HCI_ERR_SUCCESS;
577 cmd_complete(hci, BT_HCI_CMD_LE_SET_ADV_ENABLE,
578 &status, sizeof(status));
581 static void cmd_le_read_white_list_size(struct bt_le *hci,
582 const void *data, uint8_t size)
584 struct bt_hci_rsp_le_read_white_list_size rsp;
586 rsp.status = BT_HCI_ERR_SUCCESS;
587 rsp.size = hci->le_white_list_size;
589 cmd_complete(hci, BT_HCI_CMD_LE_READ_WHITE_LIST_SIZE,
593 static void cmd_le_clear_white_list(struct bt_le *hci,
594 const void *data, uint8_t size)
598 status = BT_HCI_ERR_SUCCESS;
599 cmd_complete(hci, BT_HCI_CMD_LE_CLEAR_WHITE_LIST,
600 &status, sizeof(status));
603 static void cmd_le_encrypt(struct bt_le *hci, const void *data, uint8_t size)
605 const struct bt_hci_cmd_le_encrypt *cmd = data;
606 struct bt_hci_rsp_le_encrypt rsp;
608 if (!bt_crypto_e(hci->crypto, cmd->key, cmd->plaintext, rsp.data)) {
609 cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
610 BT_HCI_CMD_LE_ENCRYPT);
614 rsp.status = BT_HCI_ERR_SUCCESS;
616 cmd_complete(hci, BT_HCI_CMD_LE_ENCRYPT, &rsp, sizeof(rsp));
619 static void cmd_le_rand(struct bt_le *hci, const void *data, uint8_t size)
621 struct bt_hci_rsp_le_rand rsp;
624 if (!bt_crypto_random_bytes(hci->crypto, value, 8)) {
625 cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
630 rsp.status = BT_HCI_ERR_SUCCESS;
631 memcpy(&rsp.number, value, 8);
633 cmd_complete(hci, BT_HCI_CMD_LE_RAND, &rsp, sizeof(rsp));
636 static void cmd_le_read_supported_states(struct bt_le *hci,
637 const void *data, uint8_t size)
639 struct bt_hci_rsp_le_read_supported_states rsp;
641 rsp.status = BT_HCI_ERR_SUCCESS;
642 memcpy(rsp.states, hci->le_states, 8);
644 cmd_complete(hci, BT_HCI_CMD_LE_READ_SUPPORTED_STATES,
648 static const struct {
650 void (*func) (struct bt_le *hci, const void *data, uint8_t size);
654 { BT_HCI_CMD_SET_EVENT_MASK, cmd_set_event_mask, 8, true },
655 { BT_HCI_CMD_RESET, cmd_reset, 0, true },
656 { BT_HCI_CMD_READ_LOCAL_VERSION, cmd_read_local_version, 0, true },
657 { BT_HCI_CMD_READ_LOCAL_COMMANDS, cmd_read_local_commands, 0, true },
658 { BT_HCI_CMD_READ_LOCAL_FEATURES, cmd_read_local_features, 0, true },
659 { BT_HCI_CMD_READ_BUFFER_SIZE, cmd_read_buffer_size, 0, true },
660 { BT_HCI_CMD_READ_BD_ADDR, cmd_read_bd_addr, 0, true },
662 { BT_HCI_CMD_LE_SET_EVENT_MASK,
663 cmd_le_set_event_mask, 8, true },
664 { BT_HCI_CMD_LE_READ_BUFFER_SIZE,
665 cmd_le_read_buffer_size, 0, true },
666 { BT_HCI_CMD_LE_READ_LOCAL_FEATURES,
667 cmd_le_read_local_features, 0, true },
668 { BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
669 cmd_le_set_random_address, 6, true },
670 { BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
671 cmd_le_set_adv_parameters, 15, true },
672 { BT_HCI_CMD_LE_READ_ADV_TX_POWER,
673 cmd_le_read_adv_tx_power, 0, true },
674 { BT_HCI_CMD_LE_SET_ADV_DATA,
675 cmd_le_set_adv_data, 32, true },
676 { BT_HCI_CMD_LE_SET_SCAN_RSP_DATA,
677 cmd_le_set_scan_rsp_data, 32, true },
678 { BT_HCI_CMD_LE_SET_ADV_ENABLE,
679 cmd_le_set_adv_enable, 1, true },
681 { BT_HCI_CMD_LE_READ_WHITE_LIST_SIZE,
682 cmd_le_read_white_list_size, 0, true },
683 { BT_HCI_CMD_LE_CLEAR_WHITE_LIST,
684 cmd_le_clear_white_list, 0, true },
686 { BT_HCI_CMD_LE_ENCRYPT, cmd_le_encrypt, 32, true },
687 { BT_HCI_CMD_LE_RAND, cmd_le_rand, 0, true },
689 { BT_HCI_CMD_LE_READ_SUPPORTED_STATES,
690 cmd_le_read_supported_states, 0, true },
695 static void process_command(struct bt_le *hci, const void *data, size_t size)
697 const struct bt_hci_cmd_hdr *hdr = data;
701 if (size < sizeof(*hdr))
704 data += sizeof(*hdr);
705 size -= sizeof(*hdr);
707 opcode = le16_to_cpu(hdr->opcode);
709 if (hdr->plen != size) {
710 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS, opcode);
714 for (i = 0; cmd_table[i].func; i++) {
715 if (cmd_table[i].opcode != opcode)
718 if ((cmd_table[i].fixed && size != cmd_table[i].size) ||
719 size < cmd_table[i].size) {
720 cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS, opcode);
724 cmd_table[i].func(hci, data, size);
728 cmd_status(hci, BT_HCI_ERR_UNKNOWN_COMMAND, opcode);
731 static void vhci_read_callback(int fd, uint32_t events, void *user_data)
733 struct bt_le *hci = user_data;
734 unsigned char buf[4096];
737 if (events & (EPOLLERR | EPOLLHUP))
740 len = read(hci->vhci_fd, buf, sizeof(buf));
746 process_command(hci, buf + 1, len - 1);
751 struct bt_le *bt_le_new(void)
753 unsigned char setup_cmd[2];
756 hci = calloc(1, sizeof(*hci));
762 hci->vhci_fd = open("/dev/vhci", O_RDWR);
763 if (hci->vhci_fd < 0) {
768 setup_cmd[0] = HCI_VENDOR_PKT;
769 setup_cmd[1] = HCI_BREDR;
771 if (write(hci->vhci_fd, setup_cmd, sizeof(setup_cmd)) < 0) {
777 mainloop_add_fd(hci->vhci_fd, EPOLLIN, vhci_read_callback, hci, NULL);
779 hci->crypto = bt_crypto_new();
781 return bt_le_ref(hci);
784 struct bt_le *bt_le_ref(struct bt_le *hci)
789 __sync_fetch_and_add(&hci->ref_count, 1);
794 void bt_le_unref(struct bt_le *hci)
799 if (__sync_sub_and_fetch(&hci->ref_count, 1))
802 bt_crypto_unref(hci->crypto);
804 mainloop_remove_fd(hci->vhci_fd);