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 cfmakeraw(&toptions); // make raw 124 125 // 8N1 126 toptions.c_cflag &= ~CSTOPB; 127 toptions.c_cflag |= CS8; 128 129 if (hci_uart_config->flowcontrol) { 130 // with flow control 131 toptions.c_cflag |= CRTSCTS; 132 } else { 133 // no flow control 134 toptions.c_cflag &= ~CRTSCTS; 135 } 136 137 toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines 138 toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl 139 140 // see: http://unixwiz.net/techtips/termios-vmin-vtime.html 141 toptions.c_cc[VMIN] = 1; 142 toptions.c_cc[VTIME] = 0; 143 144 if( tcsetattr(fd, TCSANOW, &toptions) < 0) { 145 perror("init_serialport: Couldn't set term attributes"); 146 return -1; 147 } 148 149 // set up data_source 150 hci_transport_h4->ds = (data_source_t*) malloc(sizeof(data_source_t)); 151 if (!hci_transport_h4->ds) return -1; 152 hci_transport_h4->uart_fd = fd; 153 hci_transport_h4->ds->fd = fd; 154 hci_transport_h4->ds->process = h4_process; 155 run_loop_add_data_source(hci_transport_h4->ds); 156 157 // init state machine 158 bytes_to_read = 1; 159 h4_state = H4_W4_PACKET_TYPE; 160 read_pos = 0; 161 162 // bring bluetooth module into defined state 163 uint8_t reset[] = { 0x01, 0x03, 0x0c, 0x00}; 164 write(fd, &reset, sizeof(reset)); 165 usleep(100000); // 100 ms 166 write(fd, &reset, sizeof(reset)); 167 usleep(100000); // 100 ms 168 tcflush(fd, TCIOFLUSH); 169 170 return 0; 171 } 172 173 static int h4_close(void *transport_config){ 174 // first remove run loop handler 175 run_loop_remove_data_source(hci_transport_h4->ds); 176 177 // close device 178 close(hci_transport_h4->ds->fd); 179 180 // free struct 181 free(hci_transport_h4->ds); 182 hci_transport_h4->ds = NULL; 183 return 0; 184 } 185 186 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 187 if (hci_transport_h4->ds == NULL) return -1; 188 if (hci_transport_h4->uart_fd == 0) return -1; 189 190 hci_dump_packet( (uint8_t) packet_type, 0, packet, size); 191 char *data = (char*) packet; 192 int bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 193 while (bytes_written < 1) { 194 usleep(5000); 195 bytes_written = write(hci_transport_h4->uart_fd, &packet_type, 1); 196 }; 197 while (size > 0) { 198 int bytes_written = write(hci_transport_h4->uart_fd, data, size); 199 if (bytes_written < 0) { 200 usleep(5000); 201 continue; 202 } 203 data += bytes_written; 204 size -= bytes_written; 205 } 206 return 0; 207 } 208 209 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 210 packet_handler = handler; 211 } 212 213 static void h4_deliver_packet(void){ 214 if (read_pos < 3) return; // sanity check 215 hci_dump_packet( hci_packet[0], 1, &hci_packet[1], read_pos-1); 216 packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 217 218 h4_state = H4_W4_PACKET_TYPE; 219 read_pos = 0; 220 bytes_to_read = 1; 221 } 222 223 static void h4_statemachine(void){ 224 switch (h4_state) { 225 226 case H4_W4_PACKET_TYPE: 227 if (hci_packet[0] == HCI_EVENT_PACKET){ 228 bytes_to_read = HCI_EVENT_HEADER_SIZE; 229 h4_state = H4_W4_EVENT_HEADER; 230 } else if (hci_packet[0] == HCI_ACL_DATA_PACKET){ 231 bytes_to_read = HCI_ACL_HEADER_SIZE; 232 h4_state = H4_W4_ACL_HEADER; 233 } else { 234 log_error("h4_process: invalid packet type 0x%02x", hci_packet[0]); 235 read_pos = 0; 236 bytes_to_read = 1; 237 } 238 break; 239 240 case H4_W4_EVENT_HEADER: 241 bytes_to_read = hci_packet[2]; 242 h4_state = H4_W4_PAYLOAD; 243 break; 244 245 case H4_W4_ACL_HEADER: 246 bytes_to_read = READ_BT_16( hci_packet, 3); 247 h4_state = H4_W4_PAYLOAD; 248 break; 249 250 case H4_W4_PAYLOAD: 251 h4_deliver_packet(); 252 break; 253 default: 254 break; 255 } 256 } 257 258 static int h4_process(struct data_source *ds) { 259 if (hci_transport_h4->uart_fd == 0) return -1; 260 261 int read_now = bytes_to_read; 262 263 // read up to bytes_to_read data in 264 ssize_t bytes_read = read(hci_transport_h4->uart_fd, &hci_packet[read_pos], read_now); 265 // log_info("h4_process: bytes read %u", bytes_read); 266 if (bytes_read < 0) { 267 return bytes_read; 268 } 269 270 // hexdump(&hci_packet[read_pos], bytes_read); 271 272 bytes_to_read -= bytes_read; 273 read_pos += bytes_read; 274 if (bytes_to_read > 0) { 275 return 0; 276 } 277 278 h4_statemachine(); 279 return 0; 280 } 281 282 static const char * h4_get_transport_name(void){ 283 return "H4"; 284 } 285 286 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 287 } 288 289 // get h4 singleton 290 hci_transport_t * hci_transport_h4_instance() { 291 if (hci_transport_h4 == NULL) { 292 hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t)); 293 hci_transport_h4->ds = NULL; 294 hci_transport_h4->transport.open = h4_open; 295 hci_transport_h4->transport.close = h4_close; 296 hci_transport_h4->transport.send_packet = h4_send_packet; 297 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 298 hci_transport_h4->transport.get_transport_name = h4_get_transport_name; 299 hci_transport_h4->transport.set_baudrate = NULL; 300 hci_transport_h4->transport.can_send_packet_now = NULL; 301 } 302 return (hci_transport_t *) hci_transport_h4; 303 } 304