2020-05-27 19:46:56 +02:00
|
|
|
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h>
|
|
|
|
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
2020-05-27 23:29:06 +02:00
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
2020-05-27 19:46:56 +02:00
|
|
|
|
|
|
|
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
|
|
|
|
handle = TaskManagement::getCurrentTaskHandle();
|
2020-05-27 23:29:06 +02:00
|
|
|
if(handle == nullptr) {
|
|
|
|
sif::error << "Could not retrieve task handle. Please ensure the"
|
|
|
|
"constructor was called inside a task." << std::endl;
|
|
|
|
}
|
2020-05-27 21:27:31 +02:00
|
|
|
xTaskNotifyGive(handle);
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 23:29:06 +02:00
|
|
|
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
|
|
|
|
// Clear notification value on destruction.
|
|
|
|
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-05-27 19:46:56 +02:00
|
|
|
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
|
|
|
|
TickType_t timeout = SemaphoreIF::NO_TIMEOUT;
|
|
|
|
if(timeoutMs == SemaphoreIF::MAX_TIMEOUT) {
|
2020-05-27 22:12:52 +02:00
|
|
|
timeout = SemaphoreIF::MAX_TIMEOUT;
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
2020-05-27 19:59:59 +02:00
|
|
|
else if(timeoutMs > SemaphoreIF::NO_TIMEOUT){
|
2020-05-27 22:12:52 +02:00
|
|
|
timeout = pdMS_TO_TICKS(timeoutMs);
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
2020-05-27 22:12:52 +02:00
|
|
|
return acquireWithTickTimeout(timeout);
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 22:12:52 +02:00
|
|
|
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(
|
2020-05-27 19:46:56 +02:00
|
|
|
TickType_t timeoutTicks) {
|
|
|
|
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2020-05-27 22:12:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:12:52 +02:00
|
|
|
ReturnValue_t BinarySemaphoreUsingTask::release() {
|
2020-05-29 03:03:48 +02:00
|
|
|
return release(this->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t BinarySemaphoreUsingTask::release(
|
|
|
|
TaskHandle_t taskHandle) {
|
|
|
|
if(getSemaphoreCounter(taskHandle) == 1) {
|
2020-05-27 21:27:31 +02:00
|
|
|
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
|
|
|
}
|
2020-05-29 03:03:48 +02:00
|
|
|
BaseType_t returncode = xTaskNotifyGive(taskHandle);
|
2020-05-27 19:46:56 +02:00
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2020-05-29 03:03:48 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// This should never happen.
|
2020-05-27 21:27:31 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:33:34 +02:00
|
|
|
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const {
|
2020-05-29 03:03:48 +02:00
|
|
|
return getSemaphoreCounter(this->handle);
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 03:03:48 +02:00
|
|
|
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter(
|
2020-05-27 22:12:52 +02:00
|
|
|
TaskHandle_t taskHandle) {
|
2020-05-27 19:46:56 +02:00
|
|
|
uint32_t notificationValue;
|
2020-05-29 03:03:48 +02:00
|
|
|
xTaskNotifyAndQuery(taskHandle, 0, eNoAction, ¬ificationValue);
|
2020-05-27 19:46:56 +02:00
|
|
|
return notificationValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be careful with the stack size here. This is called from an ISR!
|
2020-05-27 22:12:52 +02:00
|
|
|
ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR(
|
2020-05-27 19:46:56 +02:00
|
|
|
TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) {
|
2020-05-29 13:02:13 +02:00
|
|
|
if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
|
2020-05-29 03:03:48 +02:00
|
|
|
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
2020-05-27 19:46:56 +02:00
|
|
|
}
|
2020-05-29 03:03:48 +02:00
|
|
|
vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken);
|
2020-05-27 19:46:56 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
2020-05-29 03:03:48 +02:00
|
|
|
|
|
|
|
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR(
|
2020-05-29 13:02:13 +02:00
|
|
|
TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) {
|
|
|
|
uint32_t notificationValue = 0;
|
2020-05-29 03:03:48 +02:00
|
|
|
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, ¬ificationValue,
|
2020-05-29 13:02:13 +02:00
|
|
|
higherPriorityTaskWoken);
|
2020-05-29 03:03:48 +02:00
|
|
|
return notificationValue;
|
|
|
|
}
|