~cyphermox/bluez/5.23

« back to all changes in this revision

Viewing changes to test/hciemu.c

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2012-01-24 05:35:09 UTC
  • mfrom: (1.5.11) (7.1.6 experimental)
  • Revision ID: package-import@ubuntu.com-20120124053509-uwpwqi783lz08wm3
Tags: 4.98-1
* New upstream release.
* Update debian/bluetooth-dbus.conf.
* Update debian/control.
  Add Multi-Arch: foreign to bluez.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <stdio.h>
30
30
#include <errno.h>
31
 
#include <ctype.h>
32
31
#include <fcntl.h>
33
32
#include <unistd.h>
34
33
#include <stdlib.h>
38
37
#include <getopt.h>
39
38
#include <syslog.h>
40
39
#include <sys/time.h>
41
 
#include <sys/stat.h>
42
 
#include <sys/poll.h>
43
 
#include <sys/ioctl.h>
 
40
#include <sys/epoll.h>
44
41
#include <sys/socket.h>
45
42
#include <sys/resource.h>
 
43
#include <sys/stat.h>
46
44
 
47
45
#include <bluetooth/bluetooth.h>
48
46
#include <bluetooth/hci.h>
49
47
#include <bluetooth/hci_lib.h>
50
 
 
51
 
#include <netdb.h>
52
 
 
53
 
#include <glib.h>
54
 
 
55
 
#define GHCI_DEV                "/dev/ghci"
 
48
#include <bluetooth/l2cap.h>
56
49
 
57
50
#define VHCI_DEV                "/dev/vhci"
58
 
#define VHCI_UDEV               "/dev/hci_vhci"
59
51
 
60
52
#define VHCI_MAX_CONN           12
61
53
 
71
63
        uint8_t         eir_data[HCI_MAX_EIR_LENGTH];
72
64
        uint16_t        acl_cnt;
73
65
        bdaddr_t        bdaddr;
74
 
        int             fd;
 
66
        int             dev_fd;
 
67
        int             scan_fd;
75
68
        int             dd;
76
 
        GIOChannel      *scan;
77
69
};
78
70
 
79
71
struct vhci_conn {
80
72
        bdaddr_t        dest;
81
73
        uint16_t        handle;
82
 
        GIOChannel      *chan;
 
74
        int             fd;
83
75
};
84
76
 
85
77
struct vhci_link_info {
111
103
 
112
104
static uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e, 0x6f, 0x6f, 0x70, 0x00 };
113
105
 
114
 
static GMainLoop *event_loop;
115
 
 
116
 
static volatile sig_atomic_t __io_canceled;
117
 
 
118
 
static inline void io_init(void)
119
 
{
120
 
        __io_canceled = 0;
121
 
}
122
 
 
123
 
static inline void io_cancel(void)
 
106
#define MAX_EPOLL_EVENTS 10
 
107
 
 
108
static int epoll_fd;
 
109
 
 
110
static volatile sig_atomic_t __io_canceled = 0;
 
111
 
 
112
static void sig_term(int sig)
124
113
{
125
114
        __io_canceled = 1;
126
115
}
127
116
 
128
 
static void sig_term(int sig)
129
 
{
130
 
        io_cancel();
131
 
        g_main_loop_quit(event_loop);
132
 
}
133
 
 
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);
137
 
 
138
117
static inline int read_n(int fd, void *buf, int len)
139
118
{
140
119
        register int w, t = 0;
197
176
        return fd;
198
177
}
199
178
 
200
 
static int write_snoop(int fd, int type, int incoming, unsigned char *buf, int len)
 
179
static int write_snoop(int fd, int type, int incoming,
 
180
                                unsigned char *buf, int len)
201
181
{
202
182
        struct btsnoop_pkt pkt;
203
183
        struct timeval tv;
263
243
 
264
244
        write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
265
245
 
266
 
        if (write(vdev.fd, buf, ptr - buf) < 0)
 
246
        if (write(vdev.dev_fd, buf, ptr - buf) < 0)
267
247
                syslog(LOG_ERR, "Can't send event: %s(%d)",
268
248
                                                strerror(errno), errno);
269
249
}
295
275
 
296
276
        write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
297
277
 
298
 
        if (write(vdev.fd, buf, ptr - buf) < 0)
 
278
        if (write(vdev.dev_fd, buf, ptr - buf) < 0)
299
279
                syslog(LOG_ERR, "Can't send event: %s(%d)",
300
280
                                                strerror(errno), errno);
301
281
}
323
303
 
324
304
        write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
325
305
 
326
 
        if (write(vdev.fd, buf, ptr - buf) < 0)
 
306
        if (write(vdev.dev_fd, buf, ptr - buf) < 0)
327
307
                syslog(LOG_ERR, "Can't send event: %s (%d)",
328
308
                                                strerror(errno), errno);
329
309
}
353
333
 
354
334
        write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
355
335
 
356
 
        if (write(vdev.fd, buf, ptr - buf) < 0)
 
336
        if (write(vdev.dev_fd, buf, ptr - buf) < 0)
357
337
                syslog(LOG_ERR, "Can't send event: %s (%d)",
358
338
                                                strerror(errno), errno);
 
339
 
 
340
        /* TODO: Add io_acl_data() handling */
359
341
}
360
342
 
361
343
static void disconn_complete(struct vhci_conn *conn)
381
363
 
382
364
        write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
383
365
 
384
 
        if (write(vdev.fd, buf, ptr - buf) < 0)
 
366
        if (write(vdev.dev_fd, buf, ptr - buf) < 0)
385
367
                syslog(LOG_ERR, "Can't send event: %s (%d)",
386
368
                                                strerror(errno), errno);
387
369
 
411
393
 
412
394
        write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
413
395
 
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);
417
399
}
418
400
 
419
401
static int scan_enable(uint8_t *data)
420
402
{
 
403
        struct epoll_event scan_event;
421
404
        struct sockaddr_in sa;
422
 
        GIOChannel *sk_io;
423
405
        bdaddr_t ba;
424
406
        int sk, opt;
425
407
 
426
408
        if (!(*data & SCAN_PAGE)) {
427
 
                if (vdev.scan) {
428
 
                        g_io_channel_shutdown(vdev.scan, TRUE, NULL);
429
 
                        vdev.scan = NULL;
 
409
                if (vdev.scan_fd >= 0) {
 
410
                        close(vdev.scan_fd);
 
411
                        vdev.scan_fd = -1;
430
412
                }
431
413
                return 0;
432
414
        }
433
415
 
434
 
        if (vdev.scan)
 
416
        if (vdev.scan_fd >= 0)
435
417
                return 0;
436
418
 
437
419
        if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
446
428
        baswap(&ba, &vdev.bdaddr);
447
429
        sa.sin_family = AF_INET;
448
430
        memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
449
 
        sa.sin_port = *(uint16_t *) &ba.b[4];
 
431
        memcpy(&sa.sin_port, &ba.b[4], sizeof(sa.sin_port));
450
432
        if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
451
433
                syslog(LOG_ERR, "Can't bind socket: %s (%d)",
452
434
                                                strerror(errno), errno);
459
441
                goto failed;
460
442
        }
461
443
 
462
 
        sk_io = g_io_channel_unix_new(sk);
463
 
        g_io_add_watch(sk_io, G_IO_IN | G_IO_NVAL, io_conn_ind, NULL);
464
 
        vdev.scan = sk_io;
 
444
        memset(&scan_event, 0, sizeof(scan_event));
 
445
        scan_event.events = EPOLLIN;
 
446
        scan_event.data.fd = sk;
 
447
 
 
448
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sk, &scan_event) < 0) {
 
449
                syslog(LOG_ERR, "Failed to setup scan event watch");
 
450
                goto failed;
 
451
        }
 
452
 
 
453
        vdev.scan_fd = sk;
465
454
        return 0;
466
455
 
467
456
failed:
478
467
                return;
479
468
 
480
469
        connect_complete(conn);
481
 
 
482
 
        g_io_add_watch(conn->chan, G_IO_IN | G_IO_NVAL | G_IO_HUP,
483
 
                        io_acl_data, (gpointer) conn);
484
470
}
485
471
 
486
472
static void close_connection(struct vhci_conn *conn)
491
477
        syslog(LOG_INFO, "Closing connection %s handle %d",
492
478
                                        addr, conn->handle);
493
479
 
494
 
        g_io_channel_shutdown(conn->chan, TRUE, NULL);
495
 
        g_io_channel_unref(conn->chan);
 
480
        close(conn->fd);
496
481
 
497
482
        vconn[conn->handle - 1] = NULL;
498
483
        disconn_complete(conn);
556
541
        baswap(&ba, &cp->bdaddr);
557
542
        sa.sin_family = AF_INET;
558
543
        memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
559
 
        sa.sin_port = *(uint16_t *) &ba.b[4];
 
544
        memcpy(&sa.sin_port, &ba.b[4], sizeof(sa.sin_port));
560
545
        if (connect(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
561
546
                syslog(LOG_ERR, "Can't connect: %s (%d)",
562
547
                                                strerror(errno), errno);
584
569
 
585
570
        vconn[h] = conn;
586
571
        conn->handle = h + 1;
587
 
        conn->chan = g_io_channel_unix_new(sk);
 
572
        conn->fd = sk;
588
573
 
589
574
        connect_complete(conn);
590
 
        g_io_add_watch(conn->chan, G_IO_IN | G_IO_NVAL | G_IO_HUP,
591
 
                                io_acl_data, (gpointer) conn);
592
 
        return;
593
575
}
594
576
 
595
577
static void hci_link_control(uint16_t ocf, int plen, uint8_t *data)
798
780
        }
799
781
}
800
782
 
 
783
static void hci_status_param(uint16_t ocf, int plen, uint8_t *data)
 
784
{
 
785
        read_local_amp_info_rp ai;
 
786
        uint8_t status;
 
787
 
 
788
        const uint16_t ogf = OGF_STATUS_PARAM;
 
789
 
 
790
        switch (ocf) {
 
791
        case OCF_READ_LOCAL_AMP_INFO:
 
792
                memset(&ai, 0, sizeof(ai));
 
793
 
 
794
                /* BT only */
 
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;
 
802
 
 
803
                command_complete(ogf, ocf, sizeof(ai), &ai);
 
804
                break;
 
805
 
 
806
        default:
 
807
                status = 0x01;
 
808
                command_complete(ogf, ocf, 1, &status);
 
809
                break;
 
810
        }
 
811
}
 
812
 
801
813
static void hci_command(uint8_t *data)
802
814
{
803
815
        hci_command_hdr *ch;
827
839
        case OGF_INFO_PARAM:
828
840
                hci_info_param(ocf, ch->plen, ptr);
829
841
                break;
 
842
 
 
843
        case OGF_STATUS_PARAM:
 
844
                hci_status_param(ocf, ch->plen, ptr);
 
845
                break;
830
846
        }
831
847
}
832
848
 
835
851
        hci_acl_hdr *ah = (void *) data;
836
852
        struct vhci_conn *conn;
837
853
        uint16_t handle;
838
 
        int fd;
839
854
 
840
855
        handle = acl_handle(btohs(ah->handle));
841
856
 
844
859
                return;
845
860
        }
846
861
 
847
 
        fd = g_io_channel_unix_get_fd(conn->chan);
848
 
        if (write_n(fd, data, btohs(ah->dlen) + HCI_ACL_HDR_SIZE) < 0) {
 
862
        if (write_n(conn->fd, data, btohs(ah->dlen) + HCI_ACL_HDR_SIZE) < 0) {
849
863
                close_connection(conn);
850
864
                return;
851
865
        }
857
871
        }
858
872
}
859
873
 
860
 
static gboolean io_acl_data(GIOChannel *chan, GIOCondition cond, gpointer data)
 
874
#if 0
 
875
static void io_acl_data(void *data)
861
876
{
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;
864
879
        hci_acl_hdr *ah;
865
880
        uint16_t flags;
866
 
        int fd, len;
867
 
 
868
 
        if (cond & G_IO_NVAL) {
869
 
                g_io_channel_unref(chan);
870
 
                return FALSE;
871
 
        }
872
 
 
873
 
        if (cond & G_IO_HUP) {
874
 
                close_connection(conn);
875
 
                return FALSE;
876
 
        }
877
 
 
878
 
        fd = g_io_channel_unix_get_fd(chan);
 
881
        int len;
879
882
 
880
883
        ptr = buf + 1;
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);
883
 
                return FALSE;
 
886
                return;
884
887
        }
885
888
 
886
889
        ah = (void *) ptr;
887
890
        ptr += HCI_ACL_HDR_SIZE;
888
891
 
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);
892
 
                return FALSE;
 
895
                return;
893
896
        }
894
897
 
895
898
        buf[0] = HCI_ACLDATA_PKT;
900
903
 
901
904
        write_snoop(vdev.dd, HCI_ACLDATA_PKT, 1, buf, len);
902
905
 
903
 
        if (write(vdev.fd, buf, len) < 0)
904
 
                return FALSE;
905
 
 
906
 
        return TRUE;
 
906
        if (write(vdev.dev_fd, buf, len) < 0)
 
907
                syslog(LOG_ERR, "ACL data write error");
907
908
}
 
909
#endif
908
910
 
909
 
static gboolean io_conn_ind(GIOChannel *chan, GIOCondition cond, gpointer data)
 
911
static void io_conn_ind(void)
910
912
{
911
913
        struct vhci_link_info info;
912
914
        struct vhci_conn *conn;
913
915
        struct sockaddr_in sa;
914
916
        socklen_t len;
915
 
        int sk, nsk, h;
916
 
 
917
 
        if (cond & G_IO_NVAL)
918
 
                return FALSE;
919
 
 
920
 
        sk = g_io_channel_unix_get_fd(chan);
 
917
        int nsk, h;
921
918
 
922
919
        len = sizeof(sa);
923
 
        if ((nsk = accept(sk, (struct sockaddr *) &sa, &len)) < 0)
924
 
                return TRUE;
 
920
        if ((nsk = accept(vdev.scan_fd, (struct sockaddr *) &sa, &len)) < 0)
 
921
                return;
925
922
 
926
923
        if (read_n(nsk, &info, sizeof(info)) < 0) {
927
924
                syslog(LOG_ERR, "Can't read link info");
928
 
                return TRUE;
 
925
                return;
929
926
        }
930
927
 
931
928
        if (!(conn = malloc(sizeof(*conn)))) {
932
929
                syslog(LOG_ERR, "Can't alloc new connection");
933
930
                close(nsk);
934
 
                return TRUE;
 
931
                return;
935
932
        }
936
933
 
937
934
        bacpy(&conn->dest, &info.bdaddr);
943
940
        syslog(LOG_ERR, "Too many connections");
944
941
        free(conn);
945
942
        close(nsk);
946
 
        return TRUE;
 
943
        return;
947
944
 
948
945
accepted:
949
946
        vconn[h] = conn;
950
947
        conn->handle = h + 1;
951
 
        conn->chan = g_io_channel_unix_new(nsk);
 
948
        conn->fd = nsk;
952
949
        connect_request(conn);
953
 
 
954
 
        return TRUE;
955
950
}
956
951
 
957
 
static gboolean io_hci_data(GIOChannel *chan, GIOCondition cond, gpointer data)
 
952
static void io_hci_data(void)
958
953
{
959
954
        unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
960
955
        int type;
961
956
        ssize_t len;
962
 
        int fd;
963
957
 
964
958
        ptr = buf;
965
959
 
966
 
        fd = g_io_channel_unix_get_fd(chan);
967
 
 
968
 
        len = read(fd, buf, sizeof(buf));
 
960
        len = read(vdev.dev_fd, buf, sizeof(buf));
969
961
        if (len < 0) {
970
962
                if (errno == EAGAIN)
971
 
                        return TRUE;
 
963
                        return;
972
964
 
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);
976
 
                return FALSE;
 
966
                __io_canceled = 1;
 
967
                return;
977
968
        }
978
969
 
979
970
        type = *ptr++;
993
984
                syslog(LOG_ERR, "Unknown packet type 0x%2.2x", type);
994
985
                break;
995
986
        }
996
 
 
997
 
        return TRUE;
998
987
}
999
988
 
1000
989
static int getbdaddrbyname(char *str, bdaddr_t *ba)
1014
1003
                return 0;
1015
1004
        }
1016
1005
 
1017
 
        if (n == 1) {
1018
 
                /* IP address + port */
1019
 
                struct hostent *hent;
 
1006
        if (n == 0) {
 
1007
                /* loopback port */
 
1008
                in_addr_t addr = INADDR_LOOPBACK;
1020
1009
                bdaddr_t b;
1021
 
                char *ptr;
1022
 
 
1023
 
                ptr = strchr(str, ':');
1024
 
                *ptr++ = 0;
1025
 
 
1026
 
                if (!(hent = gethostbyname(str))) {
1027
 
                        fprintf(stderr, "Can't resolve %s\n", str);
1028
 
                        return -2;
1029
 
                }
1030
 
 
1031
 
                memcpy(&b, hent->h_addr, 4);
1032
 
                *(uint16_t *) (&b.b[4]) = htons(atoi(ptr));
 
1010
 
 
1011
                memcpy(&b, &addr, 4);
 
1012
                *(uint16_t *) (&b.b[4]) = htons(atoi(str));
1033
1013
                baswap(ba, &b);
1034
1014
 
1035
1015
                return 0;
1040
1020
        return -1;
1041
1021
}
1042
1022
 
1043
 
static void rewrite_bdaddr(unsigned char *buf, int len, bdaddr_t *bdaddr)
1044
 
{
1045
 
        hci_event_hdr *eh;
1046
 
        unsigned char *ptr = buf;
1047
 
        int type;
1048
 
 
1049
 
        if (!bdaddr)
1050
 
                return;
1051
 
 
1052
 
        if (!bacmp(bdaddr, BDADDR_ANY))
1053
 
                return;
1054
 
 
1055
 
        type = *ptr++;
1056
 
 
1057
 
        switch (type) {
1058
 
        case HCI_EVENT_PKT:
1059
 
                eh = (hci_event_hdr *) ptr;
1060
 
                ptr += HCI_EVENT_HDR_SIZE;
1061
 
 
1062
 
                if (eh->evt == EVT_CMD_COMPLETE) {
1063
 
                        evt_cmd_complete *cc = (void *) ptr;
1064
 
 
1065
 
                        ptr += EVT_CMD_COMPLETE_SIZE;
1066
 
 
1067
 
                        if (cc->opcode == htobs(cmd_opcode_pack(OGF_INFO_PARAM,
1068
 
                                                OCF_READ_BD_ADDR))) {
1069
 
                                bacpy((bdaddr_t *) (ptr + 1), bdaddr);
1070
 
                        }
1071
 
                }
1072
 
                break;
1073
 
        }
1074
 
}
1075
 
 
1076
 
static int run_proxy(int fd, int dev, bdaddr_t *bdaddr)
1077
 
{
1078
 
        unsigned char buf[HCI_MAX_FRAME_SIZE + 1];
1079
 
        struct hci_dev_info di;
1080
 
        struct hci_filter flt;
1081
 
        struct pollfd p[2];
1082
 
        int dd, err, len, need_raw;
1083
 
 
1084
 
        dd = hci_open_dev(dev);
1085
 
        if (dd < 0) {
1086
 
                syslog(LOG_ERR, "Can't open device hci%d: %s (%d)",
1087
 
                                                dev, strerror(errno), errno);
1088
 
                return 1;
1089
 
        }
1090
 
 
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);
1094
 
                hci_close_dev(dd);
1095
 
                return 1;
1096
 
        }
1097
 
 
1098
 
        need_raw = !hci_test_bit(HCI_RAW, &di.flags);
1099
 
 
1100
 
        hci_filter_clear(&flt);
1101
 
        hci_filter_all_ptypes(&flt);
1102
 
        hci_filter_all_events(&flt);
1103
 
 
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);
1107
 
                hci_close_dev(dd);
1108
 
                return 1;
1109
 
        }
1110
 
 
1111
 
        if (need_raw) {
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);
1115
 
                        hci_close_dev(dd);
1116
 
                        return 1;
1117
 
                }
1118
 
        }
1119
 
 
1120
 
        p[0].fd = fd;
1121
 
        p[0].events = POLLIN;
1122
 
        p[1].fd = dd;
1123
 
        p[1].events = POLLIN;
1124
 
 
1125
 
        while (!__io_canceled) {
1126
 
                p[0].revents = 0;
1127
 
                p[1].revents = 0;
1128
 
                err = poll(p, 2, 500);
1129
 
                if (err < 0)
1130
 
                        break;
1131
 
                if (!err)
1132
 
                        continue;
1133
 
 
1134
 
                if (p[0].revents & POLLIN) {
1135
 
                        len = read(fd, buf, sizeof(buf));
1136
 
                        if (len > 0) {
1137
 
                                rewrite_bdaddr(buf, len, bdaddr);
1138
 
                                err = write(dd, buf, len);
1139
 
                        }
1140
 
                }
1141
 
 
1142
 
                if (p[1].revents & POLLIN) {
1143
 
                        len = read(dd, buf, sizeof(buf));
1144
 
                        if (len > 0) {
1145
 
                                rewrite_bdaddr(buf, len, bdaddr);
1146
 
                                err = write(fd, buf, len);
1147
 
                        }
1148
 
                }
1149
 
        }
1150
 
 
1151
 
        if (need_raw) {
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);
1155
 
        }
1156
 
 
1157
 
        hci_close_dev(dd);
1158
 
 
1159
 
        syslog(LOG_INFO, "Exit");
1160
 
 
1161
 
        return 0;
1162
 
}
1163
 
 
1164
1023
static void usage(void)
1165
1024
{
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"
1169
1028
                "Options:\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");
1175
1033
}
1176
1034
 
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' },
1183
 
        { 0 }
 
1041
        { }
1184
1042
};
1185
1043
 
1186
1044
int main(int argc, char *argv[])
1187
1045
{
 
1046
        int exitcode = EXIT_FAILURE;
1188
1047
        struct sigaction sa;
1189
 
        GIOChannel *dev_io;
1190
1048
        char *device = NULL, *snoop = NULL;
1191
 
        bdaddr_t bdaddr;
1192
 
        int fd, dd, opt, detach = 1, dev = -1;
1193
 
 
1194
 
        bacpy(&bdaddr, BDADDR_ANY);
1195
 
 
1196
 
        while ((opt=getopt_long(argc, argv, "d:b:s:nh", main_options, NULL)) != EOF) {
 
1049
        int device_fd;
 
1050
        struct epoll_event device_event;
 
1051
        int dd, opt, detach = 1;
 
1052
 
 
1053
        while ((opt=getopt_long(argc, argv, "d:s:nh", options, NULL)) != EOF) {
1197
1054
                switch(opt) {
1198
1055
                case 'd':
1199
1056
                        device = strdup(optarg);
1200
1057
                        break;
1201
 
 
1202
 
                case 'b':
1203
 
                        str2ba(optarg, &bdaddr);
1204
 
                        break;
1205
 
 
1206
1058
                case 's':
1207
1059
                        snoop = strdup(optarg);
1208
1060
                        break;
1209
 
 
1210
1061
                case 'n':
1211
1062
                        detach = 0;
1212
1063
                        break;
1213
 
 
1214
1064
                case 'h':
 
1065
                        usage();
 
1066
                        exit(0);
1215
1067
                default:
1216
1068
                        usage();
1217
 
                        exit(0);
 
1069
                        exit(1);
1218
1070
                }
1219
1071
        }
1220
1072
 
1227
1079
                exit(1);
1228
1080
        }
1229
1081
 
1230
 
        if (strlen(argv[0]) > 3 && !strncasecmp(argv[0], "hci", 3)) {
1231
 
                dev = hci_devid(argv[0]);
1232
 
                if (dev < 0) {
1233
 
                        perror("Invalid device");
1234
 
                        exit(1);
1235
 
                }
1236
 
        } else {
1237
 
                if (getbdaddrbyname(argv[0], &vdev.bdaddr) < 0)
1238
 
                        exit(1);
1239
 
        }
 
1082
        if (getbdaddrbyname(argv[0], &vdev.bdaddr) < 0)
 
1083
                exit(1);
1240
1084
 
1241
1085
        if (detach) {
1242
1086
                if (daemon(0, 0)) {
1259
1103
        sigaction(SIGTERM, &sa, NULL);
1260
1104
        sigaction(SIGINT,  &sa, NULL);
1261
1105
 
1262
 
        io_init();
1263
 
 
1264
 
        if (!device && dev >= 0)
1265
 
                device = strdup(GHCI_DEV);
 
1106
        if (!device)
 
1107
                device = strdup(VHCI_DEV);
1266
1108
 
1267
1109
        /* Open and create virtual HCI device */
1268
 
        if (device) {
1269
 
                fd = open(device, O_RDWR);
1270
 
                if (fd < 0) {
1271
 
                        syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1272
 
                                                device, strerror(errno), errno);
1273
 
                        free(device);
1274
 
                        exit(1);
1275
 
                }
 
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);
1276
1114
                free(device);
1277
 
        } else {
1278
 
                fd = open(VHCI_DEV, O_RDWR);
1279
 
                if (fd < 0) {
1280
 
                        fd = open(VHCI_UDEV, O_RDWR);
1281
 
                        if (fd < 0) {
1282
 
                                syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1283
 
                                                VHCI_DEV, strerror(errno), errno);
1284
 
                                exit(1);
1285
 
                        }
1286
 
                }
 
1115
                return exitcode;
1287
1116
        }
1288
1117
 
 
1118
        free(device);
 
1119
 
1289
1120
        /* Create snoop file */
1290
1121
        if (snoop) {
1291
1122
                dd = create_snoop(snoop);
1297
1128
                dd = -1;
1298
1129
 
1299
1130
        /* Create event loop */
1300
 
        event_loop = g_main_loop_new(NULL, FALSE);
1301
 
 
1302
 
        if (dev >= 0)
1303
 
                return run_proxy(fd, dev, &bdaddr);
 
1131
        epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
1132
        if (epoll_fd < 0) {
 
1133
                perror("Failed to create epoll descriptor");
 
1134
                goto close_device;
 
1135
        }
1304
1136
 
1305
1137
        /* Device settings */
1306
1138
        vdev.features[0] = 0xff;
1324
1156
        vdev.eir_fec = 0x00;
1325
1157
        memset(vdev.eir_data, 0, sizeof(vdev.eir_data));
1326
1158
 
1327
 
        vdev.fd = fd;
 
1159
        vdev.dev_fd = device_fd;
1328
1160
        vdev.dd = dd;
1329
1161
 
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;
 
1165
 
 
1166
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, device_fd, &device_event) < 0) {
 
1167
                perror("Failed to setup device event watch");
 
1168
                goto close_device;
 
1169
        }
1332
1170
 
1333
1171
        setpriority(PRIO_PROCESS, 0, -19);
1334
1172
 
1335
1173
        /* Start event processor */
1336
 
        g_main_loop_run(event_loop);
1337
 
 
1338
 
        close(fd);
 
1174
        for (;;) {
 
1175
                struct epoll_event events[MAX_EPOLL_EVENTS];
 
1176
                int n, nfds;
 
1177
 
 
1178
                if (__io_canceled)
 
1179
                        break;
 
1180
 
 
1181
                nfds = epoll_wait(epoll_fd, events, MAX_EPOLL_EVENTS, -1);
 
1182
                if (nfds < 0)
 
1183
                        continue;
 
1184
 
 
1185
                for (n = 0; n < nfds; n++) {
 
1186
                        if (events[n].data.fd == vdev.dev_fd)
 
1187
                                io_hci_data();
 
1188
                        else if (events[n].data.fd == vdev.scan_fd)
 
1189
                                io_conn_ind();
 
1190
                }
 
1191
        }
 
1192
 
 
1193
        exitcode = EXIT_SUCCESS;
 
1194
 
 
1195
        epoll_ctl(epoll_fd, EPOLL_CTL_DEL, device_fd, NULL);
 
1196
 
 
1197
close_device:
 
1198
        close(device_fd);
1339
1199
 
1340
1200
        if (dd >= 0)
1341
1201
                close(dd);
1342
1202
 
 
1203
        close(epoll_fd);
 
1204
 
1343
1205
        syslog(LOG_INFO, "Exit");
1344
1206
 
1345
 
        return 0;
 
1207
        return exitcode;
1346
1208
}