Skip to content

Commit a3ffc03

Browse files
committed
feat(esp_hid): Enahcne esp_hid to support multiple BLE connections
1. Add neccessary data structures to manage multiple connections in the HID layer of BLE protocol. 2. Add interfaces to allow users to select a connection out of multiple connections or boradcast to all connections. 3. This change keep the single connection behavior the same.
1 parent 80ed209 commit a3ffc03

File tree

6 files changed

+503
-72
lines changed

6 files changed

+503
-72
lines changed

components/esp_hid/include/esp_hidd.h

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

@@ -221,6 +213,45 @@ esp_err_t esp_hidd_dev_event_handler_register(esp_hidd_dev_t *dev, esp_event_han
221213
*/
222214
esp_err_t esp_hidd_dev_event_handler_unregister(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event);
223215

216+
/**
217+
* @brief Connection information structure for querying connections
218+
*/
219+
typedef struct {
220+
uint16_t conn_id; /*!< Connection ID */
221+
uint8_t remote_bda[6]; /*!< Remote device address */
222+
} esp_hidd_conn_info_t;
223+
224+
/**
225+
* @brief Set the active connection for unicast mode
226+
* @param dev : pointer to the device
227+
* @param conn_id : connection ID to set as active (sends to this connection only)
228+
*
229+
* @return: ESP_OK on success, ESP_ERR_NOT_FOUND if connection not found
230+
* @note: This disables broadcast mode automatically
231+
*/
232+
esp_err_t esp_hidd_dev_set_active_conn(esp_hidd_dev_t *dev, uint16_t conn_id);
233+
234+
/**
235+
* @brief Query all active connections
236+
* @param dev : pointer to the device
237+
* @param conn_list : pointer to array to store connection info
238+
* @param max_count : maximum number of connections that can be stored
239+
* @param[out] count : actual number of connections returned
240+
*
241+
* @return: ESP_OK on success
242+
*/
243+
esp_err_t esp_hidd_dev_get_connections(esp_hidd_dev_t *dev, esp_hidd_conn_info_t *conn_list, size_t max_count, size_t *count);
244+
245+
/**
246+
* @brief Enable or disable broadcast mode
247+
* @param dev : pointer to the device
248+
* @param enable : true to broadcast to all connections, false for unicast to active connection
249+
*
250+
* @return: ESP_OK on success
251+
* @note: In broadcast mode, all connected devices receive the events
252+
*/
253+
esp_err_t esp_hidd_dev_set_broadcast_mode(esp_hidd_dev_t *dev, bool enable);
254+
224255
#ifdef __cplusplus
225256
}
226257
#endif

components/esp_hid/private/ble_hidd.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -18,6 +18,11 @@ extern "C" {
1818

1919
esp_err_t esp_ble_hidd_dev_init(esp_hidd_dev_t *dev, const esp_hid_device_config_t *config, esp_event_handler_t callback);
2020

21+
// Multi-connection management functions
22+
esp_err_t esp_ble_hidd_dev_set_active_conn(void *devp, uint16_t conn_id);
23+
esp_err_t esp_ble_hidd_dev_get_connections(void *devp, esp_hidd_conn_info_t *conn_list, size_t max_count, size_t *count);
24+
esp_err_t esp_ble_hidd_dev_set_broadcast_mode(void *devp, bool enable);
25+
2126
#endif /* CONFIG_GATTS_ENABLE */
2227

2328
#ifdef __cplusplus

0 commit comments

Comments
 (0)