@geastack/host 0.1.16 → 0.1.18

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.
@@ -8,7 +8,7 @@
8
8
 
9
9
  #pragma once
10
10
 
11
- #include "display.h"
11
+ #include <display.h>
12
12
  #include "host/backends.h"
13
13
  #include "host/fetch.h"
14
14
  #include "geolocation.h"
@@ -23,13 +23,16 @@
23
23
  #include "host/memory.h"
24
24
  #include "host/input.h"
25
25
  #include "host/gpio.h"
26
+ #include "host/uart.h"
27
+ #include "host/esp_now.h"
28
+ #include "host/magnetometer.h"
26
29
  #include "host/clock.h"
27
30
  #include "host/profiler.h"
28
31
  #include "host/storage.h"
29
32
  #include "host/battery.h"
30
33
  #include "host/notify.h"
31
34
  #include "host/device_control.h"
32
- #include "input.h"
35
+ #include <input.h>
33
36
  #include "host/audio.h"
34
37
  #include "host/video.h"
35
38
  #include "host/display.h"
@@ -0,0 +1,62 @@
1
+ #pragma once
2
+ #include "host/uart.h"
3
+ #include "platform/esp_now.h"
4
+ #include <cstring>
5
+ namespace gea::host {
6
+ struct EspNowFacade {
7
+ bool open(double channel, double rate) const {
8
+ return gea::platform::espnow::EspNow::open(UartFacade::integer(channel),
9
+ UartFacade::integer(rate));
10
+ }
11
+ bool isOpen() const { return gea::platform::espnow::EspNow::isOpen(); }
12
+ bool canSend() const { return gea::platform::espnow::EspNow::canSend(); }
13
+ bool setMaxTxPower(double quarterDbm) const {
14
+ return gea::platform::espnow::EspNow::setMaxTxPower(
15
+ UartFacade::integer(quarterDbm));
16
+ }
17
+ double getMaxTxPower() const {
18
+ return gea::platform::espnow::EspNow::getMaxTxPower();
19
+ }
20
+ void close() const { gea::platform::espnow::EspNow::close(); }
21
+ template <class Address, class Key>
22
+ bool addPeer(const Address &mac, const Key &key) const {
23
+ return mac.size() == 6 && (key.size() == 0 || key.size() == 16) &&
24
+ gea::platform::espnow::EspNow::addPeer(
25
+ mac.data(), key.size() ? key.data() : nullptr);
26
+ }
27
+ template <class Address, class Bytes>
28
+ double send(const Address &mac, const Bytes &bytes) const {
29
+ return mac.size() == 6 ? gea::platform::espnow::EspNow::send(
30
+ mac.data(), bytes.data(), bytes.size())
31
+ : -1;
32
+ }
33
+ template <class Bytes> static auto *writable(Bytes &&bytes) {
34
+ (void)bytes.data();
35
+ if constexpr (requires {
36
+ bytes.buffer();
37
+ bytes.byteOffset();
38
+ })
39
+ return bytes.buffer()->data() +
40
+ static_cast<std::size_t>(bytes.byteOffset());
41
+ else
42
+ return bytes.data();
43
+ }
44
+ template <class Address, class Bytes>
45
+ double receive(Address &&source, Bytes &&bytes) const {
46
+ if (source.size() != 6 || bytes.size() < 1470)
47
+ return -1;
48
+ // Check both views before removing a datagram from the queue.
49
+ auto *dst = writable(bytes);
50
+ auto *mac = writable(source);
51
+ gea::platform::espnow::Datagram packet{};
52
+ if (!gea::platform::espnow::EspNow::isOpen())
53
+ return -1;
54
+ if (!gea::platform::espnow::EspNow::receive(packet))
55
+ return 0;
56
+ std::memcpy(mac, packet.source.data(), 6);
57
+ std::memcpy(dst, packet.bytes.data(), packet.size);
58
+ return packet.size;
59
+ }
60
+ };
61
+ inline constexpr EspNowFacade EspNow{};
62
+ } // namespace gea::host
@@ -0,0 +1,52 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "platform/magnetometer.h"
5
+ #include <cmath>
6
+ #include <cstring>
7
+ #include <limits>
8
+ #include <type_traits>
9
+
10
+ namespace gea::host {
11
+ struct MagnetometerFacade {
12
+ bool open() const { return gea::platform::sensors::Magnetometer::open(); }
13
+ void close() const { gea::platform::sensors::Magnetometer::close(); }
14
+ double count() const { return gea::platform::sensors::Magnetometer::count(); }
15
+ template <class Values> bool read(double index, Values &&values) const {
16
+ static_assert(
17
+ std::is_same_v<std::remove_cvref_t<decltype(*values.data())>, double>);
18
+ if (values.size() < 5)
19
+ return false;
20
+ // The compiler's const typed-array view still owns a mutable backing
21
+ // buffer. Preserve its detached-buffer check and subarray byte offset.
22
+ (void)values.data();
23
+ auto *destination = [&]() {
24
+ if constexpr (requires {
25
+ values.buffer();
26
+ values.byteOffset();
27
+ })
28
+ return values.buffer()->data() +
29
+ static_cast<std::size_t>(values.byteOffset());
30
+ else {
31
+ static_assert(!std::is_const_v<
32
+ std::remove_reference_t<decltype(*values.data())>>);
33
+ return reinterpret_cast<std::uint8_t *>(values.data());
34
+ }
35
+ }();
36
+ gea::platform::sensors::MagnetometerReading sample{};
37
+ const bool valid = std::isfinite(index) && index >= 0 &&
38
+ index <= std::numeric_limits<unsigned>::max() &&
39
+ std::trunc(index) == index &&
40
+ gea::platform::sensors::Magnetometer::read(
41
+ static_cast<unsigned>(index), sample);
42
+ const double result[] = {static_cast<double>(sample.x),
43
+ static_cast<double>(sample.y),
44
+ static_cast<double>(sample.z),
45
+ static_cast<double>(sample.saturatedAxes),
46
+ static_cast<double>(sample.timestampUs)};
47
+ std::memcpy(destination, result, sizeof(result));
48
+ return valid;
49
+ }
50
+ };
51
+ inline constexpr MagnetometerFacade Magnetometer{};
52
+ } // namespace gea::host
@@ -33,6 +33,10 @@ double requestAnimationFrame(AnimationFrameCallback callback);
33
33
  void runAnimationFrameCallbacks(AnimationFrameTimestamp timestampMs);
34
34
  const char *animationFrameCallbackStage();
35
35
  void resetAnimationFrameCallbacks();
36
+ // Process-terminal, idempotent, called on the application thread before native
37
+ // realm/static teardown. Permanently rejects new timers/RAF (returning 0),
38
+ // including reentrant capture destructors; never use for an app remount.
39
+ void retireScheduledCallbacks();
36
40
  void animationFramePerfStatsReset();
37
41
  AnimationFramePerfStats animationFramePerfStatsRead();
38
42
 
@@ -0,0 +1,69 @@
1
+ #pragma once
2
+
3
+ #include "platform/uart.h"
4
+ #include <cmath>
5
+ #include <limits>
6
+ #include <type_traits>
7
+
8
+ namespace gea::host {
9
+ struct UartFacade {
10
+ static int integer(double value) {
11
+ return std::isfinite(value) && value >= 0 &&
12
+ value <= std::numeric_limits<int>::max() &&
13
+ std::trunc(value) == value
14
+ ? static_cast<int>(value)
15
+ : -1;
16
+ }
17
+ bool open(double port, double tx, double rx, double baud, double rxBytes,
18
+ double txBytes) const {
19
+ return gea::platform::uart::Uart::open(
20
+ integer(port), {integer(tx), integer(rx), integer(baud),
21
+ integer(rxBytes), integer(txBytes)});
22
+ }
23
+ bool isOpen(double port) const {
24
+ return gea::platform::uart::Uart::isOpen(integer(port));
25
+ }
26
+ void close(double port) const {
27
+ gea::platform::uart::Uart::close(integer(port));
28
+ }
29
+ template <typename Bytes> double read(double port, Bytes &&bytes) const {
30
+ static_assert(std::is_same_v<std::remove_cvref_t<decltype(*bytes.data())>,
31
+ std::uint8_t>);
32
+ if constexpr (requires {
33
+ bytes.buffer();
34
+ bytes.byteOffset();
35
+ }) {
36
+ // The compiler passes a const TypedArray VIEW. Its ArrayBuffer remains
37
+ // mutable: receive into that shared backing store, respecting subarrays.
38
+ // data() preserves the runtime's detached-buffer check before accessing
39
+ // it.
40
+ (void)bytes.data();
41
+ const auto count = bytes.size();
42
+ auto *destination = count
43
+ ? bytes.buffer()->data() +
44
+ static_cast<std::size_t>(bytes.byteOffset())
45
+ : nullptr;
46
+ return gea::platform::uart::Uart::read(integer(port), destination, count);
47
+ } else {
48
+ static_assert(
49
+ !std::is_const_v<std::remove_reference_t<decltype(*bytes.data())>>);
50
+ return gea::platform::uart::Uart::read(integer(port), bytes.data(),
51
+ bytes.size());
52
+ }
53
+ }
54
+ template <typename Bytes>
55
+ double write(double port, const Bytes &bytes) const {
56
+ static_assert(std::is_same_v<std::remove_cvref_t<decltype(*bytes.data())>,
57
+ std::uint8_t>);
58
+ return gea::platform::uart::Uart::write(integer(port), bytes.data(),
59
+ bytes.size());
60
+ }
61
+ double available(double port) const {
62
+ return gea::platform::uart::Uart::available(integer(port));
63
+ }
64
+ double writable(double port) const {
65
+ return gea::platform::uart::Uart::writable(integer(port));
66
+ }
67
+ };
68
+ inline constexpr UartFacade Uart{};
69
+ } // namespace gea::host
@@ -96,6 +96,14 @@ struct CallbackTable {
96
96
  };
97
97
 
98
98
  std::unordered_map<NativeWebSocketHandle, CallbackTable> &callbackTable();
99
+ // Owner-thread callback admission; closed process realms and destroyed handles refuse.
100
+ bool callbackAdmissionOpen(NativeWebSocketHandle handle);
101
+ // Owner-thread setters refuse retired handles and release replaced captures after mutation.
102
+ void set_open_callback(NativeWebSocketHandle handle, std::function<void()> cb);
103
+ void set_message_callback(NativeWebSocketHandle handle, std::function<void(const std::string &)> cb);
104
+ void set_close_callback(NativeWebSocketHandle handle, std::function<void(int, const std::string &)> cb);
105
+ void set_error_callback(NativeWebSocketHandle handle, std::function<void(const std::string &)> cb);
106
+
99
107
 
100
108
  NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols = {},
101
109
  const std::string &headers = {});
@@ -172,40 +180,43 @@ void test_set_error_callback(NativeWebSocketHandle handle, std::function<void(co
172
180
  #ifdef GEA_CPP_VALUE_AVAILABLE
173
181
 
174
182
  inline const WebSocketOnOpenProperty &WebSocketOnOpenProperty::operator=(gea_cpp_value cb) const {
183
+ if (!websocket::callbackAdmissionOpen(handle)) return *this;
175
184
  if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
176
- websocket::callbackTable()[handle].on_open = nullptr;
185
+ websocket::set_open_callback(handle, {});
177
186
  return *this;
178
187
  }
179
188
  auto callable = cb.callable;
180
- websocket::callbackTable()[handle].on_open = [callable]() {
189
+ websocket::set_open_callback(handle, [callable]() {
181
190
  (*callable)(std::vector<gea_cpp_value>{});
182
- };
191
+ });
183
192
  return *this;
184
193
  }
185
194
 
186
195
  inline const WebSocketOnMessageProperty &WebSocketOnMessageProperty::operator=(gea_cpp_value cb) const {
196
+ if (!websocket::callbackAdmissionOpen(handle)) return *this;
187
197
  if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
188
- websocket::callbackTable()[handle].on_message = nullptr;
198
+ websocket::set_message_callback(handle, {});
189
199
  return *this;
190
200
  }
191
201
  auto callable = cb.callable;
192
- websocket::callbackTable()[handle].on_message = [callable](const std::string &data) {
202
+ websocket::set_message_callback(handle, [callable](const std::string &data) {
193
203
  gea_cpp_value event;
194
204
  event.kind = gea_cpp_value::kind_t::record;
195
205
  event.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
196
206
  event.record_set_literal("data", gea_cpp_value(data));
197
207
  (*callable)(std::vector<gea_cpp_value>{event});
198
- };
208
+ });
199
209
  return *this;
200
210
  }
201
211
 
202
212
  inline const WebSocketOnCloseProperty &WebSocketOnCloseProperty::operator=(gea_cpp_value cb) const {
213
+ if (!websocket::callbackAdmissionOpen(handle)) return *this;
203
214
  if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
204
- websocket::callbackTable()[handle].on_close = nullptr;
215
+ websocket::set_close_callback(handle, {});
205
216
  return *this;
206
217
  }
207
218
  auto callable = cb.callable;
208
- websocket::callbackTable()[handle].on_close = [callable](int code, const std::string &reason) {
219
+ websocket::set_close_callback(handle, [callable](int code, const std::string &reason) {
209
220
  gea_cpp_value event;
210
221
  event.kind = gea_cpp_value::kind_t::record;
211
222
  event.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
@@ -213,23 +224,24 @@ inline const WebSocketOnCloseProperty &WebSocketOnCloseProperty::operator=(gea_c
213
224
  event.record_set_literal("reason", gea_cpp_value(reason));
214
225
  event.record_set_literal("wasClean", gea_cpp_value(code == 1000));
215
226
  (*callable)(std::vector<gea_cpp_value>{event});
216
- };
227
+ });
217
228
  return *this;
218
229
  }
219
230
 
220
231
  inline const WebSocketOnErrorProperty &WebSocketOnErrorProperty::operator=(gea_cpp_value cb) const {
232
+ if (!websocket::callbackAdmissionOpen(handle)) return *this;
221
233
  if (cb.kind != gea_cpp_value::kind_t::callable || !cb.callable) {
222
- websocket::callbackTable()[handle].on_error = nullptr;
234
+ websocket::set_error_callback(handle, {});
223
235
  return *this;
224
236
  }
225
237
  auto callable = cb.callable;
226
- websocket::callbackTable()[handle].on_error = [callable](const std::string &message) {
238
+ websocket::set_error_callback(handle, [callable](const std::string &message) {
227
239
  gea_cpp_value event;
228
240
  event.kind = gea_cpp_value::kind_t::record;
229
241
  event.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
230
242
  event.record_set_literal("message", gea_cpp_value(message));
231
243
  (*callable)(std::vector<gea_cpp_value>{event});
232
- };
244
+ });
233
245
  return *this;
234
246
  }
235
247
 
@@ -0,0 +1,14 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+ #include "host/websocket.h"
4
+
5
+ // Transport callbacks update native state immediately and enqueue script events
6
+ // on the socket's creating worker. They never invoke script on a network
7
+ // thread.
8
+ namespace gea::host::websocket {
9
+ bool transport_open(NativeWebSocketHandle);
10
+ void transport_message(NativeWebSocketHandle, std::string);
11
+ void transport_closing(NativeWebSocketHandle);
12
+ void transport_close(NativeWebSocketHandle, int, const std::string &);
13
+ void transport_error(NativeWebSocketHandle, const std::string &);
14
+ } // namespace gea::host::websocket
@@ -2,11 +2,62 @@
2
2
  #pragma once
3
3
 
4
4
  #include <display.h>
5
+ #include <event_listener_options.h>
5
6
  #include <ui/document.h>
7
+ #include <ui/tree_events.h>
8
+ #include <string>
9
+ #include <type_traits>
10
+ #include <cstdlib>
11
+
12
+ namespace gea { template <typename Signature> struct CallableObject; }
6
13
 
7
14
  namespace gea::host {
8
15
 
9
16
  struct WindowFacade {
17
+ template <typename Result, typename Argument>
18
+ void addEventListener(const std::string &type,
19
+ const gea::CallableObject<Result(Argument)> &handler,
20
+ bool capture = false, bool once = false) const {
21
+ if (gea::embedded::ui::windowEventListenersRetiring()) return;
22
+ static_assert(std::is_constructible_v<std::decay_t<Argument>,
23
+ const gea::framework::events::PointerEvent &>,
24
+ "native window listener needs an installed event carrier");
25
+ const auto held = handler;
26
+ if (!gea::embedded::ui::addWindowEventListener(type.c_str(),
27
+ handler.functionObjectIdentity().get(),
28
+ gea::framework::events::explicitEventCallback([](auto& held, gea::framework::events::PointerEvent &event) {
29
+ held.call(static_cast<std::decay_t<Argument>>(event));
30
+ }, held), capture, once)) std::abort();
31
+ }
32
+ template <typename Result>
33
+ void addEventListener(const std::string &type,
34
+ const gea::CallableObject<Result()> &handler,
35
+ bool capture = false, bool once = false) const {
36
+ if (gea::embedded::ui::windowEventListenersRetiring()) return;
37
+ const auto held = handler;
38
+ if (!gea::embedded::ui::addWindowEventListener(type.c_str(),
39
+ handler.functionObjectIdentity().get(),
40
+ gea::framework::events::explicitEventCallback([](auto& held, gea::framework::events::PointerEvent &) { held.call(); }, held),
41
+ capture, once)) std::abort();
42
+ }
43
+ template <typename Handler>
44
+ void removeEventListener(const std::string &type, const Handler &handler,
45
+ bool capture = false) const {
46
+ gea::embedded::ui::removeWindowEventListener(type.c_str(),
47
+ handler.functionObjectIdentity().get(), capture);
48
+ }
49
+ template <typename Handler, typename Options>
50
+ requires (!std::is_same_v<std::decay_t<Options>,bool>)
51
+ void addEventListener(const std::string &type, const Handler &handler, const Options &options) const {
52
+ const auto parsed = gea::framework::events::listenerOptions(options);
53
+ addEventListener(type,handler,parsed.capture,parsed.once);
54
+ }
55
+ template <typename Handler, typename Options>
56
+ requires (!std::is_same_v<std::decay_t<Options>,bool>)
57
+ void removeEventListener(const std::string &type, const Handler &handler, const Options &options) const {
58
+ const auto parsed = gea::framework::events::listenerOptions(options);
59
+ removeEventListener(type,handler,parsed.capture);
60
+ }
10
61
  double innerWidth() const {
11
62
  #if defined(GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT) && GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT
12
63
  return static_cast<double>(gea::platform::display::kWidth);
@@ -11,6 +11,10 @@
11
11
 
12
12
  namespace gea::host::workers {
13
13
 
14
+ // Application-thread process termination only. Permanently closes the main
15
+ // Context and releases its pending captures; does not join dedicated workers.
16
+ void retireMainContext();
17
+
14
18
  // Native execution realms. Only native ownership-safe messages may cross realms;
15
19
  // generated Ref objects and callbacks remain local to their creating realm.
16
20
  class Context : public std::enable_shared_from_this<Context> {
@@ -45,6 +49,7 @@ class Context : public std::enable_shared_from_this<Context> {
45
49
  void loop(std::function<void()> entry);
46
50
  std::unique_ptr<State> state_;
47
51
  friend class ContextScope;
52
+ friend void retireMainContext();
48
53
  };
49
54
 
50
55
  class ContextScope {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/host",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "GeaStack host bindings and platform service sources.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,8 +20,8 @@
20
20
  #include <cstdio>
21
21
  #include <cstring>
22
22
 
23
- #include "display.h"
24
- #include "apps.h"
23
+ #include <display.h>
24
+ #include <apps.h>
25
25
  #include "ui/document.h"
26
26
  #include "ui/node.h"
27
27
  #include "ui/style.h"