Skip to content

Instantly share code, notes, and snippets.

@Morozov-5F
Last active May 2, 2019 01:37
Show Gist options
  • Save Morozov-5F/3a1d065ff43fa4247a804b16377c10c5 to your computer and use it in GitHub Desktop.
Save Morozov-5F/3a1d065ff43fa4247a804b16377c10c5 to your computer and use it in GitHub Desktop.
Azure issue
static void
s_azure_client_connection_status_cb(IOTHUB_CLIENT_CONNECTION_STATUS result,
IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason,
void * ctx)
{
const char * connection_result = NULL;
const char * connection_reason = NULL;
if (IOTHUB_CLIENT_CONNECTION_AUTHENTICATED == result)
{
connection_result = "ONLINE";
is_online = TRUE;
}
else
{
connection_result = "OFFLINE";
is_online = FALSE;
}
switch (reason)
{
case IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN:
connection_reason = "EXPIRED SAS TOKEN";
break;
case IOTHUB_CLIENT_CONNECTION_DEVICE_DISABLED:
connection_reason = "DEVICE DISABLED";
break;
case IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL:
connection_reason = "BAD CREDENTIAL";
break;
case IOTHUB_CLIENT_CONNECTION_RETRY_EXPIRED:
connection_reason = "RETRY EXPIRED";
break;
case IOTHUB_CLIENT_CONNECTION_NO_NETWORK:
connection_reason = "NO NETWORK";
break;
case IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR:
connection_reason = "COMMUNICATION ERROR";
break;
case IOTHUB_CLIENT_CONNECTION_OK:
connection_reason = "OK";
break;
default:
connection_reason = "UNKNOWN";
break;
}
logger("Azure connection status: %s; reason: %s", connection_result, connection_reason);
/* Set some internal diagnostic data depending on connection status */
return;
}
static int
s_azure_main_loop(cat_azure_ctx_t * azure_ctx)
{
IoTHubDeviceClient_LL_DoWork(azure_ctx->azct_client);
}
static int
s_azure_sdk_ll_ctx_init(cat_azure_ctx_t * azure_ctx)
{
/* There are a lot of other init code, that is not directly related to Azure */
/* Initialize a low-level client */
azure_ctx->azct_client = IoTHubDeviceClient_LL_CreateFromConnectionString(connection_string,
MQTT_Protocol);
CAT_ASSERT(NULL != azure_ctx->azct_client);
/* Those options remain unchanged. We need this section to suppress warnings */
UNUSED(OPTION_PROXY_HOST);
UNUSED(OPTION_PROXY_USERNAME);
UNUSED(OPTION_PROXY_PASSWORD);
UNUSED(OPTION_SAS_TOKEN_LIFETIME);
UNUSED(OPTION_SAS_TOKEN_REFRESH_TIME);
UNUSED(OPTION_CBS_REQUEST_TIMEOUT);
UNUSED(OPTION_MIN_POLLING_TIME);
UNUSED(OPTION_BATCHING);
UNUSED(OPTION_PRODUCT_INFO);
UNUSED(OPTION_C2D_KEEP_ALIVE_FREQ_SECS);
UNUSED(OPTION_DIAGNOSTIC_SAMPLING_PERCENTAGE);
UNUSED(OPTION_CONNECTION_TIMEOUT);
UNUSED(OPTION_AUTO_URL_ENCODE_DECODE);
UNUSED(OPTION_SERVICE_SIDE_KEEP_ALIVE_FREQ_SECS);
UNUSED(OPTION_REMOTE_IDLE_TIMEOUT_RATIO);
UNUSED(OPTION_BLOB_UPLOAD_TIMEOUT_SECS);
/* Enable logging */
azure_log_trace = 1;
iot_hub_ret = IoTHubDeviceClient_LL_SetOption(azure_ctx->azct_client,
OPTION_LOG_TRACE,
&azure_log_trace);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Keep the connection alive */
iot_hub_ret = IoTHubDeviceClient_LL_SetOption(azure_ctx->azct_client,
OPTION_KEEP_ALIVE,
&keep_alive_interval_sec);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Set message timeout */
iot_hub_ret = IoTHubDeviceClient_LL_SetOption(azure_ctx->azct_client,
OPTION_MESSAGE_TIMEOUT,
&azure_message_timeout_msec);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Assign certificates */
iot_hub_ret = IoTHubDeviceClient_LL_SetOption(azure_ctx->azct_client,
OPTION_X509_CERT,
certificates);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Assign private key */
iot_hub_ret = IoTHubDeviceClient_LL_SetOption(azure_ctx->azct_client,
OPTION_X509_PRIVATE_KEY,
private_key);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
iot_hub_ret = IoTHubDeviceClient_LL_SetOption(azure_ctx->azct_client,
OPTION_TLS_VERSION,
&tls_version);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Set retry policy */
iot_hub_ret = IoTHubDeviceClient_LL_SetRetryPolicy(azure_ctx->azct_client,
IOTHUB_CLIENT_RETRY_INTERVAL,
0);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Setup callbacks */
iot_hub_ret = IoTHubDeviceClient_LL_SetMessageCallback(azure_ctx->azct_client,
s_azure_receive_message_cb,
azure_ctx);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
iot_hub_ret = IoTHubDeviceClient_LL_SetConnectionStatusCallback(azure_ctx->azct_client,
s_azure_client_connection_status_cb,
azure_ctx);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
iot_hub_ret = IoTHubDeviceClient_LL_SetDeviceMethodCallback(azure_ctx->azct_client,
s_azure_device_method_cb,
azure_ctx);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
iot_hub_ret = IoTHubDeviceClient_LL_SetDeviceTwinCallback(azure_ctx->azct_client,
s_azure_device_twin_cb,
azure_ctx);
CAT_ASSERT(IOTHUB_CLIENT_OK == iot_hub_ret);
/* Here we start main Azure thread that call Do_Work function with given interval in milliseconds */
set_interval(s_azure_main_loop, azure_ctx, 100);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment