2018-07-12 12:15:44 +00:00
|
|
|
/* OTA example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
2019-05-22 09:56:10 +00:00
|
|
|
#include "esp_event.h"
|
2018-07-12 12:15:44 +00:00
|
|
|
#include "esp_event_loop.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_ota_ops.h"
|
|
|
|
#include "esp_http_client.h"
|
|
|
|
#include "esp_https_ota.h"
|
2019-06-10 09:13:24 +00:00
|
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
#include "nvs.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-05-22 09:56:10 +00:00
|
|
|
#include "protocol_examples_common.h"
|
2018-07-12 12:15:44 +00:00
|
|
|
|
|
|
|
static const char *TAG = "simple_ota_example";
|
|
|
|
extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start");
|
|
|
|
extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end");
|
|
|
|
|
2019-06-10 09:13:24 +00:00
|
|
|
#define OTA_URL_SIZE 256
|
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
|
{
|
2019-05-22 09:56:10 +00:00
|
|
|
switch (evt->event_id) {
|
|
|
|
case HTTP_EVENT_ERROR:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
|
|
|
break;
|
|
|
|
case HTTP_EVENT_ON_CONNECTED:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
|
2018-07-12 12:15:44 +00:00
|
|
|
break;
|
2019-05-22 09:56:10 +00:00
|
|
|
case HTTP_EVENT_HEADER_SENT:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
|
2018-07-12 12:15:44 +00:00
|
|
|
break;
|
2019-05-22 09:56:10 +00:00
|
|
|
case HTTP_EVENT_ON_HEADER:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
|
2018-07-12 12:15:44 +00:00
|
|
|
break;
|
2019-05-22 09:56:10 +00:00
|
|
|
case HTTP_EVENT_ON_DATA:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
|
|
|
break;
|
|
|
|
case HTTP_EVENT_ON_FINISH:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
|
|
|
break;
|
|
|
|
case HTTP_EVENT_DISCONNECTED:
|
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
|
2018-07-12 12:15:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:56:10 +00:00
|
|
|
void simple_ota_example_task(void *pvParameter)
|
2018-07-12 12:15:44 +00:00
|
|
|
{
|
2019-01-30 08:54:20 +00:00
|
|
|
ESP_LOGI(TAG, "Starting OTA example");
|
2018-07-12 12:15:44 +00:00
|
|
|
|
|
|
|
esp_http_client_config_t config = {
|
2019-06-20 10:18:20 +00:00
|
|
|
.url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL,
|
2018-07-12 12:15:44 +00:00
|
|
|
.cert_pem = (char *)server_cert_pem_start,
|
|
|
|
.event_handler = _http_event_handler,
|
|
|
|
};
|
2019-06-10 09:13:24 +00:00
|
|
|
|
2019-06-20 10:18:20 +00:00
|
|
|
#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN
|
2019-06-10 09:13:24 +00:00
|
|
|
char url_buf[OTA_URL_SIZE];
|
|
|
|
if (strcmp(config.url, "FROM_STDIN") == 0) {
|
|
|
|
example_configure_stdin_stdout();
|
|
|
|
fgets(url_buf, OTA_URL_SIZE, stdin);
|
|
|
|
int len = strlen(url_buf);
|
|
|
|
url_buf[len - 1] = '\0';
|
|
|
|
config.url = url_buf;
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Configuration mismatch: wrong firmware upgrade image url");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-20 10:18:20 +00:00
|
|
|
#ifdef CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK
|
2019-06-12 08:45:06 +00:00
|
|
|
config.skip_cert_common_name_check = true;
|
|
|
|
#endif
|
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
esp_err_t ret = esp_https_ota(&config);
|
|
|
|
if (ret == ESP_OK) {
|
|
|
|
esp_restart();
|
|
|
|
} else {
|
2019-01-30 08:54:20 +00:00
|
|
|
ESP_LOGE(TAG, "Firmware upgrade failed");
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
// Initialize NVS.
|
|
|
|
esp_err_t err = nvs_flash_init();
|
2018-12-18 03:50:33 +00:00
|
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
|
|
// 1.OTA app partition table has a smaller NVS partition size than the non-OTA
|
2018-07-12 12:15:44 +00:00
|
|
|
// partition table. This size mismatch may cause NVS initialization to fail.
|
2018-12-18 03:50:33 +00:00
|
|
|
// 2.NVS partition contains data in new format and cannot be recognized by this version of code.
|
2018-07-12 12:15:44 +00:00
|
|
|
// If this happens, we erase NVS partition and initialize NVS again.
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
err = nvs_flash_init();
|
|
|
|
}
|
2019-05-22 09:56:10 +00:00
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
|
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2018-07-12 12:15:44 +00:00
|
|
|
|
|
|
|
xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
|
|
|
|
}
|