2021-07-14 00:54:39 +02:00
|
|
|
#include "fsfw/osal/freertos/CountingSemaphUsingTask.h"
|
2020-12-14 11:17:22 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "fsfw/osal/freertos/TaskManagement.h"
|
2021-07-14 00:54:39 +02:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2020-09-18 13:15:14 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
#if (tskKERNEL_VERSION_MAJOR == 8 && tskKERNEL_VERSION_MINOR > 2) || tskKERNEL_VERSION_MAJOR > 8
|
2020-12-14 23:56:44 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount)
|
|
|
|
: maxCount(maxCount) {
|
|
|
|
if (initCount > maxCount) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
|
|
|
"intial cout. Setting initial count to max count."
|
|
|
|
<< std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
initCount = maxCount;
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
handle = TaskManagement::getCurrentTaskHandle();
|
|
|
|
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 << "CountingSemaphoreUsingTask: Could not retrieve task "
|
|
|
|
"handle. Please ensure the constructor was called inside a "
|
|
|
|
"task."
|
|
|
|
<< std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
uint32_t oldNotificationValue;
|
|
|
|
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, &oldNotificationValue);
|
|
|
|
if (oldNotificationValue != 0) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "CountinSemaphoreUsingTask: Semaphore initiated but "
|
|
|
|
"current notification value is not 0. Please ensure the "
|
|
|
|
"notification value is not used for other purposes!"
|
|
|
|
<< std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < initCount; i++) {
|
|
|
|
xTaskNotifyGive(handle);
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
|
2022-02-02 10:29:30 +01:00
|
|
|
// Clear notification value on destruction.
|
|
|
|
// If this is not desired, don't call the destructor
|
|
|
|
// (or implement a boolean which disables the reset)
|
|
|
|
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t CountingSemaphoreUsingTask::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
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(TimeoutType timeoutType,
|
|
|
|
TickType_t timeoutTicks) {
|
|
|
|
// Decrement notfication value without resetting it.
|
|
|
|
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
|
|
|
|
if (getSemaphoreCounter() == oldCount - 1) {
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
} else {
|
|
|
|
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CountingSemaphoreUsingTask::release() {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (getSemaphoreCounter() == maxCount) {
|
|
|
|
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
|
|
|
}
|
|
|
|
return release(handle);
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t CountingSemaphoreUsingTask::release(TaskHandle_t taskToNotify) {
|
|
|
|
BaseType_t returncode = xTaskNotifyGive(taskToNotify);
|
|
|
|
if (returncode == pdPASS) {
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
} else {
|
|
|
|
// This should never happen.
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const {
|
2022-02-02 10:29:30 +01:00
|
|
|
uint32_t notificationValue = 0;
|
|
|
|
xTaskNotifyAndQuery(handle, 0, eNoAction, ¬ificationValue);
|
|
|
|
return notificationValue;
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() { return handle; }
|
2020-09-18 13:15:14 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR(TaskHandle_t taskToNotify,
|
|
|
|
BaseType_t* higherPriorityTaskWoken) {
|
|
|
|
vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken);
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR(
|
2022-02-02 10:29:30 +01:00
|
|
|
TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) {
|
|
|
|
uint32_t notificationValue;
|
|
|
|
xTaskNotifyAndQueryFromISR(task, 0, eNoAction, ¬ificationValue, higherPriorityTaskWoken);
|
|
|
|
return notificationValue;
|
2020-09-18 13:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
uint8_t CountingSemaphoreUsingTask::getMaxCount() const { return maxCount; }
|
2020-12-14 23:56:44 +01:00
|
|
|
|
|
|
|
#endif
|