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 int h4_process(struct btstack_data_source *ds); 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_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 74 static uint8_t hci_packet_in[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 75 76 static int h4_open(const void *transport_config){ 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 hci_transport_h4->ds->fd = fd; 88 hci_transport_h4->ds->process = h4_process; 89 btstack_run_loop_add_data_source(hci_transport_h4->ds); 90 return 0; 91 } 92 93 static int h4_close(const void *transport_config){ 94 95 mtk_bt_disable(hci_transport_h4->ds->fd); 96 97 // first remove run loop handler 98 btstack_run_loop_remove_data_source(hci_transport_h4->ds); 99 100 // free struct 101 free(hci_transport_h4->ds); 102 hci_transport_h4->ds = NULL; 103 return 0; 104 } 105 106 static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 107 108 if (hci_transport_h4->ds == NULL) return -1; 109 110 // preapare packet 111 hci_packet_out[0] = packet_type; 112 memcpy(&hci_packet_out[1], packet, size); 113 114 // send 115 int res = mtk_bt_write(hci_transport_h4->ds->fd, hci_packet_out, size + 1); 116 117 return 0; 118 } 119 120 static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 121 packet_handler = handler; 122 } 123 124 static int h4_process(struct btstack_data_source *ds) { 125 if (hci_transport_h4->ds->fd == 0) return -1; 126 127 // read up to bytes_to_read data in 128 ssize_t bytes_read = mtk_bt_read(hci_transport_h4->ds->fd, &hci_packet_in[0], sizeof(hci_packet_in)); 129 130 if (bytes_read == 0) return 0; 131 132 // iterate over packets 133 uint16_t pos = 0; 134 while (pos < bytes_read) { 135 uint16_t packet_len; 136 switch(hci_packet_in[pos]){ 137 case HCI_EVENT_PACKET: 138 packet_len = hci_packet_in[pos+2] + 3; 139 break; 140 case HCI_ACL_DATA_PACKET: 141 packet_len = little_endian_read_16(hci_packet_in, pos + 3) + 5; 142 break; 143 default: 144 log_error("h4_process: invalid packet type 0x%02x\n", hci_packet_in[pos]); 145 return; 146 } 147 148 packet_handler(hci_packet_in[pos], &hci_packet_in[pos+1], packet_len-1); 149 pos += packet_len; 150 } 151 152 return 0; 153 } 154 155 static const char * h4_get_transport_name(void){ 156 return "H4 MTK"; 157 } 158 159 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 160 } 161 162 // get h4 singleton 163 hci_transport_t * hci_transport_h4_instance(void){ 164 if (hci_transport_h4 == NULL) { 165 hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t)); 166 hci_transport_h4->ds = NULL; 167 hci_transport_h4->transport.open = h4_open; 168 hci_transport_h4->transport.close = h4_close; 169 hci_transport_h4->transport.send_packet = h4_send_packet; 170 hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler; 171 hci_transport_h4->transport.get_transport_name = h4_get_transport_name; 172 hci_transport_h4->transport.set_baudrate = NULL; 173 hci_transport_h4->transport.can_send_packet_now = NULL; 174 } 175 return (hci_transport_t *) hci_transport_h4; 176 } 177