2021-07-14 00:54:39 +02:00
|
|
|
#include "fsfw/osal/freertos/BinarySemaphore.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
|
2021-07-14 00:54:39 +02:00
|
|
|
#include "fsfw/osal/freertos/TaskManagement.h"
|
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2020-09-18 13:15:14 +02:00
|
|
|
|
|
|
|
BinarySemaphore::BinarySemaphore() {
|
2022-02-02 10:29:30 +01:00
|
|
|
handle = xSemaphoreCreateBinary();
|
|
|
|
if (handle == nullptr) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
// Initiated semaphore must be given before it can be taken.
|
|
|
|
xSemaphoreGive(handle);
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
BinarySemaphore::~BinarySemaphore() { vSemaphoreDelete(handle); }
|
2020-09-18 13:15:14 +02:00
|
|
|
|
|
|
|
BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) {
|
2022-02-02 10:29:30 +01:00
|
|
|
handle = xSemaphoreCreateBinary();
|
|
|
|
if (handle == nullptr) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "Binary semaphore creation failure" << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
xSemaphoreGive(handle);
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
BinarySemaphore& BinarySemaphore::operator=(BinarySemaphore&& s) {
|
|
|
|
if (&s != this) {
|
|
|
|
handle = xSemaphoreCreateBinary();
|
|
|
|
if (handle == nullptr) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "Binary semaphore creation failure" << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
xSemaphoreGive(handle);
|
|
|
|
}
|
|
|
|
return *this;
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType, uint32_t timeoutMs) {
|
|
|
|
TickType_t timeout = 0;
|
|
|
|
if (timeoutType == TimeoutType::POLLING) {
|
|
|
|
timeout = 0;
|
|
|
|
} else if (timeoutType == TimeoutType::WAITING) {
|
|
|
|
timeout = pdMS_TO_TICKS(timeoutMs);
|
|
|
|
} else {
|
|
|
|
timeout = portMAX_DELAY;
|
|
|
|
}
|
|
|
|
return acquireWithTickTimeout(timeoutType, timeout);
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TimeoutType timeoutType,
|
2022-02-02 10:29:30 +01:00
|
|
|
TickType_t timeoutTicks) {
|
|
|
|
if (handle == nullptr) {
|
|
|
|
return SemaphoreIF::SEMAPHORE_INVALID;
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t BinarySemaphore::release() { return release(handle); }
|
2020-09-18 13:15:14 +02:00
|
|
|
|
|
|
|
ReturnValue_t BinarySemaphore::release(SemaphoreHandle_t semaphore) {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (semaphore == nullptr) {
|
|
|
|
return SemaphoreIF::SEMAPHORE_INVALID;
|
|
|
|
}
|
|
|
|
BaseType_t returncode = xSemaphoreGive(semaphore);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
uint8_t BinarySemaphore::getSemaphoreCounter() const { return uxSemaphoreGetCount(handle); }
|
2020-09-18 13:15:14 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
SemaphoreHandle_t BinarySemaphore::getSemaphore() { return handle; }
|
2020-09-18 13:15:14 +02:00
|
|
|
|
|
|
|
// Be careful with the stack size here. This is called from an ISR!
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t BinarySemaphore::releaseFromISR(SemaphoreHandle_t semaphore,
|
|
|
|
BaseType_t* higherPriorityTaskWoken) {
|
|
|
|
if (semaphore == nullptr) {
|
|
|
|
return SemaphoreIF::SEMAPHORE_INVALID;
|
|
|
|
}
|
|
|
|
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|