1 /* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * hci_h4_transport.c 39 * 40 * HCI Transport API implementation for basic H4 protocol over POSIX 41 * 42 * Created by Matthias Ringwald on 4/29/09. 43 */ 44 45 #include "btstack-config.h" 46 47 #include <termios.h> /* POSIX terminal control definitions */ 48 #include <fcntl.h> /* File control definitions */ 49 #include <unistd.h> /* UNIX standard function definitions */ 50 #include <stdio.h> 51 #include <string.h> 52 #include <pthread.h> 53 54 #include "debug.h" 55 #include "hci.h" 56 #include "hci_transport.h" 57 #include "hci_dump.h" 58 59 static int h4_process(struct data_source *ds); 60 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 61 static hci_uart_config_t *hci_uart_config; 62 63 typedef enum { 64 H4_W4_PACKET_TYPE, 65 H4_W4_EVENT_HEADER, 66 H4_W4_ACL_HEADER, 67 H4_W4_PAYLOAD, 68 } H4_STATE; 69 70 typedef struct hci_transport_h4 { 71 hci_transport_t transport; 72 data_source_t *ds; 73 int uart_fd; // different from ds->fd for HCI reader thread 74 /* power management support, e.g. used by iOS */ 75 timer_source_t sleep_timer; 76 } hci_transport_h4_t; 77 78 79 // single instance 80 static hci_transport_h4_t * hci_transport_h4 = NULL; 81 82 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 83 84 // packet reader state machine 85 static H4_STATE h4_state; 86 static int bytes_to_read; 87 static int read_pos; 88 89 static uint8_t hci_packet[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 90 91 static int h4_open(void *transport_config){ 92 hci_uart_config = (hci_uart_config_t*) transport_config; 93 struct termios toptions; 94 int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; 95 int fd = open(hci_uart_config->device_name, flags); 96 if (fd == -1) { 97 perror("init_serialport: Unable to open port "); 98 perror(hci_uart_config->device_name); 99 return -1; 100 } 101 102 if (tcgetattr(fd, &toptions) < 0) { 103 perror("init_serialport: Couldn't get term attributes"); 104 return -1; 105 } 106 107 speed_t brate = hci_uart_config->baudrate_init; // let you override switch below if needed 108 switch(hci_uart_config->baudrate_init) { 109 case 57600: brate=B57600; break; 110 case 115200: brate=B115200; break; 111 #ifdef B230400 112 case 230400: brate=B230400; break; 113 #endif 114 #ifdef B460800 115 case 460800: brate=B460800; break; 116 #endif 117 #ifdef B921600 118 case 921600: brate=B921600; break; 119 #endif 120 } 121 cfsetospeed(&toptions, brate); 122 cfsetispeed(&toptions, brate); 123 124 // 8N1 125 toptions.c_cflag &= ~PARENB; 126 toptions.c_cflag &= ~CSTOPB; 127 toptions.c_cflag &= ~CSIZE; 128 toptions.c_cflag |= CS8; 129 130 if (hci_uart_config->flowcontrol) { 131 // with flow control 132 toptions.c_cflag |= CRTSCTS; 133 } else { 134 // no flow control 135 toptions.c_cflag &= ~CRTSCTS; 136 } 137 138 toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines 139 toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl 140 141 toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw 142 toptions.c_oflag &= ~OPOST; // make raw 143 144 // see: http://unixwiz.net/techtips/termios-vmin-vtime.html 145 toptions.c_cc[VMIN] = 1; 146 toptions.c_cc[VTIME] = 0; 147 148 if( tcsetattr(fd, TCSANOW, &toptions) < 0) { 149 perror("init_serialport: Couldn't set term attributes"); 150 return -1; 151 } 152 153 // set up data_source 154 hci_transport_h4->ds = (data_source_t*) malloc(sizeof(data_source_t)); 155 if (!hci_transport_h4->ds) return -1; 156 hci_transport_h4->uart_fd = fd; 157 hci_transport_h4->ds->fd = fd; 158 hci_transport_h4->ds->process = h4_process; 159 run_loop_add_data_source(hci_transport_h4->ds); 160 161 // init state machine 162 bytes_to_read = 1; 163 h4_state = H4_W4_PACKET_TYPE; 164 read_pos = 0; 165 166 // bring bluetooth module into defined state 167 uint8_t reset[] = { 0x01, 0x03, 0x0c, 0x00}; 168 write(fd, &reset, sizeof(reset)); 169 usleep(100000); // 100 ms 170 write(fd, &reset, sizeof(reset)); 171 usleep(100000); // 100 ms 172 tcflush(fd, TCIOFLUSH); 173 174 return 0; 175 } 176 177 static int h4_close(void *transport_config){ 178 // first remove run loop handler 179 run_loop_remove_data_source(hci_transport_h4->ds); 180 181 // close device 182 close(hci_transport_h4->ds->fd); 183 184 // free struct 185 free(hci_transport_h4->ds); 186 hci_transport_h4->ds = NULL; 187 return 0; 188 } 189 190 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 191 if (hci_transport_h4->ds == NULL) return -1; 192 if (hci_transport_h4->uart_fd == 0) return -1; 193 194 hci_dump_packet( (uint8_t) packet_type, 0, packet, size); 195 char *data = (char*) packet; 196 int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 197 while (bytes_written < 1) { 198 usleep(5000); 199 bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 200 }; 201 while (size > 0) { 202 int bytes_written = write(hci_transport_h4->uart_fd, data, size); 203 if (bytes_written < 0) { 204 usleep(5000); 205 continue; 206 } 207 data += bytes_written; 208 size -= bytes_written; 209 } 210 return 0; 211 } 212 213 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 214 packet_handler = handler; 215 } 216 217 static void h4_deliver_packet(void){ 218 if (read_pos < 3) return; // sanity check 219 hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1); 220 packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 221 222 h4_state = H4_W4_PACKET_TYPE; 223 read_pos = 0; 224 bytes_to_read = 1; 225 } 226 227 static void h4_statemachine(void){ 228 switch (h4_state) { 229 230 case H4_W4_PACKET_TYPE: 231 if (hci_packet[0] == HCI_EVENT_PACKET){ 232 bytes_to_read = HCI_EVENT_HEADER_SIZE; 233 h4_state = H4_W4_EVENT_HEADER; 234 } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){ 235 bytes_to_read = HCI_ACL_HEADER_SIZE; 236 h4_state = H4_W4_ACL_HEADER; 237 } else { 238 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet[0]); 239 read_pos = 0; 240 bytes_to_read = 1; 241 } 242 break; 243 244 case H4_W4_EVENT_HEADER: 245 bytes_to_read = hci_packet[2]; 246 h4_state = H4_W4_PAYLOAD; 247 break; 248 249 case H4_W4_ACL_HEADER: 250 bytes_to_read = READ_BT_16( hci_packet, 3); 251 h4_state = H4_W4_PAYLOAD; 252 break; 253 254 case H4_W4_PAYLOAD: 255 h4_deliver_packet(); 256 break; 257 default: 258 break; 259 } 260 } 261 262 static int h4_process(struct data_source *ds) { 263 if (hci_transport_h4->uart_fd == 0) return -1; 264 265 int read_now = bytes_to_read; 266 267 // read up to bytes_to_read data in 268 ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now); 269 // printf("h4_process: bytes read %u\n", bytes_read); 270 if (bytes_read < 0) { 271 return bytes_read; 272 } 273 274 // hexdump(&hci_packet[read_pos], bytes_read); 275 276 bytes_to_read -= bytes_read; 277 read_pos += bytes_read; 278 if (bytes_to_read > 0) { 279 return 0; 280 } 281 282 h4_statemachine(); 283 return 0; 284 } 285 286 static const char * h4_get_transport_name(void){ 287 return "H4"; 288 } 289 290 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 291 } 292 293 // get h4 singleton 294 hci_transport_t * hci_transport_h4_instance() { 295 if (hci_transport_h4 == NULL) { 296 hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t)); 297 hci_transport_h4->ds = NULL; 298 hci_transport_h4->transport.open = h4_open; 299 hci_transport_h4->transport.close = h4_close; 300 hci_transport_h4->transport.send_packet = h4_send_packet; 301 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 302 hci_transport_h4->transport.get_transport_name = h4_get_transport_name; 303 hci_transport_h4->transport.set_baudrate = NULL; 304 hci_transport_h4->transport.can_send_packet_now = NULL; 305 } 306 return (hci_transport_t *) hci_transport_h4; 307 } 308