@mikrojs/native 0.18.3-next.20260829153835 → 0.19.0

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.
@@ -1,191 +0,0 @@
1
- import {lazyEvent} from 'mikro/observable/lazy'
2
- import {err, ok} from 'mikro/result'
3
- import {Wifi as NativeWifi} from 'native:mikro/wifi'
4
-
5
- import type {Result} from '../result/types.js'
6
- import type {
7
- ApStartOptions,
8
- ApStationInfo,
9
- IpConfig,
10
- PowerSaveMode,
11
- ScanOptions,
12
- ScanResult,
13
- StaticIpConfig,
14
- Wifi,
15
- WifiAp,
16
- WifiConnectionInfo,
17
- WifiConnectOptions,
18
- WifiCountryCode,
19
- WifiDisconnectOptions,
20
- WifiDisconnectReason,
21
- WifiError,
22
- WifiStatus,
23
- } from './types.js'
24
-
25
- const WifiStatusCodes: Record<WifiStatus, number> = {
26
- STOPPED: 254,
27
- IDLE: 0,
28
- NO_SSID_AVAIL: 1,
29
- SCAN_COMPLETED: 2,
30
- CONNECTED: 3,
31
- CONNECT_FAILED: 4,
32
- CONNECTION_LOST: 5,
33
- DISCONNECTED: 6,
34
- }
35
-
36
- const StatusFromCode = new Map<number, WifiStatus>(
37
- Object.entries(WifiStatusCodes).map(([k, v]) => [v, k as WifiStatus]),
38
- )
39
-
40
- const native = new NativeWifi()
41
-
42
- const ap: WifiAp = {
43
- start(options: ApStartOptions): Result<void, WifiError> {
44
- return native.apStart(options as Parameters<typeof native.apStart>[0])
45
- },
46
-
47
- stop(): Result<void, WifiError> {
48
- return native.apStop()
49
- },
50
-
51
- get isActive(): boolean {
52
- return native.apIsActive()
53
- },
54
-
55
- get ip(): string | undefined {
56
- return native.apIp()
57
- },
58
-
59
- get stations(): ApStationInfo[] {
60
- return native.apStations()
61
- },
62
-
63
- deauthStation(mac: string): Result<void, WifiError> {
64
- return native.apDeauthStation(mac)
65
- },
66
-
67
- get inactiveTimeout(): number {
68
- const result = native.apGetInactiveTimeout()
69
- return result.ok ? result.value : 0
70
- },
71
-
72
- set inactiveTimeout(seconds: number) {
73
- native.apSetInactiveTimeout(seconds)
74
- },
75
-
76
- onStationConnect: lazyEvent<ApStationInfo>(native, 'station-connect'),
77
- onStationDisconnect: lazyEvent<ApStationInfo>(native, 'station-disconnect'),
78
- }
79
-
80
- const MAX_CONNECT_RETRIES = 5
81
- const RETRY_DELAY_MS = 2000
82
-
83
- const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
84
-
85
- const wifi: Wifi = {
86
- async connect(options: WifiConnectOptions): Promise<Result<WifiConnectionInfo, WifiError>> {
87
- const {ssid, passphrase, txPower} = options
88
- if (txPower !== undefined) {
89
- const txResult = native.setTxPower(txPower)
90
- if (!txResult.ok) return txResult
91
- }
92
- for (let attempt = 0; attempt <= MAX_CONNECT_RETRIES; attempt++) {
93
- const startResult = native.connect(ssid, passphrase)
94
- if (!startResult.ok) return startResult
95
-
96
- const asyncResult = await startResult.value
97
- if (asyncResult.ok) return ok(asyncResult.value as WifiConnectionInfo)
98
-
99
- if (attempt === MAX_CONNECT_RETRIES) return asyncResult
100
-
101
- // eslint-disable-next-line no-console
102
- console.warn(`WiFi connect failed, retrying in ${RETRY_DELAY_MS}ms…`)
103
- // Keep the radio up between retries so the next attempt reconnects fast.
104
- native.disconnect(false)
105
- await sleep(RETRY_DELAY_MS)
106
- }
107
- return err({name: 'ConnectFailed' as const, message: 'max retries exceeded'})
108
- },
109
-
110
- disconnect(options?: WifiDisconnectOptions): Result<void, WifiError> {
111
- return native.disconnect(options?.shutdown)
112
- },
113
-
114
- rssi(): Result<number, WifiError> {
115
- return native.rssi()
116
- },
117
-
118
- ip(): string | undefined {
119
- const ip = native.ip()
120
- return ip === '0.0.0.0' ? undefined : ip
121
- },
122
-
123
- status(): WifiStatus {
124
- const code = native.status()
125
- return StatusFromCode.get(code) ?? 'DISCONNECTED'
126
- },
127
-
128
- get isConnected(): boolean {
129
- return native.status() === WifiStatusCodes.CONNECTED
130
- },
131
-
132
- async scan(opts?: ScanOptions): Promise<Result<ScanResult[], WifiError>> {
133
- const startResult = native.scan(opts)
134
- if (!startResult.ok) return startResult
135
-
136
- const asyncResult = await startResult.value
137
- if (!asyncResult.ok) return asyncResult
138
- return ok(asyncResult.value as ScanResult[])
139
- },
140
-
141
- onConnect: lazyEvent<WifiConnectionInfo>(native, 'connect'),
142
- onDisconnect: lazyEvent<WifiDisconnectReason>(native, 'disconnect'),
143
- onRssiLow: lazyEvent<number>(native, 'rssi-low'),
144
-
145
- get mac(): string {
146
- const result = native.mac()
147
- return result.ok ? result.value : ''
148
- },
149
-
150
- get hostname(): string | undefined {
151
- return native.getHostname()
152
- },
153
-
154
- ipConfig: ((
155
- opts?: StaticIpConfig,
156
- ): Result<IpConfig | undefined, WifiError> | Result<void, WifiError> => {
157
- if (opts === undefined) {
158
- return native.getIpConfig()
159
- }
160
- return native.setIpConfig(opts)
161
- }) as Wifi['ipConfig'],
162
-
163
- ap,
164
-
165
- get txPower(): number {
166
- const result = native.getTxPower()
167
- return result.ok ? result.value : 0
168
- },
169
-
170
- get rssiThreshold(): number {
171
- return native.getRssiThreshold()
172
- },
173
-
174
- set rssiThreshold(value: number) {
175
- native.setRssiThreshold(value)
176
- },
177
-
178
- get powerSave(): PowerSaveMode {
179
- return native.getPowerSave() as PowerSaveMode
180
- },
181
-
182
- set powerSave(mode: PowerSaveMode) {
183
- native.setPowerSave(mode)
184
- },
185
-
186
- get country(): WifiCountryCode | undefined {
187
- return native.getCountry() as WifiCountryCode | undefined
188
- },
189
- }
190
-
191
- export {wifi}