|
| 1 | +import { spawn } from 'child_process'; |
| 2 | +import EventEmitter from 'events'; |
| 3 | + |
| 4 | +/* |
| 5 | +interface AdvancedDecoder { |
| 6 | + init( |
| 7 | + config: SWOAdvancedDecoderConfig, |
| 8 | + outputData: (output: string) => void, |
| 9 | + graphData: (data: number, id: string) => void |
| 10 | + ): void; |
| 11 | + typeName(): string; |
| 12 | + outputLabel(): string; |
| 13 | + softwareEvent(port: number, data: Buffer): void; |
| 14 | + synchronized(): void; |
| 15 | + lostSynchronization(): void; |
| 16 | +}*/ |
| 17 | + |
| 18 | +class DefmtDecoder extends EventEmitter { |
| 19 | + constructor() { |
| 20 | + this.process = null; |
| 21 | + this.elfPath = null; |
| 22 | + this.displayOutput = null; |
| 23 | + this.graphData = null; |
| 24 | + } |
| 25 | + |
| 26 | + init(config, displayOutput, graphData) { |
| 27 | + // Store the callbacks and elfPath from the config |
| 28 | + this.elfPath = config.config.elfPath; |
| 29 | + this.displayOutput = displayOutput; |
| 30 | + this.graphData = graphData; |
| 31 | + |
| 32 | + const defmtPrintPath = `${process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME}/.cargo/bin/defmt-print${process.platform === "win32" ? ".exe" : ""}`; |
| 33 | + |
| 34 | + // Spawn the defmt-print process with the provided ELF path |
| 35 | + this.process = spawn(defmtPrintPath, ['-e', this.elfPath, "stdin"]); |
| 36 | + |
| 37 | + // Handle data from defmt-print stdout and relay it to the displayOutput callback |
| 38 | + this.process.stdout.on('data', (data) => { |
| 39 | + if (this.displayOutput) { |
| 40 | + this.displayOutput(data.toString()); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + // Handle errors from defmt-print stderr |
| 45 | + this.process.stderr.on('data', (data) => { |
| 46 | + if (this.displayOutput) { |
| 47 | + //this.displayOutput(`Error: ${data.toString()}`); |
| 48 | + this.displayOutput(data.toString()); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + // Handle when the process closes |
| 53 | + this.process.on('close', (code) => { |
| 54 | + if (this.displayOutput) { |
| 55 | + this.displayOutput(`Decoding process exited with code: ${code}`); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + //sendData(input: Buffer): void; |
| 61 | + sendData(input) { |
| 62 | + // Write input data to defmt-print's stdin |
| 63 | + try { |
| 64 | + if (this.process && this.process.stdin.writable) { |
| 65 | + this.process.stdin.write(input); |
| 66 | + return; |
| 67 | + } |
| 68 | + } catch { } |
| 69 | + |
| 70 | + throw new Error('Process stdin is not writable.'); |
| 71 | + } |
| 72 | + |
| 73 | + // Expected methods from the SWODecoder API conforming to the AdvancedDecoder interface |
| 74 | + |
| 75 | + //typeName(): string; |
| 76 | + typeName() { |
| 77 | + return 'DefmtDecoder'; |
| 78 | + } |
| 79 | + |
| 80 | + //outputLabel(): string; |
| 81 | + outputLabel() { |
| 82 | + return 'RPi Pico'; |
| 83 | + } |
| 84 | + |
| 85 | + //softwareEvent(port: number, data: Buffer): void; |
| 86 | + softwareEvent(port, data) { |
| 87 | + if (this.ports.indexOf(port) !== -1) { |
| 88 | + // Handle the software event, potentially by sending data to defmt-print stdin |
| 89 | + this.sendData(data); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + //synchronized(): void; |
| 94 | + synchronized() { |
| 95 | + // Handle the synchronized event |
| 96 | + if (this.displayOutput) { |
| 97 | + this.displayOutput('Synchronized'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + //lostSynchronization(): void; |
| 102 | + lostSynchronization() { |
| 103 | + // Handle the lost synchronization event |
| 104 | + if (this.displayOutput) { |
| 105 | + this.displayOutput('Lost synchronization'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // own dispose method |
| 110 | + |
| 111 | + dispose() { |
| 112 | + // Clean up the process |
| 113 | + if (this.process) { |
| 114 | + this.process.kill(); |
| 115 | + this.process = null; |
| 116 | + } |
| 117 | + this.emit('dispose'); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +export default DefmtDecoder; |
0 commit comments