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