@geastack/host 0.1.30 → 0.1.32
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/host/display.cpp +23 -0
- package/host/http.cpp +236 -32
- package/host/local_notifications_apple.mm +199 -0
- package/host/media.cpp +63 -0
- package/host/menu_bar_apple.mm +17 -20
- package/include/gea/embedded-host.h +3 -0
- package/include/host/backends.h +5 -0
- package/include/host/display.h +3 -0
- package/include/host/http.h +83 -1
- package/include/host/local_notifications.h +56 -0
- package/include/host/media.h +115 -6
- package/include/host/notification_relay.h +84 -0
- package/include/host/phone_link.h +173 -0
- package/include/platform/internal_stack.h +102 -52
- package/package.json +1 -1
|
@@ -41,6 +41,9 @@
|
|
|
41
41
|
#include "host/menu.h"
|
|
42
42
|
#include "host/menu_bar.h"
|
|
43
43
|
#include "host/notify.h"
|
|
44
|
+
#include "host/phone_link.h"
|
|
45
|
+
#include "host/local_notifications.h"
|
|
46
|
+
#include "host/notification_relay.h"
|
|
44
47
|
#include "host/device_control.h"
|
|
45
48
|
#include <input.h>
|
|
46
49
|
#include "host/audio.h"
|
package/include/host/backends.h
CHANGED
|
@@ -389,6 +389,11 @@ public:
|
|
|
389
389
|
static double brightness();
|
|
390
390
|
static void setBrightness(double brightness);
|
|
391
391
|
static void setAA(double samples);
|
|
392
|
+
// Panel power. Weak-hooked (gea_display_set_powered / gea_display_powered):
|
|
393
|
+
// a no-op that always reports awake on boards without panel power control.
|
|
394
|
+
static void sleep();
|
|
395
|
+
static void wake();
|
|
396
|
+
static bool awake();
|
|
392
397
|
static void setFlushConfig(double rows, double depth);
|
|
393
398
|
// E-paper refresh policy + custom waveforms (weak-hooked: no-op on boards
|
|
394
399
|
// without an e-paper panel). Negative numbers = leave unchanged. LUTs must
|
package/include/host/display.h
CHANGED
|
@@ -148,6 +148,9 @@ struct DisplayFacade {
|
|
|
148
148
|
|
|
149
149
|
double getBrightness() const { return gea::framework::display::DisplayBackend::brightness(); }
|
|
150
150
|
void setBrightness(double brightness) const { gea::framework::display::DisplayBackend::setBrightness(brightness); }
|
|
151
|
+
void sleep() const { gea::framework::display::DisplayBackend::sleep(); }
|
|
152
|
+
void wake() const { gea::framework::display::DisplayBackend::wake(); }
|
|
153
|
+
bool awake() const { return gea::framework::display::DisplayBackend::awake(); }
|
|
151
154
|
std::string orientation() const { return gea::framework::display::DisplayBackend::orientation(); }
|
|
152
155
|
std::string getOrientation() const { return orientation(); }
|
|
153
156
|
void setOrientation(const std::string &orientation) const { gea::framework::display::DisplayBackend::setOrientation(orientation); }
|
package/include/host/http.h
CHANGED
|
@@ -77,7 +77,9 @@ struct HttpRequest {
|
|
|
77
77
|
};
|
|
78
78
|
|
|
79
79
|
// What a handler returns. Either an inline `body`, or a `file` path to stream
|
|
80
|
-
// from disk
|
|
80
|
+
// from disk (or "asset://<bundle>/<path>" for a compiled-in bundle, see
|
|
81
|
+
// HttpEmbeddedBundle below). `download` (when set) turns the response into an
|
|
82
|
+
// attachment. `headers` apply to body, file and embedded replies alike.
|
|
81
83
|
struct HttpReply {
|
|
82
84
|
int status = 200;
|
|
83
85
|
std::string contentType = "text/plain";
|
|
@@ -87,8 +89,88 @@ struct HttpReply {
|
|
|
87
89
|
std::vector<HttpHeader> headers;
|
|
88
90
|
};
|
|
89
91
|
|
|
92
|
+
// One file of a compiled-in static bundle (a web dashboard served by the
|
|
93
|
+
// device). The bytes live in flash `.rodata` and are sent straight from there
|
|
94
|
+
// on the server worker task: nothing is copied into JS, the frame task or the
|
|
95
|
+
// heap. Tables of these are emitted by a build step (see
|
|
96
|
+
// examples/apps/notification-center/relay/web/scripts/emit-device-assets.mjs).
|
|
97
|
+
struct HttpEmbeddedAsset {
|
|
98
|
+
const char *path; // request path, e.g. "/index.html", "/assets/app-1a2b.js"
|
|
99
|
+
const unsigned char *data;
|
|
100
|
+
std::size_t size;
|
|
101
|
+
const char *contentType; // e.g. "text/javascript; charset=utf-8"
|
|
102
|
+
const char *etag; // strong validator INCLUDING quotes, or nullptr
|
|
103
|
+
bool gzip; // bytes are gzip: send `Content-Encoding: gzip`
|
|
104
|
+
bool immutable; // content-hashed name: cache for a year
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
struct HttpEmbeddedBundle {
|
|
108
|
+
const char *name; // addressed as "asset://<name>/<path>" in HttpReply.file
|
|
109
|
+
const HttpEmbeddedAsset *assets;
|
|
110
|
+
std::size_t count;
|
|
111
|
+
// Served (200) for a path that is not in the bundle and does not look like a
|
|
112
|
+
// file (its last segment has no '.'), so client-side routes reload; nullptr
|
|
113
|
+
// makes every miss a 404.
|
|
114
|
+
const char *fallback;
|
|
115
|
+
};
|
|
116
|
+
|
|
90
117
|
namespace http {
|
|
91
118
|
|
|
119
|
+
// `HttpReply.file` values starting with this scheme are served from a
|
|
120
|
+
// registered HttpEmbeddedBundle instead of the filesystem:
|
|
121
|
+
// { file: "asset://relay-dashboard" + req.path }
|
|
122
|
+
// The bundle decides Content-Type, Content-Encoding, Cache-Control and ETag,
|
|
123
|
+
// and answers a matching If-None-Match with 304.
|
|
124
|
+
inline constexpr std::string_view kEmbeddedAssetScheme = "asset://";
|
|
125
|
+
|
|
126
|
+
inline bool is_embedded_asset_file(std::string_view file) {
|
|
127
|
+
return file.substr(0, kEmbeddedAssetScheme.size()) == kEmbeddedAssetScheme;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Registers a bundle under its name (a later registration of the same name
|
|
131
|
+
// replaces the earlier one). Returns true so a generated table can register
|
|
132
|
+
// itself from a namespace-scope initializer.
|
|
133
|
+
bool register_embedded_bundle(const HttpEmbeddedBundle *bundle);
|
|
134
|
+
const HttpEmbeddedBundle *find_embedded_bundle(std::string_view name);
|
|
135
|
+
|
|
136
|
+
// The transport-independent answer for an embedded-asset reply.
|
|
137
|
+
struct EmbeddedAssetResponse {
|
|
138
|
+
int status = 404; // 200, 304 or 404
|
|
139
|
+
const HttpEmbeddedAsset *asset = nullptr; // set for 200 and 304
|
|
140
|
+
std::vector<HttpHeader> headers; // Content-Encoding, Cache-Control, ETag, Vary
|
|
141
|
+
std::string contentType = "text/plain";
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Resolves "asset://<bundle>/<path>" against the registered bundles. `path`
|
|
145
|
+
// "" or "/" maps to "/index.html". `ifNoneMatch` is the request header value
|
|
146
|
+
// ("" when absent).
|
|
147
|
+
EmbeddedAssetResponse resolve_embedded_asset(std::string_view file,
|
|
148
|
+
std::string_view ifNoneMatch);
|
|
149
|
+
|
|
150
|
+
// Case-insensitive request header lookup ("" when absent).
|
|
151
|
+
inline std::string_view find_header(const std::vector<HttpHeader> &headers,
|
|
152
|
+
std::string_view name) {
|
|
153
|
+
for (const auto &header : headers) {
|
|
154
|
+
if (header.name.size() != name.size())
|
|
155
|
+
continue;
|
|
156
|
+
bool same = true;
|
|
157
|
+
for (std::size_t i = 0; i < name.size() && same; ++i) {
|
|
158
|
+
char a = header.name[i], b = name[i];
|
|
159
|
+
if (a >= 'A' && a <= 'Z')
|
|
160
|
+
a = static_cast<char>(a - 'A' + 'a');
|
|
161
|
+
if (b >= 'A' && b <= 'Z')
|
|
162
|
+
b = static_cast<char>(b - 'A' + 'a');
|
|
163
|
+
same = a == b;
|
|
164
|
+
}
|
|
165
|
+
if (same)
|
|
166
|
+
return header.value;
|
|
167
|
+
}
|
|
168
|
+
return {};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Reason phrase for a status code ("OK", "Not Found", ...; "" if unknown).
|
|
172
|
+
const char *status_reason(int status);
|
|
173
|
+
|
|
92
174
|
// Handler signature on the native side. Runs on the frame task. Pure value
|
|
93
175
|
// types in the signature (no gea_cpp_value), so this file's non-generated TUs
|
|
94
176
|
// can store and call it without the dynamic-value runtime.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
// Notifications posted by the app itself (`LocalNotifications` in
|
|
5
|
+
// @geastack/core): UNUserNotificationCenter on iOS/macOS, NotificationManager
|
|
6
|
+
// on Android. The base driver IS the unsupported one; a platform installs its
|
|
7
|
+
// own with setLocalNotificationsDriver(). Header-only so every target links.
|
|
8
|
+
|
|
9
|
+
#include <string>
|
|
10
|
+
|
|
11
|
+
namespace gea::host {
|
|
12
|
+
|
|
13
|
+
class LocalNotificationsDriver {
|
|
14
|
+
public:
|
|
15
|
+
virtual ~LocalNotificationsDriver() = default;
|
|
16
|
+
// "granted", "denied", "undetermined" or "unsupported".
|
|
17
|
+
virtual std::string permission() { return "unsupported"; }
|
|
18
|
+
virtual void requestPermission() {}
|
|
19
|
+
// Returns the notification id, or 0 when it could not be scheduled.
|
|
20
|
+
virtual double post(const std::string &title, const std::string &body, double delaySeconds) {
|
|
21
|
+
(void)title;
|
|
22
|
+
(void)body;
|
|
23
|
+
(void)delaySeconds;
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
virtual void cancelAll() {}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
namespace local_notifications::detail {
|
|
30
|
+
inline LocalNotificationsDriver *&driverSlot() {
|
|
31
|
+
static LocalNotificationsDriver unsupported;
|
|
32
|
+
static LocalNotificationsDriver *driver = &unsupported;
|
|
33
|
+
return driver;
|
|
34
|
+
}
|
|
35
|
+
} // namespace local_notifications::detail
|
|
36
|
+
|
|
37
|
+
inline void setLocalNotificationsDriver(LocalNotificationsDriver *driver) {
|
|
38
|
+
if (driver) local_notifications::detail::driverSlot() = driver;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
inline LocalNotificationsDriver &localNotificationsDriver() {
|
|
42
|
+
return *local_notifications::detail::driverSlot();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
struct LocalNotificationsFacade {
|
|
46
|
+
std::string permission() const { return localNotificationsDriver().permission(); }
|
|
47
|
+
void requestPermission() const { localNotificationsDriver().requestPermission(); }
|
|
48
|
+
double post(const std::string &title, const std::string &body, double delaySeconds = 0) const {
|
|
49
|
+
return localNotificationsDriver().post(title, body, delaySeconds);
|
|
50
|
+
}
|
|
51
|
+
void cancelAll() const { localNotificationsDriver().cancelAll(); }
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
inline constexpr LocalNotificationsFacade LocalNotifications{};
|
|
55
|
+
|
|
56
|
+
} // namespace gea::host
|
package/include/host/media.h
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
#include <functional>
|
|
7
7
|
#include <memory>
|
|
8
8
|
#include <string>
|
|
9
|
+
#include <type_traits>
|
|
9
10
|
#include <vector>
|
|
10
11
|
#include "host/video_pixels.h"
|
|
11
12
|
|
|
@@ -79,6 +80,19 @@ struct MediaRecorderDataEvent {
|
|
|
79
80
|
GeaAudioBlob data;
|
|
80
81
|
};
|
|
81
82
|
|
|
83
|
+
// One row of `navigator.mediaDevices.enumerateDevices()`.
|
|
84
|
+
struct MediaDeviceInfo {
|
|
85
|
+
std::string deviceId;
|
|
86
|
+
std::string kind;
|
|
87
|
+
std::string label;
|
|
88
|
+
std::string groupId;
|
|
89
|
+
// Input channels the device has (0 when the platform cannot tell); a Gea extension over the browser's list.
|
|
90
|
+
double channelCount = 0;
|
|
91
|
+
|
|
92
|
+
template <typename Value>
|
|
93
|
+
static MediaDeviceInfo __gea_native_from_value(const Value &) { return {}; }
|
|
94
|
+
};
|
|
95
|
+
|
|
82
96
|
namespace media {
|
|
83
97
|
template <typename Options>
|
|
84
98
|
std::string recorder_path_from_options(const Options &options);
|
|
@@ -195,6 +209,10 @@ class TrackPcmReader {
|
|
|
195
209
|
};
|
|
196
210
|
|
|
197
211
|
NativeMediaStreamHandle get_user_media_audio();
|
|
212
|
+
// Captures from `deviceId` (an `audioinput` id from enumerate_devices; "" or "default" is the system default).
|
|
213
|
+
NativeMediaStreamHandle get_user_media_audio(const std::string &deviceId);
|
|
214
|
+
// As above, recording only input `channel` (0-based) of that device; a negative channel mixes them all.
|
|
215
|
+
NativeMediaStreamHandle get_user_media_audio(const std::string &deviceId, int channel);
|
|
198
216
|
NativeMediaRecorderHandle create_recorder(NativeMediaStreamHandle stream, const std::string &path, const std::string &mimeType);
|
|
199
217
|
std::string recorder_state(NativeMediaRecorderHandle handle);
|
|
200
218
|
std::string recorder_mime_type(NativeMediaRecorderHandle handle);
|
|
@@ -237,13 +255,104 @@ std::string recorder_mime_from_options(const Options &options) {
|
|
|
237
255
|
options, [](const auto &record) -> decltype(record.mimeType) { return record.mimeType; }, "audio/wav");
|
|
238
256
|
}
|
|
239
257
|
|
|
240
|
-
//
|
|
241
|
-
//
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
258
|
+
// The capture devices this platform has: the system default input first, as deviceId "default", then every
|
|
259
|
+
// audio input. The platform supplies the inputs (platform_input_devices); a target without one returns nothing.
|
|
260
|
+
std::vector<MediaDeviceInfo> enumerate_devices();
|
|
261
|
+
std::vector<MediaDeviceInfo> platform_input_devices();
|
|
262
|
+
|
|
263
|
+
// The input the newest capture asked for ("" is the system default). The platform capture reads it when it
|
|
264
|
+
// starts, and a request for a different device restarts a running capture on that device.
|
|
265
|
+
std::string requested_input_device();
|
|
266
|
+
// The input channel (0-based) the newest capture asked for; negative mixes every channel.
|
|
267
|
+
int requested_input_channel();
|
|
268
|
+
void platform_input_device_changed();
|
|
269
|
+
|
|
270
|
+
// `deviceId` out of a MediaStreamConstraints-shaped value from
|
|
271
|
+
// `navigator.mediaDevices.getUserMedia({...})`, whatever layers geatsc wraps it in: an optional, a record
|
|
272
|
+
// reference, the `boolean | { deviceId, ... }` union of `audio`, and ConstrainDeviceId's
|
|
273
|
+
// `string | { exact, ideal }`. Anything without one is "".
|
|
274
|
+
template <typename Value>
|
|
275
|
+
std::string constraint_device_id(const Value &value);
|
|
276
|
+
|
|
277
|
+
// A tagged union's `is<I>()` names no arm past its last one, so the walk stops at `arity`.
|
|
278
|
+
template <std::size_t Arm, typename Union>
|
|
279
|
+
std::string constraint_device_id_arm(const Union &value) {
|
|
280
|
+
if constexpr (Arm >= Union::arity) {
|
|
281
|
+
return {};
|
|
282
|
+
} else {
|
|
283
|
+
if (value.template is<Arm>()) return constraint_device_id(value.template get<Arm>());
|
|
284
|
+
return constraint_device_id_arm<Arm + 1>(value);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
template <typename Value>
|
|
289
|
+
std::string constraint_device_id(const Value &value) {
|
|
290
|
+
if constexpr (std::is_same_v<std::decay_t<Value>, bool> || std::is_arithmetic_v<std::decay_t<Value>>) {
|
|
291
|
+
return {};
|
|
292
|
+
} else if constexpr (std::is_constructible_v<std::string, const Value &>) {
|
|
293
|
+
return std::string(value);
|
|
294
|
+
} else if constexpr (requires { value.has_value(); *value; }) {
|
|
295
|
+
return value.has_value() ? constraint_device_id(*value) : std::string();
|
|
296
|
+
} else if constexpr (requires { value.get(); *value; }) {
|
|
297
|
+
return value.get() ? constraint_device_id(*value) : std::string();
|
|
298
|
+
} else if constexpr (requires { std::decay_t<Value>::arity; value.template is<0>(); }) {
|
|
299
|
+
return constraint_device_id_arm<0>(value);
|
|
300
|
+
} else if constexpr (requires { value.audio; }) {
|
|
301
|
+
return constraint_device_id(value.audio);
|
|
302
|
+
} else if constexpr (requires { value.deviceId; }) {
|
|
303
|
+
return constraint_device_id(value.deviceId);
|
|
304
|
+
} else if constexpr (requires { value.exact; value.ideal; }) {
|
|
305
|
+
const auto exact = constraint_device_id(value.exact);
|
|
306
|
+
return exact.empty() ? constraint_device_id(value.ideal) : exact;
|
|
307
|
+
} else if constexpr (requires { value.exact; }) {
|
|
308
|
+
return constraint_device_id(value.exact);
|
|
309
|
+
} else if constexpr (requires { value.ideal; }) {
|
|
310
|
+
return constraint_device_id(value.ideal);
|
|
311
|
+
} else {
|
|
312
|
+
return {};
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// The `channel` (a Gea extension: the 0-based input channel to record) out of the same constraints; -1 when
|
|
317
|
+
// absent, which mixes every channel.
|
|
318
|
+
template <typename Value>
|
|
319
|
+
int constraint_channel(const Value &value);
|
|
320
|
+
|
|
321
|
+
template <std::size_t Arm, typename Union>
|
|
322
|
+
int constraint_channel_arm(const Union &value) {
|
|
323
|
+
if constexpr (Arm >= Union::arity) {
|
|
324
|
+
return -1;
|
|
325
|
+
} else {
|
|
326
|
+
if (value.template is<Arm>()) return constraint_channel(value.template get<Arm>());
|
|
327
|
+
return constraint_channel_arm<Arm + 1>(value);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
template <typename Value>
|
|
332
|
+
int constraint_channel(const Value &value) {
|
|
333
|
+
if constexpr (std::is_same_v<std::decay_t<Value>, bool>) {
|
|
334
|
+
return -1;
|
|
335
|
+
} else if constexpr (std::is_arithmetic_v<std::decay_t<Value>>) {
|
|
336
|
+
return value >= 0 ? static_cast<int>(value) : -1;
|
|
337
|
+
} else if constexpr (requires { value.has_value(); *value; }) {
|
|
338
|
+
return value.has_value() ? constraint_channel(*value) : -1;
|
|
339
|
+
} else if constexpr (requires { value.get(); *value; }) {
|
|
340
|
+
return value.get() ? constraint_channel(*value) : -1;
|
|
341
|
+
} else if constexpr (requires { std::decay_t<Value>::arity; value.template is<0>(); }) {
|
|
342
|
+
return constraint_channel_arm<0>(value);
|
|
343
|
+
} else if constexpr (requires { value.audio; }) {
|
|
344
|
+
return constraint_channel(value.audio);
|
|
345
|
+
} else if constexpr (requires { value.channel; }) {
|
|
346
|
+
return constraint_channel(value.channel);
|
|
347
|
+
} else {
|
|
348
|
+
return -1;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
245
352
|
template <typename Constraints>
|
|
246
|
-
NativeMediaStreamHandle get_user_media_audio(const Constraints &) {
|
|
353
|
+
NativeMediaStreamHandle get_user_media_audio(const Constraints &constraints) {
|
|
354
|
+
return get_user_media_audio(constraint_device_id(constraints), constraint_channel(constraints));
|
|
355
|
+
}
|
|
247
356
|
|
|
248
357
|
} // namespace media
|
|
249
358
|
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
// Phone side of Android notification forwarding (`NotificationRelay` in
|
|
5
|
+
// @geastack/core). The forwarding itself runs in a native Android foreground
|
|
6
|
+
// service (NotificationListenerService + CompanionDeviceManager + a BLE GATT
|
|
7
|
+
// client) so it keeps working while the app is closed; this facade configures
|
|
8
|
+
// that service and reports its state. The base driver IS the unsupported one;
|
|
9
|
+
// the Android target installs its own with setNotificationRelayDriver().
|
|
10
|
+
|
|
11
|
+
#include <string>
|
|
12
|
+
|
|
13
|
+
namespace gea::host {
|
|
14
|
+
|
|
15
|
+
class NotificationRelayDriver {
|
|
16
|
+
public:
|
|
17
|
+
virtual ~NotificationRelayDriver() = default;
|
|
18
|
+
virtual bool supported() { return false; }
|
|
19
|
+
virtual bool listenerEnabled() { return false; }
|
|
20
|
+
virtual void openListenerSettings() {}
|
|
21
|
+
virtual void pair() {}
|
|
22
|
+
virtual bool paired() { return false; }
|
|
23
|
+
virtual std::string deviceName() { return ""; }
|
|
24
|
+
virtual void forget() {}
|
|
25
|
+
// "off", "connecting" or "connected".
|
|
26
|
+
virtual std::string link() { return "off"; }
|
|
27
|
+
virtual bool enabled() { return false; }
|
|
28
|
+
virtual void setEnabled(bool enabled) { (void)enabled; }
|
|
29
|
+
virtual std::string mutedApps() { return ""; }
|
|
30
|
+
virtual void setMutedApps(const std::string &packagesCsv) { (void)packagesCsv; }
|
|
31
|
+
virtual double forwardedCount() { return 0; }
|
|
32
|
+
virtual std::string lastForwarded() { return ""; }
|
|
33
|
+
virtual double appCount() { return 0; }
|
|
34
|
+
virtual std::string appIdAt(int index) {
|
|
35
|
+
(void)index;
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
virtual std::string appNameAt(int index) {
|
|
39
|
+
(void)index;
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
virtual void sendTest() {}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
namespace notification_relay::detail {
|
|
46
|
+
inline NotificationRelayDriver *&driverSlot() {
|
|
47
|
+
static NotificationRelayDriver unsupported;
|
|
48
|
+
static NotificationRelayDriver *driver = &unsupported;
|
|
49
|
+
return driver;
|
|
50
|
+
}
|
|
51
|
+
} // namespace notification_relay::detail
|
|
52
|
+
|
|
53
|
+
inline void setNotificationRelayDriver(NotificationRelayDriver *driver) {
|
|
54
|
+
if (driver) notification_relay::detail::driverSlot() = driver;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
inline NotificationRelayDriver ¬ificationRelayDriver() {
|
|
58
|
+
return *notification_relay::detail::driverSlot();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
struct NotificationRelayFacade {
|
|
62
|
+
bool supported() const { return notificationRelayDriver().supported(); }
|
|
63
|
+
bool listenerEnabled() const { return notificationRelayDriver().listenerEnabled(); }
|
|
64
|
+
void openListenerSettings() const { notificationRelayDriver().openListenerSettings(); }
|
|
65
|
+
void pair() const { notificationRelayDriver().pair(); }
|
|
66
|
+
bool paired() const { return notificationRelayDriver().paired(); }
|
|
67
|
+
std::string deviceName() const { return notificationRelayDriver().deviceName(); }
|
|
68
|
+
void forget() const { notificationRelayDriver().forget(); }
|
|
69
|
+
std::string link() const { return notificationRelayDriver().link(); }
|
|
70
|
+
bool enabled() const { return notificationRelayDriver().enabled(); }
|
|
71
|
+
void setEnabled(bool enabled) const { notificationRelayDriver().setEnabled(enabled); }
|
|
72
|
+
std::string mutedApps() const { return notificationRelayDriver().mutedApps(); }
|
|
73
|
+
void setMutedApps(const std::string &packagesCsv) const { notificationRelayDriver().setMutedApps(packagesCsv); }
|
|
74
|
+
double forwardedCount() const { return notificationRelayDriver().forwardedCount(); }
|
|
75
|
+
std::string lastForwarded() const { return notificationRelayDriver().lastForwarded(); }
|
|
76
|
+
double appCount() const { return notificationRelayDriver().appCount(); }
|
|
77
|
+
std::string appIdAt(double index) const { return notificationRelayDriver().appIdAt(static_cast<int>(index)); }
|
|
78
|
+
std::string appNameAt(double index) const { return notificationRelayDriver().appNameAt(static_cast<int>(index)); }
|
|
79
|
+
void sendTest() const { notificationRelayDriver().sendTest(); }
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
inline constexpr NotificationRelayFacade NotificationRelay{};
|
|
83
|
+
|
|
84
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
// Phone notification sync, device side (`PhoneLink` in @geastack/core).
|
|
5
|
+
//
|
|
6
|
+
// Two layers:
|
|
7
|
+
// - PhoneLinkDriver: the platform's BLE work (ANCS client for iPhone, the
|
|
8
|
+
// Relay GATT service for the Android companion). The base class IS the
|
|
9
|
+
// unsupported driver; a platform installs its own with setPhoneLinkDriver().
|
|
10
|
+
// - The event queue below, shared by every platform. A driver posts events
|
|
11
|
+
// from its BLE task with phone_link::post(); the app drains them on the
|
|
12
|
+
// frame task through PhoneLink.next() and reads the current slot.
|
|
13
|
+
//
|
|
14
|
+
// Header-only so no target's source list has to change for the surface to link.
|
|
15
|
+
|
|
16
|
+
#include <cstdint>
|
|
17
|
+
#include <deque>
|
|
18
|
+
#include <mutex>
|
|
19
|
+
#include <string>
|
|
20
|
+
|
|
21
|
+
namespace gea::host {
|
|
22
|
+
|
|
23
|
+
namespace phone_link {
|
|
24
|
+
|
|
25
|
+
enum class EventKind : std::uint8_t { Added = 0, Modified = 1, Removed = 2 };
|
|
26
|
+
|
|
27
|
+
struct Event {
|
|
28
|
+
EventKind kind = EventKind::Added;
|
|
29
|
+
std::uint32_t uid = 0;
|
|
30
|
+
std::string appId;
|
|
31
|
+
std::string appName;
|
|
32
|
+
std::string title;
|
|
33
|
+
std::string subtitle;
|
|
34
|
+
std::string message;
|
|
35
|
+
double time = 0; // epoch ms in the phone's local time, 0 = unknown
|
|
36
|
+
int category = 0; // ANCS CategoryID
|
|
37
|
+
int flags = 0; // ANCS EventFlags
|
|
38
|
+
std::string positiveLabel;
|
|
39
|
+
std::string negativeLabel;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Oldest events are dropped past this many; a phone reconnecting after a long
|
|
43
|
+
// gap replays its pre-existing notifications and must not exhaust RAM.
|
|
44
|
+
inline constexpr std::size_t kMaxQueuedEvents = 48;
|
|
45
|
+
|
|
46
|
+
namespace detail {
|
|
47
|
+
struct Queue {
|
|
48
|
+
std::mutex lock;
|
|
49
|
+
std::deque<Event> pending;
|
|
50
|
+
Event current;
|
|
51
|
+
std::uint32_t seq = 0;
|
|
52
|
+
};
|
|
53
|
+
inline Queue &queue() {
|
|
54
|
+
static Queue q;
|
|
55
|
+
return q;
|
|
56
|
+
}
|
|
57
|
+
} // namespace detail
|
|
58
|
+
|
|
59
|
+
// Thread-safe: call from the BLE host task.
|
|
60
|
+
inline void post(Event event) {
|
|
61
|
+
auto &q = detail::queue();
|
|
62
|
+
std::lock_guard<std::mutex> guard(q.lock);
|
|
63
|
+
if (q.pending.size() >= kMaxQueuedEvents) q.pending.pop_front();
|
|
64
|
+
q.pending.push_back(std::move(event));
|
|
65
|
+
q.seq++;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Drop everything queued (e.g. the phone was forgotten).
|
|
69
|
+
inline void clear() {
|
|
70
|
+
auto &q = detail::queue();
|
|
71
|
+
std::lock_guard<std::mutex> guard(q.lock);
|
|
72
|
+
q.pending.clear();
|
|
73
|
+
q.current = Event{};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
inline bool next() {
|
|
77
|
+
auto &q = detail::queue();
|
|
78
|
+
std::lock_guard<std::mutex> guard(q.lock);
|
|
79
|
+
if (q.pending.empty()) return false;
|
|
80
|
+
q.current = std::move(q.pending.front());
|
|
81
|
+
q.pending.pop_front();
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
inline double seq() {
|
|
86
|
+
auto &q = detail::queue();
|
|
87
|
+
std::lock_guard<std::mutex> guard(q.lock);
|
|
88
|
+
return static_cast<double>(q.seq);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The current slot is only written by next(), which runs on the frame task, so
|
|
92
|
+
// the getters read it without the lock.
|
|
93
|
+
inline const Event ¤t() {
|
|
94
|
+
return detail::queue().current;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
} // namespace phone_link
|
|
98
|
+
|
|
99
|
+
class PhoneLinkDriver {
|
|
100
|
+
public:
|
|
101
|
+
virtual ~PhoneLinkDriver() = default;
|
|
102
|
+
virtual void start(const std::string &name) { (void)name; }
|
|
103
|
+
virtual void stop() {}
|
|
104
|
+
// "unsupported", "off", "advertising", "pairing", "connected" or "ready".
|
|
105
|
+
virtual std::string state() { return "unsupported"; }
|
|
106
|
+
// "ios", "android" or "".
|
|
107
|
+
virtual std::string platform() { return ""; }
|
|
108
|
+
virtual std::string peerName() { return ""; }
|
|
109
|
+
virtual std::string passkey() { return ""; }
|
|
110
|
+
virtual void forget() {}
|
|
111
|
+
virtual double phoneTime() { return 0; }
|
|
112
|
+
virtual void performAction(std::uint32_t uid, bool positive) {
|
|
113
|
+
(void)uid;
|
|
114
|
+
(void)positive;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
namespace phone_link::detail {
|
|
119
|
+
inline PhoneLinkDriver *&driverSlot() {
|
|
120
|
+
static PhoneLinkDriver unsupported;
|
|
121
|
+
static PhoneLinkDriver *driver = &unsupported;
|
|
122
|
+
return driver;
|
|
123
|
+
}
|
|
124
|
+
} // namespace phone_link::detail
|
|
125
|
+
|
|
126
|
+
inline void setPhoneLinkDriver(PhoneLinkDriver *driver) {
|
|
127
|
+
if (driver) phone_link::detail::driverSlot() = driver;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
inline PhoneLinkDriver &phoneLinkDriver() {
|
|
131
|
+
return *phone_link::detail::driverSlot();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
struct PhoneLinkFacade {
|
|
135
|
+
void start(const std::string &name) const { phoneLinkDriver().start(name); }
|
|
136
|
+
void stop() const { phoneLinkDriver().stop(); }
|
|
137
|
+
std::string state() const { return phoneLinkDriver().state(); }
|
|
138
|
+
std::string platform() const { return phoneLinkDriver().platform(); }
|
|
139
|
+
std::string peerName() const { return phoneLinkDriver().peerName(); }
|
|
140
|
+
std::string passkey() const { return phoneLinkDriver().passkey(); }
|
|
141
|
+
void forget() const {
|
|
142
|
+
phoneLinkDriver().forget();
|
|
143
|
+
phone_link::clear();
|
|
144
|
+
}
|
|
145
|
+
double phoneTime() const { return phoneLinkDriver().phoneTime(); }
|
|
146
|
+
double seq() const { return phone_link::seq(); }
|
|
147
|
+
bool next() const { return phone_link::next(); }
|
|
148
|
+
std::string kind() const {
|
|
149
|
+
switch (phone_link::current().kind) {
|
|
150
|
+
case phone_link::EventKind::Modified: return "modified";
|
|
151
|
+
case phone_link::EventKind::Removed: return "removed";
|
|
152
|
+
default: return "added";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
double uid() const { return static_cast<double>(phone_link::current().uid); }
|
|
156
|
+
std::string appId() const { return phone_link::current().appId; }
|
|
157
|
+
std::string appName() const { return phone_link::current().appName; }
|
|
158
|
+
std::string title() const { return phone_link::current().title; }
|
|
159
|
+
std::string subtitle() const { return phone_link::current().subtitle; }
|
|
160
|
+
std::string message() const { return phone_link::current().message; }
|
|
161
|
+
double time() const { return phone_link::current().time; }
|
|
162
|
+
double category() const { return static_cast<double>(phone_link::current().category); }
|
|
163
|
+
double flags() const { return static_cast<double>(phone_link::current().flags); }
|
|
164
|
+
std::string positiveLabel() const { return phone_link::current().positiveLabel; }
|
|
165
|
+
std::string negativeLabel() const { return phone_link::current().negativeLabel; }
|
|
166
|
+
void performAction(double uid, bool positive) const {
|
|
167
|
+
phoneLinkDriver().performAction(static_cast<std::uint32_t>(uid), positive);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
inline constexpr PhoneLinkFacade PhoneLink{};
|
|
172
|
+
|
|
173
|
+
} // namespace gea::host
|