-
Notifications
You must be signed in to change notification settings - Fork 1
/
Characteristic.ts
315 lines (269 loc) · 8.03 KB
/
Characteristic.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { TypedEmitter } from 'tiny-typed-emitter';
import { inspect } from 'util';
import { CUSTOM, InspectOptionsStylized } from '../Inspect';
import { GattDescriptor } from './Descriptor';
import { GattService } from './Service';
export type ReadFunction = (offset: number) => Promise<Buffer>;
export type WriteFunction = (offset: number, data: Buffer, withoutResponse: boolean) => Promise<number>;
export type GattCharacteristicProperty =
| 'broadcast'
| 'read'
| 'write-without-response'
| 'write'
| 'notify'
| 'indicate'
| 'authenticated-signed-writes'
| 'extended-properties'
| 'reliable-write'
| 'writable-auxiliaries'
| 'authorize';
export interface GattCharacteristicEvents {
notification: (data: Buffer) => void;
}
/**
* Represents a GATT Characteristic.
*/
export abstract class GattCharacteristic extends TypedEmitter<GattCharacteristicEvents> {
private readonly readFunc: ReadFunction;
private readonly writeFunc: WriteFunction;
/**
* The GATT service that this characteristic belongs to.
*/
public readonly service: GattService;
/**
* The UUID of this characteristic, no dashes (-).
*/
public readonly uuid: string;
/**
* True if this is a remote characteristic, false otherwise.
*/
public readonly isRemote: boolean;
/**
* A list of all the properties that are enabled/supported for this characteristic.
*/
public readonly properties: GattCharacteristicProperty[];
/**
* The list of properties supported by this characteristic as a byte flag per the Bluetooth Core spec.
*/
public readonly propertyFlag: number;
/**
* A list of all the properties on this characteristic that are secured.
*/
public readonly secure: GattCharacteristicProperty[];
/**
* The list of all secured properties of this characteristic as a byte flag per the Bluetooth Core spec.
*/
public readonly secureFlag: number;
/**
* The descriptors that belong to this characteristic, mapped by UUID.
* If this is a remote characteristic use {@link discoverDescriptors} to discover them.
*/
public readonly descriptors: Map<string, GattDescriptor> = new Map();
public constructor(
service: GattService,
uuid: string,
isRemote: boolean,
propsOrFlag: number | GattCharacteristicProperty[],
secureOrFlag: number | GattCharacteristicProperty[],
readFuncOrValue?: ReadFunction | Buffer,
writeFunc?: WriteFunction
) {
super();
this.service = service;
this.uuid = uuid;
this.isRemote = isRemote;
let properties: GattCharacteristicProperty[] = [];
let secure: GattCharacteristicProperty[] = [];
let propertyFlag = 0;
let secureFlag = 0;
if (typeof propsOrFlag === 'object') {
properties = propsOrFlag;
if (propsOrFlag.includes('read')) {
propertyFlag |= 0x02;
}
if (propsOrFlag.includes('write-without-response')) {
propertyFlag |= 0x04;
}
if (propsOrFlag.includes('write')) {
propertyFlag |= 0x08;
}
if (propsOrFlag.includes('notify')) {
propertyFlag |= 0x10;
}
if (propsOrFlag.includes('indicate')) {
propertyFlag |= 0x20;
}
} else {
propertyFlag = propsOrFlag;
if (propsOrFlag & 0x01) {
properties.push('broadcast');
}
if (propsOrFlag & 0x02) {
properties.push('read');
}
if (propsOrFlag & 0x04) {
properties.push('write-without-response');
}
if (propsOrFlag & 0x08) {
properties.push('write');
}
if (propsOrFlag & 0x10) {
properties.push('notify');
}
if (propsOrFlag & 0x20) {
properties.push('indicate');
}
if (propsOrFlag & 0x40) {
properties.push('authenticated-signed-writes');
}
if (propsOrFlag & 0x80) {
properties.push('extended-properties');
}
}
if (typeof secureOrFlag === 'object') {
secure = secureOrFlag;
if (secureOrFlag.includes('read')) {
secureFlag |= 0x02;
}
if (secureOrFlag.includes('write-without-response')) {
secureFlag |= 0x04;
}
if (secureOrFlag.includes('write')) {
secureFlag |= 0x08;
}
if (secureOrFlag.includes('notify')) {
secureFlag |= 0x10;
}
if (secureOrFlag.includes('indicate')) {
secureFlag |= 0x20;
}
} else {
secureFlag = secureOrFlag;
if (secureOrFlag & 0x01) {
secure.push('broadcast');
}
if (secureOrFlag & 0x02) {
secure.push('read');
}
if (secureOrFlag & 0x04) {
secure.push('write-without-response');
}
if (secureOrFlag & 0x08) {
secure.push('write');
}
if (secureOrFlag & 0x10) {
secure.push('notify');
}
if (secureOrFlag & 0x20) {
secure.push('indicate');
}
if (secureOrFlag & 0x40) {
secure.push('authenticated-signed-writes');
}
if (secureOrFlag & 0x80) {
secure.push('extended-properties');
}
}
this.properties = properties;
this.secure = secure;
this.propertyFlag = propertyFlag;
this.secureFlag = secureFlag;
if (typeof readFuncOrValue !== 'function') {
this.readFunc = async () => readFuncOrValue;
} else {
this.readFunc = readFuncOrValue;
}
this.writeFunc = writeFunc;
}
/**
* Remote only: Discover all descriptors of this characteristic.
*/
public abstract discoverDescriptors(): Promise<GattDescriptor[]>;
/**
* Remote only: Read the current value of this characteristic.
*/
public abstract read(): Promise<Buffer>;
/**
* Remote only: Write the specified data to this characteristic.
* @param data The data to write.
* @param withoutResponse Do not require a response from the remote GATT server for this write.
*/
public abstract write(data: Buffer, withoutResponse: boolean): Promise<void>;
/**
* Remote only: Enable or disable broadcasts.
* @param broadcast True to enable broadcasts, false otherwise.
*/
public abstract broadcast(broadcast: boolean): Promise<void>;
/**
* Remote only: Enable or disable notifications.
* @param notify True to enable notifies, false otherwise.
*/
public abstract notify(notify: boolean): Promise<void>;
/**
* Remote only: Enable notifications. Equivalent to calling {@link notify} with `true`.
*/
public async subscribe(): Promise<void> {
if (!this.isRemote) {
throw new Error('Can only be used for remote characteristic');
}
await this.notify(true);
}
/**
* Remote only: Disable nofitications. Equivalent to calling {@link notify} with `false`.
*/
public async unsubscribe(): Promise<void> {
if (!this.isRemote) {
throw new Error('Can only be used for remote characteristic');
}
await this.notify(false);
}
/**
* Local only: Adds a descriptor to this characteristic.
*/
public abstract addDescriptor(uuid: string, value: Buffer): Promise<GattDescriptor>;
/**
* Local only: Handles an incoming read request for this characteristic.
* @param offset The offset to start at
* @returns The read data.
*/
public async handleRead(offset: number): Promise<Buffer> {
if (this.isRemote) {
throw new Error('Can only be used for local characteristic');
}
return this.readFunc(offset);
}
/**
* Local only: Handles an incoming write request for this characteristic.
* @param offset The offset to start at.
* @param data The data to write.
* @param withoutResponse True to not produce a response code, false otherwise.
* @returns The result code.
*/
public async handleWrite(offset: number, data: Buffer, withoutResponse: boolean): Promise<number> {
if (this.isRemote) {
throw new Error('Can only be used for local characteristic');
}
return this.writeFunc(offset, data, withoutResponse);
}
public toString(): string {
return JSON.stringify(this.toJSON());
}
public toJSON(): Record<string, unknown> {
return {
uuid: this.uuid,
properties: this.properties,
secure: this.secure,
service: this.service
};
}
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}`;
}
}