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
37
#include "lib/bluetooth.h"
40
#include "src/shared/mainloop.h"
41
#include "monitor/bt.h"
45
#define uninitialized_var(x) x = x
53
static void vhci_destroy(void *user_data)
55
struct vhci *vhci = user_data;
57
btdev_destroy(vhci->btdev);
64
static void vhci_write_callback(const struct iovec *iov, int iovlen,
67
struct vhci *vhci = user_data;
70
written = writev(vhci->fd, iov, iovlen);
75
static void vhci_read_callback(int fd, uint32_t events, void *user_data)
77
struct vhci *vhci = user_data;
78
unsigned char buf[4096];
81
if (events & (EPOLLERR | EPOLLHUP))
84
len = read(vhci->fd, buf, sizeof(buf));
92
btdev_receive_h4(vhci->btdev, buf, len);
97
struct vhci *vhci_open(enum vhci_type type)
100
enum btdev_type uninitialized_var(btdev_type);
101
unsigned char uninitialized_var(ctrl_type);
102
unsigned char setup_cmd[2];
103
static uint8_t id = 0x23;
106
case VHCI_TYPE_BREDRLE:
107
btdev_type = BTDEV_TYPE_BREDRLE;
108
ctrl_type = HCI_BREDR;
110
case VHCI_TYPE_BREDR:
111
btdev_type = BTDEV_TYPE_BREDR;
112
ctrl_type = HCI_BREDR;
115
btdev_type = BTDEV_TYPE_LE;
116
ctrl_type = HCI_BREDR;
119
btdev_type = BTDEV_TYPE_AMP;
124
vhci = malloc(sizeof(*vhci));
128
memset(vhci, 0, sizeof(*vhci));
131
vhci->fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
137
setup_cmd[0] = HCI_VENDOR_PKT;
138
setup_cmd[1] = ctrl_type;
140
if (write(vhci->fd, setup_cmd, sizeof(setup_cmd)) < 0) {
146
vhci->btdev = btdev_create(btdev_type, id++);
153
btdev_set_send_handler(vhci->btdev, vhci_write_callback, vhci);
155
if (mainloop_add_fd(vhci->fd, EPOLLIN, vhci_read_callback,
156
vhci, vhci_destroy) < 0) {
157
btdev_destroy(vhci->btdev);
166
void vhci_close(struct vhci *vhci)
171
mainloop_remove_fd(vhci->fd);