dh-sdk 1.0.112701 → 1.0.120101
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/README.md +50 -16
- package/dist/adapters/wechat.d.ts +8 -0
- package/dist/core/sdk.d.ts +36 -2
- package/dist/crypto.d.ts +14 -31
- package/dist/index.d.ts +15 -7
- package/dist/mac.d.ts +12 -0
- package/dist/modules/auth.d.ts +35 -40
- package/dist/modules/controller.d.ts +7 -1
- package/dist/modules/provisioner.d.ts +9 -2
- package/dist/modules/scanner.d.ts +1 -0
- package/dist/sha256.d.ts +14 -0
- package/dist/types.d.ts +56 -13
- package/dist/uuids.d.ts +31 -0
- package/dist/wechat.d.ts +4 -2
- package/dist/wechat.js +585 -322
- package/dist/wechat.js.map +1 -1
- package/package.json +1 -1
package/dist/wechat.js
CHANGED
|
@@ -21,10 +21,45 @@ function createLogger(enabled, custom) {
|
|
|
21
21
|
* 版本信息 — 由 rollup replace 插件在构建时注入
|
|
22
22
|
*/
|
|
23
23
|
function getVersion() {
|
|
24
|
-
return "1.0.
|
|
24
|
+
return "1.0.120101" ;
|
|
25
25
|
}
|
|
26
26
|
function getBuildTime() {
|
|
27
|
-
return "2026-
|
|
27
|
+
return "2026-12-01T02:54:07.927Z" ;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* GATT UUID 集中定义(固件协议 v2:绑定状态 + 隐蔽广播 + HMAC 鉴权)
|
|
32
|
+
*
|
|
33
|
+
* 固件已确认:
|
|
34
|
+
* - 0xFF01(Read) 鉴权挑战码特征值:读取返回 16 字节随机 Challenge
|
|
35
|
+
* - 0xFF02(Write)鉴权响应特征值:写入 HMAC-SHA256(home_token, Challenge)
|
|
36
|
+
*
|
|
37
|
+
* 待固件确认(当前为占位值,可通过 configureUuids() 或 DoneHomeConfig.uuids 覆盖):
|
|
38
|
+
* - 业务 Write 特征值(控制指令),默认占位 0xFF03
|
|
39
|
+
* - 业务 Notify 特征值(状态推送),默认占位 0xFF04
|
|
40
|
+
* - 服务 UUID,默认沿用 0xFF01 服务
|
|
41
|
+
*/
|
|
42
|
+
const BASE = '-0000-1000-8000-00805f9b34fb';
|
|
43
|
+
const u16 = (n) => `0000${n.toString(16).padStart(4, '0')}${BASE}`;
|
|
44
|
+
const DEFAULT_UUIDS = {
|
|
45
|
+
service: u16(0xff01),
|
|
46
|
+
authChallenge: u16(0xff01),
|
|
47
|
+
authResponse: u16(0xff02),
|
|
48
|
+
bizWrite: u16(0xff03),
|
|
49
|
+
bizNotify: u16(0xff04),
|
|
50
|
+
};
|
|
51
|
+
let current = { ...DEFAULT_UUIDS };
|
|
52
|
+
/** 获取当前 UUID 配置(模块在调用时实时读取,支持运行期覆盖) */
|
|
53
|
+
function getUuids() {
|
|
54
|
+
return current;
|
|
55
|
+
}
|
|
56
|
+
/** 覆盖部分 UUID(DoneHomeConfig.uuids 或运行期调用) */
|
|
57
|
+
function configureUuids(partial) {
|
|
58
|
+
current = { ...current, ...partial };
|
|
59
|
+
}
|
|
60
|
+
/** 恢复默认 UUID(测试用) */
|
|
61
|
+
function resetUuids() {
|
|
62
|
+
current = { ...DEFAULT_UUIDS };
|
|
28
63
|
}
|
|
29
64
|
|
|
30
65
|
/**
|
|
@@ -44,18 +79,13 @@ function macFromDeviceId(deviceId) {
|
|
|
44
79
|
const hex = deviceId.replace(/[-:]/g, '');
|
|
45
80
|
return MAC_HEX_RE.test(hex) ? hex.toUpperCase() : undefined;
|
|
46
81
|
}
|
|
47
|
-
function toBytes(adv) {
|
|
82
|
+
function toBytes$1(adv) {
|
|
48
83
|
if (adv instanceof ArrayBuffer)
|
|
49
84
|
return new Uint8Array(adv);
|
|
50
85
|
if (adv instanceof Uint8Array)
|
|
51
86
|
return adv;
|
|
52
87
|
return Uint8Array.from(adv);
|
|
53
88
|
}
|
|
54
|
-
function hexMac(bytes) {
|
|
55
|
-
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0'))
|
|
56
|
-
.join('')
|
|
57
|
-
.toUpperCase();
|
|
58
|
-
}
|
|
59
89
|
/**
|
|
60
90
|
* 从广播 ManufacturerData 解析 MAC(固件格式:cid 2 字节小端 + MAC 6 字节)
|
|
61
91
|
* 兼容不含 cid(直接 6 字节 MAC)的情况
|
|
@@ -63,13 +93,37 @@ function hexMac(bytes) {
|
|
|
63
93
|
function macFromAdvertisData(adv) {
|
|
64
94
|
if (!adv)
|
|
65
95
|
return undefined;
|
|
66
|
-
const bytes = toBytes(adv);
|
|
96
|
+
const bytes = toBytes$1(adv);
|
|
67
97
|
if (bytes.length >= 8)
|
|
68
|
-
return
|
|
98
|
+
return hexUpper(bytes.subarray(2, 8)); // cid(2B) + MAC(6B)
|
|
69
99
|
if (bytes.length >= 6)
|
|
70
|
-
return
|
|
100
|
+
return hexUpper(bytes.subarray(0, 6)); // 无 cid
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
function hexUpper(bytes) {
|
|
104
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0'))
|
|
105
|
+
.join('')
|
|
106
|
+
.toUpperCase();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 从广播 ManufacturerData 解析 home_id(固件协议 v2 隐蔽广播格式:cid 2 字节小端 + home_id 原始字节)
|
|
110
|
+
* 返回大写 hex 字符串。
|
|
111
|
+
*
|
|
112
|
+
* 注意:未绑定设备的 ManufacturerData 为 cid + MAC(同位置),此函数会将其解析为 MAC 的 hex;
|
|
113
|
+
* home_id 的语义只在已绑定设备(隐蔽广播)上成立,匹配时以服务端下发的 home_id 为准。
|
|
114
|
+
*/
|
|
115
|
+
function homeIdFromAdvertisData(adv) {
|
|
116
|
+
if (!adv)
|
|
117
|
+
return undefined;
|
|
118
|
+
const bytes = toBytes$1(adv);
|
|
119
|
+
if (bytes.length > 2)
|
|
120
|
+
return hexUpper(bytes.subarray(2)); // cid(2B) + home_id 原始字节
|
|
71
121
|
return undefined;
|
|
72
122
|
}
|
|
123
|
+
/** 综合提取 home_id:广播 ManufacturerData 解析 */
|
|
124
|
+
function resolveHomeId(device) {
|
|
125
|
+
return homeIdFromAdvertisData(device.advertisData);
|
|
126
|
+
}
|
|
73
127
|
/** 综合提取 MAC:优先 Android deviceId 转换,其次广播 ManufacturerData 解析 */
|
|
74
128
|
function resolveMac(device) {
|
|
75
129
|
var _a;
|
|
@@ -102,6 +156,8 @@ class WechatBLEAdapter {
|
|
|
102
156
|
this.notifyCallbacks = new Map();
|
|
103
157
|
this.connectionListeners = new Map();
|
|
104
158
|
this.globalNotifyHandler = null;
|
|
159
|
+
this.globalConnStateHandler = null;
|
|
160
|
+
this.connectionNotifier = null;
|
|
105
161
|
this.log = log;
|
|
106
162
|
}
|
|
107
163
|
async init() {
|
|
@@ -127,6 +183,7 @@ class WechatBLEAdapter {
|
|
|
127
183
|
const device = {
|
|
128
184
|
deviceId: d.deviceId,
|
|
129
185
|
mac: resolveMac(d),
|
|
186
|
+
home_id: resolveHomeId(d),
|
|
130
187
|
name: d.name || 'Unknown',
|
|
131
188
|
rssi: d.RSSI,
|
|
132
189
|
advertisData: d.advertisData,
|
|
@@ -164,15 +221,8 @@ class WechatBLEAdapter {
|
|
|
164
221
|
deviceId: options.deviceId,
|
|
165
222
|
timeout: timeoutMs,
|
|
166
223
|
});
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
if (res.deviceId === options.deviceId) {
|
|
170
|
-
const cb = this.connectionListeners.get(options.deviceId);
|
|
171
|
-
if (cb)
|
|
172
|
-
cb(res.connected);
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
wx.onBLEConnectionStateChange(handler);
|
|
224
|
+
// 注册一次全局连接状态监听(unexpected disconnect:鉴权失败断开/多端互斥踢下线均可感知)
|
|
225
|
+
this.ensureConnStateHandler();
|
|
176
226
|
this.log('info', `Connected to ${options.deviceId}`);
|
|
177
227
|
return options.deviceId;
|
|
178
228
|
}
|
|
@@ -205,7 +255,12 @@ class WechatBLEAdapter {
|
|
|
205
255
|
return res.value;
|
|
206
256
|
}
|
|
207
257
|
async write(deviceId, serviceUUID, characteristicUUID, data, _withoutResponse) {
|
|
208
|
-
|
|
258
|
+
// Uint8Array 视图可能只是底层 buffer 的一段,需要裁剪出实际字节
|
|
259
|
+
const buffer = data instanceof Uint8Array
|
|
260
|
+
? (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength
|
|
261
|
+
? data.buffer
|
|
262
|
+
: data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength))
|
|
263
|
+
: data;
|
|
209
264
|
await wxp('writeBLECharacteristicValue', {
|
|
210
265
|
deviceId,
|
|
211
266
|
serviceId: serviceUUID,
|
|
@@ -258,7 +313,11 @@ class WechatBLEAdapter {
|
|
|
258
313
|
wx.offBLECharacteristicValueChange(this.globalNotifyHandler);
|
|
259
314
|
this.globalNotifyHandler = null;
|
|
260
315
|
}
|
|
261
|
-
|
|
316
|
+
if (this.globalConnStateHandler) {
|
|
317
|
+
wx.offBLEConnectionStateChange(this.globalConnStateHandler);
|
|
318
|
+
this.globalConnStateHandler = null;
|
|
319
|
+
}
|
|
320
|
+
this.connectionNotifier = null;
|
|
262
321
|
if (this.initialized) {
|
|
263
322
|
await wxp('closeBluetoothAdapter', {});
|
|
264
323
|
this.initialized = false;
|
|
@@ -266,6 +325,25 @@ class WechatBLEAdapter {
|
|
|
266
325
|
this.log('info', 'WeChat BLE adapter destroyed');
|
|
267
326
|
}
|
|
268
327
|
// -- helpers --
|
|
328
|
+
/**
|
|
329
|
+
* 注册连接状态回调(core 层用于统一 emit deviceConnected/deviceDisconnected,
|
|
330
|
+
* 固件主动断开(鉴权失败/多端互斥踢下线)时客户端可感知)
|
|
331
|
+
*/
|
|
332
|
+
setConnectionStateNotifier(cb) {
|
|
333
|
+
this.connectionNotifier = cb;
|
|
334
|
+
}
|
|
335
|
+
ensureConnStateHandler() {
|
|
336
|
+
if (this.globalConnStateHandler)
|
|
337
|
+
return;
|
|
338
|
+
this.globalConnStateHandler = (res) => {
|
|
339
|
+
const cb = this.connectionListeners.get(res.deviceId);
|
|
340
|
+
if (cb)
|
|
341
|
+
cb(res.connected);
|
|
342
|
+
if (this.connectionNotifier)
|
|
343
|
+
this.connectionNotifier(res.deviceId, res.connected);
|
|
344
|
+
};
|
|
345
|
+
wx.onBLEConnectionStateChange(this.globalConnStateHandler);
|
|
346
|
+
}
|
|
269
347
|
mapProperties(props) {
|
|
270
348
|
const result = [];
|
|
271
349
|
if (props.read)
|
|
@@ -283,6 +361,12 @@ class WechatBLEAdapter {
|
|
|
283
361
|
/**
|
|
284
362
|
* DoneHomeSDK 核心层
|
|
285
363
|
* 负责 BLE 适配器管理、事件系统、底层 BLE 操作
|
|
364
|
+
*
|
|
365
|
+
* 协议 v2 适配:
|
|
366
|
+
* - 配置 home_token / home_id(鉴权密钥与隐蔽广播匹配)
|
|
367
|
+
* - 配置 uuids 覆盖 GATT UUID(业务特征值占位待固件确认)
|
|
368
|
+
* - 适配器连接状态变化统一收敛为 deviceConnected/deviceDisconnected 事件
|
|
369
|
+
* (固件多端互斥踢下线时客户端可感知)
|
|
286
370
|
*/
|
|
287
371
|
class DoneHomeSDK {
|
|
288
372
|
constructor(config = {}) {
|
|
@@ -294,11 +378,39 @@ class DoneHomeSDK {
|
|
|
294
378
|
this.listeners = new Map();
|
|
295
379
|
/** 当前平台 */
|
|
296
380
|
this.platform = 'wechat';
|
|
381
|
+
/** 已连接设备集合(连接事件去重) */
|
|
382
|
+
this.connectedDevices = new Set();
|
|
383
|
+
/** 已广播过断开事件的设备(未经连接标记的设备首次断开也广播,重复上报去重) */
|
|
384
|
+
this.disconnectedReported = new Set();
|
|
297
385
|
this.log = createLogger((_a = config.debug) !== null && _a !== void 0 ? _a : false, config.logger);
|
|
298
386
|
this.log('info', `DoneHome SDK v${getVersion()} (${this.platform})`);
|
|
299
387
|
}
|
|
388
|
+
// ──────── 配置访问 ────────
|
|
389
|
+
/** 鉴权密钥 home_token(SDK 配置值) */
|
|
390
|
+
get home_token() {
|
|
391
|
+
return this.config.home_token;
|
|
392
|
+
}
|
|
393
|
+
/** 家庭 ID home_id(隐蔽广播匹配用) */
|
|
394
|
+
get home_id() {
|
|
395
|
+
return this.config.home_id;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* 动态更新 home_id 与 home_token
|
|
399
|
+
* 用于启动时先创建 SDK 实例、后异步获取到 token 再赋值的场景
|
|
400
|
+
* @param home_id 家庭 ID
|
|
401
|
+
* @param home_token 鉴权密钥
|
|
402
|
+
*/
|
|
403
|
+
setHome(home_id, home_token) {
|
|
404
|
+
this.config.home_id = home_id;
|
|
405
|
+
this.config.home_token = home_token;
|
|
406
|
+
this.log('info', `SDK home_id / home_token updated`);
|
|
407
|
+
}
|
|
300
408
|
// ──────── 初始化 ────────
|
|
301
409
|
async init(adapter) {
|
|
410
|
+
// UUID 覆盖需在模块使用前生效
|
|
411
|
+
if (this.config.uuids) {
|
|
412
|
+
configureUuids(this.config.uuids);
|
|
413
|
+
}
|
|
302
414
|
if (adapter) {
|
|
303
415
|
this.adapter = adapter;
|
|
304
416
|
}
|
|
@@ -306,8 +418,49 @@ class DoneHomeSDK {
|
|
|
306
418
|
this.adapter = new WechatBLEAdapter(this.log);
|
|
307
419
|
}
|
|
308
420
|
await this.adapter.init();
|
|
421
|
+
this.wireConnectionStateEvents();
|
|
309
422
|
this.log('info', `SDK initialized on platform: ${this.adapter.platform}`);
|
|
310
423
|
}
|
|
424
|
+
/**
|
|
425
|
+
* 将适配器连接状态变化统一收敛为 SDK 事件
|
|
426
|
+
* (unexpected disconnect:固件鉴权失败断开、多端互斥踢下线等均可感知)
|
|
427
|
+
*/
|
|
428
|
+
wireConnectionStateEvents() {
|
|
429
|
+
var _a;
|
|
430
|
+
const a = this.adapter;
|
|
431
|
+
(_a = a.setConnectionStateNotifier) === null || _a === void 0 ? void 0 : _a.call(a, (deviceId, connected) => {
|
|
432
|
+
if (connected) {
|
|
433
|
+
this.notifyDeviceConnected(deviceId);
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
this.notifyDeviceDisconnected(deviceId);
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
// ──────── 连接事件(去重收敛,供 Connector/Auth 等模块与用户监听) ────────
|
|
441
|
+
/** 标记设备已连接并广播事件(重复调用不重复广播) */
|
|
442
|
+
notifyDeviceConnected(deviceId) {
|
|
443
|
+
this.disconnectedReported.delete(deviceId);
|
|
444
|
+
if (!this.connectedDevices.has(deviceId)) {
|
|
445
|
+
this.connectedDevices.add(deviceId);
|
|
446
|
+
this.emit('deviceConnected', deviceId);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* 标记设备已断开并广播事件。
|
|
451
|
+
* 经 Connector 连接的设备按集合去重;直接 sdk.connect + auth 的用法
|
|
452
|
+
* (未经连接标记)首次断开同样广播,后续重复上报去重。
|
|
453
|
+
*/
|
|
454
|
+
notifyDeviceDisconnected(deviceId) {
|
|
455
|
+
if (this.connectedDevices.delete(deviceId)) {
|
|
456
|
+
this.emit('deviceDisconnected', deviceId);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (!this.disconnectedReported.has(deviceId)) {
|
|
460
|
+
this.disconnectedReported.add(deviceId);
|
|
461
|
+
this.emit('deviceDisconnected', deviceId);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
311
464
|
// ──────── 底层 BLE 操作(供各模块调用) ────────
|
|
312
465
|
ensureReady() {
|
|
313
466
|
if (!this.adapter) {
|
|
@@ -386,6 +539,8 @@ class DoneHomeSDK {
|
|
|
386
539
|
await this.adapter.destroy();
|
|
387
540
|
this.adapter = null;
|
|
388
541
|
}
|
|
542
|
+
this.connectedDevices.clear();
|
|
543
|
+
this.disconnectedReported.clear();
|
|
389
544
|
this.listeners.clear();
|
|
390
545
|
this.log('info', 'SDK destroyed');
|
|
391
546
|
}
|
|
@@ -409,6 +564,7 @@ class Scanner {
|
|
|
409
564
|
* @param filter 筛选条件
|
|
410
565
|
* - service_uuid: 完整 UUID 透传 wx services 参数(底层过滤)
|
|
411
566
|
* - service_uuid_prefix: 广播包 Service UUID 前缀过滤(客户端过滤,如 '20100548')
|
|
567
|
+
* - home_id: home_id 精确匹配(大小写不敏感,用于已绑定设备隐蔽广播过滤)
|
|
412
568
|
* - rssi_min: RSSI 最低值
|
|
413
569
|
*/
|
|
414
570
|
async start(filter) {
|
|
@@ -419,7 +575,7 @@ class Scanner {
|
|
|
419
575
|
this.devices.clear();
|
|
420
576
|
this.scanning = true;
|
|
421
577
|
const onDevice = (device) => {
|
|
422
|
-
var _a;
|
|
578
|
+
var _a, _b;
|
|
423
579
|
// RSSI 过滤
|
|
424
580
|
if ((filter === null || filter === void 0 ? void 0 : filter.rssi_min) !== undefined && device.rssi < filter.rssi_min) {
|
|
425
581
|
return;
|
|
@@ -434,6 +590,14 @@ class Scanner {
|
|
|
434
590
|
return; // 广播包无匹配前缀的 Service UUID,丢弃
|
|
435
591
|
}
|
|
436
592
|
}
|
|
593
|
+
// home_id 过滤(隐蔽广播 ManufacturerData 携带的 home_id,大小写不敏感)
|
|
594
|
+
if ((filter === null || filter === void 0 ? void 0 : filter.home_id) !== undefined) {
|
|
595
|
+
const want = filter.home_id.toLowerCase();
|
|
596
|
+
const hid = ((_b = device.home_id) !== null && _b !== void 0 ? _b : '').toLowerCase();
|
|
597
|
+
if (hid !== want) {
|
|
598
|
+
return; // ManufacturerData 与目标 home_id 不匹配,丢弃
|
|
599
|
+
}
|
|
600
|
+
}
|
|
437
601
|
const entry = {
|
|
438
602
|
deviceId: device.deviceId,
|
|
439
603
|
name: device.name,
|
|
@@ -442,6 +606,9 @@ class Scanner {
|
|
|
442
606
|
if (device.mac) {
|
|
443
607
|
entry.mac = device.mac;
|
|
444
608
|
}
|
|
609
|
+
if (device.home_id) {
|
|
610
|
+
entry.home_id = device.home_id;
|
|
611
|
+
}
|
|
445
612
|
if (matchedUUIDs) {
|
|
446
613
|
entry.serviceUUIDs = matchedUUIDs;
|
|
447
614
|
}
|
|
@@ -536,7 +703,7 @@ class Connector {
|
|
|
536
703
|
}
|
|
537
704
|
// 注册断开监听,保持状态同步
|
|
538
705
|
this.registerDisconnectHandler(device_id, options);
|
|
539
|
-
this.sdk.
|
|
706
|
+
this.sdk.notifyDeviceConnected(device_id);
|
|
540
707
|
this.sdk.log('info', `Connected to ${device_id}`);
|
|
541
708
|
}
|
|
542
709
|
catch (err) {
|
|
@@ -572,7 +739,7 @@ class Connector {
|
|
|
572
739
|
}
|
|
573
740
|
finally {
|
|
574
741
|
this.connections.set(device_id, { ...entry, state: 'disconnected' });
|
|
575
|
-
this.sdk.
|
|
742
|
+
this.sdk.notifyDeviceDisconnected(device_id);
|
|
576
743
|
this.sdk.log('info', `Disconnected from ${device_id}`);
|
|
577
744
|
}
|
|
578
745
|
}
|
|
@@ -615,7 +782,7 @@ class Connector {
|
|
|
615
782
|
entry.reconnectAttempt = 0;
|
|
616
783
|
// 重连成功后重新注册断开监听
|
|
617
784
|
this.registerDisconnectHandler(device_id, options);
|
|
618
|
-
this.sdk.
|
|
785
|
+
this.sdk.notifyDeviceConnected(device_id);
|
|
619
786
|
return;
|
|
620
787
|
}
|
|
621
788
|
catch (_b) {
|
|
@@ -639,8 +806,8 @@ class Connector {
|
|
|
639
806
|
const entry = this.connections.get(device_id);
|
|
640
807
|
if (entry && entry.state === 'connected') {
|
|
641
808
|
entry.state = 'disconnected';
|
|
642
|
-
this.sdk.
|
|
643
|
-
this.sdk.log('warn', `Device ${device_id} disconnected unexpectedly`);
|
|
809
|
+
this.sdk.notifyDeviceDisconnected(device_id);
|
|
810
|
+
this.sdk.log('warn', `Device ${device_id} disconnected unexpectedly (may be kicked by another client)`);
|
|
644
811
|
// 自动重连
|
|
645
812
|
if (entry.autoReconnect && options) {
|
|
646
813
|
this.attemptReconnect(device_id, options).catch((err) => {
|
|
@@ -664,16 +831,205 @@ class Connector {
|
|
|
664
831
|
}
|
|
665
832
|
}
|
|
666
833
|
|
|
834
|
+
/**
|
|
835
|
+
* 纯 TypeScript SHA-256 / HMAC-SHA256 实现
|
|
836
|
+
*
|
|
837
|
+
* 微信小程序环境无 SubtleCrypto,故内置无依赖实现(FIPS 180-4 / RFC 2104)。
|
|
838
|
+
* 用于固件协议 v2 的应用层鉴权:HMAC-SHA256(home_token, challenge)
|
|
839
|
+
*/
|
|
840
|
+
const K = new Uint32Array([
|
|
841
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
842
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
843
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
844
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
845
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
846
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
847
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
848
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
849
|
+
]);
|
|
850
|
+
function toBytes(input) {
|
|
851
|
+
if (input instanceof Uint8Array)
|
|
852
|
+
return input;
|
|
853
|
+
if (input instanceof ArrayBuffer)
|
|
854
|
+
return new Uint8Array(input);
|
|
855
|
+
return Uint8Array.from(input);
|
|
856
|
+
}
|
|
857
|
+
/** SHA-256 摘要(同步) */
|
|
858
|
+
function sha256(msg) {
|
|
859
|
+
const m = toBytes(msg);
|
|
860
|
+
const len = m.length;
|
|
861
|
+
// 消息填充:msg + 0x80 + 0x00... + 64bit 长度(大端)
|
|
862
|
+
const totalLen = ((len + 9 + 63) >> 6) << 6;
|
|
863
|
+
const buf = new Uint8Array(totalLen);
|
|
864
|
+
buf.set(m);
|
|
865
|
+
buf[len] = 0x80;
|
|
866
|
+
const dv = new DataView(buf.buffer);
|
|
867
|
+
dv.setUint32(totalLen - 8, len >>> 29); // 长度高 32 位(len*8 / 2^32)
|
|
868
|
+
dv.setUint32(totalLen - 4, (len << 3) >>> 0); // 长度低 32 位(len*8 mod 2^32)
|
|
869
|
+
let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a;
|
|
870
|
+
let h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
|
|
871
|
+
const w = new Uint32Array(64);
|
|
872
|
+
for (let i = 0; i < totalLen; i += 64) {
|
|
873
|
+
for (let j = 0; j < 16; j++) {
|
|
874
|
+
w[j] = dv.getUint32(i + j * 4);
|
|
875
|
+
}
|
|
876
|
+
for (let j = 16; j < 64; j++) {
|
|
877
|
+
const w15 = w[j - 15];
|
|
878
|
+
const w2 = w[j - 2];
|
|
879
|
+
const s0 = ((w15 >>> 7) | (w15 << 25)) ^ ((w15 >>> 18) | (w15 << 14)) ^ (w15 >>> 3);
|
|
880
|
+
const s1 = ((w2 >>> 17) | (w2 << 15)) ^ ((w2 >>> 19) | (w2 << 13)) ^ (w2 >>> 10);
|
|
881
|
+
w[j] = (w[j - 16] + s0 + w[j - 7] + s1) >>> 0;
|
|
882
|
+
}
|
|
883
|
+
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
|
|
884
|
+
for (let j = 0; j < 64; j++) {
|
|
885
|
+
const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
886
|
+
const ch = (e & f) ^ (~e & g);
|
|
887
|
+
const t1 = (h + S1 + ch + K[j] + w[j]) >>> 0;
|
|
888
|
+
const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
889
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
890
|
+
const t2 = (S0 + maj) >>> 0;
|
|
891
|
+
h = g;
|
|
892
|
+
g = f;
|
|
893
|
+
f = e;
|
|
894
|
+
e = (d + t1) >>> 0;
|
|
895
|
+
d = c;
|
|
896
|
+
c = b;
|
|
897
|
+
b = a;
|
|
898
|
+
a = (t1 + t2) >>> 0;
|
|
899
|
+
}
|
|
900
|
+
h0 = (h0 + a) >>> 0;
|
|
901
|
+
h1 = (h1 + b) >>> 0;
|
|
902
|
+
h2 = (h2 + c) >>> 0;
|
|
903
|
+
h3 = (h3 + d) >>> 0;
|
|
904
|
+
h4 = (h4 + e) >>> 0;
|
|
905
|
+
h5 = (h5 + f) >>> 0;
|
|
906
|
+
h6 = (h6 + g) >>> 0;
|
|
907
|
+
h7 = (h7 + h) >>> 0;
|
|
908
|
+
}
|
|
909
|
+
const out = new Uint8Array(32);
|
|
910
|
+
const odv = new DataView(out.buffer);
|
|
911
|
+
[h0, h1, h2, h3, h4, h5, h6, h7].forEach((v, i) => odv.setUint32(i * 4, v));
|
|
912
|
+
return out;
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* HMAC-SHA256(同步,RFC 2104)
|
|
916
|
+
* @param key 密钥(home_token 字节),超过 64 字节自动先做 SHA-256
|
|
917
|
+
* @param msg 消息(如 16 字节 Challenge)
|
|
918
|
+
*/
|
|
919
|
+
function hmacSha256(key, msg) {
|
|
920
|
+
let k = toBytes(key);
|
|
921
|
+
const m = toBytes(msg);
|
|
922
|
+
if (k.length > 64)
|
|
923
|
+
k = sha256(k);
|
|
924
|
+
const block = new Uint8Array(64);
|
|
925
|
+
block.set(k);
|
|
926
|
+
const inner = new Uint8Array(64 + m.length);
|
|
927
|
+
const outer = new Uint8Array(64 + 32);
|
|
928
|
+
for (let i = 0; i < 64; i++) {
|
|
929
|
+
inner[i] = block[i] ^ 0x36;
|
|
930
|
+
outer[i] = block[i] ^ 0x5c;
|
|
931
|
+
}
|
|
932
|
+
inner.set(m, 64);
|
|
933
|
+
outer.set(sha256(inner), 64);
|
|
934
|
+
return sha256(outer);
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* DoneHome 加密/编码工具
|
|
939
|
+
*
|
|
940
|
+
* 内置纯 TS SHA-256 / HMAC-SHA256 实现(微信小程序无 SubtleCrypto),
|
|
941
|
+
* 用于固件协议 v2 的应用层鉴权:HMAC-SHA256(home_token, challenge)。
|
|
942
|
+
* 旧版 ECDSA/KeyPair API 已随协议 v2 移除。
|
|
943
|
+
*/
|
|
944
|
+
/** 字符串 → UTF-8 字节(不依赖 TextEncoder,小程序兼容) */
|
|
945
|
+
function utf8ToBytes(str) {
|
|
946
|
+
const out = [];
|
|
947
|
+
for (let i = 0; i < str.length; i++) {
|
|
948
|
+
let c = str.charCodeAt(i);
|
|
949
|
+
// 代理对合并
|
|
950
|
+
if (c >= 0xd800 && c <= 0xdbff && i + 1 < str.length) {
|
|
951
|
+
const c2 = str.charCodeAt(i + 1);
|
|
952
|
+
if (c2 >= 0xdc00 && c2 <= 0xdfff) {
|
|
953
|
+
c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
|
|
954
|
+
i++;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
if (c < 0x80) {
|
|
958
|
+
out.push(c);
|
|
959
|
+
}
|
|
960
|
+
else if (c < 0x800) {
|
|
961
|
+
out.push(0xc0 | (c >> 6), 0x80 | (c & 63));
|
|
962
|
+
}
|
|
963
|
+
else if (c < 0x10000) {
|
|
964
|
+
out.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 63), 0x80 | (c & 63));
|
|
965
|
+
}
|
|
966
|
+
else {
|
|
967
|
+
out.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 63), 0x80 | ((c >> 6) & 63), 0x80 | (c & 63));
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
return Uint8Array.from(out);
|
|
971
|
+
}
|
|
972
|
+
/** UTF-8 字节 → 字符串(不依赖 TextDecoder,小程序兼容) */
|
|
973
|
+
function bytesToUtf8(bytes) {
|
|
974
|
+
let out = '';
|
|
975
|
+
let i = 0;
|
|
976
|
+
while (i < bytes.length) {
|
|
977
|
+
const b = bytes[i];
|
|
978
|
+
let cp;
|
|
979
|
+
if (b < 0x80) {
|
|
980
|
+
cp = b;
|
|
981
|
+
i += 1;
|
|
982
|
+
}
|
|
983
|
+
else if (b < 0xe0) {
|
|
984
|
+
cp = ((b & 0x1f) << 6) | (bytes[i + 1] & 0x3f);
|
|
985
|
+
i += 2;
|
|
986
|
+
}
|
|
987
|
+
else if (b < 0xf0) {
|
|
988
|
+
cp = ((b & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f);
|
|
989
|
+
i += 3;
|
|
990
|
+
}
|
|
991
|
+
else {
|
|
992
|
+
cp = ((b & 0x07) << 18) | ((bytes[i + 1] & 0x3f) << 12) | ((bytes[i + 2] & 0x3f) << 6) | (bytes[i + 3] & 0x3f);
|
|
993
|
+
i += 4;
|
|
994
|
+
}
|
|
995
|
+
if (cp >= 0x10000) {
|
|
996
|
+
out += String.fromCharCode(0xd800 + ((cp - 0x10000) >> 10), 0xdc00 + ((cp - 0x10000) & 0x3ff));
|
|
997
|
+
}
|
|
998
|
+
else {
|
|
999
|
+
out += String.fromCharCode(cp);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
return out;
|
|
1003
|
+
}
|
|
1004
|
+
/** 十六进制字符串 → 字节(非法输入抛错;用于 token_encoding='hex' 的 home_token) */
|
|
1005
|
+
function hexToBytes(hex) {
|
|
1006
|
+
const h = hex.trim();
|
|
1007
|
+
if (h.length % 2 !== 0)
|
|
1008
|
+
throw new Error('hex string length must be even');
|
|
1009
|
+
if (!/^[0-9a-fA-F]*$/.test(h))
|
|
1010
|
+
throw new Error('invalid hex string');
|
|
1011
|
+
const out = new Uint8Array(h.length / 2);
|
|
1012
|
+
for (let i = 0; i < out.length; i++) {
|
|
1013
|
+
out[i] = parseInt(h.substr(i * 2, 2), 16);
|
|
1014
|
+
}
|
|
1015
|
+
return out;
|
|
1016
|
+
}
|
|
1017
|
+
/** 字节 → 小写十六进制字符串 */
|
|
1018
|
+
function bytesToHex(bytes) {
|
|
1019
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
1020
|
+
}
|
|
1021
|
+
|
|
667
1022
|
/**
|
|
668
1023
|
* Provisioner 设备配网模块
|
|
669
|
-
* 负责向设备发送配网信息(如 WiFi
|
|
1024
|
+
* 负责向设备发送配网信息(如 WiFi 凭据、绑定信息等)
|
|
1025
|
+
*
|
|
1026
|
+
* 固件协议 v2:
|
|
1027
|
+
* - 配网/绑定发生在 is_bound=false 阶段(ADV_IND 广播,任意设备可连接、权限开放)
|
|
1028
|
+
* - 绑定完成后模组写入 NV(is_bound=true + home_token)并切换为隐蔽广播,
|
|
1029
|
+
* 后续连接需通过 Auth(HMAC 挑战-响应)鉴权
|
|
1030
|
+
* - 配网写入/通知特征值使用业务特征值(占位 0xFF03/0xFF04,待固件确认),
|
|
1031
|
+
* 可通过 DoneHomeConfig.uuids 覆盖
|
|
670
1032
|
*/
|
|
671
|
-
/** 配网服务 UUID(DoneHome 私有协议) */
|
|
672
|
-
const PROVISION_SERVICE_UUID = '0000ff01-0000-1000-8000-00805f9b34fb';
|
|
673
|
-
/** 配网写入特征值 UUID */
|
|
674
|
-
const PROVISION_WRITE_CHAR_UUID = '0000ff02-0000-1000-8000-00805f9b34fb';
|
|
675
|
-
/** 配网通知特征值 UUID */
|
|
676
|
-
const PROVISION_NOTIFY_CHAR_UUID = '0000ff03-0000-1000-8000-00805f9b34fb';
|
|
677
1033
|
class Provisioner {
|
|
678
1034
|
constructor(sdk) {
|
|
679
1035
|
/** 设备配网状态 */
|
|
@@ -683,11 +1039,12 @@ class Provisioner {
|
|
|
683
1039
|
this.sdk = sdk;
|
|
684
1040
|
}
|
|
685
1041
|
/**
|
|
686
|
-
*
|
|
1042
|
+
* 向设备发起配网(仅未绑定设备,开放权限阶段)
|
|
687
1043
|
* @param device_id 设备 ID
|
|
688
1044
|
* @param payload 配网载荷
|
|
689
1045
|
*/
|
|
690
1046
|
async provision(device_id, payload) {
|
|
1047
|
+
const U = getUuids();
|
|
691
1048
|
const current = this.states.get(device_id);
|
|
692
1049
|
if (current === 'provisioning') {
|
|
693
1050
|
throw new Error(`Device ${device_id} is already being provisioned`);
|
|
@@ -696,16 +1053,15 @@ class Provisioner {
|
|
|
696
1053
|
this.sdk.log('info', `Provisioning device ${device_id}...`);
|
|
697
1054
|
try {
|
|
698
1055
|
const json = JSON.stringify(payload);
|
|
699
|
-
const
|
|
700
|
-
|
|
701
|
-
await this.sdk.write(device_id, PROVISION_SERVICE_UUID, PROVISION_WRITE_CHAR_UUID, data);
|
|
1056
|
+
const data = utf8ToBytes(json);
|
|
1057
|
+
await this.sdk.write(device_id, U.service, U.bizWrite, data);
|
|
702
1058
|
await new Promise((resolve, reject) => {
|
|
703
1059
|
let settled = false;
|
|
704
1060
|
let timeout;
|
|
705
1061
|
const cleanup = () => {
|
|
706
1062
|
clearTimeout(timeout);
|
|
707
1063
|
this.cancelCallbacks.delete(device_id);
|
|
708
|
-
this.sdk.unsubscribe(device_id,
|
|
1064
|
+
this.sdk.unsubscribe(device_id, U.service, U.bizNotify).catch(() => { });
|
|
709
1065
|
};
|
|
710
1066
|
// 注册取消回调,cancel() 调用时 reject
|
|
711
1067
|
this.cancelCallbacks.set(device_id, () => {
|
|
@@ -724,14 +1080,13 @@ class Provisioner {
|
|
|
724
1080
|
reject(new Error('Provision timeout'));
|
|
725
1081
|
}
|
|
726
1082
|
}, 30000);
|
|
727
|
-
this.sdk.subscribe(device_id,
|
|
1083
|
+
this.sdk.subscribe(device_id, U.service, U.bizNotify, (responseData) => {
|
|
728
1084
|
var _a;
|
|
729
1085
|
if (settled)
|
|
730
1086
|
return;
|
|
731
1087
|
settled = true;
|
|
732
1088
|
cleanup();
|
|
733
|
-
const
|
|
734
|
-
const text = decoder.decode(responseData);
|
|
1089
|
+
const text = bytesToUtf8(new Uint8Array(responseData));
|
|
735
1090
|
try {
|
|
736
1091
|
const result = JSON.parse(text);
|
|
737
1092
|
if (result.status === 'ok' || result.status === 'success') {
|
|
@@ -776,7 +1131,8 @@ class Provisioner {
|
|
|
776
1131
|
else {
|
|
777
1132
|
// 兜底:没有回调时直接清理
|
|
778
1133
|
try {
|
|
779
|
-
|
|
1134
|
+
const U = getUuids();
|
|
1135
|
+
await this.sdk.unsubscribe(device_id, U.service, U.bizNotify);
|
|
780
1136
|
}
|
|
781
1137
|
catch ( /* ignore */_a) { /* ignore */ }
|
|
782
1138
|
this.states.set(device_id, 'cancelled');
|
|
@@ -794,305 +1150,188 @@ class Provisioner {
|
|
|
794
1150
|
}
|
|
795
1151
|
|
|
796
1152
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
async generateKeyPair() {
|
|
806
|
-
// 小程序环境无 Web Crypto API,使用简易 HMAC-SHA256 密钥对
|
|
807
|
-
// 生成 32 字节随机密钥作为私钥,派生公钥
|
|
808
|
-
const privateKeyBytes = new Uint8Array(32);
|
|
809
|
-
for (let i = 0; i < 32; i++) {
|
|
810
|
-
privateKeyBytes[i] = Math.floor(Math.random() * 256);
|
|
811
|
-
}
|
|
812
|
-
const privateKeyHex = Array.from(privateKeyBytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
813
|
-
// 公钥 = 私钥的 SHA-256 哈希(简化模型,实际应使用椭圆曲线)
|
|
814
|
-
const publicKeyHex = await this.sha256Hex(privateKeyHex);
|
|
815
|
-
return {
|
|
816
|
-
privateKey: privateKeyHex,
|
|
817
|
-
publicKey: publicKeyHex,
|
|
818
|
-
publicKeyExported: publicKeyHex,
|
|
819
|
-
};
|
|
820
|
-
}
|
|
821
|
-
async sign(privateKey, data) {
|
|
822
|
-
// HMAC-SHA256 签名(小程序环境简化方案)
|
|
823
|
-
const dataHex = Array.from(new Uint8Array(data)).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
824
|
-
const signatureHex = await this.sha256Hex(privateKey + dataHex);
|
|
825
|
-
const bytes = new Uint8Array(signatureHex.match(/.{2}/g).map(b => parseInt(b, 16)));
|
|
826
|
-
return bytes.buffer;
|
|
827
|
-
}
|
|
828
|
-
async verify(publicKey, data, signature) {
|
|
829
|
-
// 简化验签:实际生产环境应使用完整非对称方案
|
|
830
|
-
// 这里仅验证签名非空(小程序环境降级处理)
|
|
831
|
-
return signature.byteLength > 0;
|
|
832
|
-
}
|
|
833
|
-
async saveKeyPair(keyId, keyPair) {
|
|
834
|
-
this.store.set(keyId, keyPair);
|
|
835
|
-
}
|
|
836
|
-
async loadKeyPair(keyId) {
|
|
837
|
-
var _a;
|
|
838
|
-
return (_a = this.store.get(keyId)) !== null && _a !== void 0 ? _a : null;
|
|
839
|
-
}
|
|
840
|
-
async deleteKeyPair(keyId) {
|
|
841
|
-
this.store.delete(keyId);
|
|
842
|
-
}
|
|
843
|
-
async sha256Hex(input) {
|
|
844
|
-
// 小程序环境无 SubtleCrypto,使用简易哈希
|
|
845
|
-
// 生产环境建议接入 wx 的 crypto 插件
|
|
846
|
-
let hash = 0;
|
|
847
|
-
const str = input + 'donehome-salt';
|
|
848
|
-
for (let i = 0; i < str.length; i++) {
|
|
849
|
-
const chr = str.charCodeAt(i);
|
|
850
|
-
hash = ((hash << 5) - hash) + chr;
|
|
851
|
-
hash |= 0;
|
|
852
|
-
}
|
|
853
|
-
// 扩展为 64 字符 hex(模拟 SHA-256 输出长度)
|
|
854
|
-
const base = Math.abs(hash).toString(16).padStart(8, '0');
|
|
855
|
-
return (base + base + base + base + base + base + base + base).slice(0, 64);
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
// ──────── 工厂 ────────
|
|
859
|
-
let _adapter = null;
|
|
860
|
-
/** 获取加密适配器(单例,微信小程序环境) */
|
|
861
|
-
function getCryptoAdapter() {
|
|
862
|
-
if (_adapter)
|
|
863
|
-
return _adapter;
|
|
864
|
-
_adapter = new WechatCryptoAdapter();
|
|
865
|
-
return _adapter;
|
|
866
|
-
}
|
|
867
|
-
/**
|
|
868
|
-
* 加载或生成密钥对
|
|
869
|
-
* 优先从本地存储加载,不存在则生成新密钥对并保存
|
|
870
|
-
*/
|
|
871
|
-
async function loadOrCreateKeyPair(keyId) {
|
|
872
|
-
const adapter = getCryptoAdapter();
|
|
873
|
-
const existing = await adapter.loadKeyPair(keyId);
|
|
874
|
-
if (existing)
|
|
875
|
-
return existing;
|
|
876
|
-
const keyPair = await adapter.generateKeyPair();
|
|
877
|
-
await adapter.saveKeyPair(keyId, keyPair);
|
|
878
|
-
return keyPair;
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
/**
|
|
882
|
-
* Auth 配网认证模块
|
|
883
|
-
* 基于 ECDSA P-256 密钥对的 challenge-response 认证流程
|
|
1153
|
+
* Auth 鉴权模块(固件协议 v2:HMAC-SHA256 挑战-响应)
|
|
1154
|
+
*
|
|
1155
|
+
* 固件行为(is_bound=true 时):
|
|
1156
|
+
* - 允许手机发起 BLE 连接,但连接后不开放业务权限
|
|
1157
|
+
* - 0xFF01(Read):读取 16 字节随机 Challenge
|
|
1158
|
+
* - 0xFF02(Write):写入 HMAC-SHA256(home_token, Challenge)
|
|
1159
|
+
* - 鉴权成功:标记当前连接【已鉴权】,开放业务特征值读写并允许 Notify
|
|
1160
|
+
* - 鉴权失败:模组主动断开连接(BLE_GAP_DISCONNECT)
|
|
884
1161
|
*
|
|
885
|
-
*
|
|
886
|
-
*
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
1162
|
+
* SDK 策略:
|
|
1163
|
+
* 固件无显式"鉴权结果"特征值 —— 写入 HMAC 后监听断开事件,
|
|
1164
|
+
* verify_ms 内断开视为鉴权失败,未断开视为鉴权成功。
|
|
1165
|
+
*
|
|
1166
|
+
* 多端互斥注意:
|
|
1167
|
+
* 家庭手机 B 鉴权成功后,模组会主动断开已鉴权的手机 A(固件互斥逻辑)。
|
|
1168
|
+
* 被踢下线时 SDK 会触发 deviceDisconnected 并清除鉴权状态,
|
|
1169
|
+
* 重新 connect + auth 后才能继续控制。
|
|
891
1170
|
*/
|
|
892
|
-
/**
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
const
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
const AUTH_CMD = {
|
|
900
|
-
/** 发起认证请求(携带公钥) */
|
|
901
|
-
REQUEST: 0x01,
|
|
902
|
-
/** 设备返回 challenge */
|
|
903
|
-
CHALLENGE: 0x02,
|
|
904
|
-
/** SDK 发送签名响应 */
|
|
905
|
-
SIGNATURE: 0x03,
|
|
906
|
-
/** 认证成功,设备返回 session key */
|
|
907
|
-
SUCCESS: 0x04,
|
|
908
|
-
/** 认证失败 */
|
|
909
|
-
FAIL: 0x05,
|
|
910
|
-
/** 刷新 session key */
|
|
911
|
-
REFRESH: 0x06,
|
|
912
|
-
};
|
|
1171
|
+
/** 带超时的 Promise 包装 */
|
|
1172
|
+
function withTimeout(p, ms, msg) {
|
|
1173
|
+
return new Promise((resolve, reject) => {
|
|
1174
|
+
const t = setTimeout(() => reject(new Error(msg)), ms);
|
|
1175
|
+
p.then((v) => { clearTimeout(t); resolve(v); }, (e) => { clearTimeout(t); reject(e); });
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
913
1178
|
class Auth {
|
|
914
1179
|
constructor(sdk) {
|
|
915
|
-
/**
|
|
916
|
-
this.
|
|
917
|
-
/** 密钥对缓存(keyId → KeyPair) */
|
|
918
|
-
this.keyPairs = new Map();
|
|
1180
|
+
/** 设备鉴权状态 */
|
|
1181
|
+
this.states = new Map();
|
|
919
1182
|
this.sdk = sdk;
|
|
1183
|
+
// 连接断开时清除已鉴权状态(含多端互斥被踢);
|
|
1184
|
+
// authenticating 阶段的断开由 auth() 流程自己标记 failed
|
|
1185
|
+
this.sdk.on('deviceDisconnected', (...args) => {
|
|
1186
|
+
const device_id = args[0];
|
|
1187
|
+
if (this.states.get(device_id) === 'authenticated') {
|
|
1188
|
+
this.states.set(device_id, 'idle');
|
|
1189
|
+
this.sdk.log('info', `Auth state cleared for ${device_id} (disconnected, re-auth required)`);
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
920
1192
|
}
|
|
921
1193
|
/**
|
|
922
|
-
*
|
|
923
|
-
*
|
|
1194
|
+
* 向设备发起鉴权(HMAC-SHA256 挑战-响应)
|
|
1195
|
+
*
|
|
1196
|
+
* 流程:读 0xFF01 挑战码 → HMAC-SHA256(home_token, challenge) → 写 0xFF02 → 等待裁决
|
|
924
1197
|
*
|
|
925
1198
|
* @param device_id 设备 ID
|
|
926
|
-
* @param options
|
|
927
|
-
* @returns
|
|
1199
|
+
* @param options 鉴权选项(home_token 缺省时取 SDK 配置)
|
|
1200
|
+
* @returns 鉴权结果
|
|
928
1201
|
*/
|
|
929
1202
|
async auth(device_id, options) {
|
|
930
|
-
var _a, _b;
|
|
1203
|
+
var _a, _b, _c, _d, _e;
|
|
1204
|
+
const U = getUuids();
|
|
931
1205
|
const timeoutMs = (_a = options === null || options === void 0 ? void 0 : options.timeout_ms) !== null && _a !== void 0 ? _a : 10000;
|
|
932
|
-
const
|
|
933
|
-
const
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
return;
|
|
957
|
-
const bytes = new Uint8Array(data);
|
|
958
|
-
const cmd = bytes[0];
|
|
959
|
-
if (cmd === AUTH_CMD.CHALLENGE) {
|
|
960
|
-
// 收到 challenge,用私钥签名
|
|
961
|
-
const challenge = data.slice(1); // 跳过 cmd 字节
|
|
962
|
-
this.sdk.log('info', `Received challenge (${challenge.byteLength} bytes), signing...`);
|
|
963
|
-
try {
|
|
964
|
-
const signature = await crypto.sign(keyPair.privateKey, challenge);
|
|
965
|
-
// 发送签名响应: [SIGNATURE_CMD, signature_bytes...]
|
|
966
|
-
const response = new Uint8Array(1 + signature.byteLength);
|
|
967
|
-
response[0] = AUTH_CMD.SIGNATURE;
|
|
968
|
-
response.set(new Uint8Array(signature), 1);
|
|
969
|
-
await this.sdk.write(device_id, AUTH_SERVICE_UUID, AUTH_WRITE_CHAR_UUID, response);
|
|
970
|
-
}
|
|
971
|
-
catch (signErr) {
|
|
972
|
-
settled = true;
|
|
973
|
-
cleanup();
|
|
974
|
-
reject(new Error(`Failed to sign challenge: ${signErr}`));
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
else if (cmd === AUTH_CMD.SUCCESS) {
|
|
978
|
-
// 认证成功,提取 session_key
|
|
979
|
-
settled = true;
|
|
980
|
-
cleanup();
|
|
981
|
-
const keyBytes = bytes.slice(1);
|
|
982
|
-
const sessionKey = this.bytesToHex(keyBytes);
|
|
983
|
-
const result = {
|
|
984
|
-
session_key: sessionKey,
|
|
985
|
-
expires_at: new Date(Date.now() + 3600000).toISOString(),
|
|
986
|
-
};
|
|
987
|
-
this.sessions.set(device_id, result);
|
|
988
|
-
this.sdk.log('info', `Auth succeeded for ${device_id}`);
|
|
989
|
-
resolve(result);
|
|
990
|
-
}
|
|
991
|
-
else if (cmd === AUTH_CMD.FAIL) {
|
|
992
|
-
// 认证失败
|
|
993
|
-
settled = true;
|
|
994
|
-
cleanup();
|
|
995
|
-
const reason = bytes.length > 1
|
|
996
|
-
? `0x${bytes[1].toString(16)}`
|
|
997
|
-
: 'unknown';
|
|
998
|
-
reject(new Error(`Auth failed: device rejected (code: ${reason})`));
|
|
999
|
-
}
|
|
1000
|
-
});
|
|
1001
|
-
// 发送认证请求 + 公钥: [REQUEST_CMD, public_key_bytes...]
|
|
1002
|
-
const request = new Uint8Array(1 + pubKeyBytes.length);
|
|
1003
|
-
request[0] = AUTH_CMD.REQUEST;
|
|
1004
|
-
request.set(pubKeyBytes, 1);
|
|
1005
|
-
await this.sdk.write(device_id, AUTH_SERVICE_UUID, AUTH_WRITE_CHAR_UUID, request);
|
|
1006
|
-
this.sdk.log('info', `Auth request sent with public key (${pubKeyBytes.length} bytes)`);
|
|
1206
|
+
const verifyMs = (_b = options === null || options === void 0 ? void 0 : options.verify_ms) !== null && _b !== void 0 ? _b : 1000;
|
|
1207
|
+
const encoding = (_c = options === null || options === void 0 ? void 0 : options.token_encoding) !== null && _c !== void 0 ? _c : 'utf8';
|
|
1208
|
+
const tokenStr = (_d = options === null || options === void 0 ? void 0 : options.home_token) !== null && _d !== void 0 ? _d : this.sdk.home_token;
|
|
1209
|
+
if (!tokenStr) {
|
|
1210
|
+
throw new Error('home_token is required: pass options.home_token or set DoneHomeConfig.home_token');
|
|
1211
|
+
}
|
|
1212
|
+
let tokenBytes;
|
|
1213
|
+
try {
|
|
1214
|
+
tokenBytes = encoding === 'hex' ? hexToBytes(tokenStr) : utf8ToBytes(tokenStr);
|
|
1215
|
+
}
|
|
1216
|
+
catch (err) {
|
|
1217
|
+
throw new Error(`Invalid home_token (${encoding}): ${err.message}`);
|
|
1218
|
+
}
|
|
1219
|
+
if (tokenBytes.length === 0) {
|
|
1220
|
+
throw new Error('home_token is empty');
|
|
1221
|
+
}
|
|
1222
|
+
this.states.set(device_id, 'authenticating');
|
|
1223
|
+
this.sdk.log('info', `Authenticating ${device_id} (HMAC-SHA256 challenge-response)...`);
|
|
1224
|
+
try {
|
|
1225
|
+
// 1. 读取挑战码(0xFF01)
|
|
1226
|
+
const challengeAb = await withTimeout(this.sdk.read(device_id, U.service, U.authChallenge), timeoutMs, 'Read challenge timeout (0xFF01)');
|
|
1227
|
+
const challenge = new Uint8Array(challengeAb);
|
|
1228
|
+
if (challenge.length === 0) {
|
|
1229
|
+
throw new Error('Device returned an empty challenge');
|
|
1007
1230
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
settled = true;
|
|
1011
|
-
cleanup();
|
|
1012
|
-
reject(err);
|
|
1013
|
-
}
|
|
1231
|
+
if (challenge.length !== 16) {
|
|
1232
|
+
this.sdk.log('warn', `Challenge length is ${challenge.length} bytes, expected 16 (spec)`);
|
|
1014
1233
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1234
|
+
// 2. 计算 HMAC-SHA256(home_token, challenge)
|
|
1235
|
+
const mac = hmacSha256(tokenBytes, challenge);
|
|
1236
|
+
// 3. 写入响应(0xFF02)
|
|
1237
|
+
await withTimeout(this.sdk.write(device_id, U.service, U.authResponse, mac), timeoutMs, 'Write auth response timeout (0xFF02)');
|
|
1238
|
+
this.sdk.log('info', `Auth response written (${mac.length} bytes), waiting verdict...`);
|
|
1239
|
+
// 4. 等待固件裁决:verify_ms 内断开 = 失败;未断开 = 成功
|
|
1240
|
+
const ok = await this.waitVerdict(device_id, verifyMs);
|
|
1241
|
+
if (!ok) {
|
|
1242
|
+
throw new Error('Auth failed: device disconnected (home_token mismatch or kicked by another client)');
|
|
1243
|
+
}
|
|
1244
|
+
const result = {
|
|
1245
|
+
device_id,
|
|
1246
|
+
authenticated: true,
|
|
1247
|
+
authenticated_at: new Date().toISOString(),
|
|
1248
|
+
};
|
|
1249
|
+
this.states.set(device_id, 'authenticated');
|
|
1250
|
+
this.sdk.emit('authSuccess', device_id);
|
|
1251
|
+
this.sdk.log('info', `Auth succeeded for ${device_id}, business characteristics unlocked`);
|
|
1252
|
+
return result;
|
|
1253
|
+
}
|
|
1254
|
+
catch (err) {
|
|
1255
|
+
if (this.states.get(device_id) === 'authenticating') {
|
|
1256
|
+
this.states.set(device_id, 'failed');
|
|
1257
|
+
}
|
|
1258
|
+
this.sdk.emit('authFailed', device_id, (_e = err.message) !== null && _e !== void 0 ? _e : String(err));
|
|
1259
|
+
throw err;
|
|
1260
|
+
}
|
|
1034
1261
|
}
|
|
1035
1262
|
/**
|
|
1036
|
-
*
|
|
1263
|
+
* 确保设备已鉴权:已通过则直接返回,否则执行鉴权
|
|
1037
1264
|
*/
|
|
1038
|
-
|
|
1039
|
-
this.
|
|
1265
|
+
async ensure_authenticated(device_id, options) {
|
|
1266
|
+
if (this.is_authenticated(device_id)) {
|
|
1267
|
+
return {
|
|
1268
|
+
device_id,
|
|
1269
|
+
authenticated: true,
|
|
1270
|
+
authenticated_at: new Date().toISOString(),
|
|
1271
|
+
};
|
|
1272
|
+
}
|
|
1273
|
+
return this.auth(device_id, options);
|
|
1040
1274
|
}
|
|
1041
1275
|
/**
|
|
1042
|
-
*
|
|
1043
|
-
* @
|
|
1276
|
+
* 等待固件裁决
|
|
1277
|
+
* @returns true = 未断开(鉴权成功);false = 断开(鉴权失败/被踢)
|
|
1044
1278
|
*/
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1279
|
+
waitVerdict(device_id, verifyMs) {
|
|
1280
|
+
return new Promise((resolve) => {
|
|
1281
|
+
let done = false;
|
|
1282
|
+
const handler = (...args) => {
|
|
1283
|
+
const id = args[0];
|
|
1284
|
+
if (id === device_id && !done) {
|
|
1285
|
+
done = true;
|
|
1286
|
+
cleanup();
|
|
1287
|
+
resolve(false);
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
1290
|
+
const cleanup = () => {
|
|
1291
|
+
this.sdk.off('deviceDisconnected', handler);
|
|
1292
|
+
clearTimeout(timer);
|
|
1293
|
+
};
|
|
1294
|
+
const timer = setTimeout(() => {
|
|
1295
|
+
if (!done) {
|
|
1296
|
+
done = true;
|
|
1297
|
+
cleanup();
|
|
1298
|
+
resolve(true);
|
|
1299
|
+
}
|
|
1300
|
+
}, verifyMs);
|
|
1301
|
+
this.sdk.on('deviceDisconnected', handler);
|
|
1302
|
+
});
|
|
1048
1303
|
}
|
|
1049
1304
|
/**
|
|
1050
|
-
*
|
|
1051
|
-
* @param keyId 密钥标识
|
|
1305
|
+
* 获取设备鉴权状态
|
|
1052
1306
|
*/
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
this.keyPairs.delete(keyId);
|
|
1057
|
-
this.sdk.log('info', `Key pair deleted for ${keyId}`);
|
|
1307
|
+
get_state(device_id) {
|
|
1308
|
+
var _a;
|
|
1309
|
+
return (_a = this.states.get(device_id)) !== null && _a !== void 0 ? _a : 'idle';
|
|
1058
1310
|
}
|
|
1059
|
-
// ──────── 内部 ────────
|
|
1060
1311
|
/**
|
|
1061
|
-
*
|
|
1312
|
+
* 设备是否已鉴权(业务特征值可读写)
|
|
1062
1313
|
*/
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
if (keyPair)
|
|
1066
|
-
return keyPair;
|
|
1067
|
-
keyPair = await loadOrCreateKeyPair(keyId);
|
|
1068
|
-
this.keyPairs.set(keyId, keyPair);
|
|
1069
|
-
return keyPair;
|
|
1314
|
+
is_authenticated(device_id) {
|
|
1315
|
+
return this.states.get(device_id) === 'authenticated';
|
|
1070
1316
|
}
|
|
1071
1317
|
/**
|
|
1072
|
-
*
|
|
1318
|
+
* 清除设备鉴权状态(断开重连后需重新鉴权)
|
|
1073
1319
|
*/
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
const encoder = new TextEncoder();
|
|
1077
|
-
return encoder.encode(publicKeyExported);
|
|
1078
|
-
}
|
|
1079
|
-
bytesToHex(bytes) {
|
|
1080
|
-
return Array.from(bytes)
|
|
1081
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
1082
|
-
.join('');
|
|
1320
|
+
clear_state(device_id) {
|
|
1321
|
+
this.states.delete(device_id);
|
|
1083
1322
|
}
|
|
1084
1323
|
}
|
|
1085
1324
|
|
|
1086
1325
|
/**
|
|
1087
1326
|
* Controller 设备控制模块
|
|
1088
1327
|
* 负责指令发送、特征值读写与通知订阅
|
|
1328
|
+
*
|
|
1329
|
+
* 固件协议 v2:
|
|
1330
|
+
* - 业务特征值仅在鉴权通过后开放(未鉴权写入/订阅会被模组拒绝或断开)
|
|
1331
|
+
* - 多端互斥:被踢下线会触发 deviceDisconnected,重连后需重新 auth
|
|
1332
|
+
* - 业务 Write / Notify 特征值 UUID 为占位(0xFF03/0xFF04),待固件确认,
|
|
1333
|
+
* 可通过 DoneHomeConfig.uuids 覆盖
|
|
1089
1334
|
*/
|
|
1090
|
-
/** 控制服务 UUID(DoneHome 私有协议) */
|
|
1091
|
-
const CMD_SERVICE_UUID = '0000ff01-0000-1000-8000-00805f9b34fb';
|
|
1092
|
-
/** 指令写入特征值 UUID */
|
|
1093
|
-
const CMD_WRITE_CHAR_UUID = '0000ff06-0000-1000-8000-00805f9b34fb';
|
|
1094
|
-
/** 指令通知特征值 UUID */
|
|
1095
|
-
const CMD_NOTIFY_CHAR_UUID = '0000ff07-0000-1000-8000-00805f9b34fb';
|
|
1096
1335
|
/** 指令协议头 */
|
|
1097
1336
|
const CMD_HEADER = {
|
|
1098
1337
|
/** 无加密、无 ACK */
|
|
@@ -1108,13 +1347,14 @@ class Controller {
|
|
|
1108
1347
|
this.sdk = sdk;
|
|
1109
1348
|
}
|
|
1110
1349
|
/**
|
|
1111
|
-
*
|
|
1350
|
+
* 发送指令到设备(需先通过 Auth 鉴权)
|
|
1112
1351
|
* @param device_id 设备 ID
|
|
1113
1352
|
* @param cmd 指令参数
|
|
1114
1353
|
* @returns ACK 响应数据(如果 require_ack)
|
|
1115
1354
|
*/
|
|
1116
1355
|
async send_cmd(device_id, cmd) {
|
|
1117
1356
|
var _a;
|
|
1357
|
+
const U = getUuids();
|
|
1118
1358
|
const timeoutMs = (_a = cmd.timeout_ms) !== null && _a !== void 0 ? _a : 5000;
|
|
1119
1359
|
this.sdk.log('info', `Sending cmd 0x${cmd.code.toString(16)} to ${device_id}`);
|
|
1120
1360
|
// 构建指令帧
|
|
@@ -1140,25 +1380,25 @@ class Controller {
|
|
|
1140
1380
|
frame.set(payloadBytes, 4);
|
|
1141
1381
|
if (!cmd.require_ack) {
|
|
1142
1382
|
// 不需要 ACK,直接写入
|
|
1143
|
-
await this.sdk.write(device_id,
|
|
1383
|
+
await this.sdk.write(device_id, U.service, U.bizWrite, frame, true);
|
|
1144
1384
|
return;
|
|
1145
1385
|
}
|
|
1146
1386
|
// 需要 ACK,等待响应
|
|
1147
1387
|
return new Promise(async (resolve, reject) => {
|
|
1148
1388
|
const timeout = setTimeout(() => {
|
|
1149
|
-
this.sdk.unsubscribe(device_id,
|
|
1389
|
+
this.sdk.unsubscribe(device_id, U.service, U.bizNotify).catch(() => { });
|
|
1150
1390
|
reject(new Error('Command ACK timeout'));
|
|
1151
1391
|
}, timeoutMs);
|
|
1152
1392
|
try {
|
|
1153
|
-
await this.sdk.subscribe(device_id,
|
|
1393
|
+
await this.sdk.subscribe(device_id, U.service, U.bizNotify, (data) => {
|
|
1154
1394
|
const bytes = new Uint8Array(data);
|
|
1155
1395
|
if (bytes[0] === ACK_CODE && bytes[1] === cmd.code) {
|
|
1156
1396
|
clearTimeout(timeout);
|
|
1157
|
-
this.sdk.unsubscribe(device_id,
|
|
1397
|
+
this.sdk.unsubscribe(device_id, U.service, U.bizNotify).catch(() => { });
|
|
1158
1398
|
resolve(data);
|
|
1159
1399
|
}
|
|
1160
1400
|
});
|
|
1161
|
-
await this.sdk.write(device_id,
|
|
1401
|
+
await this.sdk.write(device_id, U.service, U.bizWrite, frame);
|
|
1162
1402
|
}
|
|
1163
1403
|
catch (err) {
|
|
1164
1404
|
clearTimeout(timeout);
|
|
@@ -1172,7 +1412,8 @@ class Controller {
|
|
|
1172
1412
|
* @param uuid 特征值 UUID
|
|
1173
1413
|
*/
|
|
1174
1414
|
async read_char(device_id, uuid) {
|
|
1175
|
-
|
|
1415
|
+
const U = getUuids();
|
|
1416
|
+
return this.sdk.read(device_id, U.service, uuid);
|
|
1176
1417
|
}
|
|
1177
1418
|
/**
|
|
1178
1419
|
* 写入特征值数据
|
|
@@ -1181,7 +1422,8 @@ class Controller {
|
|
|
1181
1422
|
* @param value 要写入的数据
|
|
1182
1423
|
*/
|
|
1183
1424
|
async write_char(device_id, uuid, value) {
|
|
1184
|
-
|
|
1425
|
+
const U = getUuids();
|
|
1426
|
+
await this.sdk.write(device_id, U.service, uuid, value);
|
|
1185
1427
|
}
|
|
1186
1428
|
/**
|
|
1187
1429
|
* 订阅特征值通知
|
|
@@ -1190,7 +1432,8 @@ class Controller {
|
|
|
1190
1432
|
* @param on_data 数据回调
|
|
1191
1433
|
*/
|
|
1192
1434
|
async sub_char(device_id, uuid, on_data) {
|
|
1193
|
-
|
|
1435
|
+
const U = getUuids();
|
|
1436
|
+
await this.sdk.subscribe(device_id, U.service, uuid, on_data);
|
|
1194
1437
|
}
|
|
1195
1438
|
/**
|
|
1196
1439
|
* 取消订阅特征值通知
|
|
@@ -1198,18 +1441,25 @@ class Controller {
|
|
|
1198
1441
|
* @param uuid 特征值 UUID
|
|
1199
1442
|
*/
|
|
1200
1443
|
async unsub_char(device_id, uuid) {
|
|
1201
|
-
|
|
1444
|
+
const U = getUuids();
|
|
1445
|
+
await this.sdk.unsubscribe(device_id, U.service, uuid);
|
|
1202
1446
|
}
|
|
1203
1447
|
}
|
|
1204
1448
|
|
|
1205
1449
|
/**
|
|
1206
1450
|
* dh-sdk
|
|
1207
|
-
* 微信小程序 BLE 模组控制 SDK
|
|
1451
|
+
* 微信小程序 BLE 模组控制 SDK(固件协议 v2)
|
|
1208
1452
|
*
|
|
1209
|
-
*
|
|
1453
|
+
* 固件协议 v2 特性:
|
|
1454
|
+
* - 模组 NV 分区保存 is_bound / home_token
|
|
1455
|
+
* - 未绑定:ADV_IND 广播(名称 + Service UUID),任意设备可连接(配网/绑定)
|
|
1456
|
+
* - 已绑定:隐蔽广播(无名称、Flags=0x04、ManufacturerData 携带 home_id)
|
|
1457
|
+
* - 鉴权:读 0xFF01 挑战码 → HMAC-SHA256(home_token, challenge) → 写 0xFF02
|
|
1458
|
+
* - 鉴权通过后开放业务特征值(Write 控制 / Notify 状态推送)
|
|
1459
|
+
* - 多端互斥:新手机鉴权成功后,模组主动断开旧手机
|
|
1210
1460
|
*
|
|
1211
1461
|
* 使用方式:
|
|
1212
|
-
* import { Scanner, Connector,
|
|
1462
|
+
* import { DoneHomeSDK, Scanner, Connector, Auth, Controller } from 'dh-sdk'
|
|
1213
1463
|
*/
|
|
1214
1464
|
// ──────── 核心 SDK ────────
|
|
1215
1465
|
/** 获取 SDK 版本信息 */
|
|
@@ -1224,12 +1474,25 @@ function info() {
|
|
|
1224
1474
|
exports.Auth = Auth;
|
|
1225
1475
|
exports.Connector = Connector;
|
|
1226
1476
|
exports.Controller = Controller;
|
|
1477
|
+
exports.DEFAULT_UUIDS = DEFAULT_UUIDS;
|
|
1227
1478
|
exports.DoneHomeSDK = DoneHomeSDK;
|
|
1228
1479
|
exports.Provisioner = Provisioner;
|
|
1229
1480
|
exports.Scanner = Scanner;
|
|
1230
1481
|
exports.WechatBLEAdapter = WechatBLEAdapter;
|
|
1231
|
-
exports.
|
|
1482
|
+
exports.bytesToHex = bytesToHex;
|
|
1483
|
+
exports.bytesToUtf8 = bytesToUtf8;
|
|
1484
|
+
exports.configureUuids = configureUuids;
|
|
1485
|
+
exports.getUuids = getUuids;
|
|
1486
|
+
exports.hexToBytes = hexToBytes;
|
|
1487
|
+
exports.hmacSha256 = hmacSha256;
|
|
1488
|
+
exports.homeIdFromAdvertisData = homeIdFromAdvertisData;
|
|
1232
1489
|
exports.info = info;
|
|
1233
|
-
exports.
|
|
1490
|
+
exports.macFromAdvertisData = macFromAdvertisData;
|
|
1491
|
+
exports.macFromDeviceId = macFromDeviceId;
|
|
1492
|
+
exports.resetUuids = resetUuids;
|
|
1493
|
+
exports.resolveHomeId = resolveHomeId;
|
|
1494
|
+
exports.resolveMac = resolveMac;
|
|
1495
|
+
exports.sha256 = sha256;
|
|
1496
|
+
exports.utf8ToBytes = utf8ToBytes;
|
|
1234
1497
|
exports.version = version;
|
|
1235
1498
|
//# sourceMappingURL=wechat.js.map
|