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