Skip to content

Commit

Permalink
Fix issue that slip decoder get sucked after port reopening
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Mar 25, 2023
1 parent a441db1 commit 9c63e88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
35 changes: 11 additions & 24 deletions src/esptool/utils/unpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,28 @@ export default function unpack(queue: Buffer, data: Buffer): { queue: Buffer, pa
queue = Buffer.concat([queue, data]);
const packets = [];
let pi = 0, qi = 0;
let packet = null;
let packet = Buffer.alloc(queue.length);
while (qi < queue.length) {
if (queue[qi] == 0xC0) {
if (packet == null) { // start
packet = Buffer.alloc(queue.length);
} else { // end
if (pi > 0) {
packets.push(packet.slice(0, pi));
packet = null;
pi = 0;
packet = Buffer.alloc(queue.length);
}
pi = 0;
qi += 1;
} else if (qi < queue.length - 1 && queue[qi] == 0xDB && queue[qi + 1] == 0xDC) {
if (packet != null) {
packet[pi] = 0xC0;
pi += 1;
}
packet[pi] = 0xC0;
pi += 1;
qi += 2;
} else if (qi < queue.length - 1 && queue[qi] == 0xDB && queue[qi + 1] == 0xDD) {
if (packet != null) {
packet[pi] = 0xDB;
pi += 1;
}
packet[pi] = 0xDB;
pi += 1;
qi += 2;
} else {
if (packet != null) {
packet[pi] = queue[qi];
pi += 1;
}
packet[pi] = queue[qi];
pi += 1;
qi += 1;
}
}
if (packet != null) {
packet = Buffer.concat([Buffer.from([0xC0]), packet.slice(0, pi)]);
} else {
packet = Buffer.alloc(0);
}
return { queue: packet, packets };
return { queue: packet.slice(0, pi), packets };
}
31 changes: 28 additions & 3 deletions tests/esptool.unpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ test('unpack(C0 00 01 02 03 C0 | C0 04 05 06 07 C0)', () => {
expect(queueOut.toString('hex')).toBe(queueExpect.toString('hex'));
});

test('unpack(C0 00 01 02 03 C0 | CC DD C0 EE FF | C0 04 05 06 07 C0)', () => {
const queueIn: Buffer = Buffer.alloc(0);

const input: Buffer = Buffer.from([
0xC0, 0x00, 0x01, 0x02, 0x03, 0xC0,
0xCC, 0xDD, 0xC0, 0xEE, 0xFF,
0xC0, 0x04, 0x05, 0x06, 0x07, 0xC0,
]);

const outputExpect: Buffer[] = [
Buffer.from([0x00, 0x01, 0x02, 0x03]),
Buffer.from([0xCC, 0xDD]),
Buffer.from([0xEE, 0xFF]),
Buffer.from([0x04, 0x05, 0x06, 0x07]),
];

const queueExpect: Buffer = Buffer.from([
]);

const { queue: queueOut, packets: outputActual } = unpack(queueIn, input);

expect(toHexList(outputActual)).toStrictEqual(toHexList(outputExpect));
expect(queueOut.toString('hex')).toBe(queueExpect.toString('hex'));
});

test('unpack(C0 00 01 02)', () => {
const queueIn: Buffer = Buffer.alloc(0);

Expand All @@ -157,7 +182,7 @@ test('unpack(C0 00 01 02)', () => {
];

const queueExpect: Buffer = Buffer.from([
0xC0, 0x00, 0x01, 0x02,
0x00, 0x01, 0x02,
]);

const { queue: queueOut, packets: outputActual } = unpack(queueIn, input);
Expand All @@ -179,7 +204,7 @@ test('unpack(C0 00 01 02 03 C0 | C0 04 05)', () => {
];

const queueExpect: Buffer = Buffer.from([
0xC0, 0x04, 0x05,
0x04, 0x05,
]);

const { queue: queueOut, packets: outputActual } = unpack(queueIn, input);
Expand Down Expand Up @@ -209,7 +234,7 @@ test('unpack(C0 00 01 02 03 C0 | C0 04 05 + 06 07 C0)', () => {
];

const queue1Expect: Buffer = Buffer.from([
0xC0, 0x04, 0x05,
0x04, 0x05,
]);

const queue2Expect: Buffer = Buffer.from([
Expand Down

0 comments on commit 9c63e88

Please sign in to comment.