@matter/nodejs-ble 0.16.10 → 0.16.11-alpha.0-20260225-033797f3c
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/cjs/BleScanner.d.ts +6 -19
- package/dist/cjs/BleScanner.d.ts.map +1 -1
- package/dist/cjs/BleScanner.js +4 -231
- package/dist/cjs/BleScanner.js.map +2 -2
- package/dist/esm/BleScanner.d.ts +6 -19
- package/dist/esm/BleScanner.d.ts.map +1 -1
- package/dist/esm/BleScanner.js +5 -243
- package/dist/esm/BleScanner.js.map +1 -1
- package/package.json +7 -7
- package/src/BleScanner.ts +8 -314
package/dist/cjs/BleScanner.d.ts
CHANGED
|
@@ -3,30 +3,17 @@
|
|
|
3
3
|
* Copyright 2022-2026 Matter.js Authors
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import { CommissionableDevice, CommissionableDeviceIdentifiers, Scanner } from "#protocol";
|
|
6
|
+
import { BleScanner as BaseBleScanner, DiscoveredBleDevice } from "#protocol";
|
|
8
7
|
import type { Peripheral } from "@stoprocent/noble";
|
|
9
8
|
import { NobleBleClient } from "./NobleBleClient.js";
|
|
10
|
-
export type DiscoveredBleDevice
|
|
11
|
-
|
|
9
|
+
export type { DiscoveredBleDevice } from "#protocol";
|
|
10
|
+
export type NobleDiscoveredBleDevice = Omit<DiscoveredBleDevice, "peripheral"> & {
|
|
12
11
|
peripheral: Peripheral;
|
|
13
|
-
hasAdditionalAdvertisementData: boolean;
|
|
14
12
|
};
|
|
15
|
-
|
|
16
|
-
SD: number;
|
|
17
|
-
};
|
|
18
|
-
export declare class BleScanner implements Scanner {
|
|
13
|
+
export declare class BleScanner extends BaseBleScanner {
|
|
19
14
|
#private;
|
|
20
|
-
readonly type = ChannelType.BLE;
|
|
21
15
|
constructor(nobleClient: NobleBleClient);
|
|
22
|
-
getDiscoveredDevice(address: string):
|
|
23
|
-
|
|
24
|
-
findOperationalDevice(): Promise<undefined>;
|
|
25
|
-
getDiscoveredOperationalDevice(): undefined;
|
|
26
|
-
findCommissionableDevices(identifier: CommissionableDeviceIdentifiers, timeout?: Duration, ignoreExistingRecords?: boolean): Promise<CommissionableDevice[]>;
|
|
27
|
-
findCommissionableDevicesContinuously(identifier: CommissionableDeviceIdentifiers, callback: (device: CommissionableDevice) => void, timeout?: Duration, cancelSignal?: Promise<void>): Promise<CommissionableDevice[]>;
|
|
28
|
-
getDiscoveredCommissionableDevices(identifier: CommissionableDeviceIdentifiers): CommissionableDevice[];
|
|
29
|
-
close(): Promise<void>;
|
|
16
|
+
getDiscoveredDevice(address: string): NobleDiscoveredBleDevice;
|
|
17
|
+
protected closeClient(): void;
|
|
30
18
|
}
|
|
31
|
-
export {};
|
|
32
19
|
//# sourceMappingURL=BleScanner.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BleScanner.d.ts","sourceRoot":"","sources":["../../src/BleScanner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"BleScanner.d.ts","sourceRoot":"","sources":["../../src/BleScanner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,YAAY,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAErD,MAAM,MAAM,wBAAwB,GAAG,IAAI,CAAC,mBAAmB,EAAE,YAAY,CAAC,GAAG;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC;AAE5G,qBAAa,UAAW,SAAQ,cAAc;;gBAG9B,WAAW,EAAE,cAAc;IAK9B,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB;cAIpD,WAAW;CAGjC"}
|
package/dist/cjs/BleScanner.js
CHANGED
|
@@ -21,250 +21,23 @@ __export(BleScanner_exports, {
|
|
|
21
21
|
BleScanner: () => BleScanner
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(BleScanner_exports);
|
|
24
|
-
var import_general = require("#general");
|
|
25
24
|
var import_protocol = require("#protocol");
|
|
26
|
-
var import_types = require("#types");
|
|
27
25
|
/**
|
|
28
26
|
* @license
|
|
29
27
|
* Copyright 2022-2026 Matter.js Authors
|
|
30
28
|
* SPDX-License-Identifier: Apache-2.0
|
|
31
29
|
*/
|
|
32
|
-
|
|
33
|
-
class BleScanner {
|
|
34
|
-
type = import_general.ChannelType.BLE;
|
|
30
|
+
class BleScanner extends import_protocol.BleScanner {
|
|
35
31
|
#nobleClient;
|
|
36
|
-
#recordWaiters = /* @__PURE__ */ new Map();
|
|
37
|
-
#discoveredMatterDevices = /* @__PURE__ */ new Map();
|
|
38
32
|
constructor(nobleClient) {
|
|
33
|
+
super(nobleClient);
|
|
39
34
|
this.#nobleClient = nobleClient;
|
|
40
|
-
this.#nobleClient.setDiscoveryCallback(
|
|
41
|
-
(address, manufacturerData) => this.#handleDiscoveredDevice(address, manufacturerData)
|
|
42
|
-
);
|
|
43
35
|
}
|
|
44
36
|
getDiscoveredDevice(address) {
|
|
45
|
-
|
|
46
|
-
if (device === void 0) {
|
|
47
|
-
throw new import_protocol.BleError(`No device found for address ${address}`);
|
|
48
|
-
}
|
|
49
|
-
return device;
|
|
37
|
+
return super.getDiscoveredDevice(address);
|
|
50
38
|
}
|
|
51
|
-
|
|
52
|
-
* Registers a deferred promise for a specific queryId together with a timeout and return the promise.
|
|
53
|
-
* The promise will be resolved when the timer runs out latest.
|
|
54
|
-
*/
|
|
55
|
-
async #registerWaiterPromise(queryId, timeout, resolveOnUpdatedRecords = true, cancelResolver) {
|
|
56
|
-
const { promise, resolver } = (0, import_general.createPromise)();
|
|
57
|
-
let timer;
|
|
58
|
-
if (timeout) {
|
|
59
|
-
timer = import_general.Time.getTimer("BLE query timeout", timeout, () => {
|
|
60
|
-
cancelResolver?.();
|
|
61
|
-
this.#finishWaiter(queryId, true);
|
|
62
|
-
}).start();
|
|
63
|
-
}
|
|
64
|
-
this.#recordWaiters.set(queryId, { resolver, timer, resolveOnUpdatedRecords, cancelResolver });
|
|
65
|
-
logger.debug(
|
|
66
|
-
`Registered waiter for query ${queryId} with timeout ${timeout === void 0 ? "(none)" : import_general.Duration.format(timeout)} ${resolveOnUpdatedRecords ? "" : " (not resolving on updated records)"}`
|
|
67
|
-
);
|
|
68
|
-
await promise;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Remove a waiter promise for a specific queryId and stop the connected timer. If required also resolve the
|
|
72
|
-
* promise.
|
|
73
|
-
*/
|
|
74
|
-
#finishWaiter(queryId, resolvePromise, isUpdatedRecord = false) {
|
|
75
|
-
const waiter = this.#recordWaiters.get(queryId);
|
|
76
|
-
if (waiter === void 0) return;
|
|
77
|
-
const { timer, resolver, resolveOnUpdatedRecords } = waiter;
|
|
78
|
-
if (isUpdatedRecord && !resolveOnUpdatedRecords) return;
|
|
79
|
-
logger.debug(`Finishing waiter for query ${queryId}, resolving: ${resolvePromise}`);
|
|
80
|
-
timer?.stop();
|
|
81
|
-
if (resolvePromise) {
|
|
82
|
-
resolver();
|
|
83
|
-
}
|
|
84
|
-
this.#recordWaiters.delete(queryId);
|
|
85
|
-
}
|
|
86
|
-
cancelCommissionableDeviceDiscovery(identifier, resolvePromise = true) {
|
|
87
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
88
|
-
const { cancelResolver } = this.#recordWaiters.get(queryKey) ?? {};
|
|
89
|
-
cancelResolver?.();
|
|
90
|
-
this.#finishWaiter(queryKey, resolvePromise);
|
|
91
|
-
}
|
|
92
|
-
#handleDiscoveredDevice(peripheral, manufacturerServiceData) {
|
|
93
|
-
const address = peripheral.address;
|
|
94
|
-
try {
|
|
95
|
-
const { discriminator, vendorId, productId, hasAdditionalAdvertisementData } = import_protocol.BtpCodec.decodeBleAdvertisementServiceData(manufacturerServiceData);
|
|
96
|
-
const deviceData = {
|
|
97
|
-
deviceIdentifier: address,
|
|
98
|
-
D: discriminator,
|
|
99
|
-
SD: discriminator >> 8 & 15,
|
|
100
|
-
VP: `${vendorId}+${productId}`,
|
|
101
|
-
CM: 1,
|
|
102
|
-
// Can be no other mode,
|
|
103
|
-
addresses: [{ type: "ble", peripheralAddress: address }]
|
|
104
|
-
};
|
|
105
|
-
const deviceExisting = this.#discoveredMatterDevices.has(address);
|
|
106
|
-
logger.debug(
|
|
107
|
-
`${deviceExisting ? "Re-" : ""}Discovered device ${address} data: ${import_general.Diagnostic.json(deviceData)}`
|
|
108
|
-
);
|
|
109
|
-
this.#discoveredMatterDevices.set(address, {
|
|
110
|
-
deviceData,
|
|
111
|
-
peripheral,
|
|
112
|
-
hasAdditionalAdvertisementData
|
|
113
|
-
});
|
|
114
|
-
const queryKey = this.#findCommissionableQueryIdentifier(deviceData);
|
|
115
|
-
if (queryKey !== void 0) {
|
|
116
|
-
this.#finishWaiter(queryKey, true, deviceExisting);
|
|
117
|
-
}
|
|
118
|
-
} catch (error) {
|
|
119
|
-
logger.debug(
|
|
120
|
-
`Discovered device ${address} ${manufacturerServiceData === void 0 ? void 0 : import_general.Bytes.toHex(manufacturerServiceData)} does not seem to be a valid Matter device: ${error}`
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
#findCommissionableQueryIdentifier(record) {
|
|
125
|
-
const longDiscriminatorQueryId = this.#buildCommissionableQueryIdentifier({ longDiscriminator: record.D });
|
|
126
|
-
if (this.#recordWaiters.has(longDiscriminatorQueryId)) {
|
|
127
|
-
return longDiscriminatorQueryId;
|
|
128
|
-
}
|
|
129
|
-
const shortDiscriminatorQueryId = this.#buildCommissionableQueryIdentifier({ shortDiscriminator: record.SD });
|
|
130
|
-
if (this.#recordWaiters.has(shortDiscriminatorQueryId)) {
|
|
131
|
-
return shortDiscriminatorQueryId;
|
|
132
|
-
}
|
|
133
|
-
if (record.VP !== void 0) {
|
|
134
|
-
const vpParts = record.VP.split("+");
|
|
135
|
-
const vendorIdQueryId = this.#buildCommissionableQueryIdentifier({
|
|
136
|
-
vendorId: (0, import_types.VendorId)(parseInt(vpParts[0]))
|
|
137
|
-
});
|
|
138
|
-
if (this.#recordWaiters.has(vendorIdQueryId)) {
|
|
139
|
-
return vendorIdQueryId;
|
|
140
|
-
}
|
|
141
|
-
if (vpParts[1] !== void 0) {
|
|
142
|
-
const productIdQueryId = this.#buildCommissionableQueryIdentifier({
|
|
143
|
-
productId: parseInt(vpParts[1])
|
|
144
|
-
});
|
|
145
|
-
if (this.#recordWaiters.has(productIdQueryId)) {
|
|
146
|
-
return productIdQueryId;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (this.#recordWaiters.has("*")) {
|
|
151
|
-
return "*";
|
|
152
|
-
}
|
|
153
|
-
return void 0;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Builds an identifier string for commissionable queries based on the given identifier object.
|
|
157
|
-
* Some identifiers are identical to the official DNS-SD identifiers, others are custom.
|
|
158
|
-
*/
|
|
159
|
-
#buildCommissionableQueryIdentifier(identifier) {
|
|
160
|
-
if ("longDiscriminator" in identifier) {
|
|
161
|
-
return `D:${identifier.longDiscriminator}`;
|
|
162
|
-
} else if ("shortDiscriminator" in identifier) {
|
|
163
|
-
return `SD:${identifier.shortDiscriminator}`;
|
|
164
|
-
} else if ("vendorId" in identifier) {
|
|
165
|
-
return `V:${identifier.vendorId}`;
|
|
166
|
-
} else if ("productId" in identifier) {
|
|
167
|
-
return `P:${identifier.productId}`;
|
|
168
|
-
} else return "*";
|
|
169
|
-
}
|
|
170
|
-
#getCommissionableDevices(identifier) {
|
|
171
|
-
const storedRecords = Array.from(this.#discoveredMatterDevices.values());
|
|
172
|
-
const foundRecords = new Array();
|
|
173
|
-
if ("longDiscriminator" in identifier) {
|
|
174
|
-
foundRecords.push(...storedRecords.filter(({ deviceData: { D } }) => D === identifier.longDiscriminator));
|
|
175
|
-
} else if ("shortDiscriminator" in identifier) {
|
|
176
|
-
foundRecords.push(
|
|
177
|
-
...storedRecords.filter(({ deviceData: { SD } }) => SD === identifier.shortDiscriminator)
|
|
178
|
-
);
|
|
179
|
-
} else if ("vendorId" in identifier) {
|
|
180
|
-
foundRecords.push(
|
|
181
|
-
...storedRecords.filter(
|
|
182
|
-
({ deviceData: { VP } }) => VP === `${identifier.vendorId}` || VP?.startsWith(`${identifier.vendorId}+`)
|
|
183
|
-
)
|
|
184
|
-
);
|
|
185
|
-
} else if ("productId" in identifier) {
|
|
186
|
-
foundRecords.push(
|
|
187
|
-
...storedRecords.filter(({ deviceData: { VP } }) => VP?.endsWith(`+${identifier.productId}`))
|
|
188
|
-
);
|
|
189
|
-
} else {
|
|
190
|
-
foundRecords.push(...storedRecords.filter(({ deviceData: { CM } }) => CM === 1 || CM === 2));
|
|
191
|
-
}
|
|
192
|
-
return foundRecords;
|
|
193
|
-
}
|
|
194
|
-
async findOperationalDevice() {
|
|
195
|
-
logger.info(`skip BLE scan because scanning for operational devices is not supported`);
|
|
196
|
-
return void 0;
|
|
197
|
-
}
|
|
198
|
-
getDiscoveredOperationalDevice() {
|
|
199
|
-
logger.info(`skip BLE scan because scanning for operational devices is not supported`);
|
|
200
|
-
return void 0;
|
|
201
|
-
}
|
|
202
|
-
async findCommissionableDevices(identifier, timeout = (0, import_general.Seconds)(10), ignoreExistingRecords = false) {
|
|
203
|
-
let storedRecords = this.#getCommissionableDevices(identifier);
|
|
204
|
-
if (ignoreExistingRecords) {
|
|
205
|
-
for (const record of storedRecords) {
|
|
206
|
-
this.#discoveredMatterDevices.delete(record.peripheral.address);
|
|
207
|
-
}
|
|
208
|
-
storedRecords = [];
|
|
209
|
-
}
|
|
210
|
-
if (storedRecords.length === 0) {
|
|
211
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
212
|
-
await this.#nobleClient.startScanning();
|
|
213
|
-
await this.#registerWaiterPromise(queryKey, timeout);
|
|
214
|
-
storedRecords = this.#getCommissionableDevices(identifier);
|
|
215
|
-
await this.#nobleClient.stopScanning();
|
|
216
|
-
}
|
|
217
|
-
return storedRecords.map(({ deviceData }) => deviceData);
|
|
218
|
-
}
|
|
219
|
-
async findCommissionableDevicesContinuously(identifier, callback, timeout, cancelSignal) {
|
|
220
|
-
const discoveredDevices = /* @__PURE__ */ new Set();
|
|
221
|
-
const discoveryEndTime = timeout ? import_general.Time.nowMs + timeout : void 0;
|
|
222
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
223
|
-
await this.#nobleClient.startScanning();
|
|
224
|
-
let queryResolver;
|
|
225
|
-
if (cancelSignal === void 0) {
|
|
226
|
-
const { promise, resolver } = (0, import_general.createPromise)();
|
|
227
|
-
cancelSignal = promise;
|
|
228
|
-
queryResolver = resolver;
|
|
229
|
-
}
|
|
230
|
-
let canceled = false;
|
|
231
|
-
cancelSignal?.then(
|
|
232
|
-
() => {
|
|
233
|
-
canceled = true;
|
|
234
|
-
this.#finishWaiter(queryKey, true);
|
|
235
|
-
},
|
|
236
|
-
(cause) => {
|
|
237
|
-
logger.error("Unexpected error canceling commissioning", cause);
|
|
238
|
-
}
|
|
239
|
-
);
|
|
240
|
-
while (!canceled) {
|
|
241
|
-
this.#getCommissionableDevices(identifier).forEach(({ deviceData }) => {
|
|
242
|
-
const { deviceIdentifier } = deviceData;
|
|
243
|
-
if (!discoveredDevices.has(deviceIdentifier)) {
|
|
244
|
-
discoveredDevices.add(deviceIdentifier);
|
|
245
|
-
callback(deviceData);
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
let remainingTime;
|
|
249
|
-
if (discoveryEndTime !== void 0) {
|
|
250
|
-
remainingTime = import_general.Millis.ceil((0, import_general.Timespan)(import_general.Time.nowMs, discoveryEndTime).duration);
|
|
251
|
-
if (remainingTime <= 0) {
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
await this.#registerWaiterPromise(queryKey, remainingTime, false, queryResolver);
|
|
256
|
-
}
|
|
257
|
-
await this.#nobleClient.stopScanning();
|
|
258
|
-
return this.#getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
|
|
259
|
-
}
|
|
260
|
-
getDiscoveredCommissionableDevices(identifier) {
|
|
261
|
-
return this.#getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
|
|
262
|
-
}
|
|
263
|
-
async close() {
|
|
39
|
+
closeClient() {
|
|
264
40
|
this.#nobleClient.close();
|
|
265
|
-
[...this.#recordWaiters.keys()].forEach(
|
|
266
|
-
(queryId) => this.#finishWaiter(queryId, !!this.#recordWaiters.get(queryId)?.timer)
|
|
267
|
-
);
|
|
268
41
|
}
|
|
269
42
|
}
|
|
270
43
|
//# sourceMappingURL=BleScanner.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/BleScanner.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,
|
|
5
|
-
"names": []
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,sBAAkE;AANlE;AAAA;AAAA;AAAA;AAAA;AAcO,MAAM,mBAAmB,gBAAAA,WAAe;AAAA,EAClC;AAAA,EAET,YAAY,aAA6B;AACrC,UAAM,WAAW;AACjB,SAAK,eAAe;AAAA,EACxB;AAAA,EAES,oBAAoB,SAA2C;AACpE,WAAO,MAAM,oBAAoB,OAAO;AAAA,EAC5C;AAAA,EAEmB,cAAc;AAC7B,SAAK,aAAa,MAAM;AAAA,EAC5B;AACJ;",
|
|
5
|
+
"names": ["BaseBleScanner"]
|
|
6
6
|
}
|
package/dist/esm/BleScanner.d.ts
CHANGED
|
@@ -3,30 +3,17 @@
|
|
|
3
3
|
* Copyright 2022-2026 Matter.js Authors
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import { CommissionableDevice, CommissionableDeviceIdentifiers, Scanner } from "#protocol";
|
|
6
|
+
import { BleScanner as BaseBleScanner, DiscoveredBleDevice } from "#protocol";
|
|
8
7
|
import type { Peripheral } from "@stoprocent/noble";
|
|
9
8
|
import { NobleBleClient } from "./NobleBleClient.js";
|
|
10
|
-
export type DiscoveredBleDevice
|
|
11
|
-
|
|
9
|
+
export type { DiscoveredBleDevice } from "#protocol";
|
|
10
|
+
export type NobleDiscoveredBleDevice = Omit<DiscoveredBleDevice, "peripheral"> & {
|
|
12
11
|
peripheral: Peripheral;
|
|
13
|
-
hasAdditionalAdvertisementData: boolean;
|
|
14
12
|
};
|
|
15
|
-
|
|
16
|
-
SD: number;
|
|
17
|
-
};
|
|
18
|
-
export declare class BleScanner implements Scanner {
|
|
13
|
+
export declare class BleScanner extends BaseBleScanner {
|
|
19
14
|
#private;
|
|
20
|
-
readonly type = ChannelType.BLE;
|
|
21
15
|
constructor(nobleClient: NobleBleClient);
|
|
22
|
-
getDiscoveredDevice(address: string):
|
|
23
|
-
|
|
24
|
-
findOperationalDevice(): Promise<undefined>;
|
|
25
|
-
getDiscoveredOperationalDevice(): undefined;
|
|
26
|
-
findCommissionableDevices(identifier: CommissionableDeviceIdentifiers, timeout?: Duration, ignoreExistingRecords?: boolean): Promise<CommissionableDevice[]>;
|
|
27
|
-
findCommissionableDevicesContinuously(identifier: CommissionableDeviceIdentifiers, callback: (device: CommissionableDevice) => void, timeout?: Duration, cancelSignal?: Promise<void>): Promise<CommissionableDevice[]>;
|
|
28
|
-
getDiscoveredCommissionableDevices(identifier: CommissionableDeviceIdentifiers): CommissionableDevice[];
|
|
29
|
-
close(): Promise<void>;
|
|
16
|
+
getDiscoveredDevice(address: string): NobleDiscoveredBleDevice;
|
|
17
|
+
protected closeClient(): void;
|
|
30
18
|
}
|
|
31
|
-
export {};
|
|
32
19
|
//# sourceMappingURL=BleScanner.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BleScanner.d.ts","sourceRoot":"","sources":["../../src/BleScanner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"BleScanner.d.ts","sourceRoot":"","sources":["../../src/BleScanner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,YAAY,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAErD,MAAM,MAAM,wBAAwB,GAAG,IAAI,CAAC,mBAAmB,EAAE,YAAY,CAAC,GAAG;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC;AAE5G,qBAAa,UAAW,SAAQ,cAAc;;gBAG9B,WAAW,EAAE,cAAc;IAK9B,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB;cAIpD,WAAW;CAGjC"}
|
package/dist/esm/BleScanner.js
CHANGED
|
@@ -3,256 +3,18 @@
|
|
|
3
3
|
* Copyright 2022-2026 Matter.js Authors
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
ChannelType,
|
|
9
|
-
createPromise,
|
|
10
|
-
Diagnostic,
|
|
11
|
-
Duration,
|
|
12
|
-
Logger,
|
|
13
|
-
Millis,
|
|
14
|
-
Seconds,
|
|
15
|
-
Time,
|
|
16
|
-
Timespan
|
|
17
|
-
} from "#general";
|
|
18
|
-
import { BleError, BtpCodec } from "#protocol";
|
|
19
|
-
import { VendorId } from "#types";
|
|
20
|
-
const logger = Logger.get("BleScanner");
|
|
21
|
-
class BleScanner {
|
|
22
|
-
type = ChannelType.BLE;
|
|
6
|
+
import { BleScanner as BaseBleScanner } from "#protocol";
|
|
7
|
+
class BleScanner extends BaseBleScanner {
|
|
23
8
|
#nobleClient;
|
|
24
|
-
#recordWaiters = /* @__PURE__ */ new Map();
|
|
25
|
-
#discoveredMatterDevices = /* @__PURE__ */ new Map();
|
|
26
9
|
constructor(nobleClient) {
|
|
10
|
+
super(nobleClient);
|
|
27
11
|
this.#nobleClient = nobleClient;
|
|
28
|
-
this.#nobleClient.setDiscoveryCallback(
|
|
29
|
-
(address, manufacturerData) => this.#handleDiscoveredDevice(address, manufacturerData)
|
|
30
|
-
);
|
|
31
12
|
}
|
|
32
13
|
getDiscoveredDevice(address) {
|
|
33
|
-
|
|
34
|
-
if (device === void 0) {
|
|
35
|
-
throw new BleError(`No device found for address ${address}`);
|
|
36
|
-
}
|
|
37
|
-
return device;
|
|
14
|
+
return super.getDiscoveredDevice(address);
|
|
38
15
|
}
|
|
39
|
-
|
|
40
|
-
* Registers a deferred promise for a specific queryId together with a timeout and return the promise.
|
|
41
|
-
* The promise will be resolved when the timer runs out latest.
|
|
42
|
-
*/
|
|
43
|
-
async #registerWaiterPromise(queryId, timeout, resolveOnUpdatedRecords = true, cancelResolver) {
|
|
44
|
-
const { promise, resolver } = createPromise();
|
|
45
|
-
let timer;
|
|
46
|
-
if (timeout) {
|
|
47
|
-
timer = Time.getTimer("BLE query timeout", timeout, () => {
|
|
48
|
-
cancelResolver?.();
|
|
49
|
-
this.#finishWaiter(queryId, true);
|
|
50
|
-
}).start();
|
|
51
|
-
}
|
|
52
|
-
this.#recordWaiters.set(queryId, { resolver, timer, resolveOnUpdatedRecords, cancelResolver });
|
|
53
|
-
logger.debug(
|
|
54
|
-
`Registered waiter for query ${queryId} with timeout ${timeout === void 0 ? "(none)" : Duration.format(timeout)} ${resolveOnUpdatedRecords ? "" : " (not resolving on updated records)"}`
|
|
55
|
-
);
|
|
56
|
-
await promise;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Remove a waiter promise for a specific queryId and stop the connected timer. If required also resolve the
|
|
60
|
-
* promise.
|
|
61
|
-
*/
|
|
62
|
-
#finishWaiter(queryId, resolvePromise, isUpdatedRecord = false) {
|
|
63
|
-
const waiter = this.#recordWaiters.get(queryId);
|
|
64
|
-
if (waiter === void 0) return;
|
|
65
|
-
const { timer, resolver, resolveOnUpdatedRecords } = waiter;
|
|
66
|
-
if (isUpdatedRecord && !resolveOnUpdatedRecords) return;
|
|
67
|
-
logger.debug(`Finishing waiter for query ${queryId}, resolving: ${resolvePromise}`);
|
|
68
|
-
timer?.stop();
|
|
69
|
-
if (resolvePromise) {
|
|
70
|
-
resolver();
|
|
71
|
-
}
|
|
72
|
-
this.#recordWaiters.delete(queryId);
|
|
73
|
-
}
|
|
74
|
-
cancelCommissionableDeviceDiscovery(identifier, resolvePromise = true) {
|
|
75
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
76
|
-
const { cancelResolver } = this.#recordWaiters.get(queryKey) ?? {};
|
|
77
|
-
cancelResolver?.();
|
|
78
|
-
this.#finishWaiter(queryKey, resolvePromise);
|
|
79
|
-
}
|
|
80
|
-
#handleDiscoveredDevice(peripheral, manufacturerServiceData) {
|
|
81
|
-
const address = peripheral.address;
|
|
82
|
-
try {
|
|
83
|
-
const { discriminator, vendorId, productId, hasAdditionalAdvertisementData } = BtpCodec.decodeBleAdvertisementServiceData(manufacturerServiceData);
|
|
84
|
-
const deviceData = {
|
|
85
|
-
deviceIdentifier: address,
|
|
86
|
-
D: discriminator,
|
|
87
|
-
SD: discriminator >> 8 & 15,
|
|
88
|
-
VP: `${vendorId}+${productId}`,
|
|
89
|
-
CM: 1,
|
|
90
|
-
// Can be no other mode,
|
|
91
|
-
addresses: [{ type: "ble", peripheralAddress: address }]
|
|
92
|
-
};
|
|
93
|
-
const deviceExisting = this.#discoveredMatterDevices.has(address);
|
|
94
|
-
logger.debug(
|
|
95
|
-
`${deviceExisting ? "Re-" : ""}Discovered device ${address} data: ${Diagnostic.json(deviceData)}`
|
|
96
|
-
);
|
|
97
|
-
this.#discoveredMatterDevices.set(address, {
|
|
98
|
-
deviceData,
|
|
99
|
-
peripheral,
|
|
100
|
-
hasAdditionalAdvertisementData
|
|
101
|
-
});
|
|
102
|
-
const queryKey = this.#findCommissionableQueryIdentifier(deviceData);
|
|
103
|
-
if (queryKey !== void 0) {
|
|
104
|
-
this.#finishWaiter(queryKey, true, deviceExisting);
|
|
105
|
-
}
|
|
106
|
-
} catch (error) {
|
|
107
|
-
logger.debug(
|
|
108
|
-
`Discovered device ${address} ${manufacturerServiceData === void 0 ? void 0 : Bytes.toHex(manufacturerServiceData)} does not seem to be a valid Matter device: ${error}`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
#findCommissionableQueryIdentifier(record) {
|
|
113
|
-
const longDiscriminatorQueryId = this.#buildCommissionableQueryIdentifier({ longDiscriminator: record.D });
|
|
114
|
-
if (this.#recordWaiters.has(longDiscriminatorQueryId)) {
|
|
115
|
-
return longDiscriminatorQueryId;
|
|
116
|
-
}
|
|
117
|
-
const shortDiscriminatorQueryId = this.#buildCommissionableQueryIdentifier({ shortDiscriminator: record.SD });
|
|
118
|
-
if (this.#recordWaiters.has(shortDiscriminatorQueryId)) {
|
|
119
|
-
return shortDiscriminatorQueryId;
|
|
120
|
-
}
|
|
121
|
-
if (record.VP !== void 0) {
|
|
122
|
-
const vpParts = record.VP.split("+");
|
|
123
|
-
const vendorIdQueryId = this.#buildCommissionableQueryIdentifier({
|
|
124
|
-
vendorId: VendorId(parseInt(vpParts[0]))
|
|
125
|
-
});
|
|
126
|
-
if (this.#recordWaiters.has(vendorIdQueryId)) {
|
|
127
|
-
return vendorIdQueryId;
|
|
128
|
-
}
|
|
129
|
-
if (vpParts[1] !== void 0) {
|
|
130
|
-
const productIdQueryId = this.#buildCommissionableQueryIdentifier({
|
|
131
|
-
productId: parseInt(vpParts[1])
|
|
132
|
-
});
|
|
133
|
-
if (this.#recordWaiters.has(productIdQueryId)) {
|
|
134
|
-
return productIdQueryId;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (this.#recordWaiters.has("*")) {
|
|
139
|
-
return "*";
|
|
140
|
-
}
|
|
141
|
-
return void 0;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Builds an identifier string for commissionable queries based on the given identifier object.
|
|
145
|
-
* Some identifiers are identical to the official DNS-SD identifiers, others are custom.
|
|
146
|
-
*/
|
|
147
|
-
#buildCommissionableQueryIdentifier(identifier) {
|
|
148
|
-
if ("longDiscriminator" in identifier) {
|
|
149
|
-
return `D:${identifier.longDiscriminator}`;
|
|
150
|
-
} else if ("shortDiscriminator" in identifier) {
|
|
151
|
-
return `SD:${identifier.shortDiscriminator}`;
|
|
152
|
-
} else if ("vendorId" in identifier) {
|
|
153
|
-
return `V:${identifier.vendorId}`;
|
|
154
|
-
} else if ("productId" in identifier) {
|
|
155
|
-
return `P:${identifier.productId}`;
|
|
156
|
-
} else return "*";
|
|
157
|
-
}
|
|
158
|
-
#getCommissionableDevices(identifier) {
|
|
159
|
-
const storedRecords = Array.from(this.#discoveredMatterDevices.values());
|
|
160
|
-
const foundRecords = new Array();
|
|
161
|
-
if ("longDiscriminator" in identifier) {
|
|
162
|
-
foundRecords.push(...storedRecords.filter(({ deviceData: { D } }) => D === identifier.longDiscriminator));
|
|
163
|
-
} else if ("shortDiscriminator" in identifier) {
|
|
164
|
-
foundRecords.push(
|
|
165
|
-
...storedRecords.filter(({ deviceData: { SD } }) => SD === identifier.shortDiscriminator)
|
|
166
|
-
);
|
|
167
|
-
} else if ("vendorId" in identifier) {
|
|
168
|
-
foundRecords.push(
|
|
169
|
-
...storedRecords.filter(
|
|
170
|
-
({ deviceData: { VP } }) => VP === `${identifier.vendorId}` || VP?.startsWith(`${identifier.vendorId}+`)
|
|
171
|
-
)
|
|
172
|
-
);
|
|
173
|
-
} else if ("productId" in identifier) {
|
|
174
|
-
foundRecords.push(
|
|
175
|
-
...storedRecords.filter(({ deviceData: { VP } }) => VP?.endsWith(`+${identifier.productId}`))
|
|
176
|
-
);
|
|
177
|
-
} else {
|
|
178
|
-
foundRecords.push(...storedRecords.filter(({ deviceData: { CM } }) => CM === 1 || CM === 2));
|
|
179
|
-
}
|
|
180
|
-
return foundRecords;
|
|
181
|
-
}
|
|
182
|
-
async findOperationalDevice() {
|
|
183
|
-
logger.info(`skip BLE scan because scanning for operational devices is not supported`);
|
|
184
|
-
return void 0;
|
|
185
|
-
}
|
|
186
|
-
getDiscoveredOperationalDevice() {
|
|
187
|
-
logger.info(`skip BLE scan because scanning for operational devices is not supported`);
|
|
188
|
-
return void 0;
|
|
189
|
-
}
|
|
190
|
-
async findCommissionableDevices(identifier, timeout = Seconds(10), ignoreExistingRecords = false) {
|
|
191
|
-
let storedRecords = this.#getCommissionableDevices(identifier);
|
|
192
|
-
if (ignoreExistingRecords) {
|
|
193
|
-
for (const record of storedRecords) {
|
|
194
|
-
this.#discoveredMatterDevices.delete(record.peripheral.address);
|
|
195
|
-
}
|
|
196
|
-
storedRecords = [];
|
|
197
|
-
}
|
|
198
|
-
if (storedRecords.length === 0) {
|
|
199
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
200
|
-
await this.#nobleClient.startScanning();
|
|
201
|
-
await this.#registerWaiterPromise(queryKey, timeout);
|
|
202
|
-
storedRecords = this.#getCommissionableDevices(identifier);
|
|
203
|
-
await this.#nobleClient.stopScanning();
|
|
204
|
-
}
|
|
205
|
-
return storedRecords.map(({ deviceData }) => deviceData);
|
|
206
|
-
}
|
|
207
|
-
async findCommissionableDevicesContinuously(identifier, callback, timeout, cancelSignal) {
|
|
208
|
-
const discoveredDevices = /* @__PURE__ */ new Set();
|
|
209
|
-
const discoveryEndTime = timeout ? Time.nowMs + timeout : void 0;
|
|
210
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
211
|
-
await this.#nobleClient.startScanning();
|
|
212
|
-
let queryResolver;
|
|
213
|
-
if (cancelSignal === void 0) {
|
|
214
|
-
const { promise, resolver } = createPromise();
|
|
215
|
-
cancelSignal = promise;
|
|
216
|
-
queryResolver = resolver;
|
|
217
|
-
}
|
|
218
|
-
let canceled = false;
|
|
219
|
-
cancelSignal?.then(
|
|
220
|
-
() => {
|
|
221
|
-
canceled = true;
|
|
222
|
-
this.#finishWaiter(queryKey, true);
|
|
223
|
-
},
|
|
224
|
-
(cause) => {
|
|
225
|
-
logger.error("Unexpected error canceling commissioning", cause);
|
|
226
|
-
}
|
|
227
|
-
);
|
|
228
|
-
while (!canceled) {
|
|
229
|
-
this.#getCommissionableDevices(identifier).forEach(({ deviceData }) => {
|
|
230
|
-
const { deviceIdentifier } = deviceData;
|
|
231
|
-
if (!discoveredDevices.has(deviceIdentifier)) {
|
|
232
|
-
discoveredDevices.add(deviceIdentifier);
|
|
233
|
-
callback(deviceData);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
let remainingTime;
|
|
237
|
-
if (discoveryEndTime !== void 0) {
|
|
238
|
-
remainingTime = Millis.ceil(Timespan(Time.nowMs, discoveryEndTime).duration);
|
|
239
|
-
if (remainingTime <= 0) {
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
await this.#registerWaiterPromise(queryKey, remainingTime, false, queryResolver);
|
|
244
|
-
}
|
|
245
|
-
await this.#nobleClient.stopScanning();
|
|
246
|
-
return this.#getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
|
|
247
|
-
}
|
|
248
|
-
getDiscoveredCommissionableDevices(identifier) {
|
|
249
|
-
return this.#getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
|
|
250
|
-
}
|
|
251
|
-
async close() {
|
|
16
|
+
closeClient() {
|
|
252
17
|
this.#nobleClient.close();
|
|
253
|
-
[...this.#recordWaiters.keys()].forEach(
|
|
254
|
-
(queryId) => this.#finishWaiter(queryId, !!this.#recordWaiters.get(queryId)?.timer)
|
|
255
|
-
);
|
|
256
18
|
}
|
|
257
19
|
}
|
|
258
20
|
export {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/BleScanner.ts"],
|
|
4
|
-
"mappings": "AAAA;AAAA;AAAA;AAAA;AAAA;AAMA
|
|
4
|
+
"mappings": "AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,SAAS,cAAc,sBAA2C;AAQ3D,MAAM,mBAAmB,eAAe;AAAA,EAClC;AAAA,EAET,YAAY,aAA6B;AACrC,UAAM,WAAW;AACjB,SAAK,eAAe;AAAA,EACxB;AAAA,EAES,oBAAoB,SAA2C;AACpE,WAAO,MAAM,oBAAoB,OAAO;AAAA,EAC5C;AAAA,EAEmB,cAAc;AAC7B,SAAK,aAAa,MAAM;AAAA,EAC5B;AACJ;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matter/nodejs-ble",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.11-alpha.0-20260225-033797f3c",
|
|
4
4
|
"description": "Matter BLE support for node.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"iot",
|
|
@@ -28,16 +28,16 @@
|
|
|
28
28
|
"build-clean": "matter-build --clean"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@matter/general": "0.16.
|
|
32
|
-
"@matter/protocol": "0.16.
|
|
33
|
-
"@matter/types": "0.16.
|
|
31
|
+
"@matter/general": "0.16.11-alpha.0-20260225-033797f3c",
|
|
32
|
+
"@matter/protocol": "0.16.11-alpha.0-20260225-033797f3c",
|
|
33
|
+
"@matter/types": "0.16.11-alpha.0-20260225-033797f3c"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@matter/tools": "0.16.
|
|
36
|
+
"@matter/tools": "0.16.11-alpha.0-20260225-033797f3c"
|
|
37
37
|
},
|
|
38
38
|
"optionalDependencies": {
|
|
39
|
-
"@stoprocent/bleno": "^0.12.
|
|
40
|
-
"@stoprocent/noble": "^2.3.
|
|
39
|
+
"@stoprocent/bleno": "^0.12.3",
|
|
40
|
+
"@stoprocent/noble": "^2.3.16"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=20.19.0 <22.0.0 || >=22.13.0"
|
package/src/BleScanner.ts
CHANGED
|
@@ -4,333 +4,27 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
Bytes,
|
|
9
|
-
ChannelType,
|
|
10
|
-
createPromise,
|
|
11
|
-
Diagnostic,
|
|
12
|
-
Duration,
|
|
13
|
-
Logger,
|
|
14
|
-
Millis,
|
|
15
|
-
Seconds,
|
|
16
|
-
Time,
|
|
17
|
-
Timer,
|
|
18
|
-
Timespan,
|
|
19
|
-
} from "#general";
|
|
20
|
-
import { BleError, BtpCodec, CommissionableDevice, CommissionableDeviceIdentifiers, Scanner } from "#protocol";
|
|
21
|
-
import { VendorId } from "#types";
|
|
7
|
+
import { BleScanner as BaseBleScanner, DiscoveredBleDevice } from "#protocol";
|
|
22
8
|
import type { Peripheral } from "@stoprocent/noble";
|
|
23
9
|
import { NobleBleClient } from "./NobleBleClient.js";
|
|
24
10
|
|
|
25
|
-
|
|
11
|
+
export type { DiscoveredBleDevice } from "#protocol";
|
|
26
12
|
|
|
27
|
-
export type
|
|
28
|
-
deviceData: CommissionableDeviceData;
|
|
29
|
-
peripheral: Peripheral;
|
|
30
|
-
hasAdditionalAdvertisementData: boolean;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type CommissionableDeviceData = CommissionableDevice & {
|
|
34
|
-
SD: number; // Additional Field for Short discriminator
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export class BleScanner implements Scanner {
|
|
38
|
-
readonly type = ChannelType.BLE;
|
|
13
|
+
export type NobleDiscoveredBleDevice = Omit<DiscoveredBleDevice, "peripheral"> & { peripheral: Peripheral };
|
|
39
14
|
|
|
15
|
+
export class BleScanner extends BaseBleScanner {
|
|
40
16
|
readonly #nobleClient: NobleBleClient;
|
|
41
|
-
readonly #recordWaiters = new Map<
|
|
42
|
-
string,
|
|
43
|
-
{
|
|
44
|
-
resolver: () => void;
|
|
45
|
-
timer?: Timer;
|
|
46
|
-
resolveOnUpdatedRecords: boolean;
|
|
47
|
-
cancelResolver?: (value: void) => void;
|
|
48
|
-
}
|
|
49
|
-
>();
|
|
50
|
-
readonly #discoveredMatterDevices = new Map<string, DiscoveredBleDevice>();
|
|
51
17
|
|
|
52
18
|
constructor(nobleClient: NobleBleClient) {
|
|
19
|
+
super(nobleClient);
|
|
53
20
|
this.#nobleClient = nobleClient;
|
|
54
|
-
this.#nobleClient.setDiscoveryCallback((address, manufacturerData) =>
|
|
55
|
-
this.#handleDiscoveredDevice(address, manufacturerData),
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public getDiscoveredDevice(address: string): DiscoveredBleDevice {
|
|
60
|
-
const device = this.#discoveredMatterDevices.get(address);
|
|
61
|
-
if (device === undefined) {
|
|
62
|
-
throw new BleError(`No device found for address ${address}`);
|
|
63
|
-
}
|
|
64
|
-
return device;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Registers a deferred promise for a specific queryId together with a timeout and return the promise.
|
|
69
|
-
* The promise will be resolved when the timer runs out latest.
|
|
70
|
-
*/
|
|
71
|
-
async #registerWaiterPromise(
|
|
72
|
-
queryId: string,
|
|
73
|
-
timeout?: Duration,
|
|
74
|
-
resolveOnUpdatedRecords = true,
|
|
75
|
-
cancelResolver?: (value: void) => void,
|
|
76
|
-
) {
|
|
77
|
-
const { promise, resolver } = createPromise<void>();
|
|
78
|
-
let timer;
|
|
79
|
-
if (timeout) {
|
|
80
|
-
timer = Time.getTimer("BLE query timeout", timeout, () => {
|
|
81
|
-
cancelResolver?.();
|
|
82
|
-
this.#finishWaiter(queryId, true);
|
|
83
|
-
}).start();
|
|
84
|
-
}
|
|
85
|
-
this.#recordWaiters.set(queryId, { resolver, timer, resolveOnUpdatedRecords, cancelResolver });
|
|
86
|
-
logger.debug(
|
|
87
|
-
`Registered waiter for query ${queryId} with timeout ${timeout === undefined ? "(none)" : Duration.format(timeout)} ${
|
|
88
|
-
resolveOnUpdatedRecords ? "" : " (not resolving on updated records)"
|
|
89
|
-
}`,
|
|
90
|
-
);
|
|
91
|
-
await promise;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Remove a waiter promise for a specific queryId and stop the connected timer. If required also resolve the
|
|
96
|
-
* promise.
|
|
97
|
-
*/
|
|
98
|
-
#finishWaiter(queryId: string, resolvePromise: boolean, isUpdatedRecord = false) {
|
|
99
|
-
const waiter = this.#recordWaiters.get(queryId);
|
|
100
|
-
if (waiter === undefined) return;
|
|
101
|
-
const { timer, resolver, resolveOnUpdatedRecords } = waiter;
|
|
102
|
-
if (isUpdatedRecord && !resolveOnUpdatedRecords) return;
|
|
103
|
-
logger.debug(`Finishing waiter for query ${queryId}, resolving: ${resolvePromise}`);
|
|
104
|
-
timer?.stop();
|
|
105
|
-
if (resolvePromise) {
|
|
106
|
-
resolver();
|
|
107
|
-
}
|
|
108
|
-
this.#recordWaiters.delete(queryId);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
cancelCommissionableDeviceDiscovery(identifier: CommissionableDeviceIdentifiers, resolvePromise = true) {
|
|
112
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
113
|
-
const { cancelResolver } = this.#recordWaiters.get(queryKey) ?? {};
|
|
114
|
-
// Mark as canceled to not loop further in discovery, if cancel-resolver is used
|
|
115
|
-
cancelResolver?.();
|
|
116
|
-
this.#finishWaiter(queryKey, resolvePromise);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
#handleDiscoveredDevice(peripheral: Peripheral, manufacturerServiceData: Bytes) {
|
|
120
|
-
const address = peripheral.address;
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
const { discriminator, vendorId, productId, hasAdditionalAdvertisementData } =
|
|
124
|
-
BtpCodec.decodeBleAdvertisementServiceData(manufacturerServiceData);
|
|
125
|
-
|
|
126
|
-
const deviceData: CommissionableDeviceData = {
|
|
127
|
-
deviceIdentifier: address,
|
|
128
|
-
D: discriminator,
|
|
129
|
-
SD: (discriminator >> 8) & 0x0f,
|
|
130
|
-
VP: `${vendorId}+${productId}`,
|
|
131
|
-
CM: 1, // Can be no other mode,
|
|
132
|
-
addresses: [{ type: "ble", peripheralAddress: address }],
|
|
133
|
-
};
|
|
134
|
-
const deviceExisting = this.#discoveredMatterDevices.has(address);
|
|
135
|
-
|
|
136
|
-
logger.debug(
|
|
137
|
-
`${deviceExisting ? "Re-" : ""}Discovered device ${address} data: ${Diagnostic.json(deviceData)}`,
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
this.#discoveredMatterDevices.set(address, {
|
|
141
|
-
deviceData,
|
|
142
|
-
peripheral,
|
|
143
|
-
hasAdditionalAdvertisementData,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
const queryKey = this.#findCommissionableQueryIdentifier(deviceData);
|
|
147
|
-
if (queryKey !== undefined) {
|
|
148
|
-
this.#finishWaiter(queryKey, true, deviceExisting);
|
|
149
|
-
}
|
|
150
|
-
} catch (error) {
|
|
151
|
-
logger.debug(
|
|
152
|
-
`Discovered device ${address} ${manufacturerServiceData === undefined ? undefined : Bytes.toHex(manufacturerServiceData)} does not seem to be a valid Matter device: ${error}`,
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
#findCommissionableQueryIdentifier(record: CommissionableDeviceData) {
|
|
158
|
-
const longDiscriminatorQueryId = this.#buildCommissionableQueryIdentifier({ longDiscriminator: record.D });
|
|
159
|
-
if (this.#recordWaiters.has(longDiscriminatorQueryId)) {
|
|
160
|
-
return longDiscriminatorQueryId;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const shortDiscriminatorQueryId = this.#buildCommissionableQueryIdentifier({ shortDiscriminator: record.SD });
|
|
164
|
-
if (this.#recordWaiters.has(shortDiscriminatorQueryId)) {
|
|
165
|
-
return shortDiscriminatorQueryId;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (record.VP !== undefined) {
|
|
169
|
-
const vpParts = record.VP.split("+");
|
|
170
|
-
const vendorIdQueryId = this.#buildCommissionableQueryIdentifier({
|
|
171
|
-
vendorId: VendorId(parseInt(vpParts[0])),
|
|
172
|
-
});
|
|
173
|
-
if (this.#recordWaiters.has(vendorIdQueryId)) {
|
|
174
|
-
return vendorIdQueryId;
|
|
175
|
-
}
|
|
176
|
-
if (vpParts[1] !== undefined) {
|
|
177
|
-
const productIdQueryId = this.#buildCommissionableQueryIdentifier({
|
|
178
|
-
productId: parseInt(vpParts[1]),
|
|
179
|
-
});
|
|
180
|
-
if (this.#recordWaiters.has(productIdQueryId)) {
|
|
181
|
-
return productIdQueryId;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (this.#recordWaiters.has("*")) {
|
|
187
|
-
return "*";
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return undefined;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Builds an identifier string for commissionable queries based on the given identifier object.
|
|
195
|
-
* Some identifiers are identical to the official DNS-SD identifiers, others are custom.
|
|
196
|
-
*/
|
|
197
|
-
#buildCommissionableQueryIdentifier(identifier: CommissionableDeviceIdentifiers) {
|
|
198
|
-
if ("longDiscriminator" in identifier) {
|
|
199
|
-
return `D:${identifier.longDiscriminator}`;
|
|
200
|
-
} else if ("shortDiscriminator" in identifier) {
|
|
201
|
-
return `SD:${identifier.shortDiscriminator}`;
|
|
202
|
-
} else if ("vendorId" in identifier) {
|
|
203
|
-
return `V:${identifier.vendorId}`;
|
|
204
|
-
} else if ("productId" in identifier) {
|
|
205
|
-
// Custom identifier because normally productId is only included in TXT record
|
|
206
|
-
return `P:${identifier.productId}`;
|
|
207
|
-
} else return "*";
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
#getCommissionableDevices(identifier: CommissionableDeviceIdentifiers) {
|
|
211
|
-
const storedRecords = Array.from(this.#discoveredMatterDevices.values());
|
|
212
|
-
|
|
213
|
-
const foundRecords = new Array<DiscoveredBleDevice>();
|
|
214
|
-
if ("longDiscriminator" in identifier) {
|
|
215
|
-
foundRecords.push(...storedRecords.filter(({ deviceData: { D } }) => D === identifier.longDiscriminator));
|
|
216
|
-
} else if ("shortDiscriminator" in identifier) {
|
|
217
|
-
foundRecords.push(
|
|
218
|
-
...storedRecords.filter(({ deviceData: { SD } }) => SD === identifier.shortDiscriminator),
|
|
219
|
-
);
|
|
220
|
-
} else if ("vendorId" in identifier) {
|
|
221
|
-
foundRecords.push(
|
|
222
|
-
...storedRecords.filter(
|
|
223
|
-
({ deviceData: { VP } }) =>
|
|
224
|
-
VP === `${identifier.vendorId}` || VP?.startsWith(`${identifier.vendorId}+`),
|
|
225
|
-
),
|
|
226
|
-
);
|
|
227
|
-
} else if ("productId" in identifier) {
|
|
228
|
-
foundRecords.push(
|
|
229
|
-
...storedRecords.filter(({ deviceData: { VP } }) => VP?.endsWith(`+${identifier.productId}`)),
|
|
230
|
-
);
|
|
231
|
-
} else {
|
|
232
|
-
foundRecords.push(...storedRecords.filter(({ deviceData: { CM } }) => CM === 1 || CM === 2));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return foundRecords;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
async findOperationalDevice(): Promise<undefined> {
|
|
239
|
-
logger.info(`skip BLE scan because scanning for operational devices is not supported`);
|
|
240
|
-
return undefined;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
getDiscoveredOperationalDevice(): undefined {
|
|
244
|
-
logger.info(`skip BLE scan because scanning for operational devices is not supported`);
|
|
245
|
-
return undefined;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
async findCommissionableDevices(
|
|
249
|
-
identifier: CommissionableDeviceIdentifiers,
|
|
250
|
-
timeout = Seconds(10),
|
|
251
|
-
ignoreExistingRecords = false,
|
|
252
|
-
): Promise<CommissionableDevice[]> {
|
|
253
|
-
let storedRecords = this.#getCommissionableDevices(identifier);
|
|
254
|
-
if (ignoreExistingRecords) {
|
|
255
|
-
// We want to have a fresh discovery result, so clear out the stored records because they might be outdated
|
|
256
|
-
for (const record of storedRecords) {
|
|
257
|
-
this.#discoveredMatterDevices.delete(record.peripheral.address);
|
|
258
|
-
}
|
|
259
|
-
storedRecords = [];
|
|
260
|
-
}
|
|
261
|
-
if (storedRecords.length === 0) {
|
|
262
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
263
|
-
|
|
264
|
-
await this.#nobleClient.startScanning();
|
|
265
|
-
await this.#registerWaiterPromise(queryKey, timeout);
|
|
266
|
-
|
|
267
|
-
storedRecords = this.#getCommissionableDevices(identifier);
|
|
268
|
-
await this.#nobleClient.stopScanning();
|
|
269
|
-
}
|
|
270
|
-
return storedRecords.map(({ deviceData }) => deviceData);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async findCommissionableDevicesContinuously(
|
|
274
|
-
identifier: CommissionableDeviceIdentifiers,
|
|
275
|
-
callback: (device: CommissionableDevice) => void,
|
|
276
|
-
timeout?: Duration,
|
|
277
|
-
cancelSignal?: Promise<void>,
|
|
278
|
-
): Promise<CommissionableDevice[]> {
|
|
279
|
-
const discoveredDevices = new Set<string>();
|
|
280
|
-
|
|
281
|
-
const discoveryEndTime = timeout ? Time.nowMs + timeout : undefined;
|
|
282
|
-
const queryKey = this.#buildCommissionableQueryIdentifier(identifier);
|
|
283
|
-
await this.#nobleClient.startScanning();
|
|
284
|
-
|
|
285
|
-
let queryResolver: ((value: void) => void) | undefined;
|
|
286
|
-
if (cancelSignal === undefined) {
|
|
287
|
-
const { promise, resolver } = createPromise<void>();
|
|
288
|
-
cancelSignal = promise;
|
|
289
|
-
queryResolver = resolver;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
let canceled = false;
|
|
293
|
-
cancelSignal?.then(
|
|
294
|
-
() => {
|
|
295
|
-
canceled = true;
|
|
296
|
-
this.#finishWaiter(queryKey, true);
|
|
297
|
-
},
|
|
298
|
-
cause => {
|
|
299
|
-
logger.error("Unexpected error canceling commissioning", cause);
|
|
300
|
-
},
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
while (!canceled) {
|
|
304
|
-
this.#getCommissionableDevices(identifier).forEach(({ deviceData }) => {
|
|
305
|
-
const { deviceIdentifier } = deviceData;
|
|
306
|
-
if (!discoveredDevices.has(deviceIdentifier)) {
|
|
307
|
-
discoveredDevices.add(deviceIdentifier);
|
|
308
|
-
callback(deviceData);
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
let remainingTime;
|
|
313
|
-
if (discoveryEndTime !== undefined) {
|
|
314
|
-
remainingTime = Millis.ceil(Timespan(Time.nowMs, discoveryEndTime).duration);
|
|
315
|
-
if (remainingTime <= 0) {
|
|
316
|
-
break;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
await this.#registerWaiterPromise(queryKey, remainingTime, false, queryResolver);
|
|
321
|
-
}
|
|
322
|
-
await this.#nobleClient.stopScanning();
|
|
323
|
-
return this.#getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
|
|
324
21
|
}
|
|
325
22
|
|
|
326
|
-
|
|
327
|
-
return
|
|
23
|
+
override getDiscoveredDevice(address: string): NobleDiscoveredBleDevice {
|
|
24
|
+
return super.getDiscoveredDevice(address) as NobleDiscoveredBleDevice;
|
|
328
25
|
}
|
|
329
26
|
|
|
330
|
-
|
|
27
|
+
protected override closeClient() {
|
|
331
28
|
this.#nobleClient.close();
|
|
332
|
-
[...this.#recordWaiters.keys()].forEach(queryId =>
|
|
333
|
-
this.#finishWaiter(queryId, !!this.#recordWaiters.get(queryId)?.timer),
|
|
334
|
-
);
|
|
335
29
|
}
|
|
336
30
|
}
|