static int device_method_callback()

in roscpp_azure_iothub/src/ros_azure_iothub_cpp_node.cpp [99:149]


static int device_method_callback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* resp_size, void* userContextCallback)
{
    const char* SetTelemetryIntervalMethod = "SetTelemetryInterval";
    const char* device_id = (const char*)userContextCallback;
    char* end = NULL;
    int newInterval;

    int status = 501;
    const char* RESPONSE_STRING = "{ \"Response\": \"Unknown method requested.\" }";

    ROS_INFO("Device Method called for device %s", device_id);
    ROS_INFO("Device Method name:    %s", method_name);
    ROS_INFO("Device Method payload: %.*s", (int)size, (const char*)payload);

    if (strcmp(method_name, SetTelemetryIntervalMethod) == 0)
    {
        if (payload)
        {
            newInterval = (int)strtol((char*)payload, &end, 10);

            // Interval must be greater than zero.
            if (newInterval > 0)
            {
                // Expect sec and covert to ms
                g_interval = 1000 * (int)strtol((char*)payload, &end, 10);
                status = 200;
                RESPONSE_STRING = "{ \"Response\": \"Telemetry reporting interval updated.\" }";
            }
            else
            {
                status = 500;
                RESPONSE_STRING = "{ \"Response\": \"Invalid telemetry reporting interval.\" }";
            }
        }
    }

    ROS_INFO("Response status: %d", status);
    ROS_INFO("Response payload: %s", RESPONSE_STRING);

    *resp_size = strlen(RESPONSE_STRING);
    *response = reinterpret_cast<unsigned char*>(malloc(*resp_size));
    if (*response)
    {
        memcpy(*response, RESPONSE_STRING, *resp_size);
    }
    else
    {
        status = -1;
    }
    return status;
}