Skip to content

Commit

Permalink
add tracing for Transport class
Browse files Browse the repository at this point in the history
  • Loading branch information
brianignacio5 committed Jul 14, 2023
1 parent 58df4cd commit ce97c41
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/webserial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -16,6 +16,30 @@ class Transport {
return this.device.getInfo().usbProductId;
}

hexDump(buffer: Uint8Array) {
const bufferStr = String.fromCharCode.apply(String, [].slice.call(buffer));

Check failure on line 20 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

Use the spread operator instead of '.apply()'
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);

Check failure on line 31 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

'code' is never reassigned. Use 'const' instead
return " " + hex[(0xf0 & code) >> 4] + hex[0x0f & code];
})
.join("");
codes += " ".repeat(BLOCK_SIZE - block.length);
let chars = block.replace(/[\x00-\x1F\x20]/g, ".");

Check failure on line 36 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected control character(s) in regular expression: \x00, \x1f
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,
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit ce97c41

Please sign in to comment.