in iothub_client/src/iothubtransport_mqtt_common.c [3000:3118]
static PMQTTTRANSPORT_HANDLE_DATA InitializeTransportHandleData(const IOTHUB_CLIENT_CONFIG* upperConfig, PDLIST_ENTRY waitingToSend, IOTHUB_AUTHORIZATION_HANDLE auth_module, const char* moduleId)
{
PMQTTTRANSPORT_HANDLE_DATA state = (PMQTTTRANSPORT_HANDLE_DATA)malloc(sizeof(MQTTTRANSPORT_HANDLE_DATA));
if (state == NULL)
{
LogError("Could not create MQTT transport state. Memory allocation failed.");
}
else
{
memset(state, 0, sizeof(MQTTTRANSPORT_HANDLE_DATA));
if ((state->msgTickCounter = tickcounter_create()) == NULL)
{
LogError("Invalid Argument: iotHubName is empty");
freeTransportHandleData(state);
state = NULL;
}
else if ((state->retry_control_handle = retry_control_create(DEFAULT_RETRY_POLICY, DEFAULT_RETRY_TIMEOUT_IN_SECONDS)) == NULL)
{
LogError("Failed creating default retry control");
freeTransportHandleData(state);
state = NULL;
}
else if ((state->device_id = STRING_construct(upperConfig->deviceId)) == NULL)
{
LogError("failure constructing device_id.");
freeTransportHandleData(state);
state = NULL;
}
else if ((moduleId != NULL) && ((state->module_id = STRING_construct(moduleId)) == NULL))
{
LogError("failure constructing module_id.");
freeTransportHandleData(state);
state = NULL;
}
else if ((state->devicesAndModulesPath = buildDevicesAndModulesPath(upperConfig, moduleId)) == NULL)
{
LogError("failure constructing devicesPath.");
freeTransportHandleData(state);
state = NULL;
}
else
{
if ((state->topic_MqttEvent = buildMqttEventString(upperConfig->deviceId, moduleId)) == NULL)
{
LogError("Could not create topic_MqttEvent for MQTT");
freeTransportHandleData(state);
state = NULL;
}
else
{
state->mqttClient = mqtt_client_init(mqttNotificationCallback, mqttOperationCompleteCallback, state, processErrorCallback, state);
if (state->mqttClient == NULL)
{
LogError("failure initializing mqtt client.");
freeTransportHandleData(state);
state = NULL;
}
else
{
if (upperConfig->protocolGatewayHostName == NULL)
{
state->hostAddress = STRING_construct_sprintf("%s.%s", upperConfig->iotHubName, upperConfig->iotHubSuffix);
}
else
{
state->hostAddress = STRING_construct(upperConfig->protocolGatewayHostName);
}
if (state->hostAddress == NULL)
{
LogError("failure constructing host address.");
freeTransportHandleData(state);
state = NULL;
}
else if ((state->configPassedThroughUsername = buildConfigForUsernameStep1(upperConfig, moduleId)) == NULL)
{
freeTransportHandleData(state);
state = NULL;
}
else
{
DList_InitializeListHead(&(state->telemetry_waitingForAck));
DList_InitializeListHead(&(state->ack_waiting_queue));
DList_InitializeListHead(&(state->pending_get_twin_queue));
state->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
state->isRecoverableError = true;
state->packetId = 1;
state->waitingToSend = waitingToSend;
state->currPacketState = CONNECT_TYPE;
state->keepAliveValue = DEFAULT_MQTT_KEEPALIVE;
state->connect_timeout_in_sec = DEFAULT_CONNACK_TIMEOUT;
state->topics_ToSubscribe = UNSUBSCRIBE_FROM_TOPIC;
srand((unsigned int)get_time(NULL));
state->authorization_module = auth_module;
state->isDestroyCalled = false;
state->isRetryExpiredCallbackCalled = false;
state->isRegistered = false;
state->device_twin_get_sent = false;
state->xioTransport = NULL;
state->portNum = 0;
state->connectFailCount = 0;
state->connectTick = 0;
state->topic_MqttMessage = NULL;
state->topic_GetState = NULL;
state->topic_NotifyState = NULL;
state->topic_DeviceMethods = NULL;
state->topic_InputQueue = NULL;
state->log_trace = state->raw_trace = false;
state->isConnectUsernameSet = false;
state->auto_url_encode_decode = false;
state->conn_attempted = false;
}
}
}
}
}
return state;
}