Unprotected shared state access
This commit is contained in:
119
main/control.c
119
main/control.c
@ -25,9 +25,9 @@
|
|||||||
(60U * 4U) // Burner fault detection after 4 minutes
|
(60U * 4U) // Burner fault detection after 4 minutes
|
||||||
|
|
||||||
static const char *TAG = "smart-oil-heater-control-system-control";
|
static const char *TAG = "smart-oil-heater-control-system-control";
|
||||||
static eControlState sControlState = CONTROL_STARTING;
|
static eControlState gControlState = CONTROL_STARTING;
|
||||||
// Control table for daily schedules
|
// Control table for daily schedules
|
||||||
static const sControlDay aControlTable[] = {
|
static const sControlDay gControlTable[] = {
|
||||||
{MONDAY,
|
{MONDAY,
|
||||||
2U,
|
2U,
|
||||||
{{{4, 45},
|
{{{4, 45},
|
||||||
@ -85,15 +85,25 @@ static const sControlDay aControlTable[] = {
|
|||||||
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
|
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
|
||||||
CHAMBER_TEMPERATURE_TARGET}}},
|
CHAMBER_TEMPERATURE_TARGET}}},
|
||||||
};
|
};
|
||||||
static sControlTemperatureEntry currentControlEntry =
|
static sControlTemperatureEntry gCurrentControlEntry =
|
||||||
aControlTable[0].aTemperatureEntries[0];
|
gControlTable[0].aTemperatureEntries[0];
|
||||||
|
static SemaphoreHandle_t xMutexAccessControl = NULL;
|
||||||
|
|
||||||
// Function prototypes
|
// Function prototypes
|
||||||
void taskControl(void *pvParameters);
|
void taskControl(void *pvParameters);
|
||||||
void findControlCurrentTemperatureEntry(void);
|
void findControlCurrentTemperatureEntry(void);
|
||||||
|
void setControlState(eControlState state);
|
||||||
|
|
||||||
void initControl(void)
|
void initControl(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
xMutexAccessControl = xSemaphoreCreateRecursiveMutex();
|
||||||
|
if (xMutexAccessControl == NULL)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to create mutex");
|
||||||
|
}
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
|
||||||
BaseType_t taskCreated =
|
BaseType_t taskCreated =
|
||||||
xTaskCreate(taskControl, // Function to implement the task
|
xTaskCreate(taskControl, // Function to implement the task
|
||||||
"taskControl", // Task name
|
"taskControl", // Task name
|
||||||
@ -128,7 +138,7 @@ void taskControl(void *pvParameters)
|
|||||||
if (getSafetyState() != SAFETY_NO_ERROR)
|
if (getSafetyState() != SAFETY_NO_ERROR)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Control not possible due to safety fault!");
|
ESP_LOGW(TAG, "Control not possible due to safety fault!");
|
||||||
sControlState = CONTROL_FAULT_SAFETY;
|
setControlState(CONTROL_FAULT_SAFETY);
|
||||||
if (bHeatingInAction)
|
if (bHeatingInAction)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Disabling burner due to safety fault");
|
ESP_LOGW(TAG, "Disabling burner due to safety fault");
|
||||||
@ -143,7 +153,7 @@ void taskControl(void *pvParameters)
|
|||||||
if (getSntpState() != SYNC_SUCCESSFUL)
|
if (getSntpState() != SYNC_SUCCESSFUL)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Control not possible due to SNTP fault!");
|
ESP_LOGW(TAG, "Control not possible due to SNTP fault!");
|
||||||
sControlState = CONTROL_FAULT_SNTP;
|
setControlState(CONTROL_FAULT_SNTP);
|
||||||
if (bHeatingInAction)
|
if (bHeatingInAction)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Disabling burner due to SNTP fault");
|
ESP_LOGW(TAG, "Disabling burner due to SNTP fault");
|
||||||
@ -155,8 +165,6 @@ void taskControl(void *pvParameters)
|
|||||||
}
|
}
|
||||||
|
|
||||||
findControlCurrentTemperatureEntry();
|
findControlCurrentTemperatureEntry();
|
||||||
sControlTemperatureEntry currentControlEntry =
|
|
||||||
getControlCurrentTemperatureEntry();
|
|
||||||
|
|
||||||
if (getOutdoorTemperature().fDampedValue >=
|
if (getOutdoorTemperature().fDampedValue >=
|
||||||
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
|
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
|
||||||
@ -178,10 +186,10 @@ void taskControl(void *pvParameters)
|
|||||||
// ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating");
|
// ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating");
|
||||||
setBurnerState(DISABLED);
|
setBurnerState(DISABLED);
|
||||||
setSafetyControlState(DISABLED);
|
setSafetyControlState(DISABLED);
|
||||||
sControlState = CONTROL_OUTDOOR_TOO_WARM;
|
setControlState(CONTROL_OUTDOOR_TOO_WARM);
|
||||||
}
|
}
|
||||||
else if ((getReturnFlowTemperature().average60s.fValue <=
|
else if ((getReturnFlowTemperature().average60s.fValue <=
|
||||||
currentControlEntry.fReturnFlowTemperature) &&
|
getControlCurrentTemperatureEntry().fReturnFlowTemperature) &&
|
||||||
(getChamberTemperature().fCurrentValue <=
|
(getChamberTemperature().fCurrentValue <=
|
||||||
CHAMBER_TEMPERATURE_THRESHOLD))
|
CHAMBER_TEMPERATURE_THRESHOLD))
|
||||||
{
|
{
|
||||||
@ -192,12 +200,12 @@ void taskControl(void *pvParameters)
|
|||||||
setBurnerState(ENABLED);
|
setBurnerState(ENABLED);
|
||||||
setSafetyControlState(ENABLED);
|
setSafetyControlState(ENABLED);
|
||||||
i64BurnerEnableTimestamp = esp_timer_get_time();
|
i64BurnerEnableTimestamp = esp_timer_get_time();
|
||||||
sControlState = CONTROL_HEATING;
|
setControlState(CONTROL_HEATING);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
|
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
|
||||||
sControlState = CONTROL_RETURN_FLOW_TOO_WARM;
|
setControlState(CONTROL_RETURN_FLOW_TOO_WARM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,9 +213,9 @@ void taskControl(void *pvParameters)
|
|||||||
if (bHeatingInAction)
|
if (bHeatingInAction)
|
||||||
{
|
{
|
||||||
if ((getChamberTemperature().fCurrentValue >=
|
if ((getChamberTemperature().fCurrentValue >=
|
||||||
currentControlEntry.fChamberTemperature) ||
|
getControlCurrentTemperatureEntry().fChamberTemperature) ||
|
||||||
(getChamberTemperature().predict60s.fValue >=
|
(getChamberTemperature().predict60s.fValue >=
|
||||||
currentControlEntry.fChamberTemperature))
|
getControlCurrentTemperatureEntry().fChamberTemperature))
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
|
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
|
||||||
bHeatingInAction = false;
|
bHeatingInAction = false;
|
||||||
@ -224,7 +232,7 @@ void taskControl(void *pvParameters)
|
|||||||
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner");
|
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner");
|
||||||
bHeatingInAction = false;
|
bHeatingInAction = false;
|
||||||
eBurnerState = BURNER_FAULT;
|
eBurnerState = BURNER_FAULT;
|
||||||
sControlState = CONTROL_FAULT_BURNER;
|
setControlState(CONTROL_FAULT_BURNER);
|
||||||
setBurnerState(DISABLED);
|
setBurnerState(DISABLED);
|
||||||
setSafetyControlState(ENABLED);
|
setSafetyControlState(ENABLED);
|
||||||
}
|
}
|
||||||
@ -253,7 +261,37 @@ void taskControl(void *pvParameters)
|
|||||||
} // End of while(1)
|
} // End of while(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
eControlState getControlState(void) { return sControlState; }
|
void setControlState(eControlState state)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
gControlState = state;
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: setControlState()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eControlState getControlState(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
eControlState ret = CONTROL_FAULT_SAFETY;
|
||||||
|
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
ret = gControlState;
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: getControlState()");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
eControlWeekday getControlCurrentWeekday(void)
|
eControlWeekday getControlCurrentWeekday(void)
|
||||||
{
|
{
|
||||||
@ -294,12 +332,15 @@ void findControlCurrentTemperatureEntry(void)
|
|||||||
int currentHour = timeinfo.tm_hour;
|
int currentHour = timeinfo.tm_hour;
|
||||||
int currentMinute = timeinfo.tm_min;
|
int currentMinute = timeinfo.tm_min;
|
||||||
|
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
|
||||||
// ESP_LOGI(TAG, "Searching for control entry - Day: %d, Time: %02d:%02d", currentDay, currentHour, currentMinute);
|
// ESP_LOGI(TAG, "Searching for control entry - Day: %d, Time: %02d:%02d", currentDay, currentHour, currentMinute);
|
||||||
|
|
||||||
// Search through all days and entries
|
// Search through all days and entries
|
||||||
for (int dayIndex = 0; dayIndex < 7; dayIndex++)
|
for (int dayIndex = 0; dayIndex < 7; dayIndex++)
|
||||||
{
|
{
|
||||||
const sControlDay *day = &aControlTable[dayIndex];
|
const sControlDay *day = &gControlTable[dayIndex];
|
||||||
|
|
||||||
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
|
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
|
||||||
{
|
{
|
||||||
@ -314,31 +355,32 @@ void findControlCurrentTemperatureEntry(void)
|
|||||||
|
|
||||||
if (isFutureDay || isTodayFutureTime)
|
if (isFutureDay || isTodayFutureTime)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Found next scheduled entry, so determine the previous (active) one
|
// Found next scheduled entry, so determine the previous (active) one
|
||||||
if (entryIndex > 0)
|
if (entryIndex > 0)
|
||||||
{
|
{
|
||||||
// Use previous entry from same day
|
// Use previous entry from same day
|
||||||
currentControlEntry = day->aTemperatureEntries[entryIndex - 1];
|
gCurrentControlEntry = day->aTemperatureEntries[entryIndex - 1];
|
||||||
}
|
}
|
||||||
else if (dayIndex > 0)
|
else if (dayIndex > 0)
|
||||||
{
|
{
|
||||||
// Use last entry from previous day
|
// Use last entry from previous day
|
||||||
const sControlDay *previousDay = &aControlTable[dayIndex - 1];
|
const sControlDay *previousDay = &gControlTable[dayIndex - 1];
|
||||||
currentControlEntry = previousDay->aTemperatureEntries[previousDay->entryCount - 1];
|
gCurrentControlEntry = previousDay->aTemperatureEntries[previousDay->entryCount - 1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// First entry of the week - wrap to last entry of Sunday
|
// First entry of the week - wrap to last entry of Sunday
|
||||||
const sControlDay *sunday = &aControlTable[6];
|
const sControlDay *sunday = &gControlTable[6];
|
||||||
currentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
|
gCurrentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
ESP_LOGI(TAG, "Active entry found - Time: %02d:%02d, "
|
ESP_LOGI(TAG, "Active entry found - Time: %02d:%02d, "
|
||||||
"Return Temp: %lf, Chamber Temp: %lf",
|
"Return Temp: %lf, Chamber Temp: %lf",
|
||||||
currentControlEntry.timestamp.hour,
|
gCurrentControlEntry.timestamp.hour,
|
||||||
currentControlEntry.timestamp.minute,
|
gCurrentControlEntry.timestamp.minute,
|
||||||
currentControlEntry.fReturnFlowTemperature,
|
gCurrentControlEntry.fReturnFlowTemperature,
|
||||||
currentControlEntry.fChamberTemperature);
|
gCurrentControlEntry.fChamberTemperature);
|
||||||
*/
|
*/
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -347,13 +389,30 @@ void findControlCurrentTemperatureEntry(void)
|
|||||||
|
|
||||||
// If we reached here, current time is after all entries this week
|
// If we reached here, current time is after all entries this week
|
||||||
// Use the last entry (Sunday evening)
|
// Use the last entry (Sunday evening)
|
||||||
const sControlDay *sunday = &aControlTable[6];
|
const sControlDay *sunday = &gControlTable[6];
|
||||||
currentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
|
gCurrentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
|
||||||
|
|
||||||
// ESP_LOGI(TAG, "Using last entry of week - Time: %02d:%02d", currentControlEntry.timestamp.hour, currentControlEntry.timestamp.minute);
|
// ESP_LOGI(TAG, "Using last entry of week - Time: %02d:%02d", gCurrentControlEntry.timestamp.hour, gCurrentControlEntry.timestamp.minute);
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: findControlCurrentTemperatureEntry()");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sControlTemperatureEntry getControlCurrentTemperatureEntry(void)
|
sControlTemperatureEntry getControlCurrentTemperatureEntry(void)
|
||||||
{
|
{
|
||||||
return currentControlEntry;
|
sControlTemperatureEntry ret = gControlTable[0].aTemperatureEntries[0];
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
ret = gCurrentControlEntry;
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: getControlCurrentTemperatureEntry()");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user