vhci: Use io.h instead of mainloop.h

The likes of mainloop_add_fd is not implemented in mainloop-glib.c while
io_set_read_handler so this makes it possible to use vhci instance with
both libshared-glib and libshared-mainloop.
This commit is contained in:
Luiz Augusto von Dentz 2021-10-14 16:29:13 -07:00
parent 16f01989c2
commit 66573e120f

View File

@ -24,14 +24,14 @@
#include "lib/bluetooth.h" #include "lib/bluetooth.h"
#include "lib/hci.h" #include "lib/hci.h"
#include "src/shared/mainloop.h" #include "src/shared/io.h"
#include "monitor/bt.h" #include "monitor/bt.h"
#include "btdev.h" #include "btdev.h"
#include "vhci.h" #include "vhci.h"
struct vhci { struct vhci {
enum btdev_type type; enum btdev_type type;
int fd; struct io *io;
struct btdev *btdev; struct btdev *btdev;
}; };
@ -40,8 +40,7 @@ static void vhci_destroy(void *user_data)
struct vhci *vhci = user_data; struct vhci *vhci = user_data;
btdev_destroy(vhci->btdev); btdev_destroy(vhci->btdev);
io_destroy(vhci->io);
close(vhci->fd);
free(vhci); free(vhci);
} }
@ -52,23 +51,21 @@ static void vhci_write_callback(const struct iovec *iov, int iovlen,
struct vhci *vhci = user_data; struct vhci *vhci = user_data;
ssize_t written; ssize_t written;
written = writev(vhci->fd, iov, iovlen); written = io_send(vhci->io, iov, iovlen);
if (written < 0) if (written < 0)
return; return;
} }
static void vhci_read_callback(int fd, uint32_t events, void *user_data) static bool vhci_read_callback(struct io *io, void *user_data)
{ {
struct vhci *vhci = user_data; struct vhci *vhci = user_data;
int fd = io_get_fd(vhci->io);
unsigned char buf[4096]; unsigned char buf[4096];
ssize_t len; ssize_t len;
if (events & (EPOLLERR | EPOLLHUP)) len = read(fd, buf, sizeof(buf));
return;
len = read(vhci->fd, buf, sizeof(buf));
if (len < 1) if (len < 1)
return; return false;
switch (buf[0]) { switch (buf[0]) {
case BT_H4_CMD_PKT: case BT_H4_CMD_PKT:
@ -78,6 +75,8 @@ static void vhci_read_callback(int fd, uint32_t events, void *user_data)
btdev_receive_h4(vhci->btdev, buf, len); btdev_receive_h4(vhci->btdev, buf, len);
break; break;
} }
return true;
} }
bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback, bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback,
@ -105,20 +104,12 @@ struct vhci *vhci_open(uint8_t type)
struct vhci *vhci; struct vhci *vhci;
struct vhci_create_req req; struct vhci_create_req req;
struct vhci_create_rsp rsp; struct vhci_create_rsp rsp;
int fd;
vhci = malloc(sizeof(*vhci)); fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
if (!vhci) if (fd < 0)
return NULL; return NULL;
memset(vhci, 0, sizeof(*vhci));
vhci->type = type;
vhci->fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
if (vhci->fd < 0) {
free(vhci);
return NULL;
}
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
req.pkt_type = HCI_VENDOR_PKT; req.pkt_type = HCI_VENDOR_PKT;
@ -131,34 +122,38 @@ struct vhci *vhci_open(uint8_t type)
break; break;
} }
if (write(vhci->fd, &req, sizeof(req)) < 0) { if (write(fd, &req, sizeof(req)) < 0) {
close(vhci->fd); close(fd);
free(vhci);
return NULL; return NULL;
} }
memset(&rsp, 0, sizeof(rsp)); memset(&rsp, 0, sizeof(rsp));
if (read(vhci->fd, &rsp, sizeof(rsp)) < 0) { if (read(fd, &rsp, sizeof(rsp)) < 0) {
close(vhci->fd); close(fd);
free(vhci);
return NULL; return NULL;
} }
vhci = malloc(sizeof(*vhci));
if (!vhci)
return NULL;
memset(vhci, 0, sizeof(*vhci));
vhci->type = type;
vhci->io = io_new(fd);
io_set_close_on_destroy(vhci->io, true);
vhci->btdev = btdev_create(type, rsp.index); vhci->btdev = btdev_create(type, rsp.index);
if (!vhci->btdev) { if (!vhci->btdev) {
close(vhci->fd); vhci_destroy(vhci);
free(vhci);
return NULL; return NULL;
} }
btdev_set_send_handler(vhci->btdev, vhci_write_callback, vhci); btdev_set_send_handler(vhci->btdev, vhci_write_callback, vhci);
if (mainloop_add_fd(vhci->fd, EPOLLIN, vhci_read_callback, if (!io_set_read_handler(vhci->io, vhci_read_callback, vhci, NULL)) {
vhci, vhci_destroy) < 0) { vhci_destroy(vhci);
btdev_destroy(vhci->btdev);
close(vhci->fd);
free(vhci);
return NULL; return NULL;
} }
@ -170,7 +165,7 @@ void vhci_close(struct vhci *vhci)
if (!vhci) if (!vhci)
return; return;
mainloop_remove_fd(vhci->fd); vhci_destroy(vhci);
} }
struct btdev *vhci_get_btdev(struct vhci *vhci) struct btdev *vhci_get_btdev(struct vhci *vhci)