From 31a6bbab34bd5b7fababd7a362e6616bfcb2db75 Mon Sep 17 00:00:00 2001 From: Liu Han Date: Thu, 18 Apr 2019 17:15:30 +0800 Subject: [PATCH] feat(aws-iot): Modify thing_shadow example to support wolfssl library --- .../thing_shadow/main/thing_shadow_sample.c | 62 ++++++++++++++++--- .../aws_iot/thing_shadow/sdkconfig.defaults | 6 +- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/examples/protocols/aws_iot/thing_shadow/main/thing_shadow_sample.c b/examples/protocols/aws_iot/thing_shadow/main/thing_shadow_sample.c index d544bec9..70d82701 100644 --- a/examples/protocols/aws_iot/thing_shadow/main/thing_shadow_sample.c +++ b/examples/protocols/aws_iot/thing_shadow/main/thing_shadow_sample.c @@ -49,6 +49,9 @@ #include "aws_iot_mqtt_client_interface.h" #include "aws_iot_shadow_interface.h" +#if CONFIG_SSL_USING_WOLFSSL +#include "lwip/apps/sntp.h" +#endif /*! * The goal of this sample application is to demonstrate the capabilities of shadow. * This device(say Connected Window) will open the window of a room based on temperature @@ -128,6 +131,43 @@ static const char * ROOT_CA_PATH = CONFIG_EXAMPLE_ROOT_CA_PATH; #error "Invalid method for loading certs" #endif +#if CONFIG_SSL_USING_WOLFSSL +static void get_time() +{ + struct timeval now; + int sntp_retry_cnt = 0; + int sntp_retry_time = 0; + + sntp_setoperatingmode(0); + sntp_setservername(0, "pool.ntp.org"); + + sntp_setservername(1, "ntp1.aliyun.com"); + sntp_setservername(2, "time.aisa.apple.com"); + sntp_init(); + + while (1) { + for (int32_t i = 0; (i < (SNTP_RECV_TIMEOUT / 100)) && now.tv_sec < 1525952900; i++) { + vTaskDelay(100 / portTICK_RATE_MS); + gettimeofday(&now, NULL); + } + + if (now.tv_sec < 1525952900) { + sntp_retry_time = SNTP_RECV_TIMEOUT << sntp_retry_cnt; + + if (SNTP_RECV_TIMEOUT << (sntp_retry_cnt + 1) < SNTP_RETRY_TIMEOUT_MAX) { + sntp_retry_cnt ++; + } + + ESP_LOGI(TAG,"SNTP get time failed, retry after %d ms\n", sntp_retry_time); + vTaskDelay(sntp_retry_time / portTICK_RATE_MS); + } else { + ESP_LOGI(TAG,"SNTP get time success\n"); + break; + } + } +} +#endif + static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { @@ -251,6 +291,10 @@ void aws_iot_task(void *param) { xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +#if CONFIG_SSL_USING_WOLFSSL + /* CA date verification need system time */ + get_time(); +#endif ESP_LOGI(TAG, "Shadow Init"); rc = aws_iot_shadow_init(&mqttClient, &sp); if(SUCCESS != rc) { @@ -264,11 +308,13 @@ void aws_iot_task(void *param) { scp.mqttClientIdLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID); ESP_LOGI(TAG, "Shadow Connect"); - rc = aws_iot_shadow_connect(&mqttClient, &scp); - if(SUCCESS != rc) { - ESP_LOGE(TAG, "aws_iot_shadow_connect returned error %d, aborting...", rc); - abort(); - } + do { + rc = aws_iot_shadow_connect(&mqttClient, &scp); + if(SUCCESS != rc) { + ESP_LOGE(TAG, "aws_iot_shadow_connect returned error %d, aborting...", rc); + vTaskDelay(1000 / portTICK_RATE_MS); + } + } while(SUCCESS != rc); /* * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h @@ -289,7 +335,7 @@ void aws_iot_task(void *param) { temperature = STARTING_ROOMTEMPERATURE; // loop and publish a change in temperature - while(1) { + while(NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc) { rc = aws_iot_shadow_yield(&mqttClient, 200); if(NETWORK_ATTEMPTING_RECONNECT == rc || shadowUpdateInProgress) { rc = aws_iot_shadow_yield(&mqttClient, 1000); @@ -318,6 +364,8 @@ void aws_iot_task(void *param) { ESP_LOGI(TAG, "*****************************************************************************************"); ESP_LOGI(TAG, "Stack remaining for task '%s' is %lu bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); + ESP_LOGI(TAG, "Free memory for application is %d bytes", esp_get_free_heap_size()); + vTaskDelay(1000 / portTICK_RATE_MS); } @@ -367,5 +415,5 @@ void app_main() initialise_wifi(); /* Temporarily pin task to core, due to FPU uncertainty */ - xTaskCreate(&aws_iot_task, "aws_iot_task", 9216, NULL, 5, NULL); + xTaskCreate(&aws_iot_task, "aws_iot_task", 4096, NULL, 5, NULL); } diff --git a/examples/protocols/aws_iot/thing_shadow/sdkconfig.defaults b/examples/protocols/aws_iot/thing_shadow/sdkconfig.defaults index c3848c39..3167a008 100644 --- a/examples/protocols/aws_iot/thing_shadow/sdkconfig.defaults +++ b/examples/protocols/aws_iot/thing_shadow/sdkconfig.defaults @@ -1,9 +1,7 @@ # Enable AWS IoT SDK support CONFIG_AWS_IOT_SDK=y -CONFIG_SSL_USING_MBEDTLS=y -CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=8192 -CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=1024 -CONFIG_MBEDTLS_SSL_ALPN=y +CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL=y +CONFIG_SSL_USING_WOLFSSL=y # Enable FATFS read only with long filename support # for loading Cert/CA/etc from filesystem