@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.
- package/LICENSE +202 -0
- package/README.md +25 -0
- package/audio_runtime.cpp +807 -0
- package/bluetooth.cpp +125 -0
- package/geolocation.cpp +43 -0
- package/host/apps.cpp +20 -0
- package/host/audio.cpp +240 -0
- package/host/ble.cpp +239 -0
- package/host/camera.cpp +193 -0
- package/host/display.cpp +230 -0
- package/host/fetch.cpp +1060 -0
- package/host/geolocation.cpp +31 -0
- package/host/gpio.cpp +55 -0
- package/host/http.cpp +349 -0
- package/host/image.cpp +335 -0
- package/host/imu.cpp +51 -0
- package/host/input.cpp +12 -0
- package/host/media.cpp +579 -0
- package/host/memory.cpp +91 -0
- package/host/rtc.cpp +212 -0
- package/host/rtc_esp.cpp +291 -0
- package/host/tile_loader.cpp +478 -0
- package/host/timers.cpp +327 -0
- package/host/touch.cpp +25 -0
- package/host/websocket.cpp +279 -0
- package/host/wifi.cpp +99 -0
- package/include/gea/embedded-host.h +38 -0
- package/include/host/apps.h +15 -0
- package/include/host/audio.h +158 -0
- package/include/host/backends.h +455 -0
- package/include/host/battery.h +25 -0
- package/include/host/camera.h +236 -0
- package/include/host/clock.h +45 -0
- package/include/host/device_control.h +23 -0
- package/include/host/display.h +266 -0
- package/include/host/display_orientation.h +291 -0
- package/include/host/fetch.h +310 -0
- package/include/host/gpio.h +36 -0
- package/include/host/http.h +161 -0
- package/include/host/image.h +144 -0
- package/include/host/input.h +14 -0
- package/include/host/media.h +165 -0
- package/include/host/memory.h +44 -0
- package/include/host/navigator.h +283 -0
- package/include/host/notify.h +41 -0
- package/include/host/profiler.h +49 -0
- package/include/host/rtc.h +174 -0
- package/include/host/storage.h +141 -0
- package/include/host/tile_loader.h +59 -0
- package/include/host/timers.h +41 -0
- package/include/host/touch.h +20 -0
- package/include/host/websocket.h +179 -0
- package/include/host/window.h +31 -0
- package/input.cpp +68 -0
- package/package.json +27 -0
- package/services/app_state.cpp +19 -0
- package/services/battery_service.cpp +19 -0
- package/services/bluetooth_service.cpp +37 -0
- package/services/diagnostics.cpp +233 -0
- package/services/diagnostics_connection.cpp +231 -0
- package/services/diagnostics_internal.h +15 -0
- package/services/diagnostics_server.cpp +135 -0
- package/services/mirror.cpp +364 -0
- package/services/network_services.cpp +62 -0
- package/services/runtime_log.cpp +23 -0
- package/services/settings_surface.h +221 -0
- package/wifi.cpp +185 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include "backends.h"
|
|
5
|
+
|
|
6
|
+
#include <string>
|
|
7
|
+
|
|
8
|
+
namespace gea::host {
|
|
9
|
+
|
|
10
|
+
using GeolocationCoordinates = gea::framework::geolocation::GeolocationCoordinates;
|
|
11
|
+
using GeolocationPosition = gea::framework::geolocation::GeolocationPosition;
|
|
12
|
+
using GeolocationPositionError = gea::framework::geolocation::GeolocationPositionError;
|
|
13
|
+
using GeolocationOptions = gea::framework::geolocation::GeolocationOptions;
|
|
14
|
+
|
|
15
|
+
class AccelerometerValueProperty {
|
|
16
|
+
public:
|
|
17
|
+
using Getter = double (*)();
|
|
18
|
+
|
|
19
|
+
constexpr AccelerometerValueProperty() = default;
|
|
20
|
+
explicit constexpr AccelerometerValueProperty(Getter getter) : getter_(getter) {}
|
|
21
|
+
|
|
22
|
+
double operator()() const {
|
|
23
|
+
return getter_ ? getter_() : 0.0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
operator double() const {
|
|
27
|
+
return (*this)();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private:
|
|
31
|
+
Getter getter_ = nullptr;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
class AccelerometerFacade {
|
|
35
|
+
public:
|
|
36
|
+
constexpr AccelerometerFacade()
|
|
37
|
+
: tiltX(gea::framework::sensors::AccelerometerBackend::tiltX),
|
|
38
|
+
tiltY(gea::framework::sensors::AccelerometerBackend::tiltY),
|
|
39
|
+
x(gea::framework::sensors::AccelerometerBackend::accelerationX),
|
|
40
|
+
y(gea::framework::sensors::AccelerometerBackend::accelerationY),
|
|
41
|
+
z(gea::framework::sensors::AccelerometerBackend::accelerationZ),
|
|
42
|
+
accelerationX(gea::framework::sensors::AccelerometerBackend::accelerationX),
|
|
43
|
+
accelerationY(gea::framework::sensors::AccelerometerBackend::accelerationY),
|
|
44
|
+
accelerationZ(gea::framework::sensors::AccelerometerBackend::accelerationZ),
|
|
45
|
+
gyroscopeX(gea::framework::sensors::AccelerometerBackend::gyroscopeX),
|
|
46
|
+
gyroscopeY(gea::framework::sensors::AccelerometerBackend::gyroscopeY),
|
|
47
|
+
gyroscopeZ(gea::framework::sensors::AccelerometerBackend::gyroscopeZ) {}
|
|
48
|
+
|
|
49
|
+
void start() const { gea::framework::sensors::AccelerometerBackend::init(); }
|
|
50
|
+
void init() const { gea::framework::sensors::AccelerometerBackend::init(); }
|
|
51
|
+
void close() const { gea::framework::sensors::AccelerometerBackend::close(); }
|
|
52
|
+
void calibrateBias() const { gea::framework::sensors::AccelerometerBackend::calibrateBias(); }
|
|
53
|
+
|
|
54
|
+
AccelerometerValueProperty tiltX;
|
|
55
|
+
AccelerometerValueProperty tiltY;
|
|
56
|
+
AccelerometerValueProperty x;
|
|
57
|
+
AccelerometerValueProperty y;
|
|
58
|
+
AccelerometerValueProperty z;
|
|
59
|
+
AccelerometerValueProperty accelerationX;
|
|
60
|
+
AccelerometerValueProperty accelerationY;
|
|
61
|
+
AccelerometerValueProperty accelerationZ;
|
|
62
|
+
AccelerometerValueProperty gyroscopeX;
|
|
63
|
+
AccelerometerValueProperty gyroscopeY;
|
|
64
|
+
AccelerometerValueProperty gyroscopeZ;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
using IMU = AccelerometerFacade;
|
|
68
|
+
|
|
69
|
+
inline constexpr AccelerometerFacade Accelerometer{};
|
|
70
|
+
inline constexpr const AccelerometerFacade &imu = Accelerometer;
|
|
71
|
+
|
|
72
|
+
class WiFiNetwork {
|
|
73
|
+
public:
|
|
74
|
+
explicit WiFiNetwork(double index) : index_(index) {}
|
|
75
|
+
std::string ssid() const { return gea::framework::network::WifiBackend::scanSsidAt(index_); }
|
|
76
|
+
double rssi() const { return gea::framework::network::WifiBackend::scanRssiAt(index_); }
|
|
77
|
+
bool secured() const { return gea::framework::network::WifiBackend::scanSecuredAt(index_); }
|
|
78
|
+
|
|
79
|
+
private:
|
|
80
|
+
double index_ = 0.0;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
class WiFi {
|
|
84
|
+
public:
|
|
85
|
+
const WiFi &operator()() const { return *this; }
|
|
86
|
+
bool enabled() const { return gea::framework::network::WifiBackend::enabled(); }
|
|
87
|
+
void setEnabled(bool enabled) const { gea::framework::network::WifiBackend::setEnabled(enabled); }
|
|
88
|
+
bool connected() const { return gea::framework::network::WifiBackend::connected(); }
|
|
89
|
+
std::string ssid() const { return gea::framework::network::WifiBackend::ssid(); }
|
|
90
|
+
std::string ip() const { return gea::framework::network::WifiBackend::ip(); }
|
|
91
|
+
std::string mac() const { return gea::framework::network::WifiBackend::mac(); }
|
|
92
|
+
double rssi() const { return gea::framework::network::WifiBackend::rssi(); }
|
|
93
|
+
void configure(const std::string &ssid, const std::string &password) const { gea::framework::network::WifiBackend::configure(ssid, password); }
|
|
94
|
+
bool waitForConnection(double timeoutMs) const { return gea::framework::network::WifiBackend::waitForConnection(timeoutMs); }
|
|
95
|
+
void startScan() const { gea::framework::network::WifiBackend::startScan(); }
|
|
96
|
+
bool scanning() const { return gea::framework::network::WifiBackend::scanning(); }
|
|
97
|
+
double scanCount() const { return gea::framework::network::WifiBackend::scanCount(); }
|
|
98
|
+
WiFiNetwork network(double index) const { return WiFiNetwork(index); }
|
|
99
|
+
std::string scanSsidAt(double index) const { return network(index).ssid(); }
|
|
100
|
+
double scanRssiAt(double index) const { return network(index).rssi(); }
|
|
101
|
+
bool scanSecuredAt(double index) const { return network(index).secured(); }
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
class BluetoothKeyboard {
|
|
105
|
+
public:
|
|
106
|
+
void tap(double hid_code) const { gea::framework::bluetooth::HidBackend::keyTap(hid_code); }
|
|
107
|
+
void down(double modifier, double hid_code) const { gea::framework::bluetooth::HidBackend::keyDown(modifier, hid_code); }
|
|
108
|
+
void up() const { gea::framework::bluetooth::HidBackend::keyUp(); }
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
class BluetoothMouse {
|
|
112
|
+
public:
|
|
113
|
+
void move(double dx, double dy, double buttons = 0.0, double wheel = 0.0) const {
|
|
114
|
+
gea::framework::bluetooth::HidBackend::mouseMove(dx, dy, buttons, wheel);
|
|
115
|
+
}
|
|
116
|
+
void click(double button) const { gea::framework::bluetooth::HidBackend::mouseClick(button); }
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
class BluetoothMidi {
|
|
120
|
+
public:
|
|
121
|
+
// BLE-MIDI (MIDI over GATT). enable() registers the MIDI service with the
|
|
122
|
+
// driver (idempotent; call before startAdvertising so the service UUID is
|
|
123
|
+
// advertised). send() transmits one already-framed BLE-MIDI packet — the
|
|
124
|
+
// TS side owns the header/timestamp framing.
|
|
125
|
+
void enable() const { gea::framework::bluetooth::HidBackend::midiEnable(); }
|
|
126
|
+
bool bound() const { return gea::framework::bluetooth::HidBackend::midiBound(); }
|
|
127
|
+
void send(const std::vector<double> &bytes) const { gea::framework::bluetooth::HidBackend::midiSend(bytes); }
|
|
128
|
+
// Central role: scan for BLE-MIDI peripherals and connect as a GATT client.
|
|
129
|
+
// Scan results use the WiFi-scan polling idiom (startScan / scanCount / *At).
|
|
130
|
+
void startScan() const { gea::framework::bluetooth::HidBackend::midiStartScan(); }
|
|
131
|
+
void stopScan() const { gea::framework::bluetooth::HidBackend::midiStopScan(); }
|
|
132
|
+
bool scanning() const { return gea::framework::bluetooth::HidBackend::midiScanning(); }
|
|
133
|
+
double scanCount() const { return gea::framework::bluetooth::HidBackend::midiScanCount(); }
|
|
134
|
+
std::string scanNameAt(double index) const { return gea::framework::bluetooth::HidBackend::midiScanNameAt(index); }
|
|
135
|
+
void connect(double index) const { gea::framework::bluetooth::HidBackend::midiConnect(index); }
|
|
136
|
+
void disconnect() const { gea::framework::bluetooth::HidBackend::midiDisconnect(); }
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
class BluetoothHidHost {
|
|
140
|
+
public:
|
|
141
|
+
// HID host role: this device is the central and a remote HID peripheral
|
|
142
|
+
// (keyboard/macro pad) is the input source. Scanning follows the same
|
|
143
|
+
// polling idiom as MIDI; raw input reports queue driver-side until the
|
|
144
|
+
// app drains them (reportCount / reportIdAt / reportLenAt / reportByteAt
|
|
145
|
+
// / clearReports) and decodes the peripheral's report format itself.
|
|
146
|
+
void startScan() const { gea::framework::bluetooth::HidBackend::hidHostStartScan(); }
|
|
147
|
+
void stopScan() const { gea::framework::bluetooth::HidBackend::hidHostStopScan(); }
|
|
148
|
+
bool scanning() const { return gea::framework::bluetooth::HidBackend::hidHostScanning(); }
|
|
149
|
+
double scanCount() const { return gea::framework::bluetooth::HidBackend::hidHostScanCount(); }
|
|
150
|
+
std::string scanNameAt(double index) const { return gea::framework::bluetooth::HidBackend::hidHostScanNameAt(index); }
|
|
151
|
+
void connect(double index) const { gea::framework::bluetooth::HidBackend::hidHostConnect(index); }
|
|
152
|
+
void disconnect() const { gea::framework::bluetooth::HidBackend::hidHostDisconnect(); }
|
|
153
|
+
bool bound() const { return gea::framework::bluetooth::HidBackend::hidHostBound(); }
|
|
154
|
+
double reportCount() const { return gea::framework::bluetooth::HidBackend::hidHostReportCount(); }
|
|
155
|
+
double reportIdAt(double index) const { return gea::framework::bluetooth::HidBackend::hidHostReportIdAt(index); }
|
|
156
|
+
double reportLenAt(double index) const { return gea::framework::bluetooth::HidBackend::hidHostReportLenAt(index); }
|
|
157
|
+
double reportByteAt(double index, double byteIndex) const {
|
|
158
|
+
return gea::framework::bluetooth::HidBackend::hidHostReportByteAt(index, byteIndex);
|
|
159
|
+
}
|
|
160
|
+
void clearReports() const { gea::framework::bluetooth::HidBackend::hidHostClearReports(); }
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
class BluetoothConnections {
|
|
164
|
+
public:
|
|
165
|
+
// Live connection registry across every concurrent role. kind: 0 = HID
|
|
166
|
+
// central peer (desktop using us as keyboard/mouse), 1 = MIDI central
|
|
167
|
+
// peer (DAW), 2 = HID peripheral we host (macro pad), 3 = BLE-MIDI
|
|
168
|
+
// peripheral we drive (pedal/WIDI adapter).
|
|
169
|
+
double count() const { return gea::framework::bluetooth::HidBackend::connectionCount(); }
|
|
170
|
+
double kindAt(double index) const { return gea::framework::bluetooth::HidBackend::connectionKindAt(index); }
|
|
171
|
+
std::string nameAt(double index) const { return gea::framework::bluetooth::HidBackend::connectionNameAt(index); }
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
class BluetoothConfig {
|
|
175
|
+
public:
|
|
176
|
+
// Config service (custom GATT). A Web Bluetooth portal reads the app's
|
|
177
|
+
// config blob and, once paired with the 4-digit code shown on-device,
|
|
178
|
+
// writes a new one. setDocument() publishes the blob the portal reads
|
|
179
|
+
// back (native std::vector<double>, like BluetoothMidi::send). The app
|
|
180
|
+
// polls pendingLength() each frame; on a committed inbound blob it drains
|
|
181
|
+
// pendingByteAt()/consumePending() and re-serializes via setDocument().
|
|
182
|
+
// The pairing trio drives the on-device pairing overlay.
|
|
183
|
+
void setDocument(const std::vector<double> &bytes) const { gea::framework::bluetooth::HidBackend::configSetDocument(bytes); }
|
|
184
|
+
double pendingLength() const { return gea::framework::bluetooth::HidBackend::configPendingLength(); }
|
|
185
|
+
double pendingByteAt(double index) const { return gea::framework::bluetooth::HidBackend::configPendingByteAt(index); }
|
|
186
|
+
void consumePending() const { gea::framework::bluetooth::HidBackend::configConsumePending(); }
|
|
187
|
+
bool pairing() const { return gea::framework::bluetooth::HidBackend::configPairing(); }
|
|
188
|
+
std::string pairCode() const { return gea::framework::bluetooth::HidBackend::configPairCode(); }
|
|
189
|
+
void dismissPairing() const { gea::framework::bluetooth::HidBackend::configDismissPairing(); }
|
|
190
|
+
// Device -> portal activity push (see BluetoothConfig::pushActivity in the TS API).
|
|
191
|
+
void pushActivity(double index) const { gea::framework::bluetooth::HidBackend::configPushActivity(index); }
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
class Bluetooth {
|
|
195
|
+
public:
|
|
196
|
+
BluetoothKeyboard keyboard;
|
|
197
|
+
BluetoothMouse mouse;
|
|
198
|
+
BluetoothMidi midi;
|
|
199
|
+
BluetoothHidHost hidHost;
|
|
200
|
+
BluetoothConnections connections;
|
|
201
|
+
BluetoothConfig config;
|
|
202
|
+
|
|
203
|
+
const Bluetooth &operator()() const { return *this; }
|
|
204
|
+
bool enabled() const { return gea::framework::bluetooth::HidBackend::enabled(); }
|
|
205
|
+
void setEnabled(bool enabled) const { gea::framework::bluetooth::HidBackend::setEnabled(enabled); }
|
|
206
|
+
void init(const std::string &device_name, double appearance = 0.0, const std::string &mac_address = std::string()) const {
|
|
207
|
+
gea::framework::bluetooth::HidBackend::init(device_name, appearance, mac_address);
|
|
208
|
+
}
|
|
209
|
+
void startAdvertising() const { gea::framework::bluetooth::HidBackend::startAdvertising(); }
|
|
210
|
+
void stopAdvertising() const { gea::framework::bluetooth::HidBackend::stopAdvertising(); }
|
|
211
|
+
bool connected() const { return gea::framework::bluetooth::HidBackend::connected(); }
|
|
212
|
+
bool bound() const { return gea::framework::bluetooth::HidBackend::bound(); }
|
|
213
|
+
double batteryLevel() const { return gea::framework::bluetooth::HidBackend::batteryLevel(); }
|
|
214
|
+
std::string mac() const { return gea::framework::bluetooth::HidBackend::mac(); }
|
|
215
|
+
std::string deviceName() const { return gea::framework::bluetooth::HidBackend::deviceName(); }
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
class Geolocation {
|
|
219
|
+
public:
|
|
220
|
+
const Geolocation &operator()() const { return *this; }
|
|
221
|
+
|
|
222
|
+
bool hasFix() const { return gea::framework::geolocation::GeolocationBackend::hasFix(); }
|
|
223
|
+
GeolocationPosition currentPosition() const { return gea::framework::geolocation::GeolocationBackend::currentPosition(); }
|
|
224
|
+
GeolocationCoordinates coords() const { return currentPosition().coords; }
|
|
225
|
+
double latitude() const { return gea::framework::geolocation::GeolocationBackend::latitude(); }
|
|
226
|
+
double longitude() const { return gea::framework::geolocation::GeolocationBackend::longitude(); }
|
|
227
|
+
double accuracy() const { return gea::framework::geolocation::GeolocationBackend::accuracy(); }
|
|
228
|
+
|
|
229
|
+
template <typename Success>
|
|
230
|
+
void getCurrentPosition(Success success) const {
|
|
231
|
+
auto position = currentPosition();
|
|
232
|
+
if (position.hasFix) success(position);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
template <typename Success, typename Error>
|
|
236
|
+
void getCurrentPosition(Success success, Error error) const {
|
|
237
|
+
auto position = currentPosition();
|
|
238
|
+
if (position.hasFix) success(position);
|
|
239
|
+
else error(GeolocationPositionError{2.0, "Position unavailable"});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
template <typename Success, typename Error, typename Options>
|
|
243
|
+
void getCurrentPosition(Success success, Error error, const Options &options) const {
|
|
244
|
+
(void)options;
|
|
245
|
+
getCurrentPosition(success, error);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
template <typename Success>
|
|
249
|
+
double watchPosition(Success success) const {
|
|
250
|
+
getCurrentPosition(success);
|
|
251
|
+
return 1.0;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
template <typename Success, typename Error>
|
|
255
|
+
double watchPosition(Success success, Error error) const {
|
|
256
|
+
getCurrentPosition(success, error);
|
|
257
|
+
return 1.0;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
template <typename Success, typename Error, typename Options>
|
|
261
|
+
double watchPosition(Success success, Error error, const Options &options) const {
|
|
262
|
+
(void)options;
|
|
263
|
+
getCurrentPosition(success, error);
|
|
264
|
+
return 1.0;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
void clearWatch(double id) const { (void)id; }
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
class Navigator {
|
|
271
|
+
public:
|
|
272
|
+
const char *userAgent = "gea-embedded";
|
|
273
|
+
const char *language = "en-US";
|
|
274
|
+
const char *platform = "embedded";
|
|
275
|
+
bool onLine = true;
|
|
276
|
+
WiFi wifi;
|
|
277
|
+
Bluetooth bluetooth;
|
|
278
|
+
Geolocation geolocation;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
inline constexpr Navigator navigator{};
|
|
282
|
+
|
|
283
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <string>
|
|
6
|
+
|
|
7
|
+
namespace gea::host {
|
|
8
|
+
|
|
9
|
+
namespace notify_detail {
|
|
10
|
+
// One shared instance across all TUs (inline function-local statics are
|
|
11
|
+
// ODR-merged). The notification is transient (RAM, not persisted).
|
|
12
|
+
inline std::string &buffer() {
|
|
13
|
+
static std::string s;
|
|
14
|
+
return s;
|
|
15
|
+
}
|
|
16
|
+
inline std::uint32_t &counter() {
|
|
17
|
+
static std::uint32_t n = 0;
|
|
18
|
+
return n;
|
|
19
|
+
}
|
|
20
|
+
} // namespace notify_detail
|
|
21
|
+
|
|
22
|
+
// Post a transient notification from platform/device code (e.g. the GEADEV
|
|
23
|
+
// NOTIFY command, driven by the gea Companion). Apps observe Notify.seq()
|
|
24
|
+
// changing and read Notify.text() to show a banner.
|
|
25
|
+
inline void postNotification(const std::string &text) {
|
|
26
|
+
notify_detail::buffer() = text;
|
|
27
|
+
notify_detail::counter()++;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Transient host->app notification channel for app code. text() is the latest
|
|
31
|
+
// message; seq() increments on each post, so an app can detect a NEW one
|
|
32
|
+
// (rather than re-showing the same text). Reads are plain RAM (no flash), so
|
|
33
|
+
// they are safe from any task.
|
|
34
|
+
struct NotifyFacade {
|
|
35
|
+
std::string text() const { return notify_detail::buffer(); }
|
|
36
|
+
double seq() const { return static_cast<double>(notify_detail::counter()); }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
inline constexpr NotifyFacade Notify{};
|
|
40
|
+
|
|
41
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#ifdef ESP_PLATFORM
|
|
5
|
+
#include "esp_cpu.h"
|
|
6
|
+
#include "esp_timer.h"
|
|
7
|
+
#else
|
|
8
|
+
#include <chrono>
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
namespace gea::host {
|
|
12
|
+
|
|
13
|
+
struct ProfilerFacade {
|
|
14
|
+
double nowUs() const {
|
|
15
|
+
#ifdef ESP_PLATFORM
|
|
16
|
+
return static_cast<double>(esp_timer_get_time());
|
|
17
|
+
#else
|
|
18
|
+
using Clock = std::chrono::steady_clock;
|
|
19
|
+
return static_cast<double>(
|
|
20
|
+
std::chrono::duration_cast<std::chrono::microseconds>(
|
|
21
|
+
Clock::now().time_since_epoch()).count());
|
|
22
|
+
#endif
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Raw CPU cycle counter. On Xtensa this is a single CCOUNT register read,
|
|
26
|
+
// where nowUs() is an esp_timer_get_time() FUNCTION CALL — which forces
|
|
27
|
+
// values live across it and spills registers. Instrumenting a hot leaf
|
|
28
|
+
// function with nowUs() therefore measures the probe as much as the code:
|
|
29
|
+
// bubble-grid's computeFisheye read 106us/call with inner nowUs() probes,
|
|
30
|
+
// and four separate optimisations aimed at that number changed nothing.
|
|
31
|
+
// Use this for any in-function split; keep nowUs() for wall-clock spans.
|
|
32
|
+
//
|
|
33
|
+
// CCOUNT is 32-bit and wraps roughly every 18s at 240MHz, so only DIFFERENCES
|
|
34
|
+
// over short intervals are meaningful; a span crossing a wrap reads negative.
|
|
35
|
+
double nowCycles() const {
|
|
36
|
+
#ifdef ESP_PLATFORM
|
|
37
|
+
return static_cast<double>(esp_cpu_get_cycle_count());
|
|
38
|
+
#else
|
|
39
|
+
using Clock = std::chrono::steady_clock;
|
|
40
|
+
return static_cast<double>(
|
|
41
|
+
std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
42
|
+
Clock::now().time_since_epoch()).count());
|
|
43
|
+
#endif
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
inline constexpr ProfilerFacade Profiler{};
|
|
48
|
+
|
|
49
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include "host/media.h"
|
|
5
|
+
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <functional>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <unordered_map>
|
|
12
|
+
#include <utility>
|
|
13
|
+
#include <vector>
|
|
14
|
+
|
|
15
|
+
namespace gea::host {
|
|
16
|
+
|
|
17
|
+
using NativeRtcPeerHandle = std::uint32_t;
|
|
18
|
+
|
|
19
|
+
class RTCPeerConnectionOnIceCandidateProperty {
|
|
20
|
+
public:
|
|
21
|
+
NativeRtcPeerHandle handle = 0;
|
|
22
|
+
constexpr RTCPeerConnectionOnIceCandidateProperty() = default;
|
|
23
|
+
explicit constexpr RTCPeerConnectionOnIceCandidateProperty(NativeRtcPeerHandle h) : handle(h) {}
|
|
24
|
+
#ifdef GEA_CPP_VALUE_AVAILABLE
|
|
25
|
+
const RTCPeerConnectionOnIceCandidateProperty &operator=(gea_cpp_value callback) const;
|
|
26
|
+
#endif
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
class RTCPeerConnectionOnTrackProperty {
|
|
30
|
+
public:
|
|
31
|
+
NativeRtcPeerHandle handle = 0;
|
|
32
|
+
constexpr RTCPeerConnectionOnTrackProperty() = default;
|
|
33
|
+
explicit constexpr RTCPeerConnectionOnTrackProperty(NativeRtcPeerHandle h) : handle(h) {}
|
|
34
|
+
#ifdef GEA_CPP_VALUE_AVAILABLE
|
|
35
|
+
const RTCPeerConnectionOnTrackProperty &operator=(gea_cpp_value callback) const;
|
|
36
|
+
#endif
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
class RTCPeerConnectionOnConnectionStateProperty {
|
|
40
|
+
public:
|
|
41
|
+
NativeRtcPeerHandle handle = 0;
|
|
42
|
+
constexpr RTCPeerConnectionOnConnectionStateProperty() = default;
|
|
43
|
+
explicit constexpr RTCPeerConnectionOnConnectionStateProperty(NativeRtcPeerHandle h) : handle(h) {}
|
|
44
|
+
#ifdef GEA_CPP_VALUE_AVAILABLE
|
|
45
|
+
const RTCPeerConnectionOnConnectionStateProperty &operator=(gea_cpp_value callback) const;
|
|
46
|
+
#endif
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
class RTCPeerConnection {
|
|
50
|
+
public:
|
|
51
|
+
NativeRtcPeerHandle nativeHandle = 0;
|
|
52
|
+
mutable RTCPeerConnectionOnIceCandidateProperty onicecandidate;
|
|
53
|
+
mutable RTCPeerConnectionOnTrackProperty ontrack;
|
|
54
|
+
mutable RTCPeerConnectionOnConnectionStateProperty onconnectionstatechange;
|
|
55
|
+
mutable RTCPeerConnectionOnConnectionStateProperty oniceconnectionstatechange;
|
|
56
|
+
|
|
57
|
+
constexpr RTCPeerConnection() = default;
|
|
58
|
+
explicit RTCPeerConnection(NativeRtcPeerHandle h)
|
|
59
|
+
: nativeHandle(h),
|
|
60
|
+
onicecandidate(h),
|
|
61
|
+
ontrack(h),
|
|
62
|
+
onconnectionstatechange(h),
|
|
63
|
+
oniceconnectionstatechange(h) {}
|
|
64
|
+
explicit RTCPeerConnection(double h)
|
|
65
|
+
: nativeHandle(static_cast<NativeRtcPeerHandle>(h)),
|
|
66
|
+
onicecandidate(nativeHandle),
|
|
67
|
+
ontrack(nativeHandle),
|
|
68
|
+
onconnectionstatechange(nativeHandle),
|
|
69
|
+
oniceconnectionstatechange(nativeHandle) {}
|
|
70
|
+
constexpr operator double() const { return static_cast<double>(nativeHandle); }
|
|
71
|
+
|
|
72
|
+
std::string connectionState() const;
|
|
73
|
+
std::string iceConnectionState() const;
|
|
74
|
+
void addTrack(MediaStreamTrack track, MediaStream stream) const;
|
|
75
|
+
std::string createOffer() const;
|
|
76
|
+
std::string createAnswer() const;
|
|
77
|
+
void setLocalDescription(const std::string &sdp_type, const std::string &sdp) const;
|
|
78
|
+
void setRemoteDescription(const std::string &sdp_type, const std::string &sdp) const;
|
|
79
|
+
void addIceCandidate(const std::string &candidate, const std::string &sdp_mid, double sdp_mline_index) const;
|
|
80
|
+
void close() const;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
namespace rtc {
|
|
84
|
+
|
|
85
|
+
struct CallbackTable {
|
|
86
|
+
std::function<void(const std::string &, const std::string &, int)> on_ice_candidate;
|
|
87
|
+
std::function<void(NativeMediaTrackHandle)> on_track;
|
|
88
|
+
std::function<void()> on_connection_state_change;
|
|
89
|
+
std::function<void()> on_ice_connection_state_change;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
std::unordered_map<NativeRtcPeerHandle, CallbackTable> &callbackTable();
|
|
93
|
+
|
|
94
|
+
NativeRtcPeerHandle create_handle();
|
|
95
|
+
NativeRtcPeerHandle create_handle_with_config(const std::string &ice_servers_json);
|
|
96
|
+
|
|
97
|
+
// Overload accepting an RTCConfiguration-shaped record (from
|
|
98
|
+
// `new RTCPeerConnection({iceServers: [...], ...})`). The geatsc-emitted
|
|
99
|
+
// call site passes a struct; for now we ignore the contents and fall back
|
|
100
|
+
// to the no-arg form. A later step can read iceServers off the record and
|
|
101
|
+
// route to create_handle_with_config.
|
|
102
|
+
template <typename Config>
|
|
103
|
+
NativeRtcPeerHandle create_handle(const Config &) { return create_handle(); }
|
|
104
|
+
void destroy_handle(NativeRtcPeerHandle handle);
|
|
105
|
+
|
|
106
|
+
void runCallbacks();
|
|
107
|
+
|
|
108
|
+
// Producer hooks called from platform code (libpeer callbacks on ESP, tests on host).
|
|
109
|
+
void enqueue_ice_candidate(NativeRtcPeerHandle h, const std::string &candidate, const std::string &sdp_mid, int idx);
|
|
110
|
+
void enqueue_track(NativeRtcPeerHandle h, NativeMediaTrackHandle track);
|
|
111
|
+
void enqueue_connection_state_change(NativeRtcPeerHandle h, const std::string &new_state);
|
|
112
|
+
void enqueue_ice_connection_state_change(NativeRtcPeerHandle h, const std::string &new_state);
|
|
113
|
+
|
|
114
|
+
} // namespace rtc
|
|
115
|
+
|
|
116
|
+
#ifdef GEA_CPP_VALUE_AVAILABLE
|
|
117
|
+
|
|
118
|
+
inline const RTCPeerConnectionOnIceCandidateProperty &RTCPeerConnectionOnIceCandidateProperty::operator=(gea_cpp_value cb) const {
|
|
119
|
+
if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
|
|
120
|
+
rtc::callbackTable()[handle].on_ice_candidate = nullptr;
|
|
121
|
+
return *this;
|
|
122
|
+
}
|
|
123
|
+
auto callable = cb.callable;
|
|
124
|
+
rtc::callbackTable()[handle].on_ice_candidate = [callable](const std::string &candidate, const std::string &sdpMid, int sdpMLineIndex) {
|
|
125
|
+
gea_cpp_value event;
|
|
126
|
+
event.kind = gea_cpp_value::kind_t::record;
|
|
127
|
+
event.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
|
|
128
|
+
if (candidate.empty()) {
|
|
129
|
+
event.record_set_literal("candidate", gea_cpp_value::missing());
|
|
130
|
+
} else {
|
|
131
|
+
gea_cpp_value inner;
|
|
132
|
+
inner.kind = gea_cpp_value::kind_t::record;
|
|
133
|
+
inner.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
|
|
134
|
+
inner.record_set_literal("candidate", gea_cpp_value(candidate));
|
|
135
|
+
inner.record_set_literal("sdpMid", gea_cpp_value(sdpMid));
|
|
136
|
+
inner.record_set_literal("sdpMLineIndex", gea_cpp_value(static_cast<double>(sdpMLineIndex)));
|
|
137
|
+
event.record_set_literal("candidate", inner);
|
|
138
|
+
}
|
|
139
|
+
(*callable)(std::vector<gea_cpp_value>{event});
|
|
140
|
+
};
|
|
141
|
+
return *this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
inline const RTCPeerConnectionOnTrackProperty &RTCPeerConnectionOnTrackProperty::operator=(gea_cpp_value cb) const {
|
|
145
|
+
if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
|
|
146
|
+
rtc::callbackTable()[handle].on_track = nullptr;
|
|
147
|
+
return *this;
|
|
148
|
+
}
|
|
149
|
+
auto callable = cb.callable;
|
|
150
|
+
rtc::callbackTable()[handle].on_track = [callable](NativeMediaTrackHandle trackHandle) {
|
|
151
|
+
gea_cpp_value event;
|
|
152
|
+
event.kind = gea_cpp_value::kind_t::record;
|
|
153
|
+
event.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
|
|
154
|
+
event.record_set_literal("track", gea_cpp_value(static_cast<double>(trackHandle)));
|
|
155
|
+
(*callable)(std::vector<gea_cpp_value>{event});
|
|
156
|
+
};
|
|
157
|
+
return *this;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
inline const RTCPeerConnectionOnConnectionStateProperty &RTCPeerConnectionOnConnectionStateProperty::operator=(gea_cpp_value cb) const {
|
|
161
|
+
if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
|
|
162
|
+
rtc::callbackTable()[handle].on_connection_state_change = nullptr;
|
|
163
|
+
return *this;
|
|
164
|
+
}
|
|
165
|
+
auto callable = cb.callable;
|
|
166
|
+
rtc::callbackTable()[handle].on_connection_state_change = [callable]() {
|
|
167
|
+
(*callable)(std::vector<gea_cpp_value>{});
|
|
168
|
+
};
|
|
169
|
+
return *this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#endif // GEA_CPP_VALUE_AVAILABLE
|
|
173
|
+
|
|
174
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include "services/storage_service.h"
|
|
5
|
+
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
#include <cstring>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <utility>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace gea::host {
|
|
13
|
+
|
|
14
|
+
// Browser-compatible `localStorage` for app code: a synchronous, in-RAM
|
|
15
|
+
// key/value store that PERSISTS across reboots / power cycles. It mirrors the
|
|
16
|
+
// DOM Storage API exactly — getItem / setItem / removeItem / clear / key(index)
|
|
17
|
+
// / length — and is reached through the global `localStorage`, with no import.
|
|
18
|
+
//
|
|
19
|
+
// WHY IT IS SAFE TO CALL FROM ANYWHERE (the old "frame task only" caveat is
|
|
20
|
+
// gone): the full key/value set lives in `entries_` in RAM, so every getItem /
|
|
21
|
+
// setItem / removeItem / clear / key / length is a pure in-memory operation —
|
|
22
|
+
// safe from ANY task, at any time (a store constructor, init(), tick(), or an
|
|
23
|
+
// event handler). A mutation only flips a dirty flag; the actual flash write is
|
|
24
|
+
// deferred to flushPending(), which the runtime calls once per frame from the
|
|
25
|
+
// main frame task (gea_main, whose stack is in internal RAM). SPI flash — which
|
|
26
|
+
// momentarily disables the flash cache and would fault a PSRAM-stack task — is
|
|
27
|
+
// therefore only ever touched from that one safe task. load() likewise runs
|
|
28
|
+
// once at boot from the frame task.
|
|
29
|
+
//
|
|
30
|
+
// Persistence is delegated to gea::framework::services::StorageService, which
|
|
31
|
+
// stores the whole set as one opaque blob (an NVS blob on esp32, RAM elsewhere),
|
|
32
|
+
// so this header stays target-agnostic and link-light: it is header-only and its
|
|
33
|
+
// methods are instantiated only where an app actually uses localStorage.
|
|
34
|
+
struct StorageFacade {
|
|
35
|
+
// Number of stored keys, exposed as the `localStorage.length` property. Kept
|
|
36
|
+
// in sync on every mutation (read directly as a member, no accessor call).
|
|
37
|
+
double length = 0;
|
|
38
|
+
|
|
39
|
+
std::string getItem(const std::string &key) const {
|
|
40
|
+
for (const auto &entry : entries_)
|
|
41
|
+
if (entry.first == key) return entry.second;
|
|
42
|
+
return std::string();
|
|
43
|
+
}
|
|
44
|
+
void setItem(const std::string &key, const std::string &value) {
|
|
45
|
+
for (auto &entry : entries_)
|
|
46
|
+
if (entry.first == key) {
|
|
47
|
+
if (entry.second != value) {
|
|
48
|
+
entry.second = value;
|
|
49
|
+
dirty_ = true;
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
entries_.emplace_back(key, value);
|
|
54
|
+
length = static_cast<double>(entries_.size());
|
|
55
|
+
dirty_ = true;
|
|
56
|
+
}
|
|
57
|
+
void removeItem(const std::string &key) {
|
|
58
|
+
for (auto it = entries_.begin(); it != entries_.end(); ++it)
|
|
59
|
+
if (it->first == key) {
|
|
60
|
+
entries_.erase(it);
|
|
61
|
+
length = static_cast<double>(entries_.size());
|
|
62
|
+
dirty_ = true;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
void clear() {
|
|
67
|
+
if (entries_.empty()) return;
|
|
68
|
+
entries_.clear();
|
|
69
|
+
length = 0;
|
|
70
|
+
dirty_ = true;
|
|
71
|
+
}
|
|
72
|
+
std::string key(double index) const {
|
|
73
|
+
const auto raw = static_cast<std::size_t>(index);
|
|
74
|
+
return raw < entries_.size() ? entries_[raw].first : std::string();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// --- runtime plumbing (not part of the app-facing localStorage API) ------
|
|
78
|
+
// Restore the persisted set. Called once at boot from the frame task.
|
|
79
|
+
void load() {
|
|
80
|
+
std::string blob;
|
|
81
|
+
gea::framework::services::StorageService::loadKv(blob);
|
|
82
|
+
deserialize(blob);
|
|
83
|
+
dirty_ = false;
|
|
84
|
+
}
|
|
85
|
+
// Persist if a mutation happened since the last flush. Called once per frame
|
|
86
|
+
// from the main frame task — the only context that is allowed to touch flash.
|
|
87
|
+
void flushPending() {
|
|
88
|
+
if (!dirty_) return;
|
|
89
|
+
gea::framework::services::StorageService::saveKv(serialize());
|
|
90
|
+
dirty_ = false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private:
|
|
94
|
+
std::vector<std::pair<std::string, std::string>> entries_;
|
|
95
|
+
bool dirty_ = false;
|
|
96
|
+
|
|
97
|
+
// Blob layout: each entry is two length-prefixed chunks (4-byte host-order
|
|
98
|
+
// length + bytes), key then value. Binary-safe: keys/values may contain NUL.
|
|
99
|
+
// Same device serializes and deserializes, so host byte order round-trips.
|
|
100
|
+
std::string serialize() const {
|
|
101
|
+
std::string out;
|
|
102
|
+
for (const auto &entry : entries_) {
|
|
103
|
+
appendChunk(out, entry.first);
|
|
104
|
+
appendChunk(out, entry.second);
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
void deserialize(const std::string &blob) {
|
|
109
|
+
entries_.clear();
|
|
110
|
+
std::size_t pos = 0;
|
|
111
|
+
while (true) {
|
|
112
|
+
std::string k;
|
|
113
|
+
std::string v;
|
|
114
|
+
if (!readChunk(blob, pos, k)) break;
|
|
115
|
+
if (!readChunk(blob, pos, v)) break;
|
|
116
|
+
entries_.emplace_back(std::move(k), std::move(v));
|
|
117
|
+
}
|
|
118
|
+
length = static_cast<double>(entries_.size());
|
|
119
|
+
}
|
|
120
|
+
static void appendChunk(std::string &out, const std::string &chunk) {
|
|
121
|
+
const std::uint32_t len = static_cast<std::uint32_t>(chunk.size());
|
|
122
|
+
char header[sizeof(len)];
|
|
123
|
+
std::memcpy(header, &len, sizeof(len));
|
|
124
|
+
out.append(header, sizeof(header));
|
|
125
|
+
out.append(chunk);
|
|
126
|
+
}
|
|
127
|
+
static bool readChunk(const std::string &blob, std::size_t &pos, std::string &out) {
|
|
128
|
+
if (pos + sizeof(std::uint32_t) > blob.size()) return false;
|
|
129
|
+
std::uint32_t len = 0;
|
|
130
|
+
std::memcpy(&len, blob.data() + pos, sizeof(len));
|
|
131
|
+
pos += sizeof(len);
|
|
132
|
+
if (pos + len > blob.size()) return false;
|
|
133
|
+
out.assign(blob.data() + pos, len);
|
|
134
|
+
pos += len;
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
inline StorageFacade Storage;
|
|
140
|
+
|
|
141
|
+
} // namespace gea::host
|