@geastack/host 0.1.8

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 (67) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +25 -0
  3. package/audio_runtime.cpp +807 -0
  4. package/bluetooth.cpp +125 -0
  5. package/geolocation.cpp +43 -0
  6. package/host/apps.cpp +20 -0
  7. package/host/audio.cpp +240 -0
  8. package/host/ble.cpp +239 -0
  9. package/host/camera.cpp +193 -0
  10. package/host/display.cpp +230 -0
  11. package/host/fetch.cpp +1060 -0
  12. package/host/geolocation.cpp +31 -0
  13. package/host/gpio.cpp +55 -0
  14. package/host/http.cpp +349 -0
  15. package/host/image.cpp +335 -0
  16. package/host/imu.cpp +51 -0
  17. package/host/input.cpp +12 -0
  18. package/host/media.cpp +579 -0
  19. package/host/memory.cpp +91 -0
  20. package/host/rtc.cpp +212 -0
  21. package/host/rtc_esp.cpp +291 -0
  22. package/host/tile_loader.cpp +478 -0
  23. package/host/timers.cpp +327 -0
  24. package/host/touch.cpp +25 -0
  25. package/host/websocket.cpp +279 -0
  26. package/host/wifi.cpp +99 -0
  27. package/include/gea/embedded-host.h +38 -0
  28. package/include/host/apps.h +15 -0
  29. package/include/host/audio.h +158 -0
  30. package/include/host/backends.h +455 -0
  31. package/include/host/battery.h +25 -0
  32. package/include/host/camera.h +236 -0
  33. package/include/host/clock.h +45 -0
  34. package/include/host/device_control.h +23 -0
  35. package/include/host/display.h +266 -0
  36. package/include/host/display_orientation.h +291 -0
  37. package/include/host/fetch.h +310 -0
  38. package/include/host/gpio.h +36 -0
  39. package/include/host/http.h +161 -0
  40. package/include/host/image.h +144 -0
  41. package/include/host/input.h +14 -0
  42. package/include/host/media.h +165 -0
  43. package/include/host/memory.h +44 -0
  44. package/include/host/navigator.h +283 -0
  45. package/include/host/notify.h +41 -0
  46. package/include/host/profiler.h +49 -0
  47. package/include/host/rtc.h +174 -0
  48. package/include/host/storage.h +141 -0
  49. package/include/host/tile_loader.h +59 -0
  50. package/include/host/timers.h +41 -0
  51. package/include/host/touch.h +20 -0
  52. package/include/host/websocket.h +179 -0
  53. package/include/host/window.h +31 -0
  54. package/input.cpp +68 -0
  55. package/package.json +27 -0
  56. package/services/app_state.cpp +19 -0
  57. package/services/battery_service.cpp +19 -0
  58. package/services/bluetooth_service.cpp +37 -0
  59. package/services/diagnostics.cpp +233 -0
  60. package/services/diagnostics_connection.cpp +231 -0
  61. package/services/diagnostics_internal.h +15 -0
  62. package/services/diagnostics_server.cpp +135 -0
  63. package/services/mirror.cpp +364 -0
  64. package/services/network_services.cpp +62 -0
  65. package/services/runtime_log.cpp +23 -0
  66. package/services/settings_surface.h +221 -0
  67. package/wifi.cpp +185 -0
@@ -0,0 +1,327 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "host/timers.h"
3
+
4
+ #include "gea_perf_config.h" // GEA_EMBEDDED_RAF_PERF (+ GEA_EMBEDDED_PERF master)
5
+
6
+ #include <cmath>
7
+ #include <cstdio>
8
+ #include <cstdlib>
9
+ #ifdef ESP_PLATFORM
10
+ #include "esp_timer.h"
11
+ #else
12
+ #include <chrono>
13
+ #endif
14
+ #include <algorithm>
15
+ #include <limits>
16
+ #include <memory>
17
+ #include <mutex>
18
+ #include <string>
19
+ #include <vector>
20
+ #include <utility>
21
+
22
+ namespace gea::host {
23
+
24
+ namespace {
25
+
26
+ std::int64_t nowUs()
27
+ {
28
+ #ifdef ESP_PLATFORM
29
+ return esp_timer_get_time();
30
+ #else
31
+ using Clock = std::chrono::steady_clock;
32
+ return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now().time_since_epoch()).count();
33
+ #endif
34
+ }
35
+
36
+ // Perf-only clock for the requestAnimationFrame queue timing below. Returns 0
37
+ // (no timer read) when raf perf is compiled out, so the bracketing reads and the
38
+ // perfStats_ accumulation fold away. nowUs() above stays the FUNCTIONAL clock
39
+ // (timer scheduling / now()); only this gated variant is used for perf timing.
40
+ [[maybe_unused]] std::int64_t rafPerfNowUs()
41
+ {
42
+ #if GEA_EMBEDDED_RAF_PERF
43
+ return nowUs();
44
+ #else
45
+ return 0;
46
+ #endif
47
+ }
48
+
49
+ double nextTimerId()
50
+ {
51
+ static std::int64_t id = 1;
52
+ return static_cast<double>(id++);
53
+ }
54
+
55
+ double normalizeDelayMs(double delayMs)
56
+ {
57
+ if (!std::isfinite(delayMs) || delayMs < 0) return 0;
58
+ return delayMs;
59
+ }
60
+
61
+ struct ScheduledTimer {
62
+ double id = 0;
63
+ TimerCallback callback;
64
+ double delayMs = 0;
65
+ double nextFireMs = 0;
66
+ bool interval = false;
67
+ bool active = true;
68
+ bool running = false;
69
+ };
70
+
71
+ class TimerScheduler {
72
+ public:
73
+ static TimerScheduler &instance()
74
+ {
75
+ static TimerScheduler scheduler;
76
+ return scheduler;
77
+ }
78
+
79
+ double schedule(TimerCallback callback, double delayMs, bool interval)
80
+ {
81
+ if (!callback) return 0;
82
+ auto timer = std::make_shared<ScheduledTimer>();
83
+ timer->id = nextTimerId();
84
+ timer->callback = std::move(callback);
85
+ timer->delayMs = normalizeDelayMs(delayMs);
86
+ timer->nextFireMs = currentTimeMs() + timer->delayMs;
87
+ timer->interval = interval;
88
+ if (timer->interval && timer->delayMs <= 0) timer->delayMs = 1;
89
+ std::lock_guard<std::mutex> lock(mutex_);
90
+ timers_.push_back(timer);
91
+ return timer->id;
92
+ }
93
+
94
+ void clear(double id)
95
+ {
96
+ std::lock_guard<std::mutex> lock(mutex_);
97
+ for (auto &timer : timers_) {
98
+ if (timer && timer->id == id) {
99
+ timer->active = false;
100
+ return;
101
+ }
102
+ }
103
+ }
104
+
105
+ void run(AnimationFrameTimestamp timestampMs)
106
+ {
107
+ if (!std::isfinite(timestampMs)) timestampMs = currentTimeMs();
108
+ lastTimestampMs_ = timestampMs;
109
+
110
+ std::vector<std::shared_ptr<ScheduledTimer>> due;
111
+ {
112
+ std::lock_guard<std::mutex> lock(mutex_);
113
+ for (auto &timer : timers_) {
114
+ if (!timer || !timer->active || timer->running) continue;
115
+ if (timer->nextFireMs <= timestampMs) {
116
+ timer->running = true;
117
+ due.push_back(timer);
118
+ }
119
+ }
120
+ }
121
+
122
+ for (auto &timer : due) {
123
+ if (timer->active && timer->callback) timer->callback();
124
+
125
+ std::lock_guard<std::mutex> lock(mutex_);
126
+ timer->running = false;
127
+ if (!timer->active) continue;
128
+ if (timer->interval) {
129
+ timer->nextFireMs = timestampMs + std::max(1.0, timer->delayMs);
130
+ } else {
131
+ timer->active = false;
132
+ }
133
+ }
134
+
135
+ compactInactiveTimers();
136
+ }
137
+
138
+ void reset()
139
+ {
140
+ std::lock_guard<std::mutex> lock(mutex_);
141
+ for (auto &timer : timers_) {
142
+ if (timer) timer->active = false;
143
+ }
144
+ timers_.clear();
145
+ }
146
+
147
+ private:
148
+ double currentTimeMs() const
149
+ {
150
+ if (std::isfinite(lastTimestampMs_)) return lastTimestampMs_;
151
+ return static_cast<double>(nowUs()) / 1000.0;
152
+ }
153
+
154
+ void compactInactiveTimers()
155
+ {
156
+ std::lock_guard<std::mutex> lock(mutex_);
157
+ timers_.erase(
158
+ std::remove_if(timers_.begin(), timers_.end(), [](const auto &timer) { return !timer || (!timer->active && !timer->running); }),
159
+ timers_.end());
160
+ }
161
+
162
+ std::mutex mutex_;
163
+ std::vector<std::shared_ptr<ScheduledTimer>> timers_;
164
+ double lastTimestampMs_ = std::numeric_limits<double>::quiet_NaN();
165
+ };
166
+
167
+ class AnimationFrameQueue {
168
+ public:
169
+ static AnimationFrameQueue &instance()
170
+ {
171
+ static AnimationFrameQueue queue;
172
+ return queue;
173
+ }
174
+
175
+ double request(AnimationFrameCallback callback)
176
+ {
177
+ #if GEA_EMBEDDED_RAF_PERF
178
+ perfStats_.requestCount++;
179
+ #endif
180
+ if (!callback) {
181
+ #if GEA_EMBEDDED_RAF_PERF
182
+ perfStats_.droppedCount++;
183
+ #endif
184
+ return 0;
185
+ }
186
+ const double id = nextTimerId();
187
+ pending_.push_back(std::move(callback));
188
+ if (debugLoggingEnabled())
189
+ logDebug("request id=" + std::to_string(static_cast<long long>(id)) +
190
+ " pending=" + std::to_string(pending_.size()));
191
+ return id;
192
+ }
193
+
194
+ void run(double timestampMs)
195
+ {
196
+ [[maybe_unused]] const std::int64_t runStartUs = rafPerfNowUs();
197
+ const int count = static_cast<int>(pending_.size());
198
+ #if GEA_EMBEDDED_RAF_PERF
199
+ perfStats_.pendingAtRun += count;
200
+ #endif
201
+ [[maybe_unused]] const std::int64_t prepareStartUs = rafPerfNowUs();
202
+ std::vector<AnimationFrameCallback> active;
203
+ active.swap(pending_);
204
+ if (debugLoggingEnabled())
205
+ logDebug("run count=" + std::to_string(count) + " ts=" + std::to_string(timestampMs));
206
+ #if GEA_EMBEDDED_RAF_PERF
207
+ perfStats_.prepareUs += rafPerfNowUs() - prepareStartUs;
208
+ #endif
209
+
210
+ for (int i = 0; i < count; i++) {
211
+ if (active[i]) {
212
+ [[maybe_unused]] const std::int64_t callbackStartUs = rafPerfNowUs();
213
+ active[i](timestampMs);
214
+ #if GEA_EMBEDDED_RAF_PERF
215
+ const std::int64_t callbackUs = rafPerfNowUs() - callbackStartUs;
216
+ perfStats_.callbackUs += callbackUs;
217
+ if (callbackUs > perfStats_.maxCallbackUs) perfStats_.maxCallbackUs = callbackUs;
218
+ perfStats_.callbackCount++;
219
+ #endif
220
+ }
221
+ }
222
+ if (debugLoggingEnabled())
223
+ logDebug("run done nextPending=" + std::to_string(pending_.size()));
224
+ #if GEA_EMBEDDED_RAF_PERF
225
+ perfStats_.totalUs += rafPerfNowUs() - runStartUs;
226
+ #endif
227
+ }
228
+
229
+ void reset()
230
+ {
231
+ pending_.clear();
232
+ }
233
+
234
+ void resetPerfStats()
235
+ {
236
+ perfStats_ = {};
237
+ }
238
+
239
+ AnimationFramePerfStats perfStats() const
240
+ {
241
+ return perfStats_;
242
+ }
243
+
244
+ private:
245
+ // Cached once: building the logDebug string argument (std::to_string(double) +
246
+ // concat) ran EVERY frame at 3 call sites even though GEA_RAF_DEBUG_LOG is never
247
+ // set on-device — ~348µs/frame of pure waste (the whole "prepare" phase). Gate the
248
+ // call sites on this so the strings are only built when logging is actually on.
249
+ static bool debugLoggingEnabled()
250
+ {
251
+ static const bool enabled = [] {
252
+ const char *p = std::getenv("GEA_RAF_DEBUG_LOG");
253
+ return p && p[0] != '\0';
254
+ }();
255
+ return enabled;
256
+ }
257
+
258
+ void logDebug(const std::string &message)
259
+ {
260
+ const char *path = std::getenv("GEA_RAF_DEBUG_LOG");
261
+ if (!path || path[0] == '\0') return;
262
+ if (++debugLogCount_ > 400) return;
263
+ if (FILE *file = std::fopen(path, "a")) {
264
+ std::fprintf(file, "[raf] %s\n", message.c_str());
265
+ std::fclose(file);
266
+ }
267
+ }
268
+
269
+ std::vector<AnimationFrameCallback> pending_{};
270
+ AnimationFramePerfStats perfStats_{};
271
+ int debugLogCount_ = 0;
272
+ };
273
+
274
+ } // namespace
275
+
276
+ double setTimeout(TimerCallback callback, double delayMs)
277
+ {
278
+ return TimerScheduler::instance().schedule(std::move(callback), delayMs, false);
279
+ }
280
+
281
+ double setInterval(TimerCallback callback, double delayMs)
282
+ {
283
+ return TimerScheduler::instance().schedule(std::move(callback), delayMs, true);
284
+ }
285
+
286
+ void clearTimeout(double id)
287
+ {
288
+ TimerScheduler::instance().clear(id);
289
+ }
290
+
291
+ void clearInterval(double id)
292
+ {
293
+ TimerScheduler::instance().clear(id);
294
+ }
295
+
296
+ void resetScheduledTimers()
297
+ {
298
+ TimerScheduler::instance().reset();
299
+ }
300
+
301
+ double requestAnimationFrame(AnimationFrameCallback callback)
302
+ {
303
+ return AnimationFrameQueue::instance().request(std::move(callback));
304
+ }
305
+
306
+ void runAnimationFrameCallbacks(AnimationFrameTimestamp timestampMs)
307
+ {
308
+ TimerScheduler::instance().run(static_cast<double>(timestampMs));
309
+ AnimationFrameQueue::instance().run(timestampMs);
310
+ }
311
+
312
+ void resetAnimationFrameCallbacks()
313
+ {
314
+ AnimationFrameQueue::instance().reset();
315
+ }
316
+
317
+ void animationFramePerfStatsReset()
318
+ {
319
+ AnimationFrameQueue::instance().resetPerfStats();
320
+ }
321
+
322
+ AnimationFramePerfStats animationFramePerfStatsRead()
323
+ {
324
+ return AnimationFrameQueue::instance().perfStats();
325
+ }
326
+
327
+ } // namespace gea::host
package/host/touch.cpp ADDED
@@ -0,0 +1,25 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Firmware-side touch host binding. The framework owns the touch-facing shape;
3
+ // the generated ABI entry point below only adapts current compiler output.
4
+
5
+ #include "gea/embedded-host.h"
6
+ #include "touch.h"
7
+
8
+ namespace gea::framework::host {
9
+
10
+ class TouchSurface {
11
+ public:
12
+ static gea::host::TouchSample read()
13
+ {
14
+ int x = 0;
15
+ int y = 0;
16
+ const int touching = platform::touch::Touchscreen::readCached(&x, &y);
17
+ return {touching != 0, static_cast<double>(x), static_cast<double>(y)};
18
+ }
19
+ };
20
+
21
+ } // namespace gea::framework::host
22
+
23
+ gea::host::TouchSample gea::host::Touch::read() const {
24
+ return gea::framework::host::TouchSurface::read();
25
+ }
@@ -0,0 +1,279 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "host/websocket.h"
3
+
4
+ #include <atomic>
5
+ #include <functional>
6
+ #include <mutex>
7
+ #include <unordered_map>
8
+ #include <utility>
9
+ #include <vector>
10
+
11
+ namespace gea::host {
12
+
13
+ namespace {
14
+
15
+ struct WebSocketInstance {
16
+ std::string url;
17
+ int readyState = WebSocket::CONNECTING;
18
+ };
19
+
20
+ std::unordered_map<NativeWebSocketHandle, WebSocketInstance> &instances() {
21
+ static std::unordered_map<NativeWebSocketHandle, WebSocketInstance> map;
22
+ return map;
23
+ }
24
+
25
+ std::mutex &instancesMutex() {
26
+ static std::mutex m;
27
+ return m;
28
+ }
29
+
30
+ std::atomic<NativeWebSocketHandle> &nextHandle() {
31
+ static std::atomic<NativeWebSocketHandle> id{1};
32
+ return id;
33
+ }
34
+
35
+ enum class EventKind { Open, Message, Close, Error };
36
+
37
+ struct PendingEvent {
38
+ NativeWebSocketHandle handle = 0;
39
+ EventKind kind = EventKind::Open;
40
+ std::string text;
41
+ int code = 0;
42
+ };
43
+
44
+ std::mutex &queueMutex() {
45
+ static std::mutex m;
46
+ return m;
47
+ }
48
+
49
+ std::vector<PendingEvent> &eventQueue() {
50
+ static std::vector<PendingEvent> q;
51
+ return q;
52
+ }
53
+
54
+ void enqueue(PendingEvent ev) {
55
+ std::lock_guard<std::mutex> lock(queueMutex());
56
+ eventQueue().push_back(std::move(ev));
57
+ }
58
+
59
+ } // namespace
60
+
61
+ namespace websocket {
62
+
63
+ std::unordered_map<NativeWebSocketHandle, CallbackTable> &callbackTable() {
64
+ static std::unordered_map<NativeWebSocketHandle, CallbackTable> table;
65
+ return table;
66
+ }
67
+
68
+ #if defined(ESP_PLATFORM) && !defined(GEA_EMBEDDED_WIFI_DISABLED)
69
+ void platform_open(NativeWebSocketHandle, const std::string &);
70
+ void platform_send(NativeWebSocketHandle, const std::string &);
71
+ void platform_close(NativeWebSocketHandle);
72
+ void platform_destroy(NativeWebSocketHandle);
73
+ #else
74
+ __attribute__((weak)) void platform_open(NativeWebSocketHandle, const std::string &) {}
75
+ __attribute__((weak)) void platform_send(NativeWebSocketHandle, const std::string &) {}
76
+ __attribute__((weak)) void platform_close(NativeWebSocketHandle) {}
77
+ __attribute__((weak)) void platform_destroy(NativeWebSocketHandle) {}
78
+ #endif
79
+
80
+ NativeWebSocketHandle create_handle(const std::string &url) {
81
+ auto handle = nextHandle()++;
82
+ {
83
+ std::lock_guard<std::mutex> lock(instancesMutex());
84
+ instances()[handle] = WebSocketInstance{url, WebSocket::CONNECTING};
85
+ }
86
+ platform_open(handle, url);
87
+ return handle;
88
+ }
89
+
90
+ void destroy_handle(NativeWebSocketHandle handle) {
91
+ platform_destroy(handle);
92
+ std::lock_guard<std::mutex> lock(instancesMutex());
93
+ instances().erase(handle);
94
+ callbackTable().erase(handle);
95
+ }
96
+
97
+ void runCallbacks() {
98
+ std::vector<PendingEvent> drained;
99
+ {
100
+ std::lock_guard<std::mutex> lock(queueMutex());
101
+ drained.swap(eventQueue());
102
+ }
103
+ for (const auto &ev : drained) {
104
+ auto it = callbackTable().find(ev.handle);
105
+ if (it == callbackTable().end()) continue;
106
+ switch (ev.kind) {
107
+ case EventKind::Open:
108
+ if (it->second.on_open) it->second.on_open();
109
+ break;
110
+ case EventKind::Message:
111
+ if (it->second.on_message) it->second.on_message(ev.text);
112
+ break;
113
+ case EventKind::Close:
114
+ if (it->second.on_close) it->second.on_close(ev.code, ev.text);
115
+ break;
116
+ case EventKind::Error:
117
+ if (it->second.on_error) it->second.on_error(ev.text);
118
+ break;
119
+ }
120
+ }
121
+ }
122
+
123
+ void test_inject_open(NativeWebSocketHandle handle) {
124
+ enqueue(PendingEvent{handle, EventKind::Open, {}, 0});
125
+ }
126
+ void test_inject_message(NativeWebSocketHandle handle, const std::string &data) {
127
+ enqueue(PendingEvent{handle, EventKind::Message, data, 0});
128
+ }
129
+ void test_inject_close(NativeWebSocketHandle handle, int code, const std::string &reason) {
130
+ enqueue(PendingEvent{handle, EventKind::Close, reason, code});
131
+ }
132
+
133
+ void test_set_open_callback(NativeWebSocketHandle handle, std::function<void()> cb) {
134
+ callbackTable()[handle].on_open = std::move(cb);
135
+ }
136
+ void test_set_message_callback(NativeWebSocketHandle handle, std::function<void(const std::string &)> cb) {
137
+ callbackTable()[handle].on_message = std::move(cb);
138
+ }
139
+ void test_set_close_callback(NativeWebSocketHandle handle, std::function<void(int, const std::string &)> cb) {
140
+ callbackTable()[handle].on_close = std::move(cb);
141
+ }
142
+ void test_set_error_callback(NativeWebSocketHandle handle, std::function<void(const std::string &)> cb) {
143
+ callbackTable()[handle].on_error = std::move(cb);
144
+ }
145
+
146
+ } // namespace websocket
147
+
148
+ std::string WebSocket::url() const {
149
+ std::lock_guard<std::mutex> lock(instancesMutex());
150
+ auto it = instances().find(nativeHandle);
151
+ return it == instances().end() ? std::string{} : it->second.url;
152
+ }
153
+
154
+ double WebSocket::readyState() const {
155
+ std::lock_guard<std::mutex> lock(instancesMutex());
156
+ auto it = instances().find(nativeHandle);
157
+ return it == instances().end() ? CLOSED : static_cast<double>(it->second.readyState);
158
+ }
159
+
160
+ void WebSocket::send(const std::string &data) const {
161
+ websocket::platform_send(nativeHandle, data);
162
+ }
163
+
164
+ void WebSocket::close() const {
165
+ websocket::platform_close(nativeHandle);
166
+ }
167
+
168
+ void WebSocket::close(double) const {
169
+ websocket::platform_close(nativeHandle);
170
+ }
171
+
172
+ void WebSocket::close(double, const std::string &) const {
173
+ websocket::platform_close(nativeHandle);
174
+ }
175
+
176
+ } // namespace gea::host
177
+
178
+ #if defined(ESP_PLATFORM) && !defined(GEA_EMBEDDED_WIFI_DISABLED)
179
+
180
+ #include "esp_event.h"
181
+ #include "esp_log.h"
182
+ #include "esp_websocket_client.h"
183
+
184
+ namespace gea::host::websocket {
185
+
186
+ namespace {
187
+
188
+ struct EspWsState {
189
+ esp_websocket_client_handle_t client = nullptr;
190
+ };
191
+
192
+ std::unordered_map<NativeWebSocketHandle, EspWsState> &espStateTable() {
193
+ static std::unordered_map<NativeWebSocketHandle, EspWsState> table;
194
+ return table;
195
+ }
196
+
197
+ void handleWsEvent(void *arg, esp_event_base_t, int32_t event_id, void *data) {
198
+ const auto handle = static_cast<NativeWebSocketHandle>(reinterpret_cast<std::uintptr_t>(arg));
199
+ switch (event_id) {
200
+ case WEBSOCKET_EVENT_CONNECTED: {
201
+ {
202
+ std::lock_guard<std::mutex> lock(gea::host::instancesMutex());
203
+ if (auto it = gea::host::instances().find(handle); it != gea::host::instances().end()) {
204
+ it->second.readyState = WebSocket::OPEN;
205
+ }
206
+ }
207
+ gea::host::enqueue(gea::host::PendingEvent{handle, gea::host::EventKind::Open, {}, 0});
208
+ break;
209
+ }
210
+ case WEBSOCKET_EVENT_DATA: {
211
+ const auto *event = static_cast<esp_websocket_event_data_t *>(data);
212
+ if (event && event->op_code == 0x01 /* text frame */ && event->data_len > 0) {
213
+ gea::host::enqueue(gea::host::PendingEvent{
214
+ handle, gea::host::EventKind::Message,
215
+ std::string(event->data_ptr, event->data_len), 0});
216
+ }
217
+ break;
218
+ }
219
+ case WEBSOCKET_EVENT_DISCONNECTED: {
220
+ {
221
+ std::lock_guard<std::mutex> lock(gea::host::instancesMutex());
222
+ if (auto it = gea::host::instances().find(handle); it != gea::host::instances().end()) {
223
+ it->second.readyState = WebSocket::CLOSED;
224
+ }
225
+ }
226
+ gea::host::enqueue(gea::host::PendingEvent{handle, gea::host::EventKind::Close, std::string("closed"), 1000});
227
+ break;
228
+ }
229
+ case WEBSOCKET_EVENT_ERROR: {
230
+ gea::host::enqueue(gea::host::PendingEvent{handle, gea::host::EventKind::Error, std::string("error"), 0});
231
+ break;
232
+ }
233
+ }
234
+ }
235
+
236
+ } // namespace
237
+
238
+ void platform_open(NativeWebSocketHandle handle, const std::string &url) {
239
+ esp_websocket_client_config_t config = {};
240
+ config.uri = url.c_str();
241
+
242
+ auto client = esp_websocket_client_init(&config);
243
+ if (!client) {
244
+ ESP_LOGE("gea::host::ws", "init failed for %s", url.c_str());
245
+ return;
246
+ }
247
+ esp_websocket_register_events(
248
+ client, WEBSOCKET_EVENT_ANY, handleWsEvent,
249
+ reinterpret_cast<void *>(static_cast<std::uintptr_t>(handle)));
250
+ esp_websocket_client_start(client);
251
+ espStateTable()[handle] = EspWsState{client};
252
+ }
253
+
254
+ void platform_send(NativeWebSocketHandle handle, const std::string &data) {
255
+ auto it = espStateTable().find(handle);
256
+ if (it == espStateTable().end() || it->second.client == nullptr) return;
257
+ esp_websocket_client_send_text(it->second.client, data.c_str(),
258
+ static_cast<int>(data.size()), portMAX_DELAY);
259
+ }
260
+
261
+ void platform_close(NativeWebSocketHandle handle) {
262
+ auto it = espStateTable().find(handle);
263
+ if (it == espStateTable().end() || it->second.client == nullptr) return;
264
+ esp_websocket_client_close(it->second.client, portMAX_DELAY);
265
+ }
266
+
267
+ void platform_destroy(NativeWebSocketHandle handle) {
268
+ auto it = espStateTable().find(handle);
269
+ if (it == espStateTable().end()) return;
270
+ if (it->second.client) {
271
+ esp_websocket_client_stop(it->second.client);
272
+ esp_websocket_client_destroy(it->second.client);
273
+ }
274
+ espStateTable().erase(it);
275
+ }
276
+
277
+ } // namespace gea::host::websocket
278
+
279
+ #endif // ESP_PLATFORM && network capability