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 return 0; 167 } 168 169 static int h4_close(void *transport_config){ 170 // first remove run loop handler 171 run_loop_remove_data_source(hci_transport_h4->ds); 172 173 // close device 174 close(hci_transport_h4->ds->fd); 175 176 // free struct 177 free(hci_transport_h4->ds); 178 hci_transport_h4->ds = NULL; 179 return 0; 180 } 181 182 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 183 if (hci_transport_h4->ds == NULL) return -1; 184 if (hci_transport_h4->uart_fd == 0) return -1; 185 186 hci_dump_packet( (uint8_t) packet_type, 0, packet, size); 187 char *data = (char*) packet; 188 int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 189 while (bytes_written < 1) { 190 usleep(5000); 191 bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 192 }; 193 while (size > 0) { 194 int bytes_written = write(hci_transport_h4->uart_fd, data, size); 195 if (bytes_written < 0) { 196 usleep(5000); 197 continue; 198 } 199 data += bytes_written; 200 size -= bytes_written; 201 } 202 return 0; 203 } 204 205 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 206 packet_handler = handler; 207 } 208 209 static void h4_deliver_packet(void){ 210 if (read_pos < 3) return; // sanity check 211 hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1); 212 packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 213 214 h4_state = H4_W4_PACKET_TYPE; 215 read_pos = 0; 216 bytes_to_read = 1; 217 } 218 219 static void h4_statemachine(void){ 220 switch (h4_state) { 221 222 case H4_W4_PACKET_TYPE: 223 if (hci_packet[0] == HCI_EVENT_PACKET){ 224 bytes_to_read = HCI_EVENT_HEADER_SIZE; 225 h4_state = H4_W4_EVENT_HEADER; 226 } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){ 227 bytes_to_read = HCI_ACL_HEADER_SIZE; 228 h4_state = H4_W4_ACL_HEADER; 229 } else { 230 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet[0]); 231 read_pos = 0; 232 bytes_to_read = 1; 233 } 234 break; 235 236 case H4_W4_EVENT_HEADER: 237 bytes_to_read = hci_packet[2]; 238 h4_state = H4_W4_PAYLOAD; 239 break; 240 241 case H4_W4_ACL_HEADER: 242 bytes_to_read = READ_BT_16( hci_packet, 3); 243 h4_state = H4_W4_PAYLOAD; 244 break; 245 246 case H4_W4_PAYLOAD: 247 h4_deliver_packet(); 248 break; 249 default: 250 break; 251 } 252 } 253 254 static int h4_process(struct data_source *ds) { 255 if (hci_transport_h4->uart_fd == 0) return -1; 256 257 int read_now = bytes_to_read; 258 259 // read up to bytes_to_read data in 260 ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now); 261 // printf("h4_process: bytes read %u\n", bytes_read); 262 if (bytes_read < 0) { 263 return bytes_read; 264 } 265 266 // hexdump(&hci_packet[read_pos], bytes_read); 267 268 bytes_to_read -= bytes_read; 269 read_pos += bytes_read; 270 if (bytes_to_read > 0) { 271 return 0; 272 } 273 274 h4_statemachine(); 275 return 0; 276 } 277 278 static const char * h4_get_transport_name(void){ 279 return "H4"; 280 } 281 282 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 283 } 284 285 // get h4 singleton 286 hci_transport_t * hci_transport_h4_instance() { 287 if (hci_transport_h4 == NULL) { 288 hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t)); 289 hci_transport_h4->ds = NULL; 290 hci_transport_h4->transport.open = h4_open; 291 hci_transport_h4->transport.close = h4_close; 292 hci_transport_h4->transport.send_packet = h4_send_packet; 293 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 294 hci_transport_h4->transport.get_transport_name = h4_get_transport_name; 295 hci_transport_h4->transport.set_baudrate = NULL; 296 hci_transport_h4->transport.can_send_packet_now = NULL; 297 } 298 return (hci_transport_t *) hci_transport_h4; 299 } 300