@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,291 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include <display.h>
5
+ #include "backends.h"
6
+ #include "ui/document.h"
7
+ #include "ui/style.h"
8
+ #include "ui/tree_internal.h"
9
+
10
+ #include <algorithm>
11
+ #include <cmath>
12
+ #include <string>
13
+ #include <type_traits>
14
+ #include <vector>
15
+
16
+ namespace gea::framework::display {
17
+
18
+ enum class DisplayOrientation {
19
+ PortraitPrimary = 0,
20
+ PortraitSecondary = 1,
21
+ LandscapePrimary = 2,
22
+ LandscapeSecondary = 3,
23
+ };
24
+
25
+ } // namespace gea::framework::display
26
+
27
+ namespace gea::platform::display {
28
+
29
+ #if defined(__GNUC__) || defined(__clang__)
30
+ void applyOrientation(gea::framework::display::DisplayOrientation orientation) __attribute__((weak));
31
+ bool autoRotateAllowed() __attribute__((weak));
32
+ #else
33
+ void applyOrientation(gea::framework::display::DisplayOrientation orientation);
34
+ bool autoRotateAllowed();
35
+ #endif
36
+
37
+ } // namespace gea::platform::display
38
+
39
+ namespace gea::framework::display {
40
+
41
+ namespace detail {
42
+
43
+ inline bool orientationIsLandscape(DisplayOrientation orientation)
44
+ {
45
+ return orientation == DisplayOrientation::LandscapePrimary || orientation == DisplayOrientation::LandscapeSecondary;
46
+ }
47
+
48
+ inline const char *orientationName(DisplayOrientation orientation)
49
+ {
50
+ switch (orientation) {
51
+ case DisplayOrientation::PortraitSecondary:
52
+ return "portrait-secondary";
53
+ case DisplayOrientation::LandscapePrimary:
54
+ return "landscape-primary";
55
+ case DisplayOrientation::LandscapeSecondary:
56
+ return "landscape-secondary";
57
+ case DisplayOrientation::PortraitPrimary:
58
+ default:
59
+ return "portrait-primary";
60
+ }
61
+ }
62
+
63
+ inline DisplayOrientation parseOrientation(const std::string &value, DisplayOrientation fallback = DisplayOrientation::PortraitPrimary)
64
+ {
65
+ if (value == "portrait-primary" || value == "portrait") return DisplayOrientation::PortraitPrimary;
66
+ if (value == "portrait-secondary") return DisplayOrientation::PortraitSecondary;
67
+ if (value == "landscape-primary" || value == "landscape") return DisplayOrientation::LandscapePrimary;
68
+ if (value == "landscape-secondary") return DisplayOrientation::LandscapeSecondary;
69
+ return fallback;
70
+ }
71
+
72
+ inline bool supportTokenMatches(const std::string &token, DisplayOrientation orientation)
73
+ {
74
+ if (token == "all") return true;
75
+ if (token == "portrait") return !orientationIsLandscape(orientation);
76
+ if (token == "landscape") return orientationIsLandscape(orientation);
77
+ return parseOrientation(token, DisplayOrientation::PortraitPrimary) == orientation && token != "portrait";
78
+ }
79
+
80
+ class DisplayOrientationState {
81
+ public:
82
+ static int nativeWidth() { return gea::platform::display::kNativeWidth; }
83
+ static int nativeHeight() { return gea::platform::display::kNativeHeight; }
84
+
85
+ static int width() { return orientationIsLandscape(orientation()) ? nativeHeight() : nativeWidth(); }
86
+ static int height() { return orientationIsLandscape(orientation()) ? nativeWidth() : nativeHeight(); }
87
+
88
+ static DisplayOrientation orientation() { return orientation_; }
89
+ static std::string orientationString() { return std::string(orientationName(orientation_)); }
90
+
91
+ static void setOrientation(const std::string &value)
92
+ {
93
+ setOrientation(parseOrientation(value, orientation_));
94
+ }
95
+
96
+ static void setOrientation(DisplayOrientation next)
97
+ {
98
+ // A device that doesn't implement the rotation hook is fixed to its native
99
+ // scanout orientation (PortraitPrimary); ignore other requests so the
100
+ // app still renders natively instead of into a mismatched framebuffer.
101
+ if (!platformSupportsRotation() && next != DisplayOrientation::PortraitPrimary) return;
102
+ if (!supports(next)) return;
103
+ if (orientation_ == next) return;
104
+ orientation_ = next;
105
+ applyMetrics();
106
+ }
107
+
108
+ static bool autoRotate() { return autoRotate_; }
109
+
110
+ static void setAutoRotate(bool enabled)
111
+ {
112
+ if (enabled && !platformAllowsAutoRotate()) {
113
+ autoRotate_ = false;
114
+ return;
115
+ }
116
+ autoRotate_ = enabled;
117
+ if (autoRotate_) {
118
+ gea::framework::sensors::AccelerometerBackend::init();
119
+ updateAutoRotationFromAccelerometer();
120
+ }
121
+ }
122
+
123
+ static std::vector<std::string> supportedOrientations()
124
+ {
125
+ return supportedOrientations_;
126
+ }
127
+
128
+ static void setSupportedOrientations(const std::string &orientation)
129
+ {
130
+ setSupportedOrientations(std::vector<std::string>{orientation});
131
+ }
132
+
133
+ static void setSupportedOrientations(const std::vector<std::string> &orientations)
134
+ {
135
+ supportedOrientations_.clear();
136
+ for (const auto &orientation : orientations) {
137
+ if (orientation == "all" || orientation == "portrait" || orientation == "landscape" ||
138
+ orientation == "portrait-primary" || orientation == "portrait-secondary" ||
139
+ orientation == "landscape-primary" || orientation == "landscape-secondary") {
140
+ if (std::find(supportedOrientations_.begin(), supportedOrientations_.end(), orientation) == supportedOrientations_.end()) {
141
+ supportedOrientations_.push_back(orientation);
142
+ }
143
+ }
144
+ }
145
+ if (supportedOrientations_.empty()) supportedOrientations_.push_back("portrait-primary");
146
+ // Without a platform rotation hook the device only supports its native
147
+ // orientation, regardless of what the app asks for (so setSupported
148
+ // Orientations('landscape') is effectively a no-op and the app renders).
149
+ if (!platformSupportsRotation()) supportedOrientations_.assign(1, "portrait-primary");
150
+ if (!supports(orientation_)) setOrientation(firstSupportedConcrete());
151
+ }
152
+
153
+ template <typename Options>
154
+ static void setSupportedOrientationsFrom(const Options &options)
155
+ {
156
+ if constexpr (std::is_convertible_v<Options, std::string>) {
157
+ setSupportedOrientations(static_cast<std::string>(options));
158
+ } else if constexpr (requires { options.size(); options[0]; }) {
159
+ std::vector<std::string> values;
160
+ for (const auto &item : options) values.push_back(static_cast<std::string>(item));
161
+ setSupportedOrientations(values);
162
+ } else {
163
+ setSupportedOrientations(static_cast<std::string>(options));
164
+ }
165
+ }
166
+
167
+ static void updateAutoRotationFromAccelerometer()
168
+ {
169
+ if (!autoRotate_) return;
170
+ updateAutoRotation(gea::framework::sensors::AccelerometerBackend::accelerationX(),
171
+ gea::framework::sensors::AccelerometerBackend::accelerationY(),
172
+ gea::framework::sensors::AccelerometerBackend::accelerationZ());
173
+ }
174
+
175
+ static void updateAutoRotation(double accelerationX, double accelerationY, double accelerationZ)
176
+ {
177
+ if (!autoRotate_) return;
178
+ const double planar = std::sqrt(accelerationX * accelerationX + accelerationY * accelerationY);
179
+ if (planar < std::max(3.0, std::fabs(accelerationZ) * 0.35)) return;
180
+ DisplayOrientation next = orientation_;
181
+ if (std::fabs(accelerationX) > std::fabs(accelerationY)) {
182
+ next = accelerationX < 0 ? DisplayOrientation::LandscapePrimary : DisplayOrientation::LandscapeSecondary;
183
+ } else {
184
+ next = accelerationY > 0 ? DisplayOrientation::PortraitPrimary : DisplayOrientation::PortraitSecondary;
185
+ }
186
+ if (supports(next)) setOrientation(next);
187
+ }
188
+
189
+ static void mapPanelToViewport(int *x, int *y)
190
+ {
191
+ if (!x || !y) return;
192
+ const int px = *x;
193
+ const int py = *y;
194
+ const int w = nativeWidth();
195
+ const int h = nativeHeight();
196
+ switch (orientation_) {
197
+ case DisplayOrientation::PortraitSecondary:
198
+ *x = w - 1 - px;
199
+ *y = h - 1 - py;
200
+ break;
201
+ case DisplayOrientation::LandscapePrimary:
202
+ *x = h - 1 - py;
203
+ *y = px;
204
+ break;
205
+ case DisplayOrientation::LandscapeSecondary:
206
+ *x = py;
207
+ *y = w - 1 - px;
208
+ break;
209
+ case DisplayOrientation::PortraitPrimary:
210
+ default:
211
+ *x = px;
212
+ *y = py;
213
+ break;
214
+ }
215
+ }
216
+
217
+ private:
218
+ // A device can leave its native scanout orientation only when the platform
219
+ // provides the rotation hook (a strong gea::platform::display::applyOrientation,
220
+ // e.g. the ESP32-P4 PPA path). The CO5300 QSPI boards don't (the panel has no
221
+ // swap_xy and the S3 has no PPA), so for them the weak symbol stays null,
222
+ // orientation is fixed to native, and setOrientation/setSupportedOrientations
223
+ // become no-ops — a landscape-requesting app (e.g. maps) then renders in the
224
+ // panel's native orientation instead of drawing into a mismatched (black) FB.
225
+ static bool platformSupportsRotation()
226
+ {
227
+ #if defined(__GNUC__) || defined(__clang__)
228
+ return gea::platform::display::applyOrientation != nullptr;
229
+ #else
230
+ return true;
231
+ #endif
232
+ }
233
+
234
+ static bool platformAllowsAutoRotate()
235
+ {
236
+ #if defined(__GNUC__) || defined(__clang__)
237
+ if (gea::platform::display::autoRotateAllowed) return gea::platform::display::autoRotateAllowed();
238
+ return true;
239
+ #else
240
+ return gea::platform::display::autoRotateAllowed();
241
+ #endif
242
+ }
243
+
244
+ static bool supports(DisplayOrientation orientation)
245
+ {
246
+ return std::any_of(supportedOrientations_.begin(), supportedOrientations_.end(), [orientation](const std::string &token) {
247
+ return supportTokenMatches(token, orientation);
248
+ });
249
+ }
250
+
251
+ static DisplayOrientation firstSupportedConcrete()
252
+ {
253
+ for (const auto &token : supportedOrientations_) {
254
+ if (token == "all" || token == "portrait" || token == "portrait-primary") return DisplayOrientation::PortraitPrimary;
255
+ if (token == "portrait-secondary") return DisplayOrientation::PortraitSecondary;
256
+ if (token == "landscape" || token == "landscape-primary") return DisplayOrientation::LandscapePrimary;
257
+ if (token == "landscape-secondary") return DisplayOrientation::LandscapeSecondary;
258
+ }
259
+ return DisplayOrientation::PortraitPrimary;
260
+ }
261
+
262
+ static void applyMetrics()
263
+ {
264
+ const int w = width();
265
+ const int h = height();
266
+ gea::embedded::ui::Document::setPreferredMountSize(w, h);
267
+ gea::embedded::ui::setViewportMetrics(w, h, gea::embedded::ui::devicePixelRatio());
268
+ #if defined(__GNUC__) || defined(__clang__)
269
+ if (gea::platform::display::applyOrientation) gea::platform::display::applyOrientation(orientation_);
270
+ #else
271
+ gea::platform::display::applyOrientation(orientation_);
272
+ #endif
273
+ auto &tree = gea::embedded::ui::Tree::instance();
274
+ const int root = tree.mountedRoot();
275
+ if (root >= 0) {
276
+ auto &document = gea::embedded::ui::Document::instance();
277
+ auto body = document.body();
278
+ body.style().width(w);
279
+ body.style().height(h);
280
+ document.mount(body, w, h);
281
+ }
282
+ }
283
+
284
+ inline static DisplayOrientation orientation_ = DisplayOrientation::PortraitPrimary;
285
+ inline static bool autoRotate_ = false;
286
+ inline static std::vector<std::string> supportedOrientations_{"portrait-primary"};
287
+ };
288
+
289
+ } // namespace detail
290
+
291
+ } // namespace gea::framework::display
@@ -0,0 +1,310 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include <algorithm>
5
+ #include <atomic>
6
+ #include <cctype>
7
+ #include <cstdint>
8
+ #include <functional>
9
+ #include <map>
10
+ #include <string>
11
+ #include <type_traits>
12
+ #include <vector>
13
+
14
+ namespace gea::host {
15
+
16
+ using FetchHeaderMap = std::map<std::string, std::string>;
17
+
18
+ struct FetchResponse {
19
+ bool ok = false;
20
+ double status = 0;
21
+ std::string status_text;
22
+ FetchHeaderMap headers;
23
+ std::vector<std::uint8_t> body;
24
+
25
+ std::string text() const {
26
+ return std::string(reinterpret_cast<const char *>(body.data()), body.size());
27
+ }
28
+
29
+ // Raw response body. Both spellings return the bytes by value so the typed
30
+ // `await fetch(url).bytes()` / `.arrayBuffer()` flow straight into
31
+ // `loadImage(Uint8Array)` (which lowers to `std::vector<std::uint8_t>`).
32
+ std::vector<std::uint8_t> bytes() const {
33
+ return body;
34
+ }
35
+
36
+ std::vector<std::uint8_t> arrayBuffer() const {
37
+ return body;
38
+ }
39
+
40
+ #ifdef GEA_CPP_VALUE_AVAILABLE
41
+ gea_cpp_value __gea_to_value() const {
42
+ gea_cpp_value out = gea_cpp_value::empty_record();
43
+ out.record_set_literal("ok", gea_cpp_key(ok));
44
+ out.record_set_literal("status", gea_cpp_key(status));
45
+ out.record_set_literal("statusText", gea_cpp_key(status_text));
46
+ out.record_set_literal("status_text", gea_cpp_key(status_text));
47
+
48
+ gea_cpp_value header_record = gea_cpp_value::empty_record();
49
+ for (const auto &kv : headers) {
50
+ header_record.record_set_literal(kv.first.c_str(), gea_cpp_key(kv.second));
51
+ }
52
+ out.record_set_literal("headers", header_record);
53
+
54
+ out.record_set_literal("text", gea_cpp_value(std::function<std::string()>([body_text = text()]() {
55
+ return body_text;
56
+ })));
57
+ return out;
58
+ }
59
+ #endif
60
+ };
61
+
62
+ struct FetchRequestInit {
63
+ std::string method = "GET";
64
+ FetchHeaderMap headers;
65
+ std::string body;
66
+ // Per-request timeout override in ms; 0 = use the builder default. Callers
67
+ // that retry (e.g. the tile loader) set this short so a transient link stall
68
+ // fails fast and is retried, instead of waiting out a ~15s TCP retransmit.
69
+ int timeout_ms = 0;
70
+
71
+ void setBody(const std::string &value) {
72
+ body = value;
73
+ }
74
+
75
+ void setBody(const std::vector<std::uint8_t> &value) {
76
+ body.assign(reinterpret_cast<const char *>(value.data()), value.size());
77
+ }
78
+ };
79
+
80
+ FetchResponse fetch(const std::string &url);
81
+ FetchResponse fetch(const std::string &url, const FetchRequestInit &init);
82
+ // Synchronous streaming multipart upload (see fetchUploadFileAsync). Runs on the
83
+ // caller's task; fetchUploadFileAsync drives it from a background task.
84
+ FetchResponse uploadFile(const std::string &url, const std::string &authorization,
85
+ const std::string &contentType, const std::string &prefix,
86
+ const std::string &filePath, const std::string &suffix);
87
+ // Synchronous streaming download: GET `url` and write the body to `destPath` in
88
+ // chunks (never buffered whole in RAM). `authorization` sets the Authorization
89
+ // header (pass "" to skip). Runs on the caller's task; fetchDownloadFileAsync
90
+ // drives it from a background task.
91
+ FetchResponse downloadFile(const std::string &url, const std::string &authorization,
92
+ const std::string &destPath);
93
+ double fetchAsync(const std::string &url);
94
+ double fetchAsync(const std::string &url, const FetchRequestInit &init);
95
+ bool fetchReady(double id);
96
+ FetchResponse fetchResult(double id);
97
+ void fetchRelease(double id);
98
+
99
+ // Streaming multipart file upload (POST). The request body is sent as
100
+ // `prefix` + <`filePath` contents, streamed from disk in chunks> + `suffix`, so
101
+ // a large file (e.g. a long WAV) is uploaded without ever loading it into RAM.
102
+ // `authorization` / `contentType` set those two request headers (pass "" to
103
+ // skip). Async: returns a job id, polled/collected with the same
104
+ // fetchReady / fetchResult / fetchRelease as fetchAsync.
105
+ double fetchUploadFileAsync(const std::string &url, const std::string &authorization,
106
+ const std::string &contentType, const std::string &prefix,
107
+ const std::string &filePath, const std::string &suffix);
108
+ // Streaming download (GET) to a device file path, written in chunks off the
109
+ // network worker. Async: returns a job id, polled with fetchReady / fetchResult
110
+ // / fetchRelease like fetchAsync. Progress shares the fetchUploadProgress /
111
+ // fetchUploadSent / fetchUploadTotal counters (one transfer runs at a time).
112
+ double fetchDownloadFileAsync(const std::string &url, const std::string &authorization,
113
+ const std::string &destPath);
114
+
115
+ // Upload progress for the single in-flight streaming upload (Sync uploads one
116
+ // note at a time). The streamer reports bytes via uploadProgressReset/Add; JS
117
+ // reads the fraction via fetchUploadProgress. Header-only (function-local
118
+ // statics share one instance across TUs) so no namespace-placement juggling.
119
+ inline std::atomic<long long> &__uploadSentBytes() {
120
+ static std::atomic<long long> v{0};
121
+ return v;
122
+ }
123
+ inline std::atomic<long long> &__uploadTotalBytes() {
124
+ static std::atomic<long long> v{0};
125
+ return v;
126
+ }
127
+ inline void uploadProgressReset(long long total) {
128
+ __uploadSentBytes().store(0);
129
+ __uploadTotalBytes().store(total);
130
+ }
131
+ inline void uploadProgressAdd(long long bytes) { __uploadSentBytes().fetch_add(bytes); }
132
+ // 0..1 fraction of the active upload, or -1 when no upload total is set.
133
+ inline double fetchUploadProgress(double /*id*/) {
134
+ const long long total = __uploadTotalBytes().load();
135
+ if (total <= 0) return -1.0;
136
+ double frac = static_cast<double>(__uploadSentBytes().load()) / static_cast<double>(total);
137
+ if (frac < 0.0) frac = 0.0;
138
+ if (frac > 1.0) frac = 1.0;
139
+ return frac;
140
+ }
141
+ // Raw byte counts for the active upload (for MB / speed readouts). 0 when idle.
142
+ inline double fetchUploadSent(double /*id*/) { return static_cast<double>(__uploadSentBytes().load()); }
143
+ inline double fetchUploadTotal(double /*id*/) { return static_cast<double>(__uploadTotalBytes().load()); }
144
+
145
+ #ifdef GEA_CPP_VALUE_AVAILABLE
146
+ inline FetchRequestInit fetch_init_from_value(const gea_cpp_value &init_value) {
147
+ FetchRequestInit init;
148
+ if (init_value.record_has(std::string("method"))) {
149
+ init.method = static_cast<std::string>(init_value.record_get(std::string("method")));
150
+ }
151
+ if (init_value.record_has(std::string("body"))) {
152
+ gea_cpp_value body_value = init_value.record_get(std::string("body"));
153
+ if (body_value.kind == gea_cpp_value::kind_t::array_value) {
154
+ const std::vector<gea_cpp_value> values = static_cast<std::vector<gea_cpp_value>>(body_value);
155
+ init.body.resize(values.size());
156
+ for (std::size_t i = 0; i < values.size(); i++) {
157
+ const auto byte = static_cast<int>(static_cast<double>(values[i]));
158
+ init.body[i] = static_cast<char>(byte & 0xff);
159
+ }
160
+ } else {
161
+ init.body = static_cast<std::string>(body_value);
162
+ }
163
+ }
164
+ if (init_value.record_has(std::string("headers"))) {
165
+ auto headers_value = init_value.record_get(std::string("headers"));
166
+ if (headers_value.entries) {
167
+ for (const auto &kv : *headers_value.entries) {
168
+ if (kv.second.kind != gea_cpp_value::kind_t::string) continue;
169
+ std::string lower(kv.first.size(), '\0');
170
+ std::transform(kv.first.begin(), kv.first.end(), lower.begin(),
171
+ [](unsigned char c) { return std::tolower(c); });
172
+ init.headers[lower] = static_cast<std::string>(kv.second);
173
+ }
174
+ }
175
+ }
176
+ return init;
177
+ }
178
+
179
+ inline FetchResponse fetch(const std::string &url, const gea_cpp_value &init) {
180
+ return fetch(url, fetch_init_from_value(init));
181
+ }
182
+
183
+ template <typename T, typename = void>
184
+ struct fetch_has_field_method : std::false_type {};
185
+ template <typename T>
186
+ struct fetch_has_field_method<T, std::void_t<decltype(std::declval<const T &>().method)>> : std::true_type {};
187
+
188
+ template <typename T, typename = void>
189
+ struct fetch_has_field_headers : std::false_type {};
190
+ template <typename T>
191
+ struct fetch_has_field_headers<T, std::void_t<decltype(std::declval<const T &>().headers)>> : std::true_type {};
192
+
193
+ template <typename T, typename = void>
194
+ struct fetch_has_field_body : std::false_type {};
195
+ template <typename T>
196
+ struct fetch_has_field_body<T, std::void_t<decltype(std::declval<const T &>().body)>> : std::true_type {};
197
+
198
+ template <typename T, typename = void>
199
+ struct fetch_has_flag_method : std::false_type {};
200
+ template <typename T>
201
+ struct fetch_has_flag_method<T, std::void_t<decltype(std::declval<const T &>().__gea_has_method)>> : std::true_type {};
202
+
203
+ template <typename T, typename = void>
204
+ struct fetch_has_flag_headers : std::false_type {};
205
+ template <typename T>
206
+ struct fetch_has_flag_headers<T, std::void_t<decltype(std::declval<const T &>().__gea_has_headers)>> : std::true_type {};
207
+
208
+ template <typename T, typename = void>
209
+ struct fetch_has_flag_body : std::false_type {};
210
+ template <typename T>
211
+ struct fetch_has_flag_body<T, std::void_t<decltype(std::declval<const T &>().__gea_has_body)>> : std::true_type {};
212
+
213
+ template <typename T>
214
+ inline bool fetch_field_present_method(const T &record) {
215
+ if constexpr (fetch_has_flag_method<T>::value) return record.__gea_has_method;
216
+ else return true;
217
+ }
218
+
219
+ template <typename T>
220
+ inline bool fetch_field_present_headers(const T &record) {
221
+ if constexpr (fetch_has_flag_headers<T>::value) return record.__gea_has_headers;
222
+ else return true;
223
+ }
224
+
225
+ template <typename T>
226
+ inline bool fetch_field_present_body(const T &record) {
227
+ if constexpr (fetch_has_flag_body<T>::value) return record.__gea_has_body;
228
+ else return true;
229
+ }
230
+
231
+ inline void fetch_assign_headers(FetchRequestInit &init, const gea_cpp_value &headers_value) {
232
+ if (!headers_value.entries) return;
233
+ for (const auto &kv : *headers_value.entries) {
234
+ if (kv.second.kind != gea_cpp_value::kind_t::string) continue;
235
+ std::string lower(kv.first.size(), '\0');
236
+ std::transform(kv.first.begin(), kv.first.end(), lower.begin(),
237
+ [](unsigned char c) { return std::tolower(c); });
238
+ init.headers[lower] = static_cast<std::string>(kv.second);
239
+ }
240
+ }
241
+
242
+ template <typename Headers>
243
+ inline void fetch_assign_headers(FetchRequestInit &init, const Headers &headers) {
244
+ if constexpr (requires { headers.__gea_to_value(); }) {
245
+ fetch_assign_headers(init, headers.__gea_to_value());
246
+ } else {
247
+ fetch_assign_headers(init, gea_cpp_value::pack_value(headers));
248
+ }
249
+ }
250
+
251
+ template <typename Body>
252
+ inline void fetch_assign_body(FetchRequestInit &init, const Body &body) {
253
+ using B = std::decay_t<Body>;
254
+ if constexpr (std::is_same_v<B, std::string>) {
255
+ init.setBody(body);
256
+ } else if constexpr (requires {
257
+ body.data();
258
+ body.size();
259
+ requires std::is_same_v<std::decay_t<decltype(*body.data())>, std::uint8_t>;
260
+ }) {
261
+ // A byte buffer (Uint8Array / ArrayBuffer). The is_same check lives INSIDE
262
+ // the requires so a body type without `.data()` (e.g. a `gea_cpp_value` from
263
+ // an omitted optional `body`) is SFINAE-false here rather than a hard error.
264
+ init.body.assign(reinterpret_cast<const char *>(body.data()), body.size());
265
+ } else {
266
+ gea_cpp_value body_value = gea_cpp_value::pack_value(body);
267
+ if (body_value.kind == gea_cpp_value::kind_t::array_value) {
268
+ const std::vector<gea_cpp_value> values = static_cast<std::vector<gea_cpp_value>>(body_value);
269
+ init.body.resize(values.size());
270
+ for (std::size_t i = 0; i < values.size(); i++) {
271
+ const auto byte = static_cast<int>(static_cast<double>(values[i]));
272
+ init.body[i] = static_cast<char>(byte & 0xff);
273
+ }
274
+ } else {
275
+ init.body = static_cast<std::string>(body_value);
276
+ }
277
+ }
278
+ }
279
+
280
+ template <typename Init>
281
+ inline FetchRequestInit fetch_init_from_record(const Init &record) {
282
+ FetchRequestInit init;
283
+ if constexpr (fetch_has_field_method<Init>::value) {
284
+ if (fetch_field_present_method(record)) init.method = record.method;
285
+ }
286
+ if constexpr (fetch_has_field_headers<Init>::value) {
287
+ if (fetch_field_present_headers(record)) fetch_assign_headers(init, record.headers);
288
+ }
289
+ if constexpr (fetch_has_field_body<Init>::value) {
290
+ if (fetch_field_present_body(record)) fetch_assign_body(init, record.body);
291
+ }
292
+ return init;
293
+ }
294
+
295
+ template <typename Init,
296
+ typename = std::enable_if_t<!std::is_same_v<std::decay_t<Init>, FetchRequestInit> &&
297
+ !std::is_same_v<std::decay_t<Init>, gea_cpp_value>>>
298
+ inline FetchResponse fetch(const std::string &url, const Init &init) {
299
+ return fetch(url, fetch_init_from_record(init));
300
+ }
301
+
302
+ template <typename Init,
303
+ typename = std::enable_if_t<!std::is_same_v<std::decay_t<Init>, FetchRequestInit> &&
304
+ !std::is_same_v<std::decay_t<Init>, gea_cpp_value>>>
305
+ inline double fetchAsync(const std::string &url, const Init &init) {
306
+ return fetchAsync(url, fetch_init_from_record(init));
307
+ }
308
+ #endif
309
+
310
+ } // namespace gea::host
@@ -0,0 +1,36 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "backends.h"
5
+
6
+ namespace gea::host {
7
+
8
+ struct GpioFacade {
9
+ bool configureOutput(double pin) const { return gea::framework::gpio::GpioBackend::configureOutput(pin); }
10
+ bool configureInput(double pin, bool pull_up) const
11
+ {
12
+ return gea::framework::gpio::GpioBackend::configureInput(pin, pull_up);
13
+ }
14
+ bool write(double pin, bool level) const { return gea::framework::gpio::GpioBackend::write(pin, level); }
15
+ bool read(double pin) const { return gea::framework::gpio::GpioBackend::read(pin); }
16
+ };
17
+
18
+ struct LedFacade {
19
+ bool set(double pin, double r, double g, double b) const
20
+ {
21
+ return gea::framework::gpio::AddressableLedBackend::set(pin, r, g, b);
22
+ }
23
+ bool off(double pin) const { return gea::framework::gpio::AddressableLedBackend::off(pin); }
24
+ bool attach(double pin, double count) const { return gea::framework::gpio::AddressableLedBackend::attach(pin, count); }
25
+ bool setPixel(double index, double r, double g, double b) const
26
+ {
27
+ return gea::framework::gpio::AddressableLedBackend::setPixel(index, r, g, b);
28
+ }
29
+ bool show() const { return gea::framework::gpio::AddressableLedBackend::show(); }
30
+ void detach() const { gea::framework::gpio::AddressableLedBackend::detach(); }
31
+ };
32
+
33
+ inline constexpr GpioFacade Gpio{};
34
+ inline constexpr LedFacade Led{};
35
+
36
+ } // namespace gea::host