Repository navigation
Expand file tree
/
Copy pathgatt-transport.mjs
More file actions
67 lines (65 loc) · 2.78 KB
/
Copy pathgatt-transport.mjs
File metadata and controls
67 lines (65 loc) · 2.78 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
import {unwrap} from './ota-packets.mjs';
// Receives one already-connected device and its resolved GATT characteristics.
// Device selection, compatibility checks and operation authorization belong to the caller.
export class GattTransport {
#device;
#channels;
#busy=false;
#closed=false;
constructor(device, channels) { this.#device=device; this.#channels=channels; }
async exchange(channel, packets, accept, timeout) {
if(this.#closed) throw new Error('Transport closed; outcome may be unknown');
if(this.#busy) throw new Error('Transport busy');
const pair=this.#channels[channel];
if(!pair || !Array.isArray(packets) || !packets.length
|| !packets.every(p=>p instanceof Uint8Array && p.length>=10 && p.length<=240)
|| typeof accept!=='function' || !Number.isFinite(timeout) || timeout<=0) {
throw new Error('Invalid exchange');
}
if(!this.#device.gatt.connected) throw new Error('Device disconnected');
packets=packets.map(p=>p.slice());
this.#busy=true;
try {
return await new Promise((resolve,reject)=>{
let done=false, sending=false, sent=false, response=null;
const cleanup=()=>{
clearTimeout(timer);
pair.notify.removeEventListener('characteristicvaluechanged',onReply);
this.#device.removeEventListener('gattserverdisconnected',onDisconnect);
};
const fail=error=>{
if(done) return;
done=true; this.#closed=true; cleanup(); reject(error);
};
const finish=()=>{
if(done || !sent || response===null) return;
done=true; cleanup(); resolve(response);
};
const onDisconnect=()=>fail(new Error('Device disconnected; no retry'));
const onReply=event=>{
if(done || !sending) return;
const value=event.target.value;
if(!(value instanceof DataView)) return;
const reply=unwrap(new Uint8Array(value.buffer,value.byteOffset,value.byteLength));
try {
if(reply && accept(reply)) { response=reply.payload; finish(); }
} catch(error) { fail(error); }
};
const timer=setTimeout(()=>fail(new Error('OTA reply timeout; no retry')),timeout);
pair.notify.addEventListener('characteristicvaluechanged',onReply);
this.#device.addEventListener('gattserverdisconnected',onDisconnect);
const send=async()=>{
await pair.notify.startNotifications();
for(const packet of packets) {
if(done) return;
if(!this.#device.gatt.connected) { onDisconnect(); return; }
sending=true;
await pair.write.writeValueWithoutResponse(packet);
}
sent=true; finish();
};
send().catch(fail);
});
} finally { this.#busy=false; }
}
}