1 #include <hardware/bluetooth.h>
2 #include <netinet/in.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 
9 #include "osi/include/osi.h"
10 
11 typedef int (*handler_t)(int argc, char **argv);
12 
13 typedef enum {
14   HCI_PACKET_COMMAND  = 1,
15   HCI_PACKET_ACL_DATA = 2,
16   HCI_PACKET_SCO_DATA = 3,
17   HCI_PACKET_EVENT    = 4,
18 } hci_packet_t;
19 
20 typedef struct {
21   const char *name;
22   const char *help;
23   handler_t handler;
24 } command_t;
25 
26 static int help(int argc, char **argv);
27 static int set_discoverable(int argc, char **argv);
28 static int set_name(int argc, char **argv);
29 static int set_pcm_loopback(int argc, char **argv);
30 
31 static bool write_hci_command(hci_packet_t type, const void *packet, size_t length);
32 static const command_t *find_command(const char *name);
33 static void usage(const char *name);
34 
35 static const command_t commands[] = {
36   { "help", "<command> - shows help text for <command>.", help },
37   { "setDiscoverable", "(true|false) - whether the controller should be discoverable.", set_discoverable },
38   { "setName", "<name> - sets the device's Bluetooth name to <name>.", set_name },
39   { "setPcmLoopback", "(true|false) - enables or disables PCM loopback on the controller.", set_pcm_loopback },
40 };
41 
help(int argc,char ** argv)42 static int help(int argc, char **argv) {
43   if (!argc) {
44     printf("No help command specified.\n");
45     return 1;
46   }
47 
48   const command_t *command = find_command(argv[0]);
49   if (!command) {
50     printf("No command named '%s'.\n", argv[0]);
51     return 2;
52   }
53 
54   printf("%s %s\n", argv[0], command->help);
55   return 0;
56 }
57 
set_discoverable(int argc,char ** argv)58 static int set_discoverable(int argc, char **argv) {
59   if (argc != 1) {
60     printf("Discoverable mode not specified.\n");
61     return 1;
62   }
63 
64   if (strcmp(argv[0], "true") && strcmp(argv[0], "false")) {
65     printf("Invalid discoverable mode '%s'.\n", argv[0]);
66     return 2;
67   }
68 
69   uint8_t packet[] = { 0x1A, 0x0C, 0x01, 0x00 };
70   if (argv[0][0] == 't')
71     packet[ARRAY_SIZE(packet) - 1] = 0x03;
72 
73   return !write_hci_command(HCI_PACKET_COMMAND, packet, ARRAY_SIZE(packet));
74 }
75 
set_name(int argc,char ** argv)76 static int set_name(int argc, char **argv) {
77   if (argc != 1) {
78     printf("Device name not specified.\n");
79     return 1;
80   }
81 
82   size_t len = strlen(argv[0]);
83   if (len > 247) {
84     printf("Device name cannot exceed 247 bytes.\n");
85     return 2;
86   }
87 
88   uint8_t packet[251] = { 0x13, 0x0C, 248 };
89   memcpy(&packet[3], argv[0], len + 1);
90 
91   if (!write_hci_command(HCI_PACKET_COMMAND, packet, sizeof(packet)))
92     return 1;
93 
94   memset(&packet[0], sizeof(packet), 0);
95   packet[0] = 0x52;
96   packet[1] = 0x0C;
97   packet[2] = 0xF1;  // HCI command packet length.
98   packet[3] = 0x01;  // FEC required.
99   packet[4] = len + 1;
100   packet[5] = 0x09;  // Device name field tag.
101   memcpy(&packet[6], argv[0], len);
102   return !write_hci_command(HCI_PACKET_COMMAND, packet, 0xF4);
103 }
104 
set_pcm_loopback(int argc,char ** argv)105 static int set_pcm_loopback(int argc, char **argv) {
106   if (argc != 1) {
107     printf("PCM loopback mode not specified.\n");
108     return 1;
109   }
110 
111   if (strcmp(argv[0], "true") && strcmp(argv[0], "false")) {
112     printf("Invalid PCM mode '%s'.\n", argv[0]);
113     return 2;
114   }
115 
116   uint8_t packet[] = { 0x24, 0xFC, 0x01, 0x00 };
117   if (argv[0][0] == 't')
118     packet[ARRAY_SIZE(packet) - 1] = 0x01;
119 
120   return !write_hci_command(HCI_PACKET_COMMAND, packet, ARRAY_SIZE(packet));
121 }
122 
main(int argc,char ** argv)123 int main(int argc, char **argv) {
124   if (argc < 2) {
125     usage(argv[0]);
126     return -1;
127   }
128 
129   const command_t *command = find_command(argv[1]);
130   if (!command) {
131     printf("Unrecognized command '%s'.\n", argv[1]);
132     return -2;
133   }
134 
135   if (!command->handler) {
136     printf("Unhandled command '%s'.\n", argv[1]);
137     return -3;
138   }
139 
140   return command->handler(argc - 2, &argv[2]);
141 }
142 
write_hci_command(hci_packet_t type,const void * packet,size_t length)143 static bool write_hci_command(hci_packet_t type, const void *packet, size_t length) {
144   int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
145   if (sock == INVALID_FD)
146     goto error;
147 
148   struct sockaddr_in addr;
149   addr.sin_family = AF_INET;
150   addr.sin_addr.s_addr = htonl(0x7F000001);
151   addr.sin_port = htons(8873);
152   if (connect(sock, (const struct sockaddr *)&addr, sizeof(addr)) == -1)
153     goto error;
154 
155   if (send(sock, &type, 1, 0) != 1)
156     goto error;
157 
158   if (send(sock, &length, 2, 0) != 2)
159     goto error;
160 
161   if (send(sock, packet, length, 0) != (ssize_t)length)
162     goto error;
163 
164   close(sock);
165   return true;
166 
167 error:;
168   close(sock);
169   return false;
170 }
171 
find_command(const char * name)172 static const command_t *find_command(const char *name) {
173   for (size_t i = 0; i < ARRAY_SIZE(commands); ++i)
174     if (!strcmp(commands[i].name, name))
175       return &commands[i];
176   return NULL;
177 }
178 
usage(const char * name)179 static void usage(const char *name) {
180   printf("Usage: %s <command> [options]\n", name);
181   printf("Commands:\n");
182   for (size_t i = 0; i < ARRAY_SIZE(commands); ++i)
183     printf("  %s\n", commands[i].name);
184   printf("For detailed help on a command, run '%s help <command>'.\n", name);
185 }
186