2016-09-26 09:37:39 -04:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#include <string.h>
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_defs.h"
|
|
|
|
#include "common/bt_trace.h"
|
|
|
|
#include "stack/bt_types.h"
|
|
|
|
#include "hci/hci_hal.h"
|
|
|
|
#include "hci/hci_internals.h"
|
|
|
|
#include "hci/hci_layer.h"
|
|
|
|
#include "osi/thread.h"
|
2022-07-17 13:56:28 -04:00
|
|
|
#include "osi/pkt_queue.h"
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
#include "osi/mutex.h"
|
2022-07-19 03:32:54 -04:00
|
|
|
#include "osi/alarm.h"
|
2022-07-18 07:40:34 -04:00
|
|
|
#endif
|
2017-12-07 08:48:27 -05:00
|
|
|
#include "esp_bt.h"
|
2018-11-01 05:33:44 -04:00
|
|
|
#include "stack/hcimsgs.h"
|
2022-07-17 13:56:28 -04:00
|
|
|
|
2018-06-20 23:41:42 -04:00
|
|
|
#if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
|
|
|
|
#include "l2c_int.h"
|
|
|
|
#endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
|
2018-09-14 05:24:25 -04:00
|
|
|
#include "stack/hcimsgs.h"
|
2024-04-08 08:16:28 -04:00
|
|
|
#include "hci_log/bt_hci_log.h"
|
2022-07-17 13:56:28 -04:00
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
#define HCI_BLE_EVENT 0x3e
|
|
|
|
#define PACKET_TYPE_TO_INBOUND_INDEX(type) ((type) - 2)
|
|
|
|
#define PACKET_TYPE_TO_INDEX(type) ((type) - 1)
|
2022-07-17 08:09:56 -04:00
|
|
|
#define HCI_UPSTREAM_DATA_QUEUE_IDX (1)
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
#define HCI_BLE_ADV_MIN_CREDITS_TO_RELEASE (10)
|
2022-07-19 03:32:54 -04:00
|
|
|
#define HCI_ADV_FLOW_MONITOR_PERIOD_MS (500)
|
2022-07-18 07:40:34 -04:00
|
|
|
#else
|
2022-07-18 03:53:08 -04:00
|
|
|
#define HCI_HAL_BLE_ADV_RPT_QUEUE_LEN_MAX (200)
|
2022-07-18 07:40:34 -04:00
|
|
|
#endif
|
2022-07-17 08:09:56 -04:00
|
|
|
|
2018-07-01 22:59:51 -04:00
|
|
|
extern bool BTU_check_queue_is_congest(void);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t preamble_sizes[] = {
|
2016-11-24 13:10:15 -05:00
|
|
|
HCI_COMMAND_PREAMBLE_SIZE,
|
|
|
|
HCI_ACL_PREAMBLE_SIZE,
|
|
|
|
HCI_SCO_PREAMBLE_SIZE,
|
|
|
|
HCI_EVENT_PREAMBLE_SIZE
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const uint16_t outbound_event_types[] = {
|
2016-11-24 13:10:15 -05:00
|
|
|
MSG_HC_TO_STACK_HCI_ERR,
|
|
|
|
MSG_HC_TO_STACK_HCI_ACL,
|
|
|
|
MSG_HC_TO_STACK_HCI_SCO,
|
|
|
|
MSG_HC_TO_STACK_HCI_EVT
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2016-11-24 13:10:15 -05:00
|
|
|
fixed_queue_t *rx_q;
|
2022-07-17 13:56:28 -04:00
|
|
|
struct pkt_queue *adv_rpt_q;
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
osi_mutex_t adv_flow_lock;
|
2022-07-19 03:32:54 -04:00
|
|
|
osi_alarm_t *adv_flow_monitor;
|
2022-07-18 07:40:34 -04:00
|
|
|
int adv_credits;
|
|
|
|
int adv_credits_to_release;
|
2022-07-19 17:48:28 -04:00
|
|
|
pkt_linked_item_t *adv_fc_cmd_buf;
|
|
|
|
bool cmd_buf_in_use;
|
2022-07-18 07:40:34 -04:00
|
|
|
#endif
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_hal_callbacks_t *callbacks;
|
|
|
|
osi_thread_t *hci_h4_thread;
|
|
|
|
struct osi_event *upstream_data_ready;
|
2016-09-26 09:37:39 -04:00
|
|
|
} hci_hal_env_t;
|
|
|
|
|
|
|
|
|
|
|
|
static hci_hal_env_t hci_hal_env;
|
|
|
|
static const hci_hal_t interface;
|
2017-01-03 02:53:06 -05:00
|
|
|
static const esp_vhci_host_callback_t vhci_host_cb;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
static void host_send_pkt_available_cb(void);
|
|
|
|
static int host_recv_pkt_cb(uint8_t *data, uint16_t len);
|
2022-07-17 08:09:56 -04:00
|
|
|
static void hci_hal_h4_hdl_rx_packet(BT_HDR *packet);
|
2022-07-17 13:56:28 -04:00
|
|
|
static void hci_hal_h4_hdl_rx_adv_rpt(pkt_linked_item_t *linked_pkt);
|
2022-07-17 08:09:56 -04:00
|
|
|
static void hci_upstream_data_handler(void *arg);
|
|
|
|
static bool hci_upstream_data_post(uint32_t timeout);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2022-07-19 03:32:54 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
static void hci_adv_flow_monitor(void *context);
|
2022-07-19 17:48:28 -04:00
|
|
|
static void hci_adv_flow_cmd_free_cb(pkt_linked_item_t *linked_pkt);
|
2022-07-19 03:32:54 -04:00
|
|
|
#endif
|
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
static bool hci_hal_env_init(const hci_hal_callbacks_t *upper_callbacks, osi_thread_t *task_thread)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
2022-07-17 08:09:56 -04:00
|
|
|
assert(upper_callbacks != NULL);
|
|
|
|
assert(task_thread != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_hal_env.hci_h4_thread = task_thread;
|
|
|
|
hci_hal_env.callbacks = (hci_hal_callbacks_t *)upper_callbacks;
|
2022-07-18 07:40:34 -04:00
|
|
|
|
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
2022-07-19 17:48:28 -04:00
|
|
|
hci_hal_env.adv_fc_cmd_buf = osi_calloc(HCI_CMD_LINKED_BUF_SIZE(HCIC_PARAM_SIZE_BLE_UPDATE_ADV_FLOW_CONTROL));
|
|
|
|
assert(hci_hal_env.adv_fc_cmd_buf != NULL);
|
2022-07-18 07:40:34 -04:00
|
|
|
osi_mutex_new(&hci_hal_env.adv_flow_lock);
|
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
hci_hal_env.adv_credits = BLE_ADV_REPORT_FLOW_CONTROL_NUM;
|
|
|
|
hci_hal_env.adv_credits_to_release = 0;
|
2022-07-19 17:48:28 -04:00
|
|
|
hci_hal_env.cmd_buf_in_use = false;
|
2022-07-18 07:40:34 -04:00
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
2022-07-19 03:32:54 -04:00
|
|
|
hci_hal_env.adv_flow_monitor = osi_alarm_new("adv_fc_mon", hci_adv_flow_monitor, NULL, HCI_ADV_FLOW_MONITOR_PERIOD_MS);
|
|
|
|
assert (hci_hal_env.adv_flow_monitor != NULL);
|
2022-07-18 07:40:34 -04:00
|
|
|
#endif
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
hci_hal_env.rx_q = fixed_queue_new(QUEUE_SIZE_MAX);
|
2022-07-17 08:09:56 -04:00
|
|
|
assert(hci_hal_env.rx_q != NULL);
|
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
hci_hal_env.adv_rpt_q = pkt_queue_create();
|
|
|
|
assert(hci_hal_env.adv_rpt_q != NULL);
|
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
struct osi_event *event = osi_event_create(hci_upstream_data_handler, NULL);
|
|
|
|
assert(event != NULL);
|
|
|
|
hci_hal_env.upstream_data_ready = event;
|
|
|
|
osi_event_bind(hci_hal_env.upstream_data_ready, hci_hal_env.hci_h4_thread, HCI_UPSTREAM_DATA_QUEUE_IDX);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void hci_hal_env_deinit(void)
|
|
|
|
{
|
2024-01-29 02:08:44 -05:00
|
|
|
fixed_queue_t *rx_q = hci_hal_env.rx_q;
|
|
|
|
struct pkt_queue *adv_rpt_q = hci_hal_env.adv_rpt_q;
|
|
|
|
struct osi_event *upstream_data_ready = hci_hal_env.upstream_data_ready;
|
2022-07-17 08:09:56 -04:00
|
|
|
|
2024-01-29 02:08:44 -05:00
|
|
|
hci_hal_env.rx_q = NULL;
|
2022-07-17 13:56:28 -04:00
|
|
|
hci_hal_env.adv_rpt_q = NULL;
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_hal_env.upstream_data_ready = NULL;
|
|
|
|
|
2024-01-29 02:08:44 -05:00
|
|
|
fixed_queue_free(rx_q, osi_free_func);
|
|
|
|
|
|
|
|
pkt_queue_destroy(adv_rpt_q, NULL);
|
|
|
|
|
|
|
|
osi_event_delete(upstream_data_ready);
|
|
|
|
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
2022-07-19 17:48:28 -04:00
|
|
|
hci_hal_env.cmd_buf_in_use = true;
|
2022-07-19 03:32:54 -04:00
|
|
|
osi_alarm_cancel(hci_hal_env.adv_flow_monitor);
|
|
|
|
osi_alarm_free(hci_hal_env.adv_flow_monitor);
|
|
|
|
hci_hal_env.adv_flow_monitor = NULL;
|
2022-07-18 07:40:34 -04:00
|
|
|
osi_mutex_free(&hci_hal_env.adv_flow_lock);
|
2022-07-19 17:48:28 -04:00
|
|
|
osi_free(hci_hal_env.adv_fc_cmd_buf);
|
|
|
|
hci_hal_env.adv_fc_cmd_buf = NULL;
|
2022-07-18 07:40:34 -04:00
|
|
|
#endif
|
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_hal_env.hci_h4_thread = NULL;
|
|
|
|
|
|
|
|
memset(&hci_hal_env, 0, sizeof(hci_hal_env_t));
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-10 04:10:20 -04:00
|
|
|
static bool hal_open(const hci_hal_callbacks_t *upper_callbacks, void *task_thread)
|
2016-11-05 07:57:53 -04:00
|
|
|
{
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_hal_env_init(upper_callbacks, (osi_thread_t *)task_thread);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
//register vhci host cb
|
2018-06-15 09:40:51 -04:00
|
|
|
if (esp_vhci_host_register_callback(&vhci_host_cb) != ESP_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-13 09:28:53 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void hal_close(void)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
|
|
|
hci_hal_env_deinit();
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: transmit_data -TX data to low-layer
|
|
|
|
* It is ported from Bluedroid source code, so it is not
|
|
|
|
* needed to use write() to send data.
|
|
|
|
* TODO: Just use firmware API to send data.
|
|
|
|
*/
|
|
|
|
static uint16_t transmit_data(serial_data_type_t type,
|
|
|
|
uint8_t *data, uint16_t length)
|
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
uint8_t previous_byte;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
assert(data != NULL);
|
|
|
|
assert(length > 0);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s invalid data type: %d", __func__, type);
|
2016-11-24 13:10:15 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
// Write the signal byte right before the data
|
|
|
|
--data;
|
|
|
|
previous_byte = *data;
|
|
|
|
*(data) = type;
|
|
|
|
++length;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
BTTRC_DUMP_BUFFER("Transmit Pkt", data, length);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2024-04-08 08:16:28 -04:00
|
|
|
#if (BT_HCI_LOG_INCLUDED == TRUE)
|
2024-05-13 23:13:54 -04:00
|
|
|
bt_hci_log_record_hci_data(data[0], &data[1], length - 1);
|
2024-04-08 08:16:28 -04:00
|
|
|
#endif
|
2016-11-24 13:10:15 -05:00
|
|
|
// TX Data to target
|
2017-01-03 02:53:06 -05:00
|
|
|
esp_vhci_host_send_packet(data, length);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
// Be nice and restore the old value of that byte
|
|
|
|
*(data) = previous_byte;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return length - 1;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal functions
|
2022-07-17 08:09:56 -04:00
|
|
|
static void hci_upstream_data_handler(void *arg)
|
2016-11-05 07:57:53 -04:00
|
|
|
{
|
2022-07-17 13:56:28 -04:00
|
|
|
fixed_queue_t *rx_q = hci_hal_env.rx_q;
|
|
|
|
struct pkt_queue *adv_rpt_q = hci_hal_env.adv_rpt_q;
|
|
|
|
size_t pkts_to_process;
|
|
|
|
|
|
|
|
do {
|
|
|
|
pkts_to_process = fixed_queue_length(rx_q);
|
|
|
|
for (size_t i = 0; i < pkts_to_process; i++) {
|
|
|
|
BT_HDR *packet = fixed_queue_dequeue(rx_q, 0);
|
|
|
|
if (packet != NULL) {
|
|
|
|
hci_hal_h4_hdl_rx_packet(packet);
|
|
|
|
}
|
2022-07-17 08:09:56 -04:00
|
|
|
}
|
2022-07-17 13:56:28 -04:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
do {
|
|
|
|
pkts_to_process = pkt_queue_length(adv_rpt_q);
|
|
|
|
for (size_t i = 0; i < pkts_to_process; i++) {
|
|
|
|
pkt_linked_item_t *linked_pkt = pkt_queue_dequeue(adv_rpt_q);
|
|
|
|
if (linked_pkt != NULL) {
|
|
|
|
hci_hal_h4_hdl_rx_adv_rpt(linked_pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
2022-07-17 08:09:56 -04:00
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
if (!fixed_queue_is_empty(rx_q) || pkt_queue_length(adv_rpt_q) > 0) {
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_upstream_data_post(OSI_THREAD_MAX_TIMEOUT);
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
static bool hci_upstream_data_post(uint32_t timeout)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2022-07-17 08:09:56 -04:00
|
|
|
return osi_thread_post_event(hci_hal_env.upstream_data_ready, timeout);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-20 23:41:42 -04:00
|
|
|
#if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
|
|
|
|
static void hci_packet_complete(BT_HDR *packet){
|
2021-06-10 04:24:30 -04:00
|
|
|
uint8_t type;
|
2018-06-20 23:41:42 -04:00
|
|
|
uint16_t handle;
|
2021-06-10 04:24:30 -04:00
|
|
|
uint16_t num_packets = 1;
|
2018-06-20 23:41:42 -04:00
|
|
|
uint8_t *stream = packet->data + packet->offset;
|
|
|
|
|
|
|
|
STREAM_TO_UINT8(type, stream);
|
|
|
|
if (type == DATA_TYPE_ACL/* || type == DATA_TYPE_SCO*/) {
|
|
|
|
STREAM_TO_UINT16(handle, stream);
|
|
|
|
handle = handle & HCI_DATA_HANDLE_MASK;
|
2021-06-10 04:24:30 -04:00
|
|
|
btsnd_hcic_host_num_xmitted_pkts(1, &handle, &num_packets);
|
2018-06-20 23:41:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
|
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
bool host_recv_adv_packet(uint8_t *packet)
|
2018-07-01 22:59:51 -04:00
|
|
|
{
|
|
|
|
assert(packet);
|
2022-07-17 13:56:28 -04:00
|
|
|
if(packet[0] == DATA_TYPE_EVENT && packet[1] == HCI_BLE_EVENT) {
|
2022-07-20 23:50:32 -04:00
|
|
|
if(packet[3] == HCI_BLE_ADV_PKT_RPT_EVT || packet[3] == HCI_BLE_DIRECT_ADV_EVT
|
2019-03-01 08:59:55 -05:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
2022-07-17 13:56:28 -04:00
|
|
|
|| packet[3] == HCI_BLE_ADV_DISCARD_REPORT_EVT
|
2019-03-01 08:59:55 -05:00
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-01 22:59:51 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-20 23:41:42 -04:00
|
|
|
|
2019-03-01 08:59:55 -05:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
2022-07-19 03:32:54 -04:00
|
|
|
static void hci_adv_flow_monitor(void *context)
|
|
|
|
{
|
|
|
|
hci_adv_credits_force_release(0);
|
|
|
|
}
|
|
|
|
|
2022-07-18 07:40:34 -04:00
|
|
|
static void hci_adv_credits_consumed(uint16_t num)
|
|
|
|
{
|
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
assert(hci_hal_env.adv_credits >= num);
|
|
|
|
hci_hal_env.adv_credits -= num;
|
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_adv_credits_prep_to_release(uint16_t num)
|
|
|
|
{
|
|
|
|
if (num == 0) {
|
|
|
|
return hci_hal_env.adv_credits_to_release;
|
|
|
|
}
|
|
|
|
|
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
int credits_to_release = hci_hal_env.adv_credits_to_release + num;
|
|
|
|
assert(hci_hal_env.adv_credits_to_release <= BLE_ADV_REPORT_FLOW_CONTROL_NUM);
|
|
|
|
hci_hal_env.adv_credits_to_release = credits_to_release;
|
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
|
|
|
|
2022-07-19 17:48:28 -04:00
|
|
|
if (credits_to_release == num && num != 0) {
|
2022-07-19 03:32:54 -04:00
|
|
|
osi_alarm_cancel(hci_hal_env.adv_flow_monitor);
|
|
|
|
osi_alarm_set(hci_hal_env.adv_flow_monitor, HCI_ADV_FLOW_MONITOR_PERIOD_MS);
|
|
|
|
}
|
2022-07-18 07:40:34 -04:00
|
|
|
return credits_to_release;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hci_adv_credits_release(void)
|
2019-03-01 08:59:55 -05:00
|
|
|
{
|
2022-07-18 07:40:34 -04:00
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
int credits_released = hci_hal_env.adv_credits_to_release;
|
|
|
|
hci_hal_env.adv_credits += credits_released;
|
|
|
|
hci_hal_env.adv_credits_to_release -= credits_released;
|
|
|
|
assert(hci_hal_env.adv_credits <= BLE_ADV_REPORT_FLOW_CONTROL_NUM);
|
|
|
|
assert(hci_hal_env.adv_credits_to_release >= 0);
|
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
|
|
|
|
2022-07-19 03:32:54 -04:00
|
|
|
if (hci_hal_env.adv_credits_to_release == 0) {
|
|
|
|
osi_alarm_cancel(hci_hal_env.adv_flow_monitor);
|
|
|
|
}
|
2022-07-18 07:40:34 -04:00
|
|
|
return credits_released;
|
|
|
|
}
|
|
|
|
|
2022-07-19 17:48:28 -04:00
|
|
|
static int hci_adv_credits_release_rollback(uint16_t num)
|
|
|
|
{
|
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
hci_hal_env.adv_credits -= num;
|
|
|
|
hci_hal_env.adv_credits_to_release += num;
|
|
|
|
assert(hci_hal_env.adv_credits >=0);
|
|
|
|
assert(hci_hal_env.adv_credits_to_release <= BLE_ADV_REPORT_FLOW_CONTROL_NUM);
|
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_adv_flow_cmd_free_cb(pkt_linked_item_t *linked_pkt)
|
|
|
|
{
|
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
hci_hal_env.cmd_buf_in_use = false;
|
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
|
|
|
hci_adv_credits_try_release(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hci_adv_flow_try_send_command(uint16_t credits_released)
|
|
|
|
{
|
|
|
|
bool sent = false;
|
|
|
|
bool use_static_buffer = false;
|
|
|
|
|
|
|
|
/* first try using static buffer, then dynamic buffer */
|
|
|
|
if (!hci_hal_env.cmd_buf_in_use) {
|
|
|
|
osi_mutex_lock(&hci_hal_env.adv_flow_lock, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
if (!hci_hal_env.cmd_buf_in_use) {
|
|
|
|
hci_hal_env.cmd_buf_in_use = true;
|
|
|
|
use_static_buffer = true;
|
|
|
|
}
|
|
|
|
osi_mutex_unlock(&hci_hal_env.adv_flow_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_static_buffer) {
|
|
|
|
hci_cmd_metadata_t *metadata = (hci_cmd_metadata_t *)(hci_hal_env.adv_fc_cmd_buf->data);
|
|
|
|
BT_HDR *static_buffer = &metadata->command;
|
|
|
|
metadata->command_free_cb = hci_adv_flow_cmd_free_cb;
|
|
|
|
sent = btsnd_hcic_ble_update_adv_report_flow_control(credits_released, static_buffer);
|
|
|
|
} else {
|
|
|
|
sent = btsnd_hcic_ble_update_adv_report_flow_control(credits_released, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2022-07-18 07:40:34 -04:00
|
|
|
int hci_adv_credits_try_release(uint16_t num)
|
|
|
|
{
|
|
|
|
int credits_released = 0;
|
|
|
|
if (hci_adv_credits_prep_to_release(num) >= HCI_BLE_ADV_MIN_CREDITS_TO_RELEASE) {
|
|
|
|
credits_released = hci_adv_credits_release();
|
|
|
|
if (credits_released > 0) {
|
2022-07-19 17:48:28 -04:00
|
|
|
if (!hci_adv_flow_try_send_command(credits_released)) {
|
|
|
|
hci_adv_credits_release_rollback(credits_released);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert (credits_released == 0);
|
2019-03-01 08:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 07:40:34 -04:00
|
|
|
return credits_released;
|
|
|
|
}
|
2019-11-04 01:33:39 -05:00
|
|
|
|
2022-07-18 07:40:34 -04:00
|
|
|
int hci_adv_credits_force_release(uint16_t num)
|
|
|
|
{
|
|
|
|
hci_adv_credits_prep_to_release(num);
|
|
|
|
int credits_released = hci_adv_credits_release();
|
|
|
|
if (credits_released > 0) {
|
2022-07-19 17:48:28 -04:00
|
|
|
if (!hci_adv_flow_try_send_command(credits_released)) {
|
|
|
|
hci_adv_credits_release_rollback(credits_released);
|
|
|
|
}
|
2022-07-18 07:40:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return credits_released;
|
2019-03-01 08:59:55 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void hci_hal_h4_hdl_rx_packet(BT_HDR *packet)
|
|
|
|
{
|
|
|
|
uint8_t type, hdr_size;
|
|
|
|
uint16_t length;
|
2021-11-12 01:40:23 -05:00
|
|
|
uint8_t *stream = NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!packet) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-12 01:40:23 -05:00
|
|
|
stream = packet->data + packet->offset;
|
2018-06-20 23:41:42 -04:00
|
|
|
|
|
|
|
#if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
|
|
|
|
hci_packet_complete(packet);
|
|
|
|
#endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
STREAM_TO_UINT8(type, stream);
|
|
|
|
packet->offset++;
|
|
|
|
packet->len--;
|
|
|
|
if (type == HCI_BLE_EVENT) {
|
2019-03-15 08:59:55 -04:00
|
|
|
#if (!CONFIG_BT_STACK_NO_LOG)
|
2017-04-20 07:18:05 -04:00
|
|
|
uint8_t len = 0;
|
2016-11-24 13:10:15 -05:00
|
|
|
STREAM_TO_UINT8(len, stream);
|
2019-03-15 08:59:55 -04:00
|
|
|
#endif
|
2024-04-08 08:16:28 -04:00
|
|
|
HCI_TRACE_ERROR("Workaround stream corrupted during LE SCAN: pkt_len=%d ble_event_len=%d\n",
|
2016-11-24 13:10:15 -05:00
|
|
|
packet->len, len);
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s Unknown HCI message type. Dropping this byte 0x%x,"
|
2016-11-24 13:10:15 -05:00
|
|
|
" min %x, max %x\n", __func__, type,
|
|
|
|
DATA_TYPE_ACL, DATA_TYPE_EVENT);
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
hdr_size = preamble_sizes[type - 1];
|
|
|
|
if (packet->len < hdr_size) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("Wrong packet length type=%d pkt_len=%d hdr_len=%d",
|
2016-11-24 13:10:15 -05:00
|
|
|
type, packet->len, hdr_size);
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == DATA_TYPE_ACL) {
|
|
|
|
stream += hdr_size - 2;
|
|
|
|
STREAM_TO_UINT16(length, stream);
|
|
|
|
} else {
|
|
|
|
stream += hdr_size - 1;
|
|
|
|
STREAM_TO_UINT8(length, stream);
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((length + hdr_size) != packet->len) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("Wrong packet length type=%d hdr_len=%d pd_len=%d "
|
2016-11-24 13:10:15 -05:00
|
|
|
"pkt_len=%d", type, hdr_size, length, packet->len);
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-03-01 08:59:55 -05:00
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
packet->event = outbound_event_types[PACKET_TYPE_TO_INDEX(type)];
|
|
|
|
hci_hal_env.callbacks->packet_ready(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_hal_h4_hdl_rx_adv_rpt(pkt_linked_item_t *linked_pkt)
|
|
|
|
{
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t hdr_size;
|
|
|
|
uint16_t length;
|
|
|
|
uint8_t *stream = NULL;
|
|
|
|
|
|
|
|
if (!linked_pkt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_HDR* packet = (BT_HDR *)linked_pkt->data;
|
|
|
|
stream = packet->data + packet->offset;
|
|
|
|
|
|
|
|
assert(host_recv_adv_packet(stream) == true);
|
|
|
|
|
|
|
|
STREAM_TO_UINT8(type, stream);
|
|
|
|
packet->offset++;
|
|
|
|
packet->len--;
|
|
|
|
hdr_size = preamble_sizes[type - 1];
|
|
|
|
|
|
|
|
if (packet->len < hdr_size) {
|
|
|
|
HCI_TRACE_ERROR("Wrong packet length type=%d pkt_len=%d hdr_len=%d",
|
|
|
|
type, packet->len, hdr_size);
|
2022-07-18 07:40:34 -04:00
|
|
|
goto _discard_packet;
|
2022-07-17 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream += hdr_size - 1;
|
|
|
|
STREAM_TO_UINT8(length, stream);
|
|
|
|
if ((length + hdr_size) != packet->len) {
|
|
|
|
HCI_TRACE_ERROR("Wrong packet length type=%d hdr_len=%d pd_len=%d "
|
|
|
|
"pkt_len=%d", type, hdr_size, length, packet->len);
|
2022-07-18 07:40:34 -04:00
|
|
|
goto _discard_packet;
|
2022-07-17 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
2018-07-01 22:59:51 -04:00
|
|
|
#if SCAN_QUEUE_CONGEST_CHECK
|
2022-07-17 13:56:28 -04:00
|
|
|
if(BTU_check_queue_is_congest()) {
|
2019-04-26 04:11:53 -04:00
|
|
|
HCI_TRACE_DEBUG("BtuQueue is congested");
|
2022-07-18 07:40:34 -04:00
|
|
|
goto _discard_packet;
|
2018-07-01 22:59:51 -04:00
|
|
|
}
|
|
|
|
#endif
|
2022-07-17 13:56:28 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
packet->event = outbound_event_types[PACKET_TYPE_TO_INDEX(type)];
|
2022-07-17 13:56:28 -04:00
|
|
|
hci_hal_env.callbacks->adv_rpt_ready(linked_pkt);
|
2022-07-18 07:40:34 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
_discard_packet:
|
|
|
|
osi_free(linked_pkt);
|
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
hci_adv_credits_prep_to_release(1);
|
|
|
|
#endif
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void host_send_pkt_available_cb(void)
|
|
|
|
{
|
|
|
|
//Controller rx cache buffer is ready for receiving new host packet
|
|
|
|
//Just Call Host main thread task to process pending packets.
|
2022-07-21 14:57:53 -04:00
|
|
|
hci_downstream_data_post(OSI_THREAD_MAX_TIMEOUT);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2024-05-13 23:13:54 -04:00
|
|
|
|
|
|
|
void bt_record_hci_data(uint8_t *data, uint16_t len)
|
|
|
|
{
|
|
|
|
#if (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
if ((data[0] == DATA_TYPE_EVENT) && (data[1] == HCI_BLE_EVENT) && ((data[3] == HCI_BLE_ADV_PKT_RPT_EVT) || (data[3] == HCI_BLE_DIRECT_ADV_EVT)
|
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
|| (data[3] == HCI_BLE_ADV_DISCARD_REPORT_EVT)
|
|
|
|
#endif // (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
#if (BLE_50_FEATURE_SUPPORT == TRUE)
|
|
|
|
|| (data[3] == HCI_BLE_EXT_ADV_REPORT_EVT) || (data[3] == HCI_BLE_PERIOD_ADV_REPORT_EVT)
|
|
|
|
#endif // (BLE_50_FEATURE_SUPPORT == TRUE)
|
|
|
|
)) {
|
|
|
|
bt_hci_log_record_hci_adv(HCI_LOG_DATA_TYPE_ADV, &data[2], len - 2);
|
|
|
|
} else {
|
|
|
|
uint8_t data_type = ((data[0] == 2) ? HCI_LOG_DATA_TYPE_C2H_ACL : data[0]);
|
|
|
|
bt_hci_log_record_hci_data(data_type, &data[1], len - 1);
|
|
|
|
}
|
|
|
|
#endif // (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
|
|
|
|
{
|
|
|
|
//Target has packet to host, malloc new buffer for packet
|
2022-07-17 13:56:28 -04:00
|
|
|
BT_HDR *pkt = NULL;
|
|
|
|
pkt_linked_item_t *linked_pkt = NULL;
|
2016-11-24 13:10:15 -05:00
|
|
|
size_t pkt_size;
|
|
|
|
|
2018-07-17 03:46:56 -04:00
|
|
|
if (hci_hal_env.rx_q == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-13 23:13:54 -04:00
|
|
|
bt_record_hci_data(data, len);
|
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
bool is_adv_rpt = host_recv_adv_packet(data);
|
|
|
|
|
|
|
|
if (!is_adv_rpt) {
|
|
|
|
pkt_size = BT_HDR_SIZE + len;
|
|
|
|
pkt = (BT_HDR *) osi_calloc(pkt_size);
|
|
|
|
if (!pkt) {
|
2024-04-08 08:16:28 -04:00
|
|
|
HCI_TRACE_ERROR("%s couldn't acquire memory for inbound data buffer.\n", __func__);
|
2022-07-17 13:56:28 -04:00
|
|
|
assert(0);
|
|
|
|
}
|
2019-11-04 03:39:53 -05:00
|
|
|
|
2022-07-17 13:56:28 -04:00
|
|
|
pkt->offset = 0;
|
|
|
|
pkt->len = len;
|
|
|
|
pkt->layer_specific = 0;
|
|
|
|
memcpy(pkt->data, data, len);
|
|
|
|
fixed_queue_enqueue(hci_hal_env.rx_q, pkt, FIXED_QUEUE_MAX_TIMEOUT);
|
|
|
|
} else {
|
2022-07-18 03:53:08 -04:00
|
|
|
#if !BLE_ADV_REPORT_FLOW_CONTROL
|
|
|
|
// drop the packets if pkt_queue length goes beyond upper limit
|
|
|
|
if (pkt_queue_length(hci_hal_env.adv_rpt_q) > HCI_HAL_BLE_ADV_RPT_QUEUE_LEN_MAX) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-17 13:56:28 -04:00
|
|
|
pkt_size = BT_PKT_LINKED_HDR_SIZE + BT_HDR_SIZE + len;
|
2024-06-20 00:22:37 -04:00
|
|
|
linked_pkt = (pkt_linked_item_t *) osi_calloc_base(pkt_size);
|
2022-07-17 13:56:28 -04:00
|
|
|
if (!linked_pkt) {
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
hci_adv_credits_consumed(1);
|
|
|
|
hci_adv_credits_prep_to_release(1);
|
|
|
|
#endif
|
2022-07-17 13:56:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pkt = (BT_HDR *)linked_pkt->data;
|
|
|
|
pkt->offset = 0;
|
|
|
|
pkt->len = len;
|
|
|
|
pkt->layer_specific = 0;
|
|
|
|
memcpy(pkt->data, data, len);
|
|
|
|
pkt_queue_enqueue(hci_hal_env.adv_rpt_q, linked_pkt);
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
hci_adv_credits_consumed(1);
|
|
|
|
#endif
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2022-07-17 13:56:28 -04:00
|
|
|
|
2022-07-17 08:09:56 -04:00
|
|
|
hci_upstream_data_post(OSI_THREAD_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
BTTRC_DUMP_BUFFER("Recv Pkt", pkt->data, len);
|
|
|
|
|
|
|
|
return 0;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
2022-01-31 07:21:46 -05:00
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
static const esp_vhci_host_callback_t vhci_host_cb = {
|
2016-11-24 13:10:15 -05:00
|
|
|
.notify_host_send_available = host_send_pkt_available_cb,
|
|
|
|
.notify_host_recv = host_recv_pkt_cb,
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const hci_hal_t interface = {
|
2016-11-24 13:10:15 -05:00
|
|
|
hal_open,
|
|
|
|
hal_close,
|
|
|
|
transmit_data,
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
const hci_hal_t *hci_hal_h4_get_interface(void)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
|
|
|
return &interface;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|