3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2000-2002 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2003-2010 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
36 #include <sys/epoll.h>
37 #include <sys/socket.h>
38 #include <sys/resource.h>
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/hci.h>
43 #include <bluetooth/hci_lib.h>
44 #include <bluetooth/l2cap.h>
46 #define VHCI_DEV "/dev/vhci"
48 #define VHCI_MAX_CONN 12
50 #define VHCI_ACL_MTU 192
51 #define VHCI_ACL_MAX_PKT 8
61 uint8_t eir_data[HCI_MAX_EIR_LENGTH];
77 struct vhci_link_info {
82 } __attribute__ ((packed));
84 static struct vhci_device vdev;
85 static struct vhci_conn *vconn[VHCI_MAX_CONN];
88 uint8_t id[8]; /* Identification Pattern */
89 uint32_t version; /* Version Number = 1 */
90 uint32_t type; /* Datalink Type */
91 } __attribute__ ((packed));
92 #define BTSNOOP_HDR_SIZE (sizeof(struct btsnoop_hdr))
95 uint32_t size; /* Original Length */
96 uint32_t len; /* Included Length */
97 uint32_t flags; /* Packet Flags */
98 uint32_t drops; /* Cumulative Drops */
99 uint64_t ts; /* Timestamp microseconds */
100 uint8_t data[0]; /* Packet Data */
101 } __attribute__ ((packed));
102 #define BTSNOOP_PKT_SIZE (sizeof(struct btsnoop_pkt))
104 static uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e, 0x6f, 0x6f, 0x70, 0x00 };
106 #define MAX_EPOLL_EVENTS 10
110 static volatile sig_atomic_t __io_canceled = 0;
112 static void sig_term(int sig)
117 static inline int read_n(int fd, void *buf, int len)
119 register int w, t = 0;
121 while (!__io_canceled && len > 0) {
122 if ((w = read(fd, buf, len)) < 0 ){
123 if( errno == EINTR || errno == EAGAIN )
129 len -= w; buf += w; t += w;
134 /* Write exactly len bytes (Signal safe)*/
135 static inline int write_n(int fd, void *buf, int len)
137 register int w, t = 0;
139 while (!__io_canceled && len > 0) {
140 if ((w = write(fd, buf, len)) < 0 ){
141 if( errno == EINTR || errno == EAGAIN )
147 len -= w; buf += w; t += w;
152 static int create_snoop(char *file)
154 struct btsnoop_hdr hdr;
157 fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
161 memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));
162 hdr.version = htonl(1);
163 hdr.type = htonl(1002);
165 len = write(fd, &hdr, BTSNOOP_HDR_SIZE);
171 if (len != BTSNOOP_HDR_SIZE) {
179 static int write_snoop(int fd, int type, int incoming,
180 unsigned char *buf, int len)
182 struct btsnoop_pkt pkt;
190 memset(&tv, 0, sizeof(tv));
191 gettimeofday(&tv, NULL);
192 ts = (tv.tv_sec - 946684800ll) * 1000000ll + tv.tv_usec;
194 pkt.size = htonl(size);
196 pkt.flags = ntohl(incoming & 0x01);
197 pkt.drops = htonl(0);
198 pkt.ts = hton64(ts + 0x00E03AB44A676000ll);
200 if (type == HCI_COMMAND_PKT || type == HCI_EVENT_PKT)
201 pkt.flags |= ntohl(0x02);
203 if (write(fd, &pkt, BTSNOOP_PKT_SIZE) < 0)
206 if (write(fd, buf, size) < 0)
212 static struct vhci_conn *conn_get_by_bdaddr(bdaddr_t *ba)
216 for (i = 0; i < VHCI_MAX_CONN; i++)
217 if (!bacmp(&vconn[i]->dest, ba))
223 static void reset_vdev(void)
225 /* Device settings */
226 vdev.features[0] = 0xff;
227 vdev.features[1] = 0xff;
228 vdev.features[2] = 0x8f;
229 vdev.features[3] = 0xfe;
230 vdev.features[4] = 0x9b;
231 vdev.features[5] = 0xf9;
232 vdev.features[6] = 0x00;
233 vdev.features[7] = 0x80;
235 vdev.features[4] |= 0x40; /* LE Supported */
236 vdev.features[6] |= 0x01; /* Extended Inquiry Response */
237 vdev.features[6] |= 0x02; /* BR/EDR and LE */
238 vdev.features[6] |= 0x08; /* Secure Simple Pairing */
240 memset(vdev.name, 0, sizeof(vdev.name));
241 strncpy((char *) vdev.name, "BlueZ (Virtual HCI)",
242 sizeof(vdev.name) - 1);
244 vdev.dev_class[0] = 0x00;
245 vdev.dev_class[1] = 0x00;
246 vdev.dev_class[2] = 0x00;
248 vdev.scan_enable = 0x00;
249 vdev.ssp_mode = 0x00;
250 vdev.inq_mode = 0x00;
252 memset(vdev.eir_data, 0, sizeof(vdev.eir_data));
254 vdev.le_simul = 0x00;
257 static void command_status(uint16_t ogf, uint16_t ocf, uint8_t status)
259 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
264 *ptr++ = HCI_EVENT_PKT;
267 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
269 he->evt = EVT_CMD_STATUS;
270 he->plen = EVT_CMD_STATUS_SIZE;
272 cs = (void *) ptr; ptr += EVT_CMD_STATUS_SIZE;
276 cs->opcode = htobs(cmd_opcode_pack(ogf, ocf));
278 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
280 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
281 syslog(LOG_ERR, "Can't send event: %s(%d)",
282 strerror(errno), errno);
285 static void command_complete(uint16_t ogf, uint16_t ocf, int plen, void *data)
287 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
288 evt_cmd_complete *cc;
292 *ptr++ = HCI_EVENT_PKT;
295 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
297 he->evt = EVT_CMD_COMPLETE;
298 he->plen = EVT_CMD_COMPLETE_SIZE + plen;
300 cc = (void *) ptr; ptr += EVT_CMD_COMPLETE_SIZE;
303 cc->opcode = htobs(cmd_opcode_pack(ogf, ocf));
306 memcpy(ptr, data, plen);
310 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
312 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
313 syslog(LOG_ERR, "Can't send event: %s(%d)",
314 strerror(errno), errno);
317 static void connect_request(struct vhci_conn *conn)
319 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
320 evt_conn_request *cr;
324 *ptr++ = HCI_EVENT_PKT;
327 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
329 he->evt = EVT_CONN_REQUEST;
330 he->plen = EVT_CONN_REQUEST_SIZE;
332 cr = (void *) ptr; ptr += EVT_CONN_REQUEST_SIZE;
334 bacpy(&cr->bdaddr, &conn->dest);
335 memset(&cr->dev_class, 0, sizeof(cr->dev_class));
336 cr->link_type = ACL_LINK;
338 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
340 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
341 syslog(LOG_ERR, "Can't send event: %s (%d)",
342 strerror(errno), errno);
345 static void connect_complete(struct vhci_conn *conn)
347 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
348 evt_conn_complete *cc;
352 *ptr++ = HCI_EVENT_PKT;
355 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
357 he->evt = EVT_CONN_COMPLETE;
358 he->plen = EVT_CONN_COMPLETE_SIZE;
360 cc = (void *) ptr; ptr += EVT_CONN_COMPLETE_SIZE;
362 bacpy(&cc->bdaddr, &conn->dest);
364 cc->handle = htobs(conn->handle);
365 cc->link_type = ACL_LINK;
366 cc->encr_mode = 0x00;
368 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
370 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
371 syslog(LOG_ERR, "Can't send event: %s (%d)",
372 strerror(errno), errno);
374 /* TODO: Add io_acl_data() handling */
377 static void disconn_complete(struct vhci_conn *conn)
379 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
380 evt_disconn_complete *dc;
384 *ptr++ = HCI_EVENT_PKT;
387 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
389 he->evt = EVT_DISCONN_COMPLETE;
390 he->plen = EVT_DISCONN_COMPLETE_SIZE;
392 dc = (void *) ptr; ptr += EVT_DISCONN_COMPLETE_SIZE;
395 dc->handle = htobs(conn->handle);
398 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
400 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
401 syslog(LOG_ERR, "Can't send event: %s (%d)",
402 strerror(errno), errno);
407 static void num_completed_pkts(struct vhci_conn *conn)
409 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
410 evt_num_comp_pkts *np;
414 *ptr++ = HCI_EVENT_PKT;
417 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
419 he->evt = EVT_NUM_COMP_PKTS;
420 he->plen = EVT_NUM_COMP_PKTS_SIZE;
422 np = (void *) ptr; ptr += EVT_NUM_COMP_PKTS_SIZE;
425 bt_put_le16(conn->handle, ptr);
427 bt_put_le16(vdev.acl_cnt, ptr);
430 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
432 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
433 syslog(LOG_ERR, "Can't send event: %s (%d)",
434 strerror(errno), errno);
437 static uint8_t scan_enable(uint8_t *data)
440 struct epoll_event scan_event;
441 struct sockaddr_in sa;
445 if (!(*data & SCAN_PAGE)) {
446 if (vdev.scan_fd >= 0) {
453 if (vdev.scan_fd >= 0)
456 if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
457 syslog(LOG_ERR, "Can't create socket: %s (%d)",
458 strerror(errno), errno);
463 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
465 baswap(&ba, &vdev.bdaddr);
466 sa.sin_family = AF_INET;
467 memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
468 memcpy(&sa.sin_port, &ba.b[4], sizeof(sa.sin_port));
469 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
470 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
471 strerror(errno), errno);
475 if (listen(sk, 10)) {
476 syslog(LOG_ERR, "Can't listen on socket: %s (%d)",
477 strerror(errno), errno);
481 memset(&scan_event, 0, sizeof(scan_event));
482 scan_event.events = EPOLLIN;
483 scan_event.data.fd = sk;
485 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sk, &scan_event) < 0) {
486 syslog(LOG_ERR, "Failed to setup scan event watch");
501 static void accept_connection(uint8_t *data)
503 accept_conn_req_cp *cp = (void *) data;
504 struct vhci_conn *conn;
506 if (!(conn = conn_get_by_bdaddr(&cp->bdaddr)))
509 connect_complete(conn);
512 static void close_connection(struct vhci_conn *conn)
516 ba2str(&conn->dest, addr);
517 syslog(LOG_INFO, "Closing connection %s handle %d",
522 vconn[conn->handle - 1] = NULL;
523 disconn_complete(conn);
527 static void disconnect(uint8_t *data)
529 disconnect_cp *cp = (void *) data;
530 struct vhci_conn *conn;
533 handle = btohs(cp->handle);
535 if (handle > VHCI_MAX_CONN)
538 if (!(conn = vconn[handle-1]))
541 close_connection(conn);
544 static void create_connection(uint8_t *data)
546 create_conn_cp *cp = (void *) data;
547 struct vhci_link_info info;
548 struct vhci_conn *conn;
549 struct sockaddr_in sa;
553 for (h = 0; h < VHCI_MAX_CONN; h++)
557 syslog(LOG_ERR, "Too many connections");
561 if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
562 syslog(LOG_ERR, "Can't create socket: %s (%d)",
563 strerror(errno), errno);
568 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
570 baswap(&ba, &vdev.bdaddr);
571 sa.sin_family = AF_INET;
572 sa.sin_addr.s_addr = INADDR_ANY; // *(uint32_t *) &ba;
573 sa.sin_port = 0; // *(uint16_t *) &ba.b[4];
574 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
575 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
576 strerror(errno), errno);
581 baswap(&ba, &cp->bdaddr);
582 sa.sin_family = AF_INET;
583 memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
584 memcpy(&sa.sin_port, &ba.b[4], sizeof(sa.sin_port));
585 if (connect(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
586 syslog(LOG_ERR, "Can't connect: %s (%d)",
587 strerror(errno), errno);
593 memset(&info, 0, sizeof(info));
594 bacpy(&info.bdaddr, &vdev.bdaddr);
595 info.link_type = ACL_LINK;
597 write_n(sk, (void *) &info, sizeof(info));
599 if (!(conn = malloc(sizeof(*conn)))) {
600 syslog(LOG_ERR, "Can't alloc new connection: %s (%d)",
601 strerror(errno), errno);
606 memcpy((uint8_t *) &ba, (uint8_t *) &sa.sin_addr, 4);
607 memcpy((uint8_t *) &ba.b[4], (uint8_t *) &sa.sin_port, 2);
608 baswap(&conn->dest, &ba);
611 conn->handle = h + 1;
614 connect_complete(conn);
617 static void hci_link_control(uint16_t ocf, int plen, uint8_t *data)
619 const uint16_t ogf = OGF_LINK_CTL;
622 case OCF_CREATE_CONN:
623 command_status(ogf, ocf, 0x00);
624 create_connection(data);
627 case OCF_ACCEPT_CONN_REQ:
628 command_status(ogf, ocf, 0x00);
629 accept_connection(data);
633 command_status(ogf, ocf, 0x00);
638 command_status(ogf, ocf, 0x01);
643 static void hci_link_policy(uint16_t ocf, int plen, uint8_t *data)
645 const uint16_t ogf = OGF_INFO_PARAM;
649 command_status(ogf, ocf, 0x01);
654 static void hci_host_control(uint16_t ocf, int plen, uint8_t *data)
656 read_scan_enable_rp se;
657 read_local_name_rp ln;
658 read_class_of_dev_rp cd;
659 read_inquiry_mode_rp im;
660 read_ext_inquiry_response_rp ir;
661 read_simple_pairing_mode_rp pm;
662 read_le_host_supported_rp hs;
665 const uint16_t ogf = OGF_HOST_CTL;
671 command_complete(ogf, ocf, 1, &status);
674 case OCF_SET_EVENT_FLT:
676 command_complete(ogf, ocf, 1, &status);
679 case OCF_CHANGE_LOCAL_NAME:
681 memcpy(vdev.name, data, sizeof(vdev.name));
682 command_complete(ogf, ocf, 1, &status);
685 case OCF_READ_LOCAL_NAME:
687 memcpy(ln.name, vdev.name, sizeof(ln.name));
688 command_complete(ogf, ocf, sizeof(ln), &ln);
691 case OCF_WRITE_CONN_ACCEPT_TIMEOUT:
692 case OCF_WRITE_PAGE_TIMEOUT:
694 command_complete(ogf, ocf, 1, &status);
697 case OCF_READ_SCAN_ENABLE:
699 se.enable = vdev.scan_enable;
700 command_complete(ogf, ocf, sizeof(se), &se);
703 case OCF_WRITE_SCAN_ENABLE:
705 vdev.scan_enable = scan_enable(data);
706 command_complete(ogf, ocf, 1, &status);
709 case OCF_WRITE_AUTH_ENABLE:
711 command_complete(ogf, ocf, 1, &status);
714 case OCF_WRITE_ENCRYPT_MODE:
716 command_complete(ogf, ocf, 1, &status);
719 case OCF_READ_CLASS_OF_DEV:
721 memcpy(cd.dev_class, vdev.dev_class, 3);
722 command_complete(ogf, ocf, sizeof(cd), &cd);
725 case OCF_WRITE_CLASS_OF_DEV:
727 memcpy(vdev.dev_class, data, 3);
728 command_complete(ogf, ocf, 1, &status);
731 case OCF_READ_INQUIRY_MODE:
733 im.mode = vdev.inq_mode;
734 command_complete(ogf, ocf, sizeof(im), &im);
737 case OCF_WRITE_INQUIRY_MODE:
739 vdev.inq_mode = data[0];
740 command_complete(ogf, ocf, 1, &status);
743 case OCF_READ_EXT_INQUIRY_RESPONSE:
745 ir.fec = vdev.eir_fec;
746 memcpy(ir.data, vdev.eir_data, HCI_MAX_EIR_LENGTH);
747 command_complete(ogf, ocf, sizeof(ir), &ir);
750 case OCF_WRITE_EXT_INQUIRY_RESPONSE:
752 vdev.eir_fec = data[0];
753 memcpy(vdev.eir_data, data + 1, HCI_MAX_EIR_LENGTH);
754 command_complete(ogf, ocf, 1, &status);
757 case OCF_READ_SIMPLE_PAIRING_MODE:
759 pm.mode = vdev.ssp_mode;
760 command_complete(ogf, ocf, sizeof(pm), &pm);
763 case OCF_WRITE_SIMPLE_PAIRING_MODE:
765 vdev.ssp_mode = data[0];
766 command_complete(ogf, ocf, 1, &status);
769 case OCF_READ_LE_HOST_SUPPORTED:
771 hs.le = vdev.le_mode;
772 hs.simul = vdev.le_simul;
773 command_complete(ogf, ocf, sizeof(hs), &hs);
776 case OCF_WRITE_LE_HOST_SUPPORTED:
778 vdev.le_mode = data[0];
779 vdev.le_simul = data[1];
780 command_complete(ogf, ocf, 1, &status);
784 command_status(ogf, ocf, 0x01);
789 static void hci_info_param(uint16_t ocf, int plen, uint8_t *data)
791 read_local_version_rp lv;
792 read_local_features_rp lf;
793 read_local_ext_features_rp ef;
794 read_buffer_size_rp bs;
797 const uint16_t ogf = OGF_INFO_PARAM;
800 case OCF_READ_LOCAL_VERSION:
803 lv.hci_rev = htobs(0x0000);
805 lv.manufacturer = htobs(63);
806 lv.lmp_subver = htobs(0x0000);
807 command_complete(ogf, ocf, sizeof(lv), &lv);
810 case OCF_READ_LOCAL_FEATURES:
812 memcpy(lf.features, vdev.features, 8);
813 command_complete(ogf, ocf, sizeof(lf), &lf);
816 case OCF_READ_LOCAL_EXT_FEATURES:
821 memcpy(ef.features, vdev.features, 8);
822 } else if (*data == 1) {
825 memset(ef.features, 0, 8);
826 ef.features[0] |= (!!vdev.ssp_mode << 0);
827 ef.features[0] |= (!!vdev.le_mode << 1);
828 ef.features[0] |= (!!vdev.le_simul << 2);
832 memset(ef.features, 0, 8);
834 command_complete(ogf, ocf, sizeof(ef), &ef);
837 case OCF_READ_BUFFER_SIZE:
839 bs.acl_mtu = htobs(VHCI_ACL_MTU);
841 bs.acl_max_pkt = htobs(VHCI_ACL_MAX_PKT);
842 bs.sco_max_pkt = htobs(0);
843 command_complete(ogf, ocf, sizeof(bs), &bs);
846 case OCF_READ_BD_ADDR:
848 bacpy(&ba.bdaddr, &vdev.bdaddr);
849 command_complete(ogf, ocf, sizeof(ba), &ba);
853 command_status(ogf, ocf, 0x01);
858 static void hci_status_param(uint16_t ocf, int plen, uint8_t *data)
860 read_local_amp_info_rp ai;
862 const uint16_t ogf = OGF_STATUS_PARAM;
865 case OCF_READ_LOCAL_AMP_INFO:
866 memset(&ai, 0, sizeof(ai));
869 ai.amp_status = 0x01;
870 ai.max_pdu_size = htobl(L2CAP_DEFAULT_MTU);
871 ai.controller_type = HCI_AMP;
872 ai.max_amp_assoc_length = htobl(HCI_MAX_ACL_SIZE);
873 /* No flushing at all */
874 ai.max_flush_timeout = 0xFFFFFFFF;
875 ai.best_effort_flush_timeout = 0xFFFFFFFF;
877 command_complete(ogf, ocf, sizeof(ai), &ai);
881 command_status(ogf, ocf, 0x01);
886 static void hci_le_control(uint16_t ocf, int plen, uint8_t *data)
888 le_read_buffer_size_rp bs;
890 const uint16_t ogf = OGF_LE_CTL;
893 case OCF_LE_READ_BUFFER_SIZE:
895 bs.pkt_len = htobs(VHCI_ACL_MTU);
896 bs.max_pkt = htobs(VHCI_ACL_MAX_PKT);
897 command_complete(ogf, ocf, sizeof(bs), &bs);
901 command_status(ogf, ocf, 0x01);
906 static void hci_command(uint8_t *data)
912 ch = (hci_command_hdr *) ptr;
913 ptr += HCI_COMMAND_HDR_SIZE;
915 ch->opcode = btohs(ch->opcode);
916 ogf = cmd_opcode_ogf(ch->opcode);
917 ocf = cmd_opcode_ocf(ch->opcode);
921 hci_link_control(ocf, ch->plen, ptr);
924 case OGF_LINK_POLICY:
925 hci_link_policy(ocf, ch->plen, ptr);
929 hci_host_control(ocf, ch->plen, ptr);
933 hci_info_param(ocf, ch->plen, ptr);
936 case OGF_STATUS_PARAM:
937 hci_status_param(ocf, ch->plen, ptr);
941 hci_le_control(ocf, ch->plen, ptr);
945 command_status(ogf, ocf, 0x01);
950 static void hci_acl_data(uint8_t *data)
952 hci_acl_hdr *ah = (void *) data;
953 struct vhci_conn *conn;
956 handle = acl_handle(btohs(ah->handle));
958 if (handle > VHCI_MAX_CONN || !(conn = vconn[handle - 1])) {
959 syslog(LOG_ERR, "Bad connection handle %d", handle);
963 if (write_n(conn->fd, data, btohs(ah->dlen) + HCI_ACL_HDR_SIZE) < 0) {
964 close_connection(conn);
968 if (++vdev.acl_cnt > VHCI_ACL_MAX_PKT - 1) {
969 /* Send num of complete packets event */
970 num_completed_pkts(conn);
976 static void io_acl_data(void *data)
978 struct vhci_conn *conn = data;
979 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
985 if (read_n(conn->fd, ptr, HCI_ACL_HDR_SIZE) <= 0) {
986 close_connection(conn);
991 ptr += HCI_ACL_HDR_SIZE;
993 len = btohs(ah->dlen);
994 if (read_n(conn->fd, ptr, len) <= 0) {
995 close_connection(conn);
999 buf[0] = HCI_ACLDATA_PKT;
1001 flags = acl_flags(btohs(ah->handle));
1002 ah->handle = htobs(acl_handle_pack(conn->handle, flags));
1003 len += HCI_ACL_HDR_SIZE + 1;
1005 write_snoop(vdev.dd, HCI_ACLDATA_PKT, 1, buf, len);
1007 if (write(vdev.dev_fd, buf, len) < 0)
1008 syslog(LOG_ERR, "ACL data write error");
1012 static void io_conn_ind(void)
1014 struct vhci_link_info info;
1015 struct vhci_conn *conn;
1016 struct sockaddr_in sa;
1021 if ((nsk = accept(vdev.scan_fd, (struct sockaddr *) &sa, &len)) < 0)
1024 if (read_n(nsk, &info, sizeof(info)) < 0) {
1025 syslog(LOG_ERR, "Can't read link info");
1029 if (!(conn = malloc(sizeof(*conn)))) {
1030 syslog(LOG_ERR, "Can't alloc new connection");
1035 bacpy(&conn->dest, &info.bdaddr);
1037 for (h = 0; h < VHCI_MAX_CONN; h++)
1041 syslog(LOG_ERR, "Too many connections");
1048 conn->handle = h + 1;
1050 connect_request(conn);
1053 static void io_hci_data(void)
1055 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
1061 len = read(vdev.dev_fd, buf, sizeof(buf));
1063 if (errno == EAGAIN)
1066 syslog(LOG_ERR, "Read failed: %s (%d)", strerror(errno), errno);
1073 write_snoop(vdev.dd, type, 0, buf, len);
1076 case HCI_COMMAND_PKT:
1080 case HCI_ACLDATA_PKT:
1085 syslog(LOG_ERR, "Unknown packet type 0x%2.2x", type);
1090 static int getbdaddrbyname(char *str, bdaddr_t *ba)
1096 /* Check address format */
1097 for (i = 0, n = 0; i < len; i++)
1109 in_addr_t addr = INADDR_LOOPBACK;
1110 uint16_t be16 = htons(atoi(str));
1113 memcpy(&b, &addr, 4);
1114 memcpy(&b.b[4], &be16, sizeof(be16));
1120 fprintf(stderr, "Invalid address format\n");
1125 static void usage(void)
1127 printf("hciemu - HCI emulator ver %s\n", VERSION);
1128 printf("Usage: \n");
1129 printf("\thciemu [options] port_number\n"
1131 "\t[-d device] use specified device node\n"
1132 "\t[-s file] create snoop file\n"
1133 "\t[-n] do not detach\n"
1134 "\t[-h] help, you are looking at it\n");
1137 static const struct option options[] = {
1138 { "device", 1, 0, 'd' },
1139 { "bdaddr", 1, 0, 'b' },
1140 { "snoop", 1, 0, 's' },
1141 { "nodetach", 0, 0, 'n' },
1142 { "help", 0, 0, 'h' },
1146 int main(int argc, char *argv[])
1148 int exitcode = EXIT_FAILURE;
1149 struct sigaction sa;
1150 char *device = NULL, *snoop = NULL;
1152 struct epoll_event device_event;
1153 int dd, opt, detach = 1;
1155 while ((opt=getopt_long(argc, argv, "d:s:nh", options, NULL)) != EOF) {
1158 device = strdup(optarg);
1161 snoop = strdup(optarg);
1184 if (getbdaddrbyname(argv[0], &vdev.bdaddr) < 0)
1189 perror("Can't start daemon");
1194 /* Start logging to syslog and stderr */
1195 openlog("hciemu", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
1196 syslog(LOG_INFO, "HCI emulation daemon ver %s started", VERSION);
1198 memset(&sa, 0, sizeof(sa));
1199 sa.sa_flags = SA_NOCLDSTOP;
1200 sa.sa_handler = SIG_IGN;
1201 sigaction(SIGCHLD, &sa, NULL);
1202 sigaction(SIGPIPE, &sa, NULL);
1204 sa.sa_handler = sig_term;
1205 sigaction(SIGTERM, &sa, NULL);
1206 sigaction(SIGINT, &sa, NULL);
1209 device = strdup(VHCI_DEV);
1211 /* Open and create virtual HCI device */
1212 device_fd = open(device, O_RDWR);
1213 if (device_fd < 0) {
1214 syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1215 device, strerror(errno), errno);
1222 /* Create snoop file */
1224 dd = create_snoop(snoop);
1226 syslog(LOG_ERR, "Can't create snoop file %s: %s (%d)",
1227 snoop, strerror(errno), errno);
1232 /* Create event loop */
1233 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1235 perror("Failed to create epoll descriptor");
1241 vdev.dev_fd = device_fd;
1244 memset(&device_event, 0, sizeof(device_event));
1245 device_event.events = EPOLLIN;
1246 device_event.data.fd = device_fd;
1248 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, device_fd, &device_event) < 0) {
1249 perror("Failed to setup device event watch");
1253 setpriority(PRIO_PROCESS, 0, -19);
1255 /* Start event processor */
1257 struct epoll_event events[MAX_EPOLL_EVENTS];
1263 nfds = epoll_wait(epoll_fd, events, MAX_EPOLL_EVENTS, -1);
1267 for (n = 0; n < nfds; n++) {
1268 if (events[n].data.fd == vdev.dev_fd)
1270 else if (events[n].data.fd == vdev.scan_fd)
1275 exitcode = EXIT_SUCCESS;
1277 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, device_fd, NULL);
1287 syslog(LOG_INFO, "Exit");