@geastack/host 0.1.13 → 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.
- package/audio_runtime.cpp +183 -98
- package/host/audio.cpp +47 -24
- package/host/audio_buffer_runtime.cpp +117 -0
- package/host/audio_worklet.cpp +29 -17
- package/host/camera.cpp +34 -1
- package/host/device.cpp +36 -0
- package/host/http.cpp +155 -56
- package/host/image.cpp +188 -97
- package/host/media.cpp +18 -6
- package/host/mjpeg.cpp +1 -1
- package/host/rtc.cpp +1 -1
- package/host/rtc_esp.cpp +2 -2
- package/host/timers.cpp +2 -2
- package/host/wifi.cpp +30 -32
- package/include/gea/audio-worklet-runtime.h +4 -3
- package/include/gea/http-native-runtime.h +127 -0
- package/include/host/audio.h +84 -13
- package/include/host/backends.h +88 -51
- package/include/host/battery.h +10 -0
- package/include/host/camera.h +1 -0
- package/include/host/camera_frame.h +69 -0
- package/include/host/clock.h +6 -0
- package/include/host/http.h +80 -20
- package/include/host/image.h +1 -0
- package/include/host/navigator.h +238 -89
- package/include/host/storage.h +9 -6
- package/include/host/timers.h +2 -5
- package/include/host/video_pixels.h +6 -0
- package/include/platform/internal_stack.h +89 -0
- package/input.cpp +57 -33
- package/package.json +1 -1
- package/wifi.cpp +140 -121
|
@@ -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
|
package/include/host/clock.h
CHANGED
|
@@ -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>(
|
package/include/host/http.h
CHANGED
|
@@ -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 }`.
|
|
14
|
-
// boundary
|
|
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;
|
|
29
|
-
std::string path;
|
|
30
|
-
std::string query;
|
|
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)
|
|
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,
|
|
78
|
-
const std::string &
|
|
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 =
|
|
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(
|
|
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 =
|
|
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 &&
|
|
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 &&
|
|
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
|
|
189
|
+
#endif // GEA_CPP_VALUE_AVAILABLE
|
|
135
190
|
|
|
136
|
-
}
|
|
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
|
-
|
|
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)
|
|
202
|
+
explicit HttpServer(double h)
|
|
203
|
+
: nativeHandle(static_cast<NativeHttpServerHandle>(h)) {}
|
|
148
204
|
|
|
149
|
-
constexpr operator double() const {
|
|
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 {
|
|
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
|
-
}
|
|
221
|
+
} // namespace gea::host
|
package/include/host/image.h
CHANGED
|
@@ -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.
|