|
46 | 46 | #include "nvs.h" |
47 | 47 | #include "nvs_flash.h" |
48 | 48 |
|
| 49 | +#ifdef CONFIG_SSL_USING_WOLFSSL |
| 50 | +#include "lwip/apps/sntp.h" |
| 51 | +#endif |
| 52 | + |
49 | 53 | #include "aws_iot_config.h" |
50 | 54 | #include "aws_iot_log.h" |
51 | 55 | #include "aws_iot_version.h" |
@@ -111,6 +115,39 @@ char HostAddress[255] = AWS_IOT_MQTT_HOST; |
111 | 115 | */ |
112 | 116 | uint32_t port = AWS_IOT_MQTT_PORT; |
113 | 117 |
|
| 118 | +#ifdef CONFIG_SSL_USING_WOLFSSL |
| 119 | +static void get_time() |
| 120 | +{ |
| 121 | + struct timeval now; |
| 122 | + int sntp_retry_cnt = 0; |
| 123 | + int sntp_retry_time = 0; |
| 124 | + |
| 125 | + sntp_setoperatingmode(0); |
| 126 | + sntp_setservername(0, "pool.ntp.org"); |
| 127 | + sntp_init(); |
| 128 | + |
| 129 | + while (1) { |
| 130 | + for (int32_t i = 0; (i < (SNTP_RECV_TIMEOUT / 100)) && now.tv_sec < 1525952900; i++) { |
| 131 | + vTaskDelay(100 / portTICK_RATE_MS); |
| 132 | + gettimeofday(&now, NULL); |
| 133 | + } |
| 134 | + |
| 135 | + if (now.tv_sec < 1525952900) { |
| 136 | + sntp_retry_time = SNTP_RECV_TIMEOUT << sntp_retry_cnt; |
| 137 | + |
| 138 | + if (SNTP_RECV_TIMEOUT << (sntp_retry_cnt + 1) < SNTP_RETRY_TIMEOUT_MAX) { |
| 139 | + sntp_retry_cnt ++; |
| 140 | + } |
| 141 | + |
| 142 | + ESP_LOGE(TAG, "SNTP get time failed, retry after %d ms\n", sntp_retry_time); |
| 143 | + vTaskDelay(sntp_retry_time / portTICK_RATE_MS); |
| 144 | + } else { |
| 145 | + ESP_LOGI(TAG, "SNTP get time success\n"); |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | +#endif |
114 | 151 |
|
115 | 152 | static esp_err_t event_handler(void *ctx, system_event_t *event) |
116 | 153 | { |
@@ -223,6 +260,10 @@ void aws_iot_task(void *param) { |
223 | 260 | xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, |
224 | 261 | false, true, portMAX_DELAY); |
225 | 262 |
|
| 263 | +#ifdef CONFIG_SSL_USING_WOLFSSL |
| 264 | + get_time(); |
| 265 | +#endif |
| 266 | + |
226 | 267 | connectParams.keepAliveIntervalInSec = 10; |
227 | 268 | connectParams.isCleanSession = true; |
228 | 269 | connectParams.MQTTVersion = MQTT_3_1_1; |
|
0 commit comments