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>
|
2020-07-09 08:58:13 -04:00
|
|
|
#include "sdkconfig.h"
|
2017-12-07 08:48:27 -05:00
|
|
|
#include "esp_bt.h"
|
2020-07-09 08:58:13 -04:00
|
|
|
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_defs.h"
|
|
|
|
#include "common/bt_trace.h"
|
|
|
|
#include "stack/hcidefs.h"
|
|
|
|
#include "stack/hcimsgs.h"
|
2018-09-14 05:24:25 -04:00
|
|
|
#include "stack/btu.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_vendor_lib.h"
|
|
|
|
#include "hci/hci_internals.h"
|
|
|
|
#include "hci/hci_hal.h"
|
|
|
|
#include "hci/hci_layer.h"
|
|
|
|
#include "osi/allocator.h"
|
|
|
|
#include "hci/packet_fragmenter.h"
|
|
|
|
#include "osi/list.h"
|
|
|
|
#include "osi/alarm.h"
|
|
|
|
#include "osi/thread.h"
|
|
|
|
#include "osi/mutex.h"
|
|
|
|
#include "osi/fixed_queue.h"
|
2022-07-19 10:16:12 -04:00
|
|
|
#include "osi/fixed_pkt_queue.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2018-09-14 05:24:25 -04:00
|
|
|
#define HCI_HOST_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
|
2020-02-12 03:26:31 -05:00
|
|
|
#define HCI_HOST_TASK_STACK_SIZE (2048 + BT_TASK_EXTRA_STACK_SIZE)
|
2018-09-17 04:44:38 -04:00
|
|
|
#define HCI_HOST_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 3)
|
2018-10-10 04:10:20 -04:00
|
|
|
#define HCI_HOST_TASK_NAME "hciT"
|
2022-08-01 08:37:49 -04:00
|
|
|
#define HCI_HOST_TASK_WORKQUEUE_NUM (2)
|
|
|
|
#define HCI_HOST_TASK_WORKQUEUE0_LEN (0)
|
|
|
|
#define HCI_HOST_TASK_WORKQUEUE1_LEN (5)
|
2018-09-14 05:24:25 -04:00
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
typedef struct {
|
2016-11-24 13:10:15 -05:00
|
|
|
bool timer_is_set;
|
|
|
|
osi_alarm_t *command_response_timer;
|
|
|
|
list_t *commands_pending_response;
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_t commands_pending_response_lock;
|
2016-09-26 09:37:39 -04:00
|
|
|
} command_waiting_response_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2016-11-24 13:10:15 -05:00
|
|
|
int command_credits;
|
2022-07-19 10:16:12 -04:00
|
|
|
fixed_pkt_queue_t *command_queue;
|
2016-11-24 13:10:15 -05:00
|
|
|
fixed_queue_t *packet_queue;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
command_waiting_response_t cmd_waiting_q;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
/*
|
|
|
|
non_repeating_timer_t *command_response_timer;
|
|
|
|
list_t *commands_pending_response;
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_t commands_pending_response_lock;
|
2016-11-24 13:10:15 -05:00
|
|
|
*/
|
2016-09-26 09:37:39 -04:00
|
|
|
} hci_host_env_t;
|
|
|
|
|
|
|
|
// Using a define here, because it can be stringified for the property lookup
|
|
|
|
static const uint32_t COMMAND_PENDING_TIMEOUT = 8000;
|
|
|
|
|
|
|
|
// Our interface
|
|
|
|
static bool interface_created;
|
|
|
|
static hci_t interface;
|
|
|
|
static hci_host_env_t hci_host_env;
|
2018-09-14 05:24:25 -04:00
|
|
|
static osi_thread_t *hci_host_thread;
|
2016-09-26 09:37:39 -04:00
|
|
|
static bool hci_host_startup_flag;
|
|
|
|
|
|
|
|
// Modules we import and callbacks we export
|
|
|
|
static const hci_hal_t *hal;
|
|
|
|
static const hci_hal_callbacks_t hal_callbacks;
|
|
|
|
static const packet_fragmenter_t *packet_fragmenter;
|
|
|
|
static const packet_fragmenter_callbacks_t packet_fragmenter_callbacks;
|
|
|
|
|
|
|
|
static int hci_layer_init_env(void);
|
|
|
|
static void hci_layer_deinit_env(void);
|
|
|
|
static void hci_host_thread_handler(void *arg);
|
2022-07-19 10:16:12 -04:00
|
|
|
static void event_command_ready(fixed_pkt_queue_t *queue);
|
2016-09-26 09:37:39 -04:00
|
|
|
static void event_packet_ready(fixed_queue_t *queue);
|
2017-11-08 04:12:08 -05:00
|
|
|
static void restart_command_waiting_response_timer(command_waiting_response_t *cmd_wait_q);
|
2016-09-26 09:37:39 -04:00
|
|
|
static void command_timed_out(void *context);
|
|
|
|
static void hal_says_packet_ready(BT_HDR *packet);
|
|
|
|
static bool filter_incoming_event(BT_HDR *packet);
|
|
|
|
static serial_data_type_t event_to_data_type(uint16_t event);
|
2022-07-19 10:16:12 -04:00
|
|
|
static pkt_linked_item_t *get_waiting_command(command_opcode_t opcode);
|
2016-09-26 09:37:39 -04:00
|
|
|
static void dispatch_reassembled(BT_HDR *packet);
|
2022-08-03 09:54:52 -04:00
|
|
|
static void dispatch_adv_report(pkt_linked_item_t *linked_pkt);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
// Module lifecycle functions
|
2016-11-24 13:10:15 -05:00
|
|
|
int hci_start_up(void)
|
|
|
|
{
|
|
|
|
if (hci_layer_init_env()) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-08-01 08:37:49 -04:00
|
|
|
const size_t workqueue_len[] = {HCI_HOST_TASK_WORKQUEUE0_LEN, HCI_HOST_TASK_WORKQUEUE1_LEN};
|
|
|
|
hci_host_thread = osi_thread_create(HCI_HOST_TASK_NAME, HCI_HOST_TASK_STACK_SIZE, HCI_HOST_TASK_PRIO, HCI_HOST_TASK_PINNED_TO_CORE,
|
|
|
|
HCI_HOST_TASK_WORKQUEUE_NUM, workqueue_len);
|
2018-09-14 05:24:25 -04:00
|
|
|
if (hci_host_thread == NULL) {
|
|
|
|
return -2;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
packet_fragmenter->init(&packet_fragmenter_callbacks);
|
2018-10-10 04:10:20 -04:00
|
|
|
hal->open(&hal_callbacks, hci_host_thread);
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
hci_host_startup_flag = true;
|
|
|
|
return 0;
|
2016-09-26 09:37:39 -04:00
|
|
|
error:
|
2016-11-24 13:10:15 -05:00
|
|
|
hci_shut_down();
|
|
|
|
return -1;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void hci_shut_down(void)
|
|
|
|
{
|
|
|
|
hci_host_startup_flag = false;
|
|
|
|
hci_layer_deinit_env();
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
packet_fragmenter->cleanup();
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
//low_power_manager->cleanup();
|
|
|
|
hal->close();
|
2018-09-14 05:24:25 -04:00
|
|
|
|
|
|
|
osi_thread_free(hci_host_thread);
|
|
|
|
hci_host_thread = NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-17 23:57:28 -04:00
|
|
|
bool hci_host_task_post(uint32_t timeout)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2019-04-17 23:57:28 -04:00
|
|
|
return osi_thread_post(hci_host_thread, hci_host_thread_handler, NULL, 0, timeout);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hci_layer_init_env(void)
|
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
command_waiting_response_t *cmd_wait_q;
|
|
|
|
|
|
|
|
// The host is only allowed to send at most one command initially,
|
|
|
|
// as per the Bluetooth spec, Volume 2, Part E, 4.4 (Command Flow Control)
|
|
|
|
// This value can change when you get a command complete or command status event.
|
|
|
|
hci_host_env.command_credits = 1;
|
2022-07-19 10:16:12 -04:00
|
|
|
hci_host_env.command_queue = fixed_pkt_queue_new(QUEUE_SIZE_MAX);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hci_host_env.command_queue) {
|
2022-07-19 10:16:12 -04:00
|
|
|
fixed_pkt_queue_register_dequeue(hci_host_env.command_queue, event_command_ready);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s unable to create pending command queue.", __func__);
|
2016-11-24 13:10:15 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2018-07-01 22:59:51 -04:00
|
|
|
hci_host_env.packet_queue = fixed_queue_new(QUEUE_SIZE_MAX);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hci_host_env.packet_queue) {
|
|
|
|
fixed_queue_register_dequeue(hci_host_env.packet_queue, event_packet_ready);
|
|
|
|
} else {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s unable to create pending packet queue.", __func__);
|
2016-11-24 13:10:15 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
// Init Commands waiting response list and timer
|
|
|
|
cmd_wait_q = &hci_host_env.cmd_waiting_q;
|
|
|
|
cmd_wait_q->timer_is_set = false;
|
|
|
|
cmd_wait_q->commands_pending_response = list_new(NULL);
|
|
|
|
if (!cmd_wait_q->commands_pending_response) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s unable to create list for commands pending response.", __func__);
|
2016-11-24 13:10:15 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_new(&cmd_wait_q->commands_pending_response_lock);
|
2016-11-24 13:10:15 -05:00
|
|
|
cmd_wait_q->command_response_timer = osi_alarm_new("cmd_rsp_to", command_timed_out, cmd_wait_q, COMMAND_PENDING_TIMEOUT);
|
|
|
|
if (!cmd_wait_q->command_response_timer) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s unable to create command response timer.", __func__);
|
2016-11-24 13:10:15 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2020-07-09 08:58:13 -04:00
|
|
|
#if (BLE_50_FEATURE_SUPPORT == TRUE)
|
|
|
|
btsnd_hcic_ble_sync_sem_init();
|
|
|
|
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
return 0;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_layer_deinit_env(void)
|
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
command_waiting_response_t *cmd_wait_q;
|
|
|
|
|
|
|
|
if (hci_host_env.command_queue) {
|
2022-07-19 10:16:12 -04:00
|
|
|
fixed_pkt_queue_free(hci_host_env.command_queue, (fixed_pkt_queue_free_cb)osi_free_func);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
if (hci_host_env.packet_queue) {
|
2018-08-14 22:40:51 -04:00
|
|
|
fixed_queue_free(hci_host_env.packet_queue, osi_free_func);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd_wait_q = &hci_host_env.cmd_waiting_q;
|
|
|
|
list_free(cmd_wait_q->commands_pending_response);
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_free(&cmd_wait_q->commands_pending_response_lock);
|
2016-11-24 13:10:15 -05:00
|
|
|
osi_alarm_free(cmd_wait_q->command_response_timer);
|
|
|
|
cmd_wait_q->command_response_timer = NULL;
|
2020-07-09 08:58:13 -04:00
|
|
|
#if (BLE_50_FEATURE_SUPPORT == TRUE)
|
|
|
|
btsnd_hcic_ble_sync_sem_deinit();
|
|
|
|
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_host_thread_handler(void *arg)
|
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
/*
|
|
|
|
* Previous task handles RX queue and two TX Queues, Since there is
|
|
|
|
* a RX Thread Task in H4 layer which receives packet from driver layer.
|
|
|
|
* Now HCI Host Task has been optimized to only process TX Queue
|
|
|
|
* including command and data queue. And command queue has high priority,
|
|
|
|
* All packets will be directly copied to single queue in driver layer with
|
|
|
|
* H4 type header added (1 byte).
|
|
|
|
*/
|
2018-09-14 05:24:25 -04:00
|
|
|
if (esp_vhci_host_check_send_available()) {
|
|
|
|
/*Now Target only allowed one packet per TX*/
|
|
|
|
BT_HDR *pkt = packet_fragmenter->fragment_current_packet();
|
|
|
|
if (pkt != NULL) {
|
|
|
|
packet_fragmenter->fragment_and_dispatch(pkt);
|
|
|
|
} else {
|
2022-07-19 10:16:12 -04:00
|
|
|
if (!fixed_pkt_queue_is_empty(hci_host_env.command_queue) &&
|
2018-09-14 05:24:25 -04:00
|
|
|
hci_host_env.command_credits > 0) {
|
2022-07-19 10:16:12 -04:00
|
|
|
fixed_pkt_queue_process(hci_host_env.command_queue);
|
2018-09-14 05:24:25 -04:00
|
|
|
} else if (!fixed_queue_is_empty(hci_host_env.packet_queue)) {
|
|
|
|
fixed_queue_process(hci_host_env.packet_queue);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void transmit_command(
|
|
|
|
BT_HDR *command,
|
|
|
|
command_complete_cb complete_callback,
|
|
|
|
command_status_cb status_callback,
|
2016-11-24 13:10:15 -05:00
|
|
|
void *context)
|
|
|
|
{
|
2022-07-19 10:16:12 -04:00
|
|
|
hci_cmd_metadata_t *metadata = HCI_GET_CMD_METAMSG(command);
|
|
|
|
pkt_linked_item_t *linked_pkt = HCI_GET_CMD_LINKED_STRUCT(metadata);
|
2016-11-24 13:10:15 -05:00
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
assert(command->layer_specific == HCI_CMD_BUF_TYPE_METADATA);
|
2016-11-24 13:10:15 -05:00
|
|
|
// Store the command message type in the event field
|
|
|
|
// in case the upper layer didn't already
|
|
|
|
command->event = MSG_STACK_TO_HC_HCI_CMD;
|
2022-07-19 10:16:12 -04:00
|
|
|
HCI_TRACE_DEBUG("HCI Enqueue Comamnd opcode=0x%x\n", metadata->opcode);
|
2016-11-24 13:10:15 -05:00
|
|
|
BTTRC_DUMP_BUFFER(NULL, command->data + command->offset, command->len);
|
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
fixed_pkt_queue_enqueue(hci_host_env.command_queue, linked_pkt, FIXED_PKT_QUEUE_MAX_TIMEOUT);
|
2019-04-17 23:57:28 -04:00
|
|
|
hci_host_task_post(OSI_THREAD_MAX_TIMEOUT);
|
2018-06-20 23:41:42 -04:00
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static future_t *transmit_command_futured(BT_HDR *command)
|
|
|
|
{
|
2022-07-19 10:16:12 -04:00
|
|
|
hci_cmd_metadata_t *metadata = HCI_GET_CMD_METAMSG(command);
|
|
|
|
pkt_linked_item_t *linked_pkt = HCI_GET_CMD_LINKED_STRUCT(metadata);
|
|
|
|
|
|
|
|
assert(command->layer_specific == HCI_CMD_BUF_TYPE_METADATA);
|
|
|
|
metadata->flags_vnd |= HCI_CMD_MSG_F_VND_FUTURE;
|
2016-10-10 09:34:21 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
future_t *future = future_new();
|
2016-10-10 09:34:21 -04:00
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
metadata->complete_future = future;
|
2016-10-10 09:34:21 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
// Store the command message type in the event field
|
|
|
|
// in case the upper layer didn't already
|
|
|
|
command->event = MSG_STACK_TO_HC_HCI_CMD;
|
2016-10-10 09:34:21 -04:00
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
fixed_pkt_queue_enqueue(hci_host_env.command_queue, linked_pkt, FIXED_PKT_QUEUE_MAX_TIMEOUT);
|
2019-04-17 23:57:28 -04:00
|
|
|
hci_host_task_post(OSI_THREAD_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
return future;
|
2016-10-10 09:34:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void transmit_downward(uint16_t type, void *data)
|
|
|
|
{
|
|
|
|
if (type == MSG_STACK_TO_HC_HCI_CMD) {
|
2022-07-19 10:16:12 -04:00
|
|
|
HCI_TRACE_ERROR("%s legacy transmit of command. Use transmit_command instead.\n", __func__);
|
|
|
|
assert(0);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2019-04-17 23:57:28 -04:00
|
|
|
fixed_queue_enqueue(hci_host_env.packet_queue, data, FIXED_QUEUE_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2017-01-23 03:11:27 -05:00
|
|
|
|
2019-04-17 23:57:28 -04:00
|
|
|
hci_host_task_post(OSI_THREAD_MAX_TIMEOUT);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Command/packet transmitting functions
|
2022-07-19 10:16:12 -04:00
|
|
|
static void event_command_ready(fixed_pkt_queue_t *queue)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
2022-07-19 10:16:12 -04:00
|
|
|
pkt_linked_item_t *wait_entry = NULL;
|
2016-11-24 13:10:15 -05:00
|
|
|
command_waiting_response_t *cmd_wait_q = &hci_host_env.cmd_waiting_q;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
wait_entry = fixed_pkt_queue_dequeue(queue, FIXED_QUEUE_MAX_TIMEOUT);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
hci_cmd_metadata_t *metadata = (hci_cmd_metadata_t *)(wait_entry->data);
|
|
|
|
if (metadata->flags_src & HCI_CMD_MSG_F_SRC_NOACK) {
|
|
|
|
packet_fragmenter->fragment_and_dispatch(&metadata->command);
|
|
|
|
hci_cmd_free_cb free_func = metadata->command_free_cb ? metadata->command_free_cb : (hci_cmd_free_cb) osi_free_func;
|
|
|
|
free_func(wait_entry);
|
2018-06-20 23:41:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
hci_host_env.command_credits--;
|
2016-11-24 13:10:15 -05:00
|
|
|
// Move it to the list of commands awaiting response
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&cmd_wait_q->commands_pending_response_lock, OSI_MUTEX_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
list_append(cmd_wait_q->commands_pending_response, wait_entry);
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
// Send it off
|
2022-07-19 10:16:12 -04:00
|
|
|
packet_fragmenter->fragment_and_dispatch(&metadata->command);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
restart_command_waiting_response_timer(cmd_wait_q);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void event_packet_ready(fixed_queue_t *queue)
|
|
|
|
{
|
2019-04-17 23:57:28 -04:00
|
|
|
BT_HDR *packet = (BT_HDR *)fixed_queue_dequeue(queue, FIXED_QUEUE_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
// The queue may be the command queue or the packet queue, we don't care
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
packet_fragmenter->fragment_and_dispatch(packet);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for the fragmenter to send a fragment
|
2016-11-24 13:10:15 -05:00
|
|
|
static void transmit_fragment(BT_HDR *packet, bool send_transmit_finished)
|
|
|
|
{
|
|
|
|
uint16_t event = packet->event & MSG_EVT_MASK;
|
|
|
|
serial_data_type_t type = event_to_data_type(event);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hal->transmit_data(type, packet->data + packet->offset, packet->len);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (event != MSG_STACK_TO_HC_HCI_CMD && send_transmit_finished) {
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void fragmenter_transmit_finished(BT_HDR *packet, bool all_fragments_sent)
|
|
|
|
{
|
|
|
|
if (all_fragments_sent) {
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
|
|
|
// This is kind of a weird case, since we're dispatching a partially sent packet
|
|
|
|
// up to a higher layer.
|
|
|
|
// TODO(zachoverflow): rework upper layer so this isn't necessary.
|
2018-08-14 22:40:51 -04:00
|
|
|
//osi_free(packet);
|
2017-09-11 05:30:50 -04:00
|
|
|
|
|
|
|
/* dispatch_reassembled(packet) will send the packet back to the higher layer
|
|
|
|
when controller buffer is not enough. hci will send the remain packet back
|
|
|
|
to the l2cap layer and saved in the Link Queue (p_lcb->link_xmit_data_q).
|
|
|
|
The l2cap layer will resend the packet to lower layer when controller buffer
|
|
|
|
can be used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dispatch_reassembled(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
//data_dispatcher_dispatch(interface.event_dispatcher, packet->event & MSG_EVT_MASK, packet);
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
static void restart_command_waiting_response_timer(command_waiting_response_t *cmd_wait_q)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2017-11-08 04:12:08 -05:00
|
|
|
osi_mutex_lock(&cmd_wait_q->commands_pending_response_lock, OSI_MUTEX_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (cmd_wait_q->timer_is_set) {
|
|
|
|
osi_alarm_cancel(cmd_wait_q->command_response_timer);
|
|
|
|
cmd_wait_q->timer_is_set = false;
|
|
|
|
}
|
2017-11-08 04:12:08 -05:00
|
|
|
if (!list_is_empty(cmd_wait_q->commands_pending_response)) {
|
|
|
|
osi_alarm_set(cmd_wait_q->command_response_timer, COMMAND_PENDING_TIMEOUT);
|
|
|
|
cmd_wait_q->timer_is_set = true;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2017-11-08 04:12:08 -05:00
|
|
|
osi_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void command_timed_out(void *context)
|
|
|
|
{
|
|
|
|
command_waiting_response_t *cmd_wait_q = (command_waiting_response_t *)context;
|
2022-07-19 10:16:12 -04:00
|
|
|
pkt_linked_item_t *wait_entry;
|
2016-11-24 13:10:15 -05:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&cmd_wait_q->commands_pending_response_lock, OSI_MUTEX_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
wait_entry = (list_is_empty(cmd_wait_q->commands_pending_response) ?
|
|
|
|
NULL : list_front(cmd_wait_q->commands_pending_response));
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
if (wait_entry == NULL) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s with no commands pending response", __func__);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else
|
|
|
|
// We shouldn't try to recover the stack from this command timeout.
|
|
|
|
// If it's caused by a software bug, fix it. If it's a hardware bug, fix it.
|
|
|
|
{
|
2022-07-19 10:16:12 -04:00
|
|
|
hci_cmd_metadata_t *metadata = (hci_cmd_metadata_t *)(wait_entry->data);
|
|
|
|
HCI_TRACE_ERROR("%s hci layer timeout waiting for response to a command. opcode: 0x%x", __func__, metadata->opcode);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event/packet receiving functions
|
2016-11-24 13:10:15 -05:00
|
|
|
static void hal_says_packet_ready(BT_HDR *packet)
|
|
|
|
{
|
|
|
|
if (packet->event != MSG_HC_TO_STACK_HCI_EVT) {
|
|
|
|
packet_fragmenter->reassemble_and_dispatch(packet);
|
|
|
|
} else if (!filter_incoming_event(packet)) {
|
|
|
|
dispatch_reassembled(packet);
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2022-08-03 09:54:52 -04:00
|
|
|
static void hal_says_adv_rpt_ready(pkt_linked_item_t *linked_pkt)
|
|
|
|
{
|
|
|
|
dispatch_adv_report(linked_pkt);
|
|
|
|
}
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
// Returns true if the event was intercepted and should not proceed to
|
|
|
|
// higher layers. Also inspects an incoming event for interesting
|
|
|
|
// information, like how many commands are now able to be sent.
|
2016-11-24 13:10:15 -05:00
|
|
|
static bool filter_incoming_event(BT_HDR *packet)
|
|
|
|
{
|
2022-07-19 10:16:12 -04:00
|
|
|
pkt_linked_item_t *wait_entry = NULL;
|
|
|
|
hci_cmd_metadata_t *metadata = NULL;
|
2016-11-24 13:10:15 -05:00
|
|
|
uint8_t *stream = packet->data + packet->offset;
|
|
|
|
uint8_t event_code;
|
|
|
|
command_opcode_t opcode;
|
|
|
|
|
|
|
|
STREAM_TO_UINT8(event_code, stream);
|
|
|
|
STREAM_SKIP_UINT8(stream); // Skip the parameter total length field
|
|
|
|
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_DEBUG("Receive packet event_code=0x%x\n", event_code);
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
if (event_code == HCI_COMMAND_COMPLETE_EVT) {
|
|
|
|
STREAM_TO_UINT8(hci_host_env.command_credits, stream);
|
|
|
|
STREAM_TO_UINT16(opcode, stream);
|
|
|
|
wait_entry = get_waiting_command(opcode);
|
2022-07-19 10:16:12 -04:00
|
|
|
metadata = (hci_cmd_metadata_t *)(wait_entry->data);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!wait_entry) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_WARNING("%s command complete event with no matching command. opcode: 0x%x.", __func__, opcode);
|
2022-07-19 10:16:12 -04:00
|
|
|
} else if (metadata->command_complete_cb) {
|
|
|
|
metadata->command_complete_cb(packet, metadata->context);
|
2020-07-09 08:58:13 -04:00
|
|
|
#if (BLE_50_FEATURE_SUPPORT == TRUE)
|
2021-01-27 01:16:41 -05:00
|
|
|
BlE_SYNC *sync_info = btsnd_hcic_ble_get_sync_info();
|
|
|
|
if(!sync_info) {
|
|
|
|
HCI_TRACE_WARNING("%s sync_info is NULL. opcode = 0x%x", __func__, opcode);
|
|
|
|
} else {
|
|
|
|
if (sync_info->sync_sem && sync_info->opcode == opcode) {
|
|
|
|
osi_sem_give(&sync_info->sync_sem);
|
|
|
|
sync_info->opcode = 0;
|
|
|
|
}
|
2020-07-09 08:58:13 -04:00
|
|
|
}
|
|
|
|
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
|
2022-07-19 10:16:12 -04:00
|
|
|
} else if (metadata->flags_vnd & HCI_CMD_MSG_F_VND_FUTURE) {
|
|
|
|
future_ready((future_t *)(metadata->complete_future), packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
goto intercepted;
|
|
|
|
} else if (event_code == HCI_COMMAND_STATUS_EVT) {
|
|
|
|
uint8_t status;
|
|
|
|
STREAM_TO_UINT8(status, stream);
|
|
|
|
STREAM_TO_UINT8(hci_host_env.command_credits, stream);
|
|
|
|
STREAM_TO_UINT16(opcode, stream);
|
|
|
|
|
|
|
|
// If a command generates a command status event, it won't be getting a command complete event
|
|
|
|
|
|
|
|
wait_entry = get_waiting_command(opcode);
|
2022-07-19 10:16:12 -04:00
|
|
|
metadata = (hci_cmd_metadata_t *)(wait_entry->data);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!wait_entry) {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_WARNING("%s command status event with no matching command. opcode: 0x%x", __func__, opcode);
|
2022-07-19 10:16:12 -04:00
|
|
|
} else if (metadata->command_status_cb) {
|
|
|
|
metadata->command_status_cb(status, &metadata->command, metadata->context);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
goto intercepted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-09-26 09:37:39 -04:00
|
|
|
intercepted:
|
2017-11-08 04:12:08 -05:00
|
|
|
restart_command_waiting_response_timer(&hci_host_env.cmd_waiting_q);
|
2016-12-14 08:45:18 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
/*Tell HCI Host Task to continue TX Pending commands*/
|
|
|
|
if (hci_host_env.command_credits &&
|
2022-07-19 10:16:12 -04:00
|
|
|
!fixed_pkt_queue_is_empty(hci_host_env.command_queue)) {
|
2019-04-17 23:57:28 -04:00
|
|
|
hci_host_task_post(OSI_THREAD_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (wait_entry) {
|
|
|
|
// If it has a callback, it's responsible for freeing the packet
|
|
|
|
if (event_code == HCI_COMMAND_STATUS_EVT ||
|
2022-07-19 10:16:12 -04:00
|
|
|
(!metadata->command_complete_cb && !metadata->complete_future)) {
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
// If it has a callback, it's responsible for freeing the command
|
2022-07-19 10:16:12 -04:00
|
|
|
if (event_code == HCI_COMMAND_COMPLETE_EVT || !metadata->command_status_cb) {
|
|
|
|
hci_cmd_free_cb free_func = metadata->command_free_cb ? metadata->command_free_cb : (hci_cmd_free_cb) osi_free_func;
|
|
|
|
free_func(wait_entry);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for the fragmenter to dispatch up a completely reassembled packet
|
2016-11-24 13:10:15 -05:00
|
|
|
static void dispatch_reassembled(BT_HDR *packet)
|
|
|
|
{
|
|
|
|
// Events should already have been dispatched before this point
|
2017-01-23 03:11:27 -05:00
|
|
|
//Tell Up-layer received packet.
|
2019-04-17 23:57:28 -04:00
|
|
|
if (btu_task_post(SIG_BTU_HCI_MSG, packet, OSI_THREAD_MAX_TIMEOUT) == false) {
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(packet);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2022-08-03 09:54:52 -04:00
|
|
|
static void dispatch_adv_report(pkt_linked_item_t *linked_pkt)
|
|
|
|
{
|
|
|
|
// Events should already have been dispatched before this point
|
|
|
|
//Tell Up-layer received packet.
|
|
|
|
if (btu_task_post(SIG_BTU_HCI_ADV_RPT_MSG, linked_pkt, OSI_THREAD_MAX_TIMEOUT) == false) {
|
|
|
|
osi_free(linked_pkt);
|
2022-07-18 07:40:34 -04:00
|
|
|
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
|
|
|
|
hci_adv_credits_try_release(1);
|
|
|
|
#endif
|
2022-08-03 09:54:52 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
// Misc internal functions
|
|
|
|
|
|
|
|
// TODO(zachoverflow): we seem to do this a couple places, like the HCI inject module. #centralize
|
2016-11-24 13:10:15 -05:00
|
|
|
static serial_data_type_t event_to_data_type(uint16_t event)
|
|
|
|
{
|
|
|
|
if (event == MSG_STACK_TO_HC_HCI_ACL) {
|
|
|
|
return DATA_TYPE_ACL;
|
|
|
|
} else if (event == MSG_STACK_TO_HC_HCI_SCO) {
|
|
|
|
return DATA_TYPE_SCO;
|
|
|
|
} else if (event == MSG_STACK_TO_HC_HCI_CMD) {
|
|
|
|
return DATA_TYPE_COMMAND;
|
|
|
|
} else {
|
2018-04-24 22:11:06 -04:00
|
|
|
HCI_TRACE_ERROR("%s invalid event type, could not translate 0x%x\n", __func__, event);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 10:16:12 -04:00
|
|
|
static pkt_linked_item_t *get_waiting_command(command_opcode_t opcode)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
|
|
|
command_waiting_response_t *cmd_wait_q = &hci_host_env.cmd_waiting_q;
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&cmd_wait_q->commands_pending_response_lock, OSI_MUTEX_MAX_TIMEOUT);
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
for (const list_node_t *node = list_begin(cmd_wait_q->commands_pending_response);
|
|
|
|
node != list_end(cmd_wait_q->commands_pending_response);
|
|
|
|
node = list_next(node)) {
|
2022-07-19 10:16:12 -04:00
|
|
|
pkt_linked_item_t *wait_entry = list_node(node);
|
|
|
|
if (wait_entry) {
|
|
|
|
hci_cmd_metadata_t *metadata = (hci_cmd_metadata_t *)(wait_entry->data);
|
|
|
|
if (metadata->opcode == opcode) {
|
|
|
|
list_remove(cmd_wait_q->commands_pending_response, wait_entry);
|
|
|
|
osi_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
|
|
|
|
return wait_entry;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void init_layer_interface(void)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
|
|
|
if (!interface_created) {
|
|
|
|
interface.transmit_command = transmit_command;
|
|
|
|
interface.transmit_command_futured = transmit_command_futured;
|
|
|
|
interface.transmit_downward = transmit_downward;
|
|
|
|
interface_created = true;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const hci_hal_callbacks_t hal_callbacks = {
|
2022-08-03 09:54:52 -04:00
|
|
|
hal_says_packet_ready,
|
|
|
|
hal_says_adv_rpt_ready,
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const packet_fragmenter_callbacks_t packet_fragmenter_callbacks = {
|
2016-11-24 13:10:15 -05:00
|
|
|
transmit_fragment,
|
|
|
|
dispatch_reassembled,
|
|
|
|
fragmenter_transmit_finished
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
const hci_t *hci_layer_get_interface(void)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
|
|
|
hal = hci_hal_h4_get_interface();
|
|
|
|
packet_fragmenter = packet_fragmenter_get_interface();
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
init_layer_interface();
|
|
|
|
return &interface;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|