@onekeyfe/hd-transport-electron 1.2.0-alpha.69 → 1.2.0-alpha.70
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/dist/{index-bd1cfdb6.js → index-84fea839.js} +1 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.js +1 -1
- package/dist/{noble-ble-handler-7ac9c43d.js → noble-ble-handler-a0b2f06d.js} +31 -45
- package/dist/noble-ble-handler.d.ts.map +1 -1
- package/dist/types/desktop-api.d.ts +0 -5
- package/dist/types/desktop-api.d.ts.map +1 -1
- package/dist/types/noble-extended.d.ts +0 -1
- package/dist/types/noble-extended.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/noble-ble-handler.ts +18 -49
- package/src/types/desktop-api.ts +1 -2
- package/src/types/noble-extended.ts +0 -1
- package/dist/ble-packet-capacity.d.ts +0 -2
- package/dist/ble-packet-capacity.d.ts.map +0 -1
- package/src/__tests__/ble-packet-capacity.test.ts +0 -13
- package/src/ble-packet-capacity.ts +0 -13
|
@@ -32,7 +32,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
32
32
|
|
|
33
33
|
function initNobleBleSupport(webContents) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-
|
|
35
|
+
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-a0b2f06d.js'); });
|
|
36
36
|
setupNobleBleHandlers(webContents);
|
|
37
37
|
});
|
|
38
38
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ interface DeviceInfo extends OneKeyDeviceInfoBase {
|
|
|
6
6
|
id: string;
|
|
7
7
|
name: string;
|
|
8
8
|
state: string;
|
|
9
|
-
mtu?: number;
|
|
10
9
|
}
|
|
11
10
|
interface CharacteristicPair {
|
|
12
11
|
write: Characteristic;
|
|
@@ -39,7 +38,6 @@ interface NobleBleAPI {
|
|
|
39
38
|
getDevice: (uuid: string) => Promise<{
|
|
40
39
|
id: string;
|
|
41
40
|
name: string;
|
|
42
|
-
mtu?: number;
|
|
43
41
|
} | null>;
|
|
44
42
|
connect: (uuid: string) => Promise<void>;
|
|
45
43
|
disconnect: (uuid: string) => Promise<void>;
|
|
@@ -47,10 +45,6 @@ interface NobleBleAPI {
|
|
|
47
45
|
unsubscribe: (uuid: string) => Promise<void>;
|
|
48
46
|
write: (uuid: string, data: string) => Promise<void>;
|
|
49
47
|
onNotification: (callback: (deviceId: string, data: string) => void) => () => void;
|
|
50
|
-
onMtuChanged?: (callback: (device: {
|
|
51
|
-
id: string;
|
|
52
|
-
mtu: number;
|
|
53
|
-
}) => void) => () => void;
|
|
54
48
|
onDeviceDisconnected: (callback: (device: {
|
|
55
49
|
id: string;
|
|
56
50
|
name: string;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var index = require('./index-
|
|
3
|
+
var index = require('./index-84fea839.js');
|
|
4
4
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
5
5
|
var pRetry = require('p-retry');
|
|
6
6
|
|
|
@@ -8,14 +8,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
8
8
|
|
|
9
9
|
var pRetry__default = /*#__PURE__*/_interopDefaultLegacy(pRetry);
|
|
10
10
|
|
|
11
|
-
const BLE_ATT_HEADER_BYTES = 3;
|
|
12
|
-
function resolveBlePacketCapacity(mtu, maximumPacketCapacity, fallbackPacketCapacity) {
|
|
13
|
-
if (typeof mtu !== 'number' || !Number.isFinite(mtu) || mtu <= BLE_ATT_HEADER_BYTES) {
|
|
14
|
-
return fallbackPacketCapacity;
|
|
15
|
-
}
|
|
16
|
-
return Math.min(maximumPacketCapacity, Math.floor(mtu) - BLE_ATT_HEADER_BYTES);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
11
|
function safeLog(logger, level, message, ...args) {
|
|
20
12
|
if (logger) {
|
|
21
13
|
logger[level](message, ...args);
|
|
@@ -106,7 +98,6 @@ const notificationCallbacks = new Map();
|
|
|
106
98
|
const subscribedDevices = new Map();
|
|
107
99
|
const subscriptionOperations = new Map();
|
|
108
100
|
const deviceDisconnectListeners = new Map();
|
|
109
|
-
const deviceMtuListeners = new Map();
|
|
110
101
|
const ONEKEY_SERVICE_UUIDS = [hdShared.ONEKEY_SERVICE_UUID];
|
|
111
102
|
const ONEKEY_SERVICE_UUID_ALIASES = hdShared.createKnownBleUuidAliases(hdShared.ONEKEY_SERVICE_UUID);
|
|
112
103
|
const ONEKEY_WRITE_UUID_ALIASES = hdShared.createKnownBleUuidAliases(hdShared.ONEKEY_WRITE_CHARACTERISTIC_UUID);
|
|
@@ -116,8 +107,8 @@ const DEVICE_SCAN_TIMEOUT = 5000;
|
|
|
116
107
|
const DEVICE_CHECK_INTERVAL = 500;
|
|
117
108
|
const SERVICE_DISCOVERY_TIMEOUT = 10000;
|
|
118
109
|
const BLE_CLEANUP_TIMEOUT = 250;
|
|
119
|
-
const
|
|
120
|
-
const
|
|
110
|
+
const BLE_PACKET_SIZE = 192;
|
|
111
|
+
const UNIFIED_WRITE_DELAY = 5;
|
|
121
112
|
const RETRY_CONFIG = { MAX_ATTEMPTS: 15, WRITE_TIMEOUT: 2000 };
|
|
122
113
|
const IS_WINDOWS = process.platform === 'win32';
|
|
123
114
|
const ABORTABLE_WRITE_ERROR_PATTERNS = [
|
|
@@ -309,11 +300,6 @@ function cleanupDevice(deviceId, webContents, options = {}) {
|
|
|
309
300
|
disconnectEntry.peripheral.removeListener('disconnect', disconnectEntry.listener);
|
|
310
301
|
deviceDisconnectListeners.delete(deviceId);
|
|
311
302
|
}
|
|
312
|
-
const mtuEntry = deviceMtuListeners.get(deviceId);
|
|
313
|
-
if (mtuEntry) {
|
|
314
|
-
mtuEntry.peripheral.removeListener('mtu', mtuEntry.listener);
|
|
315
|
-
deviceMtuListeners.delete(deviceId);
|
|
316
|
-
}
|
|
317
303
|
connectedDevices.delete(deviceId);
|
|
318
304
|
deviceCharacteristics.delete(deviceId);
|
|
319
305
|
notificationCallbacks.delete(deviceId);
|
|
@@ -357,25 +343,8 @@ function setupDisconnectListener(peripheral, deviceId, webContents) {
|
|
|
357
343
|
};
|
|
358
344
|
deviceDisconnectListeners.set(deviceId, { peripheral, listener });
|
|
359
345
|
peripheral.on('disconnect', listener);
|
|
360
|
-
setupMtuListener(peripheral, deviceId, webContents);
|
|
361
|
-
}
|
|
362
|
-
function setupMtuListener(peripheral, deviceId, webContents) {
|
|
363
|
-
const existing = deviceMtuListeners.get(deviceId);
|
|
364
|
-
if (existing) {
|
|
365
|
-
existing.peripheral.removeListener('mtu', existing.listener);
|
|
366
|
-
}
|
|
367
|
-
const listener = (mtu) => {
|
|
368
|
-
if (!Number.isFinite(mtu) || mtu <= 0)
|
|
369
|
-
return;
|
|
370
|
-
webContents.send(hdShared.EOneKeyBleMessageKeys.NOBLE_BLE_MTU_CHANGED, {
|
|
371
|
-
id: deviceId,
|
|
372
|
-
mtu,
|
|
373
|
-
});
|
|
374
|
-
};
|
|
375
|
-
deviceMtuListeners.set(deviceId, { peripheral, listener });
|
|
376
|
-
peripheral.on('mtu', listener);
|
|
377
346
|
}
|
|
378
|
-
function
|
|
347
|
+
function writeCharacteristicWithAck(deviceId, writeCharacteristic, buffer) {
|
|
379
348
|
return index.__awaiter(this, void 0, void 0, function* () {
|
|
380
349
|
return new Promise((resolve, reject) => {
|
|
381
350
|
writeCharacteristic.write(buffer, true, (error) => {
|
|
@@ -407,7 +376,7 @@ function attemptWindowsWriteUntilPaired(deviceId, doGetWriteCharacteristic, payl
|
|
|
407
376
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Write characteristic not available for ${deviceId}`);
|
|
408
377
|
}
|
|
409
378
|
try {
|
|
410
|
-
yield
|
|
379
|
+
yield writeCharacteristicWithAck(deviceId, latestWrite, payload);
|
|
411
380
|
}
|
|
412
381
|
catch (e) {
|
|
413
382
|
const errorMessage = e instanceof Error ? e.message : String(e);
|
|
@@ -475,37 +444,44 @@ function transmitHexDataToDevice(deviceId, hexData) {
|
|
|
475
444
|
}
|
|
476
445
|
const toBuffer = Buffer.from(hexData, 'hex');
|
|
477
446
|
const doGetWriteCharacteristic = () => { var _a; return (_a = deviceCharacteristics.get(deviceId)) === null || _a === void 0 ? void 0 : _a.write; };
|
|
478
|
-
const packetCapacity = resolveBlePacketCapacity(peripheral.mtu, BLE_PACKET_SIZE_MAXIMUM, BLE_PACKET_SIZE_FALLBACK);
|
|
479
447
|
if (!IS_WINDOWS || pairedDevices.has(deviceId)) {
|
|
480
448
|
const writeCharacteristic = doGetWriteCharacteristic();
|
|
481
449
|
if (!writeCharacteristic) {
|
|
482
450
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotFound, `Write characteristic not available for ${deviceId}`);
|
|
483
451
|
}
|
|
484
|
-
if (toBuffer.length <=
|
|
485
|
-
yield
|
|
452
|
+
if (toBuffer.length <= BLE_PACKET_SIZE) {
|
|
453
|
+
yield hdShared.wait(UNIFIED_WRITE_DELAY);
|
|
454
|
+
yield writeCharacteristicWithAck(deviceId, writeCharacteristic, toBuffer);
|
|
486
455
|
return;
|
|
487
456
|
}
|
|
488
457
|
for (let offset = 0; offset < toBuffer.length;) {
|
|
489
|
-
const chunkSize = Math.min(
|
|
458
|
+
const chunkSize = Math.min(BLE_PACKET_SIZE, toBuffer.length - offset);
|
|
490
459
|
const chunk = toBuffer.subarray(offset, offset + chunkSize);
|
|
491
460
|
offset += chunkSize;
|
|
492
461
|
const latest = doGetWriteCharacteristic();
|
|
493
462
|
if (!latest) {
|
|
494
463
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotFound, `Write characteristic not available for ${deviceId}`);
|
|
495
464
|
}
|
|
496
|
-
yield
|
|
465
|
+
yield writeCharacteristicWithAck(deviceId, latest, chunk);
|
|
466
|
+
if (offset < toBuffer.length) {
|
|
467
|
+
yield hdShared.wait(UNIFIED_WRITE_DELAY);
|
|
468
|
+
}
|
|
497
469
|
}
|
|
498
470
|
return;
|
|
499
471
|
}
|
|
500
|
-
if (toBuffer.length <=
|
|
472
|
+
if (toBuffer.length <= BLE_PACKET_SIZE) {
|
|
473
|
+
yield hdShared.wait(UNIFIED_WRITE_DELAY);
|
|
501
474
|
yield attemptWindowsWriteUntilPaired(deviceId, doGetWriteCharacteristic, toBuffer, 'single');
|
|
502
475
|
return;
|
|
503
476
|
}
|
|
504
477
|
for (let offset = 0, idx = 0; offset < toBuffer.length; idx++) {
|
|
505
|
-
const chunkSize = Math.min(
|
|
478
|
+
const chunkSize = Math.min(BLE_PACKET_SIZE, toBuffer.length - offset);
|
|
506
479
|
const chunk = toBuffer.subarray(offset, offset + chunkSize);
|
|
507
480
|
offset += chunkSize;
|
|
508
481
|
yield attemptWindowsWriteUntilPaired(deviceId, doGetWriteCharacteristic, chunk, `chunk-${idx + 1}`);
|
|
482
|
+
if (offset < toBuffer.length) {
|
|
483
|
+
yield hdShared.wait(UNIFIED_WRITE_DELAY);
|
|
484
|
+
}
|
|
509
485
|
}
|
|
510
486
|
});
|
|
511
487
|
}
|
|
@@ -682,12 +658,22 @@ function getDevice(deviceId) {
|
|
|
682
658
|
const peripheral = discoveredDevices.get(deviceId);
|
|
683
659
|
if (peripheral) {
|
|
684
660
|
const deviceName = ((_a = peripheral.advertisement) === null || _a === void 0 ? void 0 : _a.localName) || 'Unknown Device';
|
|
685
|
-
return
|
|
661
|
+
return {
|
|
662
|
+
commType: 'electron-ble',
|
|
663
|
+
id: peripheral.id,
|
|
664
|
+
name: deviceName,
|
|
665
|
+
state: peripheral.state || 'disconnected',
|
|
666
|
+
};
|
|
686
667
|
}
|
|
687
668
|
const connectedPeripheral = connectedDevices.get(deviceId);
|
|
688
669
|
if (connectedPeripheral) {
|
|
689
670
|
const deviceName = ((_b = connectedPeripheral.advertisement) === null || _b === void 0 ? void 0 : _b.localName) || 'Unknown Device';
|
|
690
|
-
return
|
|
671
|
+
return {
|
|
672
|
+
commType: 'electron-ble',
|
|
673
|
+
id: connectedPeripheral.id,
|
|
674
|
+
name: deviceName,
|
|
675
|
+
state: connectedPeripheral.state || 'connected',
|
|
676
|
+
};
|
|
691
677
|
}
|
|
692
678
|
return {
|
|
693
679
|
commType: 'electron-ble',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAAsB,WAAW,EAAE,MAAM,UAAU,CAAC;AAi9ChE,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAyJpE"}
|
|
@@ -6,7 +6,6 @@ export interface NobleBleAPI {
|
|
|
6
6
|
getDevice: (uuid: string) => Promise<{
|
|
7
7
|
id: string;
|
|
8
8
|
name: string;
|
|
9
|
-
mtu?: number;
|
|
10
9
|
} | null>;
|
|
11
10
|
connect: (uuid: string) => Promise<void>;
|
|
12
11
|
disconnect: (uuid: string) => Promise<void>;
|
|
@@ -14,10 +13,6 @@ export interface NobleBleAPI {
|
|
|
14
13
|
unsubscribe: (uuid: string) => Promise<void>;
|
|
15
14
|
write: (uuid: string, data: string) => Promise<void>;
|
|
16
15
|
onNotification: (callback: (deviceId: string, data: string) => void) => () => void;
|
|
17
|
-
onMtuChanged?: (callback: (device: {
|
|
18
|
-
id: string;
|
|
19
|
-
mtu: number;
|
|
20
|
-
}) => void) => () => void;
|
|
21
16
|
onDeviceDisconnected: (callback: (device: {
|
|
22
17
|
id: string;
|
|
23
18
|
name: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"desktop-api.d.ts","sourceRoot":"","sources":["../../src/types/desktop-api.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,CAAC;IACzD,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"desktop-api.d.ts","sourceRoot":"","sources":["../../src/types/desktop-api.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,CAAC;IACzD,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC,CAAC;IAC1E,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzC,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C,WAAW,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,cAAc,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,KAAK,MAAM,IAAI,CAAC;IACnF,oBAAoB,EAAE,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,KAAK,MAAM,IAAI,CAAC;IAC/F,iBAAiB,EAAE,MAAM,OAAO,CAAC;QAC/B,SAAS,EAAE,OAAO,CAAC;QACnB,KAAK,EAAE,MAAM,CAAC;QACd,WAAW,EAAE,OAAO,CAAC;QACrB,WAAW,EAAE,OAAO,CAAC;KACtB,CAAC,CAAC;CACJ;AAGD,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,EAAE,WAAW,CAAC;CACxB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noble-extended.d.ts","sourceRoot":"","sources":["../../src/types/noble-extended.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAGnE,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"noble-extended.d.ts","sourceRoot":"","sources":["../../src/types/noble-extended.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAGnE,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,cAAc,CAAC;IACtB,MAAM,EAAE,cAAc,CAAC;CACxB;AAGD,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,CACX,YAAY,EAAE,MAAM,EAAE,EACtB,eAAe,EAAE,OAAO,EACxB,QAAQ,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,GACjC,IAAI,CAAC;IACR,YAAY,CAAC,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,IAAI,CAAC;IAC1C,EAAE,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAAC;IAClE,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI,CAAC;IACxE,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAAC;IAC9E,cAAc,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI,CAAC;CACrF;AAGD,MAAM,WAAW,MAAM;IACrB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAC5C,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAC7C,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;CAC9C;AAGD,wBAAgB,OAAO,CACrB,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,EACjC,OAAO,EAAE,MAAM,EACf,GAAG,IAAI,EAAE,GAAG,EAAE,GACb,IAAI,CAMN"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-electron",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.70",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"electron-log": ">=4.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@onekeyfe/hd-core": "1.2.0-alpha.
|
|
29
|
-
"@onekeyfe/hd-shared": "1.2.0-alpha.
|
|
30
|
-
"@onekeyfe/hd-transport": "1.2.0-alpha.
|
|
28
|
+
"@onekeyfe/hd-core": "1.2.0-alpha.70",
|
|
29
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.70",
|
|
30
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.70",
|
|
31
31
|
"@stoprocent/noble": "2.3.16",
|
|
32
32
|
"p-retry": "^4.6.2"
|
|
33
33
|
},
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"electron": "^25.0.0",
|
|
37
37
|
"typescript": "^5.3.3"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "d86b033cf970f97e060c49fe4cf618f38b9ce666"
|
|
40
40
|
}
|
package/src/noble-ble-handler.ts
CHANGED
|
@@ -21,7 +21,6 @@ import {
|
|
|
21
21
|
} from '@onekeyfe/hd-shared';
|
|
22
22
|
import pRetry from 'p-retry';
|
|
23
23
|
|
|
24
|
-
import { resolveBlePacketCapacity } from './ble-packet-capacity';
|
|
25
24
|
import { safeLog } from './types/noble-extended';
|
|
26
25
|
import { runBleCallbackOperation, softRefreshSubscription } from './ble-ops';
|
|
27
26
|
import {
|
|
@@ -66,10 +65,6 @@ const deviceDisconnectListeners = new Map<
|
|
|
66
65
|
string,
|
|
67
66
|
{ peripheral: Peripheral; listener: () => void }
|
|
68
67
|
>();
|
|
69
|
-
const deviceMtuListeners = new Map<
|
|
70
|
-
string,
|
|
71
|
-
{ peripheral: Peripheral; listener: (mtu: number) => void }
|
|
72
|
-
>();
|
|
73
68
|
|
|
74
69
|
// Windows-only response watchdog state moved to utils/windows-ble-recovery
|
|
75
70
|
|
|
@@ -91,8 +86,8 @@ const SERVICE_DISCOVERY_TIMEOUT = 10000; // 10 seconds for service discovery
|
|
|
91
86
|
const BLE_CLEANUP_TIMEOUT = 250;
|
|
92
87
|
|
|
93
88
|
// Write-related constants
|
|
94
|
-
const
|
|
95
|
-
const
|
|
89
|
+
const BLE_PACKET_SIZE = 192;
|
|
90
|
+
const UNIFIED_WRITE_DELAY = 5;
|
|
96
91
|
const RETRY_CONFIG = { MAX_ATTEMPTS: 15, WRITE_TIMEOUT: 2000 } as const;
|
|
97
92
|
const IS_WINDOWS = process.platform === 'win32';
|
|
98
93
|
const ABORTABLE_WRITE_ERROR_PATTERNS = [
|
|
@@ -369,11 +364,6 @@ function cleanupDevice(
|
|
|
369
364
|
disconnectEntry.peripheral.removeListener('disconnect', disconnectEntry.listener);
|
|
370
365
|
deviceDisconnectListeners.delete(deviceId);
|
|
371
366
|
}
|
|
372
|
-
const mtuEntry = deviceMtuListeners.get(deviceId);
|
|
373
|
-
if (mtuEntry) {
|
|
374
|
-
mtuEntry.peripheral.removeListener('mtu', mtuEntry.listener);
|
|
375
|
-
deviceMtuListeners.delete(deviceId);
|
|
376
|
-
}
|
|
377
367
|
connectedDevices.delete(deviceId);
|
|
378
368
|
deviceCharacteristics.delete(deviceId);
|
|
379
369
|
notificationCallbacks.delete(deviceId);
|
|
@@ -433,33 +423,11 @@ function setupDisconnectListener(
|
|
|
433
423
|
};
|
|
434
424
|
deviceDisconnectListeners.set(deviceId, { peripheral, listener });
|
|
435
425
|
peripheral.on('disconnect', listener);
|
|
436
|
-
setupMtuListener(peripheral, deviceId, webContents);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
function setupMtuListener(
|
|
440
|
-
peripheral: Peripheral,
|
|
441
|
-
deviceId: string,
|
|
442
|
-
webContents: WebContents
|
|
443
|
-
): void {
|
|
444
|
-
const existing = deviceMtuListeners.get(deviceId);
|
|
445
|
-
if (existing) {
|
|
446
|
-
existing.peripheral.removeListener('mtu', existing.listener);
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
const listener = (mtu: number) => {
|
|
450
|
-
if (!Number.isFinite(mtu) || mtu <= 0) return;
|
|
451
|
-
webContents.send(EOneKeyBleMessageKeys.NOBLE_BLE_MTU_CHANGED, {
|
|
452
|
-
id: deviceId,
|
|
453
|
-
mtu,
|
|
454
|
-
});
|
|
455
|
-
};
|
|
456
|
-
deviceMtuListeners.set(deviceId, { peripheral, listener });
|
|
457
|
-
peripheral.on('mtu', listener);
|
|
458
426
|
}
|
|
459
427
|
|
|
460
428
|
// ===== Write helpers (inline) =====
|
|
461
429
|
|
|
462
|
-
async function
|
|
430
|
+
async function writeCharacteristicWithAck(
|
|
463
431
|
deviceId: string,
|
|
464
432
|
writeCharacteristic: Characteristic,
|
|
465
433
|
buffer: Buffer
|
|
@@ -507,7 +475,7 @@ async function attemptWindowsWriteUntilPaired(
|
|
|
507
475
|
}
|
|
508
476
|
|
|
509
477
|
try {
|
|
510
|
-
await
|
|
478
|
+
await writeCharacteristicWithAck(deviceId, latestWrite, payload);
|
|
511
479
|
} catch (e) {
|
|
512
480
|
const errorMessage = e instanceof Error ? e.message : String(e);
|
|
513
481
|
logger?.error('[BLE-Write] Windows write error', {
|
|
@@ -591,11 +559,6 @@ async function transmitHexDataToDevice(deviceId: string, hexData: string): Promi
|
|
|
591
559
|
|
|
592
560
|
const toBuffer = Buffer.from(hexData, 'hex');
|
|
593
561
|
const doGetWriteCharacteristic = () => deviceCharacteristics.get(deviceId)?.write;
|
|
594
|
-
const packetCapacity = resolveBlePacketCapacity(
|
|
595
|
-
peripheral.mtu,
|
|
596
|
-
BLE_PACKET_SIZE_MAXIMUM,
|
|
597
|
-
BLE_PACKET_SIZE_FALLBACK
|
|
598
|
-
);
|
|
599
562
|
|
|
600
563
|
if (!IS_WINDOWS || pairedDevices.has(deviceId)) {
|
|
601
564
|
// macOS / Linux or already paired on Windows: direct write
|
|
@@ -606,13 +569,14 @@ async function transmitHexDataToDevice(deviceId: string, hexData: string): Promi
|
|
|
606
569
|
`Write characteristic not available for ${deviceId}`
|
|
607
570
|
);
|
|
608
571
|
}
|
|
609
|
-
if (toBuffer.length <=
|
|
610
|
-
await
|
|
572
|
+
if (toBuffer.length <= BLE_PACKET_SIZE) {
|
|
573
|
+
await wait(UNIFIED_WRITE_DELAY);
|
|
574
|
+
await writeCharacteristicWithAck(deviceId, writeCharacteristic, toBuffer);
|
|
611
575
|
return;
|
|
612
576
|
}
|
|
613
577
|
// chunked
|
|
614
578
|
for (let offset = 0; offset < toBuffer.length; ) {
|
|
615
|
-
const chunkSize = Math.min(
|
|
579
|
+
const chunkSize = Math.min(BLE_PACKET_SIZE, toBuffer.length - offset);
|
|
616
580
|
const chunk = toBuffer.subarray(offset, offset + chunkSize);
|
|
617
581
|
offset += chunkSize;
|
|
618
582
|
const latest = doGetWriteCharacteristic();
|
|
@@ -622,19 +586,23 @@ async function transmitHexDataToDevice(deviceId: string, hexData: string): Promi
|
|
|
622
586
|
`Write characteristic not available for ${deviceId}`
|
|
623
587
|
);
|
|
624
588
|
}
|
|
625
|
-
await
|
|
589
|
+
await writeCharacteristicWithAck(deviceId, latest, chunk);
|
|
590
|
+
if (offset < toBuffer.length) {
|
|
591
|
+
await wait(UNIFIED_WRITE_DELAY);
|
|
592
|
+
}
|
|
626
593
|
}
|
|
627
594
|
return;
|
|
628
595
|
}
|
|
629
596
|
|
|
630
597
|
// Windows unpaired path: use loop
|
|
631
|
-
if (toBuffer.length <=
|
|
598
|
+
if (toBuffer.length <= BLE_PACKET_SIZE) {
|
|
599
|
+
await wait(UNIFIED_WRITE_DELAY);
|
|
632
600
|
await attemptWindowsWriteUntilPaired(deviceId, doGetWriteCharacteristic, toBuffer, 'single');
|
|
633
601
|
return;
|
|
634
602
|
}
|
|
635
603
|
// chunked loop
|
|
636
604
|
for (let offset = 0, idx = 0; offset < toBuffer.length; idx++) {
|
|
637
|
-
const chunkSize = Math.min(
|
|
605
|
+
const chunkSize = Math.min(BLE_PACKET_SIZE, toBuffer.length - offset);
|
|
638
606
|
const chunk = toBuffer.subarray(offset, offset + chunkSize);
|
|
639
607
|
offset += chunkSize;
|
|
640
608
|
await attemptWindowsWriteUntilPaired(
|
|
@@ -643,6 +611,9 @@ async function transmitHexDataToDevice(deviceId: string, hexData: string): Promi
|
|
|
643
611
|
chunk,
|
|
644
612
|
`chunk-${idx + 1}`
|
|
645
613
|
);
|
|
614
|
+
if (offset < toBuffer.length) {
|
|
615
|
+
await wait(UNIFIED_WRITE_DELAY);
|
|
616
|
+
}
|
|
646
617
|
}
|
|
647
618
|
}
|
|
648
619
|
|
|
@@ -864,7 +835,6 @@ function getDevice(deviceId: string): DeviceInfo | null {
|
|
|
864
835
|
id: peripheral.id,
|
|
865
836
|
name: deviceName,
|
|
866
837
|
state: peripheral.state || 'disconnected',
|
|
867
|
-
...(typeof peripheral.mtu === 'number' ? { mtu: peripheral.mtu } : {}),
|
|
868
838
|
};
|
|
869
839
|
}
|
|
870
840
|
|
|
@@ -877,7 +847,6 @@ function getDevice(deviceId: string): DeviceInfo | null {
|
|
|
877
847
|
id: connectedPeripheral.id,
|
|
878
848
|
name: deviceName,
|
|
879
849
|
state: connectedPeripheral.state || 'connected',
|
|
880
|
-
...(typeof connectedPeripheral.mtu === 'number' ? { mtu: connectedPeripheral.mtu } : {}),
|
|
881
850
|
};
|
|
882
851
|
}
|
|
883
852
|
|
package/src/types/desktop-api.ts
CHANGED
|
@@ -6,14 +6,13 @@
|
|
|
6
6
|
// Noble BLE API interface - core BLE functionality
|
|
7
7
|
export interface NobleBleAPI {
|
|
8
8
|
enumerate: () => Promise<{ id: string; name: string }[]>;
|
|
9
|
-
getDevice: (uuid: string) => Promise<{ id: string; name: string
|
|
9
|
+
getDevice: (uuid: string) => Promise<{ id: string; name: string } | null>;
|
|
10
10
|
connect: (uuid: string) => Promise<void>;
|
|
11
11
|
disconnect: (uuid: string) => Promise<void>;
|
|
12
12
|
subscribe: (uuid: string) => Promise<void>;
|
|
13
13
|
unsubscribe: (uuid: string) => Promise<void>;
|
|
14
14
|
write: (uuid: string, data: string) => Promise<void>;
|
|
15
15
|
onNotification: (callback: (deviceId: string, data: string) => void) => () => void;
|
|
16
|
-
onMtuChanged?: (callback: (device: { id: string; mtu: number }) => void) => () => void;
|
|
17
16
|
onDeviceDisconnected: (callback: (device: { id: string; name: string }) => void) => () => void;
|
|
18
17
|
checkAvailability: () => Promise<{
|
|
19
18
|
available: boolean;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ble-packet-capacity.d.ts","sourceRoot":"","sources":["../src/ble-packet-capacity.ts"],"names":[],"mappings":"AAEA,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC9B,qBAAqB,EAAE,MAAM,EAC7B,sBAAsB,EAAE,MAAM,GAC7B,MAAM,CAMR"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { resolveBlePacketCapacity } from '../ble-packet-capacity';
|
|
2
|
-
|
|
3
|
-
describe('resolveBlePacketCapacity', () => {
|
|
4
|
-
test('uses ATT MTU payload capacity with an upper bound', () => {
|
|
5
|
-
expect(resolveBlePacketCapacity(247, 244, 192)).toBe(244);
|
|
6
|
-
expect(resolveBlePacketCapacity(185, 244, 192)).toBe(182);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
test('preserves the compatibility fallback when MTU is unavailable', () => {
|
|
10
|
-
expect(resolveBlePacketCapacity(null, 244, 192)).toBe(192);
|
|
11
|
-
expect(resolveBlePacketCapacity(undefined, 244, 192)).toBe(192);
|
|
12
|
-
});
|
|
13
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const BLE_ATT_HEADER_BYTES = 3;
|
|
2
|
-
|
|
3
|
-
export function resolveBlePacketCapacity(
|
|
4
|
-
mtu: number | null | undefined,
|
|
5
|
-
maximumPacketCapacity: number,
|
|
6
|
-
fallbackPacketCapacity: number
|
|
7
|
-
): number {
|
|
8
|
-
if (typeof mtu !== 'number' || !Number.isFinite(mtu) || mtu <= BLE_ATT_HEADER_BYTES) {
|
|
9
|
-
return fallbackPacketCapacity;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return Math.min(maximumPacketCapacity, Math.floor(mtu) - BLE_ATT_HEADER_BYTES);
|
|
13
|
-
}
|