112
104
static uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e, 0x6f, 0x6f, 0x70, 0x00 };
114
static GMainLoop *event_loop;
116
static volatile sig_atomic_t __io_canceled;
118
static inline void io_init(void)
123
static inline void io_cancel(void)
106
#define MAX_EPOLL_EVENTS 10
110
static volatile sig_atomic_t __io_canceled = 0;
112
static void sig_term(int sig)
125
114
__io_canceled = 1;
128
static void sig_term(int sig)
131
g_main_loop_quit(event_loop);
134
static gboolean io_acl_data(GIOChannel *chan, GIOCondition cond, gpointer data);
135
static gboolean io_conn_ind(GIOChannel *chan, GIOCondition cond, gpointer data);
136
static gboolean io_hci_data(GIOChannel *chan, GIOCondition cond, gpointer data);
138
117
static inline int read_n(int fd, void *buf, int len)
140
119
register int w, t = 0;
412
394
write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
414
if (write(vdev.fd, buf, ptr - buf) < 0)
396
if (write(vdev.dev_fd, buf, ptr - buf) < 0)
415
397
syslog(LOG_ERR, "Can't send event: %s (%d)",
416
398
strerror(errno), errno);
419
401
static int scan_enable(uint8_t *data)
403
struct epoll_event scan_event;
421
404
struct sockaddr_in sa;
426
408
if (!(*data & SCAN_PAGE)) {
428
g_io_channel_shutdown(vdev.scan, TRUE, NULL);
409
if (vdev.scan_fd >= 0) {
416
if (vdev.scan_fd >= 0)
437
419
if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
783
static void hci_status_param(uint16_t ocf, int plen, uint8_t *data)
785
read_local_amp_info_rp ai;
788
const uint16_t ogf = OGF_STATUS_PARAM;
791
case OCF_READ_LOCAL_AMP_INFO:
792
memset(&ai, 0, sizeof(ai));
795
ai.amp_status = 0x01;
796
ai.max_pdu_size = htobl(L2CAP_DEFAULT_MTU);
797
ai.controller_type = HCI_AMP;
798
ai.max_amp_assoc_length = htobl(HCI_MAX_ACL_SIZE);
799
/* No flushing at all */
800
ai.max_flush_timeout = 0xFFFFFFFF;
801
ai.best_effort_flush_timeout = 0xFFFFFFFF;
803
command_complete(ogf, ocf, sizeof(ai), &ai);
808
command_complete(ogf, ocf, 1, &status);
801
813
static void hci_command(uint8_t *data)
803
815
hci_command_hdr *ch;
860
static gboolean io_acl_data(GIOChannel *chan, GIOCondition cond, gpointer data)
875
static void io_acl_data(void *data)
862
struct vhci_conn *conn = (struct vhci_conn *) data;
877
struct vhci_conn *conn = data;
863
878
unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
868
if (cond & G_IO_NVAL) {
869
g_io_channel_unref(chan);
873
if (cond & G_IO_HUP) {
874
close_connection(conn);
878
fd = g_io_channel_unix_get_fd(chan);
881
if (read_n(fd, ptr, HCI_ACL_HDR_SIZE) <= 0) {
884
if (read_n(conn->fd, ptr, HCI_ACL_HDR_SIZE) <= 0) {
882
885
close_connection(conn);
886
889
ah = (void *) ptr;
887
890
ptr += HCI_ACL_HDR_SIZE;
889
892
len = btohs(ah->dlen);
890
if (read_n(fd, ptr, len) <= 0) {
893
if (read_n(conn->fd, ptr, len) <= 0) {
891
894
close_connection(conn);
895
898
buf[0] = HCI_ACLDATA_PKT;
901
904
write_snoop(vdev.dd, HCI_ACLDATA_PKT, 1, buf, len);
903
if (write(vdev.fd, buf, len) < 0)
906
if (write(vdev.dev_fd, buf, len) < 0)
907
syslog(LOG_ERR, "ACL data write error");
909
static gboolean io_conn_ind(GIOChannel *chan, GIOCondition cond, gpointer data)
911
static void io_conn_ind(void)
911
913
struct vhci_link_info info;
912
914
struct vhci_conn *conn;
913
915
struct sockaddr_in sa;
917
if (cond & G_IO_NVAL)
920
sk = g_io_channel_unix_get_fd(chan);
922
919
len = sizeof(sa);
923
if ((nsk = accept(sk, (struct sockaddr *) &sa, &len)) < 0)
920
if ((nsk = accept(vdev.scan_fd, (struct sockaddr *) &sa, &len)) < 0)
926
923
if (read_n(nsk, &info, sizeof(info)) < 0) {
927
924
syslog(LOG_ERR, "Can't read link info");
931
928
if (!(conn = malloc(sizeof(*conn)))) {
932
929
syslog(LOG_ERR, "Can't alloc new connection");
937
934
bacpy(&conn->dest, &info.bdaddr);
943
940
syslog(LOG_ERR, "Too many connections");
950
947
conn->handle = h + 1;
951
conn->chan = g_io_channel_unix_new(nsk);
952
949
connect_request(conn);
957
static gboolean io_hci_data(GIOChannel *chan, GIOCondition cond, gpointer data)
952
static void io_hci_data(void)
959
954
unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
966
fd = g_io_channel_unix_get_fd(chan);
968
len = read(fd, buf, sizeof(buf));
960
len = read(vdev.dev_fd, buf, sizeof(buf));
970
962
if (errno == EAGAIN)
973
965
syslog(LOG_ERR, "Read failed: %s (%d)", strerror(errno), errno);
974
g_io_channel_unref(chan);
975
g_main_loop_quit(event_loop);
1043
static void rewrite_bdaddr(unsigned char *buf, int len, bdaddr_t *bdaddr)
1046
unsigned char *ptr = buf;
1052
if (!bacmp(bdaddr, BDADDR_ANY))
1059
eh = (hci_event_hdr *) ptr;
1060
ptr += HCI_EVENT_HDR_SIZE;
1062
if (eh->evt == EVT_CMD_COMPLETE) {
1063
evt_cmd_complete *cc = (void *) ptr;
1065
ptr += EVT_CMD_COMPLETE_SIZE;
1067
if (cc->opcode == htobs(cmd_opcode_pack(OGF_INFO_PARAM,
1068
OCF_READ_BD_ADDR))) {
1069
bacpy((bdaddr_t *) (ptr + 1), bdaddr);
1076
static int run_proxy(int fd, int dev, bdaddr_t *bdaddr)
1078
unsigned char buf[HCI_MAX_FRAME_SIZE + 1];
1079
struct hci_dev_info di;
1080
struct hci_filter flt;
1082
int dd, err, len, need_raw;
1084
dd = hci_open_dev(dev);
1086
syslog(LOG_ERR, "Can't open device hci%d: %s (%d)",
1087
dev, strerror(errno), errno);
1091
if (hci_devinfo(dev, &di) < 0) {
1092
syslog(LOG_ERR, "Can't get device info for hci%d: %s (%d)",
1093
dev, strerror(errno), errno);
1098
need_raw = !hci_test_bit(HCI_RAW, &di.flags);
1100
hci_filter_clear(&flt);
1101
hci_filter_all_ptypes(&flt);
1102
hci_filter_all_events(&flt);
1104
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1105
syslog(LOG_ERR, "Can't set filter for hci%d: %s (%d)",
1106
dev, strerror(errno), errno);
1112
if (ioctl(dd, HCISETRAW, 1) < 0) {
1113
syslog(LOG_ERR, "Can't set raw mode on hci%d: %s (%d)",
1114
dev, strerror(errno), errno);
1121
p[0].events = POLLIN;
1123
p[1].events = POLLIN;
1125
while (!__io_canceled) {
1128
err = poll(p, 2, 500);
1134
if (p[0].revents & POLLIN) {
1135
len = read(fd, buf, sizeof(buf));
1137
rewrite_bdaddr(buf, len, bdaddr);
1138
err = write(dd, buf, len);
1142
if (p[1].revents & POLLIN) {
1143
len = read(dd, buf, sizeof(buf));
1145
rewrite_bdaddr(buf, len, bdaddr);
1146
err = write(fd, buf, len);
1152
if (ioctl(dd, HCISETRAW, 0) < 0)
1153
syslog(LOG_ERR, "Can't clear raw mode on hci%d: %s (%d)",
1154
dev, strerror(errno), errno);
1159
syslog(LOG_INFO, "Exit");
1164
1023
static void usage(void)
1166
1025
printf("hciemu - HCI emulator ver %s\n", VERSION);
1167
1026
printf("Usage: \n");
1168
printf("\thciemu [options] local_address\n"
1027
printf("\thciemu [options] port_number\n"
1170
"\t[-d device] use specified device\n"
1171
"\t[-b bdaddr] emulate specified address\n"
1029
"\t[-d device] use specified device node\n"
1172
1030
"\t[-s file] create snoop file\n"
1173
1031
"\t[-n] do not detach\n"
1174
1032
"\t[-h] help, you are looking at it\n");
1177
static struct option main_options[] = {
1035
static const struct option options[] = {
1178
1036
{ "device", 1, 0, 'd' },
1179
1037
{ "bdaddr", 1, 0, 'b' },
1180
1038
{ "snoop", 1, 0, 's' },
1181
1039
{ "nodetach", 0, 0, 'n' },
1182
1040
{ "help", 0, 0, 'h' },
1186
1044
int main(int argc, char *argv[])
1046
int exitcode = EXIT_FAILURE;
1188
1047
struct sigaction sa;
1190
1048
char *device = NULL, *snoop = NULL;
1192
int fd, dd, opt, detach = 1, dev = -1;
1194
bacpy(&bdaddr, BDADDR_ANY);
1196
while ((opt=getopt_long(argc, argv, "d:b:s:nh", main_options, NULL)) != EOF) {
1050
struct epoll_event device_event;
1051
int dd, opt, detach = 1;
1053
while ((opt=getopt_long(argc, argv, "d:s:nh", options, NULL)) != EOF) {
1199
1056
device = strdup(optarg);
1203
str2ba(optarg, &bdaddr);
1207
1059
snoop = strdup(optarg);
1259
1103
sigaction(SIGTERM, &sa, NULL);
1260
1104
sigaction(SIGINT, &sa, NULL);
1264
if (!device && dev >= 0)
1265
device = strdup(GHCI_DEV);
1107
device = strdup(VHCI_DEV);
1267
1109
/* Open and create virtual HCI device */
1269
fd = open(device, O_RDWR);
1271
syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1272
device, strerror(errno), errno);
1110
device_fd = open(device, O_RDWR);
1111
if (device_fd < 0) {
1112
syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1113
device, strerror(errno), errno);
1278
fd = open(VHCI_DEV, O_RDWR);
1280
fd = open(VHCI_UDEV, O_RDWR);
1282
syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1283
VHCI_DEV, strerror(errno), errno);
1289
1120
/* Create snoop file */
1291
1122
dd = create_snoop(snoop);
1324
1156
vdev.eir_fec = 0x00;
1325
1157
memset(vdev.eir_data, 0, sizeof(vdev.eir_data));
1159
vdev.dev_fd = device_fd;
1330
dev_io = g_io_channel_unix_new(fd);
1331
g_io_add_watch(dev_io, G_IO_IN, io_hci_data, NULL);
1162
memset(&device_event, 0, sizeof(device_event));
1163
device_event.events = EPOLLIN;
1164
device_event.data.fd = device_fd;
1166
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, device_fd, &device_event) < 0) {
1167
perror("Failed to setup device event watch");
1333
1171
setpriority(PRIO_PROCESS, 0, -19);
1335
1173
/* Start event processor */
1336
g_main_loop_run(event_loop);
1175
struct epoll_event events[MAX_EPOLL_EVENTS];
1181
nfds = epoll_wait(epoll_fd, events, MAX_EPOLL_EVENTS, -1);
1185
for (n = 0; n < nfds; n++) {
1186
if (events[n].data.fd == vdev.dev_fd)
1188
else if (events[n].data.fd == vdev.scan_fd)
1193
exitcode = EXIT_SUCCESS;
1195
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, device_fd, NULL);
1343
1205
syslog(LOG_INFO, "Exit");