-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gatt.ts
65 lines (52 loc) · 1.58 KB
/
Gatt.ts
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
import { inspect } from 'util';
import { CUSTOM, InspectOptionsStylized } from '../Inspect';
import { GattService } from './Service';
/**
* A local or remote GATT server.
*/
export abstract class Gatt {
/**
* True if this is a remote GATT server, false otherwise.
*/
public abstract get isRemote(): boolean;
/**
* The services that belong to this GATT server, mapped by UUID.
* If this is a remote GATT use {@link discoverServices} to discover them.
*/
public readonly services: Map<string, GattService> = new Map();
protected _mtu: number;
/**
* Local: The maximum MTU that will be agreed upon during negotiation.
* Remote: The MTU that was agreed upon during negotiation.
*/
public get mtu(): number {
return this._mtu;
}
public constructor(mtu?: number, services?: GattService[]) {
this._mtu = mtu;
if (services) {
for (const service of services) {
this.services.set(service.uuid, service);
}
}
}
public toString(): string {
return JSON.stringify(this.toJSON());
}
public toJSON(): Record<string, unknown> {
return {
mtu: this.mtu,
services: this.services.size
};
}
public [CUSTOM](depth: number, options: InspectOptionsStylized): string {
const name = this.constructor.name;
if (depth < 0) {
return options.stylize(`[${name}]`, 'special');
}
const newOptions = { ...options, depth: options.depth === null ? null : options.depth - 1 };
const padding = ' '.repeat(name.length + 1);
const inner = inspect(this.toJSON(), newOptions).replace(/\n/g, `\n${padding}`);
return `${options.stylize(name, 'special')} ${inner}`;
}
}