Skip to content

Commit 84ea68f

Browse files
authored
Merge pull request #6 from ewertons/ewertons/fixsasrefresh
Fix crash in ESP32 when Azure IoT layer triggers a SAS token refresh
2 parents 952b528 + 3b88af7 commit 84ea68f

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

examples/Azure_IoT_Central_ESP32/AzureIoT.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ log_function_t default_logging_function = NULL;
3232
#define DPS_REGISTER_CUSTOM_PAYLOAD_BEGIN "{\"modelId\":\""
3333
#define DPS_REGISTER_CUSTOM_PAYLOAD_END "\"}"
3434

35+
#define NUMBER_OF_SECONDS_IN_A_MINUTE 60
36+
3537
#define EXIT_IF_TRUE(condition, retcode, message, ...) \
3638
do \
3739
{ \
@@ -45,8 +47,6 @@ log_function_t default_logging_function = NULL;
4547
#define EXIT_IF_AZ_FAILED(azresult, retcode, message, ...) \
4648
EXIT_IF_TRUE(az_result_failed(azresult), retcode, message, ##__VA_ARGS__ )
4749

48-
#define NUMBER_OF_SECONDS_IN_A_MINUTE 60
49-
5050
/* --- Internal function prototypes --- */
5151
static uint32_t get_current_unix_time();
5252

@@ -160,7 +160,7 @@ int azure_iot_stop(azure_iot_t* azure_iot)
160160
{
161161
if (azure_iot->mqtt_client_handle != NULL)
162162
{
163-
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
163+
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
164164
{
165165
azure_iot->state = azure_iot_state_error;
166166
LogError("Failed deinitializing MQTT client.");
@@ -289,7 +289,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
289289
azure_iot->state = azure_iot_state_subscribing_to_dps;
290290

291291
packet_id = azure_iot->config->mqtt_client_interface.mqtt_client_subscribe(
292-
azure_iot->mqtt_client_handle,
292+
azure_iot->mqtt_client_handle,
293293
AZ_SPAN_FROM_STR(AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC),
294294
mqtt_qos_at_most_once);
295295

@@ -417,6 +417,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
417417
case azure_iot_state_provisioned:
418418
// Disconnect from Provisioning Service first.
419419
if (azure_iot->config->use_device_provisioning &&
420+
azure_iot->mqtt_client_handle != NULL &&
420421
azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
421422
{
422423
azure_iot->state = azure_iot_state_error;
@@ -470,7 +471,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
470471
azure_iot->state = azure_iot_state_subscribing_to_pnp_props;
471472

472473
packet_id = azure_iot->config->mqtt_client_interface.mqtt_client_subscribe(
473-
azure_iot->mqtt_client_handle,
474+
azure_iot->mqtt_client_handle,
474475
AZ_SPAN_FROM_STR(AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_SUBSCRIBE_TOPIC),
475476
mqtt_qos_at_least_once);
476477

@@ -510,16 +511,19 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
510511
{
511512
azure_iot->state = azure_iot_state_error;
512513
LogError("Failed getting current time for checking SAS token expiration.");
514+
return;
513515
}
514516
else if ((azure_iot->sas_token_expiration_time - now) < SAS_TOKEN_REFRESH_THRESHOLD_IN_SECS)
515517
{
516518
azure_iot->state = azure_iot_state_refreshing_sas;
517-
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
519+
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
518520
{
519521
azure_iot->state = azure_iot_state_error;
520522
LogError("Failed de-initializing MQTT client.");
521523
return;
522524
}
525+
526+
azure_iot->mqtt_client_handle = NULL;
523527
}
524528
break;
525529
case azure_iot_state_refreshing_sas:

examples/Azure_IoT_Central_ESP32/Azure_IoT_Central_ESP32.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ static int mqtt_client_init_function(mqtt_client_config_t* mqtt_client_config, m
135135
mqtt_config.disable_auto_reconnect = false;
136136
mqtt_config.event_handle = esp_mqtt_event_handler;
137137
mqtt_config.user_context = NULL;
138-
mqtt_config.buffer_size = 1024;
139138
mqtt_config.cert_pem = (const char*)ca_pem;
140139

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

examples/Azure_IoT_Central_ESP32_AzureIoTKit/AzureIoT.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ int azure_iot_stop(azure_iot_t* azure_iot)
160160
{
161161
if (azure_iot->mqtt_client_handle != NULL)
162162
{
163-
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
163+
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
164164
{
165165
azure_iot->state = azure_iot_state_error;
166166
LogError("Failed deinitializing MQTT client.");
@@ -417,6 +417,7 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
417417
case azure_iot_state_provisioned:
418418
// Disconnect from Provisioning Service first.
419419
if (azure_iot->config->use_device_provisioning &&
420+
azure_iot->mqtt_client_handle != NULL &&
420421
azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
421422
{
422423
azure_iot->state = azure_iot_state_error;
@@ -510,16 +511,19 @@ void azure_iot_do_work(azure_iot_t* azure_iot)
510511
{
511512
azure_iot->state = azure_iot_state_error;
512513
LogError("Failed getting current time for checking SAS token expiration.");
514+
return;
513515
}
514516
else if ((azure_iot->sas_token_expiration_time - now) < SAS_TOKEN_REFRESH_THRESHOLD_IN_SECS)
515517
{
516518
azure_iot->state = azure_iot_state_refreshing_sas;
517-
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(&azure_iot->mqtt_client_handle) != 0)
519+
if (azure_iot->config->mqtt_client_interface.mqtt_client_deinit(azure_iot->mqtt_client_handle) != 0)
518520
{
519521
azure_iot->state = azure_iot_state_error;
520522
LogError("Failed de-initializing MQTT client.");
521523
return;
522524
}
525+
526+
azure_iot->mqtt_client_handle = NULL;
523527
}
524528
break;
525529
case azure_iot_state_refreshing_sas:

examples/Azure_IoT_Central_ESP32_AzureIoTKit/Azure_IoT_Central_ESP32_AzureIoTKit.ino

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,9 @@ static int mqtt_client_init_function(mqtt_client_config_t* mqtt_client_config, m
141141
mqtt_config.disable_auto_reconnect = false;
142142
mqtt_config.event_handle = esp_mqtt_event_handler;
143143
mqtt_config.user_context = NULL;
144-
mqtt_config.buffer_size = 1024;
145144
mqtt_config.cert_pem = (const char*)ca_pem;
146145

147146
LogInfo("MQTT client target uri set to '%s'", mqtt_broker_uri);
148-
149147
mqtt_client = esp_mqtt_client_init(&mqtt_config);
150148

151149
if (mqtt_client == NULL)
@@ -181,7 +179,7 @@ static int mqtt_client_deinit_function(mqtt_client_handle_t mqtt_client_handle)
181179
esp_mqtt_client_handle_t esp_mqtt_client_handle = (esp_mqtt_client_handle_t)mqtt_client_handle;
182180

183181
LogInfo("MQTT client being disconnected.");
184-
182+
185183
if (esp_mqtt_client_stop(esp_mqtt_client_handle) != ESP_OK)
186184
{
187185
LogError("Failed stopping MQTT client.");

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Azure SDK for C
2-
version=1.0.0-beta.2
2+
version=1.0.0-beta.3
33
author=Microsoft Corporation
44
maintainer=Microsoft Corporation <aziotarduino@microsoft.com>
55
sentence=Azure SDK for C library (1.3.0-beta.1) for Arduino.

0 commit comments

Comments
 (0)