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 <stdio.h> 48 49 #include "bluetoothdrv.h" 50 51 #include <string.h> 52 53 #include "btstack_debug.h" 54 #include "hci.h" 55 #include "hci_transport.h" 56 57 static void h4_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type); 58 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 59 60 typedef struct hci_transport_h4 { 61 hci_transport_t transport; 62 btstack_data_source_t *ds; 63 /* power management support, e.g. used by iOS */ 64 btstack_timer_source_t sleep_timer; 65 } hci_transport_h4_t; 66 67 // single instance 68 static hci_transport_h4_t * hci_transport_h4 = NULL; 69 70 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 71 72 // packet bufffers 73 static uint8_t hci_packet_out[1+HCI_OUTGOING_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, cmd header + cmd data) 74 static uint8_t hci_packet_in[ 1+HCI_INCOMING_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 75 76 static int h4_open(void){ 77 int fd = mtk_bt_enable(); 78 79 if (fd < 0) { 80 log_error("mtk_bt_enable failed"); 81 return -1; 82 } 83 84 // set up data_source 85 hci_transport_h4->ds = (btstack_data_source_t*) malloc(sizeof(btstack_data_source_t)); 86 if (!hci_transport_h4->ds) return -1; 87 memset(hci_transport_h4->ds, 0, sizeof(btstack_data_source_t)); 88 btstack_run_loop_set_data_source_fd(hci_transport_h4->ds, fd); 89 btstack_run_loop_set_data_source_handler(hci_transport_h4->ds, &h4_process); 90 btstack_run_loop_enable_data_source_callbacks(hci_transport_h4->ds, DATA_SOURCE_CALLBACK_READ); 91 btstack_run_loop_add_data_source(hci_transport_h4->ds); 92 return 0; 93 } 94 95 static int h4_close(void){ 96 97 mtk_bt_disable(hci_transport_h4->ds->source.fd); 98 99 // first remove run loop handler 100 btstack_run_loop_remove_data_source(hci_transport_h4->ds); 101 102 // free struct 103 free(hci_transport_h4->ds); 104 hci_transport_h4->ds = NULL; 105 return 0; 106 } 107 108 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 109 110 if (hci_transport_h4->ds == NULL) return -1; 111 112 // preapare packet 113 hci_packet_out[0] = packet_type; 114 memcpy(&hci_packet_out[1], packet, size); 115 116 // send 117 int res = mtk_bt_write(hci_transport_h4->ds->source.fd, hci_packet_out, size + 1); 118 119 return 0; 120 } 121 122 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 123 packet_handler = handler; 124 } 125 126 static void h4_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) { 127 if (hci_transport_h4->ds->source.fd == 0) return; 128 129 // read up to bytes_to_read data in 130 ssize_t bytes_read = mtk_bt_read(hci_transport_h4->ds->source.fd, &hci_packet_in[0], sizeof(hci_packet_in)); 131 132 if (bytes_read == 0) return; 133 134 // iterate over packets 135 uint16_t pos = 0; 136 while (pos < bytes_read) { 137 uint16_t packet_len; 138 switch(hci_packet_in[pos]){ 139 case HCI_EVENT_PACKET: 140 packet_len = hci_packet_in[pos+2] + 3; 141 break; 142 case HCI_ACL_DATA_PACKET: 143 packet_len = little_endian_read_16(hci_packet_in, pos + 3) + 5; 144 break; 145 default: 146 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet_in[pos]); 147 return; 148 } 149 150 packet_handler(hci_packet_in[pos], &hci_packet_in[pos+1], packet_len-1); 151 pos += packet_len; 152 } 153 } 154 155 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 156 } 157 158 // get h4 singleton 159 const hci_transport_t * hci_transport_h4_instance_for_uart(const btstack_uart_t * uart_driver){ 160 (void) uart_driver; 161 if (hci_transport_h4 == NULL) { 162 hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t)); 163 memset(hci_transport_h4, 0, sizeof(hci_transport_h4_t)); 164 hci_transport_h4->transport.name = "H4 MTK"; 165 hci_transport_h4->transport.open = h4_open; 166 hci_transport_h4->transport.close = h4_close; 167 hci_transport_h4->transport.send_packet = h4_send_packet; 168 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 169 } 170 return (hci_transport_t *) hci_transport_h4; 171 } 172