int AdhsMmiGet()

in src/modules/adhs/src/lib/Adhs.c [141:275]


int AdhsMmiGet(MMI_HANDLE clientSession, const char* componentName, const char* objectName, MMI_JSON_STRING* payload, int* payloadSizeBytes)
{
    int status = MMI_OK;
    const char* value = NULL;
    char* fileContent = NULL;
    unsigned int fileContentSizeBytes = 0;
    regmatch_t matchGroups[3] = {0};
    regex_t permissionRegex = {0};
    char* currentMatch = NULL;
    unsigned int currentMatchSizeBytes = 0;

    if ((NULL == componentName) || (NULL == objectName) || (NULL == payload) || (NULL == payloadSizeBytes))
    {
        OsConfigLogError(AdhsGetLog(), "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(AdhsGetLog(), "MmiGet(%s, %s) called outside of a valid session", componentName, objectName);
        status = EINVAL;
    }
    else if (0 != strcmp(componentName, g_adhsComponentName))
    {
        OsConfigLogError(AdhsGetLog(), "MmiGet called for an unsupported component name '%s'", componentName);
        status = EINVAL;
    }
    else if (0 != strcmp(objectName, g_reportedOptInObjectName))
    {
        OsConfigLogError(AdhsGetLog(), "MmiGet called for an unsupported object name '%s'", objectName);
        status = EINVAL;
    }
    else
    {
        fileContent = LoadStringFromFile(g_adhsConfigFile, false, AdhsGetLog());
        if (NULL != fileContent)
        {
            fileContentSizeBytes = strlen(fileContent);
            if (0 == regcomp(&permissionRegex, g_permissionConfigPattern, REG_EXTENDED))
            {
                if (0 == regexec(&permissionRegex, fileContent, ARRAY_SIZE(matchGroups), matchGroups, 0))
                {
                    // Property value is located in the third match group.
                    if ((IsValidMatchOffsets(matchGroups[0], fileContentSizeBytes)) &&
                        (IsValidMatchOffsets(matchGroups[0], fileContentSizeBytes)) &&
                        (IsValidMatchOffsets(matchGroups[0], fileContentSizeBytes)))
                    {
                        currentMatch = fileContent + matchGroups[2].rm_so;
                        currentMatchSizeBytes = matchGroups[2].rm_eo - matchGroups[2].rm_so;

                        for (unsigned int i = 0; i < g_permissionConfigMapCount; i++)
                        {
                            if ((currentMatchSizeBytes == strlen(g_permissionConfigMapKeys[i])) &&
                                (0 == strncmp(currentMatch, g_permissionConfigMapKeys[i], currentMatchSizeBytes)))
                            {
                                value = g_permissionConfigMapValues[i];
                                break;
                            }
                        }
                    }

                    if (NULL == value)
                    {
                        if (IsDebugLoggingEnabled())
                        {
                            OsConfigLogError(AdhsGetLog(), "MmiGet failed to find valid TOML property '%s'", g_permissionConfigName);
                        }
                        status = EINVAL;
                    }
                }
                else
                {
                    if (IsDebugLoggingEnabled())
                    {
                        OsConfigLogError(AdhsGetLog(), "MmiGet failed to find TOML property '%s'", g_permissionConfigName);
                    }
                    status = EINVAL;
                }

                regfree(&permissionRegex);
            }
            else
            {
                OsConfigLogError(AdhsGetLog(), "MmiGet failed to compile regular expression '%s'", g_permissionConfigPattern);
                status = EINVAL;
            }
        }
        else
        {
            if (IsDebugLoggingEnabled())
            {
                OsConfigLogError(AdhsGetLog(), "MmiGet failed to read TOML file '%s'", g_adhsConfigFile);
            }
            status = EINVAL;
        }

        // Reset status and payload if TOML file could not be parsed or property not found, as it may yet have to be configured.
        if (MMI_OK != status)
        {
            value = g_permissionConfigMapValues[0];
            status = MMI_OK;
        }

        *payloadSizeBytes = strlen(value);

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

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

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

    FREE_MEMORY(fileContent);

    return status;
}