Implemented ResetTaret handler in TargetController

This commit is contained in:
Nav
2022-04-08 22:14:01 +01:00
parent 42fd57cb6a
commit 1696d2dcbe
8 changed files with 70 additions and 1 deletions

View File

@@ -53,6 +53,7 @@ namespace Bloom::Events
SET_TARGET_PIN_STATE,
RETRIEVE_STACK_POINTER_FROM_TARGET,
STACK_POINTER_RETRIEVED_FROM_TARGET,
TARGET_RESET,
};
class Event

View File

@@ -41,6 +41,7 @@
#include "SetTargetPinState.hpp"
#include "RetrieveStackPointerFromTarget.hpp"
#include "StackPointerRetrievedFromTarget.hpp"
#include "TargetReset.hpp"
namespace Bloom::Events
{

View File

@@ -3,12 +3,15 @@
#include <string>
#include "Event.hpp"
#include "TargetReset.hpp"
namespace Bloom::Events
{
class ResetTarget: public Event
{
public:
using TargetControllerResponseType = TargetReset;
static constexpr EventType type = EventType::RESET_TARGET;
static inline const std::string name = "ResetTargetEvent";

View File

@@ -0,0 +1,23 @@
#pragma once
#include <string>
#include "Event.hpp"
namespace Bloom::Events
{
class TargetReset: public Event
{
public:
static constexpr EventType type = EventType::TARGET_RESET;
static inline const std::string name = "TargetReset";
[[nodiscard]] EventType getType() const override {
return TargetReset::type;
}
[[nodiscard]] std::string getName() const override {
return TargetReset::name;
}
};
}

View File

@@ -257,6 +257,10 @@ namespace Bloom
std::bind(&TargetController::onRetrieveStackPointerEvent, this, std::placeholders::_1)
);
this->eventListener->registerCallbackForEventType<Events::ResetTarget>(
std::bind(&TargetController::onResetTarget, this, std::placeholders::_1)
);
this->state = TargetControllerState::ACTIVE;
EventManager::triggerEvent(
std::make_shared<TargetControllerStateReported>(this->state)
@@ -446,6 +450,15 @@ namespace Bloom
}
}
void TargetController::resetTarget(const std::optional<int>& resetEventCorrelationId) {
this->target->reset();
auto targetResetEvent = std::make_shared<Events::TargetReset>();
targetResetEvent->correlationId = resetEventCorrelationId;
EventManager::triggerEvent(targetResetEvent);
}
void TargetController::emitErrorEvent(int correlationId, const std::string& errorMessage) {
auto errorEvent = std::make_shared<Events::TargetControllerErrorOccurred>();
errorEvent->correlationId = correlationId;
@@ -487,7 +500,7 @@ namespace Bloom
this->fireTargetEvents();
}
this->target->reset();
this->resetTarget();
if (this->target->getState() != TargetState::STOPPED) {
this->target->stop();
@@ -800,4 +813,14 @@ namespace Bloom
this->emitErrorEvent(event.id, exception.getMessage());
}
}
void TargetController::onResetTarget(const Events::ResetTarget& event) {
try {
this->resetTarget(event.id);
} catch (const TargetOperationFailure& exception) {
Logger::error("Failed to reset target - " + exception.getMessage());
this->emitErrorEvent(event.id, exception.getMessage());
}
}
}

View File

@@ -276,6 +276,13 @@ namespace Bloom
*/
void fireTargetEvents();
/**
* Triggers a target reset and emits a TargetReset event.
*
* @param resetEventCorrelationId
*/
void resetTarget(const std::optional<int>& resetEventCorrelationId = std::nullopt);
/**
* When the TargetController fails to handle an event, a TargetControllerErrorOccurred event is emitted, with
* a correlation ID matching the ID of the event that triggered the handler.
@@ -425,5 +432,7 @@ namespace Bloom
* @param event
*/
void onRetrieveStackPointerEvent(const Events::RetrieveStackPointerFromTarget& event);
void onResetTarget(const Events::ResetTarget& event);
};
}

View File

@@ -140,4 +140,8 @@ namespace Bloom
std::make_shared<RetrieveStackPointerFromTarget>()
)->stackPointer;
}
void TargetControllerConsole::resetTarget() {
this->triggerTargetControllerEventAndWaitForResponse(std::make_shared<ResetTarget>());
}
}

View File

@@ -165,6 +165,11 @@ namespace Bloom
*/
std::uint32_t getStackPointer();
/**
* Triggers a reset on the target. The target will be held in a stopped state.
*/
void resetTarget();
private:
EventListener& eventListener;