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 #include <termios.h> /* POSIX terminal control definitions */ 40 #include <fcntl.h> /* File control definitions */ 41 #include <unistd.h> /* UNIX standard function definitions */ 42 #include <stdio.h> 43 #include <string.h> 44 #include <pthread.h> 45 46 #include "debug.h" 47 #include "hci.h" 48 #include "hci_transport.h" 49 #include "hci_dump.h" 50 51 // #define USE_HCI_READER_THREAD 52 53 typedef enum { 54 H4_W4_PACKET_TYPE, 55 H4_W4_EVENT_HEADER, 56 H4_W4_ACL_HEADER, 57 H4_W4_PAYLOAD, 58 H4_W4_PICKUP 59 } H4_STATE; 60 61 typedef struct hci_transport_h4 { 62 hci_transport_t transport; 63 data_source_t *ds; 64 int uart_fd; // different from ds->fd for HCI reader thread 65 66 #ifdef USE_HCI_READER_THREAD 67 // synchronization facilities for dedicated reader thread 68 int pipe_fds[2]; 69 pthread_mutex_t mutex; 70 pthread_cond_t cond; 71 #endif 72 } hci_transport_h4_t; 73 74 // single instance 75 static hci_transport_h4_t * hci_transport_h4 = NULL; 76 77 static int h4_process(struct data_source *ds); 78 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 79 static hci_uart_config_t *hci_uart_config; 80 81 #ifdef USE_HCI_READER_THREAD 82 static void *h4_reader(void *context); 83 static int h4_reader_process(struct data_source *ds); 84 #endif 85 86 87 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 88 89 // packet reader state machine 90 static H4_STATE h4_state; 91 static int bytes_to_read; 92 static int read_pos; 93 94 static uint8_t hci_packet[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 95 96 // prototypes 97 static int h4_open(void *transport_config){ 98 hci_uart_config = (hci_uart_config_t*) transport_config; 99 struct termios toptions; 100 int flags = O_RDWR | O_NOCTTY; 101 #ifndef USE_HCI_READER_THREAD 102 flags |= O_NONBLOCK; 103 #endif 104 int fd = open(hci_uart_config->device_name, flags); 105 if (fd == -1) { 106 perror("init_serialport: Unable to open port "); 107 perror(hci_uart_config->device_name); 108 return -1; 109 } 110 111 if (tcgetattr(fd, &toptions) < 0) { 112 perror("init_serialport: Couldn't get term attributes"); 113 return -1; 114 } 115 speed_t brate = hci_uart_config->baudrate_init; // let you override switch below if needed 116 switch(hci_uart_config->baudrate_init) { 117 case 57600: brate=B57600; break; 118 case 115200: brate=B115200; break; 119 #ifdef B230400 120 case 230400: brate=B230400; break; 121 #endif 122 #ifdef B460800 123 case 460800: brate=B460800; break; 124 #endif 125 #ifdef B921600 126 case 921600: brate=B921600; break; 127 #endif 128 } 129 cfsetispeed(&toptions, brate); 130 cfsetospeed(&toptions, brate); 131 132 // 8N1 133 toptions.c_cflag &= ~PARENB; 134 toptions.c_cflag &= ~CSTOPB; 135 toptions.c_cflag &= ~CSIZE; 136 toptions.c_cflag |= CS8; 137 138 if (hci_uart_config->flowcontrol) { 139 // with flow control 140 toptions.c_cflag |= CRTSCTS; 141 } else { 142 // no flow control 143 toptions.c_cflag &= ~CRTSCTS; 144 } 145 146 toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines 147 toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl 148 149 toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw 150 toptions.c_oflag &= ~OPOST; // make raw 151 152 // see: http://unixwiz.net/techtips/termios-vmin-vtime.html 153 toptions.c_cc[VMIN] = 1; 154 toptions.c_cc[VTIME] = 0; 155 156 if( tcsetattr(fd, TCSANOW, &toptions) < 0) { 157 perror("init_serialport: Couldn't set term attributes"); 158 return -1; 159 } 160 161 // set up data_source 162 hci_transport_h4->ds = malloc(sizeof(data_source_t)); 163 if (!hci_transport_h4->ds) return -1; 164 hci_transport_h4->uart_fd = fd; 165 166 #ifdef USE_HCI_READER_THREAD 167 // init synchronization tools 168 pthread_mutex_init(&hci_transport_h4->mutex, NULL); 169 pthread_cond_init(&hci_transport_h4->cond, NULL); 170 171 // create pipe 172 pipe(hci_transport_h4->pipe_fds); 173 174 // create reader thread 175 pthread_t hci_reader_thread; 176 pthread_create(&hci_reader_thread, NULL, &h4_reader, NULL); 177 178 hci_transport_h4->ds->fd = hci_transport_h4->pipe_fds[0]; 179 hci_transport_h4->ds->process = h4_reader_process; 180 #else 181 hci_transport_h4->ds->fd = fd; 182 hci_transport_h4->ds->process = h4_process; 183 #endif 184 run_loop_add_data_source(hci_transport_h4->ds); 185 186 // init state machine 187 bytes_to_read = 1; 188 h4_state = H4_W4_PACKET_TYPE; 189 read_pos = 0; 190 191 return 0; 192 } 193 194 static int h4_close(void *transport_config){ 195 // first remove run loop handler 196 run_loop_remove_data_source(hci_transport_h4->ds); 197 198 // close device 199 close(hci_transport_h4->ds->fd); 200 201 // free struct 202 free(hci_transport_h4->ds); 203 hci_transport_h4->ds = NULL; 204 return 0; 205 } 206 207 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 208 if (hci_transport_h4->ds == NULL) return -1; 209 if (hci_transport_h4->uart_fd == 0) return -1; 210 hci_dump_packet( (uint8_t) packet_type, 0, packet, size); 211 char *data = (char*) packet; 212 int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 213 while (bytes_written < 1) { 214 usleep(5000); 215 bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 216 }; 217 while (size > 0) { 218 int bytes_written = write(hci_transport_h4->uart_fd, data, size); 219 if (bytes_written < 0) { 220 usleep(5000); 221 continue; 222 } 223 data += bytes_written; 224 size -= bytes_written; 225 } 226 return 0; 227 } 228 229 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 230 packet_handler = handler; 231 } 232 233 static void h4_deliver_packet(void){ 234 if (read_pos < 3) return; // sanity check 235 hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1); 236 packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 237 238 h4_state = H4_W4_PACKET_TYPE; 239 read_pos = 0; 240 bytes_to_read = 1; 241 } 242 243 static void h4_statemachine(void){ 244 switch (h4_state) { 245 246 case H4_W4_PACKET_TYPE: 247 if (hci_packet[0] == HCI_EVENT_PACKET){ 248 bytes_to_read = HCI_EVENT_HEADER_SIZE; 249 h4_state = H4_W4_EVENT_HEADER; 250 } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){ 251 bytes_to_read = HCI_ACL_HEADER_SIZE; 252 h4_state = H4_W4_ACL_HEADER; 253 } else { 254 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet[0]); 255 read_pos = 0; 256 bytes_to_read = 1; 257 } 258 break; 259 260 case H4_W4_EVENT_HEADER: 261 bytes_to_read = hci_packet[2]; 262 h4_state = H4_W4_PAYLOAD; 263 break; 264 265 case H4_W4_ACL_HEADER: 266 bytes_to_read = READ_BT_16( hci_packet, 3); 267 h4_state = H4_W4_PAYLOAD; 268 break; 269 270 case H4_W4_PAYLOAD: 271 #ifdef USE_HCI_READER_THREAD 272 h4_state = H4_W4_PICKUP; 273 #else 274 h4_deliver_packet(); 275 #endif 276 break; 277 default: 278 break; 279 } 280 } 281 282 static int h4_process(struct data_source *ds) { 283 if (hci_transport_h4->uart_fd == 0) return -1; 284 285 int read_now = bytes_to_read; 286 // if (read_now > 100) { 287 // read_now = 100; 288 // } 289 290 // read up to bytes_to_read data in 291 ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now); 292 // printf("h4_process: bytes read %u\n", bytes_read); 293 if (bytes_read < 0) { 294 return bytes_read; 295 } 296 297 // hexdump(&hci_packet[read_pos], bytes_read); 298 299 bytes_to_read -= bytes_read; 300 read_pos += bytes_read; 301 if (bytes_to_read > 0) { 302 return 0; 303 } 304 305 h4_statemachine(); 306 return 0; 307 } 308 309 #ifdef USE_HCI_READER_THREAD 310 static int h4_reader_process(struct data_source *ds) { 311 // get token 312 char token; 313 int tokens_read = read(hci_transport_h4->pipe_fds[0], &token, 1); 314 if (tokens_read < 1) { 315 return 0; 316 } 317 318 // hci_reader received complete packet, just pick it up 319 h4_deliver_packet(); 320 321 // un-block reader 322 pthread_mutex_lock(&hci_transport_h4->mutex); 323 pthread_cond_signal(&hci_transport_h4->cond); 324 pthread_mutex_unlock(&hci_transport_h4->mutex); 325 return 0; 326 } 327 328 static void *h4_reader(void *context){ 329 while(1){ 330 // read up to bytes_to_read data in 331 int bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], bytes_to_read); 332 // error 333 if (bytes_read < 0) { 334 h4_state = H4_W4_PACKET_TYPE; 335 read_pos = 0; 336 bytes_to_read = 1; 337 continue; 338 } 339 340 bytes_to_read -= bytes_read; 341 read_pos += bytes_read; 342 343 if (bytes_to_read > 0) continue; 344 345 h4_statemachine(); 346 347 if (h4_state != H4_W4_PICKUP) continue; 348 349 // notify main thread 350 char data = 'h'; 351 write(hci_transport_h4->pipe_fds[1], &data, 1); 352 353 // wait for response 354 pthread_mutex_lock(&hci_transport_h4->mutex); 355 pthread_cond_wait(&hci_transport_h4->cond,&hci_transport_h4->mutex); 356 pthread_mutex_unlock(&hci_transport_h4->mutex); 357 } 358 } 359 #endif 360 361 static const char * h4_get_transport_name(void){ 362 return "H4"; 363 } 364 365 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 366 } 367 368 // get h4 singleton 369 hci_transport_t * hci_transport_h4_instance() { 370 if (hci_transport_h4 == NULL) { 371 hci_transport_h4 = malloc( sizeof(hci_transport_h4_t)); 372 hci_transport_h4->ds = NULL; 373 hci_transport_h4->transport.open = h4_open; 374 hci_transport_h4->transport.close = h4_close; 375 hci_transport_h4->transport.send_packet = h4_send_packet; 376 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 377 hci_transport_h4->transport.get_transport_name = h4_get_transport_name; 378 hci_transport_h4->transport.set_baudrate = NULL; 379 hci_transport_h4->transport.can_send_packet_now = NULL; 380 } 381 return (hci_transport_t *) hci_transport_h4; 382 } 383