diff --git a/src/TargetController/Commands/Command.hpp b/src/TargetController/Commands/Command.hpp index bf5beedc..241a3910 100644 --- a/src/TargetController/Commands/Command.hpp +++ b/src/TargetController/Commands/Command.hpp @@ -36,6 +36,10 @@ namespace Bloom::TargetController::Commands return Command::type; } + [[nodiscard]] virtual bool requiresActiveState() const { + return true; + } + [[nodiscard]] virtual bool requiresStoppedTargetState() const { return false; } diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index 634191bd..83967f60 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -170,6 +170,84 @@ namespace Bloom::TargetController // Install Bloom's udev rules if not already installed TargetControllerComponent::checkUdevRules(); + // Register command handlers + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleGetTargetDescriptor, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleGetTargetState, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleStopTargetExecution, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleResumeTargetExecution, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleResetTarget, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleReadTargetRegisters, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleWriteTargetRegisters, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleReadTargetMemory, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleWriteTargetMemory, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleStepTargetExecution, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleSetBreakpoint, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleRemoveBreakpoint, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleSetProgramCounter, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleGetTargetPinStates, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleSetTargetPinState, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleGetTargetStackPointer, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleGetTargetProgramCounter, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleEnableProgrammingMode, this, std::placeholders::_1) + ); + + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleDisableProgrammingMode, this, std::placeholders::_1) + ); + // Register event handlers this->eventListener->registerCallbackForEventType( std::bind(&TargetControllerComponent::onShutdownTargetControllerEvent, this, std::placeholders::_1) @@ -309,14 +387,20 @@ namespace Bloom::TargetController throw Exception("No handler registered for this command."); } - if (command->requiresStoppedTargetState() && this->lastTargetState != TargetState::STOPPED) { - throw Exception("Illegal target state - command requires target to be stopped"); + if (this->state != TargetControllerState::ACTIVE && command->requiresActiveState()) { + throw Exception("Command rejected - TargetController not in active state."); } - if (this->target->programmingModeEnabled() && command->requiresDebugMode()) { - throw Exception( - "Illegal target state - command cannot be serviced whilst the target is in programming mode." - ); + if (this->state == TargetControllerState::ACTIVE) { + if (command->requiresStoppedTargetState() && this->lastTargetState != TargetState::STOPPED) { + throw Exception("Command rejected - command requires target execution to be stopped."); + } + + if (this->target->programmingModeEnabled() && command->requiresDebugMode()) { + throw Exception( + "Command rejected - command cannot be serviced whilst the target is in programming mode." + ); + } } this->registerCommandResponse( @@ -411,26 +495,6 @@ namespace Bloom::TargetController + std::string(exception.what())); } - this->deregisterCommandHandler(GetTargetDescriptor::type); - this->deregisterCommandHandler(GetTargetState::type); - this->deregisterCommandHandler(StopTargetExecution::type); - this->deregisterCommandHandler(ResumeTargetExecution::type); - this->deregisterCommandHandler(ResetTarget::type); - this->deregisterCommandHandler(ReadTargetRegisters::type); - this->deregisterCommandHandler(WriteTargetRegisters::type); - this->deregisterCommandHandler(ReadTargetMemory::type); - this->deregisterCommandHandler(WriteTargetMemory::type); - this->deregisterCommandHandler(StepTargetExecution::type); - this->deregisterCommandHandler(SetBreakpoint::type); - this->deregisterCommandHandler(RemoveBreakpoint::type); - this->deregisterCommandHandler(SetTargetProgramCounter::type); - this->deregisterCommandHandler(GetTargetPinStates::type); - this->deregisterCommandHandler(SetTargetPinState::type); - this->deregisterCommandHandler(GetTargetStackPointer::type); - this->deregisterCommandHandler(GetTargetProgramCounter::type); - this->deregisterCommandHandler(EnableProgrammingMode::type); - this->deregisterCommandHandler(DisableProgrammingMode::type); - this->eventListener->deregisterCallbacksForEventType(); this->lastTargetState = TargetState::UNKNOWN; @@ -448,82 +512,6 @@ namespace Bloom::TargetController this->acquireHardware(); this->loadRegisterDescriptors(); - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleGetTargetDescriptor, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleGetTargetState, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleStopTargetExecution, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleResumeTargetExecution, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleResetTarget, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleReadTargetRegisters, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleWriteTargetRegisters, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleReadTargetMemory, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleWriteTargetMemory, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleStepTargetExecution, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleSetBreakpoint, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleRemoveBreakpoint, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleSetProgramCounter, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleGetTargetPinStates, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleSetTargetPinState, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleGetTargetStackPointer, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleGetTargetProgramCounter, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleEnableProgrammingMode, this, std::placeholders::_1) - ); - - this->registerCommandHandler( - std::bind(&TargetControllerComponent::handleDisableProgrammingMode, this, std::placeholders::_1) - ); - this->eventListener->registerCallbackForEventType( std::bind(&TargetControllerComponent::onDebugSessionFinishedEvent, this, std::placeholders::_1) );