|
| 1 | +import { TYPE_SINGLE, TYPE_START, TYPE_CONT, TYPE_END, HEADER_TYPE_MASK, HEADER_SEQ_MASK, ATT_OVERHEAD, MIN_PAYLOAD, MAX_GATT_VALUE_LEN, } from "./constants.js"; |
| 2 | +export class Framer { |
| 3 | + buffer = new Uint8Array(0); |
| 4 | + totalLength = 0; |
| 5 | + inProgress = false; |
| 6 | + expectedSeq = 0; |
| 7 | + feed(packet) { |
| 8 | + if (packet.length === 0) { |
| 9 | + return null; |
| 10 | + } |
| 11 | + const header = packet[0]; |
| 12 | + const packetType = header & HEADER_TYPE_MASK; |
| 13 | + const seqId = header & HEADER_SEQ_MASK; |
| 14 | + const payload = packet.slice(1); |
| 15 | + if (packetType === TYPE_SINGLE) { |
| 16 | + return new TextDecoder().decode(payload); |
| 17 | + } |
| 18 | + if (packetType === TYPE_START) { |
| 19 | + if (payload.length < 4) { |
| 20 | + this.reset(); |
| 21 | + return null; |
| 22 | + } |
| 23 | + const dataView = new DataView(payload.buffer, payload.byteOffset, payload.byteLength); |
| 24 | + this.totalLength = dataView.getUint32(0, false); |
| 25 | + this.buffer = payload.slice(4); |
| 26 | + this.inProgress = true; |
| 27 | + this.expectedSeq = (seqId + 1) & HEADER_SEQ_MASK; |
| 28 | + return null; |
| 29 | + } |
| 30 | + if (!this.inProgress) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + if (seqId !== this.expectedSeq) { |
| 34 | + this.reset(); |
| 35 | + return null; |
| 36 | + } |
| 37 | + this.expectedSeq = (this.expectedSeq + 1) & HEADER_SEQ_MASK; |
| 38 | + if (packetType === TYPE_CONT) { |
| 39 | + const newBuffer = new Uint8Array(this.buffer.length + payload.length); |
| 40 | + newBuffer.set(this.buffer); |
| 41 | + newBuffer.set(payload, this.buffer.length); |
| 42 | + this.buffer = newBuffer; |
| 43 | + return null; |
| 44 | + } |
| 45 | + if (packetType === TYPE_END) { |
| 46 | + const newBuffer = new Uint8Array(this.buffer.length + payload.length); |
| 47 | + newBuffer.set(this.buffer); |
| 48 | + newBuffer.set(payload, this.buffer.length); |
| 49 | + this.buffer = newBuffer; |
| 50 | + if (this.buffer.length !== this.totalLength) { |
| 51 | + this.reset(); |
| 52 | + return null; |
| 53 | + } |
| 54 | + const message = new TextDecoder().decode(this.buffer); |
| 55 | + this.reset(); |
| 56 | + return message; |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + reset() { |
| 61 | + this.buffer = new Uint8Array(0); |
| 62 | + this.totalLength = 0; |
| 63 | + this.inProgress = false; |
| 64 | + this.expectedSeq = 0; |
| 65 | + } |
| 66 | +} |
| 67 | +export function computeMaxPayload(mtu, maxGattValueLen = MAX_GATT_VALUE_LEN, minPayload = MIN_PAYLOAD) { |
| 68 | + let maxPayload = mtu - ATT_OVERHEAD; |
| 69 | + if (maxPayload < minPayload) { |
| 70 | + maxPayload = minPayload; |
| 71 | + } |
| 72 | + if (maxPayload > maxGattValueLen) { |
| 73 | + maxPayload = maxGattValueLen; |
| 74 | + } |
| 75 | + return maxPayload; |
| 76 | +} |
| 77 | +export function packetizeJson(message, maxPayload) { |
| 78 | + const data = new TextEncoder().encode(message); |
| 79 | + const totalLen = data.length; |
| 80 | + if (totalLen + 1 <= maxPayload) { |
| 81 | + const packet = new Uint8Array(1 + totalLen); |
| 82 | + packet[0] = TYPE_SINGLE | 0; |
| 83 | + packet.set(data, 1); |
| 84 | + return { packets: [packet], maxPayload }; |
| 85 | + } |
| 86 | + if (maxPayload <= 5) { |
| 87 | + throw new Error("MTU too small"); |
| 88 | + } |
| 89 | + const packets = []; |
| 90 | + let offset = 0; |
| 91 | + let seq = 0; |
| 92 | + const startChunkSize = maxPayload - 5; |
| 93 | + const startChunk = data.slice(offset, offset + startChunkSize); |
| 94 | + const startPacket = new Uint8Array(1 + 4 + startChunk.length); |
| 95 | + startPacket[0] = TYPE_START | (seq & HEADER_SEQ_MASK); |
| 96 | + const totalLenView = new DataView(startPacket.buffer, startPacket.byteOffset + 1, 4); |
| 97 | + totalLenView.setUint32(0, totalLen, false); |
| 98 | + startPacket.set(startChunk, 5); |
| 99 | + packets.push(startPacket); |
| 100 | + offset += startChunk.length; |
| 101 | + seq = (seq + 1) & 0xff; |
| 102 | + const contChunkSize = maxPayload - 1; |
| 103 | + while (offset < totalLen) { |
| 104 | + const remaining = totalLen - offset; |
| 105 | + if (remaining > contChunkSize) { |
| 106 | + const chunk = data.slice(offset, offset + contChunkSize); |
| 107 | + const packet = new Uint8Array(1 + chunk.length); |
| 108 | + packet[0] = TYPE_CONT | (seq & HEADER_SEQ_MASK); |
| 109 | + packet.set(chunk, 1); |
| 110 | + packets.push(packet); |
| 111 | + offset += chunk.length; |
| 112 | + } |
| 113 | + else { |
| 114 | + const chunk = data.slice(offset, offset + remaining); |
| 115 | + const packet = new Uint8Array(1 + chunk.length); |
| 116 | + packet[0] = TYPE_END | (seq & HEADER_SEQ_MASK); |
| 117 | + packet.set(chunk, 1); |
| 118 | + packets.push(packet); |
| 119 | + offset += chunk.length; |
| 120 | + } |
| 121 | + seq = (seq + 1) & 0xff; |
| 122 | + } |
| 123 | + return { packets, maxPayload }; |
| 124 | +} |
0 commit comments