1
6
7
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
35
36
42
43
46
47
63
64
65
75
76
77
78
79
80
81
82
83
84
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
108
109
110
111
114
115
116
117
118
122
123
124
125
129
133
134
135
136
140
141
142
143
144
145
146
147
148
151
152
153
154
157
158
159
160
161
162
163
164
165
166
167
168
169
170
174
175
176
180
181
185
186
190
191
192
193
194
195
196
197
201
202
203
204
205
206
207
219
220
228
229
241
242
254
255
267
268
284
285
286
298
299
315
316
332
333
349
350
366
367
379
380
390
391
401
402
412
413
423
424
434
435
445
446
456
457
467
468
478
479
480
481
482
483
484
487
488
489
490
491
495
496
497
498
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
527
528
529
530
531
532
533
534
537
538
541
547
548
554
555
561
562
568
569
580
581
593
594
605
606
617
618
629
630
635
636
637
638
639
640
641
649
650
671
672
673
674
675
676
677
680
685
686
687
688
694
695
696
701
702
705
706
707
708
709
710
711
715
716
717
722
723
724
729
730
731
732
733
734
735
736
737
741
745
746
747
748
749
750
753
754
755
756
757
765
766
771
772
773
774
775
776
777
778
779
785
786
787
788
789
804
805
820
821
825
826
846
847
848
874
875
876
877
878
879
880
881
887
888
889
890
891
892
893
894
895
901
902
903
904
905
909
910
930
931
932
953
/* ... */
#include "sys/queue.h"
#include "esp_private/esp_hidh_private.h"
#include "bt_hidh.h"
#include "ble_hidh.h"
#include <string.h>
#include <stdbool.h>
#include "esp_event.h"
#include "esp_check.h"8 includes
#if CONFIG_BT_HID_HOST_ENABLED
#include "esp_hidh_api.h"
#endif
ESP_EVENT_DEFINE_BASE(ESP_HIDH_EVENTS);
#define ESP_HIDH_DELAY_FREE_TO 100000
static const char *TAG = "ESP_HIDH";
static TAILQ_HEAD(esp_hidh_dev_head_s, esp_hidh_dev_s) s_esp_hidh_devices;
static SemaphoreHandle_t s_esp_hidh_devices_semaphore = NULL;
static esp_event_loop_handle_t s_esp_hidh_event_loop_h;
static esp_event_handler_t s_event_callback;
static inline void lock_devices(void)
{
if (s_esp_hidh_devices_semaphore != NULL) {
xSemaphoreTake(s_esp_hidh_devices_semaphore, portMAX_DELAY);
}{...}
}{ ... }
static inline void unlock_devices(void)
{
if (s_esp_hidh_devices_semaphore != NULL) {
xSemaphoreGive(s_esp_hidh_devices_semaphore);
}{...}
}{ ... }
/* ... */
bool esp_hidh_dev_exists(esp_hidh_dev_t *dev)
{
if (dev == NULL) {
return false;
}{...}
esp_hidh_dev_t * d = NULL;
lock_devices();
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
if (d == dev) {
unlock_devices();
return true;
}{...}
}{...}
unlock_devices();
return false;
}{ ... }
static void esp_hidh_event_handler_wrapper(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
esp_hidh_preprocess_event_handler(event_handler_arg, event_base, event_id, event_data);
if (s_event_callback) {
s_event_callback(event_handler_arg, event_base, event_id, event_data);
}{...}
esp_hidh_postprocess_event_handler(event_handler_arg, event_base, event_id, event_data);
}{ ... }
esp_err_t esp_hidh_init(const esp_hidh_config_t *config)
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "Config is NULL");
ESP_RETURN_ON_FALSE(!s_esp_hidh_devices_semaphore, ESP_ERR_INVALID_STATE, TAG, "Already initialized");
TAILQ_INIT(&s_esp_hidh_devices);
s_event_callback = config->callback;
const esp_event_loop_args_t event_task_args = {
.queue_size = 5,
.task_name = "esp_hidh_events",
.task_priority = uxTaskPriorityGet(NULL),
.task_stack_size = config->event_stack_size > 0 ? config->event_stack_size : 4096,
.task_core_id = tskNO_AFFINITY
}{...};
esp_event_loop_create(&event_task_args, &s_esp_hidh_event_loop_h);
s_esp_hidh_devices_semaphore = xSemaphoreCreateMutex();
ESP_GOTO_ON_FALSE(s_esp_hidh_devices_semaphore && s_esp_hidh_event_loop_h,
ESP_ERR_NO_MEM, alloc_fail, TAG, "Allocation failed");
ESP_GOTO_ON_ERROR(
esp_event_handler_register_with(s_esp_hidh_event_loop_h, ESP_HIDH_EVENTS, ESP_EVENT_ANY_ID,
esp_hidh_event_handler_wrapper, config->callback_arg),
alloc_fail, TAG, "event_loop register failed!");
#if CONFIG_BT_HID_HOST_ENABLED
ESP_GOTO_ON_ERROR(
esp_bt_hidh_init(config),
alloc_fail, TAG, "BT HIDH init failed");/* ... */
#endif
#if CONFIG_GATTC_ENABLE || CONFIG_BT_NIMBLE_ENABLED
ESP_GOTO_ON_ERROR(
esp_ble_hidh_init(config),
bt_fail, TAG, "BLE HIDH init failed");/* ... */
#endif
return ret;
#if CONFIG_GATTC_ENABLE || CONFIG_BT_NIMBLE_ENABLED
bt_fail:
#if CONFIG_BT_HID_HOST_ENABLED
esp_bt_hidh_deinit();
#endif /* ... */
#endif
alloc_fail:
if (s_esp_hidh_devices_semaphore) {
vSemaphoreDelete(s_esp_hidh_devices_semaphore);
s_esp_hidh_devices_semaphore = NULL;
}{...}
if (s_esp_hidh_event_loop_h) {
esp_event_loop_delete(s_esp_hidh_event_loop_h);
s_esp_hidh_event_loop_h = NULL;
}{...}
return ret;
}{ ... }
esp_event_loop_handle_t esp_hidh_get_event_loop(void)
{
return s_esp_hidh_event_loop_h;
}{ ... }
esp_err_t esp_hidh_deinit(void)
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE(s_esp_hidh_devices_semaphore, ESP_ERR_INVALID_STATE, TAG, "Already deinitialized");
ESP_RETURN_ON_FALSE(TAILQ_EMPTY(&s_esp_hidh_devices), ESP_ERR_INVALID_STATE, TAG, "Please disconnect all devices first!");
#if CONFIG_BT_HID_HOST_ENABLED
ESP_RETURN_ON_ERROR(
esp_bt_hidh_deinit(),
TAG, "BT HIDH deinit failed");/* ... */
#endif
#if CONFIG_GATTC_ENABLE || CONFIG_BT_NIMBLE_ENABLED
ESP_RETURN_ON_ERROR(
esp_ble_hidh_deinit(),
TAG, "BLE HIDH deinit failed");/* ... */
#endif
TAILQ_INIT(&s_esp_hidh_devices);
vSemaphoreDelete(s_esp_hidh_devices_semaphore);
s_esp_hidh_devices_semaphore = NULL;
esp_event_loop_delete(s_esp_hidh_event_loop_h);
s_esp_hidh_event_loop_h = NULL;
return ret;
}{ ... }
#if CONFIG_BLUEDROID_ENABLED
esp_hidh_dev_t *esp_hidh_dev_open(esp_bd_addr_t bda, esp_hid_transport_t transport, uint8_t remote_addr_type)
{
if (esp_hidh_dev_get_by_bda(bda) != NULL) {
ESP_LOGE(TAG, "Already Connected");
return NULL;
}{...}
esp_hidh_dev_t *dev = NULL;
#if CONFIG_GATTC_ENABLE
if (transport == ESP_HID_TRANSPORT_BLE) {
dev = esp_ble_hidh_dev_open(bda, (esp_ble_addr_type_t)remote_addr_type);
}{...}
#endif/* ... */
#if CONFIG_BT_NIMBLE_ENABLED
if (transport == ESP_HID_TRANSPORT_BLE) {
dev = esp_ble_hidh_dev_open(bda, remote_addr_type);
}{...}
#endif/* ... */
#if CONFIG_BT_HID_HOST_ENABLED
if (transport == ESP_HID_TRANSPORT_BT) {
dev = esp_bt_hidh_dev_open(bda);
}{...}
#endif/* ... */
return dev;
}{ ... }
/* ... */#endif
#if CONFIG_BT_NIMBLE_ENABLED
esp_hidh_dev_t *esp_hidh_dev_open(uint8_t *bda, esp_hid_transport_t transport, uint8_t remote_addr_type)
{
if (esp_hidh_dev_get_by_bda(bda) != NULL) {
ESP_LOGE(TAG, "Already Connected");
return NULL;
}{...}
esp_hidh_dev_t *dev = NULL;
dev = esp_ble_hidh_dev_open(bda, remote_addr_type);
return dev;
}{...}
/* ... */#endif
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);
ret = dev->close(dev);
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
void esp_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp)
{
if (esp_hidh_dev_exists(dev)) {
esp_hidh_dev_lock(dev);
dev->dump(dev, fp);
esp_hidh_dev_unlock(dev);
}{...}
}{ ... }
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);
ret = dev->report_write(dev, map_index, report_id, ESP_HID_REPORT_TYPE_OUTPUT, value, value_len);
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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);
ret = dev->report_write(dev, map_index, report_id, ESP_HID_REPORT_TYPE_FEATURE, value, value_len);
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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);
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 {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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 (dev->set_report) {
ret = dev->set_report(dev, map_index, report_id, report_type, data, length);
}{...} else {
ret = ESP_ERR_NOT_SUPPORTED;
}{...}
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
esp_err_t esp_hidh_dev_get_report(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type,
size_t max_len)
{
esp_err_t ret = ESP_OK;
if (esp_hidh_dev_exists(dev)) {
esp_hidh_dev_lock(dev);
ret = dev->report_read(dev, map_index, report_id, report_type, max_len, NULL, NULL);
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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 (dev->get_idle) {
ret = dev->get_idle(dev);
}{...} else {
ret = ESP_ERR_NOT_SUPPORTED;
}{...}
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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 (dev->set_idle) {
ret = dev->set_idle(dev, idle_time);
}{...} else {
ret = ESP_ERR_NOT_SUPPORTED;
}{...}
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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 (dev->get_protocol) {
ret = dev->get_protocol(dev);
}{...} else {
ret = ESP_ERR_NOT_SUPPORTED;
}{...}
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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 (dev->set_protocol) {
ret = dev->set_protocol(dev, protocol_mode);
}{...} else {
ret = ESP_ERR_NOT_SUPPORTED;
}{...}
esp_hidh_dev_unlock(dev);
}{...} else {
ret = ESP_FAIL;
}{...}
return ret;
}{ ... }
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);
ret = dev->addr.bda;
esp_hidh_dev_unlock(dev);
}{...}
#endif/* ... */
return ret;
}{ ... }
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);
ret = dev->transport;
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = &dev->config;
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->config.device_name ? dev->config.device_name : "";
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->config.manufacturer_name ? dev->config.manufacturer_name : "";
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->config.serial_number ? dev->config.serial_number : "";
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->config.vendor_id;
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->config.product_id;
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->config.version;
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
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);
ret = dev->usage;
esp_hidh_dev_unlock(dev);
}{...}
return ret;
}{ ... }
esp_err_t esp_hidh_dev_reports_get(esp_hidh_dev_t *dev, size_t *num_reports, esp_hid_report_item_t **reports)
{
esp_err_t ret = 0;
esp_hid_report_item_t *r = NULL;
if (!esp_hidh_dev_exists(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) {
ret = ESP_FAIL;
break;
}{...}
esp_hidh_dev_report_t *dr = dev->reports;
for (uint8_t i = 0; i < dev->reports_len; i++) {
if (dr == NULL) {
free(r);
ret = ESP_FAIL;
goto error_;
}{...}
r[i].map_index = dr->map_index;
r[i].protocol_mode = dr->protocol_mode;
r[i].usage = dr->usage;
r[i].report_id = dr->report_id;
r[i].report_type = dr->report_type;
r[i].value_len = dr->value_len;
dr = dr->next;
}{...}
*reports = r;
*num_reports = dev->reports_len;
}{...} while (0);
error_:;
esp_hidh_dev_unlock(dev);
return ret;
}{ ... }
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)) {
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);
return ESP_OK;
}{ ... }
/* ... */
/* ... */
inline void esp_hidh_dev_lock(esp_hidh_dev_t *dev)
{
if (dev && dev->mutex != NULL) {
xSemaphoreTake(dev->mutex, portMAX_DELAY);
}{...}
}{ ... }
inline void esp_hidh_dev_unlock(esp_hidh_dev_t *dev)
{
if (dev && dev->mutex != NULL) {
xSemaphoreGive(dev->mutex);
}{...}
}{ ... }
inline void esp_hidh_dev_wait(esp_hidh_dev_t *dev)
{
if (dev && dev->semaphore != NULL) {
xSemaphoreTake(dev->semaphore, portMAX_DELAY);
}{...}
}{ ... }
inline void esp_hidh_dev_send(esp_hidh_dev_t *dev)
{
if (dev && dev->semaphore != NULL) {
xSemaphoreGive(dev->semaphore);
}{...}
}{ ... }
esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_handle(esp_hidh_dev_t *dev, uint16_t handle)
{
esp_hidh_dev_report_t *r = dev->reports;
while (r) {
if (r->handle == handle) {
return r;
}{...}
r = r->next;
}{...}
return NULL;
}{ ... }
esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_id_type_proto(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t protocol_mode)
{
esp_hidh_dev_report_t *r = dev->reports;
while (r) {
if (r->map_index == map_index && r->report_type == report_type && r->report_id == report_id &&
r->protocol_mode == protocol_mode) {
return r;
}{...}
r = r->next;
}{...}
return NULL;
}{ ... }
esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_id_and_type(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type)
{
esp_hidh_dev_report_t *r = dev->reports;
while (r) {
if (r->map_index == map_index && r->report_id == report_id && r->report_type == report_type && r->protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT) {
return r;
}{...}
r = r->next;
}{...}
return NULL;
}{ ... }
esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_id_and_proto(esp_hidh_dev_t *dev, size_t report_id, int protocol_mode)
{
esp_hidh_dev_report_t *r = dev->reports;
while (r) {
if (r->report_id == report_id && (r->report_type & ESP_HID_REPORT_TYPE_INPUT) && r->protocol_mode == protocol_mode) {
return r;
}{...}
r = r->next;
}{...}
return NULL;
}{ ... }
esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_len_and_proto(esp_hidh_dev_t *dev, size_t len, int protocol_mode)
{
esp_hidh_dev_report_t *r = dev->reports;
while (r) {
if (r->value_len == len && (r->report_type & ESP_HID_REPORT_TYPE_INPUT) && r->protocol_mode == protocol_mode) {
return r;
}{...}
r = r->next;
}{...}
return NULL;
}{ ... }
/* ... */
esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_proto_and_data(esp_hidh_dev_t *dev, int protocol_mode,
size_t len, const uint8_t *data, bool *has_report_id)
{
esp_hidh_dev_report_t *r = dev->reports;
*has_report_id = false;
while (r) {
if (r->value_len == len && r->report_id == 0 && (r->report_type & ESP_HID_REPORT_TYPE_INPUT) &&
r->protocol_mode == protocol_mode) {
*has_report_id = false;
break;
}{...}
r = r->next;
}{...}
if (r == NULL) {
if (*data == 0) {
ESP_LOGE(TAG, "data not include report id!");
*has_report_id = false;
return NULL;
}{...}
r = dev->reports;
while (r) {
/* ... */
if (r->value_len <= len - 1 && r->report_id == *data && (r->report_type & ESP_HID_REPORT_TYPE_INPUT) &&
r->protocol_mode == protocol_mode) {
*has_report_id = true;
break;
}{...}
r = r->next;
}{...}
}{...}
return r;
}{ ... }
static void esp_hidh_dev_resources_free(esp_hidh_dev_t *dev)
{
esp_hidh_dev_lock(dev);
if (dev->semaphore) {
vSemaphoreDelete(dev->semaphore);
}{...}
if (dev->trans_timer) {
esp_timer_stop(dev->trans_timer);
esp_timer_delete(dev->trans_timer);
dev->trans_timer = NULL;
}{...}
free((void *)dev->config.device_name);
free((void *)dev->config.manufacturer_name);
free((void *)dev->config.serial_number);
for (uint8_t d = 0; d < dev->config.report_maps_len; d++) {
if (dev->config.report_maps[d].data) {
free((void *)dev->config.report_maps[d].data);
}{...}
}{...}
free((void *)dev->config.report_maps);
esp_hidh_dev_report_t *r;
while (dev->reports) {
r = dev->reports;
dev->reports = dev->reports->next;
free(r);
}{...}
esp_hidh_dev_unlock(dev);
if (dev->mutex) {
vSemaphoreDelete(dev->mutex);
}{...}
free(dev);
}{ ... }
esp_hidh_dev_t *esp_hidh_dev_malloc()
{
esp_hidh_dev_t *dev = (esp_hidh_dev_t *)calloc(1, sizeof(esp_hidh_dev_t));
if (dev == NULL) {
ESP_LOGE(TAG, "malloc esp_hidh_dev_t failed");
return NULL;
}{...}
dev->semaphore = xSemaphoreCreateBinary();
if (dev->semaphore == NULL) {
ESP_LOGE(TAG, "malloc semaphore failed");
esp_hidh_dev_resources_free(dev);
return NULL;
}{...}
dev->mutex = xSemaphoreCreateMutex();
if (dev->mutex == NULL) {
ESP_LOGE(TAG, "malloc mutex failed");
esp_hidh_dev_resources_free(dev);
return NULL;
}{...}
lock_devices();
TAILQ_INSERT_TAIL(&s_esp_hidh_devices, dev, devices);
unlock_devices();
return dev;
}{ ... }
/* ... */
esp_err_t esp_hidh_dev_free(esp_hidh_dev_t *dev)
{
return ESP_OK;
}{ ... }
esp_err_t esp_hidh_dev_free_inner(esp_hidh_dev_t *dev)
{
esp_err_t ret = ESP_FAIL;
if (dev == NULL) {
return ret;
}{...}
esp_hidh_dev_t *d = NULL;
esp_hidh_dev_t *next = NULL;
lock_devices();
TAILQ_FOREACH_SAFE(d, &s_esp_hidh_devices, devices, next) {
if (d == dev) {
TAILQ_REMOVE(&s_esp_hidh_devices, d, devices);
esp_hidh_dev_resources_free(d);
ret = ESP_OK;
break;
}{...}
}{...}
unlock_devices();
if (ret != ESP_OK) {
ESP_LOGW(TAG, "device not found");
}{...} else {
ESP_LOGD(TAG, "device removed");
}{...}
return ret;
}{ ... }
#if CONFIG_BLUEDROID_ENABLED
esp_hidh_dev_t *esp_hidh_dev_get_by_bda(esp_bd_addr_t bda)
{
esp_hidh_dev_t * d = NULL;
lock_devices();
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
if (memcmp(bda, d->addr.bda, sizeof(esp_bd_addr_t)) == 0) {
unlock_devices();
return d;
}{...}
}{...}
unlock_devices();
return NULL;
}{ ... }
esp_hidh_dev_t *esp_hidh_dev_get_by_handle(uint8_t handle)
{
#if CONFIG_BT_HID_HOST_ENABLED
esp_hidh_dev_t * d = NULL;
lock_devices();
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
if (d->transport == ESP_HID_TRANSPORT_BT && d->bt.handle == handle) {
unlock_devices();
return d;
}{...}
}{...}
unlock_devices();/* ... */
#endif
return NULL;
}{ ... }
esp_hidh_dev_t *esp_hidh_dev_get_by_conn_id(uint16_t conn_id)
{
#if CONFIG_GATTC_ENABLE
esp_hidh_dev_t * d = NULL;
lock_devices();
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
if (d->transport == ESP_HID_TRANSPORT_BLE && d->ble.conn_id == conn_id) {
unlock_devices();
return d;
}{...}
}{...}
unlock_devices();/* ... */
#endif
return NULL;
}{ ... }
/* ... */
void esp_hidh_preprocess_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
esp_hidh_event_t event = (esp_hidh_event_t)event_id;
esp_hidh_event_data_t *param = (esp_hidh_event_data_t *)event_data;
switch (event) {
case ESP_HIDH_INPUT_EVENT:
if (param->input.length && param->input.data) {
param->input.data = (uint8_t *)param + sizeof(esp_hidh_event_data_t);
}{...}
break;...
case ESP_HIDH_FEATURE_EVENT:
if (param->feature.length && param->feature.data) {
param->feature.data = (uint8_t *)param + sizeof(esp_hidh_event_data_t);
}{...}
break;...
default:
break;...
}{...}
}{ ... }
void esp_hidh_postprocess_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
esp_hidh_event_t event = (esp_hidh_event_t)event_id;
esp_hidh_event_data_t *param = (esp_hidh_event_data_t *)event_data;
switch (event) {
case ESP_HIDH_OPEN_EVENT:
if (param->open.status != ESP_OK) {
esp_hidh_dev_t *dev = param->open.dev;
if (dev) {
#if CONFIG_BT_HID_HOST_ENABLED
if (esp_hidh_dev_transport_get(dev) == ESP_HID_TRANSPORT_BT && param->open.status == ESP_HIDH_ERR_SDP) {
break;
}{...}
#endif/* ... */
esp_hidh_dev_free_inner(dev);
}{...}
}{...}
break;...
case ESP_HIDH_CLOSE_EVENT:
esp_hidh_dev_free_inner(param->close.dev);
break;...
default:
break;...
}{...}
}{ ... }
#endif/* ... */
#if CONFIG_BT_NIMBLE_ENABLED
esp_hidh_dev_t *esp_hidh_dev_get_by_bda(uint8_t *bda)
{
esp_hidh_dev_t * d = NULL;
lock_devices();
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
if (memcmp(bda, d->addr.bda, sizeof(uint8_t) * 6) == 0) {
unlock_devices();
return d;
}{...}
}{...}
unlock_devices();
return NULL;
}{...}
esp_hidh_dev_t *esp_hidh_dev_get_by_conn_id(uint16_t conn_id)
{
esp_hidh_dev_t * d = NULL;
lock_devices();
TAILQ_FOREACH(d, &s_esp_hidh_devices, devices) {
if (d->ble.conn_id == conn_id) {
unlock_devices();
return d;
}{...}
}{...}
unlock_devices();
return NULL;
}{...}
/* ... */
void esp_hidh_preprocess_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
esp_hidh_event_t event = (esp_hidh_event_t)event_id;
esp_hidh_event_data_t *param = (esp_hidh_event_data_t *)event_data;
switch (event) {
case ESP_HIDH_INPUT_EVENT:
if (param->input.length && param->input.data) {
param->input.data = (uint8_t *)param + sizeof(esp_hidh_event_data_t);
}{...}
break;...
case ESP_HIDH_FEATURE_EVENT:
if (param->feature.length && param->feature.data) {
param->feature.data = (uint8_t *)param + sizeof(esp_hidh_event_data_t);
}{...}
break;...
default:
break;...
}{...}
}{...}
void esp_hidh_postprocess_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
esp_hidh_event_t event = (esp_hidh_event_t)event_id;
esp_hidh_event_data_t *param = (esp_hidh_event_data_t *)event_data;
switch (event) {
case ESP_HIDH_OPEN_EVENT:
if (param->open.status != ESP_OK) {
esp_hidh_dev_t *dev = param->open.dev;
if (dev) {
esp_hidh_dev_free_inner(dev);
}{...}
}{...}
break;...
case ESP_HIDH_CLOSE_EVENT:
esp_hidh_dev_free_inner(param->close.dev);
break;...
default:
break;...
}{...}
}{...}
/* ... */#endif