#pragma once #include #include "fsfw/ipc/MutexIF.h" #include "fsfw/returnvalues/returnvalue.h" #include "fsfw_hal/common/gpio/GpioIF.h" class ManualCsLockWrapper { public: ManualCsLockWrapper(MutexIF* lock, GpioIF* gpioIF, SpiCookie* cookie, MutexIF::TimeoutType type = MutexIF::TimeoutType::BLOCKING, uint32_t timeoutMs = 0) : lock(lock), gpioIF(gpioIF), cookie(cookie), type(type), timeoutMs(timeoutMs) { if (cookie == nullptr) { // TODO: Error? Or maybe throw exception.. return; } cookie->setCsLockManual(true); lockResult = lock->lockMutex(type, timeoutMs); if (lockResult != returnvalue::OK) { return; } gpioResult = gpioIF->pullLow(cookie->getChipSelectPin()); } ~ManualCsLockWrapper() { if (gpioResult == returnvalue::OK) { gpioIF->pullHigh(cookie->getChipSelectPin()); } cookie->setCsLockManual(false); if (lockResult == returnvalue::OK) { lock->unlockMutex(); } } ReturnValue_t lockResult; ReturnValue_t gpioResult; private: MutexIF* lock; GpioIF* gpioIF; SpiCookie* cookie; MutexIF::TimeoutType type; uint32_t timeoutMs = 0; };