feat(aws-iot): Modify thing_shadow example to support wolfssl library

This commit is contained in:
Liu Han
2019-04-18 17:15:30 +08:00
parent fed71efd23
commit 31a6bbab34
2 changed files with 57 additions and 11 deletions

View File

@ -49,6 +49,9 @@
#include "aws_iot_mqtt_client_interface.h" #include "aws_iot_mqtt_client_interface.h"
#include "aws_iot_shadow_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. * 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 * 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" #error "Invalid method for loading certs"
#endif #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) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
switch(event->event_id) { switch(event->event_id) {
@ -251,6 +291,10 @@ void aws_iot_task(void *param) {
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY); false, true, portMAX_DELAY);
#if CONFIG_SSL_USING_WOLFSSL
/* CA date verification need system time */
get_time();
#endif
ESP_LOGI(TAG, "Shadow Init"); ESP_LOGI(TAG, "Shadow Init");
rc = aws_iot_shadow_init(&mqttClient, &sp); rc = aws_iot_shadow_init(&mqttClient, &sp);
if(SUCCESS != rc) { if(SUCCESS != rc) {
@ -264,11 +308,13 @@ void aws_iot_task(void *param) {
scp.mqttClientIdLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID); scp.mqttClientIdLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID);
ESP_LOGI(TAG, "Shadow Connect"); ESP_LOGI(TAG, "Shadow Connect");
rc = aws_iot_shadow_connect(&mqttClient, &scp); do {
if(SUCCESS != rc) { rc = aws_iot_shadow_connect(&mqttClient, &scp);
ESP_LOGE(TAG, "aws_iot_shadow_connect returned error %d, aborting...", rc); if(SUCCESS != rc) {
abort(); 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 * 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; temperature = STARTING_ROOMTEMPERATURE;
// loop and publish a change in temperature // 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); rc = aws_iot_shadow_yield(&mqttClient, 200);
if(NETWORK_ATTEMPTING_RECONNECT == rc || shadowUpdateInProgress) { if(NETWORK_ATTEMPTING_RECONNECT == rc || shadowUpdateInProgress) {
rc = aws_iot_shadow_yield(&mqttClient, 1000); rc = aws_iot_shadow_yield(&mqttClient, 1000);
@ -318,6 +364,8 @@ void aws_iot_task(void *param) {
ESP_LOGI(TAG, "*****************************************************************************************"); ESP_LOGI(TAG, "*****************************************************************************************");
ESP_LOGI(TAG, "Stack remaining for task '%s' is %lu bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); 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); vTaskDelay(1000 / portTICK_RATE_MS);
} }
@ -367,5 +415,5 @@ void app_main()
initialise_wifi(); initialise_wifi();
/* Temporarily pin task to core, due to FPU uncertainty */ /* 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);
} }

View File

@ -1,9 +1,7 @@
# Enable AWS IoT SDK support # Enable AWS IoT SDK support
CONFIG_AWS_IOT_SDK=y CONFIG_AWS_IOT_SDK=y
CONFIG_SSL_USING_MBEDTLS=y CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL=y
CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=8192 CONFIG_SSL_USING_WOLFSSL=y
CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=1024
CONFIG_MBEDTLS_SSL_ALPN=y
# Enable FATFS read only with long filename support # Enable FATFS read only with long filename support
# for loading Cert/CA/etc from filesystem # for loading Cert/CA/etc from filesystem