in AZ3166/src/libraries/AzureIoT/src/DevKitMQTTClient.cpp [466:625]
bool DevKitMQTTClient_Init(bool hasDeviceTwin, bool traceOn, const char * modelId)
{
if (iotHubClientHandle != NULL)
{
return true;
}
enableDeviceTwin = hasDeviceTwin;
callbackCounter = 0;
xlogging_set_log_function(AZIoTLog);
srand((unsigned int)time(NULL));
trackingId = 0;
LogInfo("IoThub Version: %s\r\n", IoTHubClient_GetVersionString());
// Create the IoTHub client
if (is_iothub_from_dps)
{
// Use DPS
iothub_hostname = DevkitDPSGetIoTHubURI();
iotHubClientHandle = IoTHubDeviceClient_LL_CreateFromDeviceAuth(iothub_hostname, DevkitDPSGetDeviceID(), MQTT_Protocol);
if (iotHubClientHandle == NULL)
{
LogError(">>>IoTHubDeviceClient_LL_CreateFromDeviceAuth failed %s, %s", iothub_hostname, DevkitDPSGetDeviceID());
return false;
}
}
else
{
if (platform_init() != 0)
{
LogError("Failed to initialize the platform.");
return false;
}
// Load connection from EEPROM
EEPROMInterface eeprom;
uint8_t connString[AZ_IOT_HUB_MAX_LEN + 1] = {'\0'};
int ret = eeprom.read(connString, AZ_IOT_HUB_MAX_LEN, 0x00, AZ_IOT_HUB_ZONE_IDX);
if (ret < 0)
{
LogError("Unable to get the azure iot connection string from EEPROM. Please set the value in configuration mode.");
return false;
}
else if (ret == 0)
{
LogError("The connection string is empty.\r\nPlease set the value in configuration mode.");
return false;
}
iothub_hostname = GetHostNameFromConnectionString((char *)connString);
// Create the IoTHub client
if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString((char *)connString, MQTT_Protocol)) == NULL)
{
LogTrace("Create", "IoT hub establish failed");
return false;
}
}
int keepalive = MQTT_KEEPALIVE_INTERVAL_S;
IoTHubClient_LL_SetOption(iotHubClientHandle, "keepalive", &keepalive);
IoTHubClient_LL_SetOption(iotHubClientHandle, "logtrace", &traceOn);
// Sets the name of ModelId for PnP device.
if (modelId != NULL) {
if (IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_MODEL_ID, modelId) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option \"model_id\"");
return false;
}
}
if (IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_TRUSTED_CERT, trustedCerts) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option \"TrustedCerts\"");
return false;
}
char *product_info = NULL;
if (miniSolutionName == NULL)
{
int len = snprintf(NULL, 0, "IoT_DevKit_%s", getDevkitVersion());
product_info = (char *)malloc(len + 1);
snprintf(product_info, len + 1, "IoT_DevKit_%s", getDevkitVersion());
}
else
{
int len = snprintf(NULL, 0, "IoT_DevKit_%s_%s", getDevkitVersion(), miniSolutionName);
product_info = (char *)malloc(len + 1);
snprintf(product_info, len + 1, "IoT_DevKit_%s_%s", getDevkitVersion(), miniSolutionName);
}
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "product_info", product_info) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option \"product_info\"");
free(product_info);
return false;
}
else
{
free(product_info);
}
// Setting Message call back, so we can receive commands.
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
{
LogError("IoTHubClient_LL_SetMessageCallback..........FAILED!");
return false;
}
if (IoTHubClient_LL_SetConnectionStatusCallback(iotHubClientHandle, ConnectionStatusCallback, &statusContext) != IOTHUB_CLIENT_OK)
{
LogError("IoTHubClient_LL_SetConnectionStatusCallback..........FAILED!");
return false;
}
if (enableDeviceTwin)
{
if (IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientHandle, DeviceTwinCallback, NULL) != IOTHUB_CLIENT_OK)
{
LogError("Failed on IoTHubClient_LL_SetDeviceTwinCallback");
return false;
}
if (IoTHubClient_LL_SetDeviceMethodCallback(iotHubClientHandle, DeviceMethodCallback, NULL) != IOTHUB_CLIENT_OK)
{
LogError("Failed on IoTHubClient_LL_SetDeviceMethodCallback");
return false;
}
}
iothub_check_ms = SystemTickCounterRead();
// Waiting for the confirmation
resetClient = false;
uint64_t start_ms = SystemTickCounterRead();
while (true)
{
IoTHubClient_LL_DoWork(iotHubClientHandle);
if (clientConnected)
{
break;
}
if (resetClient)
{
return false;
}
int diff = (int)(SystemTickCounterRead() - start_ms);
if (diff >= CONNECT_TIMEOUT_MS)
{
// Time out, reset the client
resetClient = true;
return false;
}
ThreadAPI_Sleep(500);
}
return true;
}