Sterling
DIIDevHeads IoT Integration Server
•Created by Marvee Amasi on 6/11/2024 in #middleware-and-os
What is wrong with my code ?
Perhaps you could try the code this way 😗:
void SensorActions(void *parameters)
{
PushMessageToSerial("Setup", "Sensor's Monitor task is initialized..", LOG_INFO);
while (1)
{
while (xQueueReceive(queue_sensors, (void *)&action, 10) == pdTRUE)
{
switch (action)
{
case actions_t::ACTION_SENSOR_VERSION:
{
uint8_t version[4] = {0};
sensor.GetVersions(version);
// Calculate the length of the version string
char tempBuffer[20]; // Buffer large enough to hold the version string
int length = snprintf(tempBuffer, sizeof(tempBuffer), "%d.%d.%d.%d", version[0], version[1], version[2], version[3]);
if (length < 0 || length >= sizeof(tempBuffer))
{
// Handle error
break;
}
// Allocate memory for the final string
char *buf = new char[length + 1];
snprintf(buf, length + 1, "%d.%d.%d.%d", version[0], version[1], version[2], version[3]);
PushMessageToSerial("Sensors", std::string(buf), LOG_INFO);
delete[] buf; // Correctly deallocate memory
}
break;
default:
break;
}
}
vTaskSuspend(NULL); // Suspend task to not block the other lower priority tasks
}
}
void SensorActions(void *parameters)
{
PushMessageToSerial("Setup", "Sensor's Monitor task is initialized..", LOG_INFO);
while (1)
{
while (xQueueReceive(queue_sensors, (void *)&action, 10) == pdTRUE)
{
switch (action)
{
case actions_t::ACTION_SENSOR_VERSION:
{
uint8_t version[4] = {0};
sensor.GetVersions(version);
// Calculate the length of the version string
char tempBuffer[20]; // Buffer large enough to hold the version string
int length = snprintf(tempBuffer, sizeof(tempBuffer), "%d.%d.%d.%d", version[0], version[1], version[2], version[3]);
if (length < 0 || length >= sizeof(tempBuffer))
{
// Handle error
break;
}
// Allocate memory for the final string
char *buf = new char[length + 1];
snprintf(buf, length + 1, "%d.%d.%d.%d", version[0], version[1], version[2], version[3]);
PushMessageToSerial("Sensors", std::string(buf), LOG_INFO);
delete[] buf; // Correctly deallocate memory
}
break;
default:
break;
}
}
vTaskSuspend(NULL); // Suspend task to not block the other lower priority tasks
}
}
7 replies