add mutex protections for switcher states
This commit is contained in:
@ -29,6 +29,10 @@ HeaterHandler::HeaterHandler(object_id_t setObjectId_, GpioIF* gpioInterface_, H
|
||||
if (mainLineSwitcher == nullptr) {
|
||||
throw std::invalid_argument("HeaterHandler::HeaterHandler: Invalid PowerSwitchIF");
|
||||
}
|
||||
heaterMutex = MutexFactory::instance()->createMutex();
|
||||
if (heaterMutex == nullptr) {
|
||||
throw std::runtime_error("HeaterHandler::HeaterHandler: Creating Mutex failed");
|
||||
}
|
||||
auto mqArgs = MqArgs(setObjectId_, static_cast<void*>(this));
|
||||
commandQueue = QueueFactory::instance()->createMessageQueue(
|
||||
cmdQueueSize, MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs);
|
||||
@ -43,9 +47,14 @@ ReturnValue_t HeaterHandler::performOperation(uint8_t operationCode) {
|
||||
for (const auto& heater : helper.heaters) {
|
||||
heater.first->performOperation(0);
|
||||
}
|
||||
} catch(const std::out_of_range& e) {
|
||||
for (const auto& heater : heaterVec) {
|
||||
sif::info << heater.switchState << ",";
|
||||
}
|
||||
sif::info << std::endl;
|
||||
} catch (const std::out_of_range& e) {
|
||||
sif::warning << "HeaterHandler::performOperation: "
|
||||
"Out of range error | " << e.what() << std::endl;
|
||||
"Out of range error | "
|
||||
<< e.what() << std::endl;
|
||||
}
|
||||
return RETURN_OK;
|
||||
}
|
||||
@ -77,13 +86,7 @@ ReturnValue_t HeaterHandler::initialize() {
|
||||
|
||||
ReturnValue_t HeaterHandler::initializeHeaterMap() {
|
||||
for (power::Switch_t switchNr = 0; switchNr < heater::NUMBER_OF_SWITCHES; switchNr++) {
|
||||
gpio::Levels level;
|
||||
gpioInterface->readGpio(helper.heaters[switchNr].second, level);
|
||||
SwitchState initState = SwitchState::OFF;
|
||||
if(level == gpio::Levels::HIGH) {
|
||||
|
||||
}
|
||||
heaterVec.push_back(HeaterWrapper(helper.heaters[switchNr], initState));
|
||||
heaterVec.push_back(HeaterWrapper(helper.heaters[switchNr], SwitchState::OFF));
|
||||
}
|
||||
return RETURN_OK;
|
||||
}
|
||||
@ -208,7 +211,7 @@ void HeaterHandler::handleSwitchOnCommand(heater::Switchers heaterIdx) {
|
||||
// Check state of main line switch
|
||||
ReturnValue_t mainSwitchState = mainLineSwitcher->getSwitchState(mainLineSwitch);
|
||||
if (mainSwitchState == PowerSwitchIF::SWITCH_ON) {
|
||||
if (checkSwitchState(heaterIdx) == SwitchState::ON) {
|
||||
if (checkSwitchState(heaterIdx) == SwitchState::OFF) {
|
||||
gpioId_t gpioId = heater.gpioId;
|
||||
result = gpioInterface->pullHigh(gpioId);
|
||||
if (result != RETURN_OK) {
|
||||
@ -216,7 +219,7 @@ void HeaterHandler::handleSwitchOnCommand(heater::Switchers heaterIdx) {
|
||||
<< " high" << std::endl;
|
||||
triggerEvent(GPIO_PULL_HIGH_FAILED, result);
|
||||
} else {
|
||||
triggerEvent(HEATER_WENT_ON, heaterIdx, 0);
|
||||
triggerEvent(HEATER_WENT_ON, heaterIdx, 0);
|
||||
heater.switchState = ON;
|
||||
}
|
||||
} else {
|
||||
@ -262,8 +265,12 @@ void HeaterHandler::handleSwitchOffCommand(heater::Switchers heaterIdx) {
|
||||
<< " low" << std::endl;
|
||||
triggerEvent(GPIO_PULL_LOW_FAILED, result);
|
||||
} else {
|
||||
auto result = heaterMutex->lockMutex();
|
||||
heater.switchState = OFF;
|
||||
triggerEvent(HEATER_WENT_OFF, heaterIdx, 0);
|
||||
if (result == HasReturnvaluesIF::RETURN_OK) {
|
||||
heaterMutex->unlockMutex();
|
||||
}
|
||||
triggerEvent(HEATER_WENT_OFF, heaterIdx, 0);
|
||||
// When all switches are off, also main line switch will be turned off
|
||||
if (allSwitchesOff()) {
|
||||
mainLineSwitcher->sendSwitchCommand(mainLineSwitch, PowerSwitchIF::SWITCH_OFF);
|
||||
@ -284,12 +291,14 @@ void HeaterHandler::handleSwitchOffCommand(heater::Switchers heaterIdx) {
|
||||
heater.active = false;
|
||||
}
|
||||
|
||||
HeaterHandler::SwitchState HeaterHandler::checkSwitchState(heater::Switchers switchNr) {
|
||||
HeaterHandler::SwitchState HeaterHandler::checkSwitchState(heater::Switchers switchNr) const {
|
||||
MutexGuard mg(heaterMutex);
|
||||
return heaterVec.at(switchNr).switchState;
|
||||
}
|
||||
|
||||
bool HeaterHandler::allSwitchesOff() {
|
||||
bool allSwitchesOrd = false;
|
||||
MutexGuard mg(heaterMutex);
|
||||
/* Or all switches. As soon one switch is on, allSwitchesOrd will be true */
|
||||
for (power::Switch_t switchNr = 0; switchNr < heater::NUMBER_OF_SWITCHES; switchNr++) {
|
||||
allSwitchesOrd = allSwitchesOrd || heaterVec.at(switchNr).switchState;
|
||||
@ -301,7 +310,19 @@ MessageQueueId_t HeaterHandler::getCommandQueue() const { return commandQueue->g
|
||||
|
||||
ReturnValue_t HeaterHandler::sendFuseOnCommand(uint8_t fuseNr) { return RETURN_OK; }
|
||||
|
||||
ReturnValue_t HeaterHandler::getSwitchState(uint8_t switchNr) const { return 0; }
|
||||
ReturnValue_t HeaterHandler::getSwitchState(uint8_t switchNr) const {
|
||||
ReturnValue_t mainSwitchState = mainLineSwitcher->getSwitchState(mainLineSwitch);
|
||||
if (mainSwitchState == PowerSwitchIF::SWITCH_OFF) {
|
||||
return PowerSwitchIF::SWITCH_OFF;
|
||||
}
|
||||
if (switchNr > 7) {
|
||||
return PowerSwitchIF::RETURN_FAILED;
|
||||
}
|
||||
if (checkSwitchState(static_cast<heater::Switchers>(switchNr)) == SwitchState::ON) {
|
||||
return PowerSwitchIF::SWITCH_ON;
|
||||
}
|
||||
return PowerSwitchIF::SWITCH_OFF;
|
||||
}
|
||||
|
||||
ReturnValue_t HeaterHandler::getFuseState(uint8_t fuseNr) const { return 0; }
|
||||
|
||||
|
@ -54,22 +54,22 @@ class HeaterHandler : public ExecutableObjectIF,
|
||||
|
||||
virtual ~HeaterHandler();
|
||||
|
||||
virtual ReturnValue_t performOperation(uint8_t operationCode = 0) override;
|
||||
ReturnValue_t performOperation(uint8_t operationCode = 0) override;
|
||||
|
||||
virtual ReturnValue_t sendSwitchCommand(uint8_t switchNr, ReturnValue_t onOff) override;
|
||||
virtual ReturnValue_t sendFuseOnCommand(uint8_t fuseNr) override;
|
||||
ReturnValue_t sendSwitchCommand(uint8_t switchNr, ReturnValue_t onOff) override;
|
||||
ReturnValue_t sendFuseOnCommand(uint8_t fuseNr) override;
|
||||
/**
|
||||
* @brief This function will be called from the Heater object to check
|
||||
* the current switch state.
|
||||
*/
|
||||
virtual ReturnValue_t getSwitchState(uint8_t switchNr) const override;
|
||||
virtual ReturnValue_t getFuseState(uint8_t fuseNr) const override;
|
||||
virtual uint32_t getSwitchDelayMs(void) const override;
|
||||
ReturnValue_t getSwitchState(uint8_t switchNr) const override;
|
||||
ReturnValue_t getFuseState(uint8_t fuseNr) const override;
|
||||
uint32_t getSwitchDelayMs(void) const override;
|
||||
|
||||
virtual MessageQueueId_t getCommandQueue() const override;
|
||||
virtual ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
|
||||
const uint8_t* data, size_t size) override;
|
||||
virtual ReturnValue_t initialize() override;
|
||||
MessageQueueId_t getCommandQueue() const override;
|
||||
ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
|
||||
const uint8_t* data, size_t size) override;
|
||||
ReturnValue_t initialize() override;
|
||||
|
||||
private:
|
||||
static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::HEATER_HANDLER;
|
||||
@ -98,7 +98,7 @@ class HeaterHandler : public ExecutableObjectIF,
|
||||
*/
|
||||
struct HeaterWrapper {
|
||||
HeaterWrapper(HeaterPair pair, SwitchState initState)
|
||||
: healthDevice(pair.first), gpioId(pair.second), switchState(initState) {}
|
||||
: healthDevice(pair.first), gpioId(pair.second), switchState(initState) {}
|
||||
HealthDevice* healthDevice = nullptr;
|
||||
gpioId_t gpioId = gpio::NO_GPIO;
|
||||
SwitchAction action = SwitchAction::NONE;
|
||||
@ -113,6 +113,8 @@ class HeaterHandler : public ExecutableObjectIF,
|
||||
|
||||
HeaterMap heaterVec = {};
|
||||
|
||||
MutexIF* heaterMutex = nullptr;
|
||||
|
||||
HeaterHelper helper;
|
||||
|
||||
/** Size of command queue */
|
||||
@ -141,7 +143,7 @@ class HeaterHandler : public ExecutableObjectIF,
|
||||
* @brief Returns the state of a switch (ON - true, or OFF - false).
|
||||
* @param switchNr The number of the switch to check.
|
||||
*/
|
||||
SwitchState checkSwitchState(heater::Switchers switchNr);
|
||||
SwitchState checkSwitchState(heater::Switchers switchNr) const;
|
||||
|
||||
/**
|
||||
* @brief This function runs commands waiting for execution.
|
||||
|
Reference in New Issue
Block a user