@mikrojs/native 0.22.0 → 0.23.1-pr-515.20261121200853

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.
Files changed (46) hide show
  1. package/CMakeLists.txt +19 -0
  2. package/dist/runtime/result/native-result.node-shim.d.ts +2 -0
  3. package/dist/runtime/result/native-result.node-shim.d.ts.map +1 -1
  4. package/dist/runtime/result/native-result.node-shim.js +57 -0
  5. package/dist/runtime/result/native-result.node-shim.js.map +1 -1
  6. package/dist/runtime/result/types.d.ts +33 -0
  7. package/dist/runtime/result/types.d.ts.map +1 -1
  8. package/include/mikrojs/app_store.h +16 -0
  9. package/include/mikrojs/ble_port.h +50 -0
  10. package/include/mikrojs/build_install.h +36 -0
  11. package/include/mikrojs/device_store.h +84 -0
  12. package/include/mikrojs/http_native.h +52 -0
  13. package/include/mikrojs/mikrojs.h +19 -9
  14. package/include/mikrojs/ota.h +75 -0
  15. package/include/mikrojs/ota_env.h +1 -1
  16. package/include/mikrojs/platform.h +29 -0
  17. package/include/mikrojs/private.h +4 -1
  18. package/include/mikrojs/utils.h +5 -0
  19. package/package.json +4 -4
  20. package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
  21. package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
  22. package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
  23. package/runtime/http/server-impl.ts +5 -60
  24. package/runtime/http/server.ts +0 -1
  25. package/runtime/internal.d.ts +2 -0
  26. package/runtime/result/native-result.node-shim.ts +60 -0
  27. package/runtime/result/result.ts +1 -1
  28. package/runtime/result/types.ts +61 -0
  29. package/runtime/sys/sys.ts +1 -2
  30. package/src/mik_app_store.cpp +32 -0
  31. package/src/mik_ble.cpp +1573 -0
  32. package/src/mik_build_install.cpp +785 -0
  33. package/src/mik_config.cpp +330 -0
  34. package/src/mik_console.cpp +15 -24
  35. package/src/mik_deploy.cpp +759 -0
  36. package/src/mik_ota.cpp +1403 -0
  37. package/src/mik_ota_client_module.cpp +1142 -0
  38. package/src/mik_ota_env.cpp +318 -0
  39. package/src/mik_repl.cpp +12 -4
  40. package/src/mik_result.cpp +159 -0
  41. package/src/mik_sys.cpp +25 -51
  42. package/src/mik_test_supervisor.cpp +151 -0
  43. package/src/mik_watchdog.cpp +4 -5
  44. package/src/mik_wifi_client.cpp +1 -2
  45. package/src/mikrojs.cpp +15 -6
  46. package/src/modules.cpp +1 -1
package/CMakeLists.txt CHANGED
@@ -51,8 +51,11 @@ set(MIKROJS_CORE_SOURCES
51
51
  src/mik_console.cpp
52
52
  src/mik_sys.cpp
53
53
  src/mik_repl.cpp
54
+ src/mik_deploy.cpp
55
+ src/mik_config.cpp
54
56
  src/mik_app_config.cpp
55
57
  src/mik_app_store.cpp
58
+ src/mik_test_supervisor.cpp
56
59
  src/mik_udp.cpp
57
60
  src/mik_observable.cpp
58
61
  src/mik_ota_policy.cpp
@@ -64,6 +67,14 @@ set(MIKROJS_CORE_SOURCES
64
67
  src/mik_watchdog.cpp
65
68
  src/gpio_claim.cpp
66
69
  )
70
+ # Left out of the host library, listed by the device builds:
71
+ # - src/mik_build_install.cpp needs miniz's tinfl, which the host does not
72
+ # have. Its host test is `pn test:ota-unpack`.
73
+ # - src/mik_ota.cpp, src/mik_ota_env.cpp and src/mik_ota_client_module.cpp
74
+ # need it or the HTTP module's native path. The host tests compile them
75
+ # against stand-ins for both (test/ota_stubs.cpp).
76
+ # - src/mik_ble.cpp needs NimBLE and FreeRTOS; the device builds list it with
77
+ # their mikrojs/ble_port.h hooks.
67
78
 
68
79
  add_library(mikrojs STATIC
69
80
  ${MIKROJS_CORE_SOURCES}
@@ -246,6 +257,14 @@ if(BUILD_TESTING)
246
257
  test/watchdog_test.cpp
247
258
  test/gpio_claim_test.cpp
248
259
  test/test_runner_test.cpp
260
+ test/device_store_test.cpp
261
+ test/ota_install_test.cpp
262
+ # The OTA device files, which the library leaves out, against
263
+ # stand-ins for the build unpack and the HTTP module's native path.
264
+ test/ota_stubs.cpp
265
+ src/mik_ota.cpp
266
+ src/mik_ota_env.cpp
267
+ src/mik_ota_client_module.cpp
249
268
  )
250
269
 
251
270
  # ── Check-in wire fixtures ───────────────────────────────────────
@@ -1,3 +1,5 @@
1
1
  export declare function ok(value?: unknown): unknown;
2
+ export declare function safe(fn: (...args: unknown[]) => unknown, ...args: unknown[]): unknown;
3
+ export declare function tryCall(fn: (() => unknown) | Promise<unknown>): unknown;
2
4
  export declare function err(error: unknown): unknown;
3
5
  //# sourceMappingURL=native-result.node-shim.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"native-result.node-shim.d.ts","sourceRoot":"","sources":["../../../runtime/result/native-result.node-shim.ts"],"names":[],"mappings":"AA+BA,wBAAgB,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAK3C;AAED,wBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAK3C"}
1
+ {"version":3,"file":"native-result.node-shim.d.ts","sourceRoot":"","sources":["../../../runtime/result/native-result.node-shim.ts"],"names":[],"mappings":"AA+BA,wBAAgB,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAK3C;AAiBD,wBAAgB,IAAI,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CA+BrF;AAED,wBAAgB,OAAO,CAAC,EAAE,EAAE,CAAC,MAAM,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAUvE;AAED,wBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAK3C"}
@@ -32,6 +32,63 @@ export function ok(value) {
32
32
  r.value = value;
33
33
  return r;
34
34
  }
35
+ // atob/btoa are untyped in this tsconfig
36
+ const g = globalThis;
37
+ const safeBuiltins = [
38
+ JSON.parse,
39
+ JSON.stringify,
40
+ g.atob,
41
+ g.btoa,
42
+ decodeURI,
43
+ decodeURIComponent,
44
+ encodeURI,
45
+ encodeURIComponent,
46
+ RegExp,
47
+ String.fromCodePoint,
48
+ ];
49
+ export function safe(fn, ...args) {
50
+ if (!safeBuiltins.includes(fn)) {
51
+ throw new TypeError('safe: expected one of JSON.parse, JSON.stringify, atob, btoa, decodeURI, ' +
52
+ 'decodeURIComponent, encodeURI, encodeURIComponent, RegExp, String.fromCodePoint');
53
+ }
54
+ const name = fn === JSON.parse
55
+ ? 'JSON.parse reviver'
56
+ : fn === JSON.stringify
57
+ ? 'JSON.stringify replacer'
58
+ : '';
59
+ const callArgs = name
60
+ ? args.slice(0, 3).map((a, i) => i === 1 && typeof a === 'function'
61
+ ? function (...cbArgs) {
62
+ try {
63
+ return a.apply(this, cbArgs);
64
+ }
65
+ catch (cause) {
66
+ throw new Error(`${name} threw`, { cause });
67
+ }
68
+ }
69
+ : a)
70
+ : args;
71
+ try {
72
+ return ok(fn(...callArgs));
73
+ }
74
+ catch (e) {
75
+ return err(e);
76
+ }
77
+ }
78
+ export function tryCall(fn) {
79
+ if (fn instanceof Promise)
80
+ return fn.then(ok, err);
81
+ if (typeof fn !== 'function')
82
+ throw new TypeError('tryCall: expected a function or a promise');
83
+ let value;
84
+ try {
85
+ value = fn();
86
+ }
87
+ catch (e) {
88
+ return err(e);
89
+ }
90
+ return value instanceof Promise ? value.then(ok, err) : ok(value);
91
+ }
35
92
  export function err(error) {
36
93
  const r = Object.create(proto);
37
94
  r.ok = false;
@@ -1 +1 @@
1
- {"version":3,"file":"native-result.node-shim.js","sourceRoot":"","sources":["../../../runtime/result/native-result.node-shim.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,uEAAuE;AAEvE,MAAM,KAAK,GAAG;IACZ,GAAG,CAAwD,EAA2B;QACpF,OAAO,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAA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,MAAM,UAAU,GAAG,CAAC,KAAc;IAChC,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC,CAAC,EAAE,GAAG,KAAK,CAAA;IACZ,CAAC,CAAC,KAAK,GAAG,KAAK,CAAA;IACf,OAAO,CAAC,CAAA;AACV,CAAC"}
@@ -39,6 +39,39 @@ export type Result<T, E> = OkResult<T> | ErrResult<E>;
39
39
  export declare function ok(): OkResult<void>;
40
40
  export declare function ok<T>(value: T): OkResult<T>;
41
41
  export declare function err<E>(error: E): ErrResult<E>;
42
+ /**
43
+ * Call a builtin that throws on bad input and return the outcome as a Result:
44
+ * `ok(value)`, or `err(error)` with what it threw. Synchronous.
45
+ *
46
+ * `fn` must be one of `JSON.parse`, `JSON.stringify`, `atob`, `btoa`,
47
+ * `decodeURI`, `decodeURIComponent`, `encodeURI`, `encodeURIComponent`,
48
+ * `RegExp` or `String.fromCodePoint`. Any other function is a TypeError.
49
+ * If a reviver or replacer throws, the error is an `Error` whose `cause` is
50
+ * what it threw, so a bug in it isn't mistaken for bad input.
51
+ *
52
+ * @example
53
+ * const body = safe(JSON.parse, text)
54
+ * if (!body.ok) return err(new Error('invalid body', {cause: body.error}))
55
+ */
56
+ export declare function safe(fn: RegExpConstructor, pattern: string | RegExp, flags?: string): Result<RegExp, Error>;
57
+ export declare function safe<A extends unknown[], R>(fn: (...args: A) => R, ...args: A): Result<unknown extends R ? unknown : R, Error>;
58
+ export declare function safe(fn: typeof JSON.parse, text: string, reviver?: (this: any, key: string, value: any) => any): Result<unknown, Error>;
59
+ export declare function safe(fn: typeof JSON.stringify, value: unknown, replacer?: ((this: any, key: string, value: any) => any) | (number | string)[] | null, space?: string | number): Result<string, Error>;
60
+ /**
61
+ * Call `fn` and return the outcome as a Result: `ok(value)`, or `err(error)`
62
+ * with what it threw. For code that throws and that {@link safe} doesn't
63
+ * cover, such as a library.
64
+ *
65
+ * Given a promise, or a `fn` that returns one, it returns a promise of a
66
+ * Result that never rejects. Passing the promise directly misses a throw that
67
+ * happens before the promise exists; the callback form catches both.
68
+ *
69
+ * @example
70
+ * const doc = tryCall(() => yaml.parse(text))
71
+ * const body = await tryCall(() => lib.download(url))
72
+ */
73
+ export declare function tryCall<T>(promise: Promise<T>): Promise<Result<T, unknown>>;
74
+ export declare function tryCall<T>(fn: () => T): 0 extends 1 & T ? Result<any, unknown> : [T] extends [never] ? Result<never, unknown> : T extends Promise<infer U> ? Promise<Result<U, unknown>> : Result<T, unknown>;
42
75
  export declare function matchError<E extends {
43
76
  name: string;
44
77
  }, R>(error: E, handlers: {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../runtime/result/types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,UAAW,SAAQ,KAAK;gBAC/B,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAC;CAC1D;AAED,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAA;IACjB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;IACjB,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAA;IACtB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IACxC,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IAC/C,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAC3D,KAAK,CAAC,CAAC,EAAE,CAAC,EA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C,KAAK,EAAE,OAAO,CAAC,GACtB,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GACxB,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,GAC3B,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,CAAA;AAE1B,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,CAAC,SAAS;IAAC,IAAI,EAAE,MAAM,CAAA;CAAC,EAAE,CAAC,EAC5D,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE;KAAE,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAC,IAAI,EAAE,CAAC,CAAA;KAAC,CAAC,KAAK,CAAC;CAAC,GAChE,CAAC,CAAA;AAEJ,4DAA4D;AAC5D,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CACxB"}
@@ -7,6 +7,8 @@
7
7
  * <base>/.deploy-old rollback copy of the previous live app
8
8
  * POSIX only (rename/stat/dirent); no ESP-IDF, no QuickJS. */
9
9
 
10
+ #include <stddef.h>
11
+
10
12
  enum MIKAppCommitResult {
11
13
  MIK_APP_COMMIT_OK = 0,
12
14
  MIK_APP_COMMIT_STASH_FAILED, // rename <base>/app -> <base>/.deploy-old failed
@@ -32,3 +34,17 @@ bool mik__rmdir_recursive(const char* path);
32
34
  * left no directory behind, so a full filesystem reads as ENOSPC here instead
33
35
  * of as ENOENT from a later open. */
34
36
  bool mik__mkdirs(const char* path);
37
+
38
+ /* Join `base` with each of `count` suffixes in one malloc'd block and point
39
+ * `*slots[i]` at the i-th path. Returns the block, which the caller frees, or
40
+ * NULL when out of memory. */
41
+ char* mik__join_paths(const char* base, const char* const* suffixes, size_t count,
42
+ const char** const* slots);
43
+
44
+ /* Size of the file at `path` in bytes, or -1 when it cannot be read. */
45
+ long mik__file_size(const char* path);
46
+
47
+ /* Capacity and free bytes of the app filesystem. False when the platform can't
48
+ * report them; callers then skip their size check rather than block an install
49
+ * on a missing measurement. */
50
+ bool mik__fs_space(long* total, long* free_bytes);
@@ -0,0 +1,50 @@
1
+ /* The per-chip parts of native:mikro/ble. The module itself (src/mik_ble.cpp)
2
+ * is NimBLE host code shared by every port; each port provides these hooks
3
+ * for bringing its controller and NimBLE's port layer up and down, and for
4
+ * what the host API does not cover. */
5
+ #pragma once
6
+
7
+ #include <stdint.h>
8
+
9
+ /* MIK_BLE_LOGW(fmt, ...) and MIK_BLE_LOGE(fmt, ...), from a header the port
10
+ * has on its include path. Some call sites run on the NimBLE host task, so
11
+ * the port picks a logger that is safe there. */
12
+ #include "mik_ble_port_log.h"
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ /* Starts the controller and NimBLE's port layer (nimble_port_init), so the
19
+ * host API can register services. 0, or a MIK_ERR_BLE_* code. */
20
+ int mik__ble_port_init(void);
21
+
22
+ /* Starts the host: NimBLE runs its event loop and calls ble_hs_cfg.sync_cb
23
+ * once the controller answers. */
24
+ void mik__ble_port_start_host(void);
25
+
26
+ /* Stops the host and the controller, with advertising and connections
27
+ * already ended. The next mik__ble_port_init starts them again. 0, or a code
28
+ * that stop() reports as StackShutdown. */
29
+ int mik__ble_port_stop(void);
30
+
31
+ /* Picks and sets the address the device advertises with, from the host's
32
+ * sync callback. 0 with *own_addr_type set, or a NimBLE error code. */
33
+ int mik__ble_port_select_address(uint8_t* own_addr_type);
34
+
35
+ /* The address the device advertises with, most significant byte first,
36
+ * whether or not the stack runs. 0, or the platform's error code. */
37
+ int mik__ble_port_address(uint8_t out[6]);
38
+
39
+ /* Advertising TX power in dBm. The setter returns 0, or the platform's error
40
+ * code. */
41
+ int mik__ble_port_get_tx_power(void);
42
+ int mik__ble_port_set_tx_power(int dbm);
43
+
44
+ /* A short critical section around state the host task and the JS task share. */
45
+ void mik__ble_port_lock(void);
46
+ void mik__ble_port_unlock(void);
47
+
48
+ #ifdef __cplusplus
49
+ }
50
+ #endif
@@ -0,0 +1,36 @@
1
+ #pragma once
2
+
3
+ /* Build install (mik_build_install.cpp): unpack a build archive (a .tgz of
4
+ * `app/`) and swap it in as the live app through the app-store engine. Pure
5
+ * libc plus miniz's tinfl, which the ESP32 has in ROM; the host library has no
6
+ * miniz, so it leaves this file out. */
7
+
8
+ #include <stdbool.h>
9
+
10
+ #ifdef __cplusplus
11
+ extern "C" {
12
+ #endif
13
+
14
+ /* Why an install failed. `corrupt` means the bytes are deterministically bad
15
+ * (gzip/tar/SHA) so retrying identical bytes can't help; `transient` is
16
+ * fs/mount/io; `oom` is an allocation failure. */
17
+ typedef enum MIKBuildErr {
18
+ MIK_BUILD_ERR_CORRUPT,
19
+ MIK_BUILD_ERR_TRANSIENT,
20
+ MIK_BUILD_ERR_OOM,
21
+ } MIKBuildErr;
22
+
23
+ /* SHA-256 of a whole file as 64 lowercase hex chars + NUL. False on I/O error. */
24
+ bool mik__sha256_file(const char* path, char out[65]);
25
+
26
+ /* Streaming gunzip -> untar of `tgz` into `dest_dir`, verifying the gzip
27
+ * trailer. Allocates a 32 KB inflate window plus ~16 KB while it runs. */
28
+ bool mik__unpack_tgz(const char* tgz, const char* dest_dir, const char** err, MIKBuildErr* kind);
29
+
30
+ /* Unpack `tgz` into <fs_base>/.deploy-tmp/app and promote it to <fs_base>/app.
31
+ * An interrupted swap is rolled back by MIK_DeployRecover at the next boot. */
32
+ bool mik__install_build(const char* fs_base, const char* tgz, const char** err, MIKBuildErr* kind);
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
@@ -0,0 +1,84 @@
1
+ #ifndef MIKROJS_DEVICE_STORE_H
2
+ #define MIKROJS_DEVICE_STORE_H
3
+
4
+ #include <stdbool.h>
5
+ #include <stddef.h>
6
+ #include <stdint.h>
7
+
8
+ #include "mikrojs/mikrojs.h"
9
+ #include "mikrojs/ota_env.h"
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ /* `value` is NULL for a secret and for a value that could not be read. */
16
+ typedef void (*MIKEnvEachFn)(const char* key, const char* value, bool secret, void* ud);
17
+
18
+ /* Storage the deploy and config handlers and the OTA install core need from a
19
+ * platform: the app filesystem root, a key store for env vars and kv seeds, a
20
+ * firmware identity and the OTA install state. LittleFS + NVS on the ESP32. */
21
+ typedef struct MIKDeviceStore {
22
+ /* App filesystem root, e.g. "/appfs". Deploy paths are appended to it. */
23
+ const char* fs_base;
24
+ /* Hex identity of the running firmware, stamped into the deploy checksums
25
+ * manifest so a reflash invalidates it. NULL disables the stamp. */
26
+ const char* (*firmware_hash)(void);
27
+ /* Stage a streamed build archive for install: MIK_OtaStageAdopt on a port
28
+ * with OTA. A hook because builds without mik_ota.cpp, the host library
29
+ * among them, cannot link it. NULL: builds unsupported. */
30
+ bool (*stage_build)(const char* tgz_path, const char* checksum, const char** err);
31
+
32
+ /* Config hooks. Any may be NULL: env_each then lists no env vars, and the
33
+ * command behind each of the others is refused. */
34
+ void (*env_each)(MIKEnvEachFn fn, void* ud);
35
+ bool (*env_set)(const char* key, const char* value, bool secret, bool* out_changed);
36
+ /* Returns true if the key existed. */
37
+ bool (*env_delete)(const char* key);
38
+ /* ns is "mik.kv" or "mik.sys"; blob is one CBOR value. */
39
+ bool (*kv_set)(const char* ns, const char* key, const uint8_t* blob, size_t len);
40
+ /* 1 existed, 0 absent, -1 failed. */
41
+ int (*kv_delete)(const char* ns, const char* key);
42
+
43
+ /* OTA hooks (mik_ota.cpp, mik_ota_env.cpp). NULL state_open: no OTA.
44
+ *
45
+ * kv_get reads a blob that kv_set wrote. With `out` NULL it writes the size
46
+ * into *inout_len. Absent and failed are different answers: see
47
+ * MIKOtaKvStatus. */
48
+ MIKOtaKvStatus (*kv_get)(const char* ns, const char* key, uint8_t* out, size_t* inout_len);
49
+ /* The install state: small typed values in the "mik.ota" namespace, read
50
+ * and written between state_open and state_close. state_commit makes the
51
+ * writes durable. The ESP32 keeps them in NVS with its own types, so the
52
+ * state of a device updated from older firmware stays readable. */
53
+ void* (*state_open)(bool writable);
54
+ void (*state_close)(void* h);
55
+ bool (*state_commit)(void* h);
56
+ bool (*state_get_u8)(void* h, const char* key, uint8_t* out);
57
+ bool (*state_set_u8)(void* h, const char* key, uint8_t value);
58
+ bool (*state_get_u32)(void* h, const char* key, uint32_t* out);
59
+ bool (*state_set_u32)(void* h, const char* key, uint32_t value);
60
+ /* A NUL-terminated string that fits `cap`. False when absent, empty or
61
+ * too long. */
62
+ bool (*state_get_str)(void* h, const char* key, char* out, size_t cap);
63
+ bool (*state_set_str)(void* h, const char* key, const char* value);
64
+ void (*state_erase)(void* h, const char* key);
65
+ } MIKDeviceStore;
66
+
67
+ void MIK_SetDeviceStore(const MIKDeviceStore* store);
68
+ /* Internal to the library: the store set above, or NULL. */
69
+ const MIKDeviceStore* mik__device_store(void);
70
+
71
+ /* Protocol handlers (mik_deploy.cpp, mik_config.cpp). They read their payload
72
+ * from the transport themselves. */
73
+ bool MIK_HandleDeployCommand(MIKReplTransport* transport, uint8_t cmd_type, uint32_t payload_len);
74
+ bool MIK_HandleConfigCommand(MIKReplTransport* transport, uint8_t cmd_type, uint32_t payload_len);
75
+ void MIK_DeploySessionReset(void);
76
+
77
+ /* Finish an interrupted deploy swap. Call at boot before loading the app. */
78
+ void MIK_DeployRecover(void);
79
+
80
+ #ifdef __cplusplus
81
+ }
82
+ #endif
83
+
84
+ #endif /* MIKROJS_DEVICE_STORE_H */
@@ -0,0 +1,52 @@
1
+ /* The native request path of the platform's HTTP module (native:mikro/http).
2
+ * C code, the OTA client, shares that module's task, TLS setup and budgets
3
+ * rather than running a second HTTP client. Each port's HTTP module implements
4
+ * these; a build without networking implements them as refusals. */
5
+ #pragma once
6
+
7
+ #include <stddef.h>
8
+ #include <stdint.h>
9
+
10
+ struct JSContext;
11
+ struct MIKRuntime;
12
+
13
+ /* A C consumer of one request's messages, used when the requester is native
14
+ * code rather than JS. Set on a pending entry, it replaces the two promises
15
+ * entirely: nothing on that entry is ever handed to JS.
16
+ *
17
+ * Callbacks run from the module's loop consumer, i.e. on the JS loop thread,
18
+ * never on the HTTP background task. `done` fires exactly once and is terminal. */
19
+ struct MIKHttpNativeSink {
20
+ void (*headers)(void* user_data, int status);
21
+ void (*data)(void* user_data, const uint8_t* data, size_t len);
22
+ void (*done)(void* user_data, int status, const char* error_msg);
23
+ void* user_data;
24
+ };
25
+
26
+ /* One native request. Strings and buffers are borrowed for the duration of the
27
+ * start call only; the module copies them. */
28
+ struct MIKHttpNativeRequest {
29
+ const char* url;
30
+ const char* method;
31
+ const char* const* header_keys;
32
+ const char* const* header_values;
33
+ size_t header_count;
34
+ const uint8_t* body;
35
+ size_t body_len;
36
+ /* Whole-request bound, milliseconds; 0 for none. The socket timeout only
37
+ * bounds a single read, so a server that dribbles is otherwise unbounded
38
+ * and holds the task and its TLS session indefinitely. */
39
+ uint32_t timeout_ms;
40
+ };
41
+
42
+ /* Bring the transport up for a native consumer that never imports the JS module.
43
+ * Must be called before mik__http_start_native. Idempotent. */
44
+ void mik__http_ensure_native(struct JSContext* ctx);
45
+
46
+ /* Start a request whose messages go to `sink`. Returns the request id, or 0 when
47
+ * it could not be started. */
48
+ uint32_t mik__http_start_native(struct MIKRuntime* rt, const MIKHttpNativeRequest* req,
49
+ const MIKHttpNativeSink* sink);
50
+
51
+ /* Abandon a native request. No further sink callbacks fire for it. */
52
+ void mik__http_cancel_native(struct MIKRuntime* rt, uint32_t id);
@@ -82,6 +82,13 @@ void MIK_SetConfig(MIKRuntime* mik_rt, const MIKConfig* config);
82
82
  int MIK_LoadTests(const char* base_path, char*** out_paths, size_t* out_count);
83
83
  void MIK_FreeTests(char** paths, size_t count);
84
84
 
85
+ /* Run a test manifest through the protocol session open on `transport`: each
86
+ * file in a fresh runtime from `create`, with test helpers enabled, followed by
87
+ * MANIFEST_DONE. Serving the session afterwards is the caller's. */
88
+ struct MIKReplTransport;
89
+ void MIK_RunTestManifest(struct MIKReplTransport* transport, char** paths, size_t count,
90
+ MIKRuntime* (*create)(void* opaque), void* opaque);
91
+
85
92
  void MIK_DefaultOptions(MIKRunOptions* options);
86
93
  MIKRuntime* MIK_NewRuntime(void);
87
94
  MIKRuntime* MIK_NewRuntimeOptions(MIKRunOptions* options);
@@ -430,8 +437,8 @@ extern bool mik__proto_send_in_progress;
430
437
  void MIK_ProtocolOpen(MIKReplTransport* transport);
431
438
 
432
439
  /* Close the session. Fires the transport's session_end hook (used for
433
- * deploy state cleanup), then clears all session state. Safe to call
434
- * from any state. */
440
+ * deploy state cleanup), then clears all session state, the boot memory
441
+ * reading included. Safe to call from any state. */
435
442
  void MIK_ProtocolClose(void);
436
443
 
437
444
  /* Bind a runtime to the active session. Required before serving any
@@ -439,15 +446,18 @@ void MIK_ProtocolClose(void);
439
446
  * and must detach before freeing. */
440
447
  void MIK_ProtocolAttach(MIKRuntime* mik_rt);
441
448
 
442
- /* Record what `mik_rt` was handed at boot, reported on MSG_READY. Call once,
449
+ /* Record what `mik_rt` was handed at boot, reported on MSG_READY. Call it
443
450
  * from the boot path, at the point the app's entry is about to be evaluated:
444
451
  * the figures then describe the floor an app starts from rather than whatever
445
- * happens to be free when a client connects. Later calls are ignored, so a
446
- * supervisor that swaps runtimes can't overwrite the boot reading with a
447
- * per-test one. MIK_ProtocolAttach calls this itself, which covers embedders
448
- * that never call it; a boot path with a test supervisor should call it
449
- * explicitly, before the supervisor allocates, so both modes report the same
450
- * figure. */
452
+ * happens to be free when a client connects. The reading is kept until
453
+ * MIK_ProtocolClose and later calls before that are ignored, so a supervisor
454
+ * that swaps runtimes can't overwrite the boot reading with a per-test one.
455
+ * A port that restarts the app in place captures once per run, after the
456
+ * previous session's MIK_ProtocolClose and before the next MIK_ProtocolOpen:
457
+ * a reading taken before that close is dropped with the session.
458
+ * MIK_ProtocolAttach calls this itself, which covers embedders that never
459
+ * call it; a boot path with a test supervisor should call it explicitly,
460
+ * before the supervisor allocates, so both modes report the same figure. */
451
461
  void MIK_CaptureBootMemory(MIKRuntime* mik_rt);
452
462
 
453
463
  /* Unbind the current runtime. Call before MIK_FreeRuntime when swapping
@@ -0,0 +1,75 @@
1
+ /* Over-the-air updates of the app build (mik_ota.cpp, mik_ota_env.cpp,
2
+ * mik_ota_client_module.cpp): the platform-facing entry points. The install
3
+ * state and the app filesystem come from the device store (device_store.h),
4
+ * the network from the HTTP module's native path (http_native.h). */
5
+ #pragma once
6
+
7
+ #include <stdbool.h>
8
+ #include <stdint.h>
9
+
10
+ #include "mikrojs/mikrojs.h"
11
+ #include "mikrojs/ota_env.h"
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ /* Boot reconcile. Runs before the JS app loads, after MIK_DeployRecover():
18
+ * reflash guard, a cable-deploy (adopt) install, the trial verdict, and a
19
+ * deferred clean-heap install of a staged build. True when it installed a
20
+ * build the platform should now start fresh: the ESP32 restarts the chip, a
21
+ * port that restarts the app in place starts a new session. */
22
+ bool MIK_OtaBootReconcile(void);
23
+
24
+ /* Stage a streamed .tgz for install at the next boot with adopt semantics:
25
+ * no trial, no rollback baseline, one install attempt. Backs
26
+ * MIK_CMD_DEPLOY_BUILD through the device store's stage_build. Verifies the
27
+ * build against `checksum` (lowercase hex, may be empty to skip) so a corrupt
28
+ * upload still fails synchronously over serial, then marks it pending for the
29
+ * boot reconcile's adopt install. Returns false with *err pointing at a static
30
+ * reason string on failure. */
31
+ bool MIK_OtaStageAdopt(const char* tgz_path, const char* checksum, const char** err);
32
+
33
+ /* Outcome of the last adopt-mode boot install, for MIK_CMD_DEPLOY_RESULT.
34
+ * Kept separate from the reconcile record on purpose: that record reaches the
35
+ * registry as `lastInstall` and would blacklist a checksum a developer is
36
+ * about to legitimately re-push over the cable. */
37
+ typedef struct MIKDeployResult {
38
+ uint8_t status; /* 0 none, 1 ok, 2 fail */
39
+ char checksum[96];
40
+ char reason[32];
41
+ char detail[160];
42
+ } MIKDeployResult;
43
+
44
+ /* Handle MIK_CMD_DEPLOY_RESULT: reply with the recorded adopt-install outcome
45
+ * and clear it. Not part of MIK_HandleDeployCommand, because it is read after
46
+ * the post-deploy restart and must not start a deploy session. */
47
+ bool MIK_OtaHandleDeployResult(MIKReplTransport* transport, uint32_t payload_len);
48
+
49
+ /* Error handler (MIK_SetErrorHandler) for the app runtime. A fatal JS error
50
+ * (an uncaught exception or unhandled rejection) during a trial is recorded,
51
+ * so the next reconcile reverts the build even though the restart looks clean.
52
+ * No-op outside a trial. */
53
+ void MIK_OtaTrialErrorHandler(JSContext* ctx, JSValue error, void* opaque);
54
+
55
+ /* ── Internal to the library ─────────────────────────────────────── */
56
+
57
+ /* Read and clear the recorded adopt-install outcome. status 0 with empty
58
+ * strings when nothing is recorded. */
59
+ void mik__ota_take_deploy_result(MIKDeployResult* out);
60
+
61
+ /* True while the running build is an unconfirmed OTA trial. */
62
+ bool mik__ota_in_trial(void);
63
+
64
+ /* Fill the install-op slots of an OTA env with the staging machinery, the same
65
+ * cores the native:mikro/ota JS bindings call. */
66
+ void mik__ota_fill_install_ops(MIKOtaEnv* env);
67
+
68
+ /* The OTA environment for this runtime, fully populated (install ops, kv,
69
+ * HTTP, identity, clock). Valid until the runtime is freed. `bytecode_version`
70
+ * comes from the module init, which has a JSContext. */
71
+ const MIKOtaEnv* mik__ota_env_for(struct MIKRuntime* rt, int bytecode_version);
72
+
73
+ #ifdef __cplusplus
74
+ }
75
+ #endif
@@ -162,7 +162,7 @@ struct MIKOtaEnv {
162
162
  bool (*kv_set_i32)(void* opaque, const char* key, int32_t val);
163
163
  bool (*kv_remove)(void* opaque, const char* key);
164
164
 
165
- /* ── Install ops (firmware mik_ota.cpp) ────────────────────────────── */
165
+ /* ── Install ops (mik_ota.cpp) ────────────────────────────── */
166
166
  bool (*stage_begin)(void* opaque, const char* checksum, size_t size, size_t* out_resume_offset,
167
167
  char* err_buf, size_t err_len);
168
168
  bool (*stage_write)(void* opaque, const uint8_t* data, size_t len, char* err_buf,
@@ -9,6 +9,25 @@
9
9
  extern "C" {
10
10
  #endif
11
11
 
12
+ /* get_reset_reason() values the OTA trial verdict compares against: the
13
+ * watchdog and panic resets count as a crash of the trial build, a brownout
14
+ * as ambiguous. A platform reports these resets with these strings. */
15
+ #define MIK_RESET_PANIC "panic"
16
+ #define MIK_RESET_WATCHDOG "watchdog"
17
+ #define MIK_RESET_INT_WATCHDOG "interrupt-watchdog"
18
+ #define MIK_RESET_TASK_WATCHDOG "task-watchdog"
19
+ #define MIK_RESET_BROWNOUT "brownout"
20
+
21
+ /* What sys.board reports about the chip. `features` is NULL-terminated: "wifi",
22
+ * "ble" and "bt" only with their stack compiled in, "ieee802154" on silicon alone. */
23
+ typedef struct MIKChipInfo {
24
+ int cores;
25
+ int revision;
26
+ uint64_t flash;
27
+ uint64_t psram;
28
+ const char* const* features;
29
+ } MIKChipInfo;
30
+
12
31
  typedef struct MIKPlatform {
13
32
  int64_t (*get_boot_us)(void); /* High-res timer, resets on deep sleep */
14
33
  int64_t (*get_rtc_us)(void); /* RTC timer, survives deep sleep */
@@ -106,6 +125,16 @@ typedef struct MIKPlatform {
106
125
  * stack otherwise starts only as a side effect of the wifi or http
107
126
  * module loading, and lwip aborts on a socket call before that. */
108
127
  void (*net_init)(void);
128
+ /** What last woke the chip from sleep, as a stable lowercase string:
129
+ * "timer", "ext0", "ext1", "gpio", "touchpad", "ulp", or "undefined"
130
+ * when the boot was not a wake. NULL on platforms without deep sleep. */
131
+ const char* (*get_wakeup_cause)(void);
132
+ /** The chip the firmware runs on, as a stable lowercase id ("esp32c6",
133
+ * "rp2350"). NULL on hosts, where callers report "host". The returned
134
+ * pointer must remain valid for the lifetime of the platform. */
135
+ const char* (*get_chip_name)(void);
136
+ /** Facts for sys.board. NULL on hosts: 1 core, no flash, no features. */
137
+ void (*get_chip_info)(MIKChipInfo* info);
109
138
  } MIKPlatform;
110
139
 
111
140
  /* Log levels (matching ESP-IDF ESP_LOG_xxx values) */
@@ -153,6 +153,9 @@ struct MIKRuntime {
153
153
  * module. Created lazily on first http module init, freed in
154
154
  * MIK_FreeRuntime. */
155
155
  JSValue http_body_consumed_ctor;
156
+ /* Private-symbol atom marking error objects mik__report_uncaught already
157
+ * reported. Created with the runtime, freed after JS_FreeContext. */
158
+ JSAtom reported_atom;
156
159
  JSValue env_obj; /* Frozen object with env vars, set on import.meta.env */
157
160
  struct {
158
161
  JSValue on_data; /* JS callback for stdin data, JS_UNDEFINED when not listening */
@@ -357,7 +360,7 @@ JSModuleDef* mik__test_load(JSContext* ctx);
357
360
  * (lightSleep). One store; the interrupt handler stamps on its next poll. */
358
361
  void mik__blocking_begin(struct MIKRuntime* mik_rt);
359
362
  /* JS_SetInterruptHandler callback; opaque is the MIKRuntime*. */
360
- int mik__watchdog_interrupt(JSRuntime* rt, void* opaque);
363
+ int mik__watchdog_interrupt(JSContext* ctx, void* opaque);
361
364
  /* If the blocking deadline was hit, log the watchdog line and clear the
362
365
  * flag. Returns whether it did; callers print the error trace after. */
363
366
  bool mik__watchdog_report_blocking(struct MIKRuntime* mik_rt);
@@ -157,3 +157,8 @@ JSValue mik__result_err_tag(JSContext* ctx, const char* name);
157
157
  * ownership of `error_obj`. For variants with extra fields beyond
158
158
  * {name, message} — path, code, limit, etc. */
159
159
  JSValue mik__result_err_obj(JSContext* ctx, JSValue error_obj);
160
+ /* Throw a PanicError, for a failure with no Result to return. Use it for
161
+ * hardware failures instead of JS_ThrowInternalError, which means the engine
162
+ * itself is in a bad state and which JS code cannot catch. */
163
+ JSValue mik__throw_panic(JSContext* ctx, const char* fmt, ...)
164
+ __attribute__((format(printf, 2, 3)));