I have a MQTT device that connects to Central and submits telemetry. I have created some commands on Central that trigger successfully on the device. However, when I send string value via command, I am having troubles finding the right command to store that value in a variable for further use in the code. I can display the content of the value as follows:
LogInfo("URL: %*s", az_span_size(command.payload)-2, az_span_ptr(command.payload) + 1);
This results in:
[INFO] URL: https://raw.githubusercontent.com/testurl"
However I also have an issue when I send a different command (say version) and display it in the same way, I get:
[INFO] Firmware Version: 1.0.1"thubusercontent.com/testurl"
So I guess, I have 2 questions:
It would be great if I could use az_ command set as this is all happening in SDK C context.
Many thanks
Tried using az_span_to_string and it crashes. Cannot see any other direcgtly relevant az_ function taht I could use.
Here is the code being used:
if (az_span_is_content_equal(command.command_name, COMMAND_NAME_OTA_URL))
{
LogInfo("URL: %*s", az_span_size(command.payload)-2, az_span_ptr(command.payload) + 1);
response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
}
else if (az_span_is_content_equal(command.command_name, COMMAND_NAME_TOGGLE_C12880))
{
SEN_C12880 = !SEN_C12880;
LogInfo("Sensor State: %s", (SEN_C12880 ? "ON" : "OFF"));
response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
}
else if (az_span_is_content_equal(command.command_name, COMMAND_NAME_TOGGLE_FIRMWARE_VERSION))
{
LogInfo("Firmware Version: %*s", az_span_size(command.payload)-2, az_span_ptr(command.payload) + 1);
response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
}
else if (az_span_is_content_equal(command.command_name, COMMAND_NAME_DISPLAY_TEXT))
{
// The payload comes surrounded by quotes, so to remove them we offset the payload by 1 and its size by 2.
LogInfo("OLED display: %.*s", az_span_size(command.payload) - 2, az_span_ptr(command.payload) + 1);
response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
}
else
{
LogError("Command not recognized (%.*s).", az_span_size(command.command_name), az_span_ptr(command.command_name));
response_code = COMMAND_RESPONSE_CODE_REJECTED;
}
So...after a bit of digging and good ol' trial 'n' error...it really comes to the following:
Defne variables:
char cmd_payload[1024];
char cmd_name[128];
In the Azure_IoT_PnP_Template.cpp:
int azure_pnp_handle_command_request(azure_iot_t* azure_iot, command_request_t command)
{
_az_PRECONDITION_NOT_NULL(azure_iot);
uint16_t response_code;
sprintf(cmd_name, "%*s",az_span_size(command.command_name), az_span_ptr(command.command_name));
String str_cmd_name = String(cmd_name, az_span_size(command.command_name));
strcpy(cmd_name, str_cmd_name.c_str());
The rest is history!!!