in iothub_service_client/src/iothub_registrymanager.c [825:962]
static IOTHUB_REGISTRYMANAGER_RESULT parseDeviceOrModuleListJson(BUFFER_HANDLE jsonBuffer, SINGLYLINKEDLIST_HANDLE deviceOrModuleList, IOTHUB_REGISTRYMANAGER_MODEL_TYPE struct_type, int struct_version)
{
IOTHUB_REGISTRYMANAGER_RESULT result;
const char* bufferStr = NULL;
JSON_Value* root_value = NULL;
JSON_Array* device_or_module_array = NULL;
JSON_Status jsonStatus = JSONFailure;
if (jsonBuffer == NULL)
{
LogError("jsonBuffer cannot be NULL");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
else if (deviceOrModuleList == NULL)
{
LogError("deviceOrModuleList cannot be NULL");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
else
{
if ((bufferStr = (const char*)BUFFER_u_char(jsonBuffer)) == NULL)
{
LogError("BUFFER_u_char failed");
result = IOTHUB_REGISTRYMANAGER_ERROR;
}
else if ((root_value = json_parse_string(bufferStr)) == NULL)
{
LogError("json_parse_string failed");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
else if ((device_or_module_array = json_value_get_array(root_value)) == NULL)
{
LogError("json_value_get_object failed");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
else
{
result = IOTHUB_REGISTRYMANAGER_OK;
size_t array_count = json_array_get_count(device_or_module_array);
for (size_t i = 0; i < array_count; i++)
{
JSON_Object* device_or_module_object = NULL;
IOTHUB_DEVICE_OR_MODULE iothubDeviceOrModule;
if ((device_or_module_object = json_array_get_object(device_or_module_array, i)) == NULL)
{
LogError("json_array_get_object failed");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
else
{
initializeDeviceOrModuleInfoMembers(&iothubDeviceOrModule);
result = parseDeviceOrModuleJsonObject(device_or_module_object, &iothubDeviceOrModule);
if (IOTHUB_REGISTRYMANAGER_OK != result)
{
free_deviceOrModule_members(&iothubDeviceOrModule);
}
else
{
if (struct_type == IOTHUB_REGISTRYMANAGER_MODEL_TYPE_DEVICE)
{
result = addDeviceOrModuleToLinkedListAsDevice(&iothubDeviceOrModule, deviceOrModuleList);
if (result == IOTHUB_REGISTRYMANAGER_OK) //only free these if we pass, if we fail, will deal with below
{
free_nonDevice_members_from_deviceOrModule(&iothubDeviceOrModule);
}
}
else //IOTHUB_REGISTRYMANAGER_MODEL_TYPE_MODULE
{
result = addDeviceOrModuleToLinkedListAsModule(&iothubDeviceOrModule, deviceOrModuleList, struct_version);
//no nonModule members to free... yet
}
if (result != IOTHUB_REGISTRYMANAGER_OK)
{
free_deviceOrModule_members(&iothubDeviceOrModule);
}
}
}
if ((device_or_module_object != NULL) && ((jsonStatus = json_object_clear(device_or_module_object)) != JSONSuccess))
{
LogError("json_object_clear failed");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
if (result != IOTHUB_REGISTRYMANAGER_OK)
{
break;
}
}
}
}
if (device_or_module_array != NULL)
{
if ((jsonStatus = json_array_clear(device_or_module_array)) != JSONSuccess)
{
LogError("json_array_clear failed");
result = IOTHUB_REGISTRYMANAGER_JSON_ERROR;
}
}
if (root_value != NULL)
{
json_value_free(root_value);
}
if (result != IOTHUB_REGISTRYMANAGER_OK)
{
if (deviceOrModuleList != NULL)
{
LIST_ITEM_HANDLE itemHandle = singlylinkedlist_get_head_item(deviceOrModuleList);
while (itemHandle != NULL)
{
const void* curr_item = singlylinkedlist_item_get_value(itemHandle);
if (struct_type == IOTHUB_REGISTRYMANAGER_MODEL_TYPE_DEVICE)
{
IOTHUB_DEVICE* deviceInfo = (IOTHUB_DEVICE*)curr_item;
free_device_members(deviceInfo);
free(deviceInfo);
}
else //IOTHUB_REGISTRYMANAGER_MODEL_TYPE_MODULE
{
IOTHUB_MODULE* moduleInfo = (IOTHUB_MODULE*)curr_item;
IoTHubRegistryManager_FreeModuleMembers(moduleInfo);
free(moduleInfo);
}
LIST_ITEM_HANDLE lastHandle = itemHandle;
itemHandle = singlylinkedlist_get_next_item(itemHandle);
singlylinkedlist_remove(deviceOrModuleList, lastHandle);
}
}
}
return result;
}