@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,31 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "gea/embedded-host.h"
3
+
4
+ namespace gea::framework::geolocation {
5
+
6
+ bool GeolocationBackend::hasFix()
7
+ {
8
+ return geolocation().hasFix();
9
+ }
10
+
11
+ GeolocationPosition GeolocationBackend::currentPosition()
12
+ {
13
+ return geolocation().currentPosition();
14
+ }
15
+
16
+ double GeolocationBackend::latitude()
17
+ {
18
+ return currentPosition().coords.latitude;
19
+ }
20
+
21
+ double GeolocationBackend::longitude()
22
+ {
23
+ return currentPosition().coords.longitude;
24
+ }
25
+
26
+ double GeolocationBackend::accuracy()
27
+ {
28
+ return currentPosition().coords.accuracy;
29
+ }
30
+
31
+ } // namespace gea::framework::geolocation
package/host/gpio.cpp ADDED
@@ -0,0 +1,55 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "gpio.h"
3
+
4
+ #include "gea/embedded-host.h"
5
+
6
+ namespace gea::framework::gpio {
7
+
8
+ namespace {
9
+
10
+ // The TS surface hands every number over as a double. Truncating here — once,
11
+ // at the boundary — keeps the platform contract in ints and means a fractional
12
+ // pin number lands on a pin instead of on undefined behaviour.
13
+ int toPin(double value) { return static_cast<int>(value); }
14
+
15
+ } // namespace
16
+
17
+ bool GpioBackend::configureOutput(double pin)
18
+ {
19
+ return gea::platform::gpio::Gpio::configureOutput(toPin(pin));
20
+ }
21
+
22
+ bool GpioBackend::configureInput(double pin, bool pull_up)
23
+ {
24
+ return gea::platform::gpio::Gpio::configureInput(toPin(pin), pull_up);
25
+ }
26
+
27
+ bool GpioBackend::write(double pin, bool level)
28
+ {
29
+ return gea::platform::gpio::Gpio::write(toPin(pin), level);
30
+ }
31
+
32
+ bool GpioBackend::read(double pin) { return gea::platform::gpio::Gpio::read(toPin(pin)); }
33
+
34
+ bool AddressableLedBackend::set(double pin, double r, double g, double b)
35
+ {
36
+ return gea::platform::gpio::AddressableLed::set(toPin(pin), toPin(r), toPin(g), toPin(b));
37
+ }
38
+
39
+ bool AddressableLedBackend::off(double pin) { return gea::platform::gpio::AddressableLed::off(toPin(pin)); }
40
+
41
+ bool AddressableLedBackend::attach(double pin, double count)
42
+ {
43
+ return gea::platform::gpio::AddressableLed::attach(toPin(pin), toPin(count));
44
+ }
45
+
46
+ bool AddressableLedBackend::setPixel(double index, double r, double g, double b)
47
+ {
48
+ return gea::platform::gpio::AddressableLed::setPixel(toPin(index), toPin(r), toPin(g), toPin(b));
49
+ }
50
+
51
+ bool AddressableLedBackend::show() { return gea::platform::gpio::AddressableLed::show(); }
52
+
53
+ void AddressableLedBackend::detach() { gea::platform::gpio::AddressableLed::detach(); }
54
+
55
+ } // namespace gea::framework::gpio
package/host/http.cpp ADDED
@@ -0,0 +1,349 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // This library TU provides only the platform/transport parts of the http
3
+ // facade (register_handler, runRequests, platform_listen). The gea_cpp_value
4
+ // reply-parsing `create_server` is header-inline and compiled in the geatsc app
5
+ // TU, where the full runtime value type exists. The native test harness defines
6
+ // GEA_CPP_VALUE_AVAILABLE globally, so disable it here (matching host/fetch.cpp)
7
+ // to keep that value-dependent block out of this TU, which lacks the type.
8
+ #ifdef GEA_CPP_VALUE_AVAILABLE
9
+ #define GEA_HTTP_RESTORE_CPP_VALUE_AVAILABLE 1
10
+ #undef GEA_CPP_VALUE_AVAILABLE
11
+ #endif
12
+ #include "host/http.h"
13
+ #ifdef GEA_HTTP_RESTORE_CPP_VALUE_AVAILABLE
14
+ #define GEA_CPP_VALUE_AVAILABLE 1
15
+ #undef GEA_HTTP_RESTORE_CPP_VALUE_AVAILABLE
16
+ #endif
17
+
18
+ #include <atomic>
19
+ #include <memory>
20
+ #include <mutex>
21
+ #include <utility>
22
+ #include <vector>
23
+
24
+ namespace gea::host::http {
25
+
26
+ namespace {
27
+
28
+ // A request handed from a server worker task to the frame task and back. The
29
+ // worker fills `req`, posts it, and waits on `done`; the frame task runs the
30
+ // handler, fills `reply`, and sets `done`.
31
+ struct PendingRequest {
32
+ NativeHttpServerHandle server = 0;
33
+ HttpRequest req;
34
+ HttpReply reply;
35
+ std::atomic<bool> done{false};
36
+ };
37
+
38
+ std::mutex &queueMutex() {
39
+ static std::mutex m;
40
+ return m;
41
+ }
42
+
43
+ std::vector<std::shared_ptr<PendingRequest>> &requestQueue() {
44
+ static std::vector<std::shared_ptr<PendingRequest>> q;
45
+ return q;
46
+ }
47
+
48
+ std::mutex &handlerMutex() {
49
+ static std::mutex m;
50
+ return m;
51
+ }
52
+
53
+ NativeHttpServerHandle nextHandle() {
54
+ static std::atomic<NativeHttpServerHandle> id{1};
55
+ return id++;
56
+ }
57
+
58
+ // Post a request and block until the frame task has produced a reply (or the
59
+ // deadline passes). Returns the filled reply. Called on the server worker task.
60
+ HttpReply dispatchAndWait(NativeHttpServerHandle server, HttpRequest req, int timeoutMs,
61
+ int pollMs, const std::function<void(int)> &sleep) {
62
+ auto pending = std::make_shared<PendingRequest>();
63
+ pending->server = server;
64
+ pending->req = std::move(req);
65
+ {
66
+ std::lock_guard<std::mutex> lock(queueMutex());
67
+ requestQueue().push_back(pending);
68
+ }
69
+ int waited = 0;
70
+ while (!pending->done.load(std::memory_order_acquire)) {
71
+ if (waited >= timeoutMs) {
72
+ HttpReply timeout;
73
+ timeout.status = 504;
74
+ timeout.contentType = "text/plain";
75
+ timeout.body = "Gateway Timeout";
76
+ // The frame task may still resolve `pending` later; it just won't be
77
+ // read. The shared_ptr keeps it alive until both sides drop it.
78
+ return timeout;
79
+ }
80
+ sleep(pollMs);
81
+ waited += pollMs;
82
+ }
83
+ return pending->reply;
84
+ }
85
+
86
+ } // namespace
87
+
88
+ std::unordered_map<NativeHttpServerHandle, RequestHandler> &handlerTable() {
89
+ static std::unordered_map<NativeHttpServerHandle, RequestHandler> table;
90
+ return table;
91
+ }
92
+
93
+ NativeHttpServerHandle register_handler(RequestHandler handler) {
94
+ const NativeHttpServerHandle handle = nextHandle();
95
+ std::lock_guard<std::mutex> lock(handlerMutex());
96
+ handlerTable()[handle] = std::move(handler);
97
+ return handle;
98
+ }
99
+
100
+ void unregister_handler(NativeHttpServerHandle handle) {
101
+ std::lock_guard<std::mutex> lock(handlerMutex());
102
+ handlerTable().erase(handle);
103
+ }
104
+
105
+ void runRequests() {
106
+ std::vector<std::shared_ptr<PendingRequest>> drained;
107
+ {
108
+ std::lock_guard<std::mutex> lock(queueMutex());
109
+ if (requestQueue().empty()) return;
110
+ drained.swap(requestQueue());
111
+ }
112
+ for (const auto &pending : drained) {
113
+ RequestHandler handler;
114
+ {
115
+ std::lock_guard<std::mutex> lock(handlerMutex());
116
+ auto it = handlerTable().find(pending->server);
117
+ if (it != handlerTable().end()) handler = it->second;
118
+ }
119
+ if (handler) {
120
+ pending->reply = handler(pending->req);
121
+ } else {
122
+ pending->reply.status = 404;
123
+ pending->reply.contentType = "text/plain";
124
+ pending->reply.body = "Not found";
125
+ }
126
+ pending->done.store(true, std::memory_order_release);
127
+ }
128
+ }
129
+
130
+ HttpReply test_dispatch(NativeHttpServerHandle handle, const std::string &method,
131
+ const std::string &path, const std::string &query) {
132
+ RequestHandler handler;
133
+ {
134
+ std::lock_guard<std::mutex> lock(handlerMutex());
135
+ auto it = handlerTable().find(handle);
136
+ if (it != handlerTable().end()) handler = it->second;
137
+ }
138
+ HttpReply reply;
139
+ if (!handler) {
140
+ reply.status = 404;
141
+ reply.body = "Not found";
142
+ return reply;
143
+ }
144
+ HttpRequest req;
145
+ req.method = method;
146
+ req.path = path;
147
+ req.query = query;
148
+ return handler(req);
149
+ }
150
+
151
+ } // namespace gea::host::http
152
+
153
+ #if defined(ESP_PLATFORM) && !defined(GEA_EMBEDDED_WIFI_DISABLED)
154
+
155
+ #include "esp_http_server.h"
156
+ #include "esp_heap_caps.h"
157
+ #include "esp_log.h"
158
+
159
+ #include "freertos/FreeRTOS.h"
160
+ #include "freertos/task.h"
161
+
162
+ #include <cstdio>
163
+ #include <cstring>
164
+ #include <unordered_map>
165
+
166
+ namespace gea::host::http {
167
+
168
+ namespace {
169
+
170
+ constexpr const char *kTag = "gea::host::http";
171
+ // The frame task drains requests every frame; e-paper frames can be ~1s, so
172
+ // allow generous slack before declaring a gateway timeout.
173
+ constexpr int kRendezvousTimeoutMs = 8000;
174
+ constexpr int kRendezvousPollMs = 10;
175
+ constexpr std::size_t kStreamChunkBytes = 4096;
176
+
177
+ std::unordered_map<NativeHttpServerHandle, httpd_handle_t> &espServers() {
178
+ static std::unordered_map<NativeHttpServerHandle, httpd_handle_t> map;
179
+ return map;
180
+ }
181
+
182
+ std::string methodName(int method) {
183
+ switch (method) {
184
+ case HTTP_POST: return "POST";
185
+ case HTTP_PUT: return "PUT";
186
+ case HTTP_DELETE: return "DELETE";
187
+ case HTTP_HEAD: return "HEAD";
188
+ case HTTP_PATCH: return "PATCH";
189
+ default: return "GET";
190
+ }
191
+ }
192
+
193
+ const char *statusLine(int status) {
194
+ switch (status) {
195
+ case 200: return "200 OK";
196
+ case 204: return "204 No Content";
197
+ case 303: return "303 See Other";
198
+ case 400: return "400 Bad Request";
199
+ case 404: return "404 Not Found";
200
+ case 500: return "500 Internal Server Error";
201
+ case 504: return "504 Gateway Timeout";
202
+ default: return "200 OK";
203
+ }
204
+ }
205
+
206
+ // Stream a file from disk to the client in chunks, so multi-MB recordings never
207
+ // have to fit in RAM.
208
+ esp_err_t sendFile(httpd_req_t *r, const HttpReply &reply) {
209
+ FILE *f = std::fopen(reply.file.c_str(), "rb");
210
+ if (!f) {
211
+ httpd_resp_set_status(r, "404 Not Found");
212
+ httpd_resp_set_type(r, "text/plain");
213
+ httpd_resp_sendstr(r, "File not found");
214
+ return ESP_OK;
215
+ }
216
+ httpd_resp_set_status(r, statusLine(reply.status));
217
+ httpd_resp_set_type(r, reply.contentType.c_str());
218
+ if (!reply.download.empty()) {
219
+ const std::string disposition = "attachment; filename=\"" + reply.download + "\"";
220
+ httpd_resp_set_hdr(r, "Content-Disposition", disposition.c_str());
221
+ }
222
+ auto *buffer = static_cast<char *>(std::malloc(kStreamChunkBytes));
223
+ if (!buffer) {
224
+ std::fclose(f);
225
+ httpd_resp_send_chunk(r, nullptr, 0);
226
+ return ESP_FAIL;
227
+ }
228
+ esp_err_t result = ESP_OK;
229
+ for (;;) {
230
+ const std::size_t read = std::fread(buffer, 1, kStreamChunkBytes, f);
231
+ if (read > 0 && httpd_resp_send_chunk(r, buffer, read) != ESP_OK) {
232
+ result = ESP_FAIL;
233
+ break;
234
+ }
235
+ if (read < kStreamChunkBytes) break; // EOF (short read)
236
+ }
237
+ std::free(buffer);
238
+ std::fclose(f);
239
+ if (result == ESP_OK) httpd_resp_send_chunk(r, nullptr, 0); // terminate
240
+ return result;
241
+ }
242
+
243
+ esp_err_t wildcardHandler(httpd_req_t *r) {
244
+ const auto server = static_cast<NativeHttpServerHandle>(reinterpret_cast<std::uintptr_t>(r->user_ctx));
245
+
246
+ // r->uri is a fixed char array (never null); construct directly to avoid a
247
+ // -Werror=address "address will never be NULL" warning.
248
+ std::string uri = (r->uri[0] != '\0') ? std::string(r->uri) : std::string("/");
249
+ std::string path = uri;
250
+ std::string query;
251
+ const std::size_t q = uri.find('?');
252
+ if (q != std::string::npos) {
253
+ path = uri.substr(0, q);
254
+ query = uri.substr(q + 1);
255
+ }
256
+
257
+ HttpRequest req;
258
+ req.method = methodName(r->method);
259
+ req.path = path;
260
+ req.query = query;
261
+
262
+ const HttpReply reply = dispatchAndWait(
263
+ server, std::move(req), kRendezvousTimeoutMs, kRendezvousPollMs,
264
+ [](int ms) { vTaskDelay(pdMS_TO_TICKS(ms)); });
265
+
266
+ if (!reply.file.empty()) return sendFile(r, reply);
267
+
268
+ httpd_resp_set_status(r, statusLine(reply.status));
269
+ httpd_resp_set_type(r, reply.contentType.c_str());
270
+ if (!reply.download.empty()) {
271
+ const std::string disposition = "attachment; filename=\"" + reply.download + "\"";
272
+ httpd_resp_set_hdr(r, "Content-Disposition", disposition.c_str());
273
+ }
274
+ httpd_resp_send(r, reply.body.data(), reply.body.size());
275
+ return ESP_OK;
276
+ }
277
+
278
+ } // namespace
279
+
280
+ bool platform_listen(NativeHttpServerHandle handle, int port) {
281
+ if (espServers().count(handle)) return true; // already listening
282
+
283
+ httpd_config_t config = HTTPD_DEFAULT_CONFIG();
284
+ config.server_port = static_cast<std::uint16_t>(port);
285
+ // Every esp_http_server instance defaults to the SAME internal control-socket
286
+ // port (32768); a second instance (the diagnostics server already owns the
287
+ // default) then fails httpd_start with ESP_FAIL. Derive a unique ctrl port
288
+ // from the listen port so the portal coexists with diagnostics/OTA servers.
289
+ config.ctrl_port = static_cast<std::uint16_t>(32768 + (port % 1000) + 1);
290
+ config.stack_size = 4096; // esp_http_server default; 8192 risked NO_MEM post-WiFi
291
+ config.max_uri_handlers = 2;
292
+ // A personal portal serves a page + an audio stream; a few sockets is plenty.
293
+ // The default (7) competes with the diagnostics/OTA servers for the limited
294
+ // LWIP socket pool, so keep this small.
295
+ config.max_open_sockets = 4;
296
+ config.lru_purge_enable = true;
297
+ config.uri_match_fn = httpd_uri_match_wildcard;
298
+ // Worker tasks block on the frame-task rendezvous; the recv/send timeouts must
299
+ // exceed it so a slow frame doesn't drop the socket mid-request.
300
+ config.recv_wait_timeout = 15;
301
+ config.send_wait_timeout = 15;
302
+
303
+ ESP_LOGI(kTag, "httpd_start :%d heap int8[free=%u largest=%u] dma[free=%u largest=%u]", port,
304
+ static_cast<unsigned>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)),
305
+ static_cast<unsigned>(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)),
306
+ static_cast<unsigned>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA)),
307
+ static_cast<unsigned>(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA)));
308
+ httpd_handle_t server = nullptr;
309
+ const esp_err_t startErr = httpd_start(&server, &config);
310
+ if (startErr != ESP_OK) {
311
+ ESP_LOGE(kTag, "httpd_start failed on port %d: %s", port, esp_err_to_name(startErr));
312
+ return false;
313
+ }
314
+
315
+ httpd_uri_t wildcard = {};
316
+ wildcard.uri = "/*";
317
+ wildcard.method = HTTP_GET;
318
+ wildcard.handler = wildcardHandler;
319
+ wildcard.user_ctx = reinterpret_cast<void *>(static_cast<std::uintptr_t>(handle));
320
+ httpd_register_uri_handler(server, &wildcard);
321
+
322
+ espServers()[handle] = server;
323
+ ESP_LOGI(kTag, "listening on :%d (handle %u)", port, static_cast<unsigned>(handle));
324
+ return true;
325
+ }
326
+
327
+ void platform_close(NativeHttpServerHandle handle) {
328
+ auto it = espServers().find(handle);
329
+ if (it == espServers().end()) return;
330
+ if (it->second) httpd_stop(it->second);
331
+ espServers().erase(it);
332
+ ESP_LOGI(kTag, "stopped (handle %u)", static_cast<unsigned>(handle));
333
+ }
334
+
335
+ } // namespace gea::host::http
336
+
337
+ #else // no ESP network capability
338
+
339
+ namespace gea::host::http {
340
+
341
+ // Off-device (host tools, simulator, unit tests): no real socket. listen/close
342
+ // are no-ops; tests drive handlers through test_dispatch(). Weak so a platform
343
+ // can override with a real implementation.
344
+ __attribute__((weak)) bool platform_listen(NativeHttpServerHandle, int) { return false; }
345
+ __attribute__((weak)) void platform_close(NativeHttpServerHandle) {}
346
+
347
+ } // namespace gea::host::http
348
+
349
+ #endif // ESP_PLATFORM && network capability