3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2000-2002 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2003-2009 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program 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
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <netinet/in.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/resource.h>
48 #include <bluetooth/bluetooth.h>
49 #include <bluetooth/hci.h>
50 #include <bluetooth/hci_lib.h>
56 #if __BYTE_ORDER == __LITTLE_ENDIAN
57 static inline uint64_t ntoh64(uint64_t n)
60 uint64_t tmp = ntohl(n & 0x00000000ffffffff);
65 #elif __BYTE_ORDER == __BIG_ENDIAN
68 #error "Unknown byte order"
70 #define hton64(x) ntoh64(x)
72 #define GHCI_DEV "/dev/ghci"
74 #define VHCI_DEV "/dev/vhci"
75 #define VHCI_UDEV "/dev/hci_vhci"
77 #define VHCI_MAX_CONN 12
79 #define VHCI_ACL_MTU 192
80 #define VHCI_ACL_MAX_PKT 8
88 uint8_t eir_data[240];
102 struct vhci_link_info {
104 uint8_t dev_class[3];
107 } __attribute__ ((packed));
109 static struct vhci_device vdev;
110 static struct vhci_conn *vconn[VHCI_MAX_CONN];
113 uint8_t id[8]; /* Identification Pattern */
114 uint32_t version; /* Version Number = 1 */
115 uint32_t type; /* Datalink Type */
116 } __attribute__ ((packed));
117 #define BTSNOOP_HDR_SIZE (sizeof(struct btsnoop_hdr))
120 uint32_t size; /* Original Length */
121 uint32_t len; /* Included Length */
122 uint32_t flags; /* Packet Flags */
123 uint32_t drops; /* Cumulative Drops */
124 uint64_t ts; /* Timestamp microseconds */
125 uint8_t data[0]; /* Packet Data */
126 } __attribute__ ((packed));
127 #define BTSNOOP_PKT_SIZE (sizeof(struct btsnoop_pkt))
129 static uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e, 0x6f, 0x6f, 0x70, 0x00 };
131 static GMainLoop *event_loop;
133 static volatile sig_atomic_t __io_canceled;
135 static inline void io_init(void)
140 static inline void io_cancel(void)
145 static void sig_term(int sig)
148 g_main_loop_quit(event_loop);
151 static gboolean io_acl_data(GIOChannel *chan, GIOCondition cond, gpointer data);
152 static gboolean io_conn_ind(GIOChannel *chan, GIOCondition cond, gpointer data);
153 static gboolean io_hci_data(GIOChannel *chan, GIOCondition cond, gpointer data);
155 static inline int read_n(int fd, void *buf, int len)
157 register int w, t = 0;
159 while (!__io_canceled && len > 0) {
160 if ((w = read(fd, buf, len)) < 0 ){
161 if( errno == EINTR || errno == EAGAIN )
167 len -= w; buf += w; t += w;
172 /* Write exactly len bytes (Signal safe)*/
173 static inline int write_n(int fd, void *buf, int len)
175 register int w, t = 0;
177 while (!__io_canceled && len > 0) {
178 if ((w = write(fd, buf, len)) < 0 ){
179 if( errno == EINTR || errno == EAGAIN )
185 len -= w; buf += w; t += w;
190 static int create_snoop(char *file)
192 struct btsnoop_hdr hdr;
195 fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
199 memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));
200 hdr.version = htonl(1);
201 hdr.type = htonl(1002);
203 len = write(fd, &hdr, BTSNOOP_HDR_SIZE);
209 if (len != BTSNOOP_HDR_SIZE) {
217 static int write_snoop(int fd, int type, int incoming, unsigned char *buf, int len)
219 struct btsnoop_pkt pkt;
228 memset(&tv, 0, sizeof(tv));
229 gettimeofday(&tv, NULL);
230 ts = (tv.tv_sec - 946684800ll) * 1000000ll + tv.tv_usec;
232 pkt.size = htonl(size);
234 pkt.flags = ntohl(incoming & 0x01);
235 pkt.drops = htonl(0);
236 pkt.ts = hton64(ts + 0x00E03AB44A676000ll);
238 if (type == HCI_COMMAND_PKT || type == HCI_EVENT_PKT)
239 pkt.flags |= ntohl(0x02);
241 err = write(fd, &pkt, BTSNOOP_PKT_SIZE);
242 err = write(fd, buf, size);
247 static struct vhci_conn *conn_get_by_bdaddr(bdaddr_t *ba)
251 for (i = 0; i < VHCI_MAX_CONN; i++)
252 if (!bacmp(&vconn[i]->dest, ba))
258 static void command_status(uint16_t ogf, uint16_t ocf, uint8_t status)
260 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
265 *ptr++ = HCI_EVENT_PKT;
268 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
270 he->evt = EVT_CMD_STATUS;
271 he->plen = EVT_CMD_STATUS_SIZE;
273 cs = (void *) ptr; ptr += EVT_CMD_STATUS_SIZE;
277 cs->opcode = htobs(cmd_opcode_pack(ogf, ocf));
279 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
281 if (write(vdev.fd, buf, ptr - buf) < 0)
282 syslog(LOG_ERR, "Can't send event: %s(%d)",
283 strerror(errno), errno);
286 static void command_complete(uint16_t ogf, uint16_t ocf, int plen, void *data)
288 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
289 evt_cmd_complete *cc;
293 *ptr++ = HCI_EVENT_PKT;
296 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
298 he->evt = EVT_CMD_COMPLETE;
299 he->plen = EVT_CMD_COMPLETE_SIZE + plen;
301 cc = (void *) ptr; ptr += EVT_CMD_COMPLETE_SIZE;
304 cc->opcode = htobs(cmd_opcode_pack(ogf, ocf));
307 memcpy(ptr, data, plen);
311 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
313 if (write(vdev.fd, buf, ptr - buf) < 0)
314 syslog(LOG_ERR, "Can't send event: %s(%d)",
315 strerror(errno), errno);
318 static void connect_request(struct vhci_conn *conn)
320 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
321 evt_conn_request *cr;
325 *ptr++ = HCI_EVENT_PKT;
328 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
330 he->evt = EVT_CONN_REQUEST;
331 he->plen = EVT_CONN_REQUEST_SIZE;
333 cr = (void *) ptr; ptr += EVT_CONN_REQUEST_SIZE;
335 bacpy(&cr->bdaddr, &conn->dest);
336 memset(&cr->dev_class, 0, sizeof(cr->dev_class));
337 cr->link_type = ACL_LINK;
339 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
341 if (write(vdev.fd, buf, ptr - buf) < 0)
342 syslog(LOG_ERR, "Can't send event: %s (%d)",
343 strerror(errno), errno);
346 static void connect_complete(struct vhci_conn *conn)
348 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
349 evt_conn_complete *cc;
353 *ptr++ = HCI_EVENT_PKT;
356 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
358 he->evt = EVT_CONN_COMPLETE;
359 he->plen = EVT_CONN_COMPLETE_SIZE;
361 cc = (void *) ptr; ptr += EVT_CONN_COMPLETE_SIZE;
363 bacpy(&cc->bdaddr, &conn->dest);
365 cc->handle = htobs(conn->handle);
366 cc->link_type = ACL_LINK;
367 cc->encr_mode = 0x00;
369 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
371 if (write(vdev.fd, buf, ptr - buf) < 0)
372 syslog(LOG_ERR, "Can't send event: %s (%d)",
373 strerror(errno), errno);
376 static void disconn_complete(struct vhci_conn *conn)
378 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
379 evt_disconn_complete *dc;
383 *ptr++ = HCI_EVENT_PKT;
386 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
388 he->evt = EVT_DISCONN_COMPLETE;
389 he->plen = EVT_DISCONN_COMPLETE_SIZE;
391 dc = (void *) ptr; ptr += EVT_DISCONN_COMPLETE_SIZE;
394 dc->handle = htobs(conn->handle);
397 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
399 if (write(vdev.fd, buf, ptr - buf) < 0)
400 syslog(LOG_ERR, "Can't send event: %s (%d)",
401 strerror(errno), errno);
406 static void num_completed_pkts(struct vhci_conn *conn)
408 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
409 evt_num_comp_pkts *np;
413 *ptr++ = HCI_EVENT_PKT;
416 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
418 he->evt = EVT_NUM_COMP_PKTS;
419 he->plen = EVT_NUM_COMP_PKTS_SIZE;
421 np = (void *) ptr; ptr += EVT_NUM_COMP_PKTS_SIZE;
424 *((uint16_t *) ptr) = htobs(conn->handle); ptr += 2;
425 *((uint16_t *) ptr) = htobs(vdev.acl_cnt); ptr += 2;
427 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
429 if (write(vdev.fd, buf, ptr - buf) < 0)
430 syslog(LOG_ERR, "Can't send event: %s (%d)",
431 strerror(errno), errno);
434 static int scan_enable(uint8_t *data)
436 struct sockaddr_in sa;
441 if (!(*data & SCAN_PAGE)) {
443 g_io_channel_close(vdev.scan);
452 if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
453 syslog(LOG_ERR, "Can't create socket: %s (%d)",
454 strerror(errno), errno);
459 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
461 baswap(&ba, &vdev.bdaddr);
462 sa.sin_family = AF_INET;
463 memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
464 sa.sin_port = *(uint16_t *) &ba.b[4];
465 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
466 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
467 strerror(errno), errno);
471 if (listen(sk, 10)) {
472 syslog(LOG_ERR, "Can't listen on socket: %s (%d)",
473 strerror(errno), errno);
477 sk_io = g_io_channel_unix_new(sk);
478 g_io_add_watch(sk_io, G_IO_IN | G_IO_NVAL, io_conn_ind, NULL);
487 static void accept_connection(uint8_t *data)
489 accept_conn_req_cp *cp = (void *) data;
490 struct vhci_conn *conn;
492 if (!(conn = conn_get_by_bdaddr(&cp->bdaddr)))
495 connect_complete(conn);
497 g_io_add_watch(conn->chan, G_IO_IN | G_IO_NVAL | G_IO_HUP,
498 io_acl_data, (gpointer) conn);
501 static void close_connection(struct vhci_conn *conn)
503 syslog(LOG_INFO, "Closing connection %s handle %d",
504 batostr(&conn->dest), conn->handle);
506 g_io_channel_close(conn->chan);
507 g_io_channel_unref(conn->chan);
509 vconn[conn->handle - 1] = NULL;
510 disconn_complete(conn);
514 static void disconnect(uint8_t *data)
516 disconnect_cp *cp = (void *) data;
517 struct vhci_conn *conn;
520 handle = btohs(cp->handle);
522 if (handle > VHCI_MAX_CONN)
525 if (!(conn = vconn[handle-1]))
528 close_connection(conn);
531 static void create_connection(uint8_t *data)
533 create_conn_cp *cp = (void *) data;
534 struct vhci_link_info info;
535 struct vhci_conn *conn;
536 struct sockaddr_in sa;
540 for (h = 0; h < VHCI_MAX_CONN; h++)
544 syslog(LOG_ERR, "Too many connections");
548 if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
549 syslog(LOG_ERR, "Can't create socket: %s (%d)",
550 strerror(errno), errno);
555 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
557 baswap(&ba, &vdev.bdaddr);
558 sa.sin_family = AF_INET;
559 sa.sin_addr.s_addr = INADDR_ANY; // *(uint32_t *) &ba;
560 sa.sin_port = 0; // *(uint16_t *) &ba.b[4];
561 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
562 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
563 strerror(errno), errno);
568 baswap(&ba, &cp->bdaddr);
569 sa.sin_family = AF_INET;
570 memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
571 sa.sin_port = *(uint16_t *) &ba.b[4];
572 if (connect(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
573 syslog(LOG_ERR, "Can't connect: %s (%d)",
574 strerror(errno), errno);
580 memset(&info, 0, sizeof(info));
581 bacpy(&info.bdaddr, &vdev.bdaddr);
582 info.link_type = ACL_LINK;
584 write_n(sk, (void *) &info, sizeof(info));
586 if (!(conn = malloc(sizeof(*conn)))) {
587 syslog(LOG_ERR, "Can't alloc new connection: %s (%d)",
588 strerror(errno), errno);
593 memcpy((uint8_t *) &ba, (uint8_t *) &sa.sin_addr, 4);
594 memcpy((uint8_t *) &ba.b[4], (uint8_t *) &sa.sin_port, 2);
595 baswap(&conn->dest, &ba);
598 conn->handle = h + 1;
599 conn->chan = g_io_channel_unix_new(sk);
601 connect_complete(conn);
602 g_io_add_watch(conn->chan, G_IO_IN | G_IO_NVAL | G_IO_HUP,
603 io_acl_data, (gpointer) conn);
607 static void hci_link_control(uint16_t ocf, int plen, uint8_t *data)
611 const uint16_t ogf = OGF_LINK_CTL;
614 case OCF_CREATE_CONN:
615 command_status(ogf, ocf, 0x00);
616 create_connection(data);
619 case OCF_ACCEPT_CONN_REQ:
620 command_status(ogf, ocf, 0x00);
621 accept_connection(data);
625 command_status(ogf, ocf, 0x00);
631 command_complete(ogf, ocf, 1, &status);
636 static void hci_link_policy(uint16_t ocf, int plen, uint8_t *data)
640 const uint16_t ogf = OGF_INFO_PARAM;
645 command_complete(ogf, ocf, 1, &status);
650 static void hci_host_control(uint16_t ocf, int plen, uint8_t *data)
652 read_local_name_rp ln;
653 read_class_of_dev_rp cd;
654 read_inquiry_mode_rp im;
655 read_ext_inquiry_response_rp ir;
658 const uint16_t ogf = OGF_HOST_CTL;
663 command_complete(ogf, ocf, 1, &status);
666 case OCF_SET_EVENT_FLT:
668 command_complete(ogf, ocf, 1, &status);
671 case OCF_CHANGE_LOCAL_NAME:
673 memcpy(vdev.name, data, sizeof(vdev.name));
674 command_complete(ogf, ocf, 1, &status);
677 case OCF_READ_LOCAL_NAME:
679 memcpy(ln.name, vdev.name, sizeof(ln.name));
680 command_complete(ogf, ocf, sizeof(ln), &ln);
683 case OCF_WRITE_CONN_ACCEPT_TIMEOUT:
684 case OCF_WRITE_PAGE_TIMEOUT:
686 command_complete(ogf, ocf, 1, &status);
689 case OCF_WRITE_SCAN_ENABLE:
690 status = scan_enable(data);
691 command_complete(ogf, ocf, 1, &status);
694 case OCF_WRITE_AUTH_ENABLE:
696 command_complete(ogf, ocf, 1, &status);
699 case OCF_WRITE_ENCRYPT_MODE:
701 command_complete(ogf, ocf, 1, &status);
704 case OCF_READ_CLASS_OF_DEV:
706 memcpy(cd.dev_class, vdev.dev_class, 3);
707 command_complete(ogf, ocf, sizeof(cd), &cd);
710 case OCF_WRITE_CLASS_OF_DEV:
712 memcpy(vdev.dev_class, data, 3);
713 command_complete(ogf, ocf, 1, &status);
716 case OCF_READ_INQUIRY_MODE:
718 im.mode = vdev.inq_mode;
719 command_complete(ogf, ocf, sizeof(im), &im);
722 case OCF_WRITE_INQUIRY_MODE:
724 vdev.inq_mode = data[0];
725 command_complete(ogf, ocf, 1, &status);
728 case OCF_READ_EXT_INQUIRY_RESPONSE:
730 ir.fec = vdev.eir_fec;
731 memcpy(ir.data, vdev.eir_data, 240);
732 command_complete(ogf, ocf, sizeof(ir), &ir);
735 case OCF_WRITE_EXT_INQUIRY_RESPONSE:
737 vdev.eir_fec = data[0];
738 memcpy(vdev.eir_data, data + 1, 240);
739 command_complete(ogf, ocf, 1, &status);
744 command_complete(ogf, ocf, 1, &status);
749 static void hci_info_param(uint16_t ocf, int plen, uint8_t *data)
751 read_local_version_rp lv;
752 read_local_features_rp lf;
753 read_local_ext_features_rp ef;
754 read_buffer_size_rp bs;
758 const uint16_t ogf = OGF_INFO_PARAM;
761 case OCF_READ_LOCAL_VERSION:
764 lv.hci_rev = htobs(0x0000);
766 lv.manufacturer = htobs(29);
767 lv.lmp_subver = htobs(0x0000);
768 command_complete(ogf, ocf, sizeof(lv), &lv);
771 case OCF_READ_LOCAL_FEATURES:
773 memcpy(lf.features, vdev.features, 8);
774 command_complete(ogf, ocf, sizeof(lf), &lf);
777 case OCF_READ_LOCAL_EXT_FEATURES:
782 memcpy(ef.features, vdev.features, 8);
786 memset(ef.features, 0, 8);
788 command_complete(ogf, ocf, sizeof(ef), &ef);
791 case OCF_READ_BUFFER_SIZE:
793 bs.acl_mtu = htobs(VHCI_ACL_MTU);
795 bs.acl_max_pkt = htobs(VHCI_ACL_MAX_PKT);
796 bs.sco_max_pkt = htobs(0);
797 command_complete(ogf, ocf, sizeof(bs), &bs);
800 case OCF_READ_BD_ADDR:
802 bacpy(&ba.bdaddr, &vdev.bdaddr);
803 command_complete(ogf, ocf, sizeof(ba), &ba);
808 command_complete(ogf, ocf, 1, &status);
813 static void hci_command(uint8_t *data)
819 ch = (hci_command_hdr *) ptr;
820 ptr += HCI_COMMAND_HDR_SIZE;
822 ch->opcode = btohs(ch->opcode);
823 ogf = cmd_opcode_ogf(ch->opcode);
824 ocf = cmd_opcode_ocf(ch->opcode);
828 hci_link_control(ocf, ch->plen, ptr);
831 case OGF_LINK_POLICY:
832 hci_link_policy(ocf, ch->plen, ptr);
836 hci_host_control(ocf, ch->plen, ptr);
840 hci_info_param(ocf, ch->plen, ptr);
845 static void hci_acl_data(uint8_t *data)
847 hci_acl_hdr *ah = (void *) data;
848 struct vhci_conn *conn;
852 handle = acl_handle(btohs(ah->handle));
854 if (handle > VHCI_MAX_CONN || !(conn = vconn[handle - 1])) {
855 syslog(LOG_ERR, "Bad connection handle %d", handle);
859 fd = g_io_channel_unix_get_fd(conn->chan);
860 if (write_n(fd, data, btohs(ah->dlen) + HCI_ACL_HDR_SIZE) < 0) {
861 close_connection(conn);
865 if (++vdev.acl_cnt > VHCI_ACL_MAX_PKT - 1) {
866 /* Send num of complete packets event */
867 num_completed_pkts(conn);
872 static gboolean io_acl_data(GIOChannel *chan, GIOCondition cond, gpointer data)
874 struct vhci_conn *conn = (struct vhci_conn *) data;
875 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
880 if (cond & G_IO_NVAL) {
881 g_io_channel_unref(chan);
885 if (cond & G_IO_HUP) {
886 close_connection(conn);
890 fd = g_io_channel_unix_get_fd(chan);
893 if (read_n(fd, ptr, HCI_ACL_HDR_SIZE) <= 0) {
894 close_connection(conn);
899 ptr += HCI_ACL_HDR_SIZE;
901 len = btohs(ah->dlen);
902 if (read_n(fd, ptr, len) <= 0) {
903 close_connection(conn);
907 buf[0] = HCI_ACLDATA_PKT;
909 flags = acl_flags(btohs(ah->handle));
910 ah->handle = htobs(acl_handle_pack(conn->handle, flags));
911 len += HCI_ACL_HDR_SIZE + 1;
913 write_snoop(vdev.dd, HCI_ACLDATA_PKT, 1, buf, len);
915 err = write(vdev.fd, buf, len);
920 static gboolean io_conn_ind(GIOChannel *chan, GIOCondition cond, gpointer data)
922 struct vhci_link_info info;
923 struct vhci_conn *conn;
924 struct sockaddr_in sa;
928 if (cond & G_IO_NVAL)
931 sk = g_io_channel_unix_get_fd(chan);
934 if ((nsk = accept(sk, (struct sockaddr *) &sa, &len)) < 0)
937 if (read_n(nsk, &info, sizeof(info)) < 0) {
938 syslog(LOG_ERR, "Can't read link info");
942 if (!(conn = malloc(sizeof(*conn)))) {
943 syslog(LOG_ERR, "Can't alloc new connection");
948 bacpy(&conn->dest, &info.bdaddr);
950 for (h = 0; h < VHCI_MAX_CONN; h++)
954 syslog(LOG_ERR, "Too many connections");
961 conn->handle = h + 1;
962 conn->chan = g_io_channel_unix_new(nsk);
963 connect_request(conn);
968 static gboolean io_hci_data(GIOChannel *chan, GIOCondition cond, gpointer data)
970 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
977 if ((err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len))) {
978 if (err == G_IO_ERROR_AGAIN)
981 syslog(LOG_ERR, "Read failed: %s (%d)", strerror(errno), errno);
982 g_io_channel_unref(chan);
983 g_main_loop_quit(event_loop);
989 write_snoop(vdev.dd, type, 0, buf, len);
992 case HCI_COMMAND_PKT:
996 case HCI_ACLDATA_PKT:
1001 syslog(LOG_ERR, "Unknown packet type 0x%2.2x", type);
1008 static int getbdaddrbyname(char *str, bdaddr_t *ba)
1014 /* Check address format */
1015 for (i = 0, n = 0; i < len; i++)
1021 baswap(ba, strtoba(str));
1026 /* IP address + port */
1027 struct hostent *hent;
1031 ptr = strchr(str, ':');
1034 if (!(hent = gethostbyname(str))) {
1035 fprintf(stderr, "Can't resolve %s\n", str);
1039 memcpy(&b, hent->h_addr, 4);
1040 *(uint16_t *) (&b.b[4]) = htons(atoi(ptr));
1046 fprintf(stderr, "Invalid address format\n");
1051 static void rewrite_bdaddr(unsigned char *buf, int len, bdaddr_t *bdaddr)
1054 unsigned char *ptr = buf;
1060 if (!bacmp(bdaddr, BDADDR_ANY))
1067 eh = (hci_event_hdr *) ptr;
1068 ptr += HCI_EVENT_HDR_SIZE;
1070 if (eh->evt == EVT_CMD_COMPLETE) {
1071 evt_cmd_complete *cc = (void *) ptr;
1073 ptr += EVT_CMD_COMPLETE_SIZE;
1075 if (cc->opcode == htobs(cmd_opcode_pack(OGF_INFO_PARAM,
1076 OCF_READ_BD_ADDR))) {
1077 bacpy((bdaddr_t *) (ptr + 1), bdaddr);
1084 static int run_proxy(int fd, int dev, bdaddr_t *bdaddr)
1086 unsigned char buf[HCI_MAX_FRAME_SIZE + 1];
1087 struct hci_dev_info di;
1088 struct hci_filter flt;
1090 int dd, err, len, need_raw;
1092 dd = hci_open_dev(dev);
1094 syslog(LOG_ERR, "Can't open device hci%d: %s (%d)",
1095 dev, strerror(errno), errno);
1099 if (hci_devinfo(dev, &di) < 0) {
1100 syslog(LOG_ERR, "Can't get device info for hci%d: %s (%d)",
1101 dev, strerror(errno), errno);
1106 need_raw = !hci_test_bit(HCI_RAW, &di.flags);
1108 hci_filter_clear(&flt);
1109 hci_filter_all_ptypes(&flt);
1110 hci_filter_all_events(&flt);
1112 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1113 syslog(LOG_ERR, "Can't set filter for hci%d: %s (%d)",
1114 dev, strerror(errno), errno);
1120 if (ioctl(dd, HCISETRAW, 1) < 0) {
1121 syslog(LOG_ERR, "Can't set raw mode on hci%d: %s (%d)",
1122 dev, strerror(errno), errno);
1129 p[0].events = POLLIN;
1131 p[1].events = POLLIN;
1133 while (!__io_canceled) {
1136 err = poll(p, 2, 500);
1142 if (p[0].revents & POLLIN) {
1143 len = read(fd, buf, sizeof(buf));
1145 rewrite_bdaddr(buf, len, bdaddr);
1146 err = write(dd, buf, len);
1150 if (p[1].revents & POLLIN) {
1151 len = read(dd, buf, sizeof(buf));
1153 rewrite_bdaddr(buf, len, bdaddr);
1154 err = write(fd, buf, len);
1160 if (ioctl(dd, HCISETRAW, 0) < 0)
1161 syslog(LOG_ERR, "Can't clear raw mode on hci%d: %s (%d)",
1162 dev, strerror(errno), errno);
1167 syslog(LOG_INFO, "Exit");
1172 static void usage(void)
1174 printf("hciemu - HCI emulator ver %s\n", VERSION);
1175 printf("Usage: \n");
1176 printf("\thciemu [options] local_address\n"
1178 "\t[-d device] use specified device\n"
1179 "\t[-b bdaddr] emulate specified address\n"
1180 "\t[-s file] create snoop file\n"
1181 "\t[-n] do not detach\n"
1182 "\t[-h] help, you are looking at it\n");
1185 static struct option main_options[] = {
1186 { "device", 1, 0, 'd' },
1187 { "bdaddr", 1, 0, 'b' },
1188 { "snoop", 1, 0, 's' },
1189 { "nodetach", 0, 0, 'n' },
1190 { "help", 0, 0, 'h' },
1194 int main(int argc, char *argv[])
1196 struct sigaction sa;
1198 char *device = NULL, *snoop = NULL;
1200 int fd, dd, opt, detach = 1, dev = -1;
1202 bacpy(&bdaddr, BDADDR_ANY);
1204 while ((opt=getopt_long(argc, argv, "d:b:s:nh", main_options, NULL)) != EOF) {
1207 device = strdup(optarg);
1211 str2ba(optarg, &bdaddr);
1215 snoop = strdup(optarg);
1238 if (strlen(argv[0]) > 3 && !strncasecmp(argv[0], "hci", 3)) {
1239 dev = hci_devid(argv[0]);
1241 perror("Invalid device");
1245 if (getbdaddrbyname(argv[0], &vdev.bdaddr) < 0)
1251 perror("Can't start daemon");
1256 /* Start logging to syslog and stderr */
1257 openlog("hciemu", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
1258 syslog(LOG_INFO, "HCI emulation daemon ver %s started", VERSION);
1260 memset(&sa, 0, sizeof(sa));
1261 sa.sa_flags = SA_NOCLDSTOP;
1262 sa.sa_handler = SIG_IGN;
1263 sigaction(SIGCHLD, &sa, NULL);
1264 sigaction(SIGPIPE, &sa, NULL);
1266 sa.sa_handler = sig_term;
1267 sigaction(SIGTERM, &sa, NULL);
1268 sigaction(SIGINT, &sa, NULL);
1272 if (!device && dev >= 0)
1273 device = strdup(GHCI_DEV);
1275 /* Open and create virtual HCI device */
1277 fd = open(device, O_RDWR);
1279 syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1280 device, strerror(errno), errno);
1286 fd = open(VHCI_DEV, O_RDWR);
1288 fd = open(VHCI_UDEV, O_RDWR);
1290 syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1291 VHCI_DEV, strerror(errno), errno);
1297 /* Create snoop file */
1299 dd = create_snoop(snoop);
1301 syslog(LOG_ERR, "Can't create snoop file %s: %s (%d)",
1302 snoop, strerror(errno), errno);
1307 /* Create event loop */
1308 event_loop = g_main_loop_new(NULL, FALSE);
1311 return run_proxy(fd, dev, &bdaddr);
1313 /* Device settings */
1314 vdev.features[0] = 0xff;
1315 vdev.features[1] = 0xff;
1316 vdev.features[2] = 0x8f;
1317 vdev.features[3] = 0xfe;
1318 vdev.features[4] = 0x9b;
1319 vdev.features[5] = 0xf9;
1320 vdev.features[6] = 0x01;
1321 vdev.features[7] = 0x80;
1323 memset(vdev.name, 0, sizeof(vdev.name));
1324 strncpy((char *) vdev.name, "BlueZ (Virtual HCI)",
1325 sizeof(vdev.name) - 1);
1327 vdev.dev_class[0] = 0x00;
1328 vdev.dev_class[1] = 0x00;
1329 vdev.dev_class[2] = 0x00;
1331 vdev.inq_mode = 0x00;
1332 vdev.eir_fec = 0x00;
1333 memset(vdev.eir_data, 0, sizeof(vdev.eir_data));
1338 dev_io = g_io_channel_unix_new(fd);
1339 g_io_add_watch(dev_io, G_IO_IN, io_hci_data, NULL);
1341 setpriority(PRIO_PROCESS, 0, -19);
1343 /* Start event processor */
1344 g_main_loop_run(event_loop);
1351 syslog(LOG_INFO, "Exit");