]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/hci/hci_hal_h4.c
Merge branch 'bugfix/coap_client_parse_url' into 'master'
[esp-idf] / components / bt / bluedroid / hci / hci_hal_h4.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include <string.h>
19 #include "common/bt_defs.h"
20 #include "common/bt_trace.h"
21 #include "stack/bt_types.h"
22 #include "hci/buffer_allocator.h"
23 #include "osi/fixed_queue.h"
24 #include "hci/hci_hal.h"
25 #include "hci/hci_internals.h"
26 #include "hci/hci_layer.h"
27 #include "osi/thread.h"
28 #include "esp_bt.h"
29
30 #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
31 #include "l2c_int.h"
32 #include "stack/hcimsgs.h"
33 #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
34
35 #define HCI_HAL_SERIAL_BUFFER_SIZE 1026
36 #define HCI_BLE_EVENT 0x3e
37 #define PACKET_TYPE_TO_INBOUND_INDEX(type) ((type) - 2)
38 #define PACKET_TYPE_TO_INDEX(type) ((type) - 1)
39
40
41 static const uint8_t preamble_sizes[] = {
42     HCI_COMMAND_PREAMBLE_SIZE,
43     HCI_ACL_PREAMBLE_SIZE,
44     HCI_SCO_PREAMBLE_SIZE,
45     HCI_EVENT_PREAMBLE_SIZE
46 };
47
48 static const uint16_t outbound_event_types[] = {
49     MSG_HC_TO_STACK_HCI_ERR,
50     MSG_HC_TO_STACK_HCI_ACL,
51     MSG_HC_TO_STACK_HCI_SCO,
52     MSG_HC_TO_STACK_HCI_EVT
53 };
54
55 typedef struct {
56     const allocator_t *allocator;
57     size_t buffer_size;
58     fixed_queue_t *rx_q;
59 } hci_hal_env_t;
60
61
62 static hci_hal_env_t hci_hal_env;
63 static const hci_hal_t interface;
64 static const hci_hal_callbacks_t *callbacks;
65 static const esp_vhci_host_callback_t vhci_host_cb;
66
67 static xTaskHandle xHciH4TaskHandle;
68 static xQueueHandle xHciH4Queue;
69
70 static void host_send_pkt_available_cb(void);
71 static int host_recv_pkt_cb(uint8_t *data, uint16_t len);
72
73 static void hci_hal_h4_rx_handler(void *arg);
74 static void event_uart_has_bytes(fixed_queue_t *queue);
75
76
77 static void hci_hal_env_init(
78     size_t buffer_size,
79     size_t max_buffer_count)
80 {
81     assert(buffer_size > 0);
82     assert(max_buffer_count > 0);
83
84     hci_hal_env.allocator = buffer_allocator_get_interface();
85     hci_hal_env.buffer_size = buffer_size;
86
87     hci_hal_env.rx_q = fixed_queue_new(max_buffer_count);
88     if (hci_hal_env.rx_q) {
89         fixed_queue_register_dequeue(hci_hal_env.rx_q, event_uart_has_bytes);
90     } else {
91         HCI_TRACE_ERROR("%s unable to create rx queue.\n", __func__);
92     }
93
94     return;
95 }
96
97 static void hci_hal_env_deinit(void)
98 {
99     fixed_queue_free(hci_hal_env.rx_q, hci_hal_env.allocator->free);
100     hci_hal_env.rx_q = NULL;
101 }
102
103 static bool hal_open(const hci_hal_callbacks_t *upper_callbacks)
104 {
105     assert(upper_callbacks != NULL);
106     callbacks = upper_callbacks;
107
108     hci_hal_env_init(HCI_HAL_SERIAL_BUFFER_SIZE, SIZE_MAX);
109
110     xHciH4Queue = xQueueCreate(HCI_H4_QUEUE_LEN, sizeof(BtTaskEvt_t));
111     xTaskCreatePinnedToCore(hci_hal_h4_rx_handler, HCI_H4_TASK_NAME, HCI_H4_TASK_STACK_SIZE, NULL, HCI_H4_TASK_PRIO, &xHciH4TaskHandle, HCI_H4_TASK_PINNED_TO_CORE);
112
113     //register vhci host cb
114     esp_vhci_host_register_callback(&vhci_host_cb);
115
116
117     return true;
118 }
119
120 static void hal_close()
121 {
122     hci_hal_env_deinit();
123
124     /* delete task and queue */
125     vTaskDelete(xHciH4TaskHandle);
126     vQueueDelete(xHciH4Queue);
127 }
128
129 /**
130  * Function: transmit_data -TX data to low-layer
131  * It is ported from Bluedroid source code, so it is not
132  * needed to use write() to send data.
133  * TODO: Just use firmware API to send data.
134  */
135 static uint16_t transmit_data(serial_data_type_t type,
136                               uint8_t *data, uint16_t length)
137 {
138     uint8_t previous_byte;
139
140     assert(data != NULL);
141     assert(length > 0);
142
143     if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
144         HCI_TRACE_ERROR("%s invalid data type: %d", __func__, type);
145         return 0;
146     }
147
148     // Write the signal byte right before the data
149     --data;
150     previous_byte = *data;
151     *(data) = type;
152     ++length;
153
154     BTTRC_DUMP_BUFFER("Transmit Pkt", data, length);
155
156     // TX Data to target
157     esp_vhci_host_send_packet(data, length);
158
159     // Be nice and restore the old value of that byte
160     *(data) = previous_byte;
161
162     return length - 1;
163 }
164
165 // Internal functions
166 static void hci_hal_h4_rx_handler(void *arg)
167 {
168     BtTaskEvt_t e;
169
170     for (;;) {
171         if (pdTRUE == xQueueReceive(xHciH4Queue, &e, (portTickType)portMAX_DELAY)) {
172             if (e.sig == SIG_HCI_HAL_RECV_PACKET) {
173                 fixed_queue_process(hci_hal_env.rx_q);
174
175             }
176         }
177     }
178 }
179
180 task_post_status_t hci_hal_h4_task_post(task_post_t timeout)
181 {
182     BtTaskEvt_t evt;
183
184     evt.sig = SIG_HCI_HAL_RECV_PACKET;
185     evt.par = 0;
186
187     if (xQueueSend(xHciH4Queue, &evt, timeout) != pdTRUE) {
188         HCI_TRACE_ERROR("xHciH4Queue failed\n");
189         return TASK_POST_SUCCESS;
190     }
191
192     return TASK_POST_FAIL;
193 }
194
195 #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
196 static void hci_packet_complete(BT_HDR *packet){
197     uint8_t type, num_handle;
198     uint16_t handle;
199     uint16_t handles[MAX_L2CAP_LINKS + 4];
200     uint16_t num_packets[MAX_L2CAP_LINKS + 4];
201     uint8_t *stream = packet->data + packet->offset;
202     tL2C_LCB  *p_lcb = NULL;
203
204     STREAM_TO_UINT8(type, stream);
205     if (type == DATA_TYPE_ACL/* || type == DATA_TYPE_SCO*/) {
206         STREAM_TO_UINT16(handle, stream);
207         handle = handle & HCI_DATA_HANDLE_MASK;
208         p_lcb = l2cu_find_lcb_by_handle(handle);
209         if (p_lcb) {
210             p_lcb->completed_packets++;
211         }
212         if (esp_vhci_host_check_send_available()){
213             num_handle = l2cu_find_completed_packets(handles, num_packets);
214             if (num_handle > 0){
215                 btsnd_hcic_host_num_xmitted_pkts (num_handle, handles, num_packets);
216             }
217         } else {
218             //Send HCI_Host_Number_of_Completed_Packets next time.
219         }
220
221     }
222 }
223 #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
224
225
226 static void hci_hal_h4_hdl_rx_packet(BT_HDR *packet)
227 {
228     uint8_t type, hdr_size;
229     uint16_t length;
230     uint8_t *stream = packet->data + packet->offset;
231
232     if (!packet) {
233         return;
234     }
235
236 #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
237     hci_packet_complete(packet);
238 #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
239
240     STREAM_TO_UINT8(type, stream);
241     packet->offset++;
242     packet->len--;
243     if (type == HCI_BLE_EVENT) {
244         uint8_t len = 0;
245         STREAM_TO_UINT8(len, stream);
246         HCI_TRACE_ERROR("Workround stream corrupted during LE SCAN: pkt_len=%d ble_event_len=%d\n",
247                   packet->len, len);
248         hci_hal_env.allocator->free(packet);
249         return;
250     }
251     if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
252         HCI_TRACE_ERROR("%s Unknown HCI message type. Dropping this byte 0x%x,"
253                   " min %x, max %x\n", __func__, type,
254                   DATA_TYPE_ACL, DATA_TYPE_EVENT);
255         hci_hal_env.allocator->free(packet);
256         return;
257     }
258     hdr_size = preamble_sizes[type - 1];
259     if (packet->len < hdr_size) {
260         HCI_TRACE_ERROR("Wrong packet length type=%d pkt_len=%d hdr_len=%d",
261                   type, packet->len, hdr_size);
262         hci_hal_env.allocator->free(packet);
263         return;
264     }
265     if (type == DATA_TYPE_ACL) {
266         stream += hdr_size - 2;
267         STREAM_TO_UINT16(length, stream);
268     } else {
269         stream += hdr_size - 1;
270         STREAM_TO_UINT8(length, stream);
271     }
272
273     if ((length + hdr_size) != packet->len) {
274         HCI_TRACE_ERROR("Wrong packet length type=%d hdr_len=%d pd_len=%d "
275                   "pkt_len=%d", type, hdr_size, length, packet->len);
276         hci_hal_env.allocator->free(packet);
277         return;
278     }
279
280     packet->event = outbound_event_types[PACKET_TYPE_TO_INDEX(type)];
281     callbacks->packet_ready(packet);
282 }
283
284 static void event_uart_has_bytes(fixed_queue_t *queue)
285 {
286     BT_HDR *packet;
287     while (!fixed_queue_is_empty(queue)) {
288         packet = fixed_queue_dequeue(queue);
289         hci_hal_h4_hdl_rx_packet(packet);
290     }
291 }
292
293 static void host_send_pkt_available_cb(void)
294 {
295     //Controller rx cache buffer is ready for receiving new host packet
296     //Just Call Host main thread task to process pending packets.
297     hci_host_task_post(TASK_POST_BLOCKING);
298 }
299
300 static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
301 {
302     //Target has packet to host, malloc new buffer for packet
303     BT_HDR *pkt;
304     size_t pkt_size;
305
306     if (hci_hal_env.rx_q == NULL) {
307         return 0;
308     }
309
310     pkt_size = BT_HDR_SIZE + len;
311     pkt = (BT_HDR *)hci_hal_env.allocator->alloc(pkt_size);
312     if (!pkt) {
313         HCI_TRACE_ERROR("%s couldn't aquire memory for inbound data buffer.\n", __func__);
314         return -1;
315     }
316     pkt->offset = 0;
317     pkt->len = len;
318     pkt->layer_specific = 0;
319     memcpy(pkt->data, data, len);
320     fixed_queue_enqueue(hci_hal_env.rx_q, pkt);
321     hci_hal_h4_task_post(TASK_POST_BLOCKING);
322
323     BTTRC_DUMP_BUFFER("Recv Pkt", pkt->data, len);
324
325     return 0;
326 }
327
328 static const esp_vhci_host_callback_t vhci_host_cb = {
329     .notify_host_send_available = host_send_pkt_available_cb,
330     .notify_host_recv = host_recv_pkt_cb,
331 };
332
333 static const hci_hal_t interface = {
334     hal_open,
335     hal_close,
336     transmit_data,
337 };
338
339 const hci_hal_t *hci_hal_h4_get_interface()
340 {
341     return &interface;
342 }
343