@geastack/targets 0.1.86 → 0.1.88

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.
Files changed (86) hide show
  1. package/package.json +7 -7
  2. package/targets/esp32/app_project.cmake +18 -1
  3. package/targets/esp32/chip_bindings/audio/echo_cancellation.cpp +213 -0
  4. package/targets/esp32/chip_bindings/audio/echo_cancellation.h +12 -0
  5. package/targets/esp32/chip_bindings/audio/es8311.cpp +280 -41
  6. package/targets/esp32/chip_bindings/displays/rgb_panel.cpp +115 -12
  7. package/targets/esp32/chip_bindings/expanders/io_expander.h +2 -0
  8. package/targets/esp32/chip_bindings/expanders/tca9554.cpp +74 -0
  9. package/targets/esp32/chip_bindings/touch/ft3168.cpp +23 -1
  10. package/targets/esp32/chip_bindings/touch/ft3168_controller.h +1 -0
  11. package/targets/esp32/chip_bindings/touch/gt911.cpp +4 -3
  12. package/targets/esp32/components/gea_framework/CMakeLists.txt +8 -1
  13. package/targets/esp32/components/gea_juice/CMakeLists.txt +46 -0
  14. package/targets/esp32/components/gea_juice/README.md +20 -0
  15. package/targets/esp32/components/gea_juice/juice_memory.c +19 -0
  16. package/targets/esp32/components/gea_juice/juice_memory.h +16 -0
  17. package/targets/esp32/components/gea_juice/patch_threads.py +50 -0
  18. package/targets/esp32/components/gea_vp8_peer/CMakeLists.txt +16 -0
  19. package/targets/esp32/components/gea_vp8_peer/README.md +39 -0
  20. package/targets/esp32/components/gea_vp8_peer/gea_vp8_peer.h +21 -0
  21. package/targets/esp32/components/gea_vp8_peer/vp8_peer.cpp +538 -0
  22. package/targets/esp32/components/gea_vpx/CMakeLists.txt +235 -0
  23. package/targets/esp32/components/gea_vpx/README.md +133 -0
  24. package/targets/esp32/components/gea_vpx/i420_rgb565_s3.S +169 -0
  25. package/targets/esp32/components/gea_vpx/vp8_blocks_s3.c +42 -0
  26. package/targets/esp32/components/gea_vpx/vp8_copy_s3.S +39 -0
  27. package/targets/esp32/components/gea_vpx/vp8_entropy_s3.c +76 -0
  28. package/targets/esp32/components/gea_vpx/vp8_filter_s3.S +58 -0
  29. package/targets/esp32/components/gea_vpx/vp8_idct_s3.S +269 -0
  30. package/targets/esp32/components/gea_vpx/vp8_intrapred_s3.S +65 -0
  31. package/targets/esp32/components/gea_vpx/vp8_loopfilter_s3.S +333 -0
  32. package/targets/esp32/components/gea_vpx/vp8_loopfilter_s3.c +62 -0
  33. package/targets/esp32/components/gea_vpx/vp8_profile_s3.c +22 -0
  34. package/targets/esp32/components/gea_vpx/vp8_profile_s3.h +20 -0
  35. package/targets/esp32/components/gea_vpx/vp8_s3.c +120 -0
  36. package/targets/esp32/components/gea_vpx/vp8_s3.h +58 -0
  37. package/targets/esp32/components/gea_vpx/vp8_workspace_s3.c +14 -0
  38. package/targets/esp32/components/gea_vpx/vpx_memory.c +41 -0
  39. package/targets/esp32/connectivity/diagnostics_transport.cpp +18 -0
  40. package/targets/esp32/connectivity/wifi.cpp +26 -13
  41. package/targets/esp32/crypto/srtp_hmac_psa.c +118 -0
  42. package/targets/esp32/display.cpp +13 -0
  43. package/targets/esp32/gea_framework.cmake +7 -0
  44. package/targets/esp32/host_sources.cmake +2 -0
  45. package/targets/esp32/i2c_completion_order.cmake +57 -0
  46. package/targets/esp32/patch_streaming_dependencies.py +233 -0
  47. package/targets/esp32/services/device_control.cpp +141 -5
  48. package/targets/esp32/services/frame_scheduler.cpp +29 -1
  49. package/targets/esp32/services/opus_benchmark.h +82 -0
  50. package/targets/esp32/services/pcm_simd_check.h +73 -0
  51. package/targets/esp32/services/srtp_benchmark.h +106 -0
  52. package/targets/esp32/services/video_simd_benchmark.h +477 -0
  53. package/targets/esp32/services/vp8_benchmark.h +147 -0
  54. package/targets/esp32/streaming_dependency_patches.cmake +103 -0
  55. package/targets/esp32-m5stack-m5paper/main/idf_component.yml +2 -0
  56. package/targets/esp32-p4-m5stack-tab5/main/idf_component.yml +2 -0
  57. package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/idf_component.yml +2 -0
  58. package/targets/esp32-p4-waveshare-touch-lcd-7/main/idf_component.yml +2 -0
  59. package/targets/esp32-s3-elecrow-rotary-2.1/main/idf_component.yml +2 -0
  60. package/targets/esp32-s3-epaper-1.54/main/idf_component.yml +2 -0
  61. package/targets/esp32-s3-lilygo-t-display-s3-long/main/idf_component.yml +2 -0
  62. package/targets/esp32-s3-m5stack-papers3/main/idf_component.yml +2 -0
  63. package/targets/esp32-s3-m5stack-sticks3/main/idf_component.yml +2 -0
  64. package/targets/esp32-s3-touch-amoled-1.75/main/idf_component.yml +2 -0
  65. package/targets/esp32-s3-touch-amoled-1.8/dependencies.lock +110 -2
  66. package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +29 -3
  67. package/targets/esp32-s3-touch-amoled-1.8/main/app_main.cpp +6 -3
  68. package/targets/esp32-s3-touch-amoled-1.8/main/idf_component.yml +11 -0
  69. package/targets/esp32-s3-touch-amoled-1.8/sdkconfig.defaults +9 -1
  70. package/targets/esp32-s3-touch-amoled-2.06/CMakeLists.txt +2 -0
  71. package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +171 -8
  72. package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +9 -2
  73. package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +10 -3
  74. package/targets/esp32-s3-touch-amoled-2.06/main/idf_component.yml +16 -1
  75. package/targets/esp32-s3-touch-amoled-2.06/main/include/board.h +3 -0
  76. package/targets/esp32-s3-touch-amoled-2.06/main/touch.cpp +7 -0
  77. package/targets/esp32-s3-touch-amoled-2.41/dependencies.lock +57 -1
  78. package/targets/esp32-s3-touch-amoled-2.41/main/idf_component.yml +10 -0
  79. package/targets/esp32-s3-touch-lcd-3.5b/dependencies.lock +12 -1
  80. package/targets/esp32-s3-touch-lcd-3.5b/main/CMakeLists.txt +5 -1
  81. package/targets/esp32-s3-touch-lcd-3.5b/main/idf_component.yml +2 -1
  82. package/targets/esp32-s3-touch-lcd-3.5b/main/include/board.h +3 -0
  83. package/targets/esp32-s3-touch-lcd-3.5b/main/platform_stubs.cpp +1 -18
  84. package/targets/esp32-s3-touch-lcd-3.5b/main/speaker_power.cpp +27 -0
  85. package/targets/esp32-s3-waveshare-touch-lcd-4b.json +504 -0
  86. package/targets.json +12 -1
@@ -21,6 +21,10 @@
21
21
  #include "lwip/sockets.h"
22
22
  #include "lwip/tcp.h"
23
23
  #include "sdkconfig.h"
24
+ #if CONFIG_ESP_CONSOLE_UART
25
+ #include "driver/uart.h"
26
+ #include "driver/uart_vfs.h"
27
+ #endif
24
28
  #if GEA_EMBEDDED_HEAP_DIAGNOSTICS_LOG && CONFIG_HEAP_TASK_TRACKING
25
29
  #include "esp_heap_task_info.h"
26
30
  #endif
@@ -327,6 +331,20 @@ public:
327
331
 
328
332
  void installLogSink(gea::framework::services::DiagnosticsVPrintSink sink) override
329
333
  {
334
+ #if CONFIG_ESP_CONSOLE_UART
335
+ // The default UART VFS busy-spins until every byte reaches its FIFO.
336
+ // Even a background logger then consumes media CPU, and stdout's lock
337
+ // can promote it through priority inheritance. The IDF driver blocks on
338
+ // its bounded TX ring instead, letting audio/video run while UART drains.
339
+ const auto port = static_cast<uart_port_t>(CONFIG_ESP_CONSOLE_UART_NUM);
340
+ const esp_err_t result = uart_is_driver_installed(port)
341
+ ? ESP_OK : uart_driver_install(port, 256, 2048, 0, nullptr, 0);
342
+ if (result == ESP_OK) {
343
+ uart_vfs_dev_use_driver(port);
344
+ } else {
345
+ ESP_LOGE(kTag, "Console UART driver installation failed: %s", esp_err_to_name(result));
346
+ }
347
+ #endif
330
348
  esp_log_set_vprintf(sink);
331
349
  }
332
350
 
@@ -81,27 +81,40 @@ public:
81
81
 
82
82
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
83
83
  #if CONFIG_ESP_WIFI_ENABLED
84
- // Use the smallest fixed DMA pools that leave room for BLE, the CO5300 SPI
85
- // bus, and the HTTP task on this S3. Hardware measurement after BLE showed
86
- // only 23,824 DMA bytes before WiFi: even a 3/8 RX, 2/1 TX/cache midpoint
87
- // associated but left 1,212 bytes, and display initialization failed. The
88
- // original slow OTA run also had a 6 KiB WiFi task and an 8 KiB HTTP task;
89
- // those stacks are now 8 KiB and 4 KiB respectively, so keep the viable
90
- // pool sizes and measure the corrected task configuration independently.
84
+ // Keep the fixed RX DMA pool small enough to leave room for BLE, the
85
+ // CO5300 SPI bus, and the HTTP task on this S3.
91
86
  // TCP OTA is the primary bulk receive path. Buy its receive depth with the
92
87
  // DYNAMIC frames, not the static ones: static RX frames are permanent
93
88
  // internal DMA allocations (1600 B each) taken from the same few tens of KiB
94
89
  // the display staging needs, while dynamic frames are allocated on demand
95
90
  // and land in PSRAM under CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP. Sixteen
96
- // dynamic frames match the widened TCP receive window; four static frames
97
- // are the minimum that still permits an 8-deep block-ack window.
91
+ // dynamic frames match the widened TCP receive window. Duplex streaming
92
+ // apps can raise both pools below: queued TCP pbufs retain dynamic RX
93
+ // buffers, so those apps need headroom for ACK, DNS, and ARP traffic too.
98
94
  cfg.static_rx_buf_num = 4;
99
95
  cfg.dynamic_rx_buf_num = 16;
100
- cfg.rx_ba_win = 8; // ESP-IDF requires <= 2 * static_rx_buf_num.
101
- cfg.static_tx_buf_num = 1;
102
- cfg.cache_tx_buf_num = 1;
96
+ #if defined(GEA_EMBEDDED_WIFI_STATIC_RX_BUFFERS)
97
+ cfg.static_rx_buf_num = GEA_EMBEDDED_WIFI_STATIC_RX_BUFFERS;
98
+ #endif
99
+ #if defined(GEA_EMBEDDED_WIFI_DYNAMIC_RX_BUFFERS)
100
+ cfg.dynamic_rx_buf_num = GEA_EMBEDDED_WIFI_DYNAMIC_RX_BUFFERS;
101
+ #endif
102
+ // With AMPDU RX, ESP-IDF recommends static RX >= the block-ack window
103
+ // for throughput and compatibility; streaming apps should use >= 8.
104
+ cfg.rx_ba_win = 8;
105
+ // Preserve the configured TX pools and aggregation. A one-frame DMA pool
106
+ // plus one cached packet cannot carry microphone uploads alongside the
107
+ // ACKs for incoming audio: wlanif returns ERR_MEM and TCP waits for retry.
108
+ // Board sdkconfig owns these capacities, not this shared driver.
109
+ #if defined(GEA_EMBEDDED_WIFI_STATIC_TX_BUFFERS)
110
+ cfg.static_tx_buf_num = GEA_EMBEDDED_WIFI_STATIC_TX_BUFFERS;
111
+ #endif
112
+ #if defined(GEA_EMBEDDED_WIFI_CACHE_TX_BUFFERS)
113
+ // Duplex streams need room for uploads and receive ACKs at the same
114
+ // time. Cache overflow otherwise drops packets into TCP's RTO path.
115
+ cfg.cache_tx_buf_num = GEA_EMBEDDED_WIFI_CACHE_TX_BUFFERS;
116
+ #endif
103
117
  cfg.mgmt_sbuf_num = 6;
104
- cfg.ampdu_tx_enable = 0;
105
118
  // Give the blob's "wifi" task a stack it can authenticate on.
106
119
  applyTaskStackOverride(&cfg);
107
120
  #endif // CONFIG_ESP_WIFI_ENABLED — native RX-buf/osi tuning; remote (P4/C6) uses defaults
@@ -0,0 +1,118 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // IDF 6's public PSA hash API selects Espressif's locked SHA peripheral driver.
3
+ // Preserve libsrtp's HMAC-SHA1 wire tags, including incremental RTP/ROC input.
4
+ #include "auth.h"
5
+ #include "alloc.h"
6
+ #include "err.h"
7
+ #include "auth_test_cases.h"
8
+ #include "psa/crypto.h"
9
+ #include <string.h>
10
+
11
+ typedef struct {
12
+ psa_hash_operation_t inner;
13
+ psa_hash_operation_t outer;
14
+ psa_hash_operation_t work;
15
+ } gea_hmac_state_t;
16
+
17
+ typedef struct {
18
+ srtp_auth_t auth;
19
+ gea_hmac_state_t state;
20
+ } gea_hmac_t;
21
+
22
+ extern const srtp_auth_type_t srtp_hmac;
23
+ srtp_debug_module_t srtp_mod_hmac = { false, "hmac sha-1 psa" };
24
+
25
+ static srtp_err_status_t gea_hmac_alloc(srtp_auth_t **auth, size_t key_len, size_t out_len) {
26
+ if (key_len > 20 || out_len > 20) return srtp_err_status_bad_param;
27
+ gea_hmac_t *instance = srtp_crypto_alloc(sizeof(*instance));
28
+ if (!instance) return srtp_err_status_alloc_fail;
29
+ instance->state.inner = psa_hash_operation_init();
30
+ instance->state.outer = psa_hash_operation_init();
31
+ instance->state.work = psa_hash_operation_init();
32
+ instance->auth.type = &srtp_hmac;
33
+ instance->auth.state = &instance->state;
34
+ instance->auth.key_len = key_len;
35
+ instance->auth.out_len = out_len;
36
+ instance->auth.prefix_len = 0;
37
+ *auth = &instance->auth;
38
+ return srtp_err_status_ok;
39
+ }
40
+
41
+ static srtp_err_status_t gea_hmac_dealloc(srtp_auth_t *auth) {
42
+ gea_hmac_t *instance = (gea_hmac_t *)auth;
43
+ psa_hash_abort(&instance->state.work);
44
+ psa_hash_abort(&instance->state.inner);
45
+ psa_hash_abort(&instance->state.outer);
46
+ octet_string_set_to_zero(instance, sizeof(*instance));
47
+ srtp_crypto_free(instance);
48
+ return srtp_err_status_ok;
49
+ }
50
+
51
+ static srtp_err_status_t gea_hmac_start(void *context) {
52
+ gea_hmac_state_t *state = context;
53
+ if (psa_hash_abort(&state->work) != PSA_SUCCESS ||
54
+ psa_hash_clone(&state->inner, &state->work) != PSA_SUCCESS)
55
+ return srtp_err_status_auth_fail;
56
+ return srtp_err_status_ok;
57
+ }
58
+
59
+ static srtp_err_status_t gea_hmac_init(void *context, const uint8_t *key, size_t key_len) {
60
+ if (key_len > 20 || (key_len && !key)) return srtp_err_status_bad_param;
61
+ gea_hmac_state_t *state = context;
62
+ uint8_t inner_pad[64], outer_pad[64];
63
+ memset(inner_pad, 0x36, sizeof(inner_pad));
64
+ memset(outer_pad, 0x5c, sizeof(outer_pad));
65
+ for (size_t i = 0; i < key_len; ++i) {
66
+ inner_pad[i] ^= key[i];
67
+ outer_pad[i] ^= key[i];
68
+ }
69
+ psa_hash_abort(&state->work);
70
+ psa_hash_abort(&state->inner);
71
+ psa_hash_abort(&state->outer);
72
+ const int ok = psa_crypto_init() == PSA_SUCCESS &&
73
+ psa_hash_setup(&state->inner, PSA_ALG_SHA_1) == PSA_SUCCESS &&
74
+ psa_hash_update(&state->inner, inner_pad, sizeof(inner_pad)) == PSA_SUCCESS &&
75
+ psa_hash_setup(&state->outer, PSA_ALG_SHA_1) == PSA_SUCCESS &&
76
+ psa_hash_update(&state->outer, outer_pad, sizeof(outer_pad)) == PSA_SUCCESS;
77
+ octet_string_set_to_zero(inner_pad, sizeof(inner_pad));
78
+ octet_string_set_to_zero(outer_pad, sizeof(outer_pad));
79
+ if (!ok) return srtp_err_status_auth_fail;
80
+ return gea_hmac_start(state);
81
+ }
82
+
83
+ static srtp_err_status_t gea_hmac_update(void *context, const uint8_t *message, size_t size) {
84
+ gea_hmac_state_t *state = context;
85
+ if (size && !message) return srtp_err_status_bad_param;
86
+ if (size && psa_hash_update(&state->work, message, size) != PSA_SUCCESS)
87
+ return srtp_err_status_auth_fail;
88
+ return srtp_err_status_ok;
89
+ }
90
+
91
+ static srtp_err_status_t gea_hmac_compute(void *context, const uint8_t *message, size_t size,
92
+ size_t tag_len, uint8_t *tag) {
93
+ gea_hmac_state_t *state = context;
94
+ if (tag_len > 20 || (tag_len && !tag)) return srtp_err_status_bad_param;
95
+ uint8_t inner_digest[20] = {0}, digest[20] = {0};
96
+ size_t length = 0;
97
+ const int ok = gea_hmac_update(state, message, size) == srtp_err_status_ok &&
98
+ psa_hash_finish(&state->work, inner_digest, sizeof(inner_digest), &length) == PSA_SUCCESS &&
99
+ length == sizeof(inner_digest) &&
100
+ psa_hash_clone(&state->outer, &state->work) == PSA_SUCCESS &&
101
+ psa_hash_update(&state->work, inner_digest, sizeof(inner_digest)) == PSA_SUCCESS &&
102
+ psa_hash_finish(&state->work, digest, sizeof(digest), &length) == PSA_SUCCESS &&
103
+ length == sizeof(digest);
104
+ if (ok && tag_len) memcpy(tag, digest, tag_len);
105
+ octet_string_set_to_zero(inner_digest, sizeof(inner_digest));
106
+ octet_string_set_to_zero(digest, sizeof(digest));
107
+ if (!ok) {
108
+ psa_hash_abort(&state->work);
109
+ return srtp_err_status_auth_fail;
110
+ }
111
+ return srtp_err_status_ok;
112
+ }
113
+
114
+ const srtp_auth_type_t srtp_hmac = {
115
+ gea_hmac_alloc, gea_hmac_dealloc, gea_hmac_init, gea_hmac_compute,
116
+ gea_hmac_update, gea_hmac_start, "HMAC-SHA1 through ESP-IDF PSA",
117
+ &srtp_hmac_test_case_0, SRTP_HMAC_SHA1
118
+ };
@@ -5926,11 +5926,18 @@ namespace
5926
5926
  // to blocking so CPU1's idle task + the task watchdog still run.
5927
5927
  while (g_geaRenderWorker.jobSeq.load(std::memory_order_acquire) == seen)
5928
5928
  {
5929
+ #if defined(GEA_EMBEDDED_APP_USES_AUDIO) && GEA_EMBEDDED_APP_USES_AUDIO
5930
+ // Audio/video workloads need this core between raster jobs. Submit
5931
+ // publishes the sequence before notifying, so blocking has no lost
5932
+ // wakeup and does not require an idle spin window.
5933
+ ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
5934
+ #else
5929
5935
  if (esp_timer_get_time() - idleSince > 4000)
5930
5936
  {
5931
5937
  ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(100));
5932
5938
  idleSince = esp_timer_get_time();
5933
5939
  }
5940
+ #endif
5934
5941
  }
5935
5942
  seen = g_geaRenderWorker.jobSeq.load(std::memory_order_acquire);
5936
5943
  const std::int64_t s = esp_timer_get_time();
@@ -6030,6 +6037,12 @@ extern "C" void gea_render_parallel_wait()
6030
6037
  const std::int64_t deadline = w0 + 100000; // 100ms safety net
6031
6038
  while (!g_geaRenderWorker.done.load(std::memory_order_acquire))
6032
6039
  {
6040
+ #if defined(GEA_EMBEDDED_APP_USES_AUDIO) && GEA_EMBEDDED_APP_USES_AUDIO
6041
+ // Media load makes a band much longer than the graphics-only spin window.
6042
+ // Its worker already notifies completion: park instead of burning CPU0
6043
+ // while the microphone encoder and idle task need to run.
6044
+ ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(2));
6045
+ #endif
6033
6046
  if (esp_timer_get_time() > deadline)
6034
6047
  {
6035
6048
  std::printf("[render-worker] WAIT TIMEOUT — worker wedged?\n");
@@ -140,6 +140,12 @@ if(COMMAND idf_build_set_property)
140
140
  # file is a board, and the runtime only tests that the macro is defined, so it is set here for
141
141
  # the whole build: the app, main and the framework must agree on the pool layout.
142
142
  idf_build_set_property(COMPILE_DEFINITIONS "GEA_EMBEDDED_CPP_BOARD" APPEND)
143
+ # Startup and the frame loop are one serialized JS mutator, even when boot
144
+ # runs on main_task and hands the heap to an external-stack runtime task.
145
+ # Per-OS-thread pools/collector lists would strand startup candidates with
146
+ # their buffered bits set; the loop then mistakes those bits for graph IDs.
147
+ # Native workers enqueue events; application callbacks run on the loop.
148
+ idf_build_set_property(COMPILE_DEFINITIONS "GEA_RUNTIME_SINGLE_THREADED=1" APPEND)
143
149
  endif()
144
150
 
145
151
  # Linker fragments are absolute paths from the CLI. ldgen merges every
@@ -206,6 +212,7 @@ set(GEA_FW_APP_SENSITIVE_SOURCES
206
212
  "${GEA_HOST}/host/http.cpp"
207
213
  "${GEA_HOST}/host/image.cpp"
208
214
  "${GEA_HOST}/host/rtc.cpp"
215
+ "${GEA_HOST}/host/mjpeg.cpp"
209
216
  "${GEA_HOST}/host/tile_loader.cpp"
210
217
  "${GEA_HOST}/services/bluetooth_service.cpp"
211
218
  "${GEA_HOST}/services/network_services.cpp"
@@ -14,6 +14,8 @@
14
14
  set(GEA_EMBEDDED_HOST_SOURCES
15
15
  "${GEA_HOST}/host/apps.cpp"
16
16
  "${GEA_HOST}/host/audio.cpp"
17
+ "${GEA_HOST}/host/video.cpp"
18
+ "${GEA_HOST}/host/mjpeg.cpp"
17
19
  "${GEA_HOST}/host/ble.cpp"
18
20
  "${GEA_HOST}/host/display.cpp"
19
21
  "${GEA_HOST}/host/fetch.cpp"
@@ -0,0 +1,57 @@
1
+ # ESP-IDF 6.0.x wakes a synchronous I2C caller before copying the final RX
2
+ # bytes. On dual-core chips that caller can replace i2c_trans.ops while the
3
+ # ISR still uses it (observed with GT911 reads followed by an address probe).
4
+ # Compile a corrected build-local source, leaving the installed SDK untouched.
5
+ function(gea_fix_i2c_completion_order)
6
+ if(NOT TARGET __idf_esp_driver_i2c)
7
+ return()
8
+ endif()
9
+
10
+ get_target_property(component_dir __idf_esp_driver_i2c SOURCE_DIR)
11
+ set(source "${component_dir}/i2c_master.c")
12
+ file(READ "${source}" contents)
13
+ set(notify [=[ if (i2c_master->event != I2C_EVENT_ALIVE) {
14
+ xQueueSendFromISR(i2c_master->event_queue, (void *)&i2c_master->event, &HPTaskAwoken);
15
+ }
16
+ ]=])
17
+ set(receive [=[ if (i2c_master->contains_read == true) {
18
+ if (int_mask & I2C_LL_INTR_MST_COMPLETE || int_mask & I2C_LL_INTR_END_DETECT) {
19
+ i2c_isr_receive_handler(i2c_master);
20
+ }
21
+ }
22
+ ]=])
23
+ string(FIND "${contents}" "${notify}${receive}" unsafe_order)
24
+ if(unsafe_order EQUAL -1)
25
+ # A newer SDK may already do the copy first. Do not rewrite a driver
26
+ # whose implementation differs from the precisely identified defect.
27
+ string(FIND "${contents}" "${receive}${notify}" fixed_order)
28
+ if(fixed_order EQUAL -1)
29
+ message(FATAL_ERROR "I2C completion ordering is unknown for this SDK; review i2c_master.c before using concurrent reads/probes")
30
+ endif()
31
+ return()
32
+ endif()
33
+
34
+ string(REPLACE "${notify}${receive}" "${receive}${notify}" corrected "${contents}")
35
+ set(output "${CMAKE_BINARY_DIR}/gea_i2c_master.c")
36
+ if(EXISTS "${output}")
37
+ file(READ "${output}" previous)
38
+ endif()
39
+ if(NOT "${previous}" STREQUAL "${corrected}")
40
+ file(WRITE "${output}" "${corrected}")
41
+ endif()
42
+ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${source}")
43
+ get_target_property(sources __idf_esp_driver_i2c SOURCES)
44
+ set(replaced_sources)
45
+ foreach(entry IN LISTS sources)
46
+ if(entry STREQUAL "i2c_master.c" OR entry STREQUAL "${source}")
47
+ list(APPEND replaced_sources "${output}")
48
+ elseif(IS_ABSOLUTE "${entry}" OR entry MATCHES "^\\$<")
49
+ list(APPEND replaced_sources "${entry}")
50
+ else()
51
+ list(APPEND replaced_sources "${component_dir}/${entry}")
52
+ endif()
53
+ endforeach()
54
+ set_property(TARGET __idf_esp_driver_i2c PROPERTY SOURCES "${replaced_sources}")
55
+ target_include_directories(__idf_esp_driver_i2c PRIVATE "${component_dir}")
56
+ message(STATUS "I2C: finish RX copy before notifying synchronous callers")
57
+ endfunction()
@@ -0,0 +1,233 @@
1
+ """Generate narrowly patched SDK sources in the normal build output.
2
+
3
+ Registry sources stay untouched. Reject source drift instead of guessing where
4
+ allocation/lifetime fixes belong. Espressif WebSocket 1.8 / codec-dev 1.6.2,
5
+ ESP-IDF 6.0.2 WebSocket transport.
6
+ """
7
+ from pathlib import Path
8
+ import sys
9
+
10
+
11
+ def replace_once(source, old, new):
12
+ if source.count(old) != 1:
13
+ raise ValueError(f"SDK source drift at {old[:80]!r}")
14
+ return source.replace(old, new, 1)
15
+
16
+
17
+ def patch_websocket(source):
18
+ source = replace_once(source, '#include "freertos/task.h"',
19
+ '#include "freertos/task.h"\n#include "freertos/idf_additions.h"\n#include "esp_heap_caps.h"')
20
+ source = replace_once(source,
21
+ ' destroy_and_free_resources(client);\n return ESP_OK;\n}',
22
+ ''' // STOPPED_BIT is published after the last client access by its task.
23
+ // Reap the suspended task here; never free a running external stack.
24
+ if (client->task_handle) {
25
+ while (eTaskGetState(client->task_handle) != eSuspended) {
26
+ vTaskDelay(1);
27
+ }
28
+ vTaskDeleteWithCaps(client->task_handle);
29
+ client->task_handle = NULL;
30
+ }
31
+ destroy_and_free_resources(client);
32
+ return ESP_OK;
33
+ }''')
34
+ source = replace_once(source,
35
+ ''' if (client->selected_for_destroying == true) {
36
+ destroy_and_free_resources(client);
37
+ } else {
38
+ xEventGroupSetBits(client->status_bits, STOPPED_BIT);
39
+ }
40
+ vTaskDelete(NULL);''',
41
+ ''' if (client->selected_for_destroying == true) {
42
+ destroy_and_free_resources(client);
43
+ vTaskDeleteWithCaps(NULL);
44
+ } else {
45
+ xEventGroupSetBits(client->status_bits, STOPPED_BIT);
46
+ vTaskSuspend(NULL); // destroy/start joins and frees this PSRAM stack.
47
+ }''')
48
+ source = replace_once(source, ' client->transport = client->config->ext_transport;',
49
+ ''' if (client->task_handle) {
50
+ while (eTaskGetState(client->task_handle) != eSuspended) {
51
+ vTaskDelay(1);
52
+ }
53
+ vTaskDeleteWithCaps(client->task_handle);
54
+ client->task_handle = NULL;
55
+ }
56
+ client->transport = client->config->ext_transport;''')
57
+ source = replace_once(source, ' if (xTaskCreatePinnedToCore(esp_websocket_client_task,',
58
+ ''' const UBaseType_t stack_caps = MALLOC_CAP_8BIT |
59
+ (heap_caps_get_total_size(MALLOC_CAP_SPIRAM) ? MALLOC_CAP_SPIRAM : MALLOC_CAP_INTERNAL);
60
+ if (xTaskCreatePinnedToCoreWithCaps(esp_websocket_client_task,''')
61
+ source = replace_once(source, '&client->task_handle, client->config->task_core_id) != pdTRUE)',
62
+ '&client->task_handle, client->config->task_core_id, stack_caps) != pdTRUE)')
63
+ source = replace_once(source,
64
+ ''' // Now acquire tx_lock with timeout (consistent with PONG handling)
65
+ if (xSemaphoreTakeRecursive(client->tx_lock, WEBSOCKET_TX_LOCK_TIMEOUT_MS) != pdPASS) {
66
+ ESP_LOGE(TAG, "Could not lock ws-client within %d timeout for PING", WEBSOCKET_TX_LOCK_TIMEOUT_MS);''',
67
+ ''' // Optional heartbeats must not suspend incoming media behind
68
+ // a busy sender. The next ping interval retries; PONG/CLOSE
69
+ // retain their required handshake behavior.
70
+ if (xSemaphoreTakeRecursive(client->tx_lock, 0) != pdPASS) {
71
+ ESP_LOGD(TAG, "Skipping PING while transmitter is busy");''')
72
+ return source
73
+
74
+
75
+ def patch_ws_transport(source):
76
+ source = replace_once(source, '#include "esp_tls_crypto.h"',
77
+ '#include "esp_tls_crypto.h"\n#include "esp_timer.h"\n#include "esp_heap_caps.h"')
78
+ source = replace_once(source, '#define MAX_WEBSOCKET_HEADER_SIZE 16',
79
+ '#define MAX_WEBSOCKET_HEADER_SIZE 16\n#define GEA_WS_TX_BUFFER_SIZE 4096')
80
+ source = replace_once(source, ' size_t response_header_len;\n} transport_ws_t;',
81
+ ''' size_t response_header_len;
82
+ char *gea_tx_buffer; /*!< Separate from handshake/RX storage; writers are serialized by the client. */
83
+ } transport_ws_t;''')
84
+ source = replace_once(source, ' free(ws->redir_host);\n free(ws->path);',
85
+ ' free(ws->gea_tx_buffer);\n free(ws->redir_host);\n free(ws->path);')
86
+ source = replace_once(source,
87
+ ''' char *buffer = (char *)b;
88
+ char ws_header[MAX_WEBSOCKET_HEADER_SIZE];
89
+ char *mask;
90
+ int header_len = 0, i;
91
+
92
+ int poll_write;
93
+ if ((poll_write = esp_transport_poll_write(ws->parent, timeout_ms)) <= 0) {
94
+ ESP_LOGE(TAG, "Error transport_poll_write(%d)", poll_write);
95
+ return poll_write;
96
+ }''',
97
+ ''' char ws_header[MAX_WEBSOCKET_HEADER_SIZE];
98
+ int header_len = 0;
99
+ if (len < 0 || (len && !b)) {
100
+ return -1;
101
+ }
102
+ const int64_t deadline_us = timeout_ms < 0 ? 0 :
103
+ esp_timer_get_time() + (int64_t)timeout_ms * 1000;
104
+ if (!ws->gea_tx_buffer) {
105
+ // Keep the bounded staging buffer out of scarce DMA/internal RAM
106
+ // when PSRAM is available. Non-PSRAM boards retain malloc fallback.
107
+ ws->gea_tx_buffer = heap_caps_malloc(GEA_WS_TX_BUFFER_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
108
+ if (!ws->gea_tx_buffer) {
109
+ ws->gea_tx_buffer = malloc(GEA_WS_TX_BUFFER_SIZE);
110
+ }
111
+ if (!ws->gea_tx_buffer) {
112
+ ESP_LOGE(TAG, "Cannot allocate WebSocket transmit buffer");
113
+ return -1;
114
+ }
115
+ }''')
116
+ source = replace_once(source,
117
+ ''' if (mask_flag) {
118
+ mask = &ws_header[header_len];
119
+ ssize_t rc;
120
+ if ((rc = getrandom(ws_header + header_len, 4, 0)) < 0) {
121
+ ESP_LOGD(TAG, "getrandom() returned %zd", rc);
122
+ return -1;
123
+ }
124
+ header_len += 4;
125
+
126
+ for (i = 0; i < len; ++i) {
127
+ buffer[i] = (buffer[i] ^ mask[i % 4]);
128
+ }
129
+ }
130
+
131
+ if (esp_transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) {
132
+ ESP_LOGE(TAG, "Error write header");
133
+ return -1;
134
+ }
135
+ if (len == 0) {
136
+ return 0;
137
+ }
138
+
139
+ int ret = esp_transport_write(ws->parent, buffer, len, timeout_ms);
140
+ // in case of masked transport we have to revert back to the original data, as ws layer
141
+ // does not create its own copy of data to be sent
142
+ if (mask_flag) {
143
+ mask = &ws_header[header_len - 4];
144
+ for (i = 0; i < len; ++i) {
145
+ buffer[i] = (buffer[i] ^ mask[i % 4]);
146
+ }
147
+ }
148
+ return ret;''',
149
+ ''' if (mask_flag) {
150
+ const ssize_t rc = getrandom(ws_header + header_len, 4, 0);
151
+ if (rc != 4) {
152
+ ESP_LOGD(TAG, "getrandom() returned %zd", rc);
153
+ return -1;
154
+ }
155
+ header_len += 4;
156
+ }
157
+
158
+ // A standalone WS header becomes a tiny TLS record, followed by another
159
+ // socket-writability wait before its payload. Coalesce both; common audio
160
+ // frames fit in one TLS write. Larger frames keep the same WS framing and
161
+ // masking position while streaming through this bounded staging buffer.
162
+ memcpy(ws->gea_tx_buffer, ws_header, header_len);
163
+ int prefix = header_len;
164
+ int offset = 0;
165
+ do {
166
+ const int capacity = GEA_WS_TX_BUFFER_SIZE - prefix;
167
+ const int count = len - offset < capacity ? len - offset : capacity;
168
+ for (int i = 0; i < count; ++i) {
169
+ const char mask = mask_flag ? ws_header[header_len - 4 + ((offset + i) & 3)] : 0;
170
+ ws->gea_tx_buffer[prefix + i] = b[offset + i] ^ mask;
171
+ }
172
+ const int chunk_len = prefix + count;
173
+ int written = 0;
174
+ while (written < chunk_len) {
175
+ int remaining_ms = timeout_ms;
176
+ if (timeout_ms >= 0) {
177
+ const int64_t left_us = deadline_us - esp_timer_get_time();
178
+ remaining_ms = left_us > 0 ? (int)((left_us + 999) / 1000) : 0;
179
+ }
180
+ const int ret = esp_transport_write(ws->parent, ws->gea_tx_buffer + written,
181
+ chunk_len - written, remaining_ms);
182
+ if (ret <= 0 || ret > chunk_len - written) {
183
+ // Once any frame bytes went out, retrying as a fresh WS frame
184
+ // would corrupt the stream. Let the client abort the connection.
185
+ ESP_LOGE(TAG, "Error write WebSocket frame (%d)", ret);
186
+ return -1;
187
+ }
188
+ written += ret;
189
+ }
190
+ offset += count;
191
+ prefix = 0;
192
+ } while (offset < len);
193
+ return len;''')
194
+ return source
195
+
196
+
197
+ def patch_codec(source):
198
+ return replace_once(source,
199
+ ''' ret = set_drv_fs(channel, true, bits_per_sample, i2s_data->clk_src, fs);
200
+ _i2s_drv_enable(paired, true, true);''',
201
+ ''' ret = set_drv_fs(channel, true, bits_per_sample, i2s_data->clk_src, fs);
202
+ // Reconfiguration may fail after releasing DMA. Enabling that channel
203
+ // dereferences a null DMA descriptor in ESP-IDF; propagate the failure.
204
+ if (ret != ESP_CODEC_DEV_OK) {
205
+ return ret;
206
+ }
207
+ ret = _i2s_drv_enable(paired, true, true);''')
208
+
209
+
210
+ def patch_srtp_aes(source):
211
+ # IDF 6 removed mbedtls/aes.h, but exposes the same CTR contract through
212
+ # its native, internally locked AES peripheral driver. Keep libsrtp's
213
+ # counter/salt construction and error handling unchanged.
214
+ source = replace_once(source, '#include <mbedtls/aes.h>', '#include "aes/esp_aes.h"')
215
+ source = replace_once(source, '#include "aes_icm_ext.h"', '#include "gea_srtp_aes_icm.h"')
216
+ source = source.replace('mbedtls_aes_setkey_enc', 'esp_aes_setkey')
217
+ return source.replace('mbedtls_aes_', 'esp_aes_')
218
+
219
+
220
+ def patch_srtp_aes_header(source):
221
+ source = replace_once(source, '#include <mbedtls/aes.h>', '#include "aes/esp_aes.h"')
222
+ return replace_once(source, 'mbedtls_aes_context *ctx;', 'esp_aes_context *ctx;')
223
+
224
+
225
+ if __name__ == '__main__':
226
+ kind, source_file, destination_file = sys.argv[1:]
227
+ source = Path(source_file).read_text()
228
+ patched = {'websocket': patch_websocket, 'ws_transport': patch_ws_transport,
229
+ 'codec': patch_codec, 'srtp_aes': patch_srtp_aes,
230
+ 'srtp_aes_header': patch_srtp_aes_header}[kind](source)
231
+ destination = Path(destination_file)
232
+ if not destination.exists() or destination.read_text() != patched:
233
+ destination.write_text(patched)