in src/modules/deviceinfo/src/lib/DeviceInfo.c [181:320]
int DeviceInfoMmiGet(MMI_HANDLE clientSession, const char* componentName, const char* objectName, MMI_JSON_STRING* payload, int* payloadSizeBytes)
{
int status = MMI_OK;
char* value = NULL;
bool isStringValue = true;
char buffer[10] = {0};
if ((NULL == componentName) || (NULL == objectName) || (NULL == payload) || (NULL == payloadSizeBytes))
{
OsConfigLogError(DeviceInfoGetLog(), "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(DeviceInfoGetLog(), "MmiGet(%s, %s) called outside of a valid session", componentName, objectName);
status = EINVAL;
}
if ((MMI_OK == status) && (strcmp(componentName, g_deviceInfoComponentName)))
{
OsConfigLogError(DeviceInfoGetLog(), "MmiGet called for an unsupported component name (%s)", componentName);
status = EINVAL;
}
if (MMI_OK == status)
{
if (0 == strcmp(objectName, g_osNameObject))
{
value = g_osName;
}
else if (0 == strcmp(objectName, g_osVersionObject))
{
value = g_osVersion;
}
else if (0 == strcmp(objectName, g_cpuTypeObject))
{
value = g_cpuType;
}
else if (0 == strcmp(objectName, g_cpuVendorObject))
{
value = g_cpuVendor;
}
else if (0 == strcmp(objectName, g_cpuModelObject))
{
value = g_cpuModel;
}
else if (0 == strcmp(objectName, g_totalMemoryObject))
{
isStringValue = false;
snprintf(buffer, sizeof(buffer), "%lu", g_totalMemory);
value = buffer;
}
else if (0 == strcmp(objectName, g_freeMemoryObject))
{
// Refresh this one at every MmiGet
g_freeMemory = GetFreeMemory(DeviceInfoGetLog());
isStringValue = false;
snprintf(buffer, sizeof(buffer), "%lu", g_freeMemory);
value = buffer;
}
else if (0 == strcmp(objectName, g_kernelNameObject))
{
value = g_kernelName;
}
else if (0 == strcmp(objectName, g_kernelReleaseObject))
{
value = g_kernelRelease;
}
else if (0 == strcmp(objectName, g_kernelVersionObject))
{
value = g_kernelVersion;
}
else if (0 == strcmp(objectName, g_productVendorObject))
{
value = g_productVendor;
}
else if (0 == strcmp(objectName, g_productNameObject))
{
value = g_productName;
}
else if (0 == strcmp(objectName, g_productVersionObject))
{
value = g_productVersion;
}
else if (0 == strcmp(objectName, g_systemCapabilitiesObject))
{
value = g_systemCapabilities;
}
else if (0 == strcmp(objectName, g_systemConfigurationObject))
{
value = g_systemConfiguration;
}
else if (0 == strcmp(objectName, g_osConfigVersionObject))
{
value = OSCONFIG_VERSION;
}
else
{
OsConfigLogError(DeviceInfoGetLog(), "MmiGet called for an unsupported object name (%s)", objectName);
status = EINVAL;
}
}
if (MMI_OK == status)
{
// The string value (can be empty string) is wrapped in "" and is not null terminated
*payloadSizeBytes = (value ? strlen(value) : 0) + (isStringValue ? 2 : 0);
if ((g_maxPayloadSizeBytes > 0) && ((unsigned)*payloadSizeBytes > g_maxPayloadSizeBytes))
{
OsConfigLogError(DeviceInfoGetLog(), "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 + 1);
if (*payload)
{
// snprintf counts in the null terminator for the target string (terminator that is excluded from payload)
snprintf(*payload, *payloadSizeBytes + 1, (isStringValue ? "\"%s\"" : "%s"), value ? value : "");
}
else
{
OsConfigLogError(DeviceInfoGetLog(), "MmiGet: failed to allocate %d bytes", *payloadSizeBytes + 1);
*payloadSizeBytes = 0;
status = ENOMEM;
}
}
OsConfigLogDebug(DeviceInfoGetLog(), "MmiGet(%p, %s, %s, %.*s, %d) returning %d", clientSession, componentName, objectName, *payloadSizeBytes, *payload, *payloadSizeBytes, status);
return status;
}