simple_ota_example.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* OTA example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_system.h"
  10. #include "esp_event.h"
  11. #include "esp_log.h"
  12. #include "esp_ota_ops.h"
  13. #include "esp_http_client.h"
  14. #include "esp_https_ota.h"
  15. #include "protocol_examples_common.h"
  16. #include "string.h"
  17. #ifdef CONFIG_EXAMPLE_USE_CERT_BUNDLE
  18. #include "esp_crt_bundle.h"
  19. #endif
  20. #include "nvs.h"
  21. #include "nvs_flash.h"
  22. #include "protocol_examples_common.h"
  23. #include <sys/socket.h>
  24. #if CONFIG_EXAMPLE_CONNECT_WIFI
  25. #include "esp_wifi.h"
  26. #endif
  27. #define HASH_LEN 32
  28. #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF
  29. /* The interface name value can refer to if_desc in esp_netif_defaults.h */
  30. #if CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_ETH
  31. static const char *bind_interface_name = EXAMPLE_NETIF_DESC_ETH;
  32. #elif CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_STA
  33. static const char *bind_interface_name = EXAMPLE_NETIF_DESC_STA;
  34. #endif
  35. #endif
  36. static const char *TAG = "simple_ota_example";
  37. extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start");
  38. extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end");
  39. #define OTA_URL_SIZE 256
  40. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  41. {
  42. switch (evt->event_id) {
  43. case HTTP_EVENT_ERROR:
  44. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  45. break;
  46. case HTTP_EVENT_ON_CONNECTED:
  47. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  48. break;
  49. case HTTP_EVENT_HEADER_SENT:
  50. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  51. break;
  52. case HTTP_EVENT_ON_HEADER:
  53. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  54. break;
  55. case HTTP_EVENT_ON_DATA:
  56. ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  57. break;
  58. case HTTP_EVENT_ON_FINISH:
  59. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  60. break;
  61. case HTTP_EVENT_DISCONNECTED:
  62. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  63. break;
  64. case HTTP_EVENT_REDIRECT:
  65. ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
  66. break;
  67. }
  68. return ESP_OK;
  69. }
  70. void simple_ota_example_task(void *pvParameter)
  71. {
  72. ESP_LOGI(TAG, "Starting OTA example task");
  73. #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF
  74. esp_netif_t *netif = get_example_netif_from_desc(bind_interface_name);
  75. if (netif == NULL) {
  76. ESP_LOGE(TAG, "Can't find netif from interface description");
  77. abort();
  78. }
  79. struct ifreq ifr;
  80. esp_netif_get_netif_impl_name(netif, ifr.ifr_name);
  81. ESP_LOGI(TAG, "Bind interface name is %s", ifr.ifr_name);
  82. #endif
  83. esp_http_client_config_t config = {
  84. .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL,
  85. #ifdef CONFIG_EXAMPLE_USE_CERT_BUNDLE
  86. .crt_bundle_attach = esp_crt_bundle_attach,
  87. #else
  88. .cert_pem = (char *)server_cert_pem_start,
  89. #endif /* CONFIG_EXAMPLE_USE_CERT_BUNDLE */
  90. .event_handler = _http_event_handler,
  91. .keep_alive_enable = true,
  92. #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF
  93. .if_name = &ifr,
  94. #endif
  95. };
  96. #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN
  97. char url_buf[OTA_URL_SIZE];
  98. if (strcmp(config.url, "FROM_STDIN") == 0) {
  99. example_configure_stdin_stdout();
  100. fgets(url_buf, OTA_URL_SIZE, stdin);
  101. int len = strlen(url_buf);
  102. url_buf[len - 1] = '\0';
  103. config.url = url_buf;
  104. } else {
  105. ESP_LOGE(TAG, "Configuration mismatch: wrong firmware upgrade image url");
  106. abort();
  107. }
  108. #endif
  109. #ifdef CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK
  110. config.skip_cert_common_name_check = true;
  111. #endif
  112. esp_https_ota_config_t ota_config = {
  113. .http_config = &config,
  114. };
  115. ESP_LOGI(TAG, "Attempting to download update from %s", config.url);
  116. esp_err_t ret = esp_https_ota(&ota_config);
  117. if (ret == ESP_OK) {
  118. ESP_LOGI(TAG, "OTA Succeed, Rebooting...");
  119. esp_restart();
  120. } else {
  121. ESP_LOGE(TAG, "Firmware upgrade failed");
  122. }
  123. while (1) {
  124. vTaskDelay(1000 / portTICK_PERIOD_MS);
  125. }
  126. }
  127. static void print_sha256(const uint8_t *image_hash, const char *label)
  128. {
  129. char hash_print[HASH_LEN * 2 + 1];
  130. hash_print[HASH_LEN * 2] = 0;
  131. for (int i = 0; i < HASH_LEN; ++i) {
  132. sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
  133. }
  134. ESP_LOGI(TAG, "%s %s", label, hash_print);
  135. }
  136. static void get_sha256_of_partitions(void)
  137. {
  138. uint8_t sha_256[HASH_LEN] = { 0 };
  139. esp_partition_t partition;
  140. // get sha256 digest for bootloader
  141. partition.address = ESP_BOOTLOADER_OFFSET;
  142. partition.size = ESP_PARTITION_TABLE_OFFSET;
  143. partition.type = ESP_PARTITION_TYPE_APP;
  144. esp_partition_get_sha256(&partition, sha_256);
  145. print_sha256(sha_256, "SHA-256 for bootloader: ");
  146. // get sha256 digest for running partition
  147. esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
  148. print_sha256(sha_256, "SHA-256 for current firmware: ");
  149. }
  150. void app_main(void)
  151. {
  152. ESP_LOGI(TAG, "OTA example app_main start");
  153. // Initialize NVS.
  154. esp_err_t err = nvs_flash_init();
  155. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  156. // 1.OTA app partition table has a smaller NVS partition size than the non-OTA
  157. // partition table. This size mismatch may cause NVS initialization to fail.
  158. // 2.NVS partition contains data in new format and cannot be recognized by this version of code.
  159. // If this happens, we erase NVS partition and initialize NVS again.
  160. ESP_ERROR_CHECK(nvs_flash_erase());
  161. err = nvs_flash_init();
  162. }
  163. ESP_ERROR_CHECK(err);
  164. get_sha256_of_partitions();
  165. ESP_ERROR_CHECK(esp_netif_init());
  166. ESP_ERROR_CHECK(esp_event_loop_create_default());
  167. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  168. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  169. * examples/protocols/README.md for more information about this function.
  170. */
  171. ESP_ERROR_CHECK(example_connect());
  172. #if CONFIG_EXAMPLE_CONNECT_WIFI
  173. /* Ensure to disable any WiFi power save mode, this allows best throughput
  174. * and hence timings for overall OTA operation.
  175. */
  176. esp_wifi_set_ps(WIFI_PS_NONE);
  177. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  178. xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
  179. }