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
40 #include <sys/epoll.h>
41 #include <sys/socket.h>
42 #include <sys/resource.h>
45 #include <bluetooth/bluetooth.h>
46 #include <bluetooth/hci.h>
47 #include <bluetooth/hci_lib.h>
48 #include <bluetooth/l2cap.h>
50 #define VHCI_DEV "/dev/vhci"
52 #define VHCI_MAX_CONN 12
54 #define VHCI_ACL_MTU 192
55 #define VHCI_ACL_MAX_PKT 8
65 uint8_t eir_data[HCI_MAX_EIR_LENGTH];
81 struct vhci_link_info {
86 } __attribute__ ((packed));
88 static struct vhci_device vdev;
89 static struct vhci_conn *vconn[VHCI_MAX_CONN];
92 uint8_t id[8]; /* Identification Pattern */
93 uint32_t version; /* Version Number = 1 */
94 uint32_t type; /* Datalink Type */
95 } __attribute__ ((packed));
96 #define BTSNOOP_HDR_SIZE (sizeof(struct btsnoop_hdr))
99 uint32_t size; /* Original Length */
100 uint32_t len; /* Included Length */
101 uint32_t flags; /* Packet Flags */
102 uint32_t drops; /* Cumulative Drops */
103 uint64_t ts; /* Timestamp microseconds */
104 uint8_t data[0]; /* Packet Data */
105 } __attribute__ ((packed));
106 #define BTSNOOP_PKT_SIZE (sizeof(struct btsnoop_pkt))
108 static uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e, 0x6f, 0x6f, 0x70, 0x00 };
110 #define MAX_EPOLL_EVENTS 10
114 static volatile sig_atomic_t __io_canceled = 0;
116 static void sig_term(int sig)
121 static inline int read_n(int fd, void *buf, int len)
123 register int w, t = 0;
125 while (!__io_canceled && len > 0) {
126 if ((w = read(fd, buf, len)) < 0 ){
127 if( errno == EINTR || errno == EAGAIN )
133 len -= w; buf += w; t += w;
138 /* Write exactly len bytes (Signal safe)*/
139 static inline int write_n(int fd, void *buf, int len)
141 register int w, t = 0;
143 while (!__io_canceled && len > 0) {
144 if ((w = write(fd, buf, len)) < 0 ){
145 if( errno == EINTR || errno == EAGAIN )
151 len -= w; buf += w; t += w;
156 static int create_snoop(char *file)
158 struct btsnoop_hdr hdr;
161 fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
165 memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));
166 hdr.version = htonl(1);
167 hdr.type = htonl(1002);
169 len = write(fd, &hdr, BTSNOOP_HDR_SIZE);
175 if (len != BTSNOOP_HDR_SIZE) {
183 static int write_snoop(int fd, int type, int incoming,
184 unsigned char *buf, int len)
186 struct btsnoop_pkt pkt;
194 memset(&tv, 0, sizeof(tv));
195 gettimeofday(&tv, NULL);
196 ts = (tv.tv_sec - 946684800ll) * 1000000ll + tv.tv_usec;
198 pkt.size = htonl(size);
200 pkt.flags = ntohl(incoming & 0x01);
201 pkt.drops = htonl(0);
202 pkt.ts = hton64(ts + 0x00E03AB44A676000ll);
204 if (type == HCI_COMMAND_PKT || type == HCI_EVENT_PKT)
205 pkt.flags |= ntohl(0x02);
207 if (write(fd, &pkt, BTSNOOP_PKT_SIZE) < 0)
210 if (write(fd, buf, size) < 0)
216 static struct vhci_conn *conn_get_by_bdaddr(bdaddr_t *ba)
220 for (i = 0; i < VHCI_MAX_CONN; i++)
221 if (!bacmp(&vconn[i]->dest, ba))
227 static void reset_vdev(void)
229 /* Device settings */
230 vdev.features[0] = 0xff;
231 vdev.features[1] = 0xff;
232 vdev.features[2] = 0x8f;
233 vdev.features[3] = 0xfe;
234 vdev.features[4] = 0x9b;
235 vdev.features[5] = 0xf9;
236 vdev.features[6] = 0x00;
237 vdev.features[7] = 0x80;
239 vdev.features[4] |= 0x40; /* LE Supported */
240 vdev.features[6] |= 0x01; /* Extended Inquiry Response */
241 vdev.features[6] |= 0x02; /* BR/EDR and LE */
242 vdev.features[6] |= 0x08; /* Secure Simple Pairing */
244 memset(vdev.name, 0, sizeof(vdev.name));
245 strncpy((char *) vdev.name, "BlueZ (Virtual HCI)",
246 sizeof(vdev.name) - 1);
248 vdev.dev_class[0] = 0x00;
249 vdev.dev_class[1] = 0x00;
250 vdev.dev_class[2] = 0x00;
252 vdev.scan_enable = 0x00;
253 vdev.ssp_mode = 0x00;
254 vdev.inq_mode = 0x00;
256 memset(vdev.eir_data, 0, sizeof(vdev.eir_data));
258 vdev.le_simul = 0x00;
261 static void command_status(uint16_t ogf, uint16_t ocf, uint8_t status)
263 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
268 *ptr++ = HCI_EVENT_PKT;
271 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
273 he->evt = EVT_CMD_STATUS;
274 he->plen = EVT_CMD_STATUS_SIZE;
276 cs = (void *) ptr; ptr += EVT_CMD_STATUS_SIZE;
280 cs->opcode = htobs(cmd_opcode_pack(ogf, ocf));
282 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
284 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
285 syslog(LOG_ERR, "Can't send event: %s(%d)",
286 strerror(errno), errno);
289 static void command_complete(uint16_t ogf, uint16_t ocf, int plen, void *data)
291 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
292 evt_cmd_complete *cc;
296 *ptr++ = HCI_EVENT_PKT;
299 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
301 he->evt = EVT_CMD_COMPLETE;
302 he->plen = EVT_CMD_COMPLETE_SIZE + plen;
304 cc = (void *) ptr; ptr += EVT_CMD_COMPLETE_SIZE;
307 cc->opcode = htobs(cmd_opcode_pack(ogf, ocf));
310 memcpy(ptr, data, plen);
314 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
316 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
317 syslog(LOG_ERR, "Can't send event: %s(%d)",
318 strerror(errno), errno);
321 static void connect_request(struct vhci_conn *conn)
323 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
324 evt_conn_request *cr;
328 *ptr++ = HCI_EVENT_PKT;
331 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
333 he->evt = EVT_CONN_REQUEST;
334 he->plen = EVT_CONN_REQUEST_SIZE;
336 cr = (void *) ptr; ptr += EVT_CONN_REQUEST_SIZE;
338 bacpy(&cr->bdaddr, &conn->dest);
339 memset(&cr->dev_class, 0, sizeof(cr->dev_class));
340 cr->link_type = ACL_LINK;
342 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
344 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
345 syslog(LOG_ERR, "Can't send event: %s (%d)",
346 strerror(errno), errno);
349 static void connect_complete(struct vhci_conn *conn)
351 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
352 evt_conn_complete *cc;
356 *ptr++ = HCI_EVENT_PKT;
359 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
361 he->evt = EVT_CONN_COMPLETE;
362 he->plen = EVT_CONN_COMPLETE_SIZE;
364 cc = (void *) ptr; ptr += EVT_CONN_COMPLETE_SIZE;
366 bacpy(&cc->bdaddr, &conn->dest);
368 cc->handle = htobs(conn->handle);
369 cc->link_type = ACL_LINK;
370 cc->encr_mode = 0x00;
372 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
374 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
375 syslog(LOG_ERR, "Can't send event: %s (%d)",
376 strerror(errno), errno);
378 /* TODO: Add io_acl_data() handling */
381 static void disconn_complete(struct vhci_conn *conn)
383 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
384 evt_disconn_complete *dc;
388 *ptr++ = HCI_EVENT_PKT;
391 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
393 he->evt = EVT_DISCONN_COMPLETE;
394 he->plen = EVT_DISCONN_COMPLETE_SIZE;
396 dc = (void *) ptr; ptr += EVT_DISCONN_COMPLETE_SIZE;
399 dc->handle = htobs(conn->handle);
402 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
404 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
405 syslog(LOG_ERR, "Can't send event: %s (%d)",
406 strerror(errno), errno);
411 static void num_completed_pkts(struct vhci_conn *conn)
413 uint8_t buf[HCI_MAX_FRAME_SIZE], *ptr = buf;
414 evt_num_comp_pkts *np;
418 *ptr++ = HCI_EVENT_PKT;
421 he = (void *) ptr; ptr += HCI_EVENT_HDR_SIZE;
423 he->evt = EVT_NUM_COMP_PKTS;
424 he->plen = EVT_NUM_COMP_PKTS_SIZE;
426 np = (void *) ptr; ptr += EVT_NUM_COMP_PKTS_SIZE;
429 *((uint16_t *) ptr) = htobs(conn->handle); ptr += 2;
430 *((uint16_t *) ptr) = htobs(vdev.acl_cnt); ptr += 2;
432 write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);
434 if (write(vdev.dev_fd, buf, ptr - buf) < 0)
435 syslog(LOG_ERR, "Can't send event: %s (%d)",
436 strerror(errno), errno);
439 static uint8_t scan_enable(uint8_t *data)
442 struct epoll_event scan_event;
443 struct sockaddr_in sa;
447 if (!(*data & SCAN_PAGE)) {
448 if (vdev.scan_fd >= 0) {
455 if (vdev.scan_fd >= 0)
458 if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
459 syslog(LOG_ERR, "Can't create socket: %s (%d)",
460 strerror(errno), errno);
465 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
467 baswap(&ba, &vdev.bdaddr);
468 sa.sin_family = AF_INET;
469 memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
470 memcpy(&sa.sin_port, &ba.b[4], sizeof(sa.sin_port));
471 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
472 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
473 strerror(errno), errno);
477 if (listen(sk, 10)) {
478 syslog(LOG_ERR, "Can't listen on socket: %s (%d)",
479 strerror(errno), errno);
483 memset(&scan_event, 0, sizeof(scan_event));
484 scan_event.events = EPOLLIN;
485 scan_event.data.fd = sk;
487 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sk, &scan_event) < 0) {
488 syslog(LOG_ERR, "Failed to setup scan event watch");
503 static void accept_connection(uint8_t *data)
505 accept_conn_req_cp *cp = (void *) data;
506 struct vhci_conn *conn;
508 if (!(conn = conn_get_by_bdaddr(&cp->bdaddr)))
511 connect_complete(conn);
514 static void close_connection(struct vhci_conn *conn)
518 ba2str(&conn->dest, addr);
519 syslog(LOG_INFO, "Closing connection %s handle %d",
524 vconn[conn->handle - 1] = NULL;
525 disconn_complete(conn);
529 static void disconnect(uint8_t *data)
531 disconnect_cp *cp = (void *) data;
532 struct vhci_conn *conn;
535 handle = btohs(cp->handle);
537 if (handle > VHCI_MAX_CONN)
540 if (!(conn = vconn[handle-1]))
543 close_connection(conn);
546 static void create_connection(uint8_t *data)
548 create_conn_cp *cp = (void *) data;
549 struct vhci_link_info info;
550 struct vhci_conn *conn;
551 struct sockaddr_in sa;
555 for (h = 0; h < VHCI_MAX_CONN; h++)
559 syslog(LOG_ERR, "Too many connections");
563 if ((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
564 syslog(LOG_ERR, "Can't create socket: %s (%d)",
565 strerror(errno), errno);
570 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
572 baswap(&ba, &vdev.bdaddr);
573 sa.sin_family = AF_INET;
574 sa.sin_addr.s_addr = INADDR_ANY; // *(uint32_t *) &ba;
575 sa.sin_port = 0; // *(uint16_t *) &ba.b[4];
576 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa))) {
577 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
578 strerror(errno), errno);
583 baswap(&ba, &cp->bdaddr);
584 sa.sin_family = AF_INET;
585 memcpy(&sa.sin_addr.s_addr, &ba, sizeof(sa.sin_addr.s_addr));
586 memcpy(&sa.sin_port, &ba.b[4], sizeof(sa.sin_port));
587 if (connect(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
588 syslog(LOG_ERR, "Can't connect: %s (%d)",
589 strerror(errno), errno);
595 memset(&info, 0, sizeof(info));
596 bacpy(&info.bdaddr, &vdev.bdaddr);
597 info.link_type = ACL_LINK;
599 write_n(sk, (void *) &info, sizeof(info));
601 if (!(conn = malloc(sizeof(*conn)))) {
602 syslog(LOG_ERR, "Can't alloc new connection: %s (%d)",
603 strerror(errno), errno);
608 memcpy((uint8_t *) &ba, (uint8_t *) &sa.sin_addr, 4);
609 memcpy((uint8_t *) &ba.b[4], (uint8_t *) &sa.sin_port, 2);
610 baswap(&conn->dest, &ba);
613 conn->handle = h + 1;
616 connect_complete(conn);
619 static void hci_link_control(uint16_t ocf, int plen, uint8_t *data)
621 const uint16_t ogf = OGF_LINK_CTL;
624 case OCF_CREATE_CONN:
625 command_status(ogf, ocf, 0x00);
626 create_connection(data);
629 case OCF_ACCEPT_CONN_REQ:
630 command_status(ogf, ocf, 0x00);
631 accept_connection(data);
635 command_status(ogf, ocf, 0x00);
640 command_status(ogf, ocf, 0x01);
645 static void hci_link_policy(uint16_t ocf, int plen, uint8_t *data)
647 const uint16_t ogf = OGF_INFO_PARAM;
651 command_status(ogf, ocf, 0x01);
656 static void hci_host_control(uint16_t ocf, int plen, uint8_t *data)
658 read_scan_enable_rp se;
659 read_local_name_rp ln;
660 read_class_of_dev_rp cd;
661 read_inquiry_mode_rp im;
662 read_ext_inquiry_response_rp ir;
663 read_simple_pairing_mode_rp pm;
664 read_le_host_supported_rp hs;
667 const uint16_t ogf = OGF_HOST_CTL;
673 command_complete(ogf, ocf, 1, &status);
676 case OCF_SET_EVENT_FLT:
678 command_complete(ogf, ocf, 1, &status);
681 case OCF_CHANGE_LOCAL_NAME:
683 memcpy(vdev.name, data, sizeof(vdev.name));
684 command_complete(ogf, ocf, 1, &status);
687 case OCF_READ_LOCAL_NAME:
689 memcpy(ln.name, vdev.name, sizeof(ln.name));
690 command_complete(ogf, ocf, sizeof(ln), &ln);
693 case OCF_WRITE_CONN_ACCEPT_TIMEOUT:
694 case OCF_WRITE_PAGE_TIMEOUT:
696 command_complete(ogf, ocf, 1, &status);
699 case OCF_READ_SCAN_ENABLE:
701 se.enable = vdev.scan_enable;
702 command_complete(ogf, ocf, sizeof(se), &se);
705 case OCF_WRITE_SCAN_ENABLE:
707 vdev.scan_enable = scan_enable(data);
708 command_complete(ogf, ocf, 1, &status);
711 case OCF_WRITE_AUTH_ENABLE:
713 command_complete(ogf, ocf, 1, &status);
716 case OCF_WRITE_ENCRYPT_MODE:
718 command_complete(ogf, ocf, 1, &status);
721 case OCF_READ_CLASS_OF_DEV:
723 memcpy(cd.dev_class, vdev.dev_class, 3);
724 command_complete(ogf, ocf, sizeof(cd), &cd);
727 case OCF_WRITE_CLASS_OF_DEV:
729 memcpy(vdev.dev_class, data, 3);
730 command_complete(ogf, ocf, 1, &status);
733 case OCF_READ_INQUIRY_MODE:
735 im.mode = vdev.inq_mode;
736 command_complete(ogf, ocf, sizeof(im), &im);
739 case OCF_WRITE_INQUIRY_MODE:
741 vdev.inq_mode = data[0];
742 command_complete(ogf, ocf, 1, &status);
745 case OCF_READ_EXT_INQUIRY_RESPONSE:
747 ir.fec = vdev.eir_fec;
748 memcpy(ir.data, vdev.eir_data, HCI_MAX_EIR_LENGTH);
749 command_complete(ogf, ocf, sizeof(ir), &ir);
752 case OCF_WRITE_EXT_INQUIRY_RESPONSE:
754 vdev.eir_fec = data[0];
755 memcpy(vdev.eir_data, data + 1, HCI_MAX_EIR_LENGTH);
756 command_complete(ogf, ocf, 1, &status);
759 case OCF_READ_SIMPLE_PAIRING_MODE:
761 pm.mode = vdev.ssp_mode;
762 command_complete(ogf, ocf, sizeof(pm), &pm);
765 case OCF_WRITE_SIMPLE_PAIRING_MODE:
767 vdev.ssp_mode = data[0];
768 command_complete(ogf, ocf, 1, &status);
771 case OCF_READ_LE_HOST_SUPPORTED:
773 hs.le = vdev.le_mode;
774 hs.simul = vdev.le_simul;
775 command_complete(ogf, ocf, sizeof(hs), &hs);
778 case OCF_WRITE_LE_HOST_SUPPORTED:
780 vdev.le_mode = data[0];
781 vdev.le_simul = data[1];
782 command_complete(ogf, ocf, 1, &status);
786 command_status(ogf, ocf, 0x01);
791 static void hci_info_param(uint16_t ocf, int plen, uint8_t *data)
793 read_local_version_rp lv;
794 read_local_features_rp lf;
795 read_local_ext_features_rp ef;
796 read_buffer_size_rp bs;
799 const uint16_t ogf = OGF_INFO_PARAM;
802 case OCF_READ_LOCAL_VERSION:
805 lv.hci_rev = htobs(0x0000);
807 lv.manufacturer = htobs(63);
808 lv.lmp_subver = htobs(0x0000);
809 command_complete(ogf, ocf, sizeof(lv), &lv);
812 case OCF_READ_LOCAL_FEATURES:
814 memcpy(lf.features, vdev.features, 8);
815 command_complete(ogf, ocf, sizeof(lf), &lf);
818 case OCF_READ_LOCAL_EXT_FEATURES:
823 memcpy(ef.features, vdev.features, 8);
824 } else if (*data == 1) {
827 memset(ef.features, 0, 8);
828 ef.features[0] |= (!!vdev.ssp_mode << 0);
829 ef.features[0] |= (!!vdev.le_mode << 1);
830 ef.features[0] |= (!!vdev.le_simul << 2);
834 memset(ef.features, 0, 8);
836 command_complete(ogf, ocf, sizeof(ef), &ef);
839 case OCF_READ_BUFFER_SIZE:
841 bs.acl_mtu = htobs(VHCI_ACL_MTU);
843 bs.acl_max_pkt = htobs(VHCI_ACL_MAX_PKT);
844 bs.sco_max_pkt = htobs(0);
845 command_complete(ogf, ocf, sizeof(bs), &bs);
848 case OCF_READ_BD_ADDR:
850 bacpy(&ba.bdaddr, &vdev.bdaddr);
851 command_complete(ogf, ocf, sizeof(ba), &ba);
855 command_status(ogf, ocf, 0x01);
860 static void hci_status_param(uint16_t ocf, int plen, uint8_t *data)
862 read_local_amp_info_rp ai;
864 const uint16_t ogf = OGF_STATUS_PARAM;
867 case OCF_READ_LOCAL_AMP_INFO:
868 memset(&ai, 0, sizeof(ai));
871 ai.amp_status = 0x01;
872 ai.max_pdu_size = htobl(L2CAP_DEFAULT_MTU);
873 ai.controller_type = HCI_AMP;
874 ai.max_amp_assoc_length = htobl(HCI_MAX_ACL_SIZE);
875 /* No flushing at all */
876 ai.max_flush_timeout = 0xFFFFFFFF;
877 ai.best_effort_flush_timeout = 0xFFFFFFFF;
879 command_complete(ogf, ocf, sizeof(ai), &ai);
883 command_status(ogf, ocf, 0x01);
888 static void hci_le_control(uint16_t ocf, int plen, uint8_t *data)
890 le_read_buffer_size_rp bs;
892 const uint16_t ogf = OGF_LE_CTL;
895 case OCF_LE_READ_BUFFER_SIZE:
897 bs.pkt_len = htobs(VHCI_ACL_MTU);
898 bs.max_pkt = htobs(VHCI_ACL_MAX_PKT);
899 command_complete(ogf, ocf, sizeof(bs), &bs);
903 command_status(ogf, ocf, 0x01);
908 static void hci_command(uint8_t *data)
914 ch = (hci_command_hdr *) ptr;
915 ptr += HCI_COMMAND_HDR_SIZE;
917 ch->opcode = btohs(ch->opcode);
918 ogf = cmd_opcode_ogf(ch->opcode);
919 ocf = cmd_opcode_ocf(ch->opcode);
923 hci_link_control(ocf, ch->plen, ptr);
926 case OGF_LINK_POLICY:
927 hci_link_policy(ocf, ch->plen, ptr);
931 hci_host_control(ocf, ch->plen, ptr);
935 hci_info_param(ocf, ch->plen, ptr);
938 case OGF_STATUS_PARAM:
939 hci_status_param(ocf, ch->plen, ptr);
943 hci_le_control(ocf, ch->plen, ptr);
947 command_status(ogf, ocf, 0x01);
952 static void hci_acl_data(uint8_t *data)
954 hci_acl_hdr *ah = (void *) data;
955 struct vhci_conn *conn;
958 handle = acl_handle(btohs(ah->handle));
960 if (handle > VHCI_MAX_CONN || !(conn = vconn[handle - 1])) {
961 syslog(LOG_ERR, "Bad connection handle %d", handle);
965 if (write_n(conn->fd, data, btohs(ah->dlen) + HCI_ACL_HDR_SIZE) < 0) {
966 close_connection(conn);
970 if (++vdev.acl_cnt > VHCI_ACL_MAX_PKT - 1) {
971 /* Send num of complete packets event */
972 num_completed_pkts(conn);
978 static void io_acl_data(void *data)
980 struct vhci_conn *conn = data;
981 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
987 if (read_n(conn->fd, ptr, HCI_ACL_HDR_SIZE) <= 0) {
988 close_connection(conn);
993 ptr += HCI_ACL_HDR_SIZE;
995 len = btohs(ah->dlen);
996 if (read_n(conn->fd, ptr, len) <= 0) {
997 close_connection(conn);
1001 buf[0] = HCI_ACLDATA_PKT;
1003 flags = acl_flags(btohs(ah->handle));
1004 ah->handle = htobs(acl_handle_pack(conn->handle, flags));
1005 len += HCI_ACL_HDR_SIZE + 1;
1007 write_snoop(vdev.dd, HCI_ACLDATA_PKT, 1, buf, len);
1009 if (write(vdev.dev_fd, buf, len) < 0)
1010 syslog(LOG_ERR, "ACL data write error");
1014 static void io_conn_ind(void)
1016 struct vhci_link_info info;
1017 struct vhci_conn *conn;
1018 struct sockaddr_in sa;
1023 if ((nsk = accept(vdev.scan_fd, (struct sockaddr *) &sa, &len)) < 0)
1026 if (read_n(nsk, &info, sizeof(info)) < 0) {
1027 syslog(LOG_ERR, "Can't read link info");
1031 if (!(conn = malloc(sizeof(*conn)))) {
1032 syslog(LOG_ERR, "Can't alloc new connection");
1037 bacpy(&conn->dest, &info.bdaddr);
1039 for (h = 0; h < VHCI_MAX_CONN; h++)
1043 syslog(LOG_ERR, "Too many connections");
1050 conn->handle = h + 1;
1052 connect_request(conn);
1055 static void io_hci_data(void)
1057 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
1063 len = read(vdev.dev_fd, buf, sizeof(buf));
1065 if (errno == EAGAIN)
1068 syslog(LOG_ERR, "Read failed: %s (%d)", strerror(errno), errno);
1075 write_snoop(vdev.dd, type, 0, buf, len);
1078 case HCI_COMMAND_PKT:
1082 case HCI_ACLDATA_PKT:
1087 syslog(LOG_ERR, "Unknown packet type 0x%2.2x", type);
1092 static int getbdaddrbyname(char *str, bdaddr_t *ba)
1098 /* Check address format */
1099 for (i = 0, n = 0; i < len; i++)
1111 in_addr_t addr = INADDR_LOOPBACK;
1112 uint16_t be16 = htons(atoi(str));
1115 memcpy(&b, &addr, 4);
1116 memcpy(&b.b[4], &be16, sizeof(be16));
1122 fprintf(stderr, "Invalid address format\n");
1127 static void usage(void)
1129 printf("hciemu - HCI emulator ver %s\n", VERSION);
1130 printf("Usage: \n");
1131 printf("\thciemu [options] port_number\n"
1133 "\t[-d device] use specified device node\n"
1134 "\t[-s file] create snoop file\n"
1135 "\t[-n] do not detach\n"
1136 "\t[-h] help, you are looking at it\n");
1139 static const struct option options[] = {
1140 { "device", 1, 0, 'd' },
1141 { "bdaddr", 1, 0, 'b' },
1142 { "snoop", 1, 0, 's' },
1143 { "nodetach", 0, 0, 'n' },
1144 { "help", 0, 0, 'h' },
1148 int main(int argc, char *argv[])
1150 int exitcode = EXIT_FAILURE;
1151 struct sigaction sa;
1152 char *device = NULL, *snoop = NULL;
1154 struct epoll_event device_event;
1155 int dd, opt, detach = 1;
1157 while ((opt=getopt_long(argc, argv, "d:s:nh", options, NULL)) != EOF) {
1160 device = strdup(optarg);
1163 snoop = strdup(optarg);
1186 if (getbdaddrbyname(argv[0], &vdev.bdaddr) < 0)
1191 perror("Can't start daemon");
1196 /* Start logging to syslog and stderr */
1197 openlog("hciemu", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
1198 syslog(LOG_INFO, "HCI emulation daemon ver %s started", VERSION);
1200 memset(&sa, 0, sizeof(sa));
1201 sa.sa_flags = SA_NOCLDSTOP;
1202 sa.sa_handler = SIG_IGN;
1203 sigaction(SIGCHLD, &sa, NULL);
1204 sigaction(SIGPIPE, &sa, NULL);
1206 sa.sa_handler = sig_term;
1207 sigaction(SIGTERM, &sa, NULL);
1208 sigaction(SIGINT, &sa, NULL);
1211 device = strdup(VHCI_DEV);
1213 /* Open and create virtual HCI device */
1214 device_fd = open(device, O_RDWR);
1215 if (device_fd < 0) {
1216 syslog(LOG_ERR, "Can't open device %s: %s (%d)",
1217 device, strerror(errno), errno);
1224 /* Create snoop file */
1226 dd = create_snoop(snoop);
1228 syslog(LOG_ERR, "Can't create snoop file %s: %s (%d)",
1229 snoop, strerror(errno), errno);
1234 /* Create event loop */
1235 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1237 perror("Failed to create epoll descriptor");
1243 vdev.dev_fd = device_fd;
1246 memset(&device_event, 0, sizeof(device_event));
1247 device_event.events = EPOLLIN;
1248 device_event.data.fd = device_fd;
1250 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, device_fd, &device_event) < 0) {
1251 perror("Failed to setup device event watch");
1255 setpriority(PRIO_PROCESS, 0, -19);
1257 /* Start event processor */
1259 struct epoll_event events[MAX_EPOLL_EVENTS];
1265 nfds = epoll_wait(epoll_fd, events, MAX_EPOLL_EVENTS, -1);
1269 for (n = 0; n < nfds; n++) {
1270 if (events[n].data.fd == vdev.dev_fd)
1272 else if (events[n].data.fd == vdev.scan_fd)
1277 exitcode = EXIT_SUCCESS;
1279 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, device_fd, NULL);
1289 syslog(LOG_INFO, "Exit");