(input, id)
| 608 | return input; |
| 609 | } |
| 610 | parseReply(input, id) { |
| 611 | if (!input) |
| 612 | throw new Error("no reply to command " + id); |
| 613 | |
| 614 | input = this.unescapeSLIP(input); |
| 615 | |
| 616 | // look for 0xc0 and parse |
| 617 | let offset = 0; |
| 618 | for (let offset = 0, length = input.length; offset < length; offset++) { |
| 619 | if (0xc0 !== input[offset]) |
| 620 | continue; |
| 621 | |
| 622 | if (1 !== input[offset + 1]) // direction is 1 for reply |
| 623 | continue; |
| 624 | |
| 625 | if (id !== input[offset + 2]) // id must match |
| 626 | continue; |
| 627 | |
| 628 | let size = input[offset + 3] | (input[offset + 4] << 8); |
| 629 | if (size < this.#statusSize) // must be big enough to hold status |
| 630 | continue; |
| 631 | let value = input[offset + 5] | (input[offset + 6] << 8) | (input[offset + 7] << 16) | (input[offset + 8] << 24) |
| 632 | |
| 633 | if (0xc0 !== input[offset + 9 + size]) |
| 634 | continue; |
| 635 | |
| 636 | let status = input[offset + 9 + size - this.#statusSize]; |
| 637 | if (0 !== status) |
| 638 | throw new Error(`status error - command ${id} failed`); |
| 639 | |
| 640 | let cmd = {id, size, value}; |
| 641 | if (size > this.#statusSize) |
| 642 | cmd.data = input.slice(offset + 9, offset + 9 + cmd.size - this.#statusSize); |
| 643 | return cmd; |
| 644 | } |
| 645 | } |
| 646 | async writeSLIP(payload, purge = true) { |
| 647 | let escapes = 0; |
| 648 | payload = new Uint8Array(payload); |
no test coverage detected