From ce97c410f82eca09476065f9cb31699d68783c61 Mon Sep 17 00:00:00 2001 From: Brian Ignacio Date: Fri, 14 Jul 2023 20:00:19 +0800 Subject: [PATCH] add tracing for Transport class --- src/webserial.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/webserial.ts b/src/webserial.ts index 2a320eb..e8fcfe0 100644 --- a/src/webserial.ts +++ b/src/webserial.ts @@ -3,7 +3,7 @@ class Transport { public left_over = new Uint8Array(0); public baudrate = 0; - constructor(public device: SerialPort) {} + constructor(public device: SerialPort, public tracing = false) {} get_info() { const info = this.device.getInfo(); @@ -16,6 +16,30 @@ class Transport { return this.device.getInfo().usbProductId; } + hexDump(buffer: Uint8Array) { + const bufferStr = String.fromCharCode.apply(String, [].slice.call(buffer)); + const BLOCK_SIZE = 16; + const lines = []; + const hex = "0123456789ABCDEF"; + + for (let b = 0; b < bufferStr.length; b += BLOCK_SIZE) { + const block = bufferStr.slice(b, Math.min(b + BLOCK_SIZE, bufferStr.length)); + const addr = "0000" + b.toString(16).slice(-4); + let codes = block + .split("") + .map((ch) => { + let code = ch.charCodeAt(0); + return " " + hex[(0xf0 & code) >> 4] + hex[0x0f & code]; + }) + .join(""); + codes += " ".repeat(BLOCK_SIZE - block.length); + let chars = block.replace(/[\x00-\x1F\x20]/g, "."); + chars += " ".repeat(BLOCK_SIZE - block.length); + lines.push(`${addr} ${codes} ${chars}`); + } + return lines.join("\n"); + } + slip_writer(data: Uint8Array) { let count_esc = 0; let i = 0, @@ -52,6 +76,10 @@ class Transport { if (this.device.writable) { const writer = this.device.writable.getWriter(); + if (this.tracing) { + console.log("Write bytes"); + console.log(this.hexDump(out_data)); + } await writer.write(out_data); writer.releaseLock(); } @@ -148,8 +176,19 @@ class Transport { } reader.releaseLock(); } + + if (this.tracing) { + console.log("Read bytes"); + console.log(this.hexDump(packet)); + } + if (this.slip_reader_enabled) { - return this.slip_reader(packet); + const slipReaderResult = this.slip_reader(packet); + if (this.tracing) { + console.log("Slip reader results"); + console.log(this.hexDump(slipReaderResult)); + } + return slipReaderResult; } return packet; } @@ -175,6 +214,10 @@ class Transport { if (done) { throw new Error("Timeout"); } + if (this.tracing) { + console.log("Raw Read bytes"); + console.log(value); + } return value; } finally { if (timeout > 0) {