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