From 595ccd5c6d2e0033dce8d289f38f8ae1fdbd0085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=CC=8Ctefan=20S=CC=8Cimek?= Date: Wed, 15 Jan 2025 09:57:19 +0100 Subject: [PATCH] refactor: start bringing common type declarations together --- .vscode/settings.json | 1 + src/backend/mi2/mi2.ts | 6 +- src/common.ts | 77 ++-------------- src/common/types/decoder.ts | 45 +++++++++ src/common/types/grapher.ts | 80 ++++++++++++++++ src/common/types/index.ts | 4 + src/common/types/rtt.ts | 56 ++++++++++++ src/common/types/swo.ts | 84 +++++++++++++++++ src/frontend/cortex_debug_session.ts | 8 +- src/frontend/extension.ts | 11 ++- src/frontend/pty.ts | 3 +- src/frontend/rtt_terminal.ts | 39 +++++--- src/frontend/server_console.ts | 3 +- src/frontend/swo/common.ts | 127 -------------------------- src/frontend/swo/core.ts | 41 ++++----- src/frontend/swo/decoders/advanced.ts | 3 +- src/frontend/swo/decoders/binary.ts | 7 +- src/frontend/swo/decoders/common.ts | 2 +- src/frontend/swo/decoders/console.ts | 7 +- src/frontend/swo/decoders/graph.ts | 5 +- src/grapher/datasource.ts | 3 +- src/grapher/main.ts | 17 ++-- src/grapher/timeseriesgraph.ts | 5 +- src/grapher/types.ts | 54 ----------- src/grapher/xygraph.ts | 3 +- tsconfig.json | 5 +- 26 files changed, 366 insertions(+), 330 deletions(-) create mode 100644 src/common/types/decoder.ts create mode 100644 src/common/types/grapher.ts create mode 100644 src/common/types/index.ts create mode 100644 src/common/types/rtt.ts create mode 100644 src/common/types/swo.ts delete mode 100644 src/frontend/swo/common.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index cf5983e6..016266c9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "editor.tabSize": 4, "editor.insertSpaces": true, "editor.useTabStops": false, + "files.trimTrailingWhitespace": false, "files.exclude": { "out": false, "dist": true, diff --git a/src/backend/mi2/mi2.ts b/src/backend/mi2/mi2.ts index addfdfe6..9c299f54 100644 --- a/src/backend/mi2/mi2.ts +++ b/src/backend/mi2/mi2.ts @@ -239,14 +239,14 @@ export class MI2 extends EventEmitter implements IBackend { } } - private onOutputStderr(lines) { - lines = lines.split('\n') as string[]; + private onOutputStderr(text: string) { + const lines = text.split('\n'); lines.forEach((line) => { this.log('stderr', line); }); } - private onOutputPartial(line) { + private onOutputPartial(line: string) { if (couldBeOutput(line)) { this.logNoNewLine('stdout', line); return true; diff --git a/src/common.ts b/src/common.ts index 0de30170..3cda3e89 100644 --- a/src/common.ts +++ b/src/common.ts @@ -9,6 +9,8 @@ import * as os from 'os'; import * as stream from 'stream'; import * as path from 'path'; import { GDBDebugSession } from './gdb'; +import { SWOConfiguration } from './common/types/swo'; +import { RTTConfiguration, RTTDecoderOpts } from "./common/types/rtt"; const readline = require('readline'); export enum ADAPTER_DEBUG_MODE { @@ -88,36 +90,6 @@ export class SWOConfigureEvent extends Event implements DebugProtocol.Event { } } -export enum TerminalInputMode { - COOKED = 'cooked', - RAW = 'raw', - RAWECHO = 'rawecho', - DISABLED = 'disabled' -} -export interface RTTCommonDecoderOpts { - type: string; // 'console', 'graph', ... - tcpPort: string; // [hostname:]port - port: number; // RTT Channel number - - // Following two used for 'Advanced' category - tcpPorts: string[]; - ports: number[]; -} - -export enum TextEncoding { - UTF8 = 'utf8', - UTF16LE = 'utf16le', - ASCII = 'ascii', - UCS2 = 'ucs2' -} - -export enum BinaryEncoding { - UNSIGNED = 'unsigned', - SIGNED = 'signed', - Q1616 = 'Q16.16', - FLOAT = 'float' -} - export interface CTIOpenOCDConfig { enabled: boolean; initCommands: string[]; @@ -125,25 +97,10 @@ export interface CTIOpenOCDConfig { resumeCommands: string[]; } -export interface RTTConsoleDecoderOpts extends RTTCommonDecoderOpts { - // Console options - label: string; // label for window - prompt: string; // Prompt to use - noprompt: boolean; // disable prompt - noclear: boolean; // do not clear screen buffer on connect - logfile: string; // log IO to file - inputmode: TerminalInputMode; - iencoding: TextEncoding; // Encoding used for input - timestamp: boolean; - // Binary only options - scale: number; - encoding: BinaryEncoding; -} - export class RTTConfigureEvent extends Event implements DebugProtocol.Event { public body: { type: string, // Currently, only 'socket' is supported - decoder: RTTCommonDecoderOpts; + decoder: RTTDecoderOpts; }; public event: string; @@ -196,28 +153,6 @@ export interface ChainedConfigurations { inherits: string[]; } -export interface SWOConfiguration { - enabled: boolean; - cpuFrequency: number; - swoFrequency: number; - decoders: any[]; - profile: boolean; - source: string; - swoPort: string; - swoPath: string; -} - -export interface RTTConfiguration { - enabled: boolean; - address: string; - searchSize: number; - searchId: string; - clearSearch: boolean; - polling_interval: number; - rtt_start_retry: number; - decoders: RTTCommonDecoderOpts[]; -} - export interface ElfSection { name: string; address: number; // New base address @@ -419,7 +354,7 @@ export class RTTServerHelper { // basis depending on the gdb-server type const dummy = '??'; for (const dec of cfg.decoders) { - if (dec.ports && (dec.ports.length > 0)) { + if (dec.type === 'advanced') { dec.tcpPorts = []; for (const p of dec.ports) { this.rttLocalPortMap[p] = dummy; @@ -433,7 +368,7 @@ export class RTTServerHelper { const portFinderOpts = { min: startPort, max: startPort + 2000, retrieve: count, consecutive: false }; return TcpPortScanner.findFreePorts(portFinderOpts, GDBServer.LOCALHOST).then((ports) => { for (const dec of cfg.decoders) { - if (dec.ports && (dec.ports.length > 0)) { + if (dec.type === 'advanced') { dec.tcpPorts = []; for (const p of dec.ports) { let str = this.rttLocalPortMap[p]; @@ -459,7 +394,7 @@ export class RTTServerHelper { let ret = false; if (cfg.enabled) { for (const dec of cfg.decoders) { - if (dec.tcpPort || dec.tcpPorts) { + if ((dec.type === 'advanced' && dec.tcpPorts) || (dec.type !== 'advanced' && dec.tcpPort)) { obj.emit('event', new RTTConfigureEvent({ type: 'socket', decoder: dec diff --git a/src/common/types/decoder.ts b/src/common/types/decoder.ts new file mode 100644 index 00000000..3936ae8b --- /dev/null +++ b/src/common/types/decoder.ts @@ -0,0 +1,45 @@ +export enum BinaryEncoding { + UNSIGNED = 'unsigned', + SIGNED = 'signed', + Q1616 = 'q16.16', + FLOAT = 'float' +} + +export enum TextEncoding { + UTF8 = 'utf8', + UTF16LE = 'utf16le', + ASCII = 'ascii', + UCS2 = 'ucs2' +} + +export interface CommonDecoderConfig { + logfile: string; +} + +export interface BasicDecoderConfig extends CommonDecoderConfig { + port: number; +} + +export interface ConsoleDecoderConfig extends BasicDecoderConfig { + label: string; + encoding: TextEncoding; + timestamp: boolean; +} + +export interface BinaryDecoderConfig extends BasicDecoderConfig { + encoding: BinaryEncoding; + scale: number; + label: string; +} + +export interface GraphDecoderConfig extends BasicDecoderConfig { + encoding: BinaryEncoding; + scale: number; + graphId: string; +} + +export interface AdvancedDecoderConfig { + decoder: string; + config: any; + ports: number[]; +} diff --git a/src/common/types/grapher.ts b/src/common/types/grapher.ts new file mode 100644 index 00000000..43233fd8 --- /dev/null +++ b/src/common/types/grapher.ts @@ -0,0 +1,80 @@ +import { SWOProfileConfig } from './swo'; + +export interface CommonGraphConfiguration { + label: string; +} + +export interface TimeseriesGraphConfiguration extends CommonGraphConfiguration { + type: 'realtime'; + minimum: number; + maximum: number; + timespan: number; + plots: Array<{ + graphId: string, + label: string, + color: string + }>; +} + +export interface XYGraphConfiguration extends CommonGraphConfiguration { + type: 'x-y-plot'; + xPort: number; + yPort: number; + xMinimum: number; + xMaximum: number; + yMinimum: number; + yMaximum: number; + initialX: number; + initialY: number; + timespan: number; + xGraphId: string; + yGraphId: string; +} + +export type GraphConfiguration = TimeseriesGraphConfiguration | XYGraphConfiguration; + +export interface GrapherMessageBase { + timestamp?: number; +} + +export interface GrapherUnsupportedMessage extends GrapherMessageBase { + type: ''; +} + +export interface GrapherInitMessage extends GrapherMessageBase { + type: 'init'; +} + +export interface GrapherStatusMessage extends GrapherMessageBase { + type: 'status'; + status: 'stopped' | 'terminated' | 'continued'; +} + +export interface GrapherDataMessage extends GrapherMessageBase { + type: 'data'; + id: string; + data: number; +} + +export type GrapherProgramStats = Array<[ string, number ]>; + +export interface GrapherProgramCounterMessage extends GrapherMessageBase { + type: 'program-counter'; + function: string; + counter: number; +} + +export interface GrapherConfigurationMessage extends GrapherMessageBase { + type: 'configure'; + graphs: [ GraphConfiguration ]; + profile: SWOProfileConfig; + status: 'stopped' | 'terminated' | 'continued'; +} + +export type GrapherMessage = + GrapherUnsupportedMessage | + GrapherInitMessage | + GrapherStatusMessage | + GrapherDataMessage | + GrapherProgramCounterMessage | + GrapherConfigurationMessage; diff --git a/src/common/types/index.ts b/src/common/types/index.ts new file mode 100644 index 00000000..b92ddeba --- /dev/null +++ b/src/common/types/index.ts @@ -0,0 +1,4 @@ +export * from './decoder'; +export * from './grapher'; +export * from './rtt'; +export * from './swo'; diff --git a/src/common/types/rtt.ts b/src/common/types/rtt.ts new file mode 100644 index 00000000..e3eec238 --- /dev/null +++ b/src/common/types/rtt.ts @@ -0,0 +1,56 @@ +import { AdvancedDecoderConfig, BasicDecoderConfig, BinaryDecoderConfig, ConsoleDecoderConfig, GraphDecoderConfig } from './decoder'; + +export enum TerminalInputMode { + COOKED = 'cooked', + RAW = 'raw', + RAWECHO = 'rawecho', + DISABLED = 'disabled' +} + +export interface RTTCommonDecoderOpts { + noclear: boolean; // do not clear screen buffer on connect + logfile: string; // log IO to file +} + +export interface RTTBasicDecoderOpts extends RTTCommonDecoderOpts, BasicDecoderConfig { + tcpPort: string; // [hostname:]port +} + +export interface RTTCommonTerminalDecoderOpts extends RTTBasicDecoderOpts { + label: string; // label for window + prompt: string; // Prompt to use + noprompt: boolean; // disable prompt + inputmode: TerminalInputMode; +} + +export interface RTTConsoleDecoderOpts extends RTTBasicDecoderOpts, RTTCommonTerminalDecoderOpts, ConsoleDecoderConfig { + type: 'console'; +} + +export interface RTTBinaryDecoderOpts extends RTTBasicDecoderOpts, RTTCommonTerminalDecoderOpts, BinaryDecoderConfig { + type: 'binary'; +} + +export type RTTTerminalDecoderOpts = RTTBinaryDecoderOpts | RTTConsoleDecoderOpts; + +export interface RTTGraphDecoderOpts extends RTTBasicDecoderOpts, GraphDecoderConfig { + type: 'graph'; +} + +export interface RTTAdvancedDecoderOpts extends RTTCommonDecoderOpts, AdvancedDecoderConfig { + type: 'advanced'; + tcpPorts: string[]; +} + +export type RTTDecoderOpts = RTTConsoleDecoderOpts | RTTBinaryDecoderOpts | RTTGraphDecoderOpts | RTTAdvancedDecoderOpts; + +export interface RTTConfiguration { + enabled: boolean; + address: string; + searchSize: number; + searchId: string; + clearSearch: boolean; + polling_interval: number; + rtt_start_retry: number; + decoders: RTTDecoderOpts[]; +} diff --git a/src/common/types/swo.ts b/src/common/types/swo.ts new file mode 100644 index 00000000..dd46e269 --- /dev/null +++ b/src/common/types/swo.ts @@ -0,0 +1,84 @@ +import { AdvancedDecoderConfig, BinaryDecoderConfig, ConsoleDecoderConfig, GraphDecoderConfig } from './decoder'; + +export interface SWOConsoleDecoderConfig extends ConsoleDecoderConfig { + type: 'console'; + showOnStartup: boolean; +} + +export interface SWOBinaryDecoderConfig extends BinaryDecoderConfig { + type: 'binary'; +} + +export interface SWOGraphDecoderConfig extends GraphDecoderConfig { + type: 'graph'; +} + +export interface SWOAdvancedDecoderConfig extends AdvancedDecoderConfig { + type: 'advanced'; +} + +export type SWODecoderConfig = + SWOConsoleDecoderConfig | + SWOBinaryDecoderConfig | + SWOGraphDecoderConfig | + SWOAdvancedDecoderConfig; + +export interface SWOProfileConfig { + interval: number; + window: number; + mapping: SWOProfileMapping[]; +} + +export interface SWOProfileMapping { + match: string; + replace: string; +} + +export interface SWOConfiguration { + enabled: boolean; + cpuFrequency: number; + swoFrequency: number; + decoders: SWODecoderConfig[]; + profile: boolean | SWOProfileConfig; + source: string; + swoPort: string; + swoPath: string; +} + +export interface AdvancedDecoder { + init( + config: SWOAdvancedDecoderConfig, + outputData: (output: string) => void, + graphData: (data: number, id: string) => void + ): void; + typeName(): string; + outputLabel(): string; + softwareEvent(port: number, data: Buffer): void; + synchronized(): void; + lostSynchronization(): void; +} + +export enum PacketType { + HARDWARE = 1, + SOFTWARE, + TIMESTAMP +} + +export enum TimestampType { + CURRENT, + DELAYED, + EVENT_DELAYED, + EVENT_TIME_DELAYED +} + +export interface TimestampPacket { + type: TimestampType; + timestamp: number; +} + +export interface Packet { + type: PacketType; + port: number; + size: number; + data: Buffer; +} diff --git a/src/frontend/cortex_debug_session.ts b/src/frontend/cortex_debug_session.ts index 9c25ff2d..2ad07bf4 100644 --- a/src/frontend/cortex_debug_session.ts +++ b/src/frontend/cortex_debug_session.ts @@ -90,20 +90,20 @@ export class CDebugSession { } } - public static FindSession(session: vscode.DebugSession) { + public static FindSession(session: vscode.DebugSession): CDebugSession { return CDebugSession.FindSessionById(session.id); } - public static FindSessionById(id: string) { + public static FindSessionById(id: string): CDebugSession { const ret = CDebugSession.CurrentSessions.find((x) => x.session.id === id); return ret; } - public static GetSession(session: vscode.DebugSession, config?: ConfigurationArguments | undefined): CDebugSession { + public static GetSession(session: vscode.DebugSession, config?: ConfigurationArguments): CDebugSession { const prev = CDebugSession.FindSessionById(session.id); if (prev) { prev.config = config || prev.config; return prev; } - return new CDebugSession(session, config || session.configuration); + return new CDebugSession(session, config || session.configuration as any as ConfigurationArguments); } // Call this method after session actually started. It inserts new session into the session tree diff --git a/src/frontend/extension.ts b/src/frontend/extension.ts index f445f917..1eac7182 100644 --- a/src/frontend/extension.ts +++ b/src/frontend/extension.ts @@ -6,7 +6,7 @@ import { LiveWatchTreeProvider, LiveVariableNode } from './views/live-watch'; import { RTTCore, SWOCore } from './swo/core'; import { - ConfigurationArguments, RTTCommonDecoderOpts, RTTConsoleDecoderOpts, + ConfigurationArguments, CortexDebugKeys, ChainedEvents, ADAPTER_DEBUG_MODE, ChainedConfig } from '../common'; import { MemoryContentProvider } from './memory_content_provider'; import Reporting from '../reporting'; @@ -22,6 +22,7 @@ import { RTTTerminal } from './rtt_terminal'; import { GDBServerConsole } from './server_console'; import { CDebugSession, CDebugChainedSessionItem } from './cortex_debug_session'; import { ServerConsoleLog } from '../backend/server'; +import { RTTBinaryDecoderOpts, RTTConsoleDecoderOpts, RTTDecoderOpts, RTTTerminalDecoderOpts } from '@common/types'; const commandExistsSync = require('command-exists').sync; interface SVDInfo { @@ -774,13 +775,13 @@ export class CortexDebugExtension { private receivedRTTConfigureEvent(e: vscode.DebugSessionCustomEvent) { if (e.body.type === 'socket') { - const decoder: RTTCommonDecoderOpts = e.body.decoder; + const decoder: RTTDecoderOpts = e.body.decoder; if ((decoder.type === 'console') || (decoder.type === 'binary')) { Reporting.sendEvent('RTT', 'Source', 'Socket: Console'); - this.rttCreateTerninal(e, decoder as RTTConsoleDecoderOpts); + this.rttCreateTerninal(e, decoder); } else { Reporting.sendEvent('RTT', 'Source', `Socket: ${decoder.type}`); - if (!decoder.ports) { + if (decoder.type !== 'advanced') { this.createRTTSource(e, decoder.tcpPort, decoder.port); } else { for (let ix = 0; ix < decoder.ports.length; ix = ix + 1) { @@ -830,7 +831,7 @@ export class CortexDebugExtension { }); } - private rttCreateTerninal(e: vscode.DebugSessionCustomEvent, decoder: RTTConsoleDecoderOpts) { + private rttCreateTerninal(e: vscode.DebugSessionCustomEvent, decoder: RTTTerminalDecoderOpts) { this.createRTTSource(e, decoder.tcpPort, decoder.port).then((src: SocketRTTSource) => { for (const terminal of this.rttTerminals) { const success = !terminal.inUse && terminal.tryReuse(decoder, src); diff --git a/src/frontend/pty.ts b/src/frontend/pty.ts index f33890a8..b970d7a3 100644 --- a/src/frontend/pty.ts +++ b/src/frontend/pty.ts @@ -1,8 +1,9 @@ import EventEmitter = require('events'); import * as os from 'os'; import * as vscode from 'vscode'; -import { ResettableTimeout, TerminalInputMode } from '../common'; +import { ResettableTimeout } from '../common'; import { BR_MAGENTA_FG, CSI, RESET } from './ansi-helpers'; +import { TerminalInputMode } from '@common/types'; const KEYS = { enter : '\r', diff --git a/src/frontend/rtt_terminal.ts b/src/frontend/rtt_terminal.ts index 4ff0ed1c..962e42f9 100644 --- a/src/frontend/rtt_terminal.ts +++ b/src/frontend/rtt_terminal.ts @@ -1,10 +1,11 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; -import { RTTConsoleDecoderOpts, TerminalInputMode, TextEncoding, BinaryEncoding, HrTimer } from '../common'; +import { HrTimer } from '../common'; import { IPtyTerminalOptions, magentaWrite, PtyTerminal } from './pty'; import { decoders as DECODER_MAP } from './swo/decoders/utils'; import { SocketRTTSource } from './swo/sources/socket'; import { RESET } from './ansi-helpers'; +import { BinaryEncoding, RTTDecoderOpts, RTTTerminalDecoderOpts, TerminalInputMode, TextEncoding } from '@common/types'; export class RTTTerminal { protected ptyTerm: PtyTerminal; @@ -21,7 +22,7 @@ export class RTTTerminal { constructor( protected context: vscode.ExtensionContext, - public options: RTTConsoleDecoderOpts, + public options: RTTTerminalDecoderOpts, src: SocketRTTSource) { this.ptyOptions = this.createTermOptions(null); this.createTerminal(); @@ -31,7 +32,9 @@ export class RTTTerminal { private connectToSource(src: SocketRTTSource) { this.hrTimer = new HrTimer(); - this.binaryFormatter = new BinaryFormatter(this.ptyTerm, this.options.encoding, this.options.scale); + if (this.options.type === 'binary') { + this.binaryFormatter = new BinaryFormatter(this.ptyTerm, this.options.encoding, this.options.scale); + } src.once('disconnected', () => { this.onClose(); }); src.on('error', (e) => { const code: string = (e as any).code; @@ -61,6 +64,7 @@ export class RTTTerminal { } private onClose() { + const tcpPort = this.source?.tcpPort; this.source = null; this.inUse = false; if (!this.options.noclear && (this.logFd >= 0)) { @@ -70,7 +74,7 @@ export class RTTTerminal { this.startOfNewLine = true; } this.ptyTerm.write(RESET + '\n'); - magentaWrite(`RTT connection on TCP port ${this.options.tcpPort} ended. Waiting for next connection...`, this.ptyTerm); + magentaWrite(`RTT connection on TCP port ${tcpPort} ended. Waiting for next connection...`, this.ptyTerm); } private onData(data: Buffer) { @@ -140,6 +144,10 @@ export class RTTTerminal { private lastTime: bigint = BigInt(-1); private lastTimeStr: string = ''; private writeNonBinary(buf: Buffer) { + if (this.options.type === 'binary') { + return; + } + let start = 0; let time = ''; if (this.options.timestamp) { @@ -180,15 +188,15 @@ export class RTTTerminal { protected createTerminal() { this.ptyTerm = new PtyTerminal(this.createTermOptions(null)); - this.ptyTerm.on('data', this.sendData.bind(this)); - this.ptyTerm.on('close', this.terminalClosed.bind(this)); + this.ptyTerm.on('data', (data) => this.sendData(data)); + this.ptyTerm.on('close', () => this.terminalClosed()); } protected createPrompt(): string { return this.options.noprompt ? '' : this.options.prompt || `RTT:${this.options.port}> `; } - protected static createTermName(options: RTTConsoleDecoderOpts, existing: string | null): string { + protected static createTermName(options: RTTTerminalDecoderOpts, existing: string | null): string { const suffix = options.type === 'binary' ? `enc:${getBinaryEncoding(options.encoding)}` : options.type; const orig = options.label || `RTT Ch:${options.port} ${suffix}`; let ret = orig; @@ -211,8 +219,9 @@ export class RTTTerminal { if (this.source) { try { if (((typeof str === 'string') || (str instanceof String)) && + (this.options.type === 'console') && (this.options.inputmode === TerminalInputMode.COOKED)) { - str = Buffer.from(str as string, this.options.iencoding); + str = Buffer.from(str as string, this.options.encoding); } this.source.write(str); } @@ -222,9 +231,15 @@ export class RTTTerminal { } } - private sanitizeEncodings(obj: RTTConsoleDecoderOpts) { - obj.encoding = getBinaryEncoding(obj.encoding); - obj.iencoding = getTextEncoding(obj.iencoding); + private sanitizeEncodings(obj: RTTDecoderOpts) { + switch (obj.type) { + case 'console': + obj.encoding = getTextEncoding(obj.encoding); + break; + case 'binary': + obj.encoding = getBinaryEncoding(obj.encoding); + break; + } } private closeLogFd(reopen: boolean) { @@ -246,7 +261,7 @@ export class RTTTerminal { // If all goes well, this will reset the terminal options. Label for the VSCode terminal has to match // since there no way to rename it. If successful, tt will reset the Terminal options and mark it as // used (inUse = true) as well - public tryReuse(options: RTTConsoleDecoderOpts, src: SocketRTTSource): boolean { + public tryReuse(options: RTTTerminalDecoderOpts, src: SocketRTTSource): boolean { if (!this.ptyTerm || !this.ptyTerm.terminal) { return false; } this.sanitizeEncodings(this.options); const newTermName = RTTTerminal.createTermName(options, this.ptyOptions.name); diff --git a/src/frontend/server_console.ts b/src/frontend/server_console.ts index 07501fa1..8079ee49 100644 --- a/src/frontend/server_console.ts +++ b/src/frontend/server_console.ts @@ -4,7 +4,8 @@ import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import { IPtyTerminalOptions, magentaWrite, PtyTerminal } from './pty'; -import { getAnyFreePort, TerminalInputMode } from '../common'; +import { getAnyFreePort } from '../common'; +import { TerminalInputMode } from '@common/types'; // vscode.commands.executeCommand('workbench.action.terminal.renameWithArg', { name: 'myName' }); diff --git a/src/frontend/swo/common.ts b/src/frontend/swo/common.ts deleted file mode 100644 index f6bf20b5..00000000 --- a/src/frontend/swo/common.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { TextEncoding } from '../../common'; - -export interface SWODecoderConfig { - type: string; -} - -export interface SWOBasicDecoderConfig extends SWODecoderConfig { - port: number; - logfile: string; -} - -export interface SWOConsoleDecoderConfig extends SWOBasicDecoderConfig { - label: string; - encoding: TextEncoding; - showOnStartup: boolean; - timestamp: boolean; -} - -export interface SWOBinaryDecoderConfig extends SWOBasicDecoderConfig { - encoding: string; - scale: number; - label: string; -} - -export interface SWOGraphDecoderConfig extends SWOBasicDecoderConfig { - encoding: string; - scale: number; - graphId: string; -} - -export interface SWOAdvancedDecoderConfig extends SWODecoderConfig { - decoder: string; - config: any; - ports: number[]; -} - -export interface GraphConfiguration { - type: string; - label: string; -} - -export interface TimeseriesGraphConfiguration extends GraphConfiguration { - minimum: number; - maximum: number; - timespan: number; - plots: Array<{ - graphId: string, - label: string, - color: string - }>; -} - -export interface XYGraphConfiguration extends GraphConfiguration { - xPort: number; - yPort: number; - xMinimum: number; - xMaximum: number; - yMinimum: number; - yMaximum: number; - initialX: number; - initialY: number; - timespan: number; - xGraphId: string; - yGraphId: string; -} - -export interface GrapherMessage { - timestamp?: number; - type: 'configure' | 'status' | 'data' | 'program-counter' | 'init'; -} - -export interface GrapherStatusMessage extends GrapherMessage { - status: 'stopped' | 'terminated' | 'continued'; -} - -export interface GrapherDataMessage extends GrapherMessage { - id: string; - data: number; -} - -export interface GrapherProgramCounterMessage extends GrapherMessage { - function: string; - counter: number; -} - -export interface GrapherConfigurationMessage extends GrapherMessage { - graphs: [GraphConfiguration]; - status: 'stopped' | 'terminated' | 'continued'; -} - -export interface AdvancedDecoder { - init( - config: SWOAdvancedDecoderConfig, - outputData: (output: string) => void, - graphData: (data: number, id: string) => void - ): void; - typeName(): string; - outputLabel(): string; - softwareEvent(port: number, data: Buffer): void; - synchronized(): void; - lostSynchronization(): void; -} - -export enum PacketType { - HARDWARE = 1, - SOFTWARE, - TIMESTAMP -} - -export enum TimestampType { - CURRENT, - DELAYED, - EVENT_DELAYED, - EVENT_TIME_DELAYED -} - -export interface TimestampPacket { - type: TimestampType; - timestamp: number; -} - -export interface Packet { - type: PacketType; - port: number; - size: number; - data: Buffer; -} diff --git a/src/frontend/swo/core.ts b/src/frontend/swo/core.ts index d46d52ef..63196fe5 100644 --- a/src/frontend/swo/core.ts +++ b/src/frontend/swo/core.ts @@ -7,17 +7,16 @@ import { SWOBinaryProcessor } from './decoders/binary'; import { SWORTTGraphProcessor } from './decoders/graph'; import { SWORTTDecoder } from './decoders/common'; import { SWORTTSource } from './sources/common'; -import { SWODecoderConfig, GraphConfiguration, SWOAdvancedDecoderConfig, - SWOBinaryDecoderConfig, SWOConsoleDecoderConfig, SWOGraphDecoderConfig, - SWOBasicDecoderConfig, GrapherMessage, GrapherStatusMessage, - GrapherProgramCounterMessage} from './common'; import { SWORTTAdvancedProcessor } from './decoders/advanced'; import { EventEmitter } from 'events'; -import { PacketType, Packet } from './common'; import { parseUnsigned } from './decoders/utils'; import { SymbolInformation } from '../../symbols'; -import { getNonce, RTTCommonDecoderOpts } from '../../common'; +import { getNonce } from '../../common'; import { SocketRTTSource, SocketSWOSource } from './sources/socket'; +import { + GraphConfiguration, GrapherMessage, GrapherProgramCounterMessage, GrapherStatusMessage, + Packet, PacketType, RTTConfiguration, SWOConfiguration, SWODecoderConfig, SWOProfileConfig +} from '@common/types'; const RingBuffer = require('ringbufferjs'); @@ -178,14 +177,8 @@ class ITMDecoder extends EventEmitter { interface ConfigurationArguments { executable: string; - swoConfig: { - enabled: boolean, - decoders: SWODecoderConfig[] - }; - rttConfig: { - enabled: boolean, - decoders: RTTCommonDecoderOpts[] - }; + swoConfig: SWOConfiguration; + rttConfig: RTTConfiguration; graphConfig: GraphConfiguration[]; } @@ -309,19 +302,19 @@ export class SWOCore extends SWORTTCoreBase { switch (conf.type) { case 'console': - this.processors.push(new SWOConsoleProcessor(conf as SWOConsoleDecoderConfig)); + this.processors.push(new SWOConsoleProcessor(conf)); break; case 'binary': - this.processors.push(new SWOBinaryProcessor(conf as SWOBinaryDecoderConfig)); + this.processors.push(new SWOBinaryProcessor(conf)); break; case 'graph': - processor = new SWORTTGraphProcessor(conf as SWOGraphDecoderConfig); + processor = new SWORTTGraphProcessor(conf); if (this.webview) { this.webview.registerProcessors(processor); } this.processors.push(processor); break; case 'advanced': try { - processor = new SWORTTAdvancedProcessor(conf as SWOAdvancedDecoderConfig); + processor = new SWORTTAdvancedProcessor(conf); if (this.webview) { this.webview.registerProcessors(processor); } this.processors.push(processor); } @@ -406,14 +399,12 @@ export class SWOCore extends SWORTTCoreBase { let mask: number = 0; configuration.forEach((c) => { if (c.type === 'advanced') { - const ac = c as SWOAdvancedDecoderConfig; - for (const port of ac.ports) { + for (const port of c.ports) { mask = (mask | (1 << port)) >>> 0; } } else { - const bc = c as SWOBasicDecoderConfig; - mask = (mask | (1 << bc.port)) >>> 0; + mask = (mask | (1 << c.port)) >>> 0; } }); return mask; @@ -456,7 +447,7 @@ class RTTDecoder extends EventEmitter { } public onData(input: string | Buffer) { - const data: Buffer = ((typeof input) === 'string') ? Buffer.from(input) : (input as Buffer) ; + const data: Buffer = (typeof input === 'string') ? Buffer.from(input) : input; for (const elt of data) { this.buffer[this.bytesRead] = elt; this.bytesRead = this.bytesRead + 1; @@ -491,7 +482,7 @@ export class RTTCore extends SWORTTCoreBase { switch (conf.type) { case 'graph': this.addRTTDecoder(this.sources[conf.port]); - const processor = new SWORTTGraphProcessor(conf as any as SWOGraphDecoderConfig); + const processor = new SWORTTGraphProcessor(conf); if (this.webview) { this.webview.registerProcessors(processor); } this.processors.push(processor); break; @@ -500,7 +491,7 @@ export class RTTCore extends SWORTTCoreBase { for (const p of conf.ports) { this.addRTTDecoder(this.sources[p]); } - const processor = new SWORTTAdvancedProcessor(conf as any as SWOAdvancedDecoderConfig); + const processor = new SWORTTAdvancedProcessor(conf); if (this.webview) { this.webview.registerProcessors(processor); } this.processors.push(processor); break; diff --git a/src/frontend/swo/decoders/advanced.ts b/src/frontend/swo/decoders/advanced.ts index 042b099b..6f790170 100644 --- a/src/frontend/swo/decoders/advanced.ts +++ b/src/frontend/swo/decoders/advanced.ts @@ -1,8 +1,7 @@ import * as vscode from 'vscode'; import { SWORTTDecoder } from './common'; -import { SWOAdvancedDecoderConfig, AdvancedDecoder, GrapherDataMessage } from '../common'; import { EventEmitter } from 'events'; -import { Packet } from '../common'; +import { AdvancedDecoder, GrapherDataMessage, Packet, SWOAdvancedDecoderConfig } from '@common/types'; declare function __webpack_require__(); declare const __non_webpack_require__: NodeRequire; diff --git a/src/frontend/swo/decoders/binary.ts b/src/frontend/swo/decoders/binary.ts index 3863ca45..bf301d98 100644 --- a/src/frontend/swo/decoders/binary.ts +++ b/src/frontend/swo/decoders/binary.ts @@ -1,11 +1,10 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; import { SWORTTDecoder } from './common'; -import { SWOBinaryDecoderConfig } from '../common'; import { decoders as DECODER_MAP } from './utils'; -import { Packet } from '../common'; import { IPtyTerminalOptions, PtyTerminal } from '../../pty'; -import { HrTimer, TerminalInputMode } from '../../../common'; +import { HrTimer } from '../../../common'; +import { Packet, SWOBinaryDecoderConfig, TerminalInputMode } from '@common/types'; function parseEncoded(buffer: Buffer, encoding: string) { return DECODER_MAP[encoding] ? DECODER_MAP[encoding](buffer) : DECODER_MAP.unsigned(buffer); @@ -27,7 +26,7 @@ export class SWOBinaryProcessor implements SWORTTDecoder { this.port = config.port; this.scale = config.scale || 1; this.encoding = (config.encoding || 'unsigned').replace('.', '_'); - this.useTerminal = 'useTerminal' in config ? (config as any).useTerminal : true; // TODO: Remove + this.useTerminal = 'useTerminal' in config ? !!config.useTerminal : true; // TODO: Remove if (this.useTerminal) { this.createVSCodeTerminal(config); diff --git a/src/frontend/swo/decoders/common.ts b/src/frontend/swo/decoders/common.ts index dddbc18b..f1b27f8e 100644 --- a/src/frontend/swo/decoders/common.ts +++ b/src/frontend/swo/decoders/common.ts @@ -1,4 +1,4 @@ -import { Packet } from '../common'; +import { Packet } from '@common/types'; export interface SWORTTDecoder { format: string; diff --git a/src/frontend/swo/decoders/console.ts b/src/frontend/swo/decoders/console.ts index 28064b52..44120091 100644 --- a/src/frontend/swo/decoders/console.ts +++ b/src/frontend/swo/decoders/console.ts @@ -2,10 +2,9 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; import { SWORTTDecoder } from './common'; -import { SWOConsoleDecoderConfig } from '../common'; -import { Packet } from '../common'; import { IPtyTerminalOptions, PtyTerminal } from '../../pty'; -import { HrTimer, TerminalInputMode, TextEncoding } from '../../../common'; +import { Packet, SWOConsoleDecoderConfig, TerminalInputMode, TextEncoding } from '@common/types'; +import { HrTimer } from '../../../common'; export class SWOConsoleProcessor implements SWORTTDecoder { private positionCount: number; @@ -27,7 +26,7 @@ export class SWOConsoleProcessor implements SWORTTDecoder { this.port = config.port; this.encoding = config.encoding || TextEncoding.UTF8; this.timestamp = !!config.timestamp; - this.useTerminal = 'useTerminal' in config ? (config as any).useTerminal : true; // TODO: Remove + this.useTerminal = 'useTerminal' in config ? !!config.useTerminal : true; // TODO: Remove if (this.useTerminal) { this.createVSCodeTerminal(config); } else { diff --git a/src/frontend/swo/decoders/graph.ts b/src/frontend/swo/decoders/graph.ts index 010ee34a..15849d78 100644 --- a/src/frontend/swo/decoders/graph.ts +++ b/src/frontend/swo/decoders/graph.ts @@ -3,8 +3,7 @@ import * as fs from 'fs'; import { SWORTTDecoder } from './common'; import { decoders as DECODER_MAP } from './utils'; import { EventEmitter } from 'events'; -import { SWOGraphDecoderConfig, GrapherDataMessage } from '../common'; -import { Packet } from '../common'; +import { GraphDecoderConfig, GrapherDataMessage, Packet } from '@common/types'; function parseEncoded(buffer: Buffer, encoding: string) { return DECODER_MAP[encoding] ? DECODER_MAP[encoding](buffer) : DECODER_MAP.unsigned(buffer); @@ -19,7 +18,7 @@ export class SWORTTGraphProcessor extends EventEmitter implements SWORTTDecoder private logFd: number = -1; private logfile: string; - constructor(config: SWOGraphDecoderConfig) { + constructor(config: GraphDecoderConfig) { super(); // core.socketServer.registerProcessor(this); this.port = config.port; diff --git a/src/grapher/datasource.ts b/src/grapher/datasource.ts index 3012a619..3514daee 100644 --- a/src/grapher/datasource.ts +++ b/src/grapher/datasource.ts @@ -1,4 +1,5 @@ -import { GraphPoint, GrapherDataMessage, GrapherProgramCounterMessage } from './types'; +import { GrapherDataMessage, GrapherProgramCounterMessage } from '@common/types'; +import { GraphPoint } from './types'; export class GraphDataSource { private data: { diff --git a/src/grapher/main.ts b/src/grapher/main.ts index d646427c..1fc103fb 100644 --- a/src/grapher/main.ts +++ b/src/grapher/main.ts @@ -1,8 +1,9 @@ import { TimeseriesGraph } from './timeseriesgraph'; import { XYGraph } from './xygraph'; import { ProgramStatsGraph } from './programstatsgraph'; -import { Graph, GrapherConfigurationMessage, TimeseriesGraphConfiguration, XYGraphConfiguration, GrapherMessage, GrapherDataMessage, GrapherStatusMessage, GrapherProgramCounterMessage } from './types'; +import { Graph } from './types'; import { GraphDataSource } from './datasource'; +import { GrapherConfigurationMessage, GrapherDataMessage, GrapherMessage, GrapherProgramCounterMessage, GrapherStatusMessage } from '@common/types'; interface VSCodeAPI { postMessage(msg: any): void; @@ -26,14 +27,14 @@ function init() { function processConfiguration(message: GrapherConfigurationMessage) { window.datasource = new GraphDataSource(); - message.graphs.forEach((config: any) => { + message.graphs.forEach((config) => { if (config.type === 'realtime') { - const graph = new TimeseriesGraph(config as TimeseriesGraphConfiguration, window.datasource); + const graph = new TimeseriesGraph(config, window.datasource); graphs.push(graph); if (message.status === 'stopped' || message.status === 'terminated') { graph.stop(); } } else if (config.type === 'x-y-plot') { - const graph = new XYGraph(config as XYGraphConfiguration, window.datasource); + const graph = new XYGraph(config, window.datasource); graphs.push(graph); if (message.status === 'stopped' || message.status === 'terminated') { graph.stop(); } } @@ -67,16 +68,16 @@ function init() { const message: GrapherMessage = event.data; switch (message.type) { case 'configure': - processConfiguration(message as GrapherConfigurationMessage); + processConfiguration(message); break; case 'data': - processData(message as GrapherDataMessage); + processData(message); break; case 'status': - processStatus(message as GrapherStatusMessage); + processStatus(message); break; case 'program-counter': - processProgramCounter(message as GrapherProgramCounterMessage); + processProgramCounter(message); break; default: console.log(`Got unrecognized message type: ${message.type}`); diff --git a/src/grapher/timeseriesgraph.ts b/src/grapher/timeseriesgraph.ts index e84c04a6..3c4c01bd 100644 --- a/src/grapher/timeseriesgraph.ts +++ b/src/grapher/timeseriesgraph.ts @@ -1,6 +1,7 @@ import * as d3 from 'd3'; -import { TimeseriesGraphConfiguration, GraphPoint, Graph } from './types'; +import { GraphPoint, Graph } from './types'; import { GraphDataSource } from './datasource'; +import { TimeseriesGraphConfiguration } from '@common/types'; declare const window: Window; declare global { @@ -171,7 +172,7 @@ export class TimeseriesGraph implements Graph { public updateGraph() { if (!this.stopped) { const now = new Date().getTime(); - this.x.domain([now - this.span, now]); + this.x.domain([ now - this.span, now ]); this.xAxis.call(d3.axisBottom(this.x)); const visAnnotations = this.annotations.filter((a) => a.timestamp >= now - this.span && a.timestamp <= now); diff --git a/src/grapher/types.ts b/src/grapher/types.ts index f4ae5a0e..0f5799ca 100644 --- a/src/grapher/types.ts +++ b/src/grapher/types.ts @@ -1,57 +1,3 @@ -export interface GraphConfiguration { - type: string; - label: string; -} - -export interface TimeseriesGraphConfiguration extends GraphConfiguration { - minimum: number; - maximum: number; - timespan: number; - plots: Array<{ - graphId: string, - label: string, - color: string - }>; -} - -export interface XYGraphConfiguration extends GraphConfiguration { - xPort: number; - yPort: number; - xMinimum: number; - xMaximum: number; - yMinimum: number; - yMaximum: number; - initialX: number; - initialY: number; - timespan: number; - xGraphId: string; - yGraphId: string; -} - -export interface GrapherMessage { - id: number; - timestamp: number; - type: 'configure' | 'status' | 'data' | 'program-counter' | 'init'; -} - -export interface GrapherStatusMessage extends GrapherMessage { - status: 'stopped' | 'terminated' | 'continued'; -} - -export interface GrapherDataMessage extends GrapherMessage { - data: number; -} - -export interface GrapherProgramCounterMessage extends GrapherMessage { - function: string; - counter: number; -} - -export interface GrapherConfigurationMessage extends GrapherMessage { - graphs: [GraphConfiguration]; - status: 'stopped' | 'terminated' | 'continued'; -} - export interface Graph { stop(): void; continue(): void; diff --git a/src/grapher/xygraph.ts b/src/grapher/xygraph.ts index 470e73cd..33fe638c 100644 --- a/src/grapher/xygraph.ts +++ b/src/grapher/xygraph.ts @@ -1,6 +1,7 @@ import * as d3 from 'd3'; -import { Graph, XYGraphConfiguration, GraphPoint } from './types'; +import { Graph, GraphPoint } from './types'; import { GraphDataSource } from './datasource'; +import { XYGraphConfiguration } from '@common/types'; declare const window: Window; declare global { diff --git a/tsconfig.json b/tsconfig.json index 41d5eb45..ea57fcc1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,10 @@ "rootDir": ".", "plugins": [ { "name": "typescript-tslint-plugin" } - ] + ], + "paths": { + "@common/*": ["./src/common/*"], + } }, "exclude": [ "node_modules",