import { LogSocket } from '../../log' ; import { DzrpBufferRemote , CONNECTION_TIMEOUT } from './dzrpbufferremote' ; import { Socket } from 'net' ; import { Settings } from '../../settings' ; import { DzrpMachineType } from '../dzrp/dzrpremote' ; import { GenericWatchpoint } from '../../genericwatchpoint' ; /**
 * The CSpect Remote.
 * It connects via socket with CSpect.
 * Or better: with the DeZog plugin for CSpect.
 * The CSpect DeZog plugin internally communicates with the
 * CSpect debugger.
 */ export class CSpectRemote extends DzrpBufferRemote { public socket : Socket ; constructor ( ) { super ( ) ; this . cmdRespTimeoutTime = Settings . launch . cspect . socketTimeout * 1000 ; } public async doInitialization ( ) { this . socket = new Socket ( ) ; this . socket . unref ( ) ; this . socket . on ( 'connect' , async ( ) => { LogSocket . log ( 'CSpectRemote: Connected to server!' ) ; this . receivedData = Buffer . alloc ( 0 ) ; this . expectedLength = 4 ; this . receivingHeader = true ; this . stopChunkTimeout ( ) ; if ( Settings . launch . history . codeCoverageEnabled ) { this . emit ( 'warning' , "launch.json: codeCoverageEnabled==true: CSpect does not support code coverage." ) ; } this . onConnect ( ) ; } ) ; this . socket . on ( 'close' , hadError => { LogSocket . log ( 'CSpectRemote: closed connection: ' + hadError ) ; console . log ( 'Close.' ) ; const err = new Error ( 'CSpect plugin terminated the connection!' ) ; this . emit ( 'error' , err ) ; } ) ; this . socket . on ( 'error' , err => { LogSocket . log ( 'CSpectRemote: Error: ' + err ) ; console . log ( 'Error: ' , err ) ; this . emit ( 'error' , err ) ; } ) ; this . socket . on ( 'data' , data => { this . dataReceived ( data ) ; } ) ; this . socket . setTimeout ( CONNECTION_TIMEOUT ) ; const port = Settings . launch . cspect . port ; const hostname = Settings . launch . cspect . hostname ; this . socket . connect ( port , hostname ) ; } /**
     * This will disconnect the socket.
     */ public async disconnect ( ) { if ( ! this . socket ) return ; await this . pause ( ) ; return new Promise < void > ( resolve => { this . socket ?. removeAllListeners ( ) ; const timeout = setTimeout ( ( ) => { if ( resolve ) { resolve ( ) ; } } , 1000 ) ; this . socket ?. end ( ( ) => { if ( resolve ) { resolve ( ) ; clearTimeout ( timeout ) ; } } ) ; this . socket = undefined as any ; } ) ; } /**
     * Writes the buffer to the socket port.
     */ protected async sendBuffer ( buffer ) { return new Promise < void > ( resolve => { const txt = this . dzrpCmdBufferToString ( buffer ) ; LogSocket . log ( '>>> CSpectRemote: Sending ' + txt ) ; this . socket . write ( buffer , ( ) => { resolve ( ) ; } ) ; } ) ; } /**
     * Watchpoints and WPMEM is disabled for CSpect for now.
     * There is a problem in CSpect: If a read-breakpoint is set it
     * can happen that the PC is not incremented anymore or that the
     * ISR routine is entered for every instruction. It's not on Mike's priority list, so I disable them here.
     * REMARK: Enable CSpect watchpoints when problem is solved in CSpect.
     */ public async enableWPMEM ( enable ) { if ( this . wpmemWatchpoints . length > 0 ) { throw Error ( "There is no support for watchpoints for CSpect." ) ; } } param wp public async setWatchpoint ( wp ) { throw Error ( "Watchpoints not supported." ) ; } /**
     * State saving is not supported in CSpect.
     */ public async stateSave ( filePath ) { throw Error ( "Saving and restoring the state is not supported with CSpect." ) ; } public async stateRestore ( filePath ) { throw Error ( "Saving and restoring the state is not supported with CSpect." ) ; } returns protected async sendDzrpCmdInit ( ) { const result = await super . sendDzrpCmdInit ( ) ; if ( result . error ) { result . error += "\nTry updating the DeZog (CSpect) Plugin (https://github.com/maziac/DeZogPlugin/releases)." ; } ; return result ; } /**
     * Not used/supported.
     */ protected async sendDzrpCmdSetBreakpoints ( bpAddresses ) { throw Error ( "'sendDzrpCmdSetBreakpoints' is not implemented.'" ) ; } /**
     * Not used/supported.
     */ protected async sendDzrpCmdRestoreMem ( elems ) { throw Error ( "'sendDzrpCmdRestoreMem' is not implemented." ) ; } }