Renamed TargetControllerConsole to TargetControllerService
This commit is contained in:
273
src/Services/TargetControllerService.cpp
Normal file
273
src/Services/TargetControllerService.cpp
Normal file
@@ -0,0 +1,273 @@
|
||||
#include "TargetControllerService.hpp"
|
||||
|
||||
// Commands
|
||||
#include "src/TargetController/Commands/GetState.hpp"
|
||||
#include "src/TargetController/Commands/Suspend.hpp"
|
||||
#include "src/TargetController/Commands/Resume.hpp"
|
||||
#include "src/TargetController/Commands/GetTargetDescriptor.hpp"
|
||||
#include "src/TargetController/Commands/GetTargetState.hpp"
|
||||
#include "src/TargetController/Commands/StopTargetExecution.hpp"
|
||||
#include "src/TargetController/Commands/ResumeTargetExecution.hpp"
|
||||
#include "src/TargetController/Commands/ResetTarget.hpp"
|
||||
#include "src/TargetController/Commands/ReadTargetRegisters.hpp"
|
||||
#include "src/TargetController/Commands/WriteTargetRegisters.hpp"
|
||||
#include "src/TargetController/Commands/ReadTargetMemory.hpp"
|
||||
#include "src/TargetController/Commands/WriteTargetMemory.hpp"
|
||||
#include "src/TargetController/Commands/EraseTargetMemory.hpp"
|
||||
#include "src/TargetController/Commands/StepTargetExecution.hpp"
|
||||
#include "src/TargetController/Commands/SetBreakpoint.hpp"
|
||||
#include "src/TargetController/Commands/RemoveBreakpoint.hpp"
|
||||
#include "src/TargetController/Commands/SetTargetProgramCounter.hpp"
|
||||
#include "src/TargetController/Commands/GetTargetPinStates.hpp"
|
||||
#include "src/TargetController/Commands/SetTargetPinState.hpp"
|
||||
#include "src/TargetController/Commands/GetTargetStackPointer.hpp"
|
||||
#include "src/TargetController/Commands/GetTargetProgramCounter.hpp"
|
||||
#include "src/TargetController/Commands/EnableProgrammingMode.hpp"
|
||||
#include "src/TargetController/Commands/DisableProgrammingMode.hpp"
|
||||
|
||||
namespace Bloom::Services
|
||||
{
|
||||
using TargetController::Commands::GetState;
|
||||
using TargetController::Commands::Suspend;
|
||||
using TargetController::Commands::Resume;
|
||||
using TargetController::Commands::GetTargetDescriptor;
|
||||
using TargetController::Commands::GetTargetState;
|
||||
using TargetController::Commands::StopTargetExecution;
|
||||
using TargetController::Commands::ResumeTargetExecution;
|
||||
using TargetController::Commands::ResetTarget;
|
||||
using TargetController::Commands::ReadTargetRegisters;
|
||||
using TargetController::Commands::WriteTargetRegisters;
|
||||
using TargetController::Commands::ReadTargetMemory;
|
||||
using TargetController::Commands::WriteTargetMemory;
|
||||
using TargetController::Commands::EraseTargetMemory;
|
||||
using TargetController::Commands::StepTargetExecution;
|
||||
using TargetController::Commands::SetBreakpoint;
|
||||
using TargetController::Commands::RemoveBreakpoint;
|
||||
using TargetController::Commands::SetTargetProgramCounter;
|
||||
using TargetController::Commands::GetTargetPinStates;
|
||||
using TargetController::Commands::SetTargetPinState;
|
||||
using TargetController::Commands::GetTargetStackPointer;
|
||||
using TargetController::Commands::GetTargetProgramCounter;
|
||||
using TargetController::Commands::EnableProgrammingMode;
|
||||
using TargetController::Commands::DisableProgrammingMode;
|
||||
|
||||
using TargetController::TargetControllerState;
|
||||
|
||||
using Targets::TargetDescriptor;
|
||||
using Targets::TargetState;
|
||||
|
||||
using Targets::TargetRegisters;
|
||||
using Targets::TargetRegisterDescriptors;
|
||||
|
||||
using Targets::TargetMemoryType;
|
||||
using Targets::TargetMemoryAddress;
|
||||
using Targets::TargetMemorySize;
|
||||
using Targets::TargetMemoryAddressRange;
|
||||
using Targets::TargetMemoryBuffer;
|
||||
using Targets::TargetProgramCounter;
|
||||
using Targets::TargetStackPointer;
|
||||
|
||||
using Targets::TargetBreakpoint;
|
||||
|
||||
using Targets::TargetPinDescriptor;
|
||||
using Targets::TargetPinState;
|
||||
using Targets::TargetPinStateMapping;
|
||||
|
||||
TargetControllerState TargetControllerService::getTargetControllerState() {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<GetState>(),
|
||||
this->defaultTimeout
|
||||
)->state;
|
||||
}
|
||||
|
||||
bool TargetControllerService::isTargetControllerInService() noexcept {
|
||||
try {
|
||||
return this->getTargetControllerState() == TargetControllerState::ACTIVE;
|
||||
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void TargetControllerService::resumeTargetController() {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<Resume>(),
|
||||
this->defaultTimeout
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
void TargetControllerService::suspendTargetController() {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<Suspend>(),
|
||||
this->defaultTimeout
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const TargetDescriptor& TargetControllerService::getTargetDescriptor() {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<GetTargetDescriptor>(),
|
||||
this->defaultTimeout
|
||||
)->targetDescriptor;
|
||||
}
|
||||
|
||||
TargetState TargetControllerService::getTargetState() {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<GetTargetState>(),
|
||||
this->defaultTimeout
|
||||
)->targetState;
|
||||
}
|
||||
|
||||
void TargetControllerService::stopTargetExecution() {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<StopTargetExecution>(),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::continueTargetExecution(std::optional<TargetProgramCounter> fromAddress) {
|
||||
auto resumeExecutionCommand = std::make_unique<ResumeTargetExecution>();
|
||||
|
||||
if (fromAddress.has_value()) {
|
||||
resumeExecutionCommand->fromProgramCounter = fromAddress.value();
|
||||
}
|
||||
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::move(resumeExecutionCommand),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::stepTargetExecution(std::optional<TargetProgramCounter> fromAddress) {
|
||||
auto stepExecutionCommand = std::make_unique<StepTargetExecution>();
|
||||
|
||||
if (fromAddress.has_value()) {
|
||||
stepExecutionCommand->fromProgramCounter = fromAddress.value();
|
||||
}
|
||||
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::move(stepExecutionCommand),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
TargetRegisters TargetControllerService::readRegisters(const TargetRegisterDescriptors& descriptors) {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<ReadTargetRegisters>(descriptors),
|
||||
this->defaultTimeout
|
||||
)->registers;
|
||||
}
|
||||
|
||||
void TargetControllerService::writeRegisters(const TargetRegisters& registers) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<WriteTargetRegisters>(registers),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
TargetMemoryBuffer TargetControllerService::readMemory(
|
||||
TargetMemoryType memoryType,
|
||||
TargetMemoryAddress startAddress,
|
||||
TargetMemorySize bytes,
|
||||
const std::set<TargetMemoryAddressRange>& excludedAddressRanges
|
||||
) {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<ReadTargetMemory>(
|
||||
memoryType,
|
||||
startAddress,
|
||||
bytes,
|
||||
excludedAddressRanges
|
||||
),
|
||||
this->defaultTimeout
|
||||
)->data;
|
||||
}
|
||||
|
||||
void TargetControllerService::writeMemory(
|
||||
TargetMemoryType memoryType,
|
||||
TargetMemoryAddress startAddress,
|
||||
const TargetMemoryBuffer& buffer
|
||||
) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<WriteTargetMemory>(memoryType, startAddress, buffer),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::eraseMemory(Targets::TargetMemoryType memoryType) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<EraseTargetMemory>(memoryType),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::setBreakpoint(TargetBreakpoint breakpoint) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<SetBreakpoint>(breakpoint),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::removeBreakpoint(TargetBreakpoint breakpoint) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<RemoveBreakpoint>(breakpoint),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
TargetProgramCounter TargetControllerService::getProgramCounter() {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<GetTargetProgramCounter>(),
|
||||
this->defaultTimeout
|
||||
)->programCounter;
|
||||
}
|
||||
|
||||
void TargetControllerService::setProgramCounter(TargetProgramCounter address) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<SetTargetProgramCounter>(address),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
TargetPinStateMapping TargetControllerService::getPinStates(int variantId) {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<GetTargetPinStates>(variantId),
|
||||
this->defaultTimeout
|
||||
)->pinStatesByNumber;
|
||||
}
|
||||
|
||||
void TargetControllerService::setPinState(TargetPinDescriptor pinDescriptor, TargetPinState pinState) {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<SetTargetPinState>(pinDescriptor, pinState),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
TargetStackPointer TargetControllerService::getStackPointer() {
|
||||
return this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<GetTargetStackPointer>(),
|
||||
this->defaultTimeout
|
||||
)->stackPointer;
|
||||
}
|
||||
|
||||
void TargetControllerService::resetTarget() {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<ResetTarget>(),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::enableProgrammingMode() {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<EnableProgrammingMode>(),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
|
||||
void TargetControllerService::disableProgrammingMode() {
|
||||
this->commandManager.sendCommandAndWaitForResponse(
|
||||
std::make_unique<DisableProgrammingMode>(),
|
||||
this->defaultTimeout
|
||||
);
|
||||
}
|
||||
}
|
||||
222
src/Services/TargetControllerService.hpp
Normal file
222
src/Services/TargetControllerService.hpp
Normal file
@@ -0,0 +1,222 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
|
||||
#include "src/TargetController/CommandManager.hpp"
|
||||
#include "src/TargetController/TargetControllerState.hpp"
|
||||
|
||||
#include "src/Targets/TargetState.hpp"
|
||||
#include "src/Targets/TargetRegister.hpp"
|
||||
#include "src/Targets/TargetMemory.hpp"
|
||||
#include "src/Targets/TargetBreakpoint.hpp"
|
||||
#include "src/Targets/TargetVariant.hpp"
|
||||
#include "src/Targets/TargetPinDescriptor.hpp"
|
||||
|
||||
#include "src/Exceptions/Exception.hpp"
|
||||
|
||||
namespace Bloom::Services
|
||||
{
|
||||
/**
|
||||
* The TargetControllerService provides an interface to the TargetController.
|
||||
*/
|
||||
class TargetControllerService
|
||||
{
|
||||
public:
|
||||
TargetControllerService() = default;
|
||||
|
||||
void setDefaultTimeout(std::chrono::milliseconds timeout) {
|
||||
this->defaultTimeout = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the current TargetController state from the TargetController. The TargetController should always
|
||||
* respond to such a request, even when it's in a suspended state.
|
||||
*
|
||||
* To check if the TargetController is in an active state, isTargetControllerInService() can be used for
|
||||
* convenience.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
TargetController::TargetControllerState getTargetControllerState();
|
||||
|
||||
/**
|
||||
* Retrieves the TargetController state and checks if it's currently active.
|
||||
*
|
||||
* @return
|
||||
* True if the TargetController is currently in an active state, otherwise false.
|
||||
*/
|
||||
bool isTargetControllerInService() noexcept;
|
||||
|
||||
/**
|
||||
* Resumes the TargetController if it's suspended. Otherwise, this function does nothing.
|
||||
*/
|
||||
void resumeTargetController();
|
||||
|
||||
/**
|
||||
* Suspends the TargetController if it's active. Otherwise, this function does nothing.
|
||||
*/
|
||||
void suspendTargetController();
|
||||
|
||||
/**
|
||||
* Requests the TargetDescriptor from the TargetController
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
const Targets::TargetDescriptor& getTargetDescriptor();
|
||||
|
||||
/**
|
||||
* Fetches the current target state.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Targets::TargetState getTargetState();
|
||||
|
||||
/**
|
||||
* Requests the TargetController to halt execution on the target.
|
||||
*/
|
||||
void stopTargetExecution();
|
||||
|
||||
/**
|
||||
* Requests the TargetController to continue execution on the target.
|
||||
*
|
||||
* @param fromAddress
|
||||
*/
|
||||
void continueTargetExecution(std::optional<Targets::TargetProgramCounter> fromAddress);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to step execution on the target.
|
||||
*
|
||||
* @param fromAddress
|
||||
*/
|
||||
void stepTargetExecution(std::optional<Targets::TargetProgramCounter> fromAddress);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to read register values from the target.
|
||||
*
|
||||
* @param descriptors
|
||||
* Descriptors of the registers to read.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Targets::TargetRegisters readRegisters(const Targets::TargetRegisterDescriptors& descriptors);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to write register values to the target.
|
||||
*
|
||||
* @param registers
|
||||
*/
|
||||
void writeRegisters(const Targets::TargetRegisters& registers);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to read memory from the target.
|
||||
*
|
||||
* @param memoryType
|
||||
* @param startAddress
|
||||
* @param bytes
|
||||
* @param excludedAddressRanges
|
||||
* @return
|
||||
*/
|
||||
Targets::TargetMemoryBuffer readMemory(
|
||||
Targets::TargetMemoryType memoryType,
|
||||
Targets::TargetMemoryAddress startAddress,
|
||||
Targets::TargetMemorySize bytes,
|
||||
const std::set<Targets::TargetMemoryAddressRange>& excludedAddressRanges = {}
|
||||
);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to write memory to the target.
|
||||
*
|
||||
* @param memoryType
|
||||
* @param startAddress
|
||||
* @param buffer
|
||||
*/
|
||||
void writeMemory(
|
||||
Targets::TargetMemoryType memoryType,
|
||||
Targets::TargetMemoryAddress startAddress,
|
||||
const Targets::TargetMemoryBuffer& buffer
|
||||
);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to erase the given target memory type.
|
||||
*
|
||||
* @param memoryType
|
||||
*/
|
||||
void eraseMemory(Targets::TargetMemoryType memoryType);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to set a breakpoint on the target.
|
||||
*
|
||||
* @param breakpoint
|
||||
*/
|
||||
void setBreakpoint(Targets::TargetBreakpoint breakpoint);
|
||||
|
||||
/**
|
||||
* Requests the TargetController to remove a breakpoint from the target.
|
||||
*
|
||||
* @param breakpoint
|
||||
*/
|
||||
void removeBreakpoint(Targets::TargetBreakpoint breakpoint);
|
||||
|
||||
/**
|
||||
* Retrieves the current program counter value from the target.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Targets::TargetProgramCounter getProgramCounter();
|
||||
|
||||
/**
|
||||
* Sets the target's program counter to the given address.
|
||||
*
|
||||
* @param address
|
||||
*/
|
||||
void setProgramCounter(Targets::TargetProgramCounter address);
|
||||
|
||||
/**
|
||||
* Retrieves the pin states for a particular target variant.
|
||||
*
|
||||
* @param variantId
|
||||
*/
|
||||
Targets::TargetPinStateMapping getPinStates(int variantId);
|
||||
|
||||
/**
|
||||
* Updates the pin state on the target, for a specific pin.
|
||||
*
|
||||
* @param pinDescriptor
|
||||
* @param pinState
|
||||
*/
|
||||
void setPinState(Targets::TargetPinDescriptor pinDescriptor, Targets::TargetPinState pinState);
|
||||
|
||||
/**
|
||||
* Retrieves the current stack pointer value from the target.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Targets::TargetStackPointer getStackPointer();
|
||||
|
||||
/**
|
||||
* Triggers a reset on the target. The target will be held in a stopped state.
|
||||
*/
|
||||
void resetTarget();
|
||||
|
||||
/**
|
||||
* Enables programming mode on the target.
|
||||
*
|
||||
* From the point of invoking this function, the TargetController will reject any subsequent commands for
|
||||
* debug operations (such as ResumeTargetExecution, ReadTargetRegisters, etc), until programming mode has
|
||||
* been disabled.
|
||||
*/
|
||||
void enableProgrammingMode();
|
||||
|
||||
/**
|
||||
* Disables programming mode on the target.
|
||||
*/
|
||||
void disableProgrammingMode();
|
||||
|
||||
private:
|
||||
TargetController::CommandManager commandManager = TargetController::CommandManager();
|
||||
|
||||
std::chrono::milliseconds defaultTimeout = std::chrono::milliseconds(60000);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user