lgq
3 天以前 081f12a52906abe6c2d139fdc144135978681009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
 * Network Module based on native network_bridge C library.
 * This module provides a singleton interface to network management.
 * Can be used across threads, but `on` and `loop` functions need to be called in the same thread.
 * Features:
 * - Initialize/deinitialize network stack
 * - Connect/disconnect to Ethernet/WiFi/4G
 * - Query network status, local IP, RSSI
 * - Scan WiFi hotspots
 * - Register event callbacks (status change, WiFi scan complete)
 *
 * Usage:
 * - Call `init()` once to initialize.
 * - Use connect/disconnect/getStatus/isConnected/getLocalIP/getRSSI/scanWifi as needed.
 * - Use `on` to register event handlers.
 * - Call loop() periodically to process events (e.g. in setInterval).
 *
 * Doc/Demo: https://github.com/DejaOS/DejaOS
 */
import { netClass } from './libvbar-m-dxnetwork.so'
 
/**
 * @typedef {object} NetOption
 * @property {string} ip - Static IP address.
 * @property {string} gateway - Static gateway address.
 * @property {string} netmask - Static netmask.
 * @property {string} [dns] - Static DNS server address.
 */
 
const dxnetwork = {};
 
// 常量与枚举
dxnetwork.NET_TYPE = { ETH: 1, WIFI: 2, MODEM: 4 }; // 4 = 4G Modem
dxnetwork.IP_MODE = { DHCP: 0, STATIC: 1 };
 
const net = new netClass();
let _callbacks = {};
 
/**
 * Initializes the network.
 * @returns {void}
 */
dxnetwork.init = function () {
    net.init();
}
 
/**
 * Deinitializes the network stack and releases resources.
 * @returns {void}
 */
dxnetwork.deinit = function () {
    net.deinit();
}
 
/**
 * Connects to a network (Ethernet/WiFi/4G).
 * @param {object} options - Connection options.
 * @param {number} options.netType - Network type:
 *   1 = Ethernet,
 *   2 = WiFi,
 *   4 = 4G Modem.
 * @param {number} [options.ipMode] - IP mode: 0 = DHCP, 1 = Static.
 * @param {string} [options.ssid] - WiFi SSID (for WiFi only).
 * @param {string} [options.psk] - WiFi password (for WiFi only).
 * @param {string} [options.apn] - APN for 4G connection.
 * @param {string} [options.user] - Username for 4G APN.
 * @param {string} [options.password] - Password for 4G APN.
 * @param {string} [options.ip] - Static IP address (for static mode).
 * @param {string} [options.gateway] - Static gateway (for static mode).
 * @param {string} [options.netmask] - Static netmask (for static mode).
 * @param {string} [options.dns] - Static DNS (for static mode).
 * @param {string} [options.macaddr] - Custom MAC address (for Ethernet only).
 * @returns {void}
 * @example
 * dxnetwork.connect({ netType: 2, ssid: 'MyWiFi', psk: 'password' });
 */
dxnetwork.connect = function (options) {
    return net.connect(options);
}
 
/**
 * Connects to Ethernet with DHCP.
 * @param {string} [macaddr] - Optional custom MAC address.Usually not needed.
 * @returns {void}
 * @example
 * dxnetwork.connectEthWithDHCP();
 * dxnetwork.connectEthWithDHCP('00:11:22:33:44:55');
 */
dxnetwork.connectEthWithDHCP = function (macaddr) {
    const options = {
        netType: 1, // Ethernet
        ipMode: 0   // DHCP
    };
    if (macaddr) {
        options.macaddr = macaddr;
    }
    return net.connect(options);
};
 
/**
 * Connects to Ethernet with a static IP configuration.
 * @param {NetOption} netOption - The static IP configuration.
 * @param {string} [macaddr] - Optional custom MAC address. Usually not needed.
 * @returns {void}
 * @example
 * dxnetwork.connectEth({
 *   ip: '192.168.1.100',
 *   gateway: '192.168.1.1',
 *   netmask: '255.255.255.0',
 *   dns: '8.8.8.8'
 * });
 */
dxnetwork.connectEth = function (netOption, macaddr) {
    if (!netOption || !netOption.ip || !netOption.gateway || !netOption.netmask) {
        throw new Error('Static IP configuration (ip, gateway, netmask) is required for connectEth.');
    }
    const options = {
        netType: 1, // Ethernet
        ipMode: 1,  // Static
        ip: netOption.ip,
        gateway: netOption.gateway,
        netmask: netOption.netmask,
        dns: netOption.dns
    };
    if (macaddr) {
        options.macaddr = macaddr;
    }
    return net.connect(options);
};
 
/**
 * Connects to a WiFi network with DHCP.
 * @param {string} ssid - The SSID of the WiFi network.
 * @param {string} psk - The password (Pre-Shared Key) of the WiFi network.
 * @returns {void}
 * @example
 * dxnetwork.connectWifiWithDHCP('MyWiFi', 'MyPassword');
 */
dxnetwork.connectWifiWithDHCP = function (ssid, psk) {
    if (!ssid) {
        throw new Error('SSID is required for connectWifiWithDHCP.');
    }
    const options = {
        netType: 2, // WiFi
        ipMode: 0,  // DHCP
        ssid: ssid,
        psk: psk
    };
    return net.connect(options);
};
 
/**
 * Connects to a WiFi network with a static IP configuration.
 * @param {string} ssid - The SSID of the WiFi network.
 * @param {string} psk - The password (Pre-Shared Key) of the WiFi network.
 * @param {NetOption} netOption - The static IP configuration.
 * @returns {void}
 * @example
 * dxnetwork.connectWifi('MyWiFi', 'MyPassword', {
 *   ip: '192.168.1.101',
 *   gateway: '192.168.1.1',
 *   netmask: '255.255.255.0'
 * });
 */
dxnetwork.connectWifi = function (ssid, psk, netOption) {
    if (!netOption || !netOption.ip || !netOption.gateway || !netOption.netmask) {
        throw new Error('Static IP configuration (ip, gateway, netmask) is required for connectWifi with static IP.');
    }
    if (!ssid) {
        throw new Error('SSID is required for connectWifi with static IP.');
    }
    const options = {
        netType: 2, // WiFi
        ipMode: 1,  // Static
        ssid: ssid,
        psk: psk,
        ip: netOption.ip,
        gateway: netOption.gateway,
        netmask: netOption.netmask,
        dns: netOption.dns
    };
    return net.connect(options);
};
 
/**
 * Connects to a 4G mobile network with default settings.
 * @returns {void}
 * @example
 * dxnetwork.connect4G();
 */
dxnetwork.connect4G = function () {
    const options = {
        netType: 4, // 4G Modem
    };
    return net.connect(options);
};
 
/**
 * Disconnects from the current network.
 * @returns {void}
 */
dxnetwork.disconnect = function () {
    net.disconnect();
}
 
/**
 * Gets the current network status.
 * @returns {number} Network status code.
 */
dxnetwork.getStatus = function () {
    return net.getStatus();
}
 
/**
 * Gets the current network type
 * @returns {number} Network type code.
 */
dxnetwork.getType = function () {
    return net.getType();
}
 
/**
 * Checks if the network is currently connected.
 * @returns {boolean} True if connected, false otherwise.
 */
dxnetwork.isConnected = function () {
    return net.isConnected();
}
 
/**
 * Gets the local IP address.
 * @returns {string} ip address.
 * @returns {string} gateway address.
 * @returns {string} netmask address.
 * @returns {string} dns address.
 */
dxnetwork.getNetParam = function () {
    return net.getNetParam();
}
 
/**
 * Gets the current signal strength (RSSI).
 * @returns {number} RSSI value.
 */
dxnetwork.getRSSI = function () {
    return net.getRSSI();
}
 
/**
 * Gets the current network MAC address.
 * @returns {string} MAC address.
 */
dxnetwork.getNetMac = function () {
    return net.getNetMac();
}
 
/**
 * Scans for available WiFi hotspots.
 * @param {number} [timeout=2500] - Timeout in milliseconds.
 * @param {number} [interval=100] - Scan interval in milliseconds.
 * @returns {Array} List of WiFi hotspots.
 */
dxnetwork.scanWifi = function (timeout, interval) {
    return net.scanWifi(timeout, interval);
};
 
/**
 * Sets callback handlers for network events.
 * Current only support status change event.
 * @param {object} callbacks - Callback functions.
 * @param {function(netType:number, status:number)} [callbacks.onStatusChange] - Called when network status changes.
 * @example
 * dxnetwork.setCallbacks({
 *   onStatusChange: function(netType, status) {
 *   }
 * });
 * @returns {void}
 */
 
dxnetwork.setCallbacks = function (callbacks) {
    _callbacks = callbacks;
}
 
/**
 * Processes events from the network event queue. Should be called periodically (e.g. in setInterval).
 * Handles status change event.
 * @example
 * setInterval(() => {
 *   try{
 *     dxnetwork.loop();
 *   } catch (e) {
 *     log.error('Error in network loop:', e);
 *   } 
 * }, 50); // Process events every 50ms
 */
dxnetwork.loop = function () {
    let ev = net.getEvent()
    if (ev && ev.type === 0 && _callbacks.onStatusChange) {
        _callbacks.onStatusChange(ev.netType, ev.netStatus);
    }
}
 
/**
 * Gets the native network client object.
 * @returns {Object|null} The native client object, or null if not initialized.
 */
dxnetwork.getNative = function () {
    return net;
}
 
export default dxnetwork;