1 /* 2 * Copyright (C) 2009 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 * 17 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 21 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * hci_h4_transport.c 34 * 35 * HCI Transport API implementation for basic H4 protocol 36 * 37 * Created by Matthias Ringwald on 4/29/09. 38 */ 39 40 #include "../config.h" 41 42 #undef USE_NETGRAPH 43 44 // don't enforce wake after 3s idle 45 #define HCI_WAKE_TIMER_MS 3000 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 60 #if defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) 61 #define BT_WAKE_DEVICE "/dev/btwake" 62 static int fd_wake = 0; 63 #endif 64 65 typedef enum { 66 H4_W4_PACKET_TYPE, 67 H4_W4_EVENT_HEADER, 68 H4_W4_ACL_HEADER, 69 H4_W4_PAYLOAD, 70 } H4_STATE; 71 72 typedef struct hci_transport_h4 { 73 hci_transport_t transport; 74 data_source_t *ds; 75 int uart_fd; // different from ds->fd for HCI reader thread 76 #if defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) 77 /* iPhone power management support */ 78 // static char * wake_device = NULL; 79 // static int wake_fd = 0; 80 timer_source_t sleep_timer; 81 #endif 82 } hci_transport_h4_t; 83 84 // single instance 85 static hci_transport_h4_t * hci_transport_h4 = NULL; 86 87 static int h4_process(struct data_source *ds); 88 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 89 static hci_uart_config_t *hci_uart_config; 90 91 #if defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) 92 static void h4_wake_on(void); 93 static void h4_wake_off(void); 94 static void h4_wake_timeout(struct timer *ts); 95 #endif 96 97 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 98 99 // packet reader state machine 100 static H4_STATE h4_state; 101 static int bytes_to_read; 102 static int read_pos; 103 104 static uint8_t hci_packet[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 105 106 #if defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) 107 static void h4_wake_on(void) 108 { 109 if (!fd_wake) { 110 fd_wake = open(BT_WAKE_DEVICE, O_RDWR); 111 usleep(10000); 112 } 113 run_loop_remove_timer(&hci_transport_h4->sleep_timer); 114 run_loop_set_timer(&hci_transport_h4->sleep_timer, HCI_WAKE_TIMER_MS); 115 hci_transport_h4->sleep_timer.process = h4_wake_timeout; 116 run_loop_add_timer(&hci_transport_h4->sleep_timer); 117 118 return; 119 } 120 121 static void h4_wake_off(void) 122 { 123 run_loop_remove_timer(&hci_transport_h4->sleep_timer); 124 if (fd_wake) { 125 close(fd_wake); 126 fd_wake = 0; 127 } 128 return; 129 } 130 131 static void h4_wake_timeout(struct timer *ts) 132 { 133 h4_wake_off(); 134 } 135 136 #endif /* defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) */ 137 138 // prototypes 139 static int h4_open(void *transport_config){ 140 hci_uart_config = (hci_uart_config_t*) transport_config; 141 struct termios toptions; 142 int flags = O_RDWR | O_NOCTTY; 143 int fd = open(hci_uart_config->device_name, flags); 144 if (fd == -1) { 145 perror("init_serialport: Unable to open port "); 146 perror(hci_uart_config->device_name); 147 return -1; 148 } 149 150 if (tcgetattr(fd, &toptions) < 0) { 151 perror("init_serialport: Couldn't get term attributes"); 152 return -1; 153 } 154 speed_t brate = hci_uart_config->baudrate_init; // let you override switch below if needed 155 switch(hci_uart_config->baudrate_init) { 156 case 57600: brate=B57600; break; 157 case 115200: brate=B115200; break; 158 #ifdef B230400 159 case 230400: brate=B230400; break; 160 #endif 161 #ifdef B460800 162 case 460800: brate=B460800; break; 163 #endif 164 #ifdef B921600 165 case 921600: brate=B921600; break; 166 #endif 167 } 168 cfsetispeed(&toptions, brate); 169 cfsetospeed(&toptions, brate); 170 171 // 8N1 172 toptions.c_cflag &= ~PARENB; 173 toptions.c_cflag &= ~CSTOPB; 174 toptions.c_cflag &= ~CSIZE; 175 toptions.c_cflag |= CS8; 176 177 if (hci_uart_config->flowcontrol) { 178 // with flow control 179 toptions.c_cflag |= CRTSCTS; 180 } else { 181 // no flow control 182 toptions.c_cflag &= ~CRTSCTS; 183 } 184 185 toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines 186 toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl 187 188 toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw 189 toptions.c_oflag &= ~OPOST; // make raw 190 191 // see: http://unixwiz.net/techtips/termios-vmin-vtime.html 192 toptions.c_cc[VMIN] = 1; 193 toptions.c_cc[VTIME] = 0; 194 195 if( tcsetattr(fd, TCSANOW, &toptions) < 0) { 196 perror("init_serialport: Couldn't set term attributes"); 197 return -1; 198 } 199 200 // set up data_source 201 hci_transport_h4->ds = malloc(sizeof(data_source_t)); 202 if (!hci_transport_h4->ds) return -1; 203 hci_transport_h4->uart_fd = fd; 204 hci_transport_h4->ds->fd = fd; 205 hci_transport_h4->ds->process = h4_process; 206 run_loop_add_data_source(hci_transport_h4->ds); 207 208 // init state machine 209 bytes_to_read = 1; 210 h4_state = H4_W4_PACKET_TYPE; 211 read_pos = 0; 212 213 return 0; 214 } 215 216 static int h4_close(void *transport_config){ 217 // first remove run loop handler 218 run_loop_remove_data_source(hci_transport_h4->ds); 219 220 // close device 221 close(hci_transport_h4->ds->fd); 222 223 #if defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) 224 // let module sleep 225 h4_wake_off(); 226 #endif 227 228 // free struct 229 free(hci_transport_h4->ds); 230 hci_transport_h4->ds = NULL; 231 return 0; 232 } 233 234 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 235 if (hci_transport_h4->ds == NULL) return -1; 236 if (hci_transport_h4->uart_fd == 0) return -1; 237 238 #if defined (USE_BLUETOOL) && defined (USE_POWERMANAGEMENT) 239 // wake Bluetooth module 240 h4_wake_on(); 241 #endif 242 243 hci_dump_packet( (uint8_t) packet_type, 0, packet, size); 244 char *data = (char*) packet; 245 int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 246 while (bytes_written < 1) { 247 usleep(5000); 248 bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 249 }; 250 while (size > 0) { 251 int bytes_written = write(hci_transport_h4->uart_fd, data, size); 252 if (bytes_written < 0) { 253 usleep(5000); 254 continue; 255 } 256 data += bytes_written; 257 size -= bytes_written; 258 } 259 return 0; 260 } 261 262 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 263 packet_handler = handler; 264 } 265 266 static void h4_deliver_packet(void){ 267 if (read_pos < 3) return; // sanity check 268 hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1); 269 packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 270 271 h4_state = H4_W4_PACKET_TYPE; 272 read_pos = 0; 273 bytes_to_read = 1; 274 } 275 276 static void h4_statemachine(void){ 277 switch (h4_state) { 278 279 case H4_W4_PACKET_TYPE: 280 if (hci_packet[0] == HCI_EVENT_PACKET){ 281 bytes_to_read = HCI_EVENT_HEADER_SIZE; 282 h4_state = H4_W4_EVENT_HEADER; 283 } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){ 284 bytes_to_read = HCI_ACL_HEADER_SIZE; 285 h4_state = H4_W4_ACL_HEADER; 286 } else { 287 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet[0]); 288 read_pos = 0; 289 bytes_to_read = 1; 290 } 291 break; 292 293 case H4_W4_EVENT_HEADER: 294 bytes_to_read = hci_packet[2]; 295 h4_state = H4_W4_PAYLOAD; 296 break; 297 298 case H4_W4_ACL_HEADER: 299 bytes_to_read = READ_BT_16( hci_packet, 3); 300 h4_state = H4_W4_PAYLOAD; 301 break; 302 303 case H4_W4_PAYLOAD: 304 h4_deliver_packet(); 305 break; 306 default: 307 break; 308 } 309 } 310 311 static int h4_process(struct data_source *ds) { 312 if (hci_transport_h4->uart_fd == 0) return -1; 313 314 int read_now = bytes_to_read; 315 // if (read_now > 100) { 316 // read_now = 100; 317 // } 318 319 // read up to bytes_to_read data in 320 ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now); 321 // printf("h4_process: bytes read %u\n", bytes_read); 322 if (bytes_read < 0) { 323 return bytes_read; 324 } 325 326 // hexdump(&hci_packet[read_pos], bytes_read); 327 328 bytes_to_read -= bytes_read; 329 read_pos += bytes_read; 330 if (bytes_to_read > 0) { 331 return 0; 332 } 333 334 h4_statemachine(); 335 return 0; 336 } 337 338 static const char * h4_get_transport_name(void){ 339 return "H4"; 340 } 341 342 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 343 } 344 345 // get h4 singleton 346 hci_transport_t * hci_transport_h4_instance() { 347 if (hci_transport_h4 == NULL) { 348 hci_transport_h4 = malloc( sizeof(hci_transport_h4_t)); 349 hci_transport_h4->ds = NULL; 350 hci_transport_h4->transport.open = h4_open; 351 hci_transport_h4->transport.close = h4_close; 352 hci_transport_h4->transport.send_packet = h4_send_packet; 353 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 354 hci_transport_h4->transport.get_transport_name = h4_get_transport_name; 355 hci_transport_h4->transport.set_baudrate = NULL; 356 hci_transport_h4->transport.can_send_packet_now = NULL; 357 } 358 return (hci_transport_t *) hci_transport_h4; 359 } 360