-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathBle.ts
More file actions
71 lines (60 loc) · 2.61 KB
/
Ble.ts
File metadata and controls
71 lines (60 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @license
* Copyright 2022-2026 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { Bytes, Channel, ChannelType, ConnectedChannel, Duration, Observable, Transport } from "@matter/general";
import { Scanner } from "../common/Scanner.js";
import { PeerCommunicationError } from "../peer/PeerCommunicationError.js";
import { MatterBle } from "./BleConsts.js";
export class BleError extends PeerCommunicationError {}
/** Thrown when a BLE write or subscribe operation fails because the peripheral disconnected. */
export class BleDisconnectedError extends BleError {}
/**
* Fired as the `cause` when the BLE transport under a PASE session closes, triggering a
* force-close of the session so pending exchanges reject immediately instead of waiting
* for MRP timeouts.
*/
export class BleChannelClosedError extends BleDisconnectedError {}
// TODO - need to factor out the general platform BLE from Matter/BTP so this can move into matter.js-general
export abstract class Ble {
abstract get peripheralInterface(): BlePeripheralInterface;
abstract get centralInterface(): Transport;
abstract get scanner(): Scanner;
}
export interface BlePeripheralInterface extends Transport {
advertise(advertiseData: Bytes, additionalAdvertisementData?: Bytes, interval?: Duration): Promise<void>;
stopAdvertising(): Promise<void>;
}
export abstract class BleChannel<T extends Bytes = Bytes> implements Channel<T>, ConnectedChannel {
readonly maxPayloadSize = MatterBle.MAX_MATTER_PAYLOAD_SIZE;
readonly isReliable = true as const;
readonly supportsLargeMessages = false;
readonly type = ChannelType.BLE;
abstract name: string;
abstract send(data: T): Promise<void>;
abstract close(): Promise<void>;
abstract onClose(listener: () => void): Transport.Listener;
abstract [Symbol.asyncIterator](): AsyncIterator<Bytes>;
readonly #closed = Observable<[]>();
#closedFired = false;
/**
* Emitted exactly once when the channel is lost (peripheral disconnect, BTP session close,
* or explicit {@link close}). Consumers that hold a secure session over this channel use
* this to force-close the session and reject pending exchanges without waiting for MRP
* timeouts.
*
* Subclasses must call {@link emitClosed} from each of their termination paths. The
* latch guarantees exactly-once semantics regardless of which triggers fire.
*/
get closed() {
return this.#closed;
}
protected emitClosed() {
if (this.#closedFired) {
return;
}
this.#closedFired = true;
this.#closed.emit();
}
}