@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
package/host/image.cpp ADDED
@@ -0,0 +1,335 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // This TU is native platform/image-store code. Boxed gea_cpp_value overloads
3
+ // are header-inline in host/image.h and compile only in generated app TUs that
4
+ // already include the JS runtime. Keep this TU runtime-free.
5
+ #ifdef GEA_CPP_VALUE_AVAILABLE
6
+ #define GEA_IMAGE_RESTORE_CPP_VALUE_AVAILABLE 1
7
+ #undef GEA_CPP_VALUE_AVAILABLE
8
+ #endif
9
+ #include "host/image.h"
10
+
11
+ #include "image.h"
12
+ #include "gea/embedded-host.h"
13
+ #include "display.h"
14
+ #include "host/fetch.h" // gea::host::fetch for fetchBytes/fetchText (WiFi tile download)
15
+ #include "platform/file_cache.h"
16
+
17
+ #ifdef GEA_IMAGE_RESTORE_CPP_VALUE_AVAILABLE
18
+ #define GEA_CPP_VALUE_AVAILABLE 1
19
+ #undef GEA_IMAGE_RESTORE_CPP_VALUE_AVAILABLE
20
+ #endif
21
+
22
+ #include <cstddef>
23
+ #include <cstdint>
24
+ #include <cstdio>
25
+ #include <cstring>
26
+ #include <dirent.h>
27
+ #include <sys/stat.h>
28
+ #include <unordered_map>
29
+ #include <vector>
30
+
31
+ #ifdef ESP_PLATFORM
32
+ #include "esp_partition.h" // readMapArchive() reads a .pmtiles flashed to ota_1
33
+ #include "esp_heap_caps.h"
34
+ #include "freertos/FreeRTOS.h"
35
+ #include "freertos/task.h"
36
+ #include "freertos/idf_additions.h" // xTaskCreateWithCaps (force internal stack)
37
+ #include "freertos/semphr.h"
38
+ #endif
39
+
40
+ namespace gea::platform::storage {
41
+ // Mount provider registered by the target at startup (null => no persistent
42
+ // storage => file cache disabled). Explicit registration (not a weak symbol)
43
+ // keeps the provider's TU linked.
44
+ namespace {
45
+ bool (*g_mount_provider)() = nullptr;
46
+ }
47
+ void setMountProvider(bool (*provider)()) { g_mount_provider = provider; }
48
+ bool ensureMounted() { return g_mount_provider ? g_mount_provider() : false; }
49
+ } // namespace gea::platform::storage
50
+
51
+ namespace gea::host {
52
+
53
+ extern "C" bool gea_embedded_asset_lookup(const char *path, const unsigned char **data, unsigned long *length) __attribute__((weak));
54
+
55
+ namespace {
56
+
57
+ gea::framework::graphics::ImageStore &store() {
58
+ return gea::framework::graphics::ImageStore::instance();
59
+ }
60
+
61
+ int integer(double value) {
62
+ return static_cast<int>(value);
63
+ }
64
+
65
+ // mkdir -p of the parent directories in `path` (FAT/VFS); existing dirs are fine.
66
+ void makeParentDirs(const std::string &path) {
67
+ for (std::size_t i = 1; i < path.size(); i++) {
68
+ if (path[i] != '/') continue;
69
+ const std::string dir = path.substr(0, i);
70
+ if (!dir.empty()) ::mkdir(dir.c_str(), 0777);
71
+ }
72
+ }
73
+
74
+ #ifdef ESP_PLATFORM
75
+ // Flash reads briefly disable the cache, so the calling task's stack must live
76
+ // in INTERNAL RAM (a PSRAM stack is inaccessible cache-off → panic). The app's
77
+ // own task runs on a PSRAM stack, so the ota_1 read runs on this short-lived
78
+ // helper task with an internal stack instead. The destination vector may be in
79
+ // PSRAM — esp_partition_read bounces external destinations through an internal
80
+ // buffer, so only the stack matters.
81
+ struct MapReadCtx {
82
+ const esp_partition_t *p;
83
+ std::vector<std::uint8_t> *out;
84
+ SemaphoreHandle_t done;
85
+ };
86
+
87
+ void mapReadTask(void *arg) {
88
+ auto *c = static_cast<MapReadCtx *>(arg);
89
+ std::uint8_t hdr[127];
90
+ if (esp_partition_read(c->p, 0, hdr, sizeof(hdr)) == ESP_OK && hdr[0] == 0x50 && hdr[1] == 0x4d) {
91
+ std::uint64_t off = 0;
92
+ std::uint64_t len = 0;
93
+ for (int i = 7; i >= 0; i--) {
94
+ off = off * 256 + hdr[56 + i]; // tile_data_offset (u64 LE @56)
95
+ len = len * 256 + hdr[64 + i]; // tile_data_length (u64 LE @64)
96
+ }
97
+ const std::uint64_t total = off + len;
98
+ if (total > 0 && total <= c->p->size) {
99
+ c->out->resize(static_cast<std::size_t>(total));
100
+ if (esp_partition_read(c->p, 0, c->out->data(), static_cast<std::size_t>(total)) != ESP_OK) c->out->clear();
101
+ }
102
+ }
103
+ xSemaphoreGive(c->done);
104
+ vTaskDeleteWithCaps(nullptr); // matches xTaskCreateWithCaps allocation
105
+ }
106
+ #endif
107
+
108
+ std::vector<std::uint8_t> readWholeFile(const std::string &path) {
109
+ std::vector<std::uint8_t> out;
110
+ std::FILE *f = std::fopen(path.c_str(), "rb");
111
+ if (!f) return out;
112
+ std::fseek(f, 0, SEEK_END);
113
+ const long size = std::ftell(f);
114
+ std::fseek(f, 0, SEEK_SET);
115
+ if (size > 0) {
116
+ out.resize(static_cast<std::size_t>(size));
117
+ const std::size_t got = std::fread(out.data(), 1, out.size(), f);
118
+ out.resize(got);
119
+ }
120
+ std::fclose(f);
121
+ return out;
122
+ }
123
+
124
+ } // namespace
125
+
126
+ double ImageService::loadBytes(std::vector<std::uint8_t> bytes) const {
127
+ const int id = store().decode(bytes.data(), static_cast<int>(bytes.size()), -1);
128
+ return static_cast<double>(id);
129
+ }
130
+
131
+ double ImageService::loadBytesOpaque(std::vector<std::uint8_t> bytes) const {
132
+ const int id = store().decodeOpaque(bytes.data(), static_cast<int>(bytes.size()), -1);
133
+ return static_cast<double>(id);
134
+ }
135
+
136
+ double ImageService::loadBytesPtr(const std::uint8_t *data, std::size_t length) const {
137
+ if (!data || length == 0) return -1.0;
138
+ // One decode per asset; an <img> that remounts reuses the same slot instead
139
+ // of leaking a fresh decode each time. The embedded symbol address is stable,
140
+ // so it's a safe cache key.
141
+ static std::unordered_map<const void *, int> cache;
142
+ const auto cached = cache.find(data);
143
+ if (cached != cache.end()) return static_cast<double>(cached->second);
144
+ // Embedded-asset bytes live in flash at a stable address: defer the
145
+ // decode to first draw so boot only pays for visible images.
146
+ const int id = store().decodeDeferred(data, static_cast<int>(length), -1);
147
+ cache.emplace(data, id);
148
+ return static_cast<double>(id);
149
+ }
150
+
151
+ double ImageService::loadAssetPath(const char *path) const {
152
+ if (!path) return -1.0;
153
+ // First: a bundled/flash asset (the common <img src="/logo.png"> case).
154
+ if (gea_embedded_asset_lookup) {
155
+ const unsigned char *data = nullptr;
156
+ unsigned long length = 0;
157
+ if (gea_embedded_asset_lookup(path, &data, &length)) return loadBytesPtr(data, static_cast<std::size_t>(length));
158
+ }
159
+ // Fallback: an absolute filesystem path (e.g. an EPUB cover extracted to the
160
+ // SD card). Lets `<img src="/sdcard/...">` show runtime images through the
161
+ // framework's own decode/render path instead of only bundled assets.
162
+ if (path[0] == '/') return loadFile(std::string(path));
163
+ return -1.0;
164
+ }
165
+
166
+ double ImageService::loadFile(const std::string &path) const {
167
+ if (path.empty() || !gea::platform::storage::ensureMounted()) return -1.0;
168
+ const std::vector<std::uint8_t> bytes = readWholeFile(path);
169
+ if (bytes.empty()) return -1.0;
170
+ return static_cast<double>(store().decode(bytes.data(), static_cast<int>(bytes.size()), -1));
171
+ }
172
+
173
+ double ImageService::loadFileOpaque(const std::string &path) const {
174
+ if (path.empty() || !gea::platform::storage::ensureMounted()) return -1.0;
175
+ const std::vector<std::uint8_t> bytes = readWholeFile(path);
176
+ if (bytes.empty()) return -1.0;
177
+ return static_cast<double>(store().decodeOpaque(bytes.data(), static_cast<int>(bytes.size()), -1));
178
+ }
179
+
180
+ bool ImageService::writeFile(const std::string &path, const std::vector<std::uint8_t> &bytes) const {
181
+ if (path.empty() || bytes.empty() || !gea::platform::storage::ensureMounted()) return false;
182
+ makeParentDirs(path);
183
+ std::FILE *f = std::fopen(path.c_str(), "wb");
184
+ if (!f) return false;
185
+ const std::size_t wrote = std::fwrite(bytes.data(), 1, bytes.size(), f);
186
+ std::fclose(f);
187
+ return wrote == bytes.size();
188
+ }
189
+
190
+ std::vector<std::uint8_t> ImageService::readFile(const std::string &path) const {
191
+ if (path.empty() || !gea::platform::storage::ensureMounted()) return {};
192
+ return readWholeFile(path);
193
+ }
194
+
195
+ std::vector<std::uint8_t> ImageService::fetchBytes(const std::string &url) const {
196
+ // Synchronous HTTP GET over WiFi (blocks the calling task until the body is
197
+ // in), returning the raw bytes — e.g. one MVT tile. Empty when WiFi is down
198
+ // or the request fails.
199
+ return gea::host::fetch(url).body;
200
+ }
201
+
202
+ std::string ImageService::fetchText(const std::string &url) const {
203
+ return gea::host::fetch(url).text();
204
+ }
205
+
206
+ std::vector<std::uint8_t> ImageService::readFileRange(const std::string &path, double offset, double length) const {
207
+ // Read just [offset, offset+length) — PMTiles' random-access pattern: the
208
+ // directory + each tile are small ranges, so the whole archive never loads
209
+ // into RAM. SD reads (fopen/fread) don't disable the flash cache, so this is
210
+ // safe straight from the app task.
211
+ if (path.empty() || length <= 0 || !gea::platform::storage::ensureMounted()) return {};
212
+ std::FILE *f = std::fopen(path.c_str(), "rb");
213
+ if (!f) return {};
214
+ std::vector<std::uint8_t> out(static_cast<std::size_t>(length));
215
+ std::fseek(f, static_cast<long>(offset), SEEK_SET);
216
+ const std::size_t got = std::fread(out.data(), 1, out.size(), f);
217
+ std::fclose(f);
218
+ out.resize(got);
219
+ return out;
220
+ }
221
+
222
+ std::string ImageService::listFiles(const std::string &path) const {
223
+ if (path.empty() || !gea::platform::storage::ensureMounted()) return {};
224
+ DIR *dir = opendir(path.c_str());
225
+ if (!dir) return {};
226
+ std::string out;
227
+ while (const dirent *entry = readdir(dir)) {
228
+ // Some VFS drivers (e.g. FATFS on ESP-IDF) don't fill d_type; stat the
229
+ // entry when the type is unknown so directories stay excluded either way.
230
+ if (entry->d_name[0] == '.') continue;
231
+ bool regular = entry->d_type == DT_REG;
232
+ if (entry->d_type == DT_UNKNOWN) {
233
+ struct stat st {};
234
+ const std::string full = path + "/" + entry->d_name;
235
+ regular = stat(full.c_str(), &st) == 0 && S_ISREG(st.st_mode);
236
+ }
237
+ if (!regular) continue;
238
+ if (!out.empty()) out += '\n';
239
+ out += entry->d_name;
240
+ }
241
+ closedir(dir);
242
+ return out;
243
+ }
244
+
245
+ std::vector<std::uint8_t> ImageService::readMapArchive() const {
246
+ // Read a .pmtiles archive flashed (via esptool) into the spare ota_1 app
247
+ // partition — a reliable USB path that doesn't depend on the SD card or the
248
+ // lossy serial console. Returns exactly the archive's bytes (its size comes
249
+ // from the PMTiles header: tile_data_offset + tile_data_length). Empty off
250
+ // esp32 or when no valid archive is present.
251
+ #ifdef ESP_PLATFORM
252
+ const esp_partition_t *p =
253
+ esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, nullptr);
254
+ if (!p) return {};
255
+ std::vector<std::uint8_t> out;
256
+ SemaphoreHandle_t done = xSemaphoreCreateBinary();
257
+ if (!done) return {};
258
+ MapReadCtx ctx{p, &out, done};
259
+ TaskHandle_t task = nullptr;
260
+ // 8 KB INTERNAL-RAM stack: a flash read disables the cache, so the reading
261
+ // task's stack must not be in PSRAM (the app's own task stack is).
262
+ if (xTaskCreateWithCaps(&mapReadTask, "map_read", 8192, &ctx, 6, &task, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) == pdPASS) {
263
+ xSemaphoreTake(done, portMAX_DELAY);
264
+ }
265
+ vSemaphoreDelete(done);
266
+ return out;
267
+ #else
268
+ return {};
269
+ #endif
270
+ }
271
+
272
+ void ImageService::draw(double id, double x, double y) const {
273
+ const int slot = integer(id);
274
+ const gea::framework::graphics::pixel::native_t *pixels = store().currentPixels(slot);
275
+ if (!pixels) return;
276
+
277
+ const unsigned char *alpha = store().currentAlpha(slot);
278
+ const int width = store().width(slot);
279
+ const int height = store().height(slot);
280
+ gea::platform::display::Display::blitImage(pixels, alpha, width, height, integer(x), integer(y));
281
+ }
282
+
283
+ double ImageService::width(double id) const {
284
+ return static_cast<double>(store().width(integer(id)));
285
+ }
286
+
287
+ double ImageService::height(double id) const {
288
+ return static_cast<double>(store().height(integer(id)));
289
+ }
290
+
291
+ double ImageService::frameCount(double id) const {
292
+ return static_cast<double>(store().frameCount(integer(id)));
293
+ }
294
+
295
+ bool ImageService::isAnimated(double id) const {
296
+ return store().isAnimated(integer(id));
297
+ }
298
+
299
+ void ImageService::setPlaying(double id, double playing) const {
300
+ store().setPlaying(integer(id), playing != 0.0);
301
+ }
302
+
303
+ void ImageService::seek(double id, double frame) const {
304
+ store().seek(integer(id), integer(frame));
305
+ }
306
+
307
+ bool ImageService::advance(double id, double deltaMs) const {
308
+ return store().advance(integer(id), integer(deltaMs));
309
+ }
310
+
311
+ void ImageService::dispose(double id) const {
312
+ store().dispose(integer(id));
313
+ }
314
+
315
+ GeaEmbeddedImage ImageService::make(double id) const {
316
+ const int slot = integer(id);
317
+ GeaEmbeddedImage handle;
318
+ handle.id = slot;
319
+ handle.width = store().width(slot);
320
+ handle.height = store().height(slot);
321
+ handle.frameCount = store().frameCount(slot);
322
+ handle.isAnimated = store().isAnimated(slot);
323
+ return handle;
324
+ }
325
+
326
+ void GeaEmbeddedImage::play() const { store().setPlaying(id, true); }
327
+ void GeaEmbeddedImage::pause() const { store().setPlaying(id, false); }
328
+ void GeaEmbeddedImage::seek(int frame) const { store().seek(id, frame); }
329
+ void GeaEmbeddedImage::dispose() const { store().dispose(id); }
330
+
331
+ } // namespace gea::host
332
+
333
+ extern "C" double gea_host_image_load_asset_path(const char *path) {
334
+ return gea::host::image.loadAssetPath(path);
335
+ }
package/host/imu.cpp ADDED
@@ -0,0 +1,51 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "imu.h"
3
+ #include "gea/embedded-host.h"
4
+
5
+ namespace gea::framework::sensors {
6
+
7
+ void AccelerometerBackend::init() {
8
+ gea::platform::sensors::Accelerometer::init();
9
+ }
10
+
11
+ void AccelerometerBackend::close() {
12
+ gea::platform::sensors::Accelerometer::close();
13
+ }
14
+
15
+ void AccelerometerBackend::calibrateBias() {
16
+ gea::platform::sensors::Accelerometer::calibrateBias();
17
+ }
18
+
19
+ double AccelerometerBackend::tiltX() {
20
+ return static_cast<double>(gea::platform::sensors::Accelerometer::tiltX());
21
+ }
22
+
23
+ double AccelerometerBackend::tiltY() {
24
+ return static_cast<double>(gea::platform::sensors::Accelerometer::tiltY());
25
+ }
26
+
27
+ double AccelerometerBackend::accelerationX() {
28
+ return gea::platform::sensors::Accelerometer::accelerationX();
29
+ }
30
+
31
+ double AccelerometerBackend::accelerationY() {
32
+ return gea::platform::sensors::Accelerometer::accelerationY();
33
+ }
34
+
35
+ double AccelerometerBackend::accelerationZ() {
36
+ return gea::platform::sensors::Accelerometer::accelerationZ();
37
+ }
38
+
39
+ double AccelerometerBackend::gyroscopeX() {
40
+ return gea::platform::sensors::Accelerometer::gyroscopeX();
41
+ }
42
+
43
+ double AccelerometerBackend::gyroscopeY() {
44
+ return gea::platform::sensors::Accelerometer::gyroscopeY();
45
+ }
46
+
47
+ double AccelerometerBackend::gyroscopeZ() {
48
+ return gea::platform::sensors::Accelerometer::gyroscopeZ();
49
+ }
50
+
51
+ } // namespace gea::framework::sensors
package/host/input.cpp ADDED
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "gea/embedded-host.h"
3
+ #include "input.h"
4
+
5
+ namespace gea::framework::input {
6
+
7
+ bool InputBackend::consumeBackButton()
8
+ {
9
+ return gea::framework::input::consumeBackButton();
10
+ }
11
+
12
+ } // namespace gea::framework::input