@typecad/hal 1.0.0-alpha.6 → 1.0.0-alpha.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/src/wifi.ts ADDED
@@ -0,0 +1,221 @@
1
+ import {
2
+ wifiConnect,
3
+ wifiConnectStart,
4
+ wifiDisconnect,
5
+ wifiStatus,
6
+ wifiIsConnected,
7
+ wifiLocalIp,
8
+ wifiRssi,
9
+ wifiMac,
10
+ wifiSetHostname,
11
+ wifiSetStaticIp,
12
+ wifiSetAutoReconnect,
13
+ wifiSetPowerSave,
14
+ wifiSetTxPower,
15
+ wifiOnEvent,
16
+ wifiApStart,
17
+ wifiApStop,
18
+ wifiApClientCount,
19
+ wifiApIp,
20
+ wifiApSetChannel,
21
+ wifiApSetHidden,
22
+ wifiApSetMaxClients,
23
+ wifiScan,
24
+ wifiScanStart,
25
+ wifiScanCount,
26
+ wifiScanSsid,
27
+ wifiScanRssi,
28
+ wifiScanEncryption,
29
+ wifiScanChannel,
30
+ wifiSaveCredentials,
31
+ wifiConnectSaved,
32
+ wifiClearCredentials,
33
+ wifiWaitConnected,
34
+ wifiWaitDisconnected,
35
+ } from './emit.js';
36
+ import { callback } from './callback.js';
37
+
38
+ /** Normalized WiFi link status (mapped from esp_wifi events by the runtime shim). */
39
+ export enum WiFiStatus {
40
+ Idle = 0,
41
+ Connecting = 1,
42
+ Connected = 2,
43
+ ConnectFailed = 3,
44
+ Disconnected = 4,
45
+ }
46
+
47
+ export enum WiFiEncryption {
48
+ Open = 0,
49
+ WEP = 1,
50
+ WPA = 2,
51
+ WPA2 = 3,
52
+ WPA3 = 4,
53
+ Enterprise = 5,
54
+ }
55
+
56
+ /**
57
+ * WiFi radio / link control, lowered to native ESP-IDF (`esp_wifi` /
58
+ * `esp_netif` / `esp_event` / `nvs_flash`) by framework-esp32.
59
+ *
60
+ * No `include()` calls here — ESP-IDF headers are framework-owned and added
61
+ * via forcedIncludes when the program uses wifi.* ops (the Preferences
62
+ * lesson: HAL files must not carry platform headers). Frameworks without a
63
+ * wifi lowering reject these ops with a diagnostic.
64
+ */
65
+ export class WiFiClass {
66
+ static readonly __instance_name = "WiFi";
67
+
68
+ /** Blocking at top level; cooperatively awaitable inside async functions. */
69
+ connect(ssid: string, password?: string, timeoutMs: number = 15000): Promise<boolean> {
70
+ wifiConnect(ssid, password, timeoutMs);
71
+ return Promise.resolve(false);
72
+ }
73
+
74
+ /** Fire-and-forget STA begin; poll status() / untilConnected(). */
75
+ connectAsync(ssid: string, password?: string): void {
76
+ wifiConnectStart(ssid, password);
77
+ }
78
+
79
+ untilConnected(timeoutMs: number = 15000): Promise<boolean> {
80
+ wifiWaitConnected(timeoutMs);
81
+ return Promise.resolve(false);
82
+ }
83
+
84
+ untilDisconnected(): Promise<void> {
85
+ wifiWaitDisconnected();
86
+ return Promise.resolve();
87
+ }
88
+
89
+ disconnect(): void {
90
+ wifiDisconnect();
91
+ }
92
+
93
+ isConnected(): boolean {
94
+ return wifiIsConnected();
95
+ }
96
+
97
+ status(): WiFiStatus {
98
+ return wifiStatus() as WiFiStatus;
99
+ }
100
+
101
+ localIP(): string {
102
+ return wifiLocalIp();
103
+ }
104
+
105
+ rssi(): number {
106
+ return wifiRssi();
107
+ }
108
+
109
+ macAddress(): string {
110
+ return wifiMac();
111
+ }
112
+
113
+ hostname(name: string): this {
114
+ wifiSetHostname(name);
115
+ return this;
116
+ }
117
+
118
+ staticIP(ip: string, gateway: string, subnet: string, dns?: string): this {
119
+ wifiSetStaticIp(ip, gateway, subnet, dns);
120
+ return this;
121
+ }
122
+
123
+ autoReconnect(enabled: boolean): this {
124
+ wifiSetAutoReconnect(enabled);
125
+ return this;
126
+ }
127
+
128
+ powerSave(mode: "default" | "none"): this {
129
+ wifiSetPowerSave(mode);
130
+ return this;
131
+ }
132
+
133
+ /** Cap TX power in dBm (roughly 2–20). Safe to call before connect() —
134
+ * the value is applied after the radio starts. */
135
+ txPower(dbm: number): this {
136
+ wifiSetTxPower(dbm);
137
+ return this;
138
+ }
139
+
140
+ saveCredentials(ssid: string, password: string): void {
141
+ wifiSaveCredentials(ssid, password);
142
+ }
143
+
144
+ connectSaved(timeoutMs: number = 15000): boolean {
145
+ return wifiConnectSaved(timeoutMs);
146
+ }
147
+
148
+ clearCredentials(): void {
149
+ wifiClearCredentials();
150
+ }
151
+
152
+ onConnect(handler: () => void): void {
153
+ wifiOnEvent("connect", callback(handler));
154
+ }
155
+
156
+ onDisconnect(handler: () => void): void {
157
+ wifiOnEvent("disconnect", callback(handler));
158
+ }
159
+
160
+ startAP(ssid: string, password?: string): boolean {
161
+ return wifiApStart(ssid, password);
162
+ }
163
+
164
+ apChannel(ch: number): this {
165
+ wifiApSetChannel(ch);
166
+ return this;
167
+ }
168
+
169
+ apHidden(hidden: boolean): this {
170
+ wifiApSetHidden(hidden);
171
+ return this;
172
+ }
173
+
174
+ apMaxClients(n: number): this {
175
+ wifiApSetMaxClients(n);
176
+ return this;
177
+ }
178
+
179
+ stopAP(): void {
180
+ wifiApStop();
181
+ }
182
+
183
+ apClientCount(): number {
184
+ return wifiApClientCount();
185
+ }
186
+
187
+ apIP(): string {
188
+ return wifiApIp();
189
+ }
190
+
191
+ scan(): number {
192
+ return wifiScan();
193
+ }
194
+
195
+ scanAsync(): Promise<void> {
196
+ wifiScanStart();
197
+ return Promise.resolve();
198
+ }
199
+
200
+ scanCount(): number {
201
+ return wifiScanCount();
202
+ }
203
+
204
+ scanSSID(i: number): string {
205
+ return wifiScanSsid(i);
206
+ }
207
+
208
+ scanRSSI(i: number): number {
209
+ return wifiScanRssi(i);
210
+ }
211
+
212
+ scanEncryption(i: number): WiFiEncryption {
213
+ return wifiScanEncryption(i) as WiFiEncryption;
214
+ }
215
+
216
+ scanChannel(i: number): number {
217
+ return wifiScanChannel(i);
218
+ }
219
+ }
220
+
221
+ export const WiFi = new WiFiClass();