Skip to content

Commit

Permalink
Merge pull request #6 from ewertons/ewertons/fixsasrefresh
Browse files Browse the repository at this point in the history
Fix crash in ESP32 when Azure IoT layer triggers a SAS token refresh
  • Loading branch information
ewertons authored Jan 21, 2022
2 parents 952b528 + 3b88af7 commit 84ea68f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
16 changes: 10 additions & 6 deletions examples/Azure_IoT_Central_ESP32/AzureIoT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ log_function_t default_logging_function = NULL;
#define DPS_REGISTER_CUSTOM_PAYLOAD_BEGIN "{\"modelId\":\""
#define DPS_REGISTER_CUSTOM_PAYLOAD_END "\"}"

#define NUMBER_OF_SECONDS_IN_A_MINUTE 60

#define EXIT_IF_TRUE(condition, retcode, message, ...) \
do \
{ \
Expand All @@ -45,8 +47,6 @@ log_function_t default_logging_function = NULL;
#define EXIT_IF_AZ_FAILED(azresult, retcode, message, ...) \
EXIT_IF_TRUE(az_result_failed(azresult), retcode, message, ##__VA_ARGS__ )

#define NUMBER_OF_SECONDS_IN_A_MINUTE 60

/* --- Internal function prototypes --- */
static uint32_t get_current_unix_time();

Expand Down Expand Up @@ -160,7 +160,7 @@ int azure_iot_stop(azure_iot_t* azure_iot)
{
if (azure_iot->mqtt_client_handle != NULL)
{
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
{
azure_iot->state = azure_iot_state_error;
LogError("Failed deinitializing MQTT client.");
Expand Down Expand Up @@ -289,7 +289,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
azure_iot->state = azure_iot_state_subscribing_to_dps;

packet_id = azure_iot->config->mqtt_client_interface.mqtt_client_subscribe(
azure_iot->mqtt_client_handle,
azure_iot->mqtt_client_handle,
AZ_SPAN_FROM_STR(AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC),
mqtt_qos_at_most_once);

Expand Down Expand Up @@ -417,6 +417,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
case azure_iot_state_provisioned:
// Disconnect from Provisioning Service first.
if (azure_iot->config->use_device_provisioning &&
azure_iot->mqtt_client_handle != NULL &&
azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
{
azure_iot->state = azure_iot_state_error;
Expand Down Expand Up @@ -470,7 +471,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
azure_iot->state = azure_iot_state_subscribing_to_pnp_props;

packet_id = azure_iot->config->mqtt_client_interface.mqtt_client_subscribe(
azure_iot->mqtt_client_handle,
azure_iot->mqtt_client_handle,
AZ_SPAN_FROM_STR(AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_SUBSCRIBE_TOPIC),
mqtt_qos_at_least_once);

Expand Down Expand Up @@ -510,16 +511,19 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
{
azure_iot->state = azure_iot_state_error;
LogError("Failed getting current time for checking SAS token expiration.");
return;
}
else if ((azure_iot->sas_token_expiration_time - now) < SAS_TOKEN_REFRESH_THRESHOLD_IN_SECS)
{
azure_iot->state = azure_iot_state_refreshing_sas;
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
{
azure_iot->state = azure_iot_state_error;
LogError("Failed de-initializing MQTT client.");
return;
}

azure_iot->mqtt_client_handle = NULL;
}
break;
case azure_iot_state_refreshing_sas:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ static int mqtt_client_init_function(mqtt_client_config_t* mqtt_client_config, m
mqtt_config.disable_auto_reconnect = false;
mqtt_config.event_handle = esp_mqtt_event_handler;
mqtt_config.user_context = NULL;
mqtt_config.buffer_size = 1024;
mqtt_config.cert_pem = (const char*)ca_pem;

LogInfo("MQTT client target uri set to '%s'", mqtt_broker_uri);
Expand Down
8 changes: 6 additions & 2 deletions examples/Azure_IoT_Central_ESP32_AzureIoTKit/AzureIoT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int azure_iot_stop(azure_iot_t* azure_iot)
{
if (azure_iot->mqtt_client_handle != NULL)
{
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
{
azure_iot->state = azure_iot_state_error;
LogError("Failed deinitializing MQTT client.");
Expand Down Expand Up @@ -417,6 +417,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
case azure_iot_state_provisioned:
// Disconnect from Provisioning Service first.
if (azure_iot->config->use_device_provisioning &&
azure_iot->mqtt_client_handle != NULL &&
azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
{
azure_iot->state = azure_iot_state_error;
Expand Down Expand Up @@ -510,16 +511,19 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
{
azure_iot->state = azure_iot_state_error;
LogError("Failed getting current time for checking SAS token expiration.");
return;
}
else if ((azure_iot->sas_token_expiration_time - now) < SAS_TOKEN_REFRESH_THRESHOLD_IN_SECS)
{
azure_iot->state = azure_iot_state_refreshing_sas;
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
{
azure_iot->state = azure_iot_state_error;
LogError("Failed de-initializing MQTT client.");
return;
}

azure_iot->mqtt_client_handle = NULL;
}
break;
case azure_iot_state_refreshing_sas:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ static int mqtt_client_init_function(mqtt_client_config_t* mqtt_client_config, m
mqtt_config.disable_auto_reconnect = false;
mqtt_config.event_handle = esp_mqtt_event_handler;
mqtt_config.user_context = NULL;
mqtt_config.buffer_size = 1024;
mqtt_config.cert_pem = (const char*)ca_pem;

LogInfo("MQTT client target uri set to '%s'", mqtt_broker_uri);

mqtt_client = esp_mqtt_client_init(&mqtt_config);

if (mqtt_client == NULL)
Expand Down Expand Up @@ -181,7 +179,7 @@ static int mqtt_client_deinit_function(mqtt_client_handle_t mqtt_client_handle)
esp_mqtt_client_handle_t esp_mqtt_client_handle = (esp_mqtt_client_handle_t)mqtt_client_handle;

LogInfo("MQTT client being disconnected.");

if (esp_mqtt_client_stop(esp_mqtt_client_handle) != ESP_OK)
{
LogError("Failed stopping MQTT client.");
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Azure SDK for C
version=1.0.0-beta.2
version=1.0.0-beta.3
author=Microsoft Corporation
maintainer=Microsoft Corporation <aziotarduino@microsoft.com>
sentence=Azure SDK for C library (1.3.0-beta.1) for Arduino.
Expand Down

0 comments on commit 84ea68f

Please sign in to comment.