1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2011-2014 Intel Corporation
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
21 #include <sys/socket.h>
25 #include <arpa/inet.h>
27 #include "src/shared/btsnoop.h"
30 static int ellisys_fd = -1;
31 static uint16_t ellisys_index = 0xffff;
33 void ellisys_enable(const char *server, uint16_t port)
35 struct sockaddr_in addr;
38 if (ellisys_fd >= 0) {
39 fprintf(stderr, "Ellisys injection already enabled\n");
43 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
45 perror("Failed to open UDP injection socket");
49 memset(&addr, 0, sizeof(addr));
50 addr.sin_family = AF_INET;
51 addr.sin_addr.s_addr = inet_addr(server);
52 addr.sin_port = htons(port);
54 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
55 perror("Failed to connect UDP injection socket");
63 void ellisys_inject_hci(struct timeval *tv, uint16_t index, uint16_t opcode,
64 const void *data, uint16_t size)
67 /* HCI Injection Service, Version 1 */
69 /* DateTimeNs Object */
70 0x02, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 /* Bitrate Object, 12000000 bps */
73 0x80, 0x00, 0x1b, 0x37, 0x4b,
74 /* HCI Packet Type Object */
76 /* HCI Packet Data Object */
91 if (ellisys_index == 0xffff)
92 ellisys_index = index;
94 if (index != ellisys_index)
100 nsec = ((tm.tm_sec + (tm.tm_min * 60) +
101 (tm.tm_hour * 3600)) * 1000000l + tv->tv_usec) * 1000l;
103 msg[4] = (1900 + tm.tm_year) & 0xff;
104 msg[5] = (1900 + tm.tm_year) >> 8;
105 msg[6] = (tm.tm_mon + 1) & 0xff;
106 msg[7] = tm.tm_mday & 0xff;
107 msg[8] = (nsec & 0x0000000000ffl);
108 msg[9] = (nsec & 0x00000000ff00l) >> 8;
109 msg[10] = (nsec & 0x000000ff0000l) >> 16;
110 msg[11] = (nsec & 0x0000ff000000l) >> 24;
111 msg[12] = (nsec & 0x00ff00000000l) >> 32;
112 msg[13] = (nsec & 0xff0000000000l) >> 40;
115 case BTSNOOP_OPCODE_COMMAND_PKT:
118 case BTSNOOP_OPCODE_EVENT_PKT:
121 case BTSNOOP_OPCODE_ACL_TX_PKT:
124 case BTSNOOP_OPCODE_ACL_RX_PKT:
127 case BTSNOOP_OPCODE_SCO_TX_PKT:
130 case BTSNOOP_OPCODE_SCO_RX_PKT:
133 case BTSNOOP_OPCODE_ISO_TX_PKT:
136 case BTSNOOP_OPCODE_ISO_RX_PKT:
143 iov[0].iov_base = msg;
144 iov[0].iov_len = sizeof(msg);
147 iov[1].iov_base = (void *) data;
148 iov[1].iov_len = size;
153 if (writev(ellisys_fd, iov, iovcnt) < 0)
154 perror("Failed to send Ellisys injection packet");