3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2011-2014 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
36 #include "lib/bluetooth.h"
39 #include "src/shared/mainloop.h"
40 #include "monitor/bt.h"
44 #define uninitialized_var(x) x = x
52 static void vhci_destroy(void *user_data)
54 struct vhci *vhci = user_data;
56 btdev_destroy(vhci->btdev);
63 static void vhci_write_callback(const struct iovec *iov, int iovlen,
66 struct vhci *vhci = user_data;
69 written = writev(vhci->fd, iov, iovlen);
74 static void vhci_read_callback(int fd, uint32_t events, void *user_data)
76 struct vhci *vhci = user_data;
77 unsigned char buf[4096];
80 if (events & (EPOLLERR | EPOLLHUP))
83 len = read(vhci->fd, buf, sizeof(buf));
91 btdev_receive_h4(vhci->btdev, buf, len);
96 struct vhci *vhci_open(enum vhci_type type)
99 enum btdev_type uninitialized_var(btdev_type);
100 unsigned char uninitialized_var(ctrl_type);
101 unsigned char setup_cmd[2];
102 static uint8_t id = 0x23;
105 case VHCI_TYPE_BREDRLE:
106 btdev_type = BTDEV_TYPE_BREDRLE;
107 ctrl_type = HCI_BREDR;
109 case VHCI_TYPE_BREDR:
110 btdev_type = BTDEV_TYPE_BREDR;
111 ctrl_type = HCI_BREDR;
114 btdev_type = BTDEV_TYPE_LE;
115 ctrl_type = HCI_BREDR;
118 btdev_type = BTDEV_TYPE_AMP;
123 vhci = malloc(sizeof(*vhci));
127 memset(vhci, 0, sizeof(*vhci));
130 vhci->fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
136 setup_cmd[0] = HCI_VENDOR_PKT;
137 setup_cmd[1] = ctrl_type;
139 if (write(vhci->fd, setup_cmd, sizeof(setup_cmd)) < 0) {
145 vhci->btdev = btdev_create(btdev_type, id++);
152 btdev_set_send_handler(vhci->btdev, vhci_write_callback, vhci);
154 if (mainloop_add_fd(vhci->fd, EPOLLIN, vhci_read_callback,
155 vhci, vhci_destroy) < 0) {
156 btdev_destroy(vhci->btdev);
165 void vhci_close(struct vhci *vhci)
170 mainloop_remove_fd(vhci->fd);