@geastack/host 0.1.14 → 0.1.15

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.
@@ -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.