3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/socket.h>
34 #include "monitor/bt.h"
35 #include "src/shared/mainloop.h"
36 #include "src/shared/io.h"
37 #include "src/shared/util.h"
38 #include "src/shared/queue.h"
39 #include "src/shared/hci.h"
43 sa_family_t hci_family;
44 unsigned short hci_dev;
45 unsigned short hci_channel;
47 #define HCI_CHANNEL_RAW 0
48 #define HCI_CHANNEL_USER 1
54 uint32_t event_mask[2];
64 unsigned int next_cmd_id;
65 unsigned int next_evt_id;
66 struct queue *cmd_queue;
67 struct queue *rsp_queue;
68 struct queue *evt_list;
76 bt_hci_callback_func_t callback;
77 bt_hci_destroy_func_t destroy;
84 bt_hci_callback_func_t callback;
85 bt_hci_destroy_func_t destroy;
89 static void cmd_free(void *data)
91 struct cmd *cmd = data;
94 cmd->destroy(cmd->user_data);
100 static void evt_free(void *data)
102 struct evt *evt = data;
105 evt->destroy(evt->user_data);
110 static void send_command(struct bt_hci *hci, uint16_t opcode,
111 void *data, uint8_t size)
113 uint8_t type = BT_H4_CMD_PKT;
114 struct bt_hci_cmd_hdr hdr;
118 if (hci->num_cmds < 1)
121 hdr.opcode = cpu_to_le16(opcode);
124 iov[0].iov_base = &type;
126 iov[1].iov_base = &hdr;
127 iov[1].iov_len = sizeof(hdr);
130 iov[2].iov_base = data;
131 iov[2].iov_len = size;
136 if (io_send(hci->io, iov, iovcnt) < 0)
142 static bool io_write_callback(struct io *io, void *user_data)
144 struct bt_hci *hci = user_data;
147 cmd = queue_pop_head(hci->cmd_queue);
149 send_command(hci, cmd->opcode, cmd->data, cmd->size);
150 queue_push_tail(hci->rsp_queue, cmd);
153 hci->writer_active = false;
158 static void wakeup_writer(struct bt_hci *hci)
160 if (hci->writer_active)
163 if (hci->num_cmds < 1)
166 if (queue_isempty(hci->cmd_queue))
169 if (!io_set_write_handler(hci->io, io_write_callback, hci, NULL))
172 hci->writer_active = true;
175 static bool match_cmd_opcode(const void *a, const void *b)
177 const struct cmd *cmd = a;
178 uint16_t opcode = PTR_TO_UINT(b);
180 return cmd->opcode == opcode;
183 static void process_response(struct bt_hci *hci, uint16_t opcode,
184 const void *data, size_t size)
188 if (opcode == BT_HCI_CMD_NOP)
191 cmd = queue_remove_if(hci->rsp_queue, match_cmd_opcode,
192 UINT_TO_PTR(opcode));
197 cmd->callback(data, size, cmd->user_data);
205 static void process_notify(void *data, void *user_data)
207 struct bt_hci_evt_hdr *hdr = user_data;
208 struct evt *evt = data;
210 if (evt->event == hdr->evt)
211 evt->callback(user_data + sizeof(struct bt_hci_evt_hdr),
212 hdr->plen, evt->user_data);
215 static void process_event(struct bt_hci *hci, const void *data, size_t size)
217 const struct bt_hci_evt_hdr *hdr = data;
218 const struct bt_hci_evt_cmd_complete *cc;
219 const struct bt_hci_evt_cmd_status *cs;
221 if (size < sizeof(struct bt_hci_evt_hdr))
224 data += sizeof(struct bt_hci_evt_hdr);
225 size -= sizeof(struct bt_hci_evt_hdr);
227 if (hdr->plen != size)
231 case BT_HCI_EVT_CMD_COMPLETE:
232 if (size < sizeof(*cc))
235 hci->num_cmds = cc->ncmd;
236 process_response(hci, le16_to_cpu(cc->opcode),
241 case BT_HCI_EVT_CMD_STATUS:
242 if (size < sizeof(*cs))
245 hci->num_cmds = cs->ncmd;
246 process_response(hci, le16_to_cpu(cs->opcode), &cs->status, 1);
250 queue_foreach(hci->evt_list, process_notify, (void *) hdr);
255 static bool io_read_callback(struct io *io, void *user_data)
257 struct bt_hci *hci = user_data;
262 fd = io_get_fd(hci->io);
269 len = read(fd, buf, sizeof(buf));
278 process_event(hci, buf + 1, len - 1);
285 static struct bt_hci *create_hci(int fd)
292 hci = new0(struct bt_hci, 1);
296 hci->io = io_new(fd);
302 hci->is_stream = true;
303 hci->writer_active = false;
305 hci->next_cmd_id = 1;
306 hci->next_evt_id = 1;
308 hci->cmd_queue = queue_new();
309 if (!hci->cmd_queue) {
315 hci->rsp_queue = queue_new();
316 if (!hci->rsp_queue) {
317 queue_destroy(hci->cmd_queue, NULL);
323 hci->evt_list = queue_new();
324 if (!hci->evt_list) {
325 queue_destroy(hci->rsp_queue, NULL);
326 queue_destroy(hci->cmd_queue, NULL);
332 if (!io_set_read_handler(hci->io, io_read_callback, hci, NULL)) {
333 queue_destroy(hci->evt_list, NULL);
334 queue_destroy(hci->rsp_queue, NULL);
335 queue_destroy(hci->cmd_queue, NULL);
341 return bt_hci_ref(hci);
344 struct bt_hci *bt_hci_new(int fd)
348 hci = create_hci(fd);
355 static int create_socket(uint16_t index, uint16_t channel)
357 struct sockaddr_hci addr;
360 fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
365 memset(&addr, 0, sizeof(addr));
366 addr.hci_family = AF_BLUETOOTH;
367 addr.hci_dev = index;
368 addr.hci_channel = channel;
370 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
378 struct bt_hci *bt_hci_new_user_channel(uint16_t index)
383 fd = create_socket(index, HCI_CHANNEL_USER);
387 hci = create_hci(fd);
393 hci->is_stream = false;
395 bt_hci_set_close_on_unref(hci, true);
400 struct bt_hci *bt_hci_new_raw_device(uint16_t index)
403 struct hci_filter flt;
406 fd = create_socket(index, HCI_CHANNEL_RAW);
410 memset(&flt, 0, sizeof(flt));
411 flt.type_mask = 1 << BT_H4_EVT_PKT;
412 flt.event_mask[0] = 0xffffffff;
413 flt.event_mask[1] = 0xffffffff;
415 if (setsockopt(fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
420 hci = create_hci(fd);
426 hci->is_stream = false;
428 bt_hci_set_close_on_unref(hci, true);
433 struct bt_hci *bt_hci_ref(struct bt_hci *hci)
438 __sync_fetch_and_add(&hci->ref_count, 1);
443 void bt_hci_unref(struct bt_hci *hci)
448 if (__sync_sub_and_fetch(&hci->ref_count, 1))
451 queue_destroy(hci->evt_list, evt_free);
452 queue_destroy(hci->cmd_queue, cmd_free);
453 queue_destroy(hci->rsp_queue, cmd_free);
460 bool bt_hci_set_close_on_unref(struct bt_hci *hci, bool do_close)
465 return io_set_close_on_destroy(hci->io, do_close);
468 unsigned int bt_hci_send(struct bt_hci *hci, uint16_t opcode,
469 const void *data, uint8_t size,
470 bt_hci_callback_func_t callback,
471 void *user_data, bt_hci_destroy_func_t destroy)
478 cmd = new0(struct cmd, 1);
482 cmd->opcode = opcode;
486 cmd->data = malloc(cmd->size);
492 memcpy(cmd->data, data, cmd->size);
495 if (hci->next_cmd_id < 1)
496 hci->next_cmd_id = 1;
498 cmd->id = hci->next_cmd_id++;
500 cmd->callback = callback;
501 cmd->destroy = destroy;
502 cmd->user_data = user_data;
504 if (!queue_push_tail(hci->cmd_queue, cmd)) {
515 static bool match_cmd_id(const void *a, const void *b)
517 const struct cmd *cmd = a;
518 unsigned int id = PTR_TO_UINT(b);
520 return cmd->id == id;
523 bool bt_hci_cancel(struct bt_hci *hci, unsigned int id)
530 cmd = queue_remove_if(hci->cmd_queue, match_cmd_id, UINT_TO_PTR(id));
532 cmd = queue_remove_if(hci->rsp_queue, match_cmd_id,
545 bool bt_hci_flush(struct bt_hci *hci)
550 if (hci->writer_active) {
551 io_set_write_handler(hci->io, NULL, NULL, NULL);
552 hci->writer_active = false;
555 queue_remove_all(hci->cmd_queue, NULL, NULL, cmd_free);
556 queue_remove_all(hci->rsp_queue, NULL, NULL, cmd_free);
561 unsigned int bt_hci_register(struct bt_hci *hci, uint8_t event,
562 bt_hci_callback_func_t callback,
563 void *user_data, bt_hci_destroy_func_t destroy)
570 evt = new0(struct evt, 1);
576 if (hci->next_evt_id < 1)
577 hci->next_evt_id = 1;
579 evt->id = hci->next_evt_id++;
581 evt->callback = callback;
582 evt->destroy = destroy;
583 evt->user_data = user_data;
585 if (!queue_push_tail(hci->evt_list, evt)) {
593 static bool match_evt_id(const void *a, const void *b)
595 const struct evt *evt = a;
596 unsigned int id = PTR_TO_UINT(b);
598 return evt->id == id;
601 bool bt_hci_unregister(struct bt_hci *hci, unsigned int id)
608 evt = queue_remove_if(hci->evt_list, match_evt_id, UINT_TO_PTR(id));