int ConfigurationMmiGet()

in src/modules/configuration/src/lib/Configuration.c [360:493]


int ConfigurationMmiGet(MMI_HANDLE clientSession, const char* componentName, const char* objectName, MMI_JSON_STRING* payload, int* payloadSizeBytes)
{
    JSON_Value* jsonValue = NULL;
    char* serializedValue = NULL;
    int status = MMI_OK;

    if ((NULL == componentName) || (NULL == objectName) || (NULL == payload) || (NULL == payloadSizeBytes))
    {
        OsConfigLogError(ConfigurationGetLog(), "MmiGet(%s, %s, %p, %p) called with invalid arguments", componentName, objectName, payload, payloadSizeBytes);
        status = EINVAL;
        return status;
    }

    *payload = NULL;
    *payloadSizeBytes = 0;

    if (!IsValidSession(clientSession))
    {
        OsConfigLogError(ConfigurationGetLog(), "MmiGet(%s, %s) called outside of a valid session", componentName, objectName);
        status = EINVAL;
    }
    else if (0 != strcmp(componentName, g_configurationComponentName))
    {
        OsConfigLogError(ConfigurationGetLog(), "MmiGet called for an unsupported component name (%s)", componentName);
        status = EINVAL;
    }
    else
    {
        if (0 == strcmp(objectName, g_modelVersionObject))
        {
            jsonValue = json_value_init_number((double)g_modelVersion);
        }
        else if (0 == strcmp(objectName, g_refreshIntervalObject))
        {
            jsonValue = json_value_init_number((double)g_refreshInterval);
        }
        else if (0 == strcmp(objectName, g_localManagementEnabledObject))
        {
            jsonValue = json_value_init_boolean(g_localManagementEnabled ? 1 : 0);
        }
        else if (0 == strcmp(objectName, g_debugLoggingEnabledObject))
        {
            jsonValue = json_value_init_boolean(g_debugLoggingEnabled ? 1 : 0);
        }
        else if (0 == strcmp(objectName, g_iotHubManagementEnabledObject))
        {
            jsonValue = json_value_init_boolean(g_iotHubManagementEnabled ? 1 : 0);
        }
        else if (0 == strcmp(objectName, g_iotHubProtocolObject))
        {
            switch (g_iotHubProtocol)
            {
                case 1:
                    jsonValue = json_value_init_string(g_mqtt);
                    break;

                case 2:
                    jsonValue = json_value_init_string(g_mqttWebSocket);
                    break;

                case 0:
                default:
                    jsonValue = json_value_init_string(g_auto);

            }
        }
        else if (0 == strcmp(objectName, g_gitManagementEnabledObject))
        {
            jsonValue = json_value_init_boolean(g_gitManagementEnabled ? 1 : 0);
        }
        else if (0 == strcmp(objectName, g_gitBranchObject))
        {
            jsonValue = json_value_init_string(g_gitBranch);
        }
        else
        {
            OsConfigLogError(ConfigurationGetLog(), "MmiGet called for an unsupported object (%s)", objectName);
            status = EINVAL;
        }

        if ((0 == status) && (NULL == jsonValue))
        {
            OsConfigLogError(ConfigurationGetLog(), "MmiGet(%s, %s) failed due to json_value_init_* failure", componentName, objectName);
            status = ENOENT;
        }
    }

    if (MMI_OK == status)
    {
        if (NULL == (serializedValue = json_serialize_to_string(jsonValue)))
        {
            OsConfigLogError(ConfigurationGetLog(), "MmiGet(%s, %s): json_serialize_to_string failed", componentName, objectName);
            status = ENOENT;
        }
        else
        {
            *payloadSizeBytes = (int)strlen(serializedValue);

            if ((g_maxPayloadSizeBytes > 0) && ((unsigned)*payloadSizeBytes > g_maxPayloadSizeBytes))
            {
                OsConfigLogError(ConfigurationGetLog(), "MmiGet(%s, %s) insufficient maxmimum size (%d bytes) versus data size (%d bytes), reported buffer will be truncated",
                    componentName, objectName, g_maxPayloadSizeBytes, *payloadSizeBytes);

                *payloadSizeBytes = g_maxPayloadSizeBytes;
            }

            *payload = (MMI_JSON_STRING)malloc(*payloadSizeBytes);
            if (*payload)
            {
                memcpy(*payload, serializedValue, *payloadSizeBytes);
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "MmiGet: failed to allocate %d bytes", *payloadSizeBytes + 1);
                *payloadSizeBytes = 0;
                status = ENOMEM;
            }
        }
    }

    OsConfigLogDebug(ConfigurationGetLog(), "MmiGet(%p, %s, %s, '%.*s', %d) returning %d", clientSession, componentName, objectName, *payloadSizeBytes, *payload, *payloadSizeBytes, status);

    if (NULL != serializedValue)
    {
        json_free_serialized_string(serializedValue);
    }

    if (NULL != jsonValue)
    {
        json_value_free(jsonValue);
    }

    return status;
}