Select one of the symbols to view example projects that use it.
 
Outline
#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"
#include "esp_hidh_api.h"
ESP_HIDH_EVENTS
#define ESP_HIDH_DELAY_FREE_TO
TAG
s_esp_hidh_devices
s_esp_hidh_devices_semaphore
s_esp_hidh_event_loop_h
s_event_callback
lock_devices()
unlock_devices()
esp_hidh_dev_exists(esp_hidh_dev_t *)
esp_hidh_event_handler_wrapper(void *, esp_event_base_t, int32_t, void *)
esp_hidh_init(const esp_hidh_config_t *)
esp_hidh_get_event_loop()
esp_hidh_deinit()
esp_hidh_dev_open(uint8_t *, esp_hid_transport_t, uint8_t)
esp_hidh_dev_close(esp_hidh_dev_t *)
esp_hidh_dev_dump(esp_hidh_dev_t *, FILE *)
esp_hidh_dev_output_set(esp_hidh_dev_t *, size_t, size_t, uint8_t *, size_t)
esp_hidh_dev_feature_set(esp_hidh_dev_t *, size_t, size_t, uint8_t *, size_t)
esp_hidh_dev_feature_get(esp_hidh_dev_t *, size_t, size_t, size_t, uint8_t *, size_t *)
esp_hidh_dev_set_report(esp_hidh_dev_t *, size_t, size_t, int, uint8_t *, size_t)
esp_hidh_dev_get_report(esp_hidh_dev_t *, size_t, size_t, int, size_t)
esp_hidh_dev_get_idle(esp_hidh_dev_t *)
esp_hidh_dev_set_idle(esp_hidh_dev_t *, uint8_t)
esp_hidh_dev_get_protocol(esp_hidh_dev_t *)
esp_hidh_dev_set_protocol(esp_hidh_dev_t *, uint8_t)
esp_hidh_dev_bda_get(esp_hidh_dev_t *)
esp_hidh_dev_transport_get(esp_hidh_dev_t *)
esp_hidh_dev_config_get(esp_hidh_dev_t *)
esp_hidh_dev_name_get(esp_hidh_dev_t *)
esp_hidh_dev_manufacturer_get(esp_hidh_dev_t *)
esp_hidh_dev_serial_get(esp_hidh_dev_t *)
esp_hidh_dev_vendor_id_get(esp_hidh_dev_t *)
esp_hidh_dev_product_id_get(esp_hidh_dev_t *)
esp_hidh_dev_version_get(esp_hidh_dev_t *)
esp_hidh_dev_usage_get(esp_hidh_dev_t *)
esp_hidh_dev_reports_get(esp_hidh_dev_t *, size_t *, esp_hid_report_item_t **)
esp_hidh_dev_report_maps_get(esp_hidh_dev_t *, size_t *, esp_hid_raw_report_map_t **)
esp_hidh_dev_lock(esp_hidh_dev_t *)
esp_hidh_dev_unlock(esp_hidh_dev_t *)
esp_hidh_dev_wait(esp_hidh_dev_t *)
esp_hidh_dev_send(esp_hidh_dev_t *)
esp_hidh_dev_get_report_by_handle(esp_hidh_dev_t *, uint16_t)
esp_hidh_dev_get_report_by_id_type_proto(esp_hidh_dev_t *, size_t, size_t, int, uint8_t)
esp_hidh_dev_get_report_by_id_and_type(esp_hidh_dev_t *, size_t, size_t, int)
esp_hidh_dev_get_input_report_by_id_and_proto(esp_hidh_dev_t *, size_t, int)
esp_hidh_dev_get_input_report_by_len_and_proto(esp_hidh_dev_t *, size_t, int)
esp_hidh_dev_get_input_report_by_proto_and_data(esp_hidh_dev_t *, int, size_t, const uint8_t *, bool *)
esp_hidh_dev_resources_free(esp_hidh_dev_t *)
esp_hidh_dev_malloc()
esp_hidh_dev_free(esp_hidh_dev_t *)
esp_hidh_dev_free_inner(esp_hidh_dev_t *)
esp_hidh_dev_get_by_bda(uint8_t *)
esp_hidh_dev_get_by_handle(uint8_t)
esp_hidh_dev_get_by_conn_id(uint16_t)
esp_hidh_preprocess_event_handler(void *, esp_event_base_t, int32_t, void *)
esp_hidh_postprocess_event_handler(void *, esp_event_base_t, int32_t, void *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_hid/src/esp_hidh.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #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 /* CONFIG_BT_HID_HOST_ENABLED */ ESP_EVENT_DEFINE_BASE(ESP_HIDH_EVENTS); #define ESP_HIDH_DELAY_FREE_TO 100000 // us 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); }{...} }{ ... } /* * Public Functions * *//* ... */ 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!"); // BT and BLE are natively supported by esp_hid. // For USB support you must initialize it manually after esp_hidh_init() #if CONFIG_BT_HID_HOST_ENABLED ESP_GOTO_ON_ERROR( esp_bt_hidh_init(config), alloc_fail, TAG, "BT HIDH init failed");/* ... */ #endif /* CONFIG_BT_HID_HOST_ENABLED */ #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 /* CONFIG_GATTC_ENABLE */ return ret; #if CONFIG_GATTC_ENABLE || CONFIG_BT_NIMBLE_ENABLED bt_fail: #if CONFIG_BT_HID_HOST_ENABLED esp_bt_hidh_deinit(); #endif /* CONFIG_BT_HID_HOST_ENABLED *//* ... */ #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 /* CONFIG_BT_HID_HOST_ENABLED */ #if CONFIG_GATTC_ENABLE || CONFIG_BT_NIMBLE_ENABLED ESP_RETURN_ON_ERROR( esp_ble_hidh_deinit(), TAG, "BLE HIDH deinit failed");/* ... */ #endif /* CONFIG_GATTC_ENABLE */ 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/* ... */ /* CONFIG_GATTC_ENABLE */ #if CONFIG_BT_NIMBLE_ENABLED if (transport == ESP_HID_TRANSPORT_BLE) { dev = esp_ble_hidh_dev_open(bda, remote_addr_type); }{...} #endif/* ... */ /* CONFIG_BT_NIMBLE_ENABLED */ #if CONFIG_BT_HID_HOST_ENABLED if (transport == ESP_HID_TRANSPORT_BT) { dev = esp_bt_hidh_dev_open(bda); }{...} #endif/* ... */ /* CONFIG_BT_HID_HOST_ENABLED */ return dev; }{ ... } /* ... */#endif /* CONFIG_BLUEDROID_ENABLED */ #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 /* CONFIG_BT_NIMBLE_ENABLED */ 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/* ... */ /* CONFIG_BLUEDROID_ENABLED || CONFIG_BT_NIMBLE_ENABLED*/ 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) { // error 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; }{ ... } /* * Private Functions * *//* ... */ /** * `lock_devices()` only protect the devices list, this mutex protect the single device instance. *//* ... */ 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; }{ ... } /** * If no Report ID item tags are present in the Report descriptor, it * can be assumed that only one Input, Output, and Feature report structure exists * and together they represent all of the device’s data. *//* ... */ 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; // first, assume data not include report id 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; }{...} // indicate data include report id 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) { /** * @note For some HID device, the input report length may exceed the length which is declared in HID * descriptor, like Logitech K380 keyboard. So loose the check condition from `r->value_len == len - 1` to * `r->value_len <= len - 1`. *//* ... */ 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++) { /* data of report map maybe is NULL */ 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; }{ ... } /** * The `dev` is allocated by the internal function, and it should also be freed by the internal function. So, when the * user call this function, it will do nothing. *//* ... */ 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 /* CONFIG_BT_HID_HOST_ENABLED */ 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 /* CONFIG_GATTC_ENABLE */ return NULL; }{ ... } /** * The deep copy data append the end of the esp_hidh_event_data_t, move the data pointer to the correct address. This is * a workaround way, it's better to use flexible array in the interface. *//* ... */ 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/* ... */ /* CONFIG_BT_HID_HOST_ENABLED */ esp_hidh_dev_free_inner(dev); }{...} }{...} break;... case ESP_HIDH_CLOSE_EVENT: esp_hidh_dev_free_inner(param->close.dev); break;... default: break;... }{...} }{ ... } #endif/* ... */ /* CONFIG_BLUEDROID_ENABLED */ #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; }{...} /** * The deep copy data append the end of the esp_hidh_event_data_t, move the data pointer to the correct address. This is * a workaround way, it's better to use flexible array in the interface. *//* ... */ 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 /* CONFIG_BT_NIMBLE_ENABLED */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.