IOTHUB_CLIENT_RESULT ReportPropertyToIotHub()

in src/adapters/pnp/PnpUtils.c [527:619]


IOTHUB_CLIENT_RESULT ReportPropertyToIotHub(const char* componentName, const char* propertyName, size_t* lastPayloadHash)
{
    IOTHUB_CLIENT_RESULT result = IOTHUB_CLIENT_OK;
    char* valuePayload = NULL;
    int valueLength = 0;
    char* decoratedPayload = NULL;
    int decoratedLength = 0;
    size_t hashPayload = 0;
    bool reportProperty = true;
    bool platformAlreadyRunning = true;
    int mpiResult = MPI_OK;

    LogAssert(GetLog(), NULL != componentName);
    LogAssert(GetLog(), NULL != propertyName);

    if (NULL == g_moduleHandle)
    {
        OsConfigLogError(GetLog(), "%s: the component needs to be initialized before reporting properties", componentName);
        return IOTHUB_CLIENT_ERROR;
    }

    mpiResult = CallMpiGet(componentName, propertyName, &valuePayload, &valueLength, GetLog());
    if ((MPI_OK != mpiResult) && RefreshMpiClientSession(&platformAlreadyRunning) && (false == platformAlreadyRunning))
    {
        CallMpiFree(valuePayload);

        mpiResult = CallMpiGet(componentName, propertyName, &valuePayload, &valueLength, GetLog());
    }

    if ((MPI_OK == mpiResult) && (valueLength > 0) && (NULL != valuePayload))
    {
        decoratedLength = strlen(componentName) + strlen(propertyName) + valueLength + EXTRA_PROP_PAYLOAD_ESTIMATE;
        decoratedPayload = (char*)malloc(decoratedLength);
        if (NULL != decoratedPayload)
        {
            snprintf(decoratedPayload, decoratedLength, g_propertyReadTemplate, componentName, propertyName, valueLength, valuePayload);

            LogAssert(GetLog(), decoratedLength >= (int)strlen(decoratedPayload));
            decoratedLength = strlen(decoratedPayload);

            if (NULL != lastPayloadHash)
            {
                hashPayload = HashString(decoratedPayload);
                if (hashPayload == *lastPayloadHash)
                {
                    reportProperty = false;
                }
                else
                {
                    *lastPayloadHash = hashPayload;
                }
            }

            if (reportProperty)
            {
                result = IoTHubDeviceClient_LL_SendReportedState(g_moduleHandle, (const unsigned char*)decoratedPayload, decoratedLength, ReadReportedStateCallback, (void*)propertyName);

                OsConfigLogDebug(GetLog(), "%s.%s: reported %.*s (%d bytes), result: %d", componentName, propertyName, decoratedLength, decoratedPayload, decoratedLength, result);

                if (IOTHUB_CLIENT_OK != result)
                {
                    OsConfigLogError(GetLog(), "%s.%s: IoTHubDeviceClient_LL_SendReportedState failed with %d", componentName, propertyName, result);
                }
            }
        }
        else
        {
            OsConfigLogError(GetLog(), "%s: out of memory allocating %u bytes to report property %s", componentName, decoratedLength, propertyName);
        }
    }
    else
    {
        // Avoid log abuse when a component specified in configuration is not active
        if (IsDebugLoggingEnabled())
        {
            if (MPI_OK == mpiResult)
            {
                OsConfigLogError(GetLog(), "%s.%s: MpiGet returned MMI_OK with no payload", componentName, propertyName);
            }
            else
            {
                OsConfigLogError(GetLog(), "%s.%s: MpiGet failed with %d", componentName, propertyName, mpiResult);
            }
        }
        result = IOTHUB_CLIENT_ERROR;
    }

    CallMpiFree(valuePayload);

    FREE_MEMORY(decoratedPayload);

    return result;
}