mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
fix(nimble): Address review comments for hidd / hidh code
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2017-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -40,6 +40,34 @@ static inline void unlock_devices(void)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Atomically checks whether a device is still in the global list and acquires
|
||||
* its mutex before releasing the list lock. This avoids TOCTOU between
|
||||
* `esp_hidh_dev_exists()` and `esp_hidh_dev_lock()` for call sites that need
|
||||
* a stable pointer.
|
||||
*/
|
||||
static bool lock_known_device(esp_hidh_dev_t *dev)
|
||||
{
|
||||
if (dev == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
esp_hidh_dev_t *d = NULL;
|
||||
lock_devices();
|
||||
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
|
||||
if (d == dev) {
|
||||
found = true;
|
||||
if (dev->mutex != NULL) {
|
||||
xSemaphoreTake(dev->mutex, portMAX_DELAY);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
unlock_devices();
|
||||
return found;
|
||||
}
|
||||
|
||||
/*
|
||||
* Public Functions
|
||||
* */
|
||||
@@ -207,8 +235,7 @@ esp_hidh_dev_t *esp_hidh_dev_open(uint8_t *bda, esp_hid_transport_t transport, u
|
||||
esp_err_t esp_hidh_dev_close(esp_hidh_dev_t *dev)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->close(dev);
|
||||
esp_hidh_dev_unlock(dev);
|
||||
} else {
|
||||
@@ -219,8 +246,7 @@ esp_err_t esp_hidh_dev_close(esp_hidh_dev_t *dev)
|
||||
|
||||
void esp_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp)
|
||||
{
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
dev->dump(dev, fp);
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -229,8 +255,7 @@ void esp_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp)
|
||||
esp_err_t esp_hidh_dev_output_set(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, uint8_t *value, size_t value_len)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->report_write(dev, map_index, report_id, ESP_HID_REPORT_TYPE_OUTPUT, value, value_len);
|
||||
esp_hidh_dev_unlock(dev);
|
||||
} else {
|
||||
@@ -242,8 +267,7 @@ esp_err_t esp_hidh_dev_output_set(esp_hidh_dev_t *dev, size_t map_index, size_t
|
||||
esp_err_t esp_hidh_dev_feature_set(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, uint8_t *value, size_t value_len)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->report_write(dev, map_index, report_id, ESP_HID_REPORT_TYPE_FEATURE, value, value_len);
|
||||
esp_hidh_dev_unlock(dev);
|
||||
} else {
|
||||
@@ -255,8 +279,7 @@ esp_err_t esp_hidh_dev_feature_set(esp_hidh_dev_t *dev, size_t map_index, size_t
|
||||
esp_err_t esp_hidh_dev_feature_get(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, size_t max_length, uint8_t *value, size_t *value_len)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->report_read(dev, map_index, report_id, ESP_HID_REPORT_TYPE_FEATURE, max_length, value, value_len);
|
||||
esp_hidh_dev_unlock(dev);
|
||||
} else {
|
||||
@@ -268,8 +291,7 @@ esp_err_t esp_hidh_dev_feature_get(esp_hidh_dev_t *dev, size_t map_index, size_t
|
||||
esp_err_t esp_hidh_dev_set_report(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t length)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
if (dev->set_report) {
|
||||
ret = dev->set_report(dev, map_index, report_id, report_type, data, length);
|
||||
} else {
|
||||
@@ -286,8 +308,7 @@ esp_err_t esp_hidh_dev_get_report(esp_hidh_dev_t *dev, size_t map_index, size_t
|
||||
size_t max_len)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->report_read(dev, map_index, report_id, report_type, max_len, NULL, NULL);
|
||||
esp_hidh_dev_unlock(dev);
|
||||
} else {
|
||||
@@ -299,8 +320,7 @@ esp_err_t esp_hidh_dev_get_report(esp_hidh_dev_t *dev, size_t map_index, size_t
|
||||
esp_err_t esp_hidh_dev_get_idle(esp_hidh_dev_t *dev)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
if (dev->get_idle) {
|
||||
ret = dev->get_idle(dev);
|
||||
} else {
|
||||
@@ -316,8 +336,7 @@ esp_err_t esp_hidh_dev_get_idle(esp_hidh_dev_t *dev)
|
||||
esp_err_t esp_hidh_dev_set_idle(esp_hidh_dev_t *dev, uint8_t idle_time)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
if (dev->set_idle) {
|
||||
ret = dev->set_idle(dev, idle_time);
|
||||
} else {
|
||||
@@ -333,8 +352,7 @@ esp_err_t esp_hidh_dev_set_idle(esp_hidh_dev_t *dev, uint8_t idle_time)
|
||||
esp_err_t esp_hidh_dev_get_protocol(esp_hidh_dev_t *dev)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
if (dev->get_protocol) {
|
||||
ret = dev->get_protocol(dev);
|
||||
} else {
|
||||
@@ -350,8 +368,7 @@ esp_err_t esp_hidh_dev_get_protocol(esp_hidh_dev_t *dev)
|
||||
esp_err_t esp_hidh_dev_set_protocol(esp_hidh_dev_t *dev, uint8_t protocol_mode)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
if (dev->set_protocol) {
|
||||
ret = dev->set_protocol(dev, protocol_mode);
|
||||
} else {
|
||||
@@ -368,8 +385,7 @@ const uint8_t *esp_hidh_dev_bda_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
uint8_t *ret = NULL;
|
||||
#if CONFIG_BLUEDROID_ENABLED || CONFIG_BT_NIMBLE_ENABLED
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->addr.bda;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -380,8 +396,7 @@ const uint8_t *esp_hidh_dev_bda_get(esp_hidh_dev_t *dev)
|
||||
esp_hid_transport_t esp_hidh_dev_transport_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
esp_hid_transport_t ret = ESP_HID_TRANSPORT_MAX;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->transport;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -391,8 +406,7 @@ esp_hid_transport_t esp_hidh_dev_transport_get(esp_hidh_dev_t *dev)
|
||||
const esp_hid_device_config_t *esp_hidh_dev_config_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
esp_hid_device_config_t *ret = NULL;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = &dev->config;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -402,8 +416,7 @@ const esp_hid_device_config_t *esp_hidh_dev_config_get(esp_hidh_dev_t *dev)
|
||||
const char *esp_hidh_dev_name_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
const char * ret = NULL;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->config.device_name ? dev->config.device_name : "";
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -413,8 +426,7 @@ const char *esp_hidh_dev_name_get(esp_hidh_dev_t *dev)
|
||||
const char *esp_hidh_dev_manufacturer_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
const char *ret = NULL;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->config.manufacturer_name ? dev->config.manufacturer_name : "";
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -424,8 +436,7 @@ const char *esp_hidh_dev_manufacturer_get(esp_hidh_dev_t *dev)
|
||||
const char *esp_hidh_dev_serial_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
const char *ret = NULL;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->config.serial_number ? dev->config.serial_number : "";
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -435,8 +446,7 @@ const char *esp_hidh_dev_serial_get(esp_hidh_dev_t *dev)
|
||||
uint16_t esp_hidh_dev_vendor_id_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
uint16_t ret = 0;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->config.vendor_id;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -446,8 +456,7 @@ uint16_t esp_hidh_dev_vendor_id_get(esp_hidh_dev_t *dev)
|
||||
uint16_t esp_hidh_dev_product_id_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
uint16_t ret = 0;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->config.product_id;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -457,8 +466,7 @@ uint16_t esp_hidh_dev_product_id_get(esp_hidh_dev_t *dev)
|
||||
uint16_t esp_hidh_dev_version_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
uint16_t ret = 0;
|
||||
if (!esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->config.version;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -468,8 +476,7 @@ uint16_t esp_hidh_dev_version_get(esp_hidh_dev_t *dev)
|
||||
esp_hid_usage_t esp_hidh_dev_usage_get(esp_hidh_dev_t *dev)
|
||||
{
|
||||
esp_hid_usage_t ret = ESP_HID_USAGE_GENERIC;
|
||||
if (esp_hidh_dev_exists(dev)) {
|
||||
esp_hidh_dev_lock(dev);
|
||||
if (lock_known_device(dev)) {
|
||||
ret = dev->usage;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
}
|
||||
@@ -481,11 +488,9 @@ esp_err_t esp_hidh_dev_reports_get(esp_hidh_dev_t *dev, size_t *num_reports, esp
|
||||
esp_err_t ret = 0;
|
||||
esp_hid_report_item_t *r = NULL;
|
||||
|
||||
if (!esp_hidh_dev_exists(dev)) {
|
||||
if (!lock_known_device(dev)) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
esp_hidh_dev_lock(dev);
|
||||
do {
|
||||
r = (esp_hid_report_item_t *)malloc(sizeof(esp_hid_report_item_t) * dev->reports_len);
|
||||
if (r == NULL) {
|
||||
@@ -521,10 +526,9 @@ error_:;
|
||||
|
||||
esp_err_t esp_hidh_dev_report_maps_get(esp_hidh_dev_t *dev, size_t *num_maps, esp_hid_raw_report_map_t **maps)
|
||||
{
|
||||
if (!esp_hidh_dev_exists(dev)) {
|
||||
if (!lock_known_device(dev)) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
esp_hidh_dev_lock(dev);
|
||||
*num_maps = dev->config.report_maps_len;
|
||||
*maps = dev->config.report_maps;
|
||||
esp_hidh_dev_unlock(dev);
|
||||
@@ -692,6 +696,9 @@ static void esp_hidh_dev_resources_free(esp_hidh_dev_t *dev)
|
||||
}
|
||||
}
|
||||
free((void *)dev->config.report_maps);
|
||||
#if CONFIG_BT_NIMBLE_ENABLED
|
||||
free((void *)dev->protocol_mode);
|
||||
#endif
|
||||
esp_hidh_dev_report_t *r;
|
||||
while (dev->reports) {
|
||||
r = dev->reports;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2017-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "ble_hidd.h"
|
||||
#include "esp_private/esp_hidd_private.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
@@ -35,6 +36,27 @@ static const char *TAG = "NIMBLE_HIDD";
|
||||
typedef struct esp_ble_hidd_dev_s esp_ble_hidd_dev_t;
|
||||
// there can be only one BLE HID device
|
||||
static esp_ble_hidd_dev_t *s_dev = NULL;
|
||||
static SemaphoreHandle_t s_hidd_mutex = NULL;
|
||||
static bool s_gap_listener_registered = false;
|
||||
static void (*s_prev_reset_cb)(int reason) = NULL;
|
||||
static void (*s_prev_sync_cb)(void) = NULL;
|
||||
static struct ble_gap_event_listener nimble_gap_event_listener;
|
||||
static void nimble_host_synced(void);
|
||||
void nimble_host_reset(int reason);
|
||||
|
||||
static inline void lock_hidd(void)
|
||||
{
|
||||
if (s_hidd_mutex) {
|
||||
xSemaphoreTake(s_hidd_mutex, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void unlock_hidd(void)
|
||||
{
|
||||
if (s_hidd_mutex) {
|
||||
xSemaphoreGive(s_hidd_mutex);
|
||||
}
|
||||
}
|
||||
/** service index is used to identify the hid service instance
|
||||
of the registered characteristic.
|
||||
Assuming the first instance of the hid service is registered first.
|
||||
@@ -90,7 +112,10 @@ static int create_hid_db(int device_index)
|
||||
/* fill hid info */
|
||||
memcpy(&hparams.hid_info, hidInfo, sizeof hparams.hid_info);
|
||||
|
||||
/* fill report map */
|
||||
if (s_dev->devices[device_index].reports_map.len > REPORT_MAP_SIZE) {
|
||||
ESP_LOGE(TAG, "Report map too large (%d > %d); aborting", s_dev->devices[device_index].reports_map.len, REPORT_MAP_SIZE);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memcpy(&hparams.report_map, (uint8_t *)s_dev->devices[device_index].reports_map.data, s_dev->devices[device_index].reports_map.len);
|
||||
hparams.report_map_len = s_dev->devices[device_index].reports_map.len;
|
||||
hparams.external_rpt_ref = BLE_SVC_BAS_UUID16;
|
||||
@@ -102,6 +127,10 @@ static int create_hid_db(int device_index)
|
||||
for (uint8_t i = 0; i < s_dev->devices[device_index].reports_len; i++) {
|
||||
hidd_le_report_item_t *report = &s_dev->devices[device_index].reports[i];
|
||||
if (report->protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT) {
|
||||
if (report_mode_rpts >= MAX_REPORTS) {
|
||||
ESP_LOGE(TAG, "Too many report-mode reports (%d >= MAX_REPORTS); truncating", report_mode_rpts);
|
||||
break;
|
||||
}
|
||||
/* only consider report mode reports, all boot mode reports will be registered by default */
|
||||
if (report->report_type == ESP_HID_REPORT_TYPE_INPUT) {
|
||||
/* Input Report */
|
||||
@@ -186,6 +215,15 @@ static int nimble_hid_stop_gatts(esp_ble_hidd_dev_t *dev)
|
||||
{
|
||||
int rc = ESP_OK;
|
||||
|
||||
if (s_gap_listener_registered) {
|
||||
ble_gap_event_listener_unregister(&nimble_gap_event_listener);
|
||||
s_gap_listener_registered = false;
|
||||
}
|
||||
|
||||
if (dev && dev->connected) {
|
||||
ble_gap_terminate(dev->conn_id, BLE_ERR_REM_USER_CONN_TERM);
|
||||
}
|
||||
|
||||
/* stop gatt database */
|
||||
ble_gatts_stop();
|
||||
|
||||
@@ -249,6 +287,7 @@ static int ble_hid_init_config(esp_ble_hidd_dev_t *dev, const esp_hid_device_con
|
||||
dev->devices[d].reports = (hidd_le_report_item_t *)malloc(rmap->reports_len * sizeof(hidd_le_report_item_t));
|
||||
if (dev->devices[d].reports == NULL) {
|
||||
ESP_LOGE(TAG, "reports malloc(%d) failed", rmap->reports_len * sizeof(hidd_le_report_item_t));
|
||||
free(rmap->reports);
|
||||
free(rmap);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
@@ -287,22 +326,40 @@ static int ble_hid_free_config(esp_ble_hidd_dev_t *dev)
|
||||
static int nimble_hidd_dev_deinit(void *devp)
|
||||
{
|
||||
esp_ble_hidd_dev_t *dev = (esp_ble_hidd_dev_t *)devp;
|
||||
lock_hidd();
|
||||
if (!s_dev) {
|
||||
ESP_LOGE(TAG, "HID device profile already uninitialized");
|
||||
unlock_hidd();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
if (s_dev != dev) {
|
||||
ESP_LOGE(TAG, "Wrong HID device provided");
|
||||
unlock_hidd();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
s_dev = NULL;
|
||||
service_index = -1; // resetting the value
|
||||
|
||||
nimble_hid_stop_gatts(dev);
|
||||
s_dev = NULL;
|
||||
service_index = -1;
|
||||
|
||||
if (ble_hs_cfg.reset_cb == nimble_host_reset) {
|
||||
ble_hs_cfg.reset_cb = s_prev_reset_cb;
|
||||
}
|
||||
if (ble_hs_cfg.sync_cb == nimble_host_synced) {
|
||||
ble_hs_cfg.sync_cb = s_prev_sync_cb;
|
||||
}
|
||||
ble_hs_cfg.gatts_register_cb = NULL;
|
||||
unlock_hidd();
|
||||
|
||||
/* STOP_EVENT may be discarded because ble_hid_free_config() deletes the event
|
||||
* loop right after this call. This does not cause a crash or data corruption. */
|
||||
esp_event_post_to(dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_STOP_EVENT, NULL, 0, portMAX_DELAY);
|
||||
ble_hid_free_config(dev);
|
||||
free(dev);
|
||||
if (s_hidd_mutex) {
|
||||
vSemaphoreDelete(s_hidd_mutex);
|
||||
s_hidd_mutex = NULL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -316,20 +373,19 @@ static int nimble_hidd_dev_battery_set(void *devp, uint8_t level)
|
||||
{
|
||||
int rc;
|
||||
esp_ble_hidd_dev_t *dev = (esp_ble_hidd_dev_t *)devp;
|
||||
lock_hidd();
|
||||
if (!dev || s_dev != dev) {
|
||||
unlock_hidd();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (!dev->connected) {
|
||||
/* Return success if not yet connected */
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
rc = ble_svc_bas_battery_level_set(level);
|
||||
if (rc) {
|
||||
ESP_LOGE(TAG, "esp_ble_gatts_send_notify failed: %d", rc);
|
||||
if (rc != 0 && dev->connected) {
|
||||
ESP_LOGE(TAG, "ble_svc_bas_battery_level_set failed: %d", rc);
|
||||
unlock_hidd();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
unlock_hidd();
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -338,6 +394,9 @@ static int nimble_hidd_dev_battery_set(void *devp, uint8_t level)
|
||||
static hidd_le_report_item_t* find_report(uint8_t id, uint8_t type, uint8_t *mode)
|
||||
{
|
||||
hidd_le_report_item_t *rpt;
|
||||
if (!s_dev) {
|
||||
return NULL;
|
||||
}
|
||||
for (uint8_t d = 0; d < s_dev->devices_len; d++) {
|
||||
for (uint8_t i = 0; i < s_dev->devices[d].reports_len; i++) {
|
||||
rpt = &s_dev->devices[d].reports[i];
|
||||
@@ -348,15 +407,17 @@ static hidd_le_report_item_t* find_report(uint8_t id, uint8_t type, uint8_t *mod
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static hidd_le_report_item_t* find_report_by_usage_and_type(uint8_t usage, uint8_t type, uint8_t *mode)
|
||||
|
||||
static hidd_le_report_item_t* find_report_by_usage_and_type(uint8_t dev_index, uint8_t usage, uint8_t type, uint8_t *mode)
|
||||
{
|
||||
hidd_le_report_item_t *rpt;
|
||||
for (uint8_t d = 0; d < s_dev->devices_len; d++) {
|
||||
for (uint8_t i = 0; i < s_dev->devices[d].reports_len; i++) {
|
||||
rpt = &s_dev->devices[d].reports[i];
|
||||
if (rpt->usage == usage && rpt->report_type == type && (!mode || (mode && *mode == rpt->protocol_mode))) {
|
||||
return rpt;
|
||||
}
|
||||
if (!s_dev || dev_index >= s_dev->devices_len) {
|
||||
return NULL;
|
||||
}
|
||||
for (uint8_t i = 0; i < s_dev->devices[dev_index].reports_len; i++) {
|
||||
rpt = &s_dev->devices[dev_index].reports[i];
|
||||
if (rpt->usage == usage && rpt->report_type == type && (!mode || (mode && *mode == rpt->protocol_mode))) {
|
||||
return rpt;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
@@ -372,6 +433,11 @@ static int nimble_hidd_dev_input_set(void *devp, size_t index, size_t id, uint8_
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (index >= s_dev->devices_len) {
|
||||
ESP_LOGE(TAG, "%s invalid device index %zu (devices_len=%u)", __func__, index, s_dev->devices_len);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (!dev->connected) {
|
||||
ESP_LOGE(TAG, "%s Device Not Connected", __func__);
|
||||
return ESP_FAIL;
|
||||
@@ -418,6 +484,11 @@ static int nimble_hidd_dev_feature_set(void *devp, size_t index, size_t id, uint
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (index >= s_dev->devices_len) {
|
||||
ESP_LOGE(TAG, "%s invalid device index %zu (devices_len=%u)", __func__, index, s_dev->devices_len);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (!dev->connected) {
|
||||
ESP_LOGE(TAG, "%s Device Not Connected", __func__);
|
||||
return ESP_FAIL;
|
||||
@@ -476,63 +547,87 @@ static void ble_hidd_dev_free(void)
|
||||
free(s_dev);
|
||||
s_dev = NULL;
|
||||
}
|
||||
if (s_hidd_mutex) {
|
||||
vSemaphoreDelete(s_hidd_mutex);
|
||||
s_hidd_mutex = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int nimble_hid_gap_event(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
struct ble_gap_conn_desc desc;
|
||||
struct os_mbuf *om;
|
||||
uint8_t data;
|
||||
int rc;
|
||||
esp_ble_hidd_dev_t *dev;
|
||||
|
||||
lock_hidd();
|
||||
dev = s_dev;
|
||||
if (dev == NULL) {
|
||||
unlock_hidd();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* HOGP encryption is not enforced at the GATT level in this component.
|
||||
* Applications must configure encryption themselves, either by setting
|
||||
* BLE_GATT_CHR_F_READ_ENC/WRITE_ENC in ble_svc_hid.c or by configuring
|
||||
* ble_hs_cfg.sm_* security parameters. */
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
/* A new connection was established or a connection attempt failed. */
|
||||
ESP_LOGD(TAG, "connection %s; status=%d",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
event->connect.status);
|
||||
if (event->connect.status != 0) {
|
||||
unlock_hidd();
|
||||
return 0;
|
||||
}
|
||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
|
||||
/* save connection handle */
|
||||
s_dev->connected = true;
|
||||
s_dev->conn_id = event->connect.conn_handle;
|
||||
dev->connected = true;
|
||||
dev->conn_id = event->connect.conn_handle;
|
||||
esp_hidd_event_data_t cb_param = {
|
||||
.connect.dev = s_dev->dev,
|
||||
.connect.dev = dev->dev,
|
||||
.connect.status = event->connect.status
|
||||
};
|
||||
|
||||
/* reset the protocol mode value */
|
||||
data = ESP_HID_PROTOCOL_MODE_REPORT;
|
||||
om = ble_hs_mbuf_from_flat(&data, 1);
|
||||
if (om == NULL) {
|
||||
ESP_LOGD(TAG, "No memory to allocate mbuf");
|
||||
}
|
||||
/* NOTE : om is freed by stack */
|
||||
for (int i = 0; i < s_dev->devices_len; i++) {
|
||||
rc = ble_att_svr_write_local(s_dev->devices[i].hid_protocol_handle, om);
|
||||
for (int i = 0; i < dev->devices_len; i++) {
|
||||
struct os_mbuf *om = ble_hs_mbuf_from_flat(&data, 1);
|
||||
if (om == NULL) {
|
||||
ESP_LOGD(TAG, "No memory to allocate mbuf");
|
||||
break;
|
||||
}
|
||||
rc = ble_att_svr_write_local(dev->devices[i].hid_protocol_handle, om);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "Write on Protocol Mode Failed: %d", rc);
|
||||
}
|
||||
}
|
||||
unlock_hidd();
|
||||
|
||||
esp_event_post_to(s_dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_CONNECT_EVENT,
|
||||
esp_event_post_to(dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_CONNECT_EVENT,
|
||||
&cb_param, sizeof(esp_hidd_event_data_t), portMAX_DELAY);
|
||||
return 0;
|
||||
break;
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
ESP_LOGD(TAG, "disconnect; reason=%d", event->disconnect.reason);
|
||||
|
||||
if (s_dev->connected) {
|
||||
s_dev->connected = false;
|
||||
if (dev->connected) {
|
||||
dev->connected = false;
|
||||
esp_hidd_event_data_t cb_param = {0};
|
||||
cb_param.disconnect.dev = s_dev->dev;
|
||||
cb_param.disconnect.dev = dev->dev;
|
||||
cb_param.disconnect.reason = event->disconnect.reason;
|
||||
esp_event_post_to(s_dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_DISCONNECT_EVENT,
|
||||
unlock_hidd();
|
||||
esp_event_post_to(dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_DISCONNECT_EVENT,
|
||||
&cb_param, sizeof(esp_hidd_event_data_t), portMAX_DELAY);
|
||||
} else {
|
||||
unlock_hidd();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
unlock_hidd();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -554,8 +649,13 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
ctxt->svc.handle);
|
||||
uuid16 = ble_uuid_u16(ctxt->svc.svc_def->uuid);
|
||||
if (uuid16 == BLE_SVC_HID_UUID16) {
|
||||
++service_index;
|
||||
s_dev->devices[service_index].hid_svc = ctxt->svc.handle;
|
||||
if (service_index + 1 >= (int)s_dev->devices_len) {
|
||||
ESP_LOGE(TAG, "Too many HID services registered (%d >= devices_len %d); ignoring",
|
||||
service_index + 1, s_dev->devices_len);
|
||||
} else {
|
||||
++service_index;
|
||||
s_dev->devices[service_index].hid_svc = ctxt->svc.handle;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -566,6 +666,10 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
||||
ctxt->chr.def_handle,
|
||||
ctxt->chr.val_handle);
|
||||
|
||||
if (service_index < 0 || (uint8_t)service_index >= s_dev->devices_len) {
|
||||
break;
|
||||
}
|
||||
uuid16 = ble_uuid_u16(ctxt->chr.chr_def->uuid);
|
||||
if (uuid16 == BLE_SVC_HID_CHR_UUID16_HID_CTRL_PT) {
|
||||
/* assuming this characteristic is from the last registered hid service */
|
||||
@@ -577,7 +681,7 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
}
|
||||
if (uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_KBD_INP) {
|
||||
protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT;
|
||||
rpt = find_report_by_usage_and_type(ESP_HID_USAGE_KEYBOARD, ESP_HID_REPORT_TYPE_INPUT, &protocol_mode);
|
||||
rpt = find_report_by_usage_and_type(service_index, ESP_HID_USAGE_KEYBOARD, ESP_HID_REPORT_TYPE_INPUT, &protocol_mode);
|
||||
if (rpt == NULL) {
|
||||
ESP_LOGE(TAG, "Unknown boot kbd input report registration");
|
||||
return;
|
||||
@@ -586,7 +690,7 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
}
|
||||
if (uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_KBD_OUT) {
|
||||
protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT;
|
||||
rpt = find_report_by_usage_and_type(ESP_HID_USAGE_KEYBOARD, ESP_HID_REPORT_TYPE_OUTPUT, &protocol_mode);
|
||||
rpt = find_report_by_usage_and_type(service_index, ESP_HID_USAGE_KEYBOARD, ESP_HID_REPORT_TYPE_OUTPUT, &protocol_mode);
|
||||
if (rpt == NULL) {
|
||||
ESP_LOGE(TAG, "Unknown boot kbd output report registration");
|
||||
return;
|
||||
@@ -595,7 +699,7 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
}
|
||||
if (uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INP) {
|
||||
protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT;
|
||||
rpt = find_report_by_usage_and_type(ESP_HID_USAGE_MOUSE, ESP_HID_REPORT_TYPE_INPUT, &protocol_mode);
|
||||
rpt = find_report_by_usage_and_type(service_index, ESP_HID_USAGE_MOUSE, ESP_HID_REPORT_TYPE_INPUT, &protocol_mode);
|
||||
if (rpt == NULL) {
|
||||
ESP_LOGE(TAG, "Unknown boot mouse input report registration");
|
||||
return;
|
||||
@@ -616,6 +720,11 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
ble_hs_mbuf_to_flat(om, &report_info, sizeof report_info, NULL);
|
||||
report_type = (uint8_t)((report_info & 0xFF00) >> 8);
|
||||
report_id = report_info & 0x00FF;
|
||||
if (ctxt->dsc.dsc_def->arg == NULL) {
|
||||
ESP_LOGE(TAG, "Report Reference descriptor has NULL arg; skipping handle assignment");
|
||||
os_mbuf_free_chain(om);
|
||||
break;
|
||||
}
|
||||
report_handle = (*(uint16_t*)(ctxt->dsc.dsc_def->arg));
|
||||
protocol_mode = ESP_HID_PROTOCOL_MODE_REPORT;
|
||||
rpt = find_report(report_id, report_type, &protocol_mode);
|
||||
@@ -635,15 +744,24 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi
|
||||
|
||||
static void nimble_host_synced(void)
|
||||
{
|
||||
esp_event_post_to(s_dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_START_EVENT, NULL, 0, portMAX_DELAY);
|
||||
lock_hidd();
|
||||
if (s_prev_sync_cb && s_prev_sync_cb != nimble_host_synced) {
|
||||
s_prev_sync_cb();
|
||||
}
|
||||
if (s_dev && s_dev->event_loop_handle != NULL) {
|
||||
esp_event_post_to(s_dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_START_EVENT, NULL, 0, portMAX_DELAY);
|
||||
}
|
||||
unlock_hidd();
|
||||
}
|
||||
|
||||
void nimble_host_reset(int reason)
|
||||
{
|
||||
if (s_prev_reset_cb && s_prev_reset_cb != nimble_host_reset) {
|
||||
s_prev_reset_cb(reason);
|
||||
}
|
||||
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
|
||||
}
|
||||
|
||||
static struct ble_gap_event_listener nimble_gap_event_listener;
|
||||
esp_err_t esp_ble_hidd_dev_init(esp_hidd_dev_t *dev_p, const esp_hid_device_config_t *config, esp_event_handler_t callback)
|
||||
{
|
||||
int rc;
|
||||
@@ -658,6 +776,15 @@ esp_err_t esp_ble_hidd_dev_init(esp_hidd_dev_t *dev_p, const esp_hid_device_conf
|
||||
ESP_LOGE(TAG, "HID device could not be allocated");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (s_hidd_mutex == NULL) {
|
||||
s_hidd_mutex = xSemaphoreCreateMutex();
|
||||
if (s_hidd_mutex == NULL) {
|
||||
free(s_dev);
|
||||
s_dev = NULL;
|
||||
ESP_LOGE(TAG, "HID device mutex allocation failed");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the hid device target environment
|
||||
s_dev->control = ESP_HID_CONTROL_EXIT_SUSPEND;
|
||||
@@ -708,15 +835,26 @@ esp_err_t esp_ble_hidd_dev_init(esp_hidd_dev_t *dev_p, const esp_hid_device_conf
|
||||
}
|
||||
}
|
||||
|
||||
s_prev_reset_cb = ble_hs_cfg.reset_cb;
|
||||
s_prev_sync_cb = ble_hs_cfg.sync_cb;
|
||||
ble_hs_cfg.reset_cb = nimble_host_reset;
|
||||
ble_hs_cfg.sync_cb = nimble_host_synced;
|
||||
ble_hs_cfg.gatts_register_cb = nimble_gatt_svr_register_cb;
|
||||
rc = nimble_hid_start_gatts();
|
||||
if (rc != ESP_OK) {
|
||||
if (ble_hs_cfg.reset_cb == nimble_host_reset) {
|
||||
ble_hs_cfg.reset_cb = s_prev_reset_cb;
|
||||
}
|
||||
if (ble_hs_cfg.sync_cb == nimble_host_synced) {
|
||||
ble_hs_cfg.sync_cb = s_prev_sync_cb;
|
||||
}
|
||||
ble_hs_cfg.gatts_register_cb = NULL;
|
||||
ble_hidd_dev_free();
|
||||
return rc;
|
||||
}
|
||||
ble_gap_event_listener_register(&nimble_gap_event_listener,
|
||||
nimble_hid_gap_event, NULL);
|
||||
s_gap_listener_registered = true;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user