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 "osi/fixed_queue.h"
23 #include "hci/hci_hal.h"
24 #include "hci/hci_internals.h"
25 #include "hci/hci_layer.h"
26 #include "osi/thread.h"
27 #include "esp_bt.h"
28 #include "stack/hcimsgs.h"
29 
30 #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
31 #include "l2c_int.h"
32 #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
33 #include "stack/hcimsgs.h"
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 extern bool BTU_check_queue_is_congest(void);
40 
41 
42 static const uint8_t preamble_sizes[] = {
43     HCI_COMMAND_PREAMBLE_SIZE,
44     HCI_ACL_PREAMBLE_SIZE,
45     HCI_SCO_PREAMBLE_SIZE,
46     HCI_EVENT_PREAMBLE_SIZE
47 };
48 
49 static const uint16_t outbound_event_types[] = {
50     MSG_HC_TO_STACK_HCI_ERR,
51     MSG_HC_TO_STACK_HCI_ACL,
52     MSG_HC_TO_STACK_HCI_SCO,
53     MSG_HC_TO_STACK_HCI_EVT
54 };
55 
56 typedef struct {
57     size_t buffer_size;
58     fixed_queue_t *rx_q;
59     uint16_t adv_free_num;
60 } hci_hal_env_t;
61 
62 
63 static hci_hal_env_t hci_hal_env;
64 static const hci_hal_t interface;
65 static const hci_hal_callbacks_t *callbacks;
66 static const esp_vhci_host_callback_t vhci_host_cb;
67 static osi_thread_t *hci_h4_thread;
68 
69 static void host_send_pkt_available_cb(void);
70 static int host_recv_pkt_cb(uint8_t *data, uint16_t len);
71 
72 static void hci_hal_h4_rx_handler(void *arg);
73 static void event_uart_has_bytes(fixed_queue_t *queue);
74 
75 
hci_hal_env_init(size_t buffer_size,size_t max_buffer_count)76 static void hci_hal_env_init(
77     size_t buffer_size,
78     size_t max_buffer_count)
79 {
80     assert(buffer_size > 0);
81     assert(max_buffer_count > 0);
82 
83     hci_hal_env.buffer_size = buffer_size;
84     hci_hal_env.adv_free_num = 0;
85 
86     hci_hal_env.rx_q = fixed_queue_new(max_buffer_count);
87     if (hci_hal_env.rx_q) {
88         fixed_queue_register_dequeue(hci_hal_env.rx_q, event_uart_has_bytes);
89     } else {
90         HCI_TRACE_ERROR("%s unable to create rx queue.\n", __func__);
91     }
92 
93     return;
94 }
95 
hci_hal_env_deinit(void)96 static void hci_hal_env_deinit(void)
97 {
98     fixed_queue_free(hci_hal_env.rx_q, osi_free_func);
99     hci_hal_env.rx_q = NULL;
100 }
101 
hal_open(const hci_hal_callbacks_t * upper_callbacks,void * task_thread)102 static bool hal_open(const hci_hal_callbacks_t *upper_callbacks, void *task_thread)
103 {
104     assert(upper_callbacks != NULL);
105     assert(task_thread != NULL);
106 
107     callbacks = upper_callbacks;
108 #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
109     hci_hal_env_init(HCI_HAL_SERIAL_BUFFER_SIZE, BLE_ADV_REPORT_FLOW_CONTROL_NUM + L2CAP_HOST_FC_ACL_BUFS + QUEUE_SIZE_MAX); // adv flow control num + ACL flow control num + hci cmd numeber
110 #else
111     hci_hal_env_init(HCI_HAL_SERIAL_BUFFER_SIZE, QUEUE_SIZE_MAX);
112 #endif
113 
114     hci_h4_thread = (osi_thread_t *)task_thread;
115 
116     //register vhci host cb
117     if (esp_vhci_host_register_callback(&vhci_host_cb) != ESP_OK) {
118         return false;
119     }
120 
121     return true;
122 }
123 
hal_close(void)124 static void hal_close(void)
125 {
126     hci_hal_env_deinit();
127 
128     hci_h4_thread = NULL;
129 }
130 
131 /**
132  * Function: transmit_data -TX data to low-layer
133  * It is ported from Bluedroid source code, so it is not
134  * needed to use write() to send data.
135  * TODO: Just use firmware API to send data.
136  */
transmit_data(serial_data_type_t type,uint8_t * data,uint16_t length)137 static uint16_t transmit_data(serial_data_type_t type,
138                               uint8_t *data, uint16_t length)
139 {
140     uint8_t previous_byte;
141 
142     assert(data != NULL);
143     assert(length > 0);
144 
145     if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
146         HCI_TRACE_ERROR("%s invalid data type: %d", __func__, type);
147         return 0;
148     }
149 
150     // Write the signal byte right before the data
151     --data;
152     previous_byte = *data;
153     *(data) = type;
154     ++length;
155 
156     BTTRC_DUMP_BUFFER("Transmit Pkt", data, length);
157 
158     // TX Data to target
159     esp_vhci_host_send_packet(data, length);
160 
161     // Be nice and restore the old value of that byte
162     *(data) = previous_byte;
163 
164     return length - 1;
165 }
166 
167 // Internal functions
hci_hal_h4_rx_handler(void * arg)168 static void hci_hal_h4_rx_handler(void *arg)
169 {
170     fixed_queue_process(hci_hal_env.rx_q);
171 }
172 
hci_hal_h4_task_post(uint32_t timeout)173 bool hci_hal_h4_task_post(uint32_t timeout)
174 {
175     return osi_thread_post(hci_h4_thread, hci_hal_h4_rx_handler, NULL, 1, timeout);
176 }
177 
178 #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
hci_packet_complete(BT_HDR * packet)179 static void hci_packet_complete(BT_HDR *packet){
180     uint8_t type;
181     uint16_t handle;
182     uint16_t num_packets = 1;
183     uint8_t *stream = packet->data + packet->offset;
184 
185     STREAM_TO_UINT8(type, stream);
186     if (type == DATA_TYPE_ACL/* || type == DATA_TYPE_SCO*/) {
187         STREAM_TO_UINT16(handle, stream);
188         handle = handle & HCI_DATA_HANDLE_MASK;
189         btsnd_hcic_host_num_xmitted_pkts(1, &handle, &num_packets);
190     }
191 }
192 #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
193 
host_recv_adv_packet(BT_HDR * packet)194 bool host_recv_adv_packet(BT_HDR *packet)
195 {
196     assert(packet);
197     if(packet->data[0] == DATA_TYPE_EVENT && packet->data[1] == HCI_BLE_EVENT) {
198         if(packet->data[3] ==  HCI_BLE_ADV_PKT_RPT_EVT
199 #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
200         || packet->data[3] ==  HCI_BLE_ADV_DISCARD_REPORT_EVT
201 #endif
202         ) {
203             return true;
204         }
205     }
206     return false;
207 }
208 
209 #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
hci_update_adv_report_flow_control(BT_HDR * packet)210 static void hci_update_adv_report_flow_control(BT_HDR *packet)
211 {
212     // this is adv packet
213     if(host_recv_adv_packet(packet)) {
214         // update adv free number
215         hci_hal_env.adv_free_num ++;
216         if (esp_vhci_host_check_send_available()){
217             // send hci cmd
218             btsnd_hcic_ble_update_adv_report_flow_control(hci_hal_env.adv_free_num);
219             hci_hal_env.adv_free_num = 0;
220         } else {
221             //do nothing
222         }
223     }
224 
225 }
226 #endif
227 
hci_hal_h4_hdl_rx_packet(BT_HDR * packet)228 static void hci_hal_h4_hdl_rx_packet(BT_HDR *packet)
229 {
230     uint8_t type, hdr_size;
231     uint16_t length;
232     uint8_t *stream = NULL;
233 
234     if (!packet) {
235         return;
236     }
237     stream = packet->data + packet->offset;
238 
239 #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
240     hci_packet_complete(packet);
241 #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
242 
243     STREAM_TO_UINT8(type, stream);
244     packet->offset++;
245     packet->len--;
246     if (type == HCI_BLE_EVENT) {
247 #if (!CONFIG_BT_STACK_NO_LOG)
248         uint8_t len = 0;
249         STREAM_TO_UINT8(len, stream);
250 #endif
251         HCI_TRACE_ERROR("Workround stream corrupted during LE SCAN: pkt_len=%d ble_event_len=%d\n",
252                   packet->len, len);
253         osi_free(packet);
254         return;
255     }
256     if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
257         HCI_TRACE_ERROR("%s Unknown HCI message type. Dropping this byte 0x%x,"
258                   " min %x, max %x\n", __func__, type,
259                   DATA_TYPE_ACL, DATA_TYPE_EVENT);
260         osi_free(packet);
261         return;
262     }
263     hdr_size = preamble_sizes[type - 1];
264     if (packet->len < hdr_size) {
265         HCI_TRACE_ERROR("Wrong packet length type=%d pkt_len=%d hdr_len=%d",
266                   type, packet->len, hdr_size);
267         osi_free(packet);
268         return;
269     }
270     if (type == DATA_TYPE_ACL) {
271         stream += hdr_size - 2;
272         STREAM_TO_UINT16(length, stream);
273     } else {
274         stream += hdr_size - 1;
275         STREAM_TO_UINT8(length, stream);
276     }
277 
278     if ((length + hdr_size) != packet->len) {
279         HCI_TRACE_ERROR("Wrong packet length type=%d hdr_len=%d pd_len=%d "
280                   "pkt_len=%d", type, hdr_size, length, packet->len);
281         osi_free(packet);
282         return;
283     }
284 
285 #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
286     hci_update_adv_report_flow_control(packet);
287 #endif
288 
289 #if SCAN_QUEUE_CONGEST_CHECK
290     if(BTU_check_queue_is_congest() && host_recv_adv_packet(packet)) {
291         HCI_TRACE_DEBUG("BtuQueue is congested");
292         osi_free(packet);
293         return;
294     }
295 #endif
296     packet->event = outbound_event_types[PACKET_TYPE_TO_INDEX(type)];
297     callbacks->packet_ready(packet);
298 }
299 
event_uart_has_bytes(fixed_queue_t * queue)300 static void event_uart_has_bytes(fixed_queue_t *queue)
301 {
302     BT_HDR *packet;
303     while (!fixed_queue_is_empty(queue)) {
304         packet = fixed_queue_dequeue(queue, FIXED_QUEUE_MAX_TIMEOUT);
305         hci_hal_h4_hdl_rx_packet(packet);
306     }
307 }
308 
host_send_pkt_available_cb(void)309 static void host_send_pkt_available_cb(void)
310 {
311     //Controller rx cache buffer is ready for receiving new host packet
312     //Just Call Host main thread task to process pending packets.
313     hci_host_task_post(OSI_THREAD_MAX_TIMEOUT);
314 }
315 
host_recv_pkt_cb(uint8_t * data,uint16_t len)316 static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
317 {
318     //Target has packet to host, malloc new buffer for packet
319     BT_HDR *pkt;
320     size_t pkt_size;
321 
322     if (hci_hal_env.rx_q == NULL) {
323         return 0;
324     }
325 
326     pkt_size = BT_HDR_SIZE + len;
327     pkt = (BT_HDR *) osi_calloc(pkt_size);
328 
329     if (!pkt) {
330         HCI_TRACE_ERROR("%s couldn't aquire memory for inbound data buffer.\n", __func__);
331         return -1;
332     }
333     pkt->offset = 0;
334     pkt->len = len;
335     pkt->layer_specific = 0;
336     memcpy(pkt->data, data, len);
337     fixed_queue_enqueue(hci_hal_env.rx_q, pkt, FIXED_QUEUE_MAX_TIMEOUT);
338     hci_hal_h4_task_post(0);
339 
340 
341     BTTRC_DUMP_BUFFER("Recv Pkt", pkt->data, len);
342 
343     return 0;
344 }
345 
346 static const esp_vhci_host_callback_t vhci_host_cb = {
347     .notify_host_send_available = host_send_pkt_available_cb,
348     .notify_host_recv = host_recv_pkt_cb,
349 };
350 
351 static const hci_hal_t interface = {
352     hal_open,
353     hal_close,
354     transmit_data,
355 };
356 
hci_hal_h4_get_interface(void)357 const hci_hal_t *hci_hal_h4_get_interface(void)
358 {
359     return &interface;
360 }
361