@geastack/targets 0.1.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +83 -0
- package/package.json +50 -0
- package/targets/cmake/geatsc-generated-archive-build.mjs +614 -0
- package/targets/cmake/geatsc_generated_archive.cmake +116 -0
- package/targets/cmake/geatsc_generated_archive_build.cmake +50 -0
- package/targets/esp32/app_project.cmake +33 -0
- package/targets/esp32/apps/app_manager.cpp +188 -0
- package/targets/esp32/apps/app_manager.h +10 -0
- package/targets/esp32/chip_bindings/README.md +58 -0
- package/targets/esp32/chip_bindings/audio/es8311.cpp +556 -0
- package/targets/esp32/chip_bindings/displays/co5300.cpp +510 -0
- package/targets/esp32/chip_bindings/displays/co5300.h +47 -0
- package/targets/esp32/chip_bindings/displays/co5300_async_stream.inc +79 -0
- package/targets/esp32/chip_bindings/displays/qspi_panel.h +65 -0
- package/targets/esp32/chip_bindings/displays/sh8601.cpp +583 -0
- package/targets/esp32/chip_bindings/displays/sh8601.h +36 -0
- package/targets/esp32/chip_bindings/gps/lc76g.cpp +296 -0
- package/targets/esp32/chip_bindings/gps/lc76g.h +7 -0
- package/targets/esp32/chip_bindings/imu/bmi270.cpp +299 -0
- package/targets/esp32/chip_bindings/imu/bmi270_config.inl +439 -0
- package/targets/esp32/chip_bindings/imu/qmi8658.cpp +213 -0
- package/targets/esp32/chip_bindings/power/axp2101.cpp +185 -0
- package/targets/esp32/chip_bindings/touch/cst9217.cpp +229 -0
- package/targets/esp32/chip_bindings/touch/cst9217_controller.h +78 -0
- package/targets/esp32/chip_bindings/touch/ft3168.cpp +340 -0
- package/targets/esp32/chip_bindings/touch/ft3168_controller.h +103 -0
- package/targets/esp32/chip_bindings/touch/ft6336.cpp +310 -0
- package/targets/esp32/chip_bindings/touch/ft6336_controller.h +90 -0
- package/targets/esp32/components/gea_framework/CMakeLists.txt +62 -0
- package/targets/esp32/connectivity/ble_hid.cpp +2916 -0
- package/targets/esp32/connectivity/ble_hid.h +8 -0
- package/targets/esp32/connectivity/diagnostics_transport.cpp +523 -0
- package/targets/esp32/connectivity/diagnostics_transport_offline.cpp +108 -0
- package/targets/esp32/connectivity/wifi.cpp +615 -0
- package/targets/esp32/connectivity/wifi_debug_offline.cpp +37 -0
- package/targets/esp32/connectivity/wifi_task_stack.h +119 -0
- package/targets/esp32/display.cpp +6010 -0
- package/targets/esp32/display_headless.cpp +468 -0
- package/targets/esp32/gea/platform_random.h +11 -0
- package/targets/esp32/gea_framework.cmake +382 -0
- package/targets/esp32/gpio.cpp +229 -0
- package/targets/esp32/host_sources.cmake +35 -0
- package/targets/esp32/imu_absent.cpp +36 -0
- package/targets/esp32/ldfragments/gea_ui_state_external.lf +35 -0
- package/targets/esp32/memory_config.h +64 -0
- package/targets/esp32/power_absent.cpp +22 -0
- package/targets/esp32/sdk-patches/0001-esp_ptr_dma_capable-accept-PSRAM.patch +70 -0
- package/targets/esp32/sdk-patches/0002-gdma-access_ext_mem-spi_master.patch +59 -0
- package/targets/esp32/sdk-patches/README.md +127 -0
- package/targets/esp32/services/app_runner.cpp +248 -0
- package/targets/esp32/services/app_state.cpp +56 -0
- package/targets/esp32/services/audio_runtime_stub.cpp +86 -0
- package/targets/esp32/services/battery_service.cpp +31 -0
- package/targets/esp32/services/device_control.cpp +1896 -0
- package/targets/esp32/services/device_control.h +31 -0
- package/targets/esp32/services/frame_scheduler.cpp +1571 -0
- package/targets/esp32/services/heap.cpp +129 -0
- package/targets/esp32/services/memory.cpp +103 -0
- package/targets/esp32/services/ota.cpp +840 -0
- package/targets/esp32/services/ota_image.cpp +162 -0
- package/targets/esp32/services/ota_image.h +16 -0
- package/targets/esp32/services/storage_service.cpp +159 -0
- package/targets/esp32/touch_absent.cpp +44 -0
- package/targets/esp32-m5stack-m5paper/CMakeLists.txt +10 -0
- package/targets/esp32-m5stack-m5paper/README.md +55 -0
- package/targets/esp32-m5stack-m5paper/dependencies.lock +76 -0
- package/targets/esp32-m5stack-m5paper/main/CMakeLists.txt +46 -0
- package/targets/esp32-m5stack-m5paper/main/Kconfig.projbuild +10 -0
- package/targets/esp32-m5stack-m5paper/main/app_main.cpp +114 -0
- package/targets/esp32-m5stack-m5paper/main/audio_stub.cpp +27 -0
- package/targets/esp32-m5stack-m5paper/main/buttons.cpp +111 -0
- package/targets/esp32-m5stack-m5paper/main/display.cpp +1074 -0
- package/targets/esp32-m5stack-m5paper/main/i2c.cpp +36 -0
- package/targets/esp32-m5stack-m5paper/main/idf_component.yml +7 -0
- package/targets/esp32-m5stack-m5paper/main/imu_stub.cpp +20 -0
- package/targets/esp32-m5stack-m5paper/main/include/board.h +100 -0
- package/targets/esp32-m5stack-m5paper/main/include/buttons.h +8 -0
- package/targets/esp32-m5stack-m5paper/main/launcher_button.cpp +82 -0
- package/targets/esp32-m5stack-m5paper/main/m5paper_psram.lf +20 -0
- package/targets/esp32-m5stack-m5paper/main/power.cpp +73 -0
- package/targets/esp32-m5stack-m5paper/main/sdcard_mount.cpp +82 -0
- package/targets/esp32-m5stack-m5paper/main/touch.cpp +279 -0
- package/targets/esp32-m5stack-m5paper/partitions.csv +9 -0
- package/targets/esp32-m5stack-m5paper/sdkconfig.defaults +84 -0
- package/targets/esp32-p4-m5stack-tab5/CMakeLists.txt +10 -0
- package/targets/esp32-p4-m5stack-tab5/dependencies.lock +276 -0
- package/targets/esp32-p4-m5stack-tab5/main/CMakeLists.txt +586 -0
- package/targets/esp32-p4-m5stack-tab5/main/Kconfig.projbuild +12 -0
- package/targets/esp32-p4-m5stack-tab5/main/app_main.cpp +177 -0
- package/targets/esp32-p4-m5stack-tab5/main/camera.cpp +2025 -0
- package/targets/esp32-p4-m5stack-tab5/main/display.cpp +4033 -0
- package/targets/esp32-p4-m5stack-tab5/main/fatfs_ffconf.h +383 -0
- package/targets/esp32-p4-m5stack-tab5/main/i2c.cpp +39 -0
- package/targets/esp32-p4-m5stack-tab5/main/idf_component.yml +21 -0
- package/targets/esp32-p4-m5stack-tab5/main/include/board.h +130 -0
- package/targets/esp32-p4-m5stack-tab5/main/include/tab5_drivers.h +68 -0
- package/targets/esp32-p4-m5stack-tab5/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-p4-m5stack-tab5/main/launcher_button.cpp +188 -0
- package/targets/esp32-p4-m5stack-tab5/main/power.cpp +22 -0
- package/targets/esp32-p4-m5stack-tab5/main/sdcard_mount.cpp +125 -0
- package/targets/esp32-p4-m5stack-tab5/main/tab5_audio.cpp +345 -0
- package/targets/esp32-p4-m5stack-tab5/main/tab5_drivers.cpp +871 -0
- package/targets/esp32-p4-m5stack-tab5/main/tab5_imu.cpp +246 -0
- package/targets/esp32-p4-m5stack-tab5/main/touch.cpp +206 -0
- package/targets/esp32-p4-m5stack-tab5/partitions.csv +12 -0
- package/targets/esp32-p4-m5stack-tab5/sdkconfig.defaults +128 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/CMakeLists.txt +19 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/component-patches/0001-esp_video-csi-raw-passthrough-p4-rev-lt-v3.patch +28 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/component-patches/README.md +45 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/component-patches/apply.sh +37 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/dependencies.lock +196 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/CMakeLists.txt +562 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/Kconfig.projbuild +12 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/app_main.cpp +166 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/camera.cpp +2105 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/display.cpp +1730 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/fatfs_ffconf.h +383 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/i2c.cpp +39 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/idf_component.yml +11 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/include/board.h +87 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/include/panel_native.h +59 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/launcher_button.cpp +191 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/power.cpp +15 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/touch.cpp +306 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/partitions.csv +13 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/patches/README.md +22 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/patches/espressif__esp_video-csi-bypass.patch +28 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/sdkconfig.defaults +109 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/tools/README-camera-capture.md +84 -0
- package/targets/esp32-p4-waveshare-touch-lcd-3.5/tools/capture-camera.py +369 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/CMakeLists.txt +10 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/dependencies.lock +258 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/CMakeLists.txt +577 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/Kconfig.projbuild +12 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/app_main.cpp +177 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/camera.cpp +1935 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/display.cpp +2832 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/fatfs_ffconf.h +383 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/i2c.cpp +39 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/idf_component.yml +23 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/include/board.h +68 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/launcher_button.cpp +191 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/power.cpp +15 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/sdcard_mount.cpp +125 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/main/touch.cpp +332 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/partitions.csv +12 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/sdkconfig.defaults +122 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/tools/README-camera-capture.md +84 -0
- package/targets/esp32-p4-waveshare-touch-lcd-7/tools/capture-camera.py +369 -0
- package/targets/esp32-s3-devkit-n16r8.json +31 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/CMakeLists.txt +10 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/dependencies.lock +40 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/CMakeLists.txt +549 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/app_main.cpp +187 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/display_rgb.cpp +1338 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/i2c.cpp +39 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/idf_component.yml +5 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/include/board.h +142 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/include/pcf8574.h +16 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/include/rotary_encoder.h +7 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/launcher_button.cpp +191 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/pcf8574.cpp +86 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/platform_stubs.cpp +47 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/rotary_encoder.cpp +135 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/main/touch.cpp +250 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/partitions.csv +8 -0
- package/targets/esp32-s3-elecrow-rotary-2.1/sdkconfig.defaults +116 -0
- package/targets/esp32-s3-epaper-1.54/CMakeLists.txt +10 -0
- package/targets/esp32-s3-epaper-1.54/dependencies.lock +76 -0
- package/targets/esp32-s3-epaper-1.54/main/CMakeLists.txt +691 -0
- package/targets/esp32-s3-epaper-1.54/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-epaper-1.54/main/app_main.cpp +181 -0
- package/targets/esp32-s3-epaper-1.54/main/buttons.cpp +144 -0
- package/targets/esp32-s3-epaper-1.54/main/display.cpp +1281 -0
- package/targets/esp32-s3-epaper-1.54/main/fatfs_ffconf.h +377 -0
- package/targets/esp32-s3-epaper-1.54/main/i2c.cpp +39 -0
- package/targets/esp32-s3-epaper-1.54/main/idf_component.yml +7 -0
- package/targets/esp32-s3-epaper-1.54/main/imu_stub.cpp +21 -0
- package/targets/esp32-s3-epaper-1.54/main/include/board.h +119 -0
- package/targets/esp32-s3-epaper-1.54/main/include/buttons.h +9 -0
- package/targets/esp32-s3-epaper-1.54/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-s3-epaper-1.54/main/launcher_button.cpp +191 -0
- package/targets/esp32-s3-epaper-1.54/main/power.cpp +15 -0
- package/targets/esp32-s3-epaper-1.54/main/sdcard_mount.cpp +89 -0
- package/targets/esp32-s3-epaper-1.54/main/touch.cpp +299 -0
- package/targets/esp32-s3-epaper-1.54/partitions.csv +8 -0
- package/targets/esp32-s3-epaper-1.54/sdkconfig.defaults +217 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/CMakeLists.txt +10 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/README.md +29 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/dependencies.lock +116 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/main/CMakeLists.txt +90 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/main/idf_component.yml +7 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/main/include/board.h +61 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/main/touch.cpp +299 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/partitions.csv +7 -0
- package/targets/esp32-s3-lilygo-t-display-s3-long/sdkconfig.defaults +50 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/CMakeLists.txt +12 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/CMakeLists.txt +17 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/ed047tc1.c +269 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/ed047tc1.h +142 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/epd_driver.c +1114 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/epd_driver.h +410 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/gea_epd.c +989 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/i2s_data_bus.c +489 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/i2s_data_bus.h +104 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/rmt_pulse.c +136 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/rmt_pulse.h +78 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/components/epd_driver/utilities.h +49 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/dependencies.lock +35 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/CMakeLists.txt +30 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/Kconfig.projbuild +10 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/app_main.cpp +95 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/audio_stub.cpp +27 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/buttons.cpp +40 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/display.cpp +627 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/i2c.cpp +36 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/idf_component.yml +4 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/imu_stub.cpp +20 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/include/board.h +85 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/include/buttons.h +8 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/launcher_button.cpp +82 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/power.cpp +36 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/sdcard_mount.cpp +90 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/main/touch.cpp +293 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/partitions.csv +9 -0
- package/targets/esp32-s3-lilygo-t5-epaper-4.7/sdkconfig.defaults +217 -0
- package/targets/esp32-s3-m5stack-papers3/CMakeLists.txt +13 -0
- package/targets/esp32-s3-m5stack-papers3/components/papers3_epd/CMakeLists.txt +5 -0
- package/targets/esp32-s3-m5stack-papers3/components/papers3_epd/include/papers3_epd.h +74 -0
- package/targets/esp32-s3-m5stack-papers3/components/papers3_epd/papers3_epd.c +671 -0
- package/targets/esp32-s3-m5stack-papers3/dependencies.lock +76 -0
- package/targets/esp32-s3-m5stack-papers3/main/CMakeLists.txt +64 -0
- package/targets/esp32-s3-m5stack-papers3/main/Kconfig.projbuild +10 -0
- package/targets/esp32-s3-m5stack-papers3/main/app_main.cpp +96 -0
- package/targets/esp32-s3-m5stack-papers3/main/audio_stub.cpp +27 -0
- package/targets/esp32-s3-m5stack-papers3/main/buttons.cpp +49 -0
- package/targets/esp32-s3-m5stack-papers3/main/display.cpp +706 -0
- package/targets/esp32-s3-m5stack-papers3/main/i2c.cpp +36 -0
- package/targets/esp32-s3-m5stack-papers3/main/idf_component.yml +14 -0
- package/targets/esp32-s3-m5stack-papers3/main/imu.cpp +351 -0
- package/targets/esp32-s3-m5stack-papers3/main/include/board.h +101 -0
- package/targets/esp32-s3-m5stack-papers3/main/include/buttons.h +8 -0
- package/targets/esp32-s3-m5stack-papers3/main/launcher_button.cpp +96 -0
- package/targets/esp32-s3-m5stack-papers3/main/power.cpp +102 -0
- package/targets/esp32-s3-m5stack-papers3/main/sdcard_mount.cpp +88 -0
- package/targets/esp32-s3-m5stack-papers3/main/touch.cpp +287 -0
- package/targets/esp32-s3-m5stack-papers3/partitions.csv +9 -0
- package/targets/esp32-s3-m5stack-papers3/sdkconfig.defaults +232 -0
- package/targets/esp32-s3-m5stack-sticks3/CMakeLists.txt +10 -0
- package/targets/esp32-s3-m5stack-sticks3/dependencies.lock +51 -0
- package/targets/esp32-s3-m5stack-sticks3/main/CMakeLists.txt +51 -0
- package/targets/esp32-s3-m5stack-sticks3/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-m5stack-sticks3/main/app_main.cpp +130 -0
- package/targets/esp32-s3-m5stack-sticks3/main/buttons.cpp +101 -0
- package/targets/esp32-s3-m5stack-sticks3/main/display.cpp +1391 -0
- package/targets/esp32-s3-m5stack-sticks3/main/i2c.cpp +39 -0
- package/targets/esp32-s3-m5stack-sticks3/main/idf_component.yml +8 -0
- package/targets/esp32-s3-m5stack-sticks3/main/imu_stub.cpp +9 -0
- package/targets/esp32-s3-m5stack-sticks3/main/include/board.h +89 -0
- package/targets/esp32-s3-m5stack-sticks3/main/include/buttons.h +9 -0
- package/targets/esp32-s3-m5stack-sticks3/main/launcher_button.cpp +191 -0
- package/targets/esp32-s3-m5stack-sticks3/main/power.cpp +15 -0
- package/targets/esp32-s3-m5stack-sticks3/main/sdcard_mount.cpp +8 -0
- package/targets/esp32-s3-m5stack-sticks3/main/touch.cpp +65 -0
- package/targets/esp32-s3-m5stack-sticks3/partitions.csv +8 -0
- package/targets/esp32-s3-m5stack-sticks3/sdkconfig.defaults +130 -0
- package/targets/esp32-s3-touch-amoled-1.75/CMakeLists.txt +10 -0
- package/targets/esp32-s3-touch-amoled-1.75/dependencies.lock +130 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/CMakeLists.txt +589 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/app_main.cpp +153 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/i2c.cpp +39 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/idf_component.yml +9 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/include/board.h +110 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/launcher_button.cpp +191 -0
- package/targets/esp32-s3-touch-amoled-1.75/main/touch.cpp +45 -0
- package/targets/esp32-s3-touch-amoled-1.75/partitions.csv +8 -0
- package/targets/esp32-s3-touch-amoled-1.75/sdkconfig.defaults +179 -0
- package/targets/esp32-s3-touch-amoled-1.8/CMakeLists.txt +10 -0
- package/targets/esp32-s3-touch-amoled-1.8/dependencies.lock +105 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +768 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/app_main.cpp +172 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/i2c.cpp +39 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/idf_component.yml +8 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/include/board.h +83 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/launcher_button.cpp +191 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/touch.cpp +41 -0
- package/targets/esp32-s3-touch-amoled-1.8/partitions.csv +8 -0
- package/targets/esp32-s3-touch-amoled-1.8/sdkconfig.defaults +179 -0
- package/targets/esp32-s3-touch-amoled-2.06/CMakeLists.txt +10 -0
- package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +64 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +1121 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +333 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/i2c.cpp +39 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/idf_component.yml +27 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/include/board.h +114 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/launcher_button.cpp +199 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/sdcard_mount.cpp +105 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/touch.cpp +49 -0
- package/targets/esp32-s3-touch-amoled-2.06/partitions.csv +14 -0
- package/targets/esp32-s3-touch-amoled-2.06/sdkconfig.defaults +273 -0
- package/targets/esp32-s3-touch-amoled-2.41/CMakeLists.txt +10 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/CMakeLists.txt +71 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/idf_component.yml +27 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/include/board.h +108 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/include/wifi_config.h.example +5 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/panel_reset.cpp +82 -0
- package/targets/esp32-s3-touch-amoled-2.41/main/touch.cpp +49 -0
- package/targets/esp32-s3-touch-amoled-2.41/partitions.csv +8 -0
- package/targets/esp32-s3-touch-amoled-2.41/sdkconfig.defaults +273 -0
- package/targets/rp2350-tufty-2350/CMakeLists.txt +348 -0
- package/targets/rp2350-tufty-2350/README.md +53 -0
- package/targets/rp2350-tufty-2350/boards/pimoroni_tufty2350.h +87 -0
- package/targets/rp2350-tufty-2350/main/bringup.cpp +187 -0
- package/targets/rp2350-tufty-2350/main/include/board.h +124 -0
- package/targets/rp2350-tufty-2350/main/rp2350_panel.cpp +428 -0
- package/targets/rp2350-tufty-2350/main/rp2350_panel.h +46 -0
- package/targets/rp2350-tufty-2350/main/st7789_parallel.pio +7 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/CMakeLists.txt +345 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/README.md +153 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/boards/waveshare_rp2350_touch_amoled_2_41.h +59 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/app.cpp +984 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/bringup.cpp +222 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/include/board.h +138 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/rp2350_gea_platform.cpp +2553 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/rp2350_gea_platform.h +18 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/rp2350_host_fetch_stub.cpp +64 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/rp2350_panel.cpp +677 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/rp2350_panel.h +48 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/vendor/waveshare_psram/rp_pico_alloc.c +354 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/vendor/waveshare_psram/rp_pico_alloc.h +56 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/vendor/waveshare_psram/tlsf/tlsf.c +1284 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/vendor/waveshare_psram/tlsf/tlsf.h +95 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/vendor/waveshare_psram/tlsf/tlsf_block_functions.h +123 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/main/vendor/waveshare_psram/tlsf/tlsf_common.h +127 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/tools/rp2350-scroll-bench.py +162 -0
- package/targets/rp2350-waveshare-touch-amoled-2.41/tools/rp2350-scroll-parity.py +198 -0
- package/targets.json +154 -0
|
@@ -0,0 +1,2916 @@
|
|
|
1
|
+
#include "connectivity/ble_hid.h"
|
|
2
|
+
|
|
3
|
+
#include "ble.h"
|
|
4
|
+
#include "bluetooth/hid_profile.h"
|
|
5
|
+
#include "imu.h"
|
|
6
|
+
#include "services/bluetooth_service.h"
|
|
7
|
+
#include "services/ota.h"
|
|
8
|
+
#include "wifi.h"
|
|
9
|
+
|
|
10
|
+
#include <cstdint>
|
|
11
|
+
#include <cstdio>
|
|
12
|
+
#include <cstring>
|
|
13
|
+
|
|
14
|
+
#include "esp_bt.h"
|
|
15
|
+
#include "esp_log.h"
|
|
16
|
+
#include "esp_mac.h"
|
|
17
|
+
#include "esp_random.h"
|
|
18
|
+
#include "esp_timer.h"
|
|
19
|
+
#include "nvs_flash.h"
|
|
20
|
+
|
|
21
|
+
#include "freertos/FreeRTOS.h"
|
|
22
|
+
#include "freertos/task.h"
|
|
23
|
+
#include "freertos/semphr.h"
|
|
24
|
+
#include "freertos/idf_additions.h"
|
|
25
|
+
|
|
26
|
+
#include "esp_memory_utils.h"
|
|
27
|
+
#include "esp_heap_caps.h"
|
|
28
|
+
|
|
29
|
+
#include "nimble/nimble_port.h"
|
|
30
|
+
#include "nimble/nimble_port_freertos.h"
|
|
31
|
+
#include "host/ble_hs.h"
|
|
32
|
+
#include "host/ble_att.h"
|
|
33
|
+
#include "host/ble_gap.h"
|
|
34
|
+
#include "host/ble_uuid.h"
|
|
35
|
+
#include "host/util/util.h"
|
|
36
|
+
#include "services/gap/ble_svc_gap.h"
|
|
37
|
+
#include "services/gatt/ble_svc_gatt.h"
|
|
38
|
+
#include "store/config/ble_store_config.h"
|
|
39
|
+
|
|
40
|
+
#include "sdkconfig.h"
|
|
41
|
+
|
|
42
|
+
// Central + observer roles (scanning, ble_gap_connect, the GATT client used to
|
|
43
|
+
// host a remote HID keyboard and drive a BLE-MIDI pedal) are compiled only on
|
|
44
|
+
// targets whose sdkconfig enables them (amoled 2.06). Peripheral-only targets
|
|
45
|
+
// keep their exact NimBLE footprint; the base-class no-op defaults serve.
|
|
46
|
+
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
|
47
|
+
#define GEA_BLE_CENTRAL 1
|
|
48
|
+
#else
|
|
49
|
+
#define GEA_BLE_CENTRAL 0
|
|
50
|
+
#endif
|
|
51
|
+
|
|
52
|
+
extern "C" void ble_store_config_init(void);
|
|
53
|
+
|
|
54
|
+
namespace gea::targets::esp32::wifi::wifi_debug {
|
|
55
|
+
int state();
|
|
56
|
+
int lastError();
|
|
57
|
+
std::uint32_t dmaFree();
|
|
58
|
+
std::uint32_t dmaLargest();
|
|
59
|
+
} // namespace wifi_debug
|
|
60
|
+
|
|
61
|
+
namespace gea::targets::esp32::ota_debug {
|
|
62
|
+
int startError();
|
|
63
|
+
bool started();
|
|
64
|
+
std::uint32_t dmaFree();
|
|
65
|
+
std::uint32_t dmaLargest();
|
|
66
|
+
} // namespace gea::targets::esp32::ota_debug
|
|
67
|
+
|
|
68
|
+
namespace gea::targets::esp32::ble {
|
|
69
|
+
|
|
70
|
+
namespace hid = gea::framework::bluetooth::hid;
|
|
71
|
+
using std::intptr_t;
|
|
72
|
+
using std::uint16_t;
|
|
73
|
+
using std::uint8_t;
|
|
74
|
+
|
|
75
|
+
class HidServer final : public gea::framework::bluetooth::BluetoothHidDriver {
|
|
76
|
+
public:
|
|
77
|
+
static HidServer &instance()
|
|
78
|
+
{
|
|
79
|
+
static HidServer server;
|
|
80
|
+
return server;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
void preinit() override;
|
|
84
|
+
void init(const char *device_name, uint16_t appearance, const char *mac_address) override;
|
|
85
|
+
bool enabled() const override { return enabled_; }
|
|
86
|
+
void setEnabled(bool enabled) override;
|
|
87
|
+
void startAdvertising() override;
|
|
88
|
+
void stopAdvertising() override;
|
|
89
|
+
bool connected() const override { return connected_; }
|
|
90
|
+
bool bound() const override { return kbdConn_ != kNoConn || mouseConn_ != kNoConn; }
|
|
91
|
+
uint8_t batteryLevel() const override { return batteryLevel_; }
|
|
92
|
+
const char *mac() override;
|
|
93
|
+
const char *deviceName() const override { return deviceName_[0] ? deviceName_ : hid::kDefaultDeviceName; }
|
|
94
|
+
void keyTap(int hid_code) override;
|
|
95
|
+
void keyDown(int modifier, int hid_code) override;
|
|
96
|
+
void keyUp() override;
|
|
97
|
+
void mouseMove(int dx, int dy, int buttons, int wheel) override;
|
|
98
|
+
void mouseClick(int button) override;
|
|
99
|
+
void setBatteryLevel(uint8_t level) override;
|
|
100
|
+
|
|
101
|
+
// BLE-MIDI peripheral. The MIDI service is registered unconditionally AFTER
|
|
102
|
+
// the HID/battery/IMU services (bonded HID handles stay put); midiEnabled_
|
|
103
|
+
// gates the advertising payload, not the registration.
|
|
104
|
+
void midiEnable() override;
|
|
105
|
+
bool midiBound() const override;
|
|
106
|
+
void midiSend(const uint8_t *packet, int length) override;
|
|
107
|
+
|
|
108
|
+
// Live registry of every link this device holds, across all roles.
|
|
109
|
+
int connectionCount() const override;
|
|
110
|
+
int connectionKindAt(int index) const override;
|
|
111
|
+
const char *connectionNameAt(int index) override;
|
|
112
|
+
|
|
113
|
+
// Config service (custom GATT). A Web Bluetooth portal reads the app's
|
|
114
|
+
// config blob and, once paired with the 4-digit code shown on-device,
|
|
115
|
+
// writes a new one. Registered unconditionally AFTER every other service
|
|
116
|
+
// (additive rule: handles ahead of it stay stable) — it is cheap, so
|
|
117
|
+
// peripheral-only targets keep it too.
|
|
118
|
+
void configSetDocument(const uint8_t *bytes, int length) override;
|
|
119
|
+
int configPendingLength() const override;
|
|
120
|
+
int configPendingByteAt(int index) const override;
|
|
121
|
+
void configConsumePending() override;
|
|
122
|
+
bool configPairing() const override;
|
|
123
|
+
const char *configPairCode() override;
|
|
124
|
+
void configDismissPairing() override;
|
|
125
|
+
// Device -> portal activity push. Stashes the fired control's index and
|
|
126
|
+
// drains it on the NimBLE host task (ble_gatts_notify_custom is host-task
|
|
127
|
+
// only), sending a CTRL notify (0xA0 + index) to a subscribed portal.
|
|
128
|
+
void configPushActivity(int index) override;
|
|
129
|
+
void drainActivityPush();
|
|
130
|
+
|
|
131
|
+
#if GEA_BLE_CENTRAL
|
|
132
|
+
// One scan engine, two filters: the scan-result list is shared and cleared
|
|
133
|
+
// on every scan start, so midi*/hidHost* getters read whichever scan ran last.
|
|
134
|
+
// App-facing GAP ops just queue a request onto the host task (see requestBleCmd).
|
|
135
|
+
void midiStartScan() override { reqScanMode_ = static_cast<int>(ScanMode::Midi); requestBleCmd(); }
|
|
136
|
+
void midiStopScan() override { reqScanMode_ = -1; requestBleCmd(); }
|
|
137
|
+
bool midiScanning() const override { return scanMode_ == ScanMode::Midi; }
|
|
138
|
+
int midiScanCount() const override { return scanCount(); }
|
|
139
|
+
const char *midiScanNameAt(int index) override { return scanNameAt(index); }
|
|
140
|
+
void midiConnect(int index) override { reqConnectIndex_ = index; reqConnectKind_ = kLinkMidiPeer; requestBleCmd(); }
|
|
141
|
+
void midiDisconnect() override { reqDisconnectMidi_ = true; requestBleCmd(); }
|
|
142
|
+
|
|
143
|
+
void hidHostStartScan() override { reqScanMode_ = static_cast<int>(ScanMode::HidHost); requestBleCmd(); }
|
|
144
|
+
void hidHostStopScan() override { reqScanMode_ = -1; requestBleCmd(); }
|
|
145
|
+
bool hidHostScanning() const override { return scanMode_ == ScanMode::HidHost; }
|
|
146
|
+
int hidHostScanCount() const override { return scanCount(); }
|
|
147
|
+
const char *hidHostScanNameAt(int index) override { return scanNameAt(index); }
|
|
148
|
+
void hidHostConnect(int index) override { reqConnectIndex_ = index; reqConnectKind_ = kLinkHidHosted; requestBleCmd(); }
|
|
149
|
+
void hidHostDisconnect() override { reqDisconnectHid_ = true; requestBleCmd(); }
|
|
150
|
+
bool hidHostBound() const override { return hidHostConn_ != kNoConn && hostSubscribed_ > 0; }
|
|
151
|
+
|
|
152
|
+
int hidHostReportCount() const override;
|
|
153
|
+
int hidHostReportIdAt(int index) const override;
|
|
154
|
+
int hidHostReportLenAt(int index) const override;
|
|
155
|
+
int hidHostReportByteAt(int index, int byteIndex) const override;
|
|
156
|
+
void hidHostClearReports() override;
|
|
157
|
+
#endif
|
|
158
|
+
|
|
159
|
+
static int gapEventCallback(struct ble_gap_event *event, void *arg)
|
|
160
|
+
{
|
|
161
|
+
(void)arg;
|
|
162
|
+
return instance().handleGapEvent(event);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static int hidAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
166
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
167
|
+
{
|
|
168
|
+
return instance().handleHidAccess(conn_handle, attr_handle, ctxt, arg);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static int batteryAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
172
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
173
|
+
{
|
|
174
|
+
return instance().handleBatteryAccess(conn_handle, attr_handle, ctxt, arg);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static int imuAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
178
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
179
|
+
{
|
|
180
|
+
return instance().handleImuAccess(conn_handle, attr_handle, ctxt, arg);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static int midiAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
184
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
185
|
+
{
|
|
186
|
+
return instance().handleMidiAccess(conn_handle, attr_handle, ctxt, arg);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static int configCtrlAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
190
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
191
|
+
{
|
|
192
|
+
return instance().handleConfigCtrlAccess(conn_handle, attr_handle, ctxt, arg);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static int configDataAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
196
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
197
|
+
{
|
|
198
|
+
return instance().handleConfigDataAccess(conn_handle, attr_handle, ctxt, arg);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
static int otaCtrlAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
202
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
203
|
+
{
|
|
204
|
+
return instance().handleOtaCtrlAccess(conn_handle, attr_handle, ctxt, arg);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static int otaDataAccessCallback(uint16_t conn_handle, uint16_t attr_handle,
|
|
208
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
209
|
+
{
|
|
210
|
+
return instance().handleOtaDataAccess(conn_handle, attr_handle, ctxt, arg);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#if GEA_BLE_CENTRAL
|
|
214
|
+
// GATT-client procedure callbacks (run on the NimBLE host task).
|
|
215
|
+
static int midiSvcDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
216
|
+
const struct ble_gatt_svc *service, void *arg);
|
|
217
|
+
static int midiChrDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
218
|
+
const struct ble_gatt_chr *chr, void *arg);
|
|
219
|
+
static int midiDscDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
220
|
+
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, void *arg);
|
|
221
|
+
static int hidSvcDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
222
|
+
const struct ble_gatt_svc *service, void *arg);
|
|
223
|
+
static int hidChrDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
224
|
+
const struct ble_gatt_chr *chr, void *arg);
|
|
225
|
+
static int hidDscDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
226
|
+
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, void *arg);
|
|
227
|
+
static int hidRefReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
228
|
+
struct ble_gatt_attr *attr, void *arg);
|
|
229
|
+
static int hidCccdWriteCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
230
|
+
struct ble_gatt_attr *attr, void *arg);
|
|
231
|
+
#endif
|
|
232
|
+
|
|
233
|
+
static void onSyncCallback() { instance().onSync(); }
|
|
234
|
+
static void onResetCallback(int reason) { instance().onReset(reason); }
|
|
235
|
+
|
|
236
|
+
static void hostTask(void *param)
|
|
237
|
+
{
|
|
238
|
+
(void)param;
|
|
239
|
+
ESP_LOGI(kTag, "NimBLE host task started");
|
|
240
|
+
nimble_port_run();
|
|
241
|
+
nimble_port_freertos_deinit();
|
|
242
|
+
vTaskDelete(nullptr);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Air-mouse simulator feed: while a central is subscribed to the custom IMU
|
|
246
|
+
// characteristic, sample the QMI8658 at ~100 Hz and notify a packed sample.
|
|
247
|
+
static void imuNotifyTask(void *param);
|
|
248
|
+
|
|
249
|
+
private:
|
|
250
|
+
enum class HidAccessId : intptr_t {
|
|
251
|
+
HidInformation = 0,
|
|
252
|
+
ReportMap = 1,
|
|
253
|
+
ControlPoint = 2,
|
|
254
|
+
ProtocolMode = 3,
|
|
255
|
+
KeyboardReport = 4,
|
|
256
|
+
MouseReport = 5,
|
|
257
|
+
KeyboardReportReference = 10,
|
|
258
|
+
MouseReportReference = 11,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
HidServer();
|
|
262
|
+
|
|
263
|
+
void configureGattTable();
|
|
264
|
+
int handleHidAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
265
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
266
|
+
int handleBatteryAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
267
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
268
|
+
int handleImuAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
269
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
270
|
+
int handleMidiAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
271
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
272
|
+
int handleConfigCtrlAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
273
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
274
|
+
int handleConfigDataAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
275
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
276
|
+
int handleOtaCtrlAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
277
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
278
|
+
int handleOtaDataAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
279
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
280
|
+
void otaCtrlNotify(uint16_t conn, uint8_t opcode,
|
|
281
|
+
gea::framework::services::OtaServer::Result result);
|
|
282
|
+
void wifiStatusNotify(uint16_t conn);
|
|
283
|
+
// Config-service helpers (host-task write handler + app-task getters share
|
|
284
|
+
// the config buffers; all guarded by stateMux_). configCtrlNotify() sends a
|
|
285
|
+
// CTRL status notification; NimBLE calls are made OUTSIDE the critical
|
|
286
|
+
// section (never notify/alloc mbufs while holding the spinlock).
|
|
287
|
+
void configCtrlNotify(uint16_t conn, const uint8_t *data, int len);
|
|
288
|
+
bool configIsAuthed(uint16_t conn) const; // caller holds stateMux_
|
|
289
|
+
void configAddAuthed(uint16_t conn);
|
|
290
|
+
void configClearConn(uint16_t conn); // drop authed slot on disconnect
|
|
291
|
+
int handleGapEvent(struct ble_gap_event *event);
|
|
292
|
+
void notifyKeyboard();
|
|
293
|
+
void notifyMouse();
|
|
294
|
+
void onSync();
|
|
295
|
+
void onReset(int reason);
|
|
296
|
+
int parseMacLe(const char *str, uint8_t out[6]) const;
|
|
297
|
+
|
|
298
|
+
// Connection registry (NimBLE host task writes, app task reads; spinlocked).
|
|
299
|
+
void linkAdd(uint16_t handle, uint8_t kind, const char *name);
|
|
300
|
+
void linkRemove(uint16_t handle);
|
|
301
|
+
void linkSetKind(uint16_t handle, uint8_t kind);
|
|
302
|
+
int inboundLinkCount() const;
|
|
303
|
+
|
|
304
|
+
#if GEA_BLE_CENTRAL
|
|
305
|
+
enum class ScanMode : uint8_t { None, Midi, HidHost };
|
|
306
|
+
|
|
307
|
+
void startScanInternal(ScanMode mode);
|
|
308
|
+
void stopScanInternal(bool restart_adv);
|
|
309
|
+
int scanCount() const;
|
|
310
|
+
const char *scanNameAt(int index);
|
|
311
|
+
void connectTo(int index, uint8_t kind);
|
|
312
|
+
bool issueCentralConnect(); // fire ble_gap_connect for the stored retry target
|
|
313
|
+
bool retryCentralConnect();
|
|
314
|
+
void handleCentralConnected(uint16_t conn_handle);
|
|
315
|
+
void handleDisc(const struct ble_gap_disc_desc *disc);
|
|
316
|
+
void handleHostNotify(const struct ble_gap_event *event);
|
|
317
|
+
void hidHostNextReport(uint16_t conn_handle);
|
|
318
|
+
void hidHostSubscribeCurrent(uint16_t conn_handle, bool input);
|
|
319
|
+
|
|
320
|
+
// GAP ops (scan/connect/terminate) MUST run on the NimBLE host task, not
|
|
321
|
+
// the gea app/touch task — calling ble_gap_* from the app task corrupts the
|
|
322
|
+
// register window and crashes (LoadProhibited in ble_gap_adv_active). The
|
|
323
|
+
// app-facing entry points below only stash a request and post this event to
|
|
324
|
+
// the default NimBLE event queue; bleCmdTrampoline drains it on the host
|
|
325
|
+
// task and calls the real startScanInternal/connectTo/stopScanInternal.
|
|
326
|
+
void requestBleCmd();
|
|
327
|
+
void runBleCmd();
|
|
328
|
+
static void bleCmdTrampoline(struct ble_npl_event *ev);
|
|
329
|
+
struct ble_npl_event bleCmdEvent_ = {};
|
|
330
|
+
bool bleCmdInited_ = false;
|
|
331
|
+
// req codes: -2 none, -1 stop-scan; else ScanMode int (1=Midi,2=HidHost)
|
|
332
|
+
volatile int reqScanMode_ = -2;
|
|
333
|
+
volatile int reqConnectIndex_ = -1;
|
|
334
|
+
volatile uint8_t reqConnectKind_ = 0;
|
|
335
|
+
volatile bool reqDisconnectMidi_ = false;
|
|
336
|
+
volatile bool reqDisconnectHid_ = false;
|
|
337
|
+
#endif
|
|
338
|
+
|
|
339
|
+
static void *accessArg(HidAccessId id)
|
|
340
|
+
{
|
|
341
|
+
return reinterpret_cast<void *>(static_cast<intptr_t>(id));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
static const char kTag[];
|
|
345
|
+
static const ble_uuid16_t kHidServiceUuid;
|
|
346
|
+
static const ble_uuid16_t kHidInformationUuid;
|
|
347
|
+
static const ble_uuid16_t kReportMapUuid;
|
|
348
|
+
static const ble_uuid16_t kHidControlPointUuid;
|
|
349
|
+
static const ble_uuid16_t kProtocolModeUuid;
|
|
350
|
+
static const ble_uuid16_t kReportUuid;
|
|
351
|
+
static const ble_uuid16_t kReportReferenceUuid;
|
|
352
|
+
static const ble_uuid16_t kBatteryServiceUuid;
|
|
353
|
+
static const ble_uuid16_t kBatteryLevelUuid;
|
|
354
|
+
// Custom raw-IMU stream service for the off-device air-mouse simulator (128-bit).
|
|
355
|
+
static const ble_uuid128_t kImuServiceUuid;
|
|
356
|
+
static const ble_uuid128_t kImuCharUuid;
|
|
357
|
+
// BLE-MIDI (MIDI over GATT) service + I/O characteristic (128-bit).
|
|
358
|
+
static const ble_uuid128_t kMidiServiceUuid;
|
|
359
|
+
static const ble_uuid128_t kMidiCharUuid;
|
|
360
|
+
// Config service (custom 128-bit): CTRL command/status + DATA blob transfer.
|
|
361
|
+
static const ble_uuid128_t kConfigServiceUuid;
|
|
362
|
+
static const ble_uuid128_t kConfigCtrlUuid;
|
|
363
|
+
static const ble_uuid128_t kConfigDataUuid;
|
|
364
|
+
static const ble_uuid128_t kOtaServiceUuid;
|
|
365
|
+
static const ble_uuid128_t kOtaCtrlUuid;
|
|
366
|
+
static const ble_uuid128_t kOtaDataUuid;
|
|
367
|
+
|
|
368
|
+
// connectionKindAt() values (contract in core ble.h).
|
|
369
|
+
static constexpr uint8_t kLinkHidServed = 0; // central on our HID service (desktop)
|
|
370
|
+
static constexpr uint8_t kLinkMidiServed = 1; // central on our MIDI service (DAW)
|
|
371
|
+
static constexpr uint8_t kLinkHidHosted = 2; // HID peripheral we host (XPPen)
|
|
372
|
+
static constexpr uint8_t kLinkMidiPeer = 3; // BLE-MIDI peripheral we drive (pedal)
|
|
373
|
+
static constexpr uint16_t kNoConn = BLE_HS_CONN_HANDLE_NONE;
|
|
374
|
+
|
|
375
|
+
char deviceName_[32] = {};
|
|
376
|
+
uint16_t appearance_ = hid::kDefaultAppearance;
|
|
377
|
+
char macAddress_[18] = {};
|
|
378
|
+
char formattedMac_[18] = "";
|
|
379
|
+
|
|
380
|
+
bool hostSynced_ = false;
|
|
381
|
+
bool controllerInited_ = false;
|
|
382
|
+
bool hostInited_ = false;
|
|
383
|
+
bool enabled_ = true;
|
|
384
|
+
|
|
385
|
+
bool connected_ = false; // >=1 inbound (peripheral-role) link
|
|
386
|
+
|
|
387
|
+
// Per-characteristic subscriber conn handles: with MAX_CONNECTIONS>1 the
|
|
388
|
+
// desktop (HID) and a DAW (MIDI) can be connected at once, so each notify
|
|
389
|
+
// target is tracked per subscription instead of one global connHandle_.
|
|
390
|
+
uint16_t kbdConn_ = kNoConn;
|
|
391
|
+
uint16_t mouseConn_ = kNoConn;
|
|
392
|
+
uint16_t batteryConn_ = kNoConn;
|
|
393
|
+
uint16_t imuConn_ = kNoConn;
|
|
394
|
+
uint16_t midiSrvConn_ = kNoConn; // central subscribed to our MIDI I/O char
|
|
395
|
+
|
|
396
|
+
uint16_t keyboardAttrHandle_ = 0;
|
|
397
|
+
uint16_t mouseAttrHandle_ = 0;
|
|
398
|
+
uint16_t batteryAttrHandle_ = 0;
|
|
399
|
+
uint16_t imuAttrHandle_ = 0;
|
|
400
|
+
uint16_t midiAttrHandle_ = 0;
|
|
401
|
+
uint8_t imuLast_[16] = {}; // last packed sample (served on a plain read)
|
|
402
|
+
|
|
403
|
+
bool midiEnabled_ = false;
|
|
404
|
+
|
|
405
|
+
uint8_t protocolMode_ = hid::kReportProtocol;
|
|
406
|
+
hid::KeyboardMouseReports reports_;
|
|
407
|
+
uint8_t batteryLevel_ = 100;
|
|
408
|
+
|
|
409
|
+
struct ble_gatt_dsc_def keyboardReportDescriptors_[2] = {};
|
|
410
|
+
struct ble_gatt_dsc_def mouseReportDescriptors_[2] = {};
|
|
411
|
+
struct ble_gatt_chr_def hidCharacteristics_[7] = {};
|
|
412
|
+
struct ble_gatt_chr_def batteryCharacteristics_[2] = {};
|
|
413
|
+
struct ble_gatt_chr_def imuCharacteristics_[2] = {};
|
|
414
|
+
struct ble_gatt_chr_def midiCharacteristics_[2] = {};
|
|
415
|
+
struct ble_gatt_chr_def configCharacteristics_[3] = {}; // CTRL, DATA, terminator
|
|
416
|
+
struct ble_gatt_chr_def otaCharacteristics_[3] = {}; // CTRL, DATA, terminator
|
|
417
|
+
struct ble_gatt_svc_def gattServices_[7] = {};
|
|
418
|
+
|
|
419
|
+
// ---- config service state (host-task callbacks vs app-task getters) -------
|
|
420
|
+
// All config buffers are guarded by stateMux_. The doc is what the portal
|
|
421
|
+
// reads back; inbound accumulates a portal write; pending holds a committed
|
|
422
|
+
// blob for the app to drain.
|
|
423
|
+
static constexpr int kConfigDocMax = 4096;
|
|
424
|
+
uint16_t configCtrlAttrHandle_ = 0;
|
|
425
|
+
uint16_t configDataAttrHandle_ = 0;
|
|
426
|
+
uint16_t configCtrlConn_ = kNoConn; // central subscribed to CTRL notifications
|
|
427
|
+
uint16_t otaCtrlAttrHandle_ = 0;
|
|
428
|
+
uint16_t otaDataAttrHandle_ = 0;
|
|
429
|
+
uint16_t otaCtrlConn_ = kNoConn;
|
|
430
|
+
// Latest control index the app asked to push to the portal (-1 = none).
|
|
431
|
+
// Written on the app task, drained on the NimBLE host task; only the most
|
|
432
|
+
// recent index is kept (a queued race just supersedes the older one).
|
|
433
|
+
volatile int reqActivityIndex_ = -1;
|
|
434
|
+
|
|
435
|
+
// PSRAM-backed (allocated in preinit) — three 4 KB blobs would be 12 KB of
|
|
436
|
+
// scarce internal .bss otherwise, which starved NimBLE's scan allocations
|
|
437
|
+
// and intermittently crashed the central scan (LoadProhibited). Guarded:
|
|
438
|
+
// if allocation ever fails, the config service is inert.
|
|
439
|
+
uint8_t *configDoc_ = nullptr; // app-published blob (portal READ)
|
|
440
|
+
int configDocLen_ = 0;
|
|
441
|
+
int configReadCursor_ = 0; // DATA read cursor (0x01 resets)
|
|
442
|
+
|
|
443
|
+
uint8_t *configInbound_ = nullptr; // portal WRITE accumulator
|
|
444
|
+
int configInboundExpected_ = 0; // length from 0x02
|
|
445
|
+
int configInboundWrite_ = 0; // DATA write cursor
|
|
446
|
+
|
|
447
|
+
uint8_t *configPending_ = nullptr; // committed blob, app drains
|
|
448
|
+
int configPendingLen_ = 0;
|
|
449
|
+
|
|
450
|
+
// Connections that completed AUTH (0x93) since they connected; a 0x02
|
|
451
|
+
// begin-WRITE is rejected unless the writing conn is in this set.
|
|
452
|
+
static constexpr int kMaxAuthed = CONFIG_BT_NIMBLE_MAX_CONNECTIONS;
|
|
453
|
+
uint16_t configAuthed_[kMaxAuthed] = {};
|
|
454
|
+
int configAuthedN_ = 0;
|
|
455
|
+
|
|
456
|
+
// Pairing overlay state. The code is random per 0x10 and cleared on
|
|
457
|
+
// ok/fail/dismiss/timeout (~60s). Mutable so the const pairing() getter can
|
|
458
|
+
// expire it.
|
|
459
|
+
mutable bool configPairingActive_ = false;
|
|
460
|
+
char configPairCode_[5] = {}; // 4 ASCII digits + NUL
|
|
461
|
+
char configPairCodeOut_[5] = {}; // stable copy for pairCode() getter
|
|
462
|
+
mutable uint32_t configPairStartMs_ = 0;
|
|
463
|
+
|
|
464
|
+
// ---- multi-role link state (host-task callbacks vs app-task getters) ----
|
|
465
|
+
// One spinlock guards the registry, scan list and report FIFO; every
|
|
466
|
+
// critical section is a bounded copy of a few dozen bytes.
|
|
467
|
+
mutable portMUX_TYPE stateMux_ = portMUX_INITIALIZER_UNLOCKED;
|
|
468
|
+
|
|
469
|
+
struct LinkEntry {
|
|
470
|
+
uint16_t handle;
|
|
471
|
+
uint8_t kind;
|
|
472
|
+
char name[24];
|
|
473
|
+
};
|
|
474
|
+
static constexpr int kMaxLinks = 4;
|
|
475
|
+
LinkEntry links_[kMaxLinks] = {};
|
|
476
|
+
int linkCount_ = 0;
|
|
477
|
+
char linkNameOut_[24] = {};
|
|
478
|
+
|
|
479
|
+
#if GEA_BLE_CENTRAL
|
|
480
|
+
struct ScanEntry {
|
|
481
|
+
uint8_t addrType;
|
|
482
|
+
uint8_t addr[6];
|
|
483
|
+
char name[24];
|
|
484
|
+
};
|
|
485
|
+
static constexpr int kMaxScan = 16;
|
|
486
|
+
ScanMode scanMode_ = ScanMode::None;
|
|
487
|
+
ScanEntry scan_[kMaxScan] = {};
|
|
488
|
+
int scanN_ = 0;
|
|
489
|
+
char scanNameOut_[24] = {};
|
|
490
|
+
uint8_t pendingCentralKind_ = 0; // kLinkHidHosted/kLinkMidiPeer while a connect is in flight
|
|
491
|
+
char pendingName_[24] = {};
|
|
492
|
+
// Central connect retry: cheap BLE HID remotes (e.g. "Shortcut Remote")
|
|
493
|
+
// routinely fail the first LL connection with reason 0x3E (0x23E =
|
|
494
|
+
// "connection failed to be established"); retry the same address a few
|
|
495
|
+
// times before giving up.
|
|
496
|
+
ble_addr_t centralRetryAddr_{};
|
|
497
|
+
uint8_t centralRetryKind_ = 0;
|
|
498
|
+
int centralConnectAttempts_ = 0;
|
|
499
|
+
static constexpr int kMaxCentralConnectAttempts = 6;
|
|
500
|
+
// A central HID connect is deferred until the radio is free: the ESP32 fails
|
|
501
|
+
// LL establishment (reason 574 / HCI 0x3E) when it initiates a fresh link
|
|
502
|
+
// while it still services another connection. connectTo() terminates every
|
|
503
|
+
// live link, then the DISCONNECT handler fires issueCentralConnect() once the
|
|
504
|
+
// last one is gone.
|
|
505
|
+
bool centralConnectDeferred_ = false;
|
|
506
|
+
|
|
507
|
+
// BLE-MIDI central link (pedal / WIDI adapter).
|
|
508
|
+
uint16_t midiPeerConn_ = kNoConn;
|
|
509
|
+
uint16_t midiSvcStart_ = 0;
|
|
510
|
+
uint16_t midiSvcEnd_ = 0;
|
|
511
|
+
uint16_t midiPeerValHandle_ = 0;
|
|
512
|
+
uint16_t midiPeerCccd_ = 0;
|
|
513
|
+
|
|
514
|
+
// Hosted HID peripheral link (XPPen ACK05).
|
|
515
|
+
struct HostReportChr {
|
|
516
|
+
uint16_t defHandle;
|
|
517
|
+
uint16_t valHandle;
|
|
518
|
+
uint16_t endHandle;
|
|
519
|
+
uint16_t cccdHandle;
|
|
520
|
+
uint16_t rptRefHandle;
|
|
521
|
+
uint8_t reportId;
|
|
522
|
+
uint8_t reportType; // 1=input 2=output 3=feature (from the 0x2908 descriptor)
|
|
523
|
+
bool subscribed;
|
|
524
|
+
};
|
|
525
|
+
static constexpr int kMaxHostReports = 8;
|
|
526
|
+
uint16_t hidHostConn_ = kNoConn;
|
|
527
|
+
uint16_t hidSvcStart_ = 0;
|
|
528
|
+
uint16_t hidSvcEnd_ = 0;
|
|
529
|
+
uint16_t hidProtoModeHandle_ = 0;
|
|
530
|
+
HostReportChr hostReports_[kMaxHostReports] = {};
|
|
531
|
+
int hostReportN_ = 0;
|
|
532
|
+
int hostDiscIdx_ = 0; // report whose descriptors are being walked
|
|
533
|
+
int hostSubscribed_ = 0;
|
|
534
|
+
bool hostRedoAfterEnc_ = false; // a secure op failed pre-bond; retry on ENC_CHANGE
|
|
535
|
+
|
|
536
|
+
// Raw input-report FIFO (oldest first, drop-oldest on overflow).
|
|
537
|
+
struct HostReportMsg {
|
|
538
|
+
uint8_t id;
|
|
539
|
+
uint8_t len;
|
|
540
|
+
uint8_t data[16];
|
|
541
|
+
};
|
|
542
|
+
static constexpr int kFifoSize = 32;
|
|
543
|
+
HostReportMsg fifo_[kFifoSize] = {};
|
|
544
|
+
int fifoHead_ = 0;
|
|
545
|
+
int fifoCount_ = 0;
|
|
546
|
+
#endif
|
|
547
|
+
|
|
548
|
+
// Arguments for the transient internal-RAM-stacked worker that performs the
|
|
549
|
+
// flash-touching first-time BLE bring-up off the PSRAM-stacked gea_init task
|
|
550
|
+
// (see init()).
|
|
551
|
+
struct BleFirstInitWork {
|
|
552
|
+
HidServer *self;
|
|
553
|
+
const char *name;
|
|
554
|
+
uint16_t appearance;
|
|
555
|
+
const char *mac;
|
|
556
|
+
SemaphoreHandle_t done;
|
|
557
|
+
};
|
|
558
|
+
static void bleFirstInitTrampoline(void *arg);
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const char HidServer::kTag[] = "gea_embedded_ble";
|
|
562
|
+
|
|
563
|
+
const ble_uuid16_t HidServer::kHidServiceUuid = BLE_UUID16_INIT(hid::kServiceUuid);
|
|
564
|
+
const ble_uuid16_t HidServer::kHidInformationUuid = BLE_UUID16_INIT(hid::kInformationUuid);
|
|
565
|
+
const ble_uuid16_t HidServer::kReportMapUuid = BLE_UUID16_INIT(hid::kReportMapUuid);
|
|
566
|
+
const ble_uuid16_t HidServer::kHidControlPointUuid = BLE_UUID16_INIT(hid::kControlPointUuid);
|
|
567
|
+
const ble_uuid16_t HidServer::kProtocolModeUuid = BLE_UUID16_INIT(hid::kProtocolModeUuid);
|
|
568
|
+
const ble_uuid16_t HidServer::kReportUuid = BLE_UUID16_INIT(hid::kReportUuid);
|
|
569
|
+
const ble_uuid16_t HidServer::kReportReferenceUuid = BLE_UUID16_INIT(hid::kReportReferenceUuid);
|
|
570
|
+
const ble_uuid16_t HidServer::kBatteryServiceUuid = BLE_UUID16_INIT(hid::kBatteryServiceUuid);
|
|
571
|
+
const ble_uuid16_t HidServer::kBatteryLevelUuid = BLE_UUID16_INIT(hid::kBatteryLevelUuid);
|
|
572
|
+
|
|
573
|
+
// Air-mouse simulator IMU stream (custom 128-bit, little-endian byte order):
|
|
574
|
+
// service 19B10000-E8F2-537E-4F6C-D104768A1214
|
|
575
|
+
// char 19B10001-E8F2-537E-4F6C-D104768A1214 (READ + NOTIFY, unencrypted)
|
|
576
|
+
// Notify payload = 16 bytes LE: u32 tms, then 6×i16 (ax,ay,az ×100 m/s²; gx,gy,gz ×10 °/s).
|
|
577
|
+
const ble_uuid128_t HidServer::kImuServiceUuid = BLE_UUID128_INIT(
|
|
578
|
+
0x14, 0x12, 0x8a, 0x76, 0x04, 0xd1, 0x6c, 0x4f,
|
|
579
|
+
0x7e, 0x53, 0xf2, 0xe8, 0x00, 0x00, 0xb1, 0x19);
|
|
580
|
+
const ble_uuid128_t HidServer::kImuCharUuid = BLE_UUID128_INIT(
|
|
581
|
+
0x14, 0x12, 0x8a, 0x76, 0x04, 0xd1, 0x6c, 0x4f,
|
|
582
|
+
0x7e, 0x53, 0xf2, 0xe8, 0x01, 0x00, 0xb1, 0x19);
|
|
583
|
+
|
|
584
|
+
// BLE-MIDI (MIDI over GATT), bytes little-endian as transmitted on-air:
|
|
585
|
+
// service 03B80E5A-EDE8-4B33-A751-6CE34EC4C700
|
|
586
|
+
// I/O char 7772E5DB-3868-4112-A1A9-F2669D106BF3 (read empty + write-no-rsp + notify)
|
|
587
|
+
const ble_uuid128_t HidServer::kMidiServiceUuid = BLE_UUID128_INIT(
|
|
588
|
+
0x00, 0xc7, 0xc4, 0x4e, 0xe3, 0x6c, 0x51, 0xa7,
|
|
589
|
+
0x33, 0x4b, 0xe8, 0xed, 0x5a, 0x0e, 0xb8, 0x03);
|
|
590
|
+
const ble_uuid128_t HidServer::kMidiCharUuid = BLE_UUID128_INIT(
|
|
591
|
+
0xf3, 0x6b, 0x10, 0x9d, 0x66, 0xf2, 0xa9, 0xa1,
|
|
592
|
+
0x12, 0x41, 0x68, 0x38, 0xdb, 0xe5, 0x72, 0x77);
|
|
593
|
+
|
|
594
|
+
// Config service (custom 128-bit), bytes little-endian as BLE_UUID128_INIT
|
|
595
|
+
// wants (reverse of the printed UUID):
|
|
596
|
+
// service A1E0F000-1B2C-4D3E-9F80-1234567890AB
|
|
597
|
+
// CTRL A1E0F001-... (write-with-rsp + notify: command/status channel)
|
|
598
|
+
// DATA A1E0F002-... (read + write-no-rsp: chunked blob transfer)
|
|
599
|
+
const ble_uuid128_t HidServer::kConfigServiceUuid = BLE_UUID128_INIT(
|
|
600
|
+
0xab, 0x90, 0x78, 0x56, 0x34, 0x12, 0x80, 0x9f,
|
|
601
|
+
0x3e, 0x4d, 0x2c, 0x1b, 0x00, 0xf0, 0xe0, 0xa1);
|
|
602
|
+
const ble_uuid128_t HidServer::kConfigCtrlUuid = BLE_UUID128_INIT(
|
|
603
|
+
0xab, 0x90, 0x78, 0x56, 0x34, 0x12, 0x80, 0x9f,
|
|
604
|
+
0x3e, 0x4d, 0x2c, 0x1b, 0x01, 0xf0, 0xe0, 0xa1);
|
|
605
|
+
const ble_uuid128_t HidServer::kConfigDataUuid = BLE_UUID128_INIT(
|
|
606
|
+
0xab, 0x90, 0x78, 0x56, 0x34, 0x12, 0x80, 0x9f,
|
|
607
|
+
0x3e, 0x4d, 0x2c, 0x1b, 0x02, 0xf0, 0xe0, 0xa1);
|
|
608
|
+
|
|
609
|
+
// Geastack BLE OTA service. The CONTROL characteristic accepts BEGIN (0x01 +
|
|
610
|
+
// u32 image bytes), FINISH (0x02), and ABORT (0x03). Its notifications are
|
|
611
|
+
// opcode|0x80, result, received-u32, expected-u32. DATA is an unacknowledged,
|
|
612
|
+
// flow-controlled stream written directly to the inactive ESP-IDF OTA
|
|
613
|
+
// partition. CONTROL alone acknowledges the begin and finish boundaries.
|
|
614
|
+
// service 7F2E1001-6D8F-4A4F-A0E9-5B8892140001
|
|
615
|
+
// control 7F2E1001-6D8F-4A4F-A0E9-5B8892140002
|
|
616
|
+
// data 7F2E1001-6D8F-4A4F-A0E9-5B8892140003
|
|
617
|
+
const ble_uuid128_t HidServer::kOtaServiceUuid = BLE_UUID128_INIT(
|
|
618
|
+
0x01, 0x00, 0x14, 0x92, 0x88, 0x5b, 0xe9, 0xa0,
|
|
619
|
+
0x4f, 0x4a, 0x8f, 0x6d, 0x01, 0x10, 0x2e, 0x7f);
|
|
620
|
+
const ble_uuid128_t HidServer::kOtaCtrlUuid = BLE_UUID128_INIT(
|
|
621
|
+
0x02, 0x00, 0x14, 0x92, 0x88, 0x5b, 0xe9, 0xa0,
|
|
622
|
+
0x4f, 0x4a, 0x8f, 0x6d, 0x01, 0x10, 0x2e, 0x7f);
|
|
623
|
+
const ble_uuid128_t HidServer::kOtaDataUuid = BLE_UUID128_INIT(
|
|
624
|
+
0x03, 0x00, 0x14, 0x92, 0x88, 0x5b, 0xe9, 0xa0,
|
|
625
|
+
0x4f, 0x4a, 0x8f, 0x6d, 0x01, 0x10, 0x2e, 0x7f);
|
|
626
|
+
|
|
627
|
+
#if GEA_BLE_CENTRAL
|
|
628
|
+
namespace {
|
|
629
|
+
// ATT security errors surface as BLE_HS_ERR_ATT_BASE + att code. A HID
|
|
630
|
+
// peripheral commonly rejects report-reference reads / CCCD writes until the
|
|
631
|
+
// link encrypts; those ops re-run after ENC_CHANGE.
|
|
632
|
+
bool attSecurityError(int status)
|
|
633
|
+
{
|
|
634
|
+
return status == BLE_HS_ERR_ATT_BASE + BLE_ATT_ERR_INSUFFICIENT_AUTHEN ||
|
|
635
|
+
status == BLE_HS_ERR_ATT_BASE + BLE_ATT_ERR_INSUFFICIENT_ENC ||
|
|
636
|
+
status == BLE_HS_ERR_ATT_BASE + BLE_ATT_ERR_INSUFFICIENT_AUTHOR;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// Lenient initiator parameters for cheap / finicky HID remotes (e.g. the
|
|
640
|
+
// "Shortcut Remote"). Passing nullptr to ble_gap_connect uses NimBLE's stock
|
|
641
|
+
// initiate params, whose narrow scan window and short (~720 ms) supervision
|
|
642
|
+
// timeout let these remotes fail LL establishment with HCI 0x3E ("Connection
|
|
643
|
+
// Failed to be Established" -> reason 0x23E / 574). A continuous 60 ms scan
|
|
644
|
+
// window catches the advertiser reliably, and a 6 s supervision timeout keeps
|
|
645
|
+
// the fragile link alive through the first few connection events. CE lengths
|
|
646
|
+
// stay 0 = controller default. Interval/timeout in their native BLE units
|
|
647
|
+
// (itvl 1.25 ms, timeout 10 ms, scan 0.625 ms).
|
|
648
|
+
const struct ble_gap_conn_params kCentralConnParams = {
|
|
649
|
+
/* scan_itvl */ 0x0060, // 96 * 0.625 ms = 60 ms
|
|
650
|
+
/* scan_window */ 0x0060, // 96 * 0.625 ms = 60 ms (continuous while initiating)
|
|
651
|
+
/* itvl_min */ 0x0018, // 24 * 1.25 ms = 30 ms
|
|
652
|
+
/* itvl_max */ 0x0028, // 40 * 1.25 ms = 50 ms
|
|
653
|
+
/* latency */ 0x0000,
|
|
654
|
+
/* supervision_timeout */ 0x0258, // 600 * 10 ms = 6000 ms
|
|
655
|
+
/* min_ce_len */ 0x0000,
|
|
656
|
+
/* max_ce_len */ 0x0000,
|
|
657
|
+
};
|
|
658
|
+
} // namespace
|
|
659
|
+
#endif
|
|
660
|
+
|
|
661
|
+
HidServer::HidServer()
|
|
662
|
+
{
|
|
663
|
+
std::snprintf(deviceName_, sizeof(deviceName_), "%s", hid::kDefaultDeviceName);
|
|
664
|
+
configureGattTable();
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
void HidServer::configureGattTable()
|
|
668
|
+
{
|
|
669
|
+
keyboardReportDescriptors_[0] = {
|
|
670
|
+
.uuid = &kReportReferenceUuid.u,
|
|
671
|
+
.att_flags = BLE_ATT_F_READ,
|
|
672
|
+
.min_key_size = 0,
|
|
673
|
+
.access_cb = hidAccessCallback,
|
|
674
|
+
.arg = accessArg(HidAccessId::KeyboardReportReference),
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
mouseReportDescriptors_[0] = {
|
|
678
|
+
.uuid = &kReportReferenceUuid.u,
|
|
679
|
+
.att_flags = BLE_ATT_F_READ,
|
|
680
|
+
.min_key_size = 0,
|
|
681
|
+
.access_cb = hidAccessCallback,
|
|
682
|
+
.arg = accessArg(HidAccessId::MouseReportReference),
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
hidCharacteristics_[0] = {
|
|
686
|
+
.uuid = &kHidInformationUuid.u,
|
|
687
|
+
.access_cb = hidAccessCallback,
|
|
688
|
+
.arg = accessArg(HidAccessId::HidInformation),
|
|
689
|
+
.descriptors = nullptr,
|
|
690
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
|
|
691
|
+
};
|
|
692
|
+
hidCharacteristics_[1] = {
|
|
693
|
+
.uuid = &kReportMapUuid.u,
|
|
694
|
+
.access_cb = hidAccessCallback,
|
|
695
|
+
.arg = accessArg(HidAccessId::ReportMap),
|
|
696
|
+
.descriptors = nullptr,
|
|
697
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
|
|
698
|
+
};
|
|
699
|
+
hidCharacteristics_[2] = {
|
|
700
|
+
.uuid = &kHidControlPointUuid.u,
|
|
701
|
+
.access_cb = hidAccessCallback,
|
|
702
|
+
.arg = accessArg(HidAccessId::ControlPoint),
|
|
703
|
+
.descriptors = nullptr,
|
|
704
|
+
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_WRITE_ENC,
|
|
705
|
+
};
|
|
706
|
+
hidCharacteristics_[3] = {
|
|
707
|
+
.uuid = &kProtocolModeUuid.u,
|
|
708
|
+
.access_cb = hidAccessCallback,
|
|
709
|
+
.arg = accessArg(HidAccessId::ProtocolMode),
|
|
710
|
+
.descriptors = nullptr,
|
|
711
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC
|
|
712
|
+
| BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_WRITE_ENC,
|
|
713
|
+
};
|
|
714
|
+
hidCharacteristics_[4] = {
|
|
715
|
+
.uuid = &kReportUuid.u,
|
|
716
|
+
.access_cb = hidAccessCallback,
|
|
717
|
+
.arg = accessArg(HidAccessId::KeyboardReport),
|
|
718
|
+
.descriptors = keyboardReportDescriptors_,
|
|
719
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
|
720
|
+
.min_key_size = 0,
|
|
721
|
+
.val_handle = &keyboardAttrHandle_,
|
|
722
|
+
};
|
|
723
|
+
hidCharacteristics_[5] = {
|
|
724
|
+
.uuid = &kReportUuid.u,
|
|
725
|
+
.access_cb = hidAccessCallback,
|
|
726
|
+
.arg = accessArg(HidAccessId::MouseReport),
|
|
727
|
+
.descriptors = mouseReportDescriptors_,
|
|
728
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
|
729
|
+
.min_key_size = 0,
|
|
730
|
+
.val_handle = &mouseAttrHandle_,
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
batteryCharacteristics_[0] = {
|
|
734
|
+
.uuid = &kBatteryLevelUuid.u,
|
|
735
|
+
.access_cb = batteryAccessCallback,
|
|
736
|
+
.arg = nullptr,
|
|
737
|
+
.descriptors = nullptr,
|
|
738
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC | BLE_GATT_CHR_F_NOTIFY,
|
|
739
|
+
.min_key_size = 0,
|
|
740
|
+
.val_handle = &batteryAttrHandle_,
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
// Custom IMU stream characteristic — READ + NOTIFY, intentionally UNENCRYPTED so the
|
|
744
|
+
// browser sim can subscribe over Web Bluetooth without bonding (the HID chars stay
|
|
745
|
+
// encrypted for the OS). Notify-only payload, served on read from imuLast_.
|
|
746
|
+
imuCharacteristics_[0] = {
|
|
747
|
+
.uuid = &kImuCharUuid.u,
|
|
748
|
+
.access_cb = imuAccessCallback,
|
|
749
|
+
.arg = nullptr,
|
|
750
|
+
.descriptors = nullptr,
|
|
751
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
|
752
|
+
.min_key_size = 0,
|
|
753
|
+
.val_handle = &imuAttrHandle_,
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
gattServices_[0] = {
|
|
757
|
+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
758
|
+
.uuid = &kHidServiceUuid.u,
|
|
759
|
+
.includes = nullptr,
|
|
760
|
+
.characteristics = hidCharacteristics_,
|
|
761
|
+
};
|
|
762
|
+
gattServices_[1] = {
|
|
763
|
+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
764
|
+
.uuid = &kBatteryServiceUuid.u,
|
|
765
|
+
.includes = nullptr,
|
|
766
|
+
.characteristics = batteryCharacteristics_,
|
|
767
|
+
};
|
|
768
|
+
// BLE-MIDI I/O characteristic — read (returns empty per the BLE-MIDI spec) +
|
|
769
|
+
// write-without-response (inbound MIDI) + notify (outbound to a subscribed
|
|
770
|
+
// DAW). Unencrypted like the IMU char so a MIDI host connects without bonding.
|
|
771
|
+
midiCharacteristics_[0] = {
|
|
772
|
+
.uuid = &kMidiCharUuid.u,
|
|
773
|
+
.access_cb = midiAccessCallback,
|
|
774
|
+
.arg = nullptr,
|
|
775
|
+
.descriptors = nullptr,
|
|
776
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_NOTIFY,
|
|
777
|
+
.min_key_size = 0,
|
|
778
|
+
.val_handle = &midiAttrHandle_,
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
// Registered AFTER hid/battery so their attribute handles stay stable (the bonded
|
|
782
|
+
// presentation-clicker host keeps working); the IMU service is purely additive.
|
|
783
|
+
gattServices_[2] = {
|
|
784
|
+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
785
|
+
.uuid = &kImuServiceUuid.u,
|
|
786
|
+
.includes = nullptr,
|
|
787
|
+
.characteristics = imuCharacteristics_,
|
|
788
|
+
};
|
|
789
|
+
// MIDI last, same additive rule: every handle in front of it is unchanged.
|
|
790
|
+
// Registration is unconditional (re-registering NimBLE services after init
|
|
791
|
+
// is not supported); midiEnabled_ gates advertising + app behavior instead.
|
|
792
|
+
gattServices_[3] = {
|
|
793
|
+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
794
|
+
.uuid = &kMidiServiceUuid.u,
|
|
795
|
+
.includes = nullptr,
|
|
796
|
+
.characteristics = midiCharacteristics_,
|
|
797
|
+
};
|
|
798
|
+
|
|
799
|
+
// Config service — CTRL (write-with-response + notify: opcode/status
|
|
800
|
+
// channel) and DATA (read + write-no-response: chunked blob transfer).
|
|
801
|
+
// Unencrypted so a Web Bluetooth portal connects without bonding; write
|
|
802
|
+
// access is gated by the 4-digit pairing code, not link encryption.
|
|
803
|
+
configCharacteristics_[0] = {
|
|
804
|
+
.uuid = &kConfigCtrlUuid.u,
|
|
805
|
+
.access_cb = configCtrlAccessCallback,
|
|
806
|
+
.arg = nullptr,
|
|
807
|
+
.descriptors = nullptr,
|
|
808
|
+
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
|
|
809
|
+
.min_key_size = 0,
|
|
810
|
+
.val_handle = &configCtrlAttrHandle_,
|
|
811
|
+
};
|
|
812
|
+
configCharacteristics_[1] = {
|
|
813
|
+
.uuid = &kConfigDataUuid.u,
|
|
814
|
+
.access_cb = configDataAccessCallback,
|
|
815
|
+
.arg = nullptr,
|
|
816
|
+
.descriptors = nullptr,
|
|
817
|
+
// F_WRITE (with response) added alongside F_WRITE_NO_RSP: the portal must
|
|
818
|
+
// write DATA chunks WITH response so each chunk is acknowledged before the
|
|
819
|
+
// next and, crucially, before the CTRL COMMIT (0x03). With write-no-response
|
|
820
|
+
// the browser resolves once the chunk is locally queued, so a COMMIT on the
|
|
821
|
+
// CTRL char can overtake the tail DATA chunks — the device then commits with
|
|
822
|
+
// configInboundWrite_ < configInboundExpected_ and rejects with code 3
|
|
823
|
+
// ("parse failed"), which is really a length mismatch. Kept F_WRITE_NO_RSP
|
|
824
|
+
// for back-compat with older portal builds.
|
|
825
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP,
|
|
826
|
+
.min_key_size = 0,
|
|
827
|
+
.val_handle = &configDataAttrHandle_,
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
// Config last, same additive rule: every handle ahead of it is unchanged.
|
|
831
|
+
gattServices_[4] = {
|
|
832
|
+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
833
|
+
.uuid = &kConfigServiceUuid.u,
|
|
834
|
+
.includes = nullptr,
|
|
835
|
+
.characteristics = configCharacteristics_,
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
#ifdef GEA_EMBEDDED_BLE_OTA
|
|
839
|
+
otaCharacteristics_[0] = {
|
|
840
|
+
.uuid = &kOtaCtrlUuid.u,
|
|
841
|
+
.access_cb = otaCtrlAccessCallback,
|
|
842
|
+
.arg = nullptr,
|
|
843
|
+
.descriptors = nullptr,
|
|
844
|
+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC
|
|
845
|
+
| BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC
|
|
846
|
+
| BLE_GATT_CHR_F_NOTIFY,
|
|
847
|
+
.min_key_size = 0,
|
|
848
|
+
.val_handle = &otaCtrlAttrHandle_,
|
|
849
|
+
};
|
|
850
|
+
otaCharacteristics_[1] = {
|
|
851
|
+
.uuid = &kOtaDataUuid.u,
|
|
852
|
+
.access_cb = otaDataAccessCallback,
|
|
853
|
+
.arg = nullptr,
|
|
854
|
+
.descriptors = nullptr,
|
|
855
|
+
// OTA data has no per-chunk application acknowledgement. CoreBluetooth
|
|
856
|
+
// and NimBLE apply link-level flow control; CONTROL acknowledges only
|
|
857
|
+
// BEGIN and FINISH. Advertising WRITE here lets clients accidentally pick
|
|
858
|
+
// the one-round-trip-per-chunk path and turns a firmware update into a
|
|
859
|
+
// many-minute transfer.
|
|
860
|
+
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_WRITE_ENC,
|
|
861
|
+
.min_key_size = 0,
|
|
862
|
+
.val_handle = &otaDataAttrHandle_,
|
|
863
|
+
};
|
|
864
|
+
gattServices_[5] = {
|
|
865
|
+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
866
|
+
.uuid = &kOtaServiceUuid.u,
|
|
867
|
+
.includes = nullptr,
|
|
868
|
+
.characteristics = otaCharacteristics_,
|
|
869
|
+
};
|
|
870
|
+
#endif
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
int HidServer::handleHidAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
874
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
875
|
+
{
|
|
876
|
+
(void)conn_handle;
|
|
877
|
+
(void)attr_handle;
|
|
878
|
+
const auto id = static_cast<HidAccessId>(reinterpret_cast<intptr_t>(arg));
|
|
879
|
+
int rc;
|
|
880
|
+
|
|
881
|
+
switch (id) {
|
|
882
|
+
case HidAccessId::HidInformation:
|
|
883
|
+
rc = os_mbuf_append(ctxt->om, hid::kInformation, sizeof(hid::kInformation));
|
|
884
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
885
|
+
|
|
886
|
+
case HidAccessId::ReportMap:
|
|
887
|
+
rc = os_mbuf_append(ctxt->om, hid::kReportMap, sizeof(hid::kReportMap));
|
|
888
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
889
|
+
|
|
890
|
+
case HidAccessId::ControlPoint:
|
|
891
|
+
return 0;
|
|
892
|
+
|
|
893
|
+
case HidAccessId::ProtocolMode:
|
|
894
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
895
|
+
rc = os_mbuf_append(ctxt->om, &protocolMode_, 1);
|
|
896
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
897
|
+
}
|
|
898
|
+
return 0;
|
|
899
|
+
|
|
900
|
+
case HidAccessId::KeyboardReport:
|
|
901
|
+
{
|
|
902
|
+
const auto report = reports_.keyboard();
|
|
903
|
+
rc = os_mbuf_append(ctxt->om, report.data, report.size);
|
|
904
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
case HidAccessId::MouseReport:
|
|
908
|
+
{
|
|
909
|
+
const auto report = reports_.mouse();
|
|
910
|
+
rc = os_mbuf_append(ctxt->om, report.data, report.size);
|
|
911
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
case HidAccessId::KeyboardReportReference:
|
|
915
|
+
rc = os_mbuf_append(ctxt->om, hid::kKeyboardReportReference, sizeof(hid::kKeyboardReportReference));
|
|
916
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
917
|
+
|
|
918
|
+
case HidAccessId::MouseReportReference:
|
|
919
|
+
rc = os_mbuf_append(ctxt->om, hid::kMouseReportReference, sizeof(hid::kMouseReportReference));
|
|
920
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
return BLE_ATT_ERR_UNLIKELY;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
int HidServer::handleBatteryAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
927
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
928
|
+
{
|
|
929
|
+
(void)conn_handle;
|
|
930
|
+
(void)attr_handle;
|
|
931
|
+
(void)arg;
|
|
932
|
+
|
|
933
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
934
|
+
int rc = os_mbuf_append(ctxt->om, &batteryLevel_, 1);
|
|
935
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
936
|
+
}
|
|
937
|
+
return BLE_ATT_ERR_UNLIKELY;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
int HidServer::handleImuAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
941
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
942
|
+
{
|
|
943
|
+
(void)conn_handle;
|
|
944
|
+
(void)attr_handle;
|
|
945
|
+
(void)arg;
|
|
946
|
+
|
|
947
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
948
|
+
int rc = os_mbuf_append(ctxt->om, imuLast_, sizeof(imuLast_));
|
|
949
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
950
|
+
}
|
|
951
|
+
return BLE_ATT_ERR_UNLIKELY;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
int HidServer::handleMidiAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
955
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
956
|
+
{
|
|
957
|
+
(void)conn_handle;
|
|
958
|
+
(void)attr_handle;
|
|
959
|
+
(void)arg;
|
|
960
|
+
|
|
961
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
962
|
+
return 0; // BLE-MIDI reads return no payload
|
|
963
|
+
}
|
|
964
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
|
965
|
+
// Inbound BLE-MIDI from a connected DAW: no consumer yet — absorb.
|
|
966
|
+
return 0;
|
|
967
|
+
}
|
|
968
|
+
return BLE_ATT_ERR_UNLIKELY;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
// ---- config service --------------------------------------------------------
|
|
972
|
+
|
|
973
|
+
void HidServer::configCtrlNotify(uint16_t conn, const uint8_t *data, int len)
|
|
974
|
+
{
|
|
975
|
+
if (conn == kNoConn || configCtrlAttrHandle_ == 0 || len <= 0) return;
|
|
976
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(data, len);
|
|
977
|
+
if (om) ble_gatts_notify_custom(conn, configCtrlAttrHandle_, om);
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
bool HidServer::configIsAuthed(uint16_t conn) const
|
|
981
|
+
{
|
|
982
|
+
for (int i = 0; i < configAuthedN_; i++) {
|
|
983
|
+
if (configAuthed_[i] == conn) return true;
|
|
984
|
+
}
|
|
985
|
+
return false;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
void HidServer::configAddAuthed(uint16_t conn)
|
|
989
|
+
{
|
|
990
|
+
portENTER_CRITICAL(&stateMux_);
|
|
991
|
+
bool present = false;
|
|
992
|
+
for (int i = 0; i < configAuthedN_; i++) {
|
|
993
|
+
if (configAuthed_[i] == conn) { present = true; break; }
|
|
994
|
+
}
|
|
995
|
+
if (!present && configAuthedN_ < kMaxAuthed) configAuthed_[configAuthedN_++] = conn;
|
|
996
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
void HidServer::configClearConn(uint16_t conn)
|
|
1000
|
+
{
|
|
1001
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1002
|
+
for (int i = 0; i < configAuthedN_; i++) {
|
|
1003
|
+
if (configAuthed_[i] != conn) continue;
|
|
1004
|
+
for (int j = i; j + 1 < configAuthedN_ && j + 1 < kMaxAuthed; j++) {
|
|
1005
|
+
configAuthed_[j] = configAuthed_[j + 1];
|
|
1006
|
+
}
|
|
1007
|
+
configAuthedN_--;
|
|
1008
|
+
break;
|
|
1009
|
+
}
|
|
1010
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
int HidServer::handleConfigCtrlAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
1014
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
1015
|
+
{
|
|
1016
|
+
(void)attr_handle;
|
|
1017
|
+
(void)arg;
|
|
1018
|
+
if (ctxt->op != BLE_GATT_ACCESS_OP_WRITE_CHR) return BLE_ATT_ERR_UNLIKELY;
|
|
1019
|
+
|
|
1020
|
+
uint8_t in[8] = {};
|
|
1021
|
+
uint16_t inLen = 0;
|
|
1022
|
+
ble_hs_mbuf_to_flat(ctxt->om, in, sizeof(in), &inLen);
|
|
1023
|
+
if (inLen < 1) return 0;
|
|
1024
|
+
|
|
1025
|
+
uint8_t note[3] = {};
|
|
1026
|
+
int noteLen = 0;
|
|
1027
|
+
|
|
1028
|
+
switch (in[0]) {
|
|
1029
|
+
case 0x01: { // begin READ: reset DATA read cursor, report current length
|
|
1030
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1031
|
+
configReadCursor_ = 0;
|
|
1032
|
+
const int len = configDocLen_;
|
|
1033
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1034
|
+
note[0] = 0x81;
|
|
1035
|
+
note[1] = static_cast<uint8_t>((len >> 8) & 0xFF);
|
|
1036
|
+
note[2] = static_cast<uint8_t>(len & 0xFF);
|
|
1037
|
+
noteLen = 3;
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
case 0x02: { // begin WRITE: allocate inbound length (gated by AUTH)
|
|
1041
|
+
if (inLen < 3) { note[0] = 0x84; note[1] = 2; noteLen = 2; break; }
|
|
1042
|
+
bool authed;
|
|
1043
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1044
|
+
authed = configIsAuthed(conn_handle);
|
|
1045
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1046
|
+
if (!authed) { note[0] = 0x84; note[1] = 1; noteLen = 2; break; }
|
|
1047
|
+
const int len = (in[1] << 8) | in[2];
|
|
1048
|
+
if (len <= 0 || len > kConfigDocMax) { note[0] = 0x84; note[1] = 2; noteLen = 2; break; }
|
|
1049
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1050
|
+
configInboundExpected_ = len;
|
|
1051
|
+
configInboundWrite_ = 0;
|
|
1052
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1053
|
+
// No status for 0x02: the portal streams DATA, then commits with 0x03.
|
|
1054
|
+
break;
|
|
1055
|
+
}
|
|
1056
|
+
case 0x03: { // COMMIT: validate length, hand inbound to the app-pending buffer
|
|
1057
|
+
bool authed;
|
|
1058
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1059
|
+
authed = configIsAuthed(conn_handle);
|
|
1060
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1061
|
+
if (!authed) { note[0] = 0x84; note[1] = 1; noteLen = 2; break; }
|
|
1062
|
+
bool ok = false;
|
|
1063
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1064
|
+
if (configInboundExpected_ > 0 && configInboundWrite_ == configInboundExpected_ && configPending_ && configInbound_) {
|
|
1065
|
+
std::memcpy(configPending_, configInbound_, configInboundExpected_);
|
|
1066
|
+
configPendingLen_ = configInboundExpected_;
|
|
1067
|
+
ok = true;
|
|
1068
|
+
}
|
|
1069
|
+
configInboundExpected_ = 0;
|
|
1070
|
+
configInboundWrite_ = 0;
|
|
1071
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1072
|
+
if (ok) { note[0] = 0x83; noteLen = 1; }
|
|
1073
|
+
else { note[0] = 0x84; note[1] = 3; noteLen = 2; } // 3 = parse/length fail
|
|
1074
|
+
break;
|
|
1075
|
+
}
|
|
1076
|
+
case 0x10: { // AUTH begin: generate a random 4-digit code, show the overlay
|
|
1077
|
+
const uint32_t code = esp_random() % 10000u;
|
|
1078
|
+
char codeBuf[5];
|
|
1079
|
+
std::snprintf(codeBuf, sizeof(codeBuf), "%04u", static_cast<unsigned>(code));
|
|
1080
|
+
const uint32_t nowMs = static_cast<uint32_t>(esp_timer_get_time() / 1000);
|
|
1081
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1082
|
+
std::memcpy(configPairCode_, codeBuf, sizeof(codeBuf));
|
|
1083
|
+
configPairingActive_ = true;
|
|
1084
|
+
configPairStartMs_ = nowMs;
|
|
1085
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1086
|
+
ESP_LOGI(kTag, "config: pairing code %s", codeBuf);
|
|
1087
|
+
note[0] = 0x91;
|
|
1088
|
+
noteLen = 1;
|
|
1089
|
+
break;
|
|
1090
|
+
}
|
|
1091
|
+
case 0x12: { // AUTH submit: verify the 4 ASCII digits
|
|
1092
|
+
bool ok = false;
|
|
1093
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1094
|
+
if (inLen >= 5 && configPairingActive_ &&
|
|
1095
|
+
in[1] == static_cast<uint8_t>(configPairCode_[0]) &&
|
|
1096
|
+
in[2] == static_cast<uint8_t>(configPairCode_[1]) &&
|
|
1097
|
+
in[3] == static_cast<uint8_t>(configPairCode_[2]) &&
|
|
1098
|
+
in[4] == static_cast<uint8_t>(configPairCode_[3])) {
|
|
1099
|
+
ok = true;
|
|
1100
|
+
}
|
|
1101
|
+
configPairingActive_ = false; // cleared on ok OR fail
|
|
1102
|
+
configPairCode_[0] = '\0';
|
|
1103
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1104
|
+
if (ok) configAddAuthed(conn_handle);
|
|
1105
|
+
note[0] = ok ? 0x93 : 0x94;
|
|
1106
|
+
noteLen = 1;
|
|
1107
|
+
break;
|
|
1108
|
+
}
|
|
1109
|
+
default:
|
|
1110
|
+
return 0;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
configCtrlNotify(conn_handle, note, noteLen);
|
|
1114
|
+
return 0;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
int HidServer::handleConfigDataAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
1118
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
1119
|
+
{
|
|
1120
|
+
(void)attr_handle;
|
|
1121
|
+
(void)arg;
|
|
1122
|
+
|
|
1123
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
1124
|
+
// Serve the next chunk of the config doc, capped at min(ATT_MTU-3, 20).
|
|
1125
|
+
const int mtu = ble_att_mtu(conn_handle);
|
|
1126
|
+
int cap = mtu > 3 ? mtu - 3 : 20;
|
|
1127
|
+
if (cap > 20) cap = 20;
|
|
1128
|
+
uint8_t chunk[20];
|
|
1129
|
+
int n = 0;
|
|
1130
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1131
|
+
int remaining = configDocLen_ - configReadCursor_;
|
|
1132
|
+
if (remaining < 0) remaining = 0;
|
|
1133
|
+
n = remaining < cap ? remaining : cap;
|
|
1134
|
+
if (n > 0 && configDoc_) {
|
|
1135
|
+
std::memcpy(chunk, configDoc_ + configReadCursor_, n);
|
|
1136
|
+
configReadCursor_ += n;
|
|
1137
|
+
} else {
|
|
1138
|
+
n = 0;
|
|
1139
|
+
}
|
|
1140
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1141
|
+
if (n > 0) {
|
|
1142
|
+
int rc = os_mbuf_append(ctxt->om, chunk, n);
|
|
1143
|
+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
1144
|
+
}
|
|
1145
|
+
return 0; // cursor exhausted: empty read
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
|
1149
|
+
// Append this chunk to the inbound buffer (bounded by the 0x02 length).
|
|
1150
|
+
uint8_t buf[512];
|
|
1151
|
+
uint16_t len = 0;
|
|
1152
|
+
ble_hs_mbuf_to_flat(ctxt->om, buf, sizeof(buf), &len);
|
|
1153
|
+
if (len > 0 && configInbound_) {
|
|
1154
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1155
|
+
int cursor = configInboundWrite_;
|
|
1156
|
+
for (int i = 0; i < len && cursor < kConfigDocMax && cursor < configInboundExpected_; i++) {
|
|
1157
|
+
configInbound_[cursor++] = buf[i];
|
|
1158
|
+
}
|
|
1159
|
+
configInboundWrite_ = cursor;
|
|
1160
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1161
|
+
}
|
|
1162
|
+
return 0;
|
|
1163
|
+
}
|
|
1164
|
+
return BLE_ATT_ERR_UNLIKELY;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// ---- BLE OTA service -------------------------------------------------------
|
|
1168
|
+
|
|
1169
|
+
void HidServer::otaCtrlNotify(uint16_t conn, uint8_t opcode,
|
|
1170
|
+
gea::framework::services::OtaServer::Result result)
|
|
1171
|
+
{
|
|
1172
|
+
if (conn == kNoConn || otaCtrlAttrHandle_ == 0) return;
|
|
1173
|
+
const uint32_t received = static_cast<uint32_t>(gea::framework::services::OtaServer::receivedBytes());
|
|
1174
|
+
const uint32_t expected = static_cast<uint32_t>(gea::framework::services::OtaServer::expectedBytes());
|
|
1175
|
+
uint8_t note[10] = {
|
|
1176
|
+
static_cast<uint8_t>(opcode | 0x80),
|
|
1177
|
+
static_cast<uint8_t>(result),
|
|
1178
|
+
static_cast<uint8_t>(received), static_cast<uint8_t>(received >> 8),
|
|
1179
|
+
static_cast<uint8_t>(received >> 16), static_cast<uint8_t>(received >> 24),
|
|
1180
|
+
static_cast<uint8_t>(expected), static_cast<uint8_t>(expected >> 8),
|
|
1181
|
+
static_cast<uint8_t>(expected >> 16), static_cast<uint8_t>(expected >> 24),
|
|
1182
|
+
};
|
|
1183
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(note, sizeof(note));
|
|
1184
|
+
if (om) ble_gatts_notify_custom(conn, otaCtrlAttrHandle_, om);
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
void HidServer::wifiStatusNotify(uint16_t conn)
|
|
1188
|
+
{
|
|
1189
|
+
if (conn == kNoConn || otaCtrlAttrHandle_ == 0) return;
|
|
1190
|
+
const int state = gea::targets::esp32::wifi::wifi_debug::state();
|
|
1191
|
+
const int error = gea::targets::esp32::wifi::wifi_debug::lastError();
|
|
1192
|
+
const bool enabled = gea::framework::network::wifi().enabled();
|
|
1193
|
+
const bool connected = gea::framework::network::wifi().connected();
|
|
1194
|
+
const int otaError = gea::targets::esp32::ota_debug::startError();
|
|
1195
|
+
const std::uint32_t liveDmaFree = gea::targets::esp32::ota_debug::dmaFree();
|
|
1196
|
+
// Stay within the 18-byte status notification that works even before the
|
|
1197
|
+
// central and peripheral have negotiated a larger ATT MTU. Byte 9 is a
|
|
1198
|
+
// format marker so the CLI can still decode older diagnostic firmware.
|
|
1199
|
+
const uint8_t note[18] = {
|
|
1200
|
+
0x85,
|
|
1201
|
+
static_cast<uint8_t>(state),
|
|
1202
|
+
static_cast<uint8_t>(error), static_cast<uint8_t>(error >> 8),
|
|
1203
|
+
static_cast<uint8_t>(error >> 16), static_cast<uint8_t>(error >> 24),
|
|
1204
|
+
static_cast<uint8_t>(enabled),
|
|
1205
|
+
static_cast<uint8_t>(connected),
|
|
1206
|
+
static_cast<uint8_t>(gea::targets::esp32::ota_debug::started()),
|
|
1207
|
+
0xa5,
|
|
1208
|
+
static_cast<uint8_t>(otaError), static_cast<uint8_t>(otaError >> 8),
|
|
1209
|
+
static_cast<uint8_t>(otaError >> 16), static_cast<uint8_t>(otaError >> 24),
|
|
1210
|
+
static_cast<uint8_t>(liveDmaFree), static_cast<uint8_t>(liveDmaFree >> 8),
|
|
1211
|
+
static_cast<uint8_t>(liveDmaFree >> 16), static_cast<uint8_t>(liveDmaFree >> 24),
|
|
1212
|
+
};
|
|
1213
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(note, sizeof(note));
|
|
1214
|
+
if (om) ble_gatts_notify_custom(conn, otaCtrlAttrHandle_, om);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
int HidServer::handleOtaCtrlAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
1218
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
1219
|
+
{
|
|
1220
|
+
(void)attr_handle;
|
|
1221
|
+
(void)arg;
|
|
1222
|
+
using OtaServer = gea::framework::services::OtaServer;
|
|
1223
|
+
|
|
1224
|
+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
1225
|
+
const uint32_t received = static_cast<uint32_t>(OtaServer::receivedBytes());
|
|
1226
|
+
const uint32_t expected = static_cast<uint32_t>(OtaServer::expectedBytes());
|
|
1227
|
+
const uint8_t status[10] = {
|
|
1228
|
+
0x80, static_cast<uint8_t>(OtaServer::Result::Ok),
|
|
1229
|
+
static_cast<uint8_t>(received), static_cast<uint8_t>(received >> 8),
|
|
1230
|
+
static_cast<uint8_t>(received >> 16), static_cast<uint8_t>(received >> 24),
|
|
1231
|
+
static_cast<uint8_t>(expected), static_cast<uint8_t>(expected >> 8),
|
|
1232
|
+
static_cast<uint8_t>(expected >> 16), static_cast<uint8_t>(expected >> 24),
|
|
1233
|
+
};
|
|
1234
|
+
return os_mbuf_append(ctxt->om, status, sizeof(status)) == 0
|
|
1235
|
+
? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
1236
|
+
}
|
|
1237
|
+
if (ctxt->op != BLE_GATT_ACCESS_OP_WRITE_CHR) return BLE_ATT_ERR_UNLIKELY;
|
|
1238
|
+
|
|
1239
|
+
uint8_t input[8] = {};
|
|
1240
|
+
uint16_t length = 0;
|
|
1241
|
+
if (ble_hs_mbuf_to_flat(ctxt->om, input, sizeof(input), &length) != 0 || length < 1) {
|
|
1242
|
+
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
OtaServer::Result result = OtaServer::Result::Ok;
|
|
1246
|
+
switch (input[0]) {
|
|
1247
|
+
case 0x01: {
|
|
1248
|
+
if (length != 5) return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
|
1249
|
+
// Ask the central for a tight interval while OTA owns the link. The
|
|
1250
|
+
// central remains authoritative and may clamp this request, but macOS
|
|
1251
|
+
// normally accepts it and gives the no-response stream substantially
|
|
1252
|
+
// more connection events per second.
|
|
1253
|
+
const struct ble_gap_upd_params otaParams = {
|
|
1254
|
+
.itvl_min = 6, // 7.5 ms
|
|
1255
|
+
.itvl_max = 12, // 15 ms
|
|
1256
|
+
.latency = 0,
|
|
1257
|
+
.supervision_timeout = 600, // 6 s
|
|
1258
|
+
.min_ce_len = 0,
|
|
1259
|
+
.max_ce_len = 0,
|
|
1260
|
+
};
|
|
1261
|
+
const int updateResult = ble_gap_update_params(conn_handle, &otaParams);
|
|
1262
|
+
if (updateResult != 0) ESP_LOGW(kTag, "OTA connection-parameter request failed: %d", updateResult);
|
|
1263
|
+
const int phyResult = ble_gap_set_prefered_le_phy(
|
|
1264
|
+
conn_handle, BLE_GAP_LE_PHY_2M_MASK, BLE_GAP_LE_PHY_2M_MASK, 0);
|
|
1265
|
+
if (phyResult != 0) ESP_LOGW(kTag, "OTA 2M PHY request failed: %d", phyResult);
|
|
1266
|
+
const uint32_t imageSize = static_cast<uint32_t>(input[1])
|
|
1267
|
+
| (static_cast<uint32_t>(input[2]) << 8)
|
|
1268
|
+
| (static_cast<uint32_t>(input[3]) << 16)
|
|
1269
|
+
| (static_cast<uint32_t>(input[4]) << 24);
|
|
1270
|
+
result = OtaServer::beginImage(imageSize);
|
|
1271
|
+
break;
|
|
1272
|
+
}
|
|
1273
|
+
case 0x02:
|
|
1274
|
+
result = OtaServer::finishImage();
|
|
1275
|
+
break;
|
|
1276
|
+
case 0x03:
|
|
1277
|
+
OtaServer::abortImage();
|
|
1278
|
+
break;
|
|
1279
|
+
case 0x05:
|
|
1280
|
+
wifiStatusNotify(conn_handle);
|
|
1281
|
+
return 0;
|
|
1282
|
+
default:
|
|
1283
|
+
return BLE_ATT_ERR_REQ_NOT_SUPPORTED;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
otaCtrlNotify(conn_handle, input[0], result);
|
|
1287
|
+
if (input[0] == 0x02 && result == OtaServer::Result::Ok) OtaServer::rebootSoon();
|
|
1288
|
+
return 0;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
int HidServer::handleOtaDataAccess(uint16_t conn_handle, uint16_t attr_handle,
|
|
1292
|
+
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
1293
|
+
{
|
|
1294
|
+
(void)attr_handle;
|
|
1295
|
+
(void)arg;
|
|
1296
|
+
if (ctxt->op != BLE_GATT_ACCESS_OP_WRITE_CHR) return BLE_ATT_ERR_UNLIKELY;
|
|
1297
|
+
if (OS_MBUF_PKTLEN(ctxt->om) > 512) return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
|
1298
|
+
|
|
1299
|
+
uint8_t data[512];
|
|
1300
|
+
uint16_t length = 0;
|
|
1301
|
+
if (ble_hs_mbuf_to_flat(ctxt->om, data, sizeof(data), &length) != 0 || length == 0) {
|
|
1302
|
+
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
|
1303
|
+
}
|
|
1304
|
+
const auto result = gea::framework::services::OtaServer::writeImage(data, length);
|
|
1305
|
+
if (result != gea::framework::services::OtaServer::Result::Ok) {
|
|
1306
|
+
otaCtrlNotify(conn_handle, 0x04, result);
|
|
1307
|
+
return BLE_ATT_ERR_UNLIKELY;
|
|
1308
|
+
}
|
|
1309
|
+
return 0;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
void HidServer::configSetDocument(const uint8_t *bytes, int length)
|
|
1313
|
+
{
|
|
1314
|
+
if (length < 0) length = 0;
|
|
1315
|
+
if (length > kConfigDocMax) length = kConfigDocMax;
|
|
1316
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1317
|
+
if (length > 0 && bytes && configDoc_) std::memcpy(configDoc_, bytes, length);
|
|
1318
|
+
else length = 0;
|
|
1319
|
+
configDocLen_ = length;
|
|
1320
|
+
configReadCursor_ = 0;
|
|
1321
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
int HidServer::configPendingLength() const
|
|
1325
|
+
{
|
|
1326
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1327
|
+
const int n = configPendingLen_;
|
|
1328
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1329
|
+
return n;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
int HidServer::configPendingByteAt(int index) const
|
|
1333
|
+
{
|
|
1334
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1335
|
+
const int v = (configPending_ && index >= 0 && index < configPendingLen_) ? configPending_[index] : 0;
|
|
1336
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1337
|
+
return v;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
void HidServer::configConsumePending()
|
|
1341
|
+
{
|
|
1342
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1343
|
+
configPendingLen_ = 0;
|
|
1344
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
bool HidServer::configPairing() const
|
|
1348
|
+
{
|
|
1349
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1350
|
+
if (configPairingActive_) {
|
|
1351
|
+
const uint32_t nowMs = static_cast<uint32_t>(esp_timer_get_time() / 1000);
|
|
1352
|
+
if (nowMs - configPairStartMs_ > 60000u) configPairingActive_ = false; // ~60s expiry
|
|
1353
|
+
}
|
|
1354
|
+
const bool active = configPairingActive_;
|
|
1355
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1356
|
+
return active;
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
const char *HidServer::configPairCode()
|
|
1360
|
+
{
|
|
1361
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1362
|
+
if (configPairingActive_) std::memcpy(configPairCodeOut_, configPairCode_, sizeof(configPairCodeOut_));
|
|
1363
|
+
else configPairCodeOut_[0] = '\0';
|
|
1364
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1365
|
+
return configPairCodeOut_;
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
void HidServer::configDismissPairing()
|
|
1369
|
+
{
|
|
1370
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1371
|
+
configPairingActive_ = false;
|
|
1372
|
+
configPairCode_[0] = '\0';
|
|
1373
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// App task: stash the fired control's index and hand off to the host task. On
|
|
1377
|
+
// central builds the existing command queue carries it; peripheral-only builds
|
|
1378
|
+
// have no queue, but ble_gatts_notify_custom is safe enough to issue inline
|
|
1379
|
+
// there (the config service is registered on those targets too).
|
|
1380
|
+
void HidServer::configPushActivity(int index)
|
|
1381
|
+
{
|
|
1382
|
+
reqActivityIndex_ = index;
|
|
1383
|
+
#if GEA_BLE_CENTRAL
|
|
1384
|
+
requestBleCmd();
|
|
1385
|
+
#else
|
|
1386
|
+
drainActivityPush();
|
|
1387
|
+
#endif
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// Host task: send the pending activity index to a subscribed portal as a CTRL
|
|
1391
|
+
// notification (opcode 0xA0 + index). No-op when nothing is pending or when no
|
|
1392
|
+
// portal is subscribed to the CTRL characteristic.
|
|
1393
|
+
void HidServer::drainActivityPush()
|
|
1394
|
+
{
|
|
1395
|
+
const int idx = reqActivityIndex_;
|
|
1396
|
+
reqActivityIndex_ = -1;
|
|
1397
|
+
if (idx < 0) return;
|
|
1398
|
+
if (configCtrlConn_ == kNoConn) return;
|
|
1399
|
+
uint8_t note[2] = {0xA0, static_cast<uint8_t>(idx)};
|
|
1400
|
+
configCtrlNotify(configCtrlConn_, note, 2);
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
namespace {
|
|
1404
|
+
// Clamp a scaled double to int16 for the packed IMU sample.
|
|
1405
|
+
int16_t packImu(double value, double scale)
|
|
1406
|
+
{
|
|
1407
|
+
double s = value * scale;
|
|
1408
|
+
if (s > 32767.0) s = 32767.0;
|
|
1409
|
+
if (s < -32768.0) s = -32768.0;
|
|
1410
|
+
return static_cast<int16_t>(s >= 0 ? s + 0.5 : s - 0.5);
|
|
1411
|
+
}
|
|
1412
|
+
} // namespace
|
|
1413
|
+
|
|
1414
|
+
void HidServer::imuNotifyTask(void *param)
|
|
1415
|
+
{
|
|
1416
|
+
(void)param;
|
|
1417
|
+
HidServer &self = instance();
|
|
1418
|
+
bool accelReady = false;
|
|
1419
|
+
TickType_t last = xTaskGetTickCount();
|
|
1420
|
+
for (;;) {
|
|
1421
|
+
vTaskDelayUntil(&last, pdMS_TO_TICKS(10)); // ~100 Hz cap
|
|
1422
|
+
if (self.imuConn_ == kNoConn || self.imuAttrHandle_ == 0) continue;
|
|
1423
|
+
if (!accelReady) {
|
|
1424
|
+
gea::platform::sensors::Accelerometer::init(); // idempotent; shares the QMI8658 mutex
|
|
1425
|
+
accelReady = true;
|
|
1426
|
+
}
|
|
1427
|
+
const int16_t vals[6] = {
|
|
1428
|
+
packImu(gea::platform::sensors::Accelerometer::accelerationX(), 100.0),
|
|
1429
|
+
packImu(gea::platform::sensors::Accelerometer::accelerationY(), 100.0),
|
|
1430
|
+
packImu(gea::platform::sensors::Accelerometer::accelerationZ(), 100.0),
|
|
1431
|
+
packImu(gea::platform::sensors::Accelerometer::gyroscopeX(), 10.0),
|
|
1432
|
+
packImu(gea::platform::sensors::Accelerometer::gyroscopeY(), 10.0),
|
|
1433
|
+
packImu(gea::platform::sensors::Accelerometer::gyroscopeZ(), 10.0),
|
|
1434
|
+
};
|
|
1435
|
+
uint8_t buf[16];
|
|
1436
|
+
const uint32_t tms = static_cast<uint32_t>(esp_timer_get_time() / 1000);
|
|
1437
|
+
std::memcpy(buf, &tms, 4);
|
|
1438
|
+
std::memcpy(buf + 4, vals, sizeof(vals));
|
|
1439
|
+
std::memcpy(self.imuLast_, buf, sizeof(buf));
|
|
1440
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(buf, sizeof(buf));
|
|
1441
|
+
if (om) ble_gatts_notify_custom(self.imuConn_, self.imuAttrHandle_, om);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
void HidServer::notifyKeyboard()
|
|
1446
|
+
{
|
|
1447
|
+
if (kbdConn_ == kNoConn || keyboardAttrHandle_ == 0) return;
|
|
1448
|
+
|
|
1449
|
+
const auto report = reports_.keyboard();
|
|
1450
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(report.data, report.size);
|
|
1451
|
+
if (om) {
|
|
1452
|
+
ble_gatts_notify_custom(kbdConn_, keyboardAttrHandle_, om);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
void HidServer::notifyMouse()
|
|
1457
|
+
{
|
|
1458
|
+
if (mouseConn_ == kNoConn || mouseAttrHandle_ == 0) return;
|
|
1459
|
+
|
|
1460
|
+
const auto report = reports_.mouse();
|
|
1461
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(report.data, report.size);
|
|
1462
|
+
if (om) {
|
|
1463
|
+
ble_gatts_notify_custom(mouseConn_, mouseAttrHandle_, om);
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
int HidServer::handleGapEvent(struct ble_gap_event *event)
|
|
1468
|
+
{
|
|
1469
|
+
switch (event->type) {
|
|
1470
|
+
case BLE_GAP_EVENT_CONNECT:
|
|
1471
|
+
ESP_LOGI(kTag, "connection %s (handle=%d)",
|
|
1472
|
+
event->connect.status == 0 ? "established" : "failed",
|
|
1473
|
+
event->connect.conn_handle);
|
|
1474
|
+
if (event->connect.status == 0) {
|
|
1475
|
+
struct ble_gap_conn_desc desc;
|
|
1476
|
+
if (ble_gap_conn_find(event->connect.conn_handle, &desc) != 0) break;
|
|
1477
|
+
if (desc.role == BLE_GAP_ROLE_SLAVE) {
|
|
1478
|
+
// Inbound: a central (desktop / DAW / sim) connected to us.
|
|
1479
|
+
if (!enabled_) {
|
|
1480
|
+
ble_gap_terminate(event->connect.conn_handle, BLE_ERR_REM_USER_CONN_TERM);
|
|
1481
|
+
break;
|
|
1482
|
+
}
|
|
1483
|
+
char nm[24];
|
|
1484
|
+
const uint8_t *v = desc.peer_id_addr.val;
|
|
1485
|
+
std::snprintf(nm, sizeof(nm), "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
1486
|
+
v[5], v[4], v[3], v[2], v[1], v[0]);
|
|
1487
|
+
linkAdd(event->connect.conn_handle, kLinkHidServed, nm);
|
|
1488
|
+
connected_ = true;
|
|
1489
|
+
|
|
1490
|
+
// Do NOT initiate security from the peripheral. Forcing pairing on every
|
|
1491
|
+
// connection pops a macOS pairing prompt for the air-mouse simulator, whose
|
|
1492
|
+
// only goal is to subscribe to the UNENCRYPTED IMU char (no bonding needed),
|
|
1493
|
+
// and the pending SMP handshake blocks that CCCD subscribe -> imu stream
|
|
1494
|
+
// never starts. A real HOGP host (macOS) initiates encryption itself the
|
|
1495
|
+
// moment it accesses an _ENC HID characteristic (report map, HID info, etc.),
|
|
1496
|
+
// so the keyboard/mouse still pair host-driven via BLE_GAP_EVENT_ENC_CHANGE /
|
|
1497
|
+
// REPEAT_PAIRING. Host-driven pairing only; the peripheral never asks.
|
|
1498
|
+
gea::framework::services::BluetoothService::notifyConnected();
|
|
1499
|
+
#if CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1
|
|
1500
|
+
// Keep advertising so a second host (a DAW after the desktop, or
|
|
1501
|
+
// vice versa) can still connect while link slots remain.
|
|
1502
|
+
startAdvertising();
|
|
1503
|
+
#endif
|
|
1504
|
+
}
|
|
1505
|
+
#if GEA_BLE_CENTRAL
|
|
1506
|
+
else {
|
|
1507
|
+
handleCentralConnected(event->connect.conn_handle);
|
|
1508
|
+
}
|
|
1509
|
+
#endif
|
|
1510
|
+
} else {
|
|
1511
|
+
#if GEA_BLE_CENTRAL
|
|
1512
|
+
if (pendingCentralKind_) {
|
|
1513
|
+
ESP_LOGW(kTag, "central connect failed (status=%d)", event->connect.status);
|
|
1514
|
+
pendingCentralKind_ = 0;
|
|
1515
|
+
}
|
|
1516
|
+
// A failed LL establishment (HCI 0x3E and friends, common on cheap HID
|
|
1517
|
+
// remotes) surfaces here as a CONNECT event with nonzero status — retry
|
|
1518
|
+
// the dial rather than giving up and dropping back to advertising.
|
|
1519
|
+
if (centralRetryKind_ == kLinkHidHosted && retryCentralConnect()) break;
|
|
1520
|
+
#endif
|
|
1521
|
+
startAdvertising();
|
|
1522
|
+
}
|
|
1523
|
+
break;
|
|
1524
|
+
|
|
1525
|
+
case BLE_GAP_EVENT_DISCONNECT: {
|
|
1526
|
+
const uint16_t h = event->disconnect.conn.conn_handle;
|
|
1527
|
+
const bool wasInbound = event->disconnect.conn.role == BLE_GAP_ROLE_SLAVE;
|
|
1528
|
+
ESP_LOGI(kTag, "disconnected (handle=%d reason=%d)", h, event->disconnect.reason);
|
|
1529
|
+
#if GEA_BLE_CENTRAL
|
|
1530
|
+
// A central HID connect that dropped BEFORE it finished binding (e.g.
|
|
1531
|
+
// reason 0x23E / 0x3E, common on cheap remotes) should be retried rather
|
|
1532
|
+
// than reported as a hard failure. A bound link that drops is a real
|
|
1533
|
+
// disconnect — clear the retry target so it isn't re-dialed.
|
|
1534
|
+
bool retryHidConnect = false;
|
|
1535
|
+
if (centralRetryKind_ == kLinkHidHosted && hostSubscribed_ == 0 && !wasInbound &&
|
|
1536
|
+
(h == hidHostConn_ || hidHostConn_ == kNoConn)) {
|
|
1537
|
+
// A central HID link dropped before it bound — either an established
|
|
1538
|
+
// link that collapsed early (h == hidHostConn_) or an LL establishment
|
|
1539
|
+
// that failed before handleCentralConnected ran (hidHostConn_ never set,
|
|
1540
|
+
// e.g. reason 0x23E / 574). Retry the dial rather than giving up.
|
|
1541
|
+
retryHidConnect = true;
|
|
1542
|
+
} else if (h == hidHostConn_) {
|
|
1543
|
+
// A bound link genuinely dropped — clear the retry target so it isn't re-dialed.
|
|
1544
|
+
centralRetryKind_ = 0;
|
|
1545
|
+
}
|
|
1546
|
+
#endif
|
|
1547
|
+
linkRemove(h);
|
|
1548
|
+
if (kbdConn_ == h) kbdConn_ = kNoConn;
|
|
1549
|
+
if (mouseConn_ == h) mouseConn_ = kNoConn;
|
|
1550
|
+
if (batteryConn_ == h) batteryConn_ = kNoConn;
|
|
1551
|
+
if (imuConn_ == h) imuConn_ = kNoConn;
|
|
1552
|
+
if (midiSrvConn_ == h) midiSrvConn_ = kNoConn;
|
|
1553
|
+
if (configCtrlConn_ == h) configCtrlConn_ = kNoConn;
|
|
1554
|
+
if (otaCtrlConn_ == h) otaCtrlConn_ = kNoConn;
|
|
1555
|
+
configClearConn(h); // drop this conn's AUTH unlock
|
|
1556
|
+
#if GEA_BLE_CENTRAL
|
|
1557
|
+
if (h == midiPeerConn_) {
|
|
1558
|
+
midiPeerConn_ = kNoConn;
|
|
1559
|
+
midiSvcStart_ = midiSvcEnd_ = 0;
|
|
1560
|
+
midiPeerValHandle_ = midiPeerCccd_ = 0;
|
|
1561
|
+
}
|
|
1562
|
+
if (h == hidHostConn_) {
|
|
1563
|
+
hidHostConn_ = kNoConn;
|
|
1564
|
+
hidSvcStart_ = hidSvcEnd_ = hidProtoModeHandle_ = 0;
|
|
1565
|
+
hostReportN_ = 0;
|
|
1566
|
+
hostDiscIdx_ = 0;
|
|
1567
|
+
hostSubscribed_ = 0;
|
|
1568
|
+
hostRedoAfterEnc_ = false;
|
|
1569
|
+
}
|
|
1570
|
+
#endif
|
|
1571
|
+
if (wasInbound) {
|
|
1572
|
+
connected_ = inboundLinkCount() > 0;
|
|
1573
|
+
if (!connected_) {
|
|
1574
|
+
reports_.clear();
|
|
1575
|
+
gea::framework::services::BluetoothService::notifyDisconnected();
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
#if GEA_BLE_CENTRAL
|
|
1579
|
+
// A central HID connect was deferred until the radio cleared. Once the
|
|
1580
|
+
// last competing link is gone, initiate it now (advertising stays off
|
|
1581
|
+
// through the whole attempt so nothing steals radio time).
|
|
1582
|
+
if (centralConnectDeferred_ && linkCount_ == 0) {
|
|
1583
|
+
centralConnectDeferred_ = false;
|
|
1584
|
+
if (issueCentralConnect()) break;
|
|
1585
|
+
}
|
|
1586
|
+
// Retry a failed central HID establishment before falling back to advertising.
|
|
1587
|
+
if (retryHidConnect && retryCentralConnect()) break;
|
|
1588
|
+
#endif
|
|
1589
|
+
// Keep advertising suppressed while a central connect is deferred or
|
|
1590
|
+
// retrying; it resumes when the remote binds or the attempt gives up.
|
|
1591
|
+
if (enabled_
|
|
1592
|
+
#if GEA_BLE_CENTRAL
|
|
1593
|
+
&& !centralConnectDeferred_ && centralRetryKind_ == 0
|
|
1594
|
+
#endif
|
|
1595
|
+
) startAdvertising();
|
|
1596
|
+
break;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
case BLE_GAP_EVENT_SUBSCRIBE: {
|
|
1600
|
+
const uint16_t attr = event->subscribe.attr_handle;
|
|
1601
|
+
const uint16_t conn = event->subscribe.conn_handle;
|
|
1602
|
+
const bool on = event->subscribe.cur_notify;
|
|
1603
|
+
ESP_LOGI(kTag, "subscribe: conn=%d attr_handle=%d, notify=%d", conn, attr, on);
|
|
1604
|
+
auto bind = [conn, on](uint16_t &slot) {
|
|
1605
|
+
if (on) slot = conn;
|
|
1606
|
+
else if (slot == conn) slot = kNoConn;
|
|
1607
|
+
};
|
|
1608
|
+
if (attr == keyboardAttrHandle_) {
|
|
1609
|
+
bind(kbdConn_);
|
|
1610
|
+
if (on) linkSetKind(conn, kLinkHidServed);
|
|
1611
|
+
} else if (attr == mouseAttrHandle_) {
|
|
1612
|
+
bind(mouseConn_);
|
|
1613
|
+
if (on) linkSetKind(conn, kLinkHidServed);
|
|
1614
|
+
} else if (attr == batteryAttrHandle_) {
|
|
1615
|
+
bind(batteryConn_);
|
|
1616
|
+
} else if (attr == imuAttrHandle_) {
|
|
1617
|
+
bind(imuConn_);
|
|
1618
|
+
} else if (attr == midiAttrHandle_) {
|
|
1619
|
+
bind(midiSrvConn_);
|
|
1620
|
+
if (on) linkSetKind(conn, kLinkMidiServed); // a DAW, not a HID host
|
|
1621
|
+
} else if (attr == configCtrlAttrHandle_) {
|
|
1622
|
+
bind(configCtrlConn_); // portal subscribed to CTRL status notifications
|
|
1623
|
+
} else if (attr == otaCtrlAttrHandle_) {
|
|
1624
|
+
bind(otaCtrlConn_);
|
|
1625
|
+
}
|
|
1626
|
+
if (kbdConn_ != kNoConn || mouseConn_ != kNoConn) {
|
|
1627
|
+
gea::framework::services::BluetoothService::notifyBound();
|
|
1628
|
+
}
|
|
1629
|
+
break;
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
case BLE_GAP_EVENT_ENC_CHANGE: {
|
|
1633
|
+
ESP_LOGI(kTag, "encryption change: status=%d", event->enc_change.status);
|
|
1634
|
+
// Confirm on serial whether the encrypted link is actually BONDED (keys stored)
|
|
1635
|
+
// vs a transient encryption-only session — the signal that the bond stuck.
|
|
1636
|
+
struct ble_gap_conn_desc desc;
|
|
1637
|
+
if (event->enc_change.status == 0 &&
|
|
1638
|
+
ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
|
|
1639
|
+
ESP_LOGI(kTag, "link encrypted: bonded=%d authenticated=%d",
|
|
1640
|
+
desc.sec_state.bonded, desc.sec_state.authenticated);
|
|
1641
|
+
}
|
|
1642
|
+
#if GEA_BLE_CENTRAL
|
|
1643
|
+
// A hosted HID peripheral (XPPen) rejects secure GATT ops until the link
|
|
1644
|
+
// encrypts: once it does, re-run the report-reference + subscribe pass.
|
|
1645
|
+
if (event->enc_change.status == 0 &&
|
|
1646
|
+
event->enc_change.conn_handle == hidHostConn_ && hostRedoAfterEnc_) {
|
|
1647
|
+
hostRedoAfterEnc_ = false;
|
|
1648
|
+
hostDiscIdx_ = 0;
|
|
1649
|
+
hidHostNextReport(hidHostConn_);
|
|
1650
|
+
}
|
|
1651
|
+
#endif
|
|
1652
|
+
break;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
#if GEA_BLE_CENTRAL
|
|
1656
|
+
case BLE_GAP_EVENT_DISC:
|
|
1657
|
+
handleDisc(&event->disc);
|
|
1658
|
+
break;
|
|
1659
|
+
|
|
1660
|
+
case BLE_GAP_EVENT_DISC_COMPLETE:
|
|
1661
|
+
scanMode_ = ScanMode::None;
|
|
1662
|
+
break;
|
|
1663
|
+
|
|
1664
|
+
case BLE_GAP_EVENT_NOTIFY_RX:
|
|
1665
|
+
handleHostNotify(event);
|
|
1666
|
+
break;
|
|
1667
|
+
#endif
|
|
1668
|
+
|
|
1669
|
+
case BLE_GAP_EVENT_REPEAT_PAIRING: {
|
|
1670
|
+
// macOS re-issues an SMP pairing request on a BONDED reconnect in common HOGP
|
|
1671
|
+
// cases. The old handler deleted the bond on EVERY repeat-pairing and forced a
|
|
1672
|
+
// fresh pair -> the macOS dialog popped on every connect and the two sides'
|
|
1673
|
+
// records diverged, so the only recovery was "forget this device" + re-pair.
|
|
1674
|
+
// Fix: if we STILL hold a usable bond for this peer, keep it and let the host
|
|
1675
|
+
// encrypt with the stored LTK (IGNORE). Only delete + RETRY when there is
|
|
1676
|
+
// genuinely no reusable bond (the peer really wiped its keys).
|
|
1677
|
+
struct ble_gap_conn_desc desc;
|
|
1678
|
+
if (ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc) != 0) {
|
|
1679
|
+
return BLE_GAP_REPEAT_PAIRING_RETRY;
|
|
1680
|
+
}
|
|
1681
|
+
struct ble_store_key_sec key_sec = {};
|
|
1682
|
+
key_sec.peer_addr = desc.peer_id_addr;
|
|
1683
|
+
struct ble_store_value_sec value_sec;
|
|
1684
|
+
if (ble_store_read_peer_sec(&key_sec, &value_sec) == 0) {
|
|
1685
|
+
ESP_LOGI(kTag, "repeat pairing: existing bond found, reusing it (ignore re-pair)");
|
|
1686
|
+
return BLE_GAP_REPEAT_PAIRING_IGNORE;
|
|
1687
|
+
}
|
|
1688
|
+
ble_store_util_delete_peer(&desc.peer_id_addr);
|
|
1689
|
+
return BLE_GAP_REPEAT_PAIRING_RETRY;
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
case BLE_GAP_EVENT_MTU:
|
|
1693
|
+
ESP_LOGI(kTag, "MTU update: conn_handle=%d, mtu=%d",
|
|
1694
|
+
event->mtu.conn_handle, event->mtu.value);
|
|
1695
|
+
break;
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
return 0;
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
void HidServer::startAdvertising()
|
|
1702
|
+
{
|
|
1703
|
+
if (!enabled_) return;
|
|
1704
|
+
if (!hostSynced_) {
|
|
1705
|
+
ESP_LOGW(kTag, "BLE host not synced; advertising deferred");
|
|
1706
|
+
return;
|
|
1707
|
+
}
|
|
1708
|
+
#if GEA_BLE_CENTRAL
|
|
1709
|
+
if (scanMode_ != ScanMode::None) return; // scan and adv can't coexist; resumes on scan stop
|
|
1710
|
+
if (ble_gap_adv_active()) return;
|
|
1711
|
+
#endif
|
|
1712
|
+
|
|
1713
|
+
struct ble_gap_adv_params adv_params = {0};
|
|
1714
|
+
struct ble_hs_adv_fields fields = {0};
|
|
1715
|
+
|
|
1716
|
+
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
|
1717
|
+
fields.name = (uint8_t *)deviceName_;
|
|
1718
|
+
fields.name_len = std::strlen(deviceName_);
|
|
1719
|
+
fields.name_is_complete = 1;
|
|
1720
|
+
fields.appearance = appearance_;
|
|
1721
|
+
fields.appearance_is_present = 1;
|
|
1722
|
+
|
|
1723
|
+
ble_uuid16_t uuids16[] = {
|
|
1724
|
+
BLE_UUID16_INIT(hid::kServiceUuid),
|
|
1725
|
+
BLE_UUID16_INIT(hid::kBatteryServiceUuid),
|
|
1726
|
+
};
|
|
1727
|
+
ble_uuid16_t batteryUuid[] = {
|
|
1728
|
+
BLE_UUID16_INIT(hid::kBatteryServiceUuid),
|
|
1729
|
+
};
|
|
1730
|
+
// appearance=0 is the app's explicit "not a HID device" contract. Mindy
|
|
1731
|
+
// serves BLE-MIDI from this shared driver and must not advertise HOGP:
|
|
1732
|
+
// doing so makes iOS attach a stale PaperS3 HID bond before the MIDI peer
|
|
1733
|
+
// can own the MIDI characteristic. The HID GATT handles remain registered for ABI
|
|
1734
|
+
// stability, but only actual HID apps publish the HID service UUID.
|
|
1735
|
+
if (appearance_ == 0) {
|
|
1736
|
+
fields.uuids16 = batteryUuid;
|
|
1737
|
+
fields.num_uuids16 = 1;
|
|
1738
|
+
fields.uuids16_is_complete = 1;
|
|
1739
|
+
} else {
|
|
1740
|
+
fields.uuids16 = uuids16;
|
|
1741
|
+
fields.num_uuids16 = 2;
|
|
1742
|
+
fields.uuids16_is_complete = 0;
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
int rc = ble_gap_adv_set_fields(&fields);
|
|
1746
|
+
if (rc != 0) {
|
|
1747
|
+
ESP_LOGE(kTag, "adv_set_fields failed: %d", rc);
|
|
1748
|
+
return;
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
// The AD packet is full (flags+name+appearance+16-bit UUIDs), so the 128-bit
|
|
1752
|
+
// BLE-MIDI service UUID rides in the scan response — active scanners
|
|
1753
|
+
// (macOS/iOS MIDI browsers) see it and list this device as a MIDI port.
|
|
1754
|
+
if (midiEnabled_) {
|
|
1755
|
+
ble_uuid128_t midi_uuid[1] = {kMidiServiceUuid};
|
|
1756
|
+
struct ble_hs_adv_fields rsp = {0};
|
|
1757
|
+
rsp.uuids128 = midi_uuid;
|
|
1758
|
+
rsp.num_uuids128 = 1;
|
|
1759
|
+
rsp.uuids128_is_complete = 1;
|
|
1760
|
+
rc = ble_gap_adv_rsp_set_fields(&rsp);
|
|
1761
|
+
if (rc != 0) {
|
|
1762
|
+
ESP_LOGE(kTag, "adv_rsp_set_fields failed: %d", rc);
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
|
|
1767
|
+
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
|
1768
|
+
|
|
1769
|
+
uint8_t own_addr_type = macAddress_[0]
|
|
1770
|
+
? BLE_OWN_ADDR_RANDOM : BLE_OWN_ADDR_PUBLIC;
|
|
1771
|
+
rc = ble_gap_adv_start(own_addr_type, nullptr, BLE_HS_FOREVER,
|
|
1772
|
+
&adv_params, gapEventCallback, nullptr);
|
|
1773
|
+
if (rc == BLE_HS_EALREADY) {
|
|
1774
|
+
return;
|
|
1775
|
+
}
|
|
1776
|
+
if (rc != 0) {
|
|
1777
|
+
// ENOMEM = every connection slot is in use; advertising resumes when one frees.
|
|
1778
|
+
ESP_LOGW(kTag, "adv_start failed: %d", rc);
|
|
1779
|
+
} else {
|
|
1780
|
+
ESP_LOGI(kTag, "advertising started as \"%s\"", deviceName_);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
void HidServer::stopAdvertising()
|
|
1785
|
+
{
|
|
1786
|
+
if (!hostSynced_) return;
|
|
1787
|
+
ble_gap_adv_stop();
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
// ---- connection registry --------------------------------------------------
|
|
1791
|
+
|
|
1792
|
+
void HidServer::linkAdd(uint16_t handle, uint8_t kind, const char *name)
|
|
1793
|
+
{
|
|
1794
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1795
|
+
int i = 0;
|
|
1796
|
+
while (i < linkCount_ && links_[i].handle != handle) i++;
|
|
1797
|
+
if (i == linkCount_) {
|
|
1798
|
+
if (linkCount_ == kMaxLinks) {
|
|
1799
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1802
|
+
linkCount_++;
|
|
1803
|
+
}
|
|
1804
|
+
links_[i].handle = handle;
|
|
1805
|
+
links_[i].kind = kind;
|
|
1806
|
+
std::snprintf(links_[i].name, sizeof(links_[i].name), "%s", name ? name : "");
|
|
1807
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
void HidServer::linkRemove(uint16_t handle)
|
|
1811
|
+
{
|
|
1812
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1813
|
+
for (int i = 0; i < linkCount_; i++) {
|
|
1814
|
+
if (links_[i].handle != handle) continue;
|
|
1815
|
+
for (int j = i; j < linkCount_ - 1; j++) links_[j] = links_[j + 1];
|
|
1816
|
+
linkCount_--;
|
|
1817
|
+
break;
|
|
1818
|
+
}
|
|
1819
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
void HidServer::linkSetKind(uint16_t handle, uint8_t kind)
|
|
1823
|
+
{
|
|
1824
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1825
|
+
for (int i = 0; i < linkCount_; i++) {
|
|
1826
|
+
if (links_[i].handle == handle) {
|
|
1827
|
+
links_[i].kind = kind;
|
|
1828
|
+
break;
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
int HidServer::inboundLinkCount() const
|
|
1835
|
+
{
|
|
1836
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1837
|
+
int n = 0;
|
|
1838
|
+
for (int i = 0; i < linkCount_; i++) {
|
|
1839
|
+
if (links_[i].kind == kLinkHidServed || links_[i].kind == kLinkMidiServed) n++;
|
|
1840
|
+
}
|
|
1841
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1842
|
+
return n;
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
int HidServer::connectionCount() const
|
|
1846
|
+
{
|
|
1847
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1848
|
+
const int n = linkCount_;
|
|
1849
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1850
|
+
return n;
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
int HidServer::connectionKindAt(int index) const
|
|
1854
|
+
{
|
|
1855
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1856
|
+
const int kind = (index >= 0 && index < linkCount_) ? links_[index].kind : 0;
|
|
1857
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1858
|
+
return kind;
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
const char *HidServer::connectionNameAt(int index)
|
|
1862
|
+
{
|
|
1863
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1864
|
+
if (index >= 0 && index < linkCount_) {
|
|
1865
|
+
std::snprintf(linkNameOut_, sizeof(linkNameOut_), "%s", links_[index].name);
|
|
1866
|
+
} else {
|
|
1867
|
+
linkNameOut_[0] = '\0';
|
|
1868
|
+
}
|
|
1869
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1870
|
+
return linkNameOut_;
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
// ---- BLE-MIDI peripheral ---------------------------------------------------
|
|
1874
|
+
|
|
1875
|
+
void HidServer::midiEnable()
|
|
1876
|
+
{
|
|
1877
|
+
if (midiEnabled_) return;
|
|
1878
|
+
midiEnabled_ = true;
|
|
1879
|
+
// The service is always registered; enabling only adds the MIDI UUID to
|
|
1880
|
+
// the scan response. Restart a live advertisement so it takes effect.
|
|
1881
|
+
if (hostSynced_ && enabled_ && ble_gap_adv_active()) {
|
|
1882
|
+
ble_gap_adv_stop();
|
|
1883
|
+
startAdvertising();
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
bool HidServer::midiBound() const
|
|
1888
|
+
{
|
|
1889
|
+
if (midiSrvConn_ != kNoConn) return true;
|
|
1890
|
+
#if GEA_BLE_CENTRAL
|
|
1891
|
+
if (midiPeerConn_ != kNoConn && midiPeerValHandle_ != 0) return true;
|
|
1892
|
+
#endif
|
|
1893
|
+
return false;
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
void HidServer::midiSend(const uint8_t *packet, int length)
|
|
1897
|
+
{
|
|
1898
|
+
if (!packet || length <= 0) return;
|
|
1899
|
+
if (midiSrvConn_ != kNoConn && midiAttrHandle_ != 0) {
|
|
1900
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(packet, length);
|
|
1901
|
+
if (om) ble_gatts_notify_custom(midiSrvConn_, midiAttrHandle_, om);
|
|
1902
|
+
}
|
|
1903
|
+
#if GEA_BLE_CENTRAL
|
|
1904
|
+
if (midiPeerConn_ != kNoConn && midiPeerValHandle_ != 0) {
|
|
1905
|
+
ble_gattc_write_no_rsp_flat(midiPeerConn_, midiPeerValHandle_, packet, length);
|
|
1906
|
+
}
|
|
1907
|
+
#endif
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
#if GEA_BLE_CENTRAL
|
|
1911
|
+
|
|
1912
|
+
// ---- central engine: scan --------------------------------------------------
|
|
1913
|
+
|
|
1914
|
+
namespace {
|
|
1915
|
+
const ble_uuid16_t kCccdUuid = BLE_UUID16_INIT(0x2902);
|
|
1916
|
+
} // namespace
|
|
1917
|
+
|
|
1918
|
+
void HidServer::startScanInternal(ScanMode mode)
|
|
1919
|
+
{
|
|
1920
|
+
ESP_LOGI(kTag, "startScan enter mode=%d synced=%d curmode=%d adv=%d",
|
|
1921
|
+
static_cast<int>(mode), hostSynced_ ? 1 : 0, static_cast<int>(scanMode_),
|
|
1922
|
+
ble_gap_adv_active() ? 1 : 0);
|
|
1923
|
+
if (!hostSynced_) { ESP_LOGW(kTag, "startScan abort: not synced"); return; }
|
|
1924
|
+
if (scanMode_ == mode) { ESP_LOGW(kTag, "startScan abort: already in mode %d", static_cast<int>(mode)); return; }
|
|
1925
|
+
ble_gap_adv_stop();
|
|
1926
|
+
if (ble_gap_disc_active()) ble_gap_disc_cancel();
|
|
1927
|
+
portENTER_CRITICAL(&stateMux_);
|
|
1928
|
+
scanN_ = 0;
|
|
1929
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
1930
|
+
scanMode_ = mode;
|
|
1931
|
+
|
|
1932
|
+
// Active scan: names and 128-bit service UUIDs commonly ride in the scan
|
|
1933
|
+
// response, and both filters need them.
|
|
1934
|
+
struct ble_gap_disc_params params = {};
|
|
1935
|
+
params.passive = 0;
|
|
1936
|
+
params.filter_duplicates = 0; // scan responses can complete an earlier bare entry
|
|
1937
|
+
const uint8_t own = macAddress_[0] ? BLE_OWN_ADDR_RANDOM : BLE_OWN_ADDR_PUBLIC;
|
|
1938
|
+
int rc = ble_gap_disc(own, 15000, ¶ms, gapEventCallback, nullptr);
|
|
1939
|
+
if (rc != 0 && rc != BLE_HS_EALREADY) {
|
|
1940
|
+
ESP_LOGW(kTag, "scan start failed: %d", rc);
|
|
1941
|
+
scanMode_ = ScanMode::None;
|
|
1942
|
+
startAdvertising();
|
|
1943
|
+
} else {
|
|
1944
|
+
ESP_LOGI(kTag, "scanning (%s)", mode == ScanMode::Midi ? "midi" : "hid");
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
void HidServer::stopScanInternal(bool restart_adv)
|
|
1949
|
+
{
|
|
1950
|
+
if (ble_gap_disc_active()) ble_gap_disc_cancel();
|
|
1951
|
+
scanMode_ = ScanMode::None;
|
|
1952
|
+
if (restart_adv) startAdvertising();
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
// Queue a GAP command for the host task and wake it. Called from the app task;
|
|
1956
|
+
// the actual ble_gap_* calls happen in runBleCmd() on the host task.
|
|
1957
|
+
void HidServer::requestBleCmd()
|
|
1958
|
+
{
|
|
1959
|
+
if (!bleCmdInited_) {
|
|
1960
|
+
ble_npl_event_init(&bleCmdEvent_, bleCmdTrampoline, this);
|
|
1961
|
+
bleCmdInited_ = true;
|
|
1962
|
+
}
|
|
1963
|
+
ble_npl_eventq_put(nimble_port_get_dflt_eventq(), &bleCmdEvent_);
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
void HidServer::bleCmdTrampoline(struct ble_npl_event *ev)
|
|
1967
|
+
{
|
|
1968
|
+
auto *self = static_cast<HidServer *>(ble_npl_event_get_arg(ev));
|
|
1969
|
+
if (self) self->runBleCmd();
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
// Runs on the NimBLE host task: safe to call ble_gap_*.
|
|
1973
|
+
void HidServer::runBleCmd()
|
|
1974
|
+
{
|
|
1975
|
+
const int scanReq = reqScanMode_;
|
|
1976
|
+
reqScanMode_ = -2;
|
|
1977
|
+
if (scanReq == -1) stopScanInternal(true);
|
|
1978
|
+
else if (scanReq == static_cast<int>(ScanMode::Midi)) startScanInternal(ScanMode::Midi);
|
|
1979
|
+
else if (scanReq == static_cast<int>(ScanMode::HidHost)) startScanInternal(ScanMode::HidHost);
|
|
1980
|
+
|
|
1981
|
+
const int connIdx = reqConnectIndex_;
|
|
1982
|
+
const uint8_t connKind = reqConnectKind_;
|
|
1983
|
+
reqConnectIndex_ = -1;
|
|
1984
|
+
if (connIdx >= 0) connectTo(connIdx, connKind);
|
|
1985
|
+
|
|
1986
|
+
if (reqDisconnectMidi_) {
|
|
1987
|
+
reqDisconnectMidi_ = false;
|
|
1988
|
+
if (midiPeerConn_ != kNoConn) ble_gap_terminate(midiPeerConn_, BLE_ERR_REM_USER_CONN_TERM);
|
|
1989
|
+
}
|
|
1990
|
+
if (reqDisconnectHid_) {
|
|
1991
|
+
reqDisconnectHid_ = false;
|
|
1992
|
+
if (hidHostConn_ != kNoConn) ble_gap_terminate(hidHostConn_, BLE_ERR_REM_USER_CONN_TERM);
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
// Device -> portal activity push queued by the app task.
|
|
1996
|
+
drainActivityPush();
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
int HidServer::scanCount() const
|
|
2000
|
+
{
|
|
2001
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2002
|
+
const int n = scanN_;
|
|
2003
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2004
|
+
return n;
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
const char *HidServer::scanNameAt(int index)
|
|
2008
|
+
{
|
|
2009
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2010
|
+
if (index >= 0 && index < scanN_) {
|
|
2011
|
+
if (scan_[index].name[0]) {
|
|
2012
|
+
std::snprintf(scanNameOut_, sizeof(scanNameOut_), "%s", scan_[index].name);
|
|
2013
|
+
} else {
|
|
2014
|
+
const uint8_t *v = scan_[index].addr;
|
|
2015
|
+
std::snprintf(scanNameOut_, sizeof(scanNameOut_), "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
2016
|
+
v[5], v[4], v[3], v[2], v[1], v[0]);
|
|
2017
|
+
}
|
|
2018
|
+
} else {
|
|
2019
|
+
scanNameOut_[0] = '\0';
|
|
2020
|
+
}
|
|
2021
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2022
|
+
return scanNameOut_;
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
void HidServer::handleDisc(const struct ble_gap_disc_desc *disc)
|
|
2026
|
+
{
|
|
2027
|
+
if (scanMode_ == ScanMode::None) return;
|
|
2028
|
+
|
|
2029
|
+
struct ble_hs_adv_fields fields;
|
|
2030
|
+
if (ble_hs_adv_parse_fields(&fields, disc->data, disc->length_data) != 0) return;
|
|
2031
|
+
|
|
2032
|
+
char name[24] = {};
|
|
2033
|
+
if (fields.name != nullptr && fields.name_len > 0) {
|
|
2034
|
+
int n = fields.name_len;
|
|
2035
|
+
if (n > (int)sizeof(name) - 1) n = sizeof(name) - 1;
|
|
2036
|
+
std::memcpy(name, fields.name, n);
|
|
2037
|
+
name[n] = '\0';
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
bool match = false;
|
|
2041
|
+
if (scanMode_ == ScanMode::Midi) {
|
|
2042
|
+
for (int i = 0; i < fields.num_uuids128 && !match; i++) {
|
|
2043
|
+
if (ble_uuid_cmp(&fields.uuids128[i].u, &kMidiServiceUuid.u) == 0) match = true;
|
|
2044
|
+
}
|
|
2045
|
+
if (!match && name[0]) {
|
|
2046
|
+
match = std::strncmp(name, "WIDI", 4) == 0 || std::strncmp(name, "PuckCC", 6) == 0;
|
|
2047
|
+
}
|
|
2048
|
+
} else {
|
|
2049
|
+
// HID-host scan. Surface any named connectable advertiser (plus anything
|
|
2050
|
+
// announcing the HID service / a HID appearance) — MANY BLE keypads &
|
|
2051
|
+
// remotes expose 0x1812 only AFTER connect, so name-only devices must
|
|
2052
|
+
// still appear. The APP picks the specific controller by name (e.g.
|
|
2053
|
+
// "Shortcut Remote") and ignores everything else, so a loose scan here is
|
|
2054
|
+
// fine; the HID service is confirmed at GATT-discovery time.
|
|
2055
|
+
for (int i = 0; i < fields.num_uuids16 && !match; i++) {
|
|
2056
|
+
if (fields.uuids16[i].value == hid::kServiceUuid) match = true;
|
|
2057
|
+
}
|
|
2058
|
+
if (!match && fields.appearance_is_present) {
|
|
2059
|
+
match = (fields.appearance & 0xFFC0) == 0x03C0;
|
|
2060
|
+
}
|
|
2061
|
+
if (!match && name[0] &&
|
|
2062
|
+
(disc->event_type == BLE_HCI_ADV_RPT_EVTYPE_ADV_IND ||
|
|
2063
|
+
disc->event_type == BLE_HCI_ADV_RPT_EVTYPE_DIR_IND ||
|
|
2064
|
+
disc->event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP)) {
|
|
2065
|
+
match = true;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
// A scan response completing a known entry carries the name even when the
|
|
2070
|
+
// filterable payload rode in the earlier ADV_IND — merge by address.
|
|
2071
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2072
|
+
int existing = -1;
|
|
2073
|
+
for (int i = 0; i < scanN_; i++) {
|
|
2074
|
+
if (scan_[i].addrType == disc->addr.type &&
|
|
2075
|
+
std::memcmp(scan_[i].addr, disc->addr.val, 6) == 0) {
|
|
2076
|
+
existing = i;
|
|
2077
|
+
break;
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
if (existing >= 0) {
|
|
2081
|
+
if (name[0] && !scan_[existing].name[0]) {
|
|
2082
|
+
std::memcpy(scan_[existing].name, name, sizeof(name));
|
|
2083
|
+
}
|
|
2084
|
+
} else if (match && scanN_ < kMaxScan) {
|
|
2085
|
+
scan_[scanN_].addrType = disc->addr.type;
|
|
2086
|
+
std::memcpy(scan_[scanN_].addr, disc->addr.val, 6);
|
|
2087
|
+
std::memcpy(scan_[scanN_].name, name, sizeof(name));
|
|
2088
|
+
scanN_++;
|
|
2089
|
+
}
|
|
2090
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2091
|
+
|
|
2092
|
+
if (match && existing < 0) {
|
|
2093
|
+
ESP_LOGI(kTag, "scan hit: %s", name[0] ? name : "(unnamed)");
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
// ---- central engine: connect + GATT client ---------------------------------
|
|
2098
|
+
|
|
2099
|
+
void HidServer::connectTo(int index, uint8_t kind)
|
|
2100
|
+
{
|
|
2101
|
+
if (!hostSynced_) return;
|
|
2102
|
+
ble_addr_t addr;
|
|
2103
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2104
|
+
const bool valid = index >= 0 && index < scanN_;
|
|
2105
|
+
if (valid) {
|
|
2106
|
+
addr.type = scan_[index].addrType;
|
|
2107
|
+
std::memcpy(addr.val, scan_[index].addr, 6);
|
|
2108
|
+
std::snprintf(pendingName_, sizeof(pendingName_), "%s", scan_[index].name);
|
|
2109
|
+
}
|
|
2110
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2111
|
+
if (!valid) return;
|
|
2112
|
+
|
|
2113
|
+
if (ble_gap_disc_active()) ble_gap_disc_cancel();
|
|
2114
|
+
if (ble_gap_adv_active()) ble_gap_adv_stop();
|
|
2115
|
+
scanMode_ = ScanMode::None;
|
|
2116
|
+
// Remember the target so the connect (and any retry) can re-issue it.
|
|
2117
|
+
centralRetryAddr_ = addr;
|
|
2118
|
+
centralRetryKind_ = kind;
|
|
2119
|
+
centralConnectAttempts_ = 1;
|
|
2120
|
+
centralConnectDeferred_ = false;
|
|
2121
|
+
pendingCentralKind_ = 0;
|
|
2122
|
+
|
|
2123
|
+
// ROOT CAUSE of the 0x3E / reason-574 failure: the ESP32 radio cannot bring
|
|
2124
|
+
// up a fresh central link while it is simultaneously servicing an existing
|
|
2125
|
+
// connection (the persistent bonded HID host on handle=1). Free the radio
|
|
2126
|
+
// first — terminate every live link — then initiate the connect from the
|
|
2127
|
+
// DISCONNECT handler once the last one is gone. The bonded host auto-
|
|
2128
|
+
// reconnects when we re-advertise after the new remote binds.
|
|
2129
|
+
if (kind == kLinkHidHosted && linkCount_ > 0) {
|
|
2130
|
+
centralConnectDeferred_ = true;
|
|
2131
|
+
uint16_t handles[kMaxLinks];
|
|
2132
|
+
int n = 0;
|
|
2133
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2134
|
+
for (int i = 0; i < linkCount_ && n < kMaxLinks; i++) handles[n++] = links_[i].handle;
|
|
2135
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2136
|
+
for (int i = 0; i < n; i++) {
|
|
2137
|
+
ESP_LOGI(kTag, "central connect: freeing radio, terminating handle=%d", handles[i]);
|
|
2138
|
+
ble_gap_terminate(handles[i], BLE_ERR_REM_USER_CONN_TERM);
|
|
2139
|
+
}
|
|
2140
|
+
return; // issueCentralConnect() resumes from handleGapEvent(DISCONNECT)
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
issueCentralConnect();
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
// Fire ble_gap_connect for the stored retry target (centralRetryAddr_/Kind_).
|
|
2147
|
+
// Runs on the NimBLE host task. Returns true if the initiate was accepted.
|
|
2148
|
+
bool HidServer::issueCentralConnect()
|
|
2149
|
+
{
|
|
2150
|
+
if (centralRetryKind_ == 0) return false;
|
|
2151
|
+
pendingCentralKind_ = centralRetryKind_;
|
|
2152
|
+
if (ble_gap_adv_active()) ble_gap_adv_stop();
|
|
2153
|
+
const uint8_t own = macAddress_[0] ? BLE_OWN_ADDR_RANDOM : BLE_OWN_ADDR_PUBLIC;
|
|
2154
|
+
// HID remotes get the lenient params; other kinds keep the stock defaults.
|
|
2155
|
+
const struct ble_gap_conn_params *cp =
|
|
2156
|
+
centralRetryKind_ == kLinkHidHosted ? &kCentralConnParams : nullptr;
|
|
2157
|
+
ESP_LOGI(kTag, "connecting to \"%s\" (kind=%d own=%d addrType=%d attempt=%d links=%d)",
|
|
2158
|
+
pendingName_, centralRetryKind_, own, centralRetryAddr_.type,
|
|
2159
|
+
centralConnectAttempts_, linkCount_);
|
|
2160
|
+
int rc = ble_gap_connect(own, ¢ralRetryAddr_, 10000, cp, gapEventCallback, nullptr);
|
|
2161
|
+
if (rc != 0 && rc != BLE_HS_EALREADY) {
|
|
2162
|
+
ESP_LOGW(kTag, "central connect failed to start: %d", rc);
|
|
2163
|
+
pendingCentralKind_ = 0;
|
|
2164
|
+
centralRetryKind_ = 0;
|
|
2165
|
+
centralConnectDeferred_ = false;
|
|
2166
|
+
startAdvertising();
|
|
2167
|
+
return false;
|
|
2168
|
+
}
|
|
2169
|
+
return true;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
// Re-issue the last central connect (called from the host task on an early
|
|
2173
|
+
// establishment failure). Returns true if a retry was launched.
|
|
2174
|
+
bool HidServer::retryCentralConnect()
|
|
2175
|
+
{
|
|
2176
|
+
if (centralRetryKind_ == 0) return false;
|
|
2177
|
+
if (centralConnectAttempts_ >= kMaxCentralConnectAttempts) {
|
|
2178
|
+
ESP_LOGW(kTag, "central connect gave up after %d attempts", centralConnectAttempts_);
|
|
2179
|
+
centralRetryKind_ = 0;
|
|
2180
|
+
centralConnectDeferred_ = false;
|
|
2181
|
+
return false;
|
|
2182
|
+
}
|
|
2183
|
+
centralConnectAttempts_++;
|
|
2184
|
+
ESP_LOGI(kTag, "central connect retry #%d", centralConnectAttempts_);
|
|
2185
|
+
return issueCentralConnect();
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
void HidServer::handleCentralConnected(uint16_t conn_handle)
|
|
2189
|
+
{
|
|
2190
|
+
const uint8_t kind = pendingCentralKind_;
|
|
2191
|
+
pendingCentralKind_ = 0;
|
|
2192
|
+
linkAdd(conn_handle, kind, pendingName_[0] ? pendingName_ : "peer");
|
|
2193
|
+
ble_gattc_exchange_mtu(conn_handle, nullptr, nullptr);
|
|
2194
|
+
|
|
2195
|
+
if (kind == kLinkMidiPeer) {
|
|
2196
|
+
midiPeerConn_ = conn_handle;
|
|
2197
|
+
midiSvcStart_ = midiSvcEnd_ = 0;
|
|
2198
|
+
midiPeerValHandle_ = midiPeerCccd_ = 0;
|
|
2199
|
+
ble_gattc_disc_svc_by_uuid(conn_handle, &kMidiServiceUuid.u, midiSvcDiscCallback, nullptr);
|
|
2200
|
+
} else if (kind == kLinkHidHosted) {
|
|
2201
|
+
hidHostConn_ = conn_handle;
|
|
2202
|
+
hidSvcStart_ = hidSvcEnd_ = hidProtoModeHandle_ = 0;
|
|
2203
|
+
hostReportN_ = 0;
|
|
2204
|
+
hostDiscIdx_ = 0;
|
|
2205
|
+
hostSubscribed_ = 0;
|
|
2206
|
+
hostRedoAfterEnc_ = false;
|
|
2207
|
+
ble_gattc_disc_svc_by_uuid(conn_handle, &kHidServiceUuid.u, hidSvcDiscCallback, nullptr);
|
|
2208
|
+
// HID peripherals gate report subscription behind an encrypted link;
|
|
2209
|
+
// initiate as central so bonding runs while discovery proceeds.
|
|
2210
|
+
ble_gap_security_initiate(conn_handle);
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
// For a fresh central HID link, keep advertising OFF through MTU exchange,
|
|
2214
|
+
// service discovery and pairing — a bonded host reconnecting mid-handshake
|
|
2215
|
+
// would steal radio time and can collapse the still-fragile remote link.
|
|
2216
|
+
// Advertising resumes once the remote binds (hid host ready) or the link
|
|
2217
|
+
// drops. Other central kinds keep serving immediately.
|
|
2218
|
+
if (kind != kLinkHidHosted) startAdvertising();
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
int HidServer::midiSvcDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2222
|
+
const struct ble_gatt_svc *service, void *arg)
|
|
2223
|
+
{
|
|
2224
|
+
(void)arg;
|
|
2225
|
+
HidServer &self = instance();
|
|
2226
|
+
if (error->status == 0 && service != nullptr) {
|
|
2227
|
+
self.midiSvcStart_ = service->start_handle;
|
|
2228
|
+
self.midiSvcEnd_ = service->end_handle;
|
|
2229
|
+
return 0;
|
|
2230
|
+
}
|
|
2231
|
+
if (error->status == BLE_HS_EDONE && self.midiSvcStart_ != 0) {
|
|
2232
|
+
ble_gattc_disc_all_chrs(conn_handle, self.midiSvcStart_, self.midiSvcEnd_,
|
|
2233
|
+
midiChrDiscCallback, nullptr);
|
|
2234
|
+
} else {
|
|
2235
|
+
ESP_LOGW(kTag, "midi peer: service discovery failed (%d)", error->status);
|
|
2236
|
+
}
|
|
2237
|
+
return 0;
|
|
2238
|
+
}
|
|
2239
|
+
|
|
2240
|
+
int HidServer::midiChrDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2241
|
+
const struct ble_gatt_chr *chr, void *arg)
|
|
2242
|
+
{
|
|
2243
|
+
(void)arg;
|
|
2244
|
+
HidServer &self = instance();
|
|
2245
|
+
if (error->status == 0 && chr != nullptr) {
|
|
2246
|
+
if (ble_uuid_cmp(&chr->uuid.u, &kMidiCharUuid.u) == 0) {
|
|
2247
|
+
self.midiPeerValHandle_ = chr->val_handle;
|
|
2248
|
+
}
|
|
2249
|
+
return 0;
|
|
2250
|
+
}
|
|
2251
|
+
if (error->status == BLE_HS_EDONE && self.midiPeerValHandle_ != 0) {
|
|
2252
|
+
ESP_LOGI(kTag, "midi peer: I/O char at %d", self.midiPeerValHandle_);
|
|
2253
|
+
ble_gattc_disc_all_dscs(conn_handle, self.midiPeerValHandle_, self.midiSvcEnd_,
|
|
2254
|
+
midiDscDiscCallback, nullptr);
|
|
2255
|
+
} else {
|
|
2256
|
+
ESP_LOGW(kTag, "midi peer: I/O characteristic not found (%d)", error->status);
|
|
2257
|
+
}
|
|
2258
|
+
return 0;
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
int HidServer::midiDscDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2262
|
+
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, void *arg)
|
|
2263
|
+
{
|
|
2264
|
+
(void)chr_val_handle;
|
|
2265
|
+
(void)arg;
|
|
2266
|
+
HidServer &self = instance();
|
|
2267
|
+
if (error->status == 0 && dsc != nullptr) {
|
|
2268
|
+
if (ble_uuid_cmp(&dsc->uuid.u, &kCccdUuid.u) == 0) {
|
|
2269
|
+
self.midiPeerCccd_ = dsc->handle;
|
|
2270
|
+
}
|
|
2271
|
+
return 0;
|
|
2272
|
+
}
|
|
2273
|
+
if (self.midiPeerCccd_ != 0) {
|
|
2274
|
+
// Subscribe to inbound MIDI from the pedal.
|
|
2275
|
+
const uint8_t enable[2] = {0x01, 0x00};
|
|
2276
|
+
ble_gattc_write_flat(conn_handle, self.midiPeerCccd_, enable, sizeof(enable), nullptr, nullptr);
|
|
2277
|
+
}
|
|
2278
|
+
ESP_LOGI(kTag, "midi peer ready (cccd=%d)", self.midiPeerCccd_);
|
|
2279
|
+
return 0;
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
int HidServer::hidSvcDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2283
|
+
const struct ble_gatt_svc *service, void *arg)
|
|
2284
|
+
{
|
|
2285
|
+
(void)arg;
|
|
2286
|
+
HidServer &self = instance();
|
|
2287
|
+
if (error->status == 0 && service != nullptr) {
|
|
2288
|
+
self.hidSvcStart_ = service->start_handle;
|
|
2289
|
+
self.hidSvcEnd_ = service->end_handle;
|
|
2290
|
+
return 0;
|
|
2291
|
+
}
|
|
2292
|
+
if (error->status == BLE_HS_EDONE && self.hidSvcStart_ != 0) {
|
|
2293
|
+
ble_gattc_disc_all_chrs(conn_handle, self.hidSvcStart_, self.hidSvcEnd_,
|
|
2294
|
+
hidChrDiscCallback, nullptr);
|
|
2295
|
+
} else {
|
|
2296
|
+
ESP_LOGW(kTag, "hid host: service discovery failed (%d)", error->status);
|
|
2297
|
+
}
|
|
2298
|
+
return 0;
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
int HidServer::hidChrDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2302
|
+
const struct ble_gatt_chr *chr, void *arg)
|
|
2303
|
+
{
|
|
2304
|
+
(void)arg;
|
|
2305
|
+
HidServer &self = instance();
|
|
2306
|
+
if (error->status == 0 && chr != nullptr) {
|
|
2307
|
+
if (ble_uuid_cmp(&chr->uuid.u, &kReportUuid.u) == 0 &&
|
|
2308
|
+
self.hostReportN_ < kMaxHostReports) {
|
|
2309
|
+
HostReportChr &r = self.hostReports_[self.hostReportN_++];
|
|
2310
|
+
r = HostReportChr{};
|
|
2311
|
+
r.defHandle = chr->def_handle;
|
|
2312
|
+
r.valHandle = chr->val_handle;
|
|
2313
|
+
r.endHandle = self.hidSvcEnd_;
|
|
2314
|
+
} else if (ble_uuid_cmp(&chr->uuid.u, &kProtocolModeUuid.u) == 0) {
|
|
2315
|
+
self.hidProtoModeHandle_ = chr->val_handle;
|
|
2316
|
+
}
|
|
2317
|
+
return 0;
|
|
2318
|
+
}
|
|
2319
|
+
if (error->status != BLE_HS_EDONE || self.hostReportN_ == 0) {
|
|
2320
|
+
ESP_LOGW(kTag, "hid host: no report characteristics (%d)", error->status);
|
|
2321
|
+
return 0;
|
|
2322
|
+
}
|
|
2323
|
+
// Bound each report's descriptor range by the next characteristic.
|
|
2324
|
+
for (int i = 0; i + 1 < self.hostReportN_; i++) {
|
|
2325
|
+
self.hostReports_[i].endHandle = self.hostReports_[i + 1].defHandle - 1;
|
|
2326
|
+
}
|
|
2327
|
+
if (self.hidProtoModeHandle_ != 0) {
|
|
2328
|
+
const uint8_t report_mode = 0x01;
|
|
2329
|
+
ble_gattc_write_no_rsp_flat(conn_handle, self.hidProtoModeHandle_, &report_mode, 1);
|
|
2330
|
+
}
|
|
2331
|
+
ESP_LOGI(kTag, "hid host: %d report chars", self.hostReportN_);
|
|
2332
|
+
self.hostDiscIdx_ = 0;
|
|
2333
|
+
self.hidHostNextReport(conn_handle);
|
|
2334
|
+
return 0;
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
void HidServer::hidHostNextReport(uint16_t conn_handle)
|
|
2338
|
+
{
|
|
2339
|
+
if (hostDiscIdx_ >= hostReportN_) {
|
|
2340
|
+
ESP_LOGI(kTag, "hid host ready: %d input reports subscribed", hostSubscribed_);
|
|
2341
|
+
// The remote is bound: the connect sequence is done, so clear the retry
|
|
2342
|
+
// target and resume advertising to let the bonded host reconnect.
|
|
2343
|
+
centralRetryKind_ = 0;
|
|
2344
|
+
centralConnectDeferred_ = false;
|
|
2345
|
+
startAdvertising();
|
|
2346
|
+
return;
|
|
2347
|
+
}
|
|
2348
|
+
const HostReportChr &r = hostReports_[hostDiscIdx_];
|
|
2349
|
+
if (r.valHandle >= r.endHandle) {
|
|
2350
|
+
// No room for descriptors — nothing to subscribe on this report.
|
|
2351
|
+
hostDiscIdx_++;
|
|
2352
|
+
hidHostNextReport(conn_handle);
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
ble_gattc_disc_all_dscs(conn_handle, r.valHandle, r.endHandle, hidDscDiscCallback, nullptr);
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
int HidServer::hidDscDiscCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2359
|
+
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, void *arg)
|
|
2360
|
+
{
|
|
2361
|
+
(void)chr_val_handle;
|
|
2362
|
+
(void)arg;
|
|
2363
|
+
HidServer &self = instance();
|
|
2364
|
+
if (self.hostDiscIdx_ >= self.hostReportN_) return 0;
|
|
2365
|
+
HostReportChr &r = self.hostReports_[self.hostDiscIdx_];
|
|
2366
|
+
if (error->status == 0 && dsc != nullptr) {
|
|
2367
|
+
if (ble_uuid_cmp(&dsc->uuid.u, &kCccdUuid.u) == 0) {
|
|
2368
|
+
r.cccdHandle = dsc->handle;
|
|
2369
|
+
} else if (ble_uuid_cmp(&dsc->uuid.u, &kReportReferenceUuid.u) == 0) {
|
|
2370
|
+
r.rptRefHandle = dsc->handle;
|
|
2371
|
+
}
|
|
2372
|
+
return 0;
|
|
2373
|
+
}
|
|
2374
|
+
if (r.rptRefHandle != 0) {
|
|
2375
|
+
int rc = ble_gattc_read(conn_handle, r.rptRefHandle, hidRefReadCallback, nullptr);
|
|
2376
|
+
if (rc != 0) {
|
|
2377
|
+
ESP_LOGW(kTag, "hid host: report-ref read failed to start (%d)", rc);
|
|
2378
|
+
self.hidHostSubscribeCurrent(conn_handle, true);
|
|
2379
|
+
}
|
|
2380
|
+
} else {
|
|
2381
|
+
// No report reference: single-report device, assume input.
|
|
2382
|
+
self.hidHostSubscribeCurrent(conn_handle, true);
|
|
2383
|
+
}
|
|
2384
|
+
return 0;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
int HidServer::hidRefReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2388
|
+
struct ble_gatt_attr *attr, void *arg)
|
|
2389
|
+
{
|
|
2390
|
+
(void)arg;
|
|
2391
|
+
HidServer &self = instance();
|
|
2392
|
+
if (self.hostDiscIdx_ >= self.hostReportN_) return 0;
|
|
2393
|
+
HostReportChr &r = self.hostReports_[self.hostDiscIdx_];
|
|
2394
|
+
if (error->status == 0 && attr != nullptr && attr->om != nullptr &&
|
|
2395
|
+
OS_MBUF_PKTLEN(attr->om) >= 2) {
|
|
2396
|
+
uint8_t ref[2] = {};
|
|
2397
|
+
os_mbuf_copydata(attr->om, 0, 2, ref);
|
|
2398
|
+
r.reportId = ref[0];
|
|
2399
|
+
r.reportType = ref[1];
|
|
2400
|
+
self.hidHostSubscribeCurrent(conn_handle, r.reportType == 0x01);
|
|
2401
|
+
return 0;
|
|
2402
|
+
}
|
|
2403
|
+
if (attSecurityError(error->status)) {
|
|
2404
|
+
// Pre-bond rejection: re-run this pass once the link encrypts.
|
|
2405
|
+
self.hostRedoAfterEnc_ = true;
|
|
2406
|
+
ble_gap_security_initiate(conn_handle);
|
|
2407
|
+
return 0;
|
|
2408
|
+
}
|
|
2409
|
+
ESP_LOGW(kTag, "hid host: report-ref read failed (%d)", error->status);
|
|
2410
|
+
self.hidHostSubscribeCurrent(conn_handle, true);
|
|
2411
|
+
return 0;
|
|
2412
|
+
}
|
|
2413
|
+
|
|
2414
|
+
void HidServer::hidHostSubscribeCurrent(uint16_t conn_handle, bool input)
|
|
2415
|
+
{
|
|
2416
|
+
if (hostDiscIdx_ >= hostReportN_) return;
|
|
2417
|
+
HostReportChr &r = hostReports_[hostDiscIdx_];
|
|
2418
|
+
if (input && r.cccdHandle != 0 && !r.subscribed) {
|
|
2419
|
+
const uint8_t enable[2] = {0x01, 0x00};
|
|
2420
|
+
int rc = ble_gattc_write_flat(conn_handle, r.cccdHandle, enable, sizeof(enable),
|
|
2421
|
+
hidCccdWriteCallback, nullptr);
|
|
2422
|
+
if (rc == 0) return; // advance from the write callback
|
|
2423
|
+
ESP_LOGW(kTag, "hid host: cccd write failed to start (%d)", rc);
|
|
2424
|
+
}
|
|
2425
|
+
hostDiscIdx_++;
|
|
2426
|
+
hidHostNextReport(conn_handle);
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
int HidServer::hidCccdWriteCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
2430
|
+
struct ble_gatt_attr *attr, void *arg)
|
|
2431
|
+
{
|
|
2432
|
+
(void)attr;
|
|
2433
|
+
(void)arg;
|
|
2434
|
+
HidServer &self = instance();
|
|
2435
|
+
if (self.hostDiscIdx_ >= self.hostReportN_) return 0;
|
|
2436
|
+
HostReportChr &r = self.hostReports_[self.hostDiscIdx_];
|
|
2437
|
+
if (error->status == 0) {
|
|
2438
|
+
if (!r.subscribed) {
|
|
2439
|
+
r.subscribed = true;
|
|
2440
|
+
self.hostSubscribed_++;
|
|
2441
|
+
}
|
|
2442
|
+
} else if (attSecurityError(error->status)) {
|
|
2443
|
+
self.hostRedoAfterEnc_ = true;
|
|
2444
|
+
ble_gap_security_initiate(conn_handle);
|
|
2445
|
+
} else {
|
|
2446
|
+
ESP_LOGW(kTag, "hid host: cccd write failed (%d)", error->status);
|
|
2447
|
+
}
|
|
2448
|
+
self.hostDiscIdx_++;
|
|
2449
|
+
self.hidHostNextReport(conn_handle);
|
|
2450
|
+
return 0;
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
// ---- central engine: inbound notifications + report FIFO -------------------
|
|
2454
|
+
|
|
2455
|
+
void HidServer::handleHostNotify(const struct ble_gap_event *event)
|
|
2456
|
+
{
|
|
2457
|
+
const uint16_t conn = event->notify_rx.conn_handle;
|
|
2458
|
+
const uint16_t attr = event->notify_rx.attr_handle;
|
|
2459
|
+
struct os_mbuf *om = event->notify_rx.om;
|
|
2460
|
+
if (om == nullptr) return;
|
|
2461
|
+
|
|
2462
|
+
if (conn == midiPeerConn_ && attr == midiPeerValHandle_) {
|
|
2463
|
+
// Inbound MIDI from the pedal: no consumer yet — absorb.
|
|
2464
|
+
return;
|
|
2465
|
+
}
|
|
2466
|
+
if (conn != hidHostConn_) return;
|
|
2467
|
+
|
|
2468
|
+
uint8_t report_id = 0;
|
|
2469
|
+
for (int i = 0; i < hostReportN_; i++) {
|
|
2470
|
+
if (hostReports_[i].valHandle == attr) {
|
|
2471
|
+
report_id = hostReports_[i].reportId;
|
|
2472
|
+
break;
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
HostReportMsg msg = {};
|
|
2477
|
+
msg.id = report_id;
|
|
2478
|
+
int len = OS_MBUF_PKTLEN(om);
|
|
2479
|
+
if (len > (int)sizeof(msg.data)) len = sizeof(msg.data);
|
|
2480
|
+
msg.len = (uint8_t)len;
|
|
2481
|
+
os_mbuf_copydata(om, 0, len, msg.data);
|
|
2482
|
+
|
|
2483
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2484
|
+
if (fifoCount_ == kFifoSize) {
|
|
2485
|
+
fifo_[fifoHead_] = msg;
|
|
2486
|
+
fifoHead_ = (fifoHead_ + 1) % kFifoSize;
|
|
2487
|
+
} else {
|
|
2488
|
+
fifo_[(fifoHead_ + fifoCount_) % kFifoSize] = msg;
|
|
2489
|
+
fifoCount_++;
|
|
2490
|
+
}
|
|
2491
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2492
|
+
}
|
|
2493
|
+
|
|
2494
|
+
int HidServer::hidHostReportCount() const
|
|
2495
|
+
{
|
|
2496
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2497
|
+
const int n = fifoCount_;
|
|
2498
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2499
|
+
return n;
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
int HidServer::hidHostReportIdAt(int index) const
|
|
2503
|
+
{
|
|
2504
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2505
|
+
const int v = (index >= 0 && index < fifoCount_)
|
|
2506
|
+
? fifo_[(fifoHead_ + index) % kFifoSize].id : 0;
|
|
2507
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2508
|
+
return v;
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
int HidServer::hidHostReportLenAt(int index) const
|
|
2512
|
+
{
|
|
2513
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2514
|
+
const int v = (index >= 0 && index < fifoCount_)
|
|
2515
|
+
? fifo_[(fifoHead_ + index) % kFifoSize].len : 0;
|
|
2516
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2517
|
+
return v;
|
|
2518
|
+
}
|
|
2519
|
+
|
|
2520
|
+
int HidServer::hidHostReportByteAt(int index, int byteIndex) const
|
|
2521
|
+
{
|
|
2522
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2523
|
+
int v = 0;
|
|
2524
|
+
if (index >= 0 && index < fifoCount_) {
|
|
2525
|
+
const HostReportMsg &msg = fifo_[(fifoHead_ + index) % kFifoSize];
|
|
2526
|
+
if (byteIndex >= 0 && byteIndex < msg.len) v = msg.data[byteIndex];
|
|
2527
|
+
}
|
|
2528
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2529
|
+
return v;
|
|
2530
|
+
}
|
|
2531
|
+
|
|
2532
|
+
void HidServer::hidHostClearReports()
|
|
2533
|
+
{
|
|
2534
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2535
|
+
fifoHead_ = 0;
|
|
2536
|
+
fifoCount_ = 0;
|
|
2537
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2538
|
+
}
|
|
2539
|
+
|
|
2540
|
+
#endif // GEA_BLE_CENTRAL
|
|
2541
|
+
|
|
2542
|
+
int HidServer::parseMacLe(const char *str, uint8_t out[6]) const
|
|
2543
|
+
{
|
|
2544
|
+
unsigned int b[6];
|
|
2545
|
+
if (std::sscanf(str, "%x:%x:%x:%x:%x:%x", &b[0], &b[1], &b[2], &b[3], &b[4], &b[5]) != 6) {
|
|
2546
|
+
return -1;
|
|
2547
|
+
}
|
|
2548
|
+
|
|
2549
|
+
/* User writes MSB-first (AA:BB:CC:DD:EE:FF); NimBLE wants LE. */
|
|
2550
|
+
for (int i = 0; i < 6; i++) out[i] = (uint8_t)b[5 - i];
|
|
2551
|
+
return 0;
|
|
2552
|
+
}
|
|
2553
|
+
|
|
2554
|
+
void HidServer::onSync()
|
|
2555
|
+
{
|
|
2556
|
+
int rc;
|
|
2557
|
+
|
|
2558
|
+
if (macAddress_[0]) {
|
|
2559
|
+
uint8_t addr[6];
|
|
2560
|
+
if (parseMacLe(macAddress_, addr) == 0) {
|
|
2561
|
+
addr[5] |= 0xC0; /* BLE random static: top 2 bits of MSB must be 11. */
|
|
2562
|
+
rc = ble_hs_id_set_rnd(addr);
|
|
2563
|
+
if (rc != 0) {
|
|
2564
|
+
ESP_LOGE(kTag, "ble_hs_id_set_rnd failed: %d", rc);
|
|
2565
|
+
} else {
|
|
2566
|
+
ESP_LOGI(kTag, "using custom MAC: %s", macAddress_);
|
|
2567
|
+
}
|
|
2568
|
+
} else {
|
|
2569
|
+
ESP_LOGE(kTag, "invalid macAddress format, expected XX:XX:XX:XX:XX:XX");
|
|
2570
|
+
}
|
|
2571
|
+
}
|
|
2572
|
+
|
|
2573
|
+
rc = ble_hs_util_ensure_addr(0);
|
|
2574
|
+
if (rc != 0) {
|
|
2575
|
+
ESP_LOGE(kTag, "ble_hs_util_ensure_addr failed: %d", rc);
|
|
2576
|
+
return;
|
|
2577
|
+
}
|
|
2578
|
+
|
|
2579
|
+
hostSynced_ = true;
|
|
2580
|
+
if (enabled_) startAdvertising();
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
void HidServer::onReset(int reason)
|
|
2584
|
+
{
|
|
2585
|
+
hostSynced_ = false;
|
|
2586
|
+
ESP_LOGE(kTag, "host reset: reason=%d", reason);
|
|
2587
|
+
}
|
|
2588
|
+
|
|
2589
|
+
void HidServer::preinit()
|
|
2590
|
+
{
|
|
2591
|
+
if (controllerInited_) return;
|
|
2592
|
+
|
|
2593
|
+
// Config blobs live in PSRAM, not internal .bss — keeps ~12 KB of internal
|
|
2594
|
+
// RAM free for the BLE controller/host (the central scan was crashing under
|
|
2595
|
+
// internal-heap pressure). PSRAM has megabytes free; if it somehow fails,
|
|
2596
|
+
// the config service simply stays inert (all accesses are guarded).
|
|
2597
|
+
if (configDoc_ == nullptr) {
|
|
2598
|
+
configDoc_ = static_cast<uint8_t *>(heap_caps_calloc(1, kConfigDocMax, MALLOC_CAP_SPIRAM));
|
|
2599
|
+
configInbound_ = static_cast<uint8_t *>(heap_caps_calloc(1, kConfigDocMax, MALLOC_CAP_SPIRAM));
|
|
2600
|
+
configPending_ = static_cast<uint8_t *>(heap_caps_calloc(1, kConfigDocMax, MALLOC_CAP_SPIRAM));
|
|
2601
|
+
if (!configDoc_ || !configInbound_ || !configPending_) {
|
|
2602
|
+
ESP_LOGE(kTag, "config buffer PSRAM alloc failed; config service disabled");
|
|
2603
|
+
}
|
|
2604
|
+
}
|
|
2605
|
+
|
|
2606
|
+
esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
|
2607
|
+
esp_err_t err = esp_bt_controller_init(&cfg);
|
|
2608
|
+
if (err != ESP_OK) {
|
|
2609
|
+
ESP_LOGE(kTag, "esp_bt_controller_init failed: %s", esp_err_to_name(err));
|
|
2610
|
+
return;
|
|
2611
|
+
}
|
|
2612
|
+
|
|
2613
|
+
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
|
2614
|
+
if (err != ESP_OK) {
|
|
2615
|
+
ESP_LOGE(kTag, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
|
2616
|
+
esp_bt_controller_deinit();
|
|
2617
|
+
return;
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
controllerInited_ = true;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
void HidServer::bleFirstInitTrampoline(void *arg)
|
|
2624
|
+
{
|
|
2625
|
+
auto *work = static_cast<BleFirstInitWork *>(arg);
|
|
2626
|
+
// Re-enter init() now that we run on an internal-RAM stack: esp_ptr_external_ram()
|
|
2627
|
+
// in init() is false here, so it skips the dispatch and performs the real
|
|
2628
|
+
// controller/host bring-up (flash ops are now cache-safe). `work` and the
|
|
2629
|
+
// strings it points at stay valid because the caller blocks on `done` below.
|
|
2630
|
+
work->self->init(work->name, work->appearance, work->mac);
|
|
2631
|
+
xSemaphoreGive(work->done);
|
|
2632
|
+
vTaskDeleteWithCaps(nullptr);
|
|
2633
|
+
}
|
|
2634
|
+
|
|
2635
|
+
void HidServer::init(const char *device_name, uint16_t appearance, const char *mac_address)
|
|
2636
|
+
{
|
|
2637
|
+
// The app owns its public BLE identity. This driver is shared by every ESP32
|
|
2638
|
+
// target, so hard-coding one board name here makes unrelated applications
|
|
2639
|
+
// advertise as that board and breaks first-party discovery (for example,
|
|
2640
|
+
// Mindy's BLE-MIDI endpoint must appear as "Mindy MIDI").
|
|
2641
|
+
const char *resolvedName =
|
|
2642
|
+
device_name && device_name[0] ? device_name : hid::kDefaultDeviceName;
|
|
2643
|
+
std::snprintf(deviceName_, sizeof(deviceName_), "%s", resolvedName);
|
|
2644
|
+
appearance_ = appearance;
|
|
2645
|
+
if (mac_address && mac_address[0]) {
|
|
2646
|
+
std::snprintf(macAddress_, sizeof(macAddress_), "%s", mac_address);
|
|
2647
|
+
} else {
|
|
2648
|
+
macAddress_[0] = '\0';
|
|
2649
|
+
}
|
|
2650
|
+
|
|
2651
|
+
if (hostInited_) {
|
|
2652
|
+
ble_svc_gap_device_name_set(deviceName_);
|
|
2653
|
+
ble_svc_gap_device_appearance_set(appearance_);
|
|
2654
|
+
return;
|
|
2655
|
+
}
|
|
2656
|
+
|
|
2657
|
+
// First-time bring-up below touches flash (BT controller PHY-calibration
|
|
2658
|
+
// load, NimBLE NVS bonding store). The flash driver disables the CPU cache
|
|
2659
|
+
// and asserts esp_task_stack_is_sane_cache_disabled(): with the cache off,
|
|
2660
|
+
// PSRAM is unreachable, so the running task's stack MUST be in internal RAM.
|
|
2661
|
+
// Applications run their _init (and thus this bring-up) on the gea_init
|
|
2662
|
+
// task, whose 64 KB stack lives in PSRAM (it has to — the JSX mount needs
|
|
2663
|
+
// 64 KB, which won't fit internal). So run the one-time bring-up on a
|
|
2664
|
+
// transient internal-RAM-stacked worker and block until it finishes. The
|
|
2665
|
+
// per-app re-init above (hostInited_) is RAM-only and stays inline. Keeps
|
|
2666
|
+
// BLE init lazy — no boot-time DRAM cost (see BluetoothService::preinitForApp).
|
|
2667
|
+
{
|
|
2668
|
+
int stack_probe;
|
|
2669
|
+
if (esp_ptr_external_ram(&stack_probe)) {
|
|
2670
|
+
// The bring-up below must run on an internal-RAM stack: its flash ops
|
|
2671
|
+
// disable the CPU cache, which also makes PSRAM unreachable, so the
|
|
2672
|
+
// running task's stack cannot be in PSRAM (esp_task_stack_is_sane_
|
|
2673
|
+
// cache_disabled() asserts exactly that). gea_init's stack IS in PSRAM,
|
|
2674
|
+
// so run the one-time bring-up on a transient internal-RAM-stacked
|
|
2675
|
+
// worker. Measured peak stack use of the full bring-up (controller +
|
|
2676
|
+
// host init, NVS bond load, connect/bind) is ~1.9 KB, so 6 KB is ~3x
|
|
2677
|
+
// margin and fits even when internal RAM is tight (needs only an ~8 KB
|
|
2678
|
+
// free block, vs the old 16 KB). If it still can't be allocated, skip
|
|
2679
|
+
// BLE — NEVER run the flash ops inline on the PSRAM stack (the crash).
|
|
2680
|
+
constexpr uint32_t kWorkerStackBytes = 6144;
|
|
2681
|
+
SemaphoreHandle_t done = xSemaphoreCreateBinary();
|
|
2682
|
+
if (!done) {
|
|
2683
|
+
ESP_LOGE(kTag, "ble_init: semaphore create failed; skipping BLE");
|
|
2684
|
+
return;
|
|
2685
|
+
}
|
|
2686
|
+
BleFirstInitWork work{this, device_name, appearance, mac_address, done};
|
|
2687
|
+
TaskHandle_t worker = nullptr;
|
|
2688
|
+
const BaseType_t ok = xTaskCreateWithCaps(
|
|
2689
|
+
&HidServer::bleFirstInitTrampoline, "ble_init", kWorkerStackBytes, &work, 5, &worker,
|
|
2690
|
+
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
|
2691
|
+
if (ok == pdPASS) {
|
|
2692
|
+
xSemaphoreTake(done, portMAX_DELAY);
|
|
2693
|
+
vSemaphoreDelete(done);
|
|
2694
|
+
return;
|
|
2695
|
+
}
|
|
2696
|
+
vSemaphoreDelete(done);
|
|
2697
|
+
const size_t internalLargest = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
|
|
2698
|
+
ESP_LOGE(kTag, "ble_init: worker create failed (stack=%u, internal_largest=%u); skipping BLE",
|
|
2699
|
+
(unsigned)kWorkerStackBytes, (unsigned)internalLargest);
|
|
2700
|
+
return;
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
if (!controllerInited_) {
|
|
2705
|
+
preinit();
|
|
2706
|
+
if (!controllerInited_) return;
|
|
2707
|
+
}
|
|
2708
|
+
|
|
2709
|
+
hostSynced_ = false;
|
|
2710
|
+
|
|
2711
|
+
esp_err_t err = esp_nimble_init();
|
|
2712
|
+
if (err != ESP_OK) {
|
|
2713
|
+
ESP_LOGE(kTag, "esp_nimble_init failed: %s", esp_err_to_name(err));
|
|
2714
|
+
return;
|
|
2715
|
+
}
|
|
2716
|
+
|
|
2717
|
+
ble_hs_cfg.reset_cb = onResetCallback;
|
|
2718
|
+
ble_hs_cfg.sync_cb = onSyncCallback;
|
|
2719
|
+
ble_hs_cfg.sm_io_cap = BLE_HS_IO_NO_INPUT_OUTPUT;
|
|
2720
|
+
ble_hs_cfg.sm_bonding = 1;
|
|
2721
|
+
ble_hs_cfg.sm_mitm = 0;
|
|
2722
|
+
ble_hs_cfg.sm_sc = 1;
|
|
2723
|
+
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
|
|
2724
|
+
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
|
|
2725
|
+
|
|
2726
|
+
ble_hs_cfg.store_read_cb = ble_store_config_read;
|
|
2727
|
+
ble_hs_cfg.store_write_cb = ble_store_config_write;
|
|
2728
|
+
ble_hs_cfg.store_delete_cb = ble_store_config_delete;
|
|
2729
|
+
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
|
|
2730
|
+
|
|
2731
|
+
ble_store_config_init();
|
|
2732
|
+
|
|
2733
|
+
ble_svc_gap_init();
|
|
2734
|
+
ble_svc_gatt_init();
|
|
2735
|
+
|
|
2736
|
+
int rc = ble_gatts_count_cfg(gattServices_);
|
|
2737
|
+
if (rc != 0) {
|
|
2738
|
+
ESP_LOGE(kTag, "ble_gatts_count_cfg failed: %d", rc);
|
|
2739
|
+
return;
|
|
2740
|
+
}
|
|
2741
|
+
rc = ble_gatts_add_svcs(gattServices_);
|
|
2742
|
+
if (rc != 0) {
|
|
2743
|
+
ESP_LOGE(kTag, "ble_gatts_add_svcs failed: %d", rc);
|
|
2744
|
+
return;
|
|
2745
|
+
}
|
|
2746
|
+
|
|
2747
|
+
ble_svc_gap_device_name_set(deviceName_);
|
|
2748
|
+
ble_svc_gap_device_appearance_set(appearance_);
|
|
2749
|
+
|
|
2750
|
+
BaseType_t task_ok = xTaskCreatePinnedToCore(
|
|
2751
|
+
hostTask,
|
|
2752
|
+
"nimble_host",
|
|
2753
|
+
NIMBLE_HS_STACK_SIZE,
|
|
2754
|
+
nullptr,
|
|
2755
|
+
configMAX_PRIORITIES - 4,
|
|
2756
|
+
nullptr,
|
|
2757
|
+
NIMBLE_CORE
|
|
2758
|
+
);
|
|
2759
|
+
if (task_ok != pdPASS) {
|
|
2760
|
+
ESP_LOGE(kTag, "failed to create NimBLE host task");
|
|
2761
|
+
return;
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2764
|
+
hostInited_ = true;
|
|
2765
|
+
|
|
2766
|
+
// Air-mouse simulator IMU stream: idles until a central subscribes to the custom
|
|
2767
|
+
// characteristic, then samples the QMI8658 at ~100 Hz and notifies. Harmless on boards
|
|
2768
|
+
// without the IMU (reads return defaults). Created once, on first host init.
|
|
2769
|
+
//
|
|
2770
|
+
// Stack lives in PSRAM (MALLOC_CAP_SPIRAM, allowed by CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM):
|
|
2771
|
+
// on a RAM-tight display board the old internal-RAM xTaskCreate(4096) silently failed
|
|
2772
|
+
// (NO_MEM) — the task never ran, so the sim saw 0 Hz even though the HID mouse (driven by
|
|
2773
|
+
// the app, not this task) worked fine. This task only does I2C reads + ble_gatts_notify
|
|
2774
|
+
// (no flash/cache-disable ops), so a PSRAM stack is safe. Log on failure instead of ignoring it.
|
|
2775
|
+
TaskHandle_t imuTask = nullptr;
|
|
2776
|
+
const BaseType_t imu_ok = xTaskCreateWithCaps(
|
|
2777
|
+
imuNotifyTask, "ble_imu", 4096, nullptr, 5, &imuTask, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
2778
|
+
if (imu_ok != pdPASS) {
|
|
2779
|
+
ESP_LOGE(kTag, "failed to create ble_imu task (sim IMU stream disabled)");
|
|
2780
|
+
}
|
|
2781
|
+
|
|
2782
|
+
ESP_LOGI(kTag, "BLE initialized: name=\"%s\" appearance=0x%04X", deviceName_, appearance_);
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
void HidServer::setEnabled(bool enabled)
|
|
2786
|
+
{
|
|
2787
|
+
if (enabled_ == enabled) return;
|
|
2788
|
+
|
|
2789
|
+
enabled_ = enabled;
|
|
2790
|
+
if (!hostInited_) return;
|
|
2791
|
+
|
|
2792
|
+
if (!enabled_) {
|
|
2793
|
+
stopAdvertising();
|
|
2794
|
+
// Terminate every live link, inbound and outbound alike.
|
|
2795
|
+
uint16_t handles[kMaxLinks];
|
|
2796
|
+
int n = 0;
|
|
2797
|
+
portENTER_CRITICAL(&stateMux_);
|
|
2798
|
+
for (int i = 0; i < linkCount_ && n < kMaxLinks; i++) handles[n++] = links_[i].handle;
|
|
2799
|
+
portEXIT_CRITICAL(&stateMux_);
|
|
2800
|
+
for (int i = 0; i < n; i++) {
|
|
2801
|
+
ble_gap_terminate(handles[i], BLE_ERR_REM_USER_CONN_TERM);
|
|
2802
|
+
}
|
|
2803
|
+
return;
|
|
2804
|
+
}
|
|
2805
|
+
|
|
2806
|
+
startAdvertising();
|
|
2807
|
+
}
|
|
2808
|
+
|
|
2809
|
+
const char *HidServer::mac()
|
|
2810
|
+
{
|
|
2811
|
+
if (macAddress_[0]) return macAddress_;
|
|
2812
|
+
if (formattedMac_[0]) return formattedMac_;
|
|
2813
|
+
|
|
2814
|
+
if (!hostSynced_) {
|
|
2815
|
+
uint8_t addr[6] = {0};
|
|
2816
|
+
if (esp_read_mac(addr, ESP_MAC_BT) != ESP_OK &&
|
|
2817
|
+
esp_read_mac(addr, ESP_MAC_BASE) != ESP_OK) return "";
|
|
2818
|
+
|
|
2819
|
+
std::snprintf(formattedMac_, sizeof(formattedMac_), "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
2820
|
+
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
|
2821
|
+
return formattedMac_;
|
|
2822
|
+
}
|
|
2823
|
+
|
|
2824
|
+
uint8_t own_addr_type = 0;
|
|
2825
|
+
uint8_t addr[6] = {0};
|
|
2826
|
+
int is_nrpa = 0;
|
|
2827
|
+
int rc = ble_hs_id_infer_auto(0, &own_addr_type);
|
|
2828
|
+
if (rc != 0) return "";
|
|
2829
|
+
|
|
2830
|
+
rc = ble_hs_id_copy_addr(own_addr_type, addr, &is_nrpa);
|
|
2831
|
+
if (rc != 0) return "";
|
|
2832
|
+
|
|
2833
|
+
std::snprintf(formattedMac_, sizeof(formattedMac_), "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
2834
|
+
addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
|
|
2835
|
+
return formattedMac_;
|
|
2836
|
+
}
|
|
2837
|
+
|
|
2838
|
+
void HidServer::keyTap(int hid_code)
|
|
2839
|
+
{
|
|
2840
|
+
reports_.clearKeyboard();
|
|
2841
|
+
reports_.keyDown(0, hid_code);
|
|
2842
|
+
notifyKeyboard();
|
|
2843
|
+
|
|
2844
|
+
reports_.keyUp();
|
|
2845
|
+
notifyKeyboard();
|
|
2846
|
+
}
|
|
2847
|
+
|
|
2848
|
+
void HidServer::keyDown(int modifier, int hid_code)
|
|
2849
|
+
{
|
|
2850
|
+
reports_.keyDown(modifier, hid_code);
|
|
2851
|
+
notifyKeyboard();
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
void HidServer::keyUp()
|
|
2855
|
+
{
|
|
2856
|
+
reports_.keyUp();
|
|
2857
|
+
notifyKeyboard();
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
void HidServer::mouseMove(int dx, int dy, int buttons, int wheel)
|
|
2861
|
+
{
|
|
2862
|
+
reports_.mouseMove(dx, dy, buttons, wheel);
|
|
2863
|
+
notifyMouse();
|
|
2864
|
+
}
|
|
2865
|
+
|
|
2866
|
+
void HidServer::mouseClick(int button)
|
|
2867
|
+
{
|
|
2868
|
+
mouseMove(0, 0, button, 0);
|
|
2869
|
+
mouseMove(0, 0, 0, 0);
|
|
2870
|
+
}
|
|
2871
|
+
|
|
2872
|
+
void HidServer::setBatteryLevel(uint8_t level)
|
|
2873
|
+
{
|
|
2874
|
+
if (level > 100) level = 100;
|
|
2875
|
+
if (level == batteryLevel_) return;
|
|
2876
|
+
|
|
2877
|
+
batteryLevel_ = level;
|
|
2878
|
+
ESP_LOGI(kTag, "battery level: %d%%", level);
|
|
2879
|
+
|
|
2880
|
+
if (batteryConn_ == kNoConn || batteryAttrHandle_ == 0) return;
|
|
2881
|
+
|
|
2882
|
+
struct os_mbuf *om = ble_hs_mbuf_from_flat(&batteryLevel_, 1);
|
|
2883
|
+
if (om) {
|
|
2884
|
+
ble_gatts_notify_custom(batteryConn_, batteryAttrHandle_, om);
|
|
2885
|
+
}
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
namespace {
|
|
2889
|
+
|
|
2890
|
+
struct BluetoothDriverRegistration {
|
|
2891
|
+
BluetoothDriverRegistration()
|
|
2892
|
+
{
|
|
2893
|
+
gea::framework::bluetooth::BluetoothHidDevice::setDriver(&HidServer::instance());
|
|
2894
|
+
}
|
|
2895
|
+
};
|
|
2896
|
+
|
|
2897
|
+
BluetoothDriverRegistration bluetoothDriverRegistration;
|
|
2898
|
+
|
|
2899
|
+
} // namespace
|
|
2900
|
+
|
|
2901
|
+
void registerHidDriver()
|
|
2902
|
+
{
|
|
2903
|
+
gea::framework::bluetooth::BluetoothHidDevice::setDriver(&HidServer::instance());
|
|
2904
|
+
}
|
|
2905
|
+
|
|
2906
|
+
void startOtaServer()
|
|
2907
|
+
{
|
|
2908
|
+
#ifdef GEA_EMBEDDED_BLE_OTA
|
|
2909
|
+
auto &server = HidServer::instance();
|
|
2910
|
+
server.init("Geastack OTA", 0, nullptr);
|
|
2911
|
+
server.setEnabled(true);
|
|
2912
|
+
server.startAdvertising();
|
|
2913
|
+
#endif
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
} // namespace gea::targets::esp32::ble
|