@geastack/host 0.1.14 → 0.1.16

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.
@@ -24,7 +24,7 @@ public:
24
24
  static double gyroscopeZ();
25
25
  };
26
26
 
27
- } // namespace gea::framework::sensors
27
+ } // namespace gea::framework::sensors
28
28
 
29
29
  namespace gea::framework::camera {
30
30
 
@@ -36,7 +36,8 @@ public:
36
36
 
37
37
  // facing: "front" | "back" | <device id from deviceIdAt>
38
38
  // preferredWidth/Height: 0 means "platform default"
39
- static bool open(const std::string &facing, double preferredWidth, double preferredHeight);
39
+ static bool open(const std::string &facing, double preferredWidth,
40
+ double preferredHeight);
40
41
  static void close();
41
42
  static bool isOpen();
42
43
 
@@ -56,6 +57,7 @@ public:
56
57
  // Capture a still image; returns the ImageStore id or -1 on failure.
57
58
  static double capture();
58
59
  static double captureMirrored();
60
+ static std::string captureFrame();
59
61
 
60
62
  // Video recording (esp32-p4: MJPEG). startRecording returns true on success;
61
63
  // stopRecording returns the clip duration in ms, or -1 on failure.
@@ -69,8 +71,10 @@ public:
69
71
 
70
72
  // Capture controls forwarded to the platform backend. See camera.h for the
71
73
  // accepted `mode` keywords; numeric args default to 0 ("leave as is").
72
- static void setExposure(const std::string &mode, double bias, double iso, double durationMs);
73
- static void setWhiteBalance(const std::string &mode, double temperatureK, double tint);
74
+ static void setExposure(const std::string &mode, double bias, double iso,
75
+ double durationMs);
76
+ static void setWhiteBalance(const std::string &mode, double temperatureK,
77
+ double tint);
74
78
  static void setFocus(const std::string &mode, double pointX, double pointY);
75
79
  static void setTorch(const std::string &mode, double level);
76
80
 
@@ -86,20 +90,24 @@ public:
86
90
  facing = static_cast<std::string>(options.facing);
87
91
  } else if constexpr (requires { options.record_get_literal("facing"); }) {
88
92
  auto value = options.record_get_literal("facing");
89
- if (!value.is_nullish()) facing = static_cast<std::string>(value);
93
+ if (!value.is_nullish())
94
+ facing = static_cast<std::string>(value);
90
95
  }
91
- if (facing.empty()) facing = "back";
96
+ if (facing.empty())
97
+ facing = "back";
92
98
  if constexpr (requires { options.width; }) {
93
99
  width = static_cast<double>(options.width);
94
100
  } else if constexpr (requires { options.record_get_literal("width"); }) {
95
101
  auto value = options.record_get_literal("width");
96
- if (!value.is_nullish()) width = static_cast<double>(value);
102
+ if (!value.is_nullish())
103
+ width = static_cast<double>(value);
97
104
  }
98
105
  if constexpr (requires { options.height; }) {
99
106
  height = static_cast<double>(options.height);
100
107
  } else if constexpr (requires { options.record_get_literal("height"); }) {
101
108
  auto value = options.record_get_literal("height");
102
- if (!value.is_nullish()) height = static_cast<double>(value);
109
+ if (!value.is_nullish())
110
+ height = static_cast<double>(value);
103
111
  }
104
112
  return open(facing, width, height);
105
113
  }
@@ -112,7 +120,8 @@ public:
112
120
  mirror = static_cast<bool>(options.mirror);
113
121
  } else if constexpr (requires { options.record_get_literal("mirror"); }) {
114
122
  auto value = options.record_get_literal("mirror");
115
- if (!value.is_nullish()) mirror = static_cast<bool>(value);
123
+ if (!value.is_nullish())
124
+ mirror = static_cast<bool>(value);
116
125
  }
117
126
  return mirror ? captureMirrored() : capture();
118
127
  }
@@ -123,18 +132,22 @@ public:
123
132
  // anonymous options structs — are read field-by-field in each control form
124
133
  // below, since member selection needs the field name at compile time.
125
134
  template <typename Options>
126
- static std::string optionString(const Options &options, const char *field, const std::string &fallback) {
135
+ static std::string optionString(const Options &options, const char *field,
136
+ const std::string &fallback) {
127
137
  if constexpr (requires { options.record_get_literal(field); }) {
128
138
  auto value = options.record_get_literal(field);
129
- if (!value.is_nullish()) return static_cast<std::string>(value);
139
+ if (!value.is_nullish())
140
+ return static_cast<std::string>(value);
130
141
  }
131
142
  return fallback;
132
143
  }
133
144
  template <typename Options>
134
- static double optionNumber(const Options &options, const char *field, double fallback) {
145
+ static double optionNumber(const Options &options, const char *field,
146
+ double fallback) {
135
147
  if constexpr (requires { options.record_get_literal(field); }) {
136
148
  auto value = options.record_get_literal(field);
137
- if (!value.is_nullish()) return static_cast<double>(value);
149
+ if (!value.is_nullish())
150
+ return static_cast<double>(value);
138
151
  }
139
152
  return fallback;
140
153
  }
@@ -145,22 +158,29 @@ public:
145
158
  // so bare presence of the member means present), falling back to the dynamic
146
159
  // record arm, then the default. Expression-shaped so the readers stay
147
160
  // declarative; undefined behind this header's include boundary.
148
- #define GEA_CAMERA_OPTION_FIELD(options, field, fallback, castT) \
149
- ([&]() { \
150
- if constexpr (requires { (options).field; }) { \
151
- bool __gea_present = true; \
152
- if constexpr (requires { (options).__gea_has_##field; }) __gea_present = (options).__gea_has_##field; \
153
- if (__gea_present) return static_cast<castT>((options).field); \
154
- } \
155
- return static_cast<castT>(optionsFieldFallback_##castT((options), #field, fallback)); \
161
+ #define GEA_CAMERA_OPTION_FIELD(options, field, fallback, castT) \
162
+ ([&]() { \
163
+ if constexpr (requires { (options).field; }) { \
164
+ bool __gea_present = true; \
165
+ if constexpr (requires { (options).__gea_has_##field; }) \
166
+ __gea_present = (options).__gea_has_##field; \
167
+ if (__gea_present) \
168
+ return static_cast<castT>((options).field); \
169
+ } \
170
+ return static_cast<castT>( \
171
+ optionsFieldFallback_##castT((options), #field, fallback)); \
156
172
  }())
157
173
 
158
174
  template <typename Options>
159
- static std::string optionsFieldFallback_OptionStringT(const Options &options, const char *field, const std::string &fallback) {
175
+ static std::string
176
+ optionsFieldFallback_OptionStringT(const Options &options, const char *field,
177
+ const std::string &fallback) {
160
178
  return optionString(options, field, fallback);
161
179
  }
162
180
  template <typename Options>
163
- static double optionsFieldFallback_OptionNumberT(const Options &options, const char *field, double fallback) {
181
+ static double optionsFieldFallback_OptionNumberT(const Options &options,
182
+ const char *field,
183
+ double fallback) {
164
184
  return optionNumber(options, field, fallback);
165
185
  }
166
186
  using OptionStringT = std::string;
@@ -171,16 +191,20 @@ public:
171
191
  // to a typed struct (preferred) or a dynamic gea_cpp_value record (legacy).
172
192
  template <typename Options>
173
193
  static void setExposureWithOptions(const Options &options) {
174
- setExposure(GEA_CAMERA_OPTION_FIELD(options, mode, std::string("continuous"), OptionStringT),
175
- GEA_CAMERA_OPTION_FIELD(options, bias, 0.0, OptionNumberT),
176
- GEA_CAMERA_OPTION_FIELD(options, iso, 0.0, OptionNumberT),
177
- GEA_CAMERA_OPTION_FIELD(options, durationMs, 0.0, OptionNumberT));
194
+ setExposure(
195
+ GEA_CAMERA_OPTION_FIELD(options, mode, std::string("continuous"),
196
+ OptionStringT),
197
+ GEA_CAMERA_OPTION_FIELD(options, bias, 0.0, OptionNumberT),
198
+ GEA_CAMERA_OPTION_FIELD(options, iso, 0.0, OptionNumberT),
199
+ GEA_CAMERA_OPTION_FIELD(options, durationMs, 0.0, OptionNumberT));
178
200
  }
179
201
  template <typename Options>
180
202
  static void setWhiteBalanceWithOptions(const Options &options) {
181
- setWhiteBalance(GEA_CAMERA_OPTION_FIELD(options, mode, std::string("auto"), OptionStringT),
182
- GEA_CAMERA_OPTION_FIELD(options, temperature, 0.0, OptionNumberT),
183
- GEA_CAMERA_OPTION_FIELD(options, tint, 0.0, OptionNumberT));
203
+ setWhiteBalance(
204
+ GEA_CAMERA_OPTION_FIELD(options, mode, std::string("auto"),
205
+ OptionStringT),
206
+ GEA_CAMERA_OPTION_FIELD(options, temperature, 0.0, OptionNumberT),
207
+ GEA_CAMERA_OPTION_FIELD(options, tint, 0.0, OptionNumberT));
184
208
  }
185
209
  template <typename Options>
186
210
  static void setFocusWithOptions(const Options &options) {
@@ -197,12 +221,15 @@ public:
197
221
  py = optionNumber(point, "y", 0.0);
198
222
  }
199
223
  }
200
- setFocus(GEA_CAMERA_OPTION_FIELD(options, mode, std::string("continuous"), OptionStringT), px, py);
224
+ setFocus(GEA_CAMERA_OPTION_FIELD(options, mode, std::string("continuous"),
225
+ OptionStringT),
226
+ px, py);
201
227
  }
202
228
  template <typename Options>
203
229
  static bool startRecordingWithOptions(const Options &options) {
204
- return startRecording(GEA_CAMERA_OPTION_FIELD(options, path, std::string(""), OptionStringT),
205
- GEA_CAMERA_OPTION_FIELD(options, fps, 0.0, OptionNumberT));
230
+ return startRecording(
231
+ GEA_CAMERA_OPTION_FIELD(options, path, std::string(""), OptionStringT),
232
+ GEA_CAMERA_OPTION_FIELD(options, fps, 0.0, OptionNumberT));
206
233
  }
207
234
  #undef GEA_CAMERA_OPTION_FIELD
208
235
  };
@@ -213,7 +240,7 @@ public:
213
240
  // registers its driver. Defined in host/camera.cpp.
214
241
  void registerCameraSurface();
215
242
 
216
- } // namespace gea::framework::camera
243
+ } // namespace gea::framework::camera
217
244
 
218
245
  namespace gea::framework::network {
219
246
 
@@ -227,6 +254,12 @@ public:
227
254
  static std::string ip();
228
255
  static std::string mac();
229
256
  static void configure(const std::string &ssid, const std::string &password);
257
+ static bool startAccessPoint(const std::string &ssid,
258
+ const std::string &password);
259
+ static void stopAccessPoint();
260
+ static std::string accessPointMac();
261
+ static bool startCaptivePortal();
262
+ static void stopCaptivePortal();
230
263
  static bool waitForConnection(double timeoutMs);
231
264
  static void startScan();
232
265
  static bool scanning();
@@ -236,7 +269,7 @@ public:
236
269
  static bool scanSecuredAt(double index);
237
270
  };
238
271
 
239
- } // namespace gea::framework::network
272
+ } // namespace gea::framework::network
240
273
 
241
274
  namespace gea::framework::geolocation {
242
275
 
@@ -249,13 +282,14 @@ public:
249
282
  static double accuracy();
250
283
  };
251
284
 
252
- } // namespace gea::framework::geolocation
285
+ } // namespace gea::framework::geolocation
253
286
 
254
287
  namespace gea::framework::bluetooth {
255
288
 
256
289
  class HidBackend {
257
290
  public:
258
- static void init(const std::string &device_name, double appearance, const std::string &mac_address);
291
+ static void init(const std::string &device_name, double appearance,
292
+ const std::string &mac_address);
259
293
  static bool enabled();
260
294
  static void setEnabled(bool enabled);
261
295
  static void startAdvertising();
@@ -306,7 +340,7 @@ public:
306
340
  static void configPushActivity(double index);
307
341
  };
308
342
 
309
- } // namespace gea::framework::bluetooth
343
+ } // namespace gea::framework::bluetooth
310
344
 
311
345
  namespace gea::framework::audio {
312
346
 
@@ -316,7 +350,7 @@ public:
316
350
  static void setVolume(double volume);
317
351
  };
318
352
 
319
- } // namespace gea::framework::audio
353
+ } // namespace gea::framework::audio
320
354
 
321
355
  namespace gea::framework::display {
322
356
 
@@ -330,11 +364,11 @@ public:
330
364
  // without an e-paper panel). Negative numbers = leave unchanged. LUTs must
331
365
  // be the panel's full waveform table (159 bytes on SSD1681-class panels);
332
366
  // hasLut=true with an empty vector restores the board's vendor default.
333
- static void setEpaperRefreshConfig(double fullRefreshEveryPartials,
334
- double fullRefreshHardCapPartials,
335
- double fastStreakWindowMs,
336
- const std::vector<std::uint8_t> &partialLut, bool hasPartialLut,
337
- const std::vector<std::uint8_t> &fastLut, bool hasFastLut);
367
+ static void setEpaperRefreshConfig(
368
+ double fullRefreshEveryPartials, double fullRefreshHardCapPartials,
369
+ double fastStreakWindowMs, const std::vector<std::uint8_t> &partialLut,
370
+ bool hasPartialLut, const std::vector<std::uint8_t> &fastLut,
371
+ bool hasFastLut);
338
372
  static void epaperFullRefresh();
339
373
  // Toggle 4-level grayscale rendering on e-paper boards that support it
340
374
  // (weak-hooked no-op elsewhere). Gray mode trades the fast partial waveform
@@ -346,7 +380,8 @@ public:
346
380
  static std::string orientation();
347
381
  static void setOrientation(const std::string &orientation);
348
382
  static std::vector<std::string> supportedOrientations();
349
- static void setSupportedOrientations(const std::vector<std::string> &orientations);
383
+ static void
384
+ setSupportedOrientations(const std::vector<std::string> &orientations);
350
385
  static void setSupportedOrientations(const std::string &orientation);
351
386
  static bool autoRotate();
352
387
  static void setAutoRotate(bool enabled);
@@ -359,17 +394,19 @@ public:
359
394
  // 4bpp targets; no-op elsewhere). 0xRRGGBB; negative disables.
360
395
  static void setTextSolidBackdrop(int rrggbb);
361
396
  // Mark the next present as a full-screen damage-all (skip the dirty-rect diff
362
- // and the persistent previous-frame copy). No-op on targets without a present diff.
397
+ // and the persistent previous-frame copy). No-op on targets without a present
398
+ // diff.
363
399
  static void invalidate();
364
400
  static std::string pixelFormat();
365
401
  static void setPixelFormat(const std::string &format);
366
402
  static std::string panelPixelFormat();
367
403
  static std::vector<std::string> supportedPixelFormats();
368
404
  static void updateAutoRotationFromAccelerometer();
369
- static void updateAutoRotation(double acceleration_x, double acceleration_y, double acceleration_z);
405
+ static void updateAutoRotation(double acceleration_x, double acceleration_y,
406
+ double acceleration_z);
370
407
  };
371
408
 
372
- } // namespace gea::framework::display
409
+ } // namespace gea::framework::display
373
410
 
374
411
  namespace gea::framework::memory {
375
412
 
@@ -398,7 +435,7 @@ public:
398
435
  static double allocationPsramPeakBytes();
399
436
  };
400
437
 
401
- } // namespace gea::framework::memory
438
+ } // namespace gea::framework::memory
402
439
 
403
440
  namespace gea::framework::input {
404
441
 
@@ -413,7 +450,7 @@ public:
413
450
  static bool consumeBackButton();
414
451
  };
415
452
 
416
- } // namespace gea::framework::input
453
+ } // namespace gea::framework::input
417
454
 
418
455
  namespace gea::framework::gpio {
419
456
 
@@ -452,4 +489,4 @@ public:
452
489
  static void detach();
453
490
  };
454
491
 
455
- } // namespace gea::framework::gpio
492
+ } // namespace gea::framework::gpio
@@ -18,8 +18,18 @@ struct BatteryFacade {
18
18
  double level() const {
19
19
  return static_cast<double>(gea::platform::power::Power::batteryPercent());
20
20
  }
21
+
22
+ bool charging() const { return gea::platform::power::Power::charging(); }
21
23
  };
22
24
 
23
25
  inline constexpr BatteryFacade Battery{};
24
26
 
27
+ struct HapticsFacade {
28
+ bool vibrate(double durationMs, double strength = 100) const {
29
+ return gea::platform::power::Power::vibrate(durationMs, static_cast<int>(strength));
30
+ }
31
+ };
32
+
33
+ inline constexpr HapticsFacade Haptics{};
34
+
25
35
  } // namespace gea::host
@@ -131,6 +131,7 @@ public:
131
131
  // Returns image id of a still capture, suitable to pass to image.draw().
132
132
  double capture() const { return gea::framework::camera::CameraBackend::capture(); }
133
133
  double captureMirrored() const { return gea::framework::camera::CameraBackend::captureMirrored(); }
134
+ std::string captureFrame() const { return gea::framework::camera::CameraBackend::captureFrame(); }
134
135
 
135
136
  // Single-arg form accepting an options record. Reads `mirror` to choose
136
137
  // the path; ignores unknown fields. Lets call sites use the same shape
@@ -0,0 +1,69 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include <algorithm>
5
+ #include <cstddef>
6
+ #include <cstdint>
7
+ #include <string>
8
+
9
+ namespace gea::host::camera_frame {
10
+
11
+ struct Size { int width = 0; int height = 0; };
12
+
13
+ // JPEG DMA works on whole 16-pixel blocks. Keep the image inside the upload
14
+ // budget while retaining the sensor aspect ratio after its mounting rotation.
15
+ inline Size uploadSize(int sourceWidth, int sourceHeight) {
16
+ if (sourceWidth <= 0 || sourceHeight <= 0) return {};
17
+ const double scale = std::min(320.0 / sourceHeight, 240.0 / sourceWidth);
18
+ return {std::max(16, int(sourceHeight * scale) / 16 * 16),
19
+ std::max(16, int(sourceWidth * scale) / 16 * 16)};
20
+ }
21
+
22
+ inline bool uyvyToRgb888Ccw90(const std::uint8_t *source, std::size_t sourceBytes,
23
+ int width, int height, std::size_t stride,
24
+ std::uint8_t *output, std::size_t outputBytes,
25
+ int outputWidth, int outputHeight, bool mirror = false) {
26
+ if (!source || !output || width <= 0 || (width & 1) || height <= 0 ||
27
+ outputWidth <= 0 || outputHeight <= 0 || outputWidth > 320 || outputHeight > 240 ||
28
+ stride < std::size_t(width) * 2 ||
29
+ sourceBytes / stride < std::size_t(height) ||
30
+ outputBytes / 3 < std::size_t(outputWidth) * outputHeight) return false;
31
+ const auto channel = [](int value) { return std::uint8_t(std::clamp(value, 0, 255)); };
32
+ for (int y = 0; y < outputHeight; ++y) {
33
+ const int sx = width - 1 - y * width / outputHeight;
34
+ for (int x = 0; x < outputWidth; ++x) {
35
+ const int sy = (mirror ? outputWidth - 1 - x : x) * height / outputWidth;
36
+ const auto *pair = source + std::size_t(sy) * stride + std::size_t(sx / 2) * 4;
37
+ const int c = std::max(0, int(pair[sx & 1 ? 3 : 1]) - 16);
38
+ const int u = int(pair[0]) - 128;
39
+ const int v = int(pair[2]) - 128;
40
+ auto *pixel = output + (std::size_t(y) * outputWidth + x) * 3;
41
+ pixel[0] = channel((298 * c + 409 * v + 128) >> 8);
42
+ pixel[1] = channel((298 * c - 100 * u - 208 * v + 128) >> 8);
43
+ pixel[2] = channel((298 * c + 516 * u + 128) >> 8);
44
+ }
45
+ }
46
+ return true;
47
+ }
48
+
49
+ inline std::string jpegDataUrl(const std::uint8_t *jpeg, std::size_t size) {
50
+ // Reject codec failure/truncation and keep every queued image bounded.
51
+ if (!jpeg || size < 4 || size > 96 * 1024 || jpeg[0] != 0xff || jpeg[1] != 0xd8 ||
52
+ jpeg[size - 2] != 0xff || jpeg[size - 1] != 0xd9) return {};
53
+ constexpr char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
54
+ std::string output = "data:image/jpeg;base64,";
55
+ const std::size_t prefix = output.size();
56
+ output.resize(prefix + (size + 2) / 3 * 4);
57
+ for (std::size_t i = 0, j = prefix; i < size; i += 3, j += 4) {
58
+ const std::uint32_t value = (std::uint32_t(jpeg[i]) << 16) |
59
+ (i + 1 < size ? std::uint32_t(jpeg[i + 1]) << 8 : 0) |
60
+ (i + 2 < size ? jpeg[i + 2] : 0);
61
+ output[j] = alphabet[value >> 18];
62
+ output[j + 1] = alphabet[(value >> 12) & 63];
63
+ output[j + 2] = i + 1 < size ? alphabet[(value >> 6) & 63] : '=';
64
+ output[j + 3] = i + 2 < size ? alphabet[value & 63] : '=';
65
+ }
66
+ return output;
67
+ }
68
+
69
+ } // namespace gea::host::camera_frame
@@ -7,6 +7,10 @@
7
7
  #include <sys/time.h>
8
8
  #endif
9
9
 
10
+ namespace gea::platform::clock {
11
+ bool setEpochMs(double timestamp);
12
+ }
13
+
10
14
  namespace gea::host {
11
15
 
12
16
  // Wall-clock access for app code.
@@ -27,6 +31,8 @@ namespace gea::host {
27
31
  // gea/embedded.h without needing a new entry in the protected
28
32
  // idf_component_register(SRCS ...) list.
29
33
  struct ClockFacade {
34
+ bool setEpochMs(double timestamp) const { return gea::platform::clock::setEpochMs(timestamp); }
35
+
30
36
  double epochMs() const {
31
37
  #if defined(_WIN32)
32
38
  return std::chrono::duration<double, std::milli>(
@@ -4,14 +4,15 @@
4
4
  #include <cstdint>
5
5
  #include <functional>
6
6
  #include <string>
7
+ #include <string_view>
7
8
  #include <unordered_map>
8
9
  #include <vector>
9
10
 
10
11
  // A lightweight device-hosted HTTP server, modeled after Node's `http` package:
11
12
  // `http.createServer(handler)` returns a server; the handler is called once per
12
13
  // request and RETURNS a plain reply record `{ status, contentType, body, file,
13
- // download }`. Like the WebSocket facade, the handler crosses the JS<->native
14
- // boundary as a boxed callable and is invoked on the FRAME task (drained by
14
+ // download }`. The compiler adapts its typed callable directly at the native
15
+ // boundary and invokes it on the FRAME task (drained by
15
16
  // `runRequests()` from the app frame loop), so app logic stays in TypeScript.
16
17
  //
17
18
  // The reply is data, never a live object: there is no `res` with mutating
@@ -23,11 +24,56 @@ namespace gea::host {
23
24
 
24
25
  using NativeHttpServerHandle = std::uint32_t;
25
26
 
27
+ struct HttpHeader {
28
+ std::string name;
29
+ std::string value;
30
+ };
31
+
32
+ // Reject malformed fields before crossing into an HTTP transport.
33
+ inline bool validHttpHeader(const HttpHeader &header) {
34
+ if (header.name.empty())
35
+ return false;
36
+ for (unsigned char ch : header.name) {
37
+ if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
38
+ !(ch >= '0' && ch <= '9') &&
39
+ std::string_view("!#$%&'*+-.^_`|~").find(ch) == std::string_view::npos)
40
+ return false;
41
+ }
42
+ return header.value.find_first_of("\r\n") == std::string::npos &&
43
+ header.value.find('\0') == std::string::npos;
44
+ }
45
+
46
+ inline std::vector<HttpHeader> parseHttpHeaders(std::string_view raw) {
47
+ std::vector<HttpHeader> headers;
48
+ std::size_t begin = 0;
49
+ while (begin < raw.size()) {
50
+ auto end = raw.find_first_of(std::string_view("\r\n\0", 3), begin);
51
+ if (end == std::string_view::npos)
52
+ end = raw.size();
53
+ const auto line = raw.substr(begin, end - begin);
54
+ const auto colon = line.find(':');
55
+ if (colon != std::string_view::npos) {
56
+ auto value = line.substr(colon + 1);
57
+ while (!value.empty() && (value.front() == ' ' || value.front() == '\t'))
58
+ value.remove_prefix(1);
59
+ while (!value.empty() && (value.back() == ' ' || value.back() == '\t'))
60
+ value.remove_suffix(1);
61
+ HttpHeader header{std::string(line.substr(0, colon)), std::string(value)};
62
+ if (validHttpHeader(header))
63
+ headers.push_back(std::move(header));
64
+ }
65
+ begin = end + 1;
66
+ }
67
+ return headers;
68
+ }
69
+
26
70
  // The request a handler sees (built natively from the incoming HTTP request).
27
71
  struct HttpRequest {
28
- std::string method; // "GET", "POST", ...
29
- std::string path; // request path, query stripped (e.g. "/note_003.wav")
30
- std::string query; // raw query string after '?' (e.g. "tag=Idea"), or ""
72
+ std::string method; // "GET", "POST", ...
73
+ std::string path; // request path, query stripped (e.g. "/note_003.wav")
74
+ std::string query; // raw query string after '?' (e.g. "tag=Idea"), or ""
75
+ std::vector<std::uint8_t> body;
76
+ std::vector<HttpHeader> headers;
31
77
  };
32
78
 
33
79
  // What a handler returns. Either an inline `body`, or a `file` path to stream
@@ -38,6 +84,7 @@ struct HttpReply {
38
84
  std::string body;
39
85
  std::string file;
40
86
  std::string download;
87
+ std::vector<HttpHeader> headers;
41
88
  };
42
89
 
43
90
  namespace http {
@@ -67,15 +114,17 @@ void platform_close(NativeHttpServerHandle handle);
67
114
  // object across module scope. Handle 0 is a no-op.
68
115
  inline void close_server(double handle) {
69
116
  const NativeHttpServerHandle h = static_cast<NativeHttpServerHandle>(handle);
70
- if (h == 0) return;
117
+ if (h == 0)
118
+ return;
71
119
  platform_close(h);
72
120
  unregister_handler(h);
73
121
  }
74
122
 
75
123
  // Test seam (non-ESP): dispatch a request synchronously through the registered
76
124
  // handler and return its reply, without any sockets.
77
- HttpReply test_dispatch(NativeHttpServerHandle handle, const std::string &method,
78
- const std::string &path, const std::string &query);
125
+ HttpReply test_dispatch(NativeHttpServerHandle handle,
126
+ const std::string &method, const std::string &path,
127
+ const std::string &query);
79
128
 
80
129
  #ifdef GEA_CPP_VALUE_AVAILABLE
81
130
  // Builds a native RequestHandler that marshals the request into a JS record,
@@ -90,13 +139,17 @@ inline NativeHttpServerHandle create_server(gea_cpp_value handler) {
90
139
  RequestHandler native = [callable](const HttpRequest &req) -> HttpReply {
91
140
  gea_cpp_value request;
92
141
  request.kind = gea_cpp_value::kind_t::record;
93
- request.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
142
+ request.entries =
143
+ std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
94
144
  request.record_set_literal("method", gea_cpp_value(req.method));
95
145
  request.record_set_literal("path", gea_cpp_value(req.path));
96
- request.record_set_literal("url", gea_cpp_value(req.query.empty() ? req.path : req.path + "?" + req.query));
146
+ request.record_set_literal(
147
+ "url", gea_cpp_value(req.query.empty() ? req.path
148
+ : req.path + "?" + req.query));
97
149
  request.record_set_literal("query", gea_cpp_value(req.query));
98
150
 
99
- const gea_cpp_value result = (*callable)(std::vector<gea_cpp_value>{request});
151
+ const gea_cpp_value result =
152
+ (*callable)(std::vector<gea_cpp_value>{request});
100
153
 
101
154
  HttpReply reply;
102
155
  if (result.kind != gea_cpp_value::kind_t::record) {
@@ -112,7 +165,8 @@ inline NativeHttpServerHandle create_server(gea_cpp_value handler) {
112
165
  reply.status = static_cast<int>(status.number);
113
166
  }
114
167
  const gea_cpp_value contentType = result.record_get_literal("contentType");
115
- if (contentType.kind == gea_cpp_value::kind_t::string && !contentType.text.empty()) {
168
+ if (contentType.kind == gea_cpp_value::kind_t::string &&
169
+ !contentType.text.empty()) {
116
170
  reply.contentType = contentType.text;
117
171
  }
118
172
  const gea_cpp_value file = result.record_get_literal("file");
@@ -120,7 +174,8 @@ inline NativeHttpServerHandle create_server(gea_cpp_value handler) {
120
174
  reply.file = file.text;
121
175
  }
122
176
  const gea_cpp_value download = result.record_get_literal("download");
123
- if (download.kind == gea_cpp_value::kind_t::string && !download.text.empty()) {
177
+ if (download.kind == gea_cpp_value::kind_t::string &&
178
+ !download.text.empty()) {
124
179
  reply.download = download.text;
125
180
  }
126
181
  const gea_cpp_value body = result.record_get_literal("body");
@@ -131,31 +186,36 @@ inline NativeHttpServerHandle create_server(gea_cpp_value handler) {
131
186
  };
132
187
  return register_handler(std::move(native));
133
188
  }
134
- #endif // GEA_CPP_VALUE_AVAILABLE
189
+ #endif // GEA_CPP_VALUE_AVAILABLE
135
190
 
136
- } // namespace http
191
+ } // namespace http
137
192
 
138
193
  // A server handle. `createServer()` lowers to `http::create_server(...)`, whose
139
194
  // double return is wrapped back into this by the plugin's nativeTypes mapping,
140
195
  // so `server.listen(...)` / `server.close()` lower to direct native calls.
141
196
  class HttpServer {
142
- public:
197
+ public:
143
198
  NativeHttpServerHandle nativeHandle = 0;
144
199
 
145
200
  constexpr HttpServer() = default;
146
201
  explicit HttpServer(NativeHttpServerHandle h) : nativeHandle(h) {}
147
- explicit HttpServer(double h) : nativeHandle(static_cast<NativeHttpServerHandle>(h)) {}
202
+ explicit HttpServer(double h)
203
+ : nativeHandle(static_cast<NativeHttpServerHandle>(h)) {}
148
204
 
149
- constexpr operator double() const { return static_cast<double>(nativeHandle); }
205
+ constexpr operator double() const {
206
+ return static_cast<double>(nativeHandle);
207
+ }
150
208
 
151
209
  // Numeric handle, so apps can persist it as a plain number and close later
152
210
  // via `http.close(handle)` without storing (and boxing) the object.
153
211
  double id() const { return static_cast<double>(nativeHandle); }
154
- bool listen(double port) const { return http::platform_listen(nativeHandle, static_cast<int>(port)); }
212
+ bool listen(double port) const {
213
+ return http::platform_listen(nativeHandle, static_cast<int>(port));
214
+ }
155
215
  void close() const {
156
216
  http::platform_close(nativeHandle);
157
217
  http::unregister_handler(nativeHandle);
158
218
  }
159
219
  };
160
220
 
161
- } // namespace gea::host
221
+ } // namespace gea::host
@@ -71,6 +71,7 @@ public:
71
71
  bool writeFile(const std::string &path, const std::vector<std::uint8_t> &bytes) const;
72
72
  // Read a whole file's raw bytes (e.g. a .pmtiles on microSD) into memory.
73
73
  // Empty vector when the file is missing or no storage is mounted.
74
+ bool removeFile(const std::string &path) const;
74
75
  std::vector<std::uint8_t> readFile(const std::string &path) const;
75
76
  // Read a .pmtiles archive flashed into the spare ota_1 partition (esptool),
76
77
  // sized from its header. Empty off-esp32 or when absent/invalid.