#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
+#include <sys/uio.h>
#include "src/shared/mainloop.h"
#include "src/shared/util.h"
#include "serial.h"
#include "server.h"
+#include "btdev.h"
#include "vhci.h"
#include "amp.h"
#include "le.h"
int letest_count = 0;
int amptest_count = 0;
int vhci_count = 0;
- enum vhci_type vhci_type = VHCI_TYPE_BREDRLE;
+ enum btdev_type type = BTDEV_TYPE_BREDRLE52;
int i;
mainloop_init();
vhci_count = 1;
break;
case 'L':
- vhci_type = VHCI_TYPE_LE;
+ type = BTDEV_TYPE_LE;
break;
case 'B':
- vhci_type = VHCI_TYPE_BREDR;
+ type = BTDEV_TYPE_BREDR;
break;
case 'A':
- vhci_type = VHCI_TYPE_AMP;
+ type = BTDEV_TYPE_AMP;
break;
case 'U':
if (optarg)
for (i = 0; i < vhci_count; i++) {
struct vhci *vhci;
- vhci = vhci_open(vhci_type);
+ vhci = vhci_open(type);
if (!vhci) {
fprintf(stderr, "Failed to open Virtual HCI device\n");
return EXIT_FAILURE;
#include "btdev.h"
#include "vhci.h"
-#define uninitialized_var(x) x = x
-
struct vhci {
- enum vhci_type type;
+ enum btdev_type type;
int fd;
struct btdev *btdev;
};
return btdev_set_debug(vhci->btdev, callback, user_data, destroy);
}
-struct vhci *vhci_open(enum vhci_type type)
+struct vhci_create_req {
+ uint8_t pkt_type;
+ uint8_t opcode;
+} __attribute__((packed));
+
+struct vhci_create_rsp {
+ uint8_t pkt_type;
+ uint8_t opcode;
+ uint16_t index;
+} __attribute__((packed));
+
+struct vhci *vhci_open(uint8_t type)
{
struct vhci *vhci;
- enum btdev_type uninitialized_var(btdev_type);
- unsigned char uninitialized_var(ctrl_type);
- unsigned char setup_cmd[2];
- static uint8_t id = 0x23;
-
- switch (type) {
- case VHCI_TYPE_BREDRLE:
- btdev_type = BTDEV_TYPE_BREDRLE52;
- ctrl_type = HCI_PRIMARY;
- break;
- case VHCI_TYPE_BREDR:
- btdev_type = BTDEV_TYPE_BREDR;
- ctrl_type = HCI_PRIMARY;
- break;
- case VHCI_TYPE_LE:
- btdev_type = BTDEV_TYPE_LE;
- ctrl_type = HCI_PRIMARY;
- break;
- case VHCI_TYPE_AMP:
- btdev_type = BTDEV_TYPE_AMP;
- ctrl_type = HCI_AMP;
- break;
- }
+ struct vhci_create_req req;
+ struct vhci_create_rsp rsp;
vhci = malloc(sizeof(*vhci));
if (!vhci)
return NULL;
}
- setup_cmd[0] = HCI_VENDOR_PKT;
- setup_cmd[1] = ctrl_type;
+ memset(&req, 0, sizeof(req));
+ req.pkt_type = HCI_VENDOR_PKT;
- if (write(vhci->fd, setup_cmd, sizeof(setup_cmd)) < 0) {
+ switch (type) {
+ case BTDEV_TYPE_AMP:
+ req.opcode = HCI_AMP;
+ break;
+ default:
+ req.opcode = HCI_PRIMARY;
+ break;
+ }
+
+ if (write(vhci->fd, &req, sizeof(req)) < 0) {
+ close(vhci->fd);
+ free(vhci);
+ return NULL;
+ }
+
+ memset(&rsp, 0, sizeof(rsp));
+
+ if (read(vhci->fd, &rsp, sizeof(rsp)) < 0) {
close(vhci->fd);
free(vhci);
return NULL;
}
- vhci->btdev = btdev_create(btdev_type, id++);
+ vhci->btdev = btdev_create(type, rsp.index);
if (!vhci->btdev) {
close(vhci->fd);
free(vhci);
mainloop_remove_fd(vhci->fd);
}
+
+struct btdev *vhci_get_btdev(struct vhci *vhci)
+{
+ if (!vhci)
+ return NULL;
+
+ return vhci->btdev;
+}
#include <stdint.h>
-enum vhci_type {
- VHCI_TYPE_BREDRLE,
- VHCI_TYPE_BREDR,
- VHCI_TYPE_LE,
- VHCI_TYPE_AMP,
-};
-
struct vhci;
typedef void (*vhci_debug_func_t)(const char *str, void *user_data);
bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback,
void *user_data, vhci_destroy_func_t destroy);
-struct vhci *vhci_open(enum vhci_type type);
+struct vhci *vhci_open(uint8_t type);
void vhci_close(struct vhci *vhci);
+
+struct btdev *vhci_get_btdev(struct vhci *vhci);