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