四博智联产品售后

 找回密码
 立即注册
搜索
查看: 4911|回复: 0

关于ota的例子bug修复

[复制链接]

17

主题

19

帖子

129

积分

超级版主

Rank: 8Rank: 8

积分
129
发表于 2017-5-27 00:20:40 | 显示全部楼层 |阅读模式

esp-idf的ota例子存在bug,当你编译下载运行后会发现输出Have written image length xxxxx多行后程序就死掉了,过了好久就提示server connect timeout
修改这一问题只需在while(flag)循环中加上延迟大概20毫秒就可以了,修改后代码如下:

  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 <string.h>
  8. #include <sys/socket.h>
  9. #include <netdb.h>

  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/event_groups.h"

  13. #include "esp_system.h"
  14. #include "esp_wifi.h"
  15. #include "esp_event_loop.h"
  16. #include "esp_log.h"
  17. #include "esp_ota_ops.h"
  18. #include "esp_partition.h"

  19. #include "nvs.h"
  20. #include "nvs_flash.h"

  21. #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
  22. #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
  23. #define EXAMPLE_SERVER_IP   CONFIG_SERVER_IP
  24. #define EXAMPLE_SERVER_PORT CONFIG_SERVER_PORT
  25. #define EXAMPLE_FILENAME CONFIG_EXAMPLE_FILENAME
  26. #define BUFFSIZE 1024
  27. #define TEXT_BUFFSIZE 1024

  28. static const char *TAG = "ota";
  29. /*an ota data write buffer ready to write to the flash*/
  30. static char ota_write_data[BUFFSIZE + 1] = { 0 };
  31. /*an packet receive buffer*/
  32. static char text[BUFFSIZE + 1] = { 0 };
  33. /* an image total length*/
  34. static int binary_file_length = 0;
  35. /*socket id*/
  36. static int socket_id = -1;
  37. static char http_request[64] = {0};

  38. /* FreeRTOS event group to signal when we are connected & ready to make a request */
  39. static EventGroupHandle_t wifi_event_group;

  40. /* The event group allows multiple bits for each event,
  41.    but we only care about one event - are we connected
  42.    to the AP with an IP? */
  43. const int CONNECTED_BIT = BIT0;

  44. static esp_err_t event_handler(void *ctx, system_event_t *event)
  45. {
  46.     switch (event->event_id) {
  47.     case SYSTEM_EVENT_STA_START:
  48.         esp_wifi_connect();
  49.         break;
  50.     case SYSTEM_EVENT_STA_GOT_IP:
  51.         xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  52.         break;
  53.     case SYSTEM_EVENT_STA_DISCONNECTED:
  54.         /* This is a workaround as ESP32 WiFi libs don't currently
  55.            auto-reassociate. */
  56.         esp_wifi_connect();
  57.         xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  58.         break;
  59.     default:
  60.         break;
  61.     }
  62.     return ESP_OK;
  63. }

  64. static void initialise_wifi(void)
  65. {
  66.     tcpip_adapter_init();
  67.     wifi_event_group = xEventGroupCreate();
  68.     ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
  69.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  70.     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  71.     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  72.     wifi_config_t wifi_config = {
  73.         .sta = {
  74.             .ssid = EXAMPLE_WIFI_SSID,
  75.             .password = EXAMPLE_WIFI_PASS,
  76.         },
  77.     };
  78.     ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
  79.     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  80.     ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  81.     ESP_ERROR_CHECK( esp_wifi_start() );
  82. }

  83. /*read buffer by byte still delim ,return read bytes counts*/
  84. static int read_until(char *buffer, char delim, int len)
  85. {
  86. //  /*TODO: delim check,buffer check,further: do an buffer length limited*/
  87.     int i = 0;
  88.     while (buffer[i] != delim && i < len) {
  89.         ++i;
  90.     }
  91.     return i + 1;
  92. }

  93. /* resolve a packet from http socket
  94. * return true if packet including \r\n\r\n that means http packet header finished,start to receive packet body
  95. * otherwise return false
  96. * */
  97. static bool read_past_http_header(char text[], int total_len, esp_ota_handle_t update_handle)
  98. {
  99.     /* i means current position */
  100.     int i = 0, i_read_len = 0;
  101.     while (text[i] != 0 && i < total_len) {
  102.         i_read_len = read_until(&text[i], '\n', total_len);
  103.         // if we resolve \r\n line,we think packet header is finished
  104.         if (i_read_len == 2) {
  105.             int i_write_len = total_len - (i + 2);
  106.             memset(ota_write_data, 0, BUFFSIZE);
  107.             /*copy first http packet body to write buffer*/
  108.             memcpy(ota_write_data, &(text[i + 2]), i_write_len);

  109.             esp_err_t err = esp_ota_write( update_handle, (const void *)ota_write_data, i_write_len);
  110.             if (err != ESP_OK) {
  111.                 ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%x", err);
  112.                 return false;
  113.             } else {
  114.                 ESP_LOGI(TAG, "esp_ota_write header OK");
  115.                 binary_file_length += i_write_len;
  116.             }
  117.             return true;
  118.         }
  119.         i += i_read_len;
  120.     }
  121.     return false;
  122. }

  123. static bool connect_to_http_server()
  124. {
  125.     ESP_LOGI(TAG, "Server IP: %s Server Port:%s", EXAMPLE_SERVER_IP, EXAMPLE_SERVER_PORT);
  126.     sprintf(http_request, "GET %s HTTP/1.1\r\nHost: %s:%s \r\n\r\n", EXAMPLE_FILENAME, EXAMPLE_SERVER_IP, EXAMPLE_SERVER_PORT);

  127.     int  http_connect_flag = -1;
  128.     struct sockaddr_in sock_info;

  129.     socket_id = socket(AF_INET, SOCK_STREAM, 0);
  130.     if (socket_id == -1) {
  131.         ESP_LOGE(TAG, "Create socket failed!");
  132.         return false;
  133.     }

  134.     // set connect info
  135.     memset(&sock_info, 0, sizeof(struct sockaddr_in));
  136.     sock_info.sin_family = AF_INET;
  137.     sock_info.sin_addr.s_addr = inet_addr(EXAMPLE_SERVER_IP);
  138.     sock_info.sin_port = htons(atoi(EXAMPLE_SERVER_PORT));

  139.     // connect to http server
  140.     http_connect_flag = connect(socket_id, (struct sockaddr *)&sock_info, sizeof(sock_info));
  141.     if (http_connect_flag == -1) {
  142.         ESP_LOGE(TAG, "Connect to server failed! errno=%d", errno);
  143.         close(socket_id);
  144.         return false;
  145.     } else {
  146.         ESP_LOGI(TAG, "Connected to server");
  147.         return true;
  148.     }
  149.     return false;
  150. }

  151. static void __attribute__((noreturn)) task_fatal_error()
  152. {
  153.     ESP_LOGE(TAG, "Exiting task due to fatal error...");
  154.     close(socket_id);
  155.     (void)vTaskDelete(NULL);

  156.     while (1) {
  157.         ;
  158.     }
  159. }

  160. static void ota_example_task(void *pvParameter)
  161. {
  162.     esp_err_t err;
  163.     /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
  164.     esp_ota_handle_t update_handle = 0 ;
  165.     const esp_partition_t *update_partition = NULL;

  166.     ESP_LOGI(TAG, "Starting OTA example...");

  167.     const esp_partition_t *configured = esp_ota_get_boot_partition();
  168.     const esp_partition_t *running = esp_ota_get_running_partition();

  169.     assert(configured == running); /* fresh from reset, should be running from configured boot partition */
  170.     ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
  171.              configured->type, configured->subtype, configured->address);

  172.     /* Wait for the callback to set the CONNECTED_BIT in the
  173.        event group.
  174.     */
  175.     xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  176.                         false, true, portMAX_DELAY);
  177.     ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server....");

  178.     /*connect to http server*/
  179.     if (connect_to_http_server()) {
  180.         ESP_LOGI(TAG, "Connected to http server");
  181.     } else {
  182.         ESP_LOGE(TAG, "Connect to http server failed!");
  183.         task_fatal_error();
  184.     }

  185.     int res = -1;
  186.     /*send GET request to http server*/
  187.     res = send(socket_id, http_request, strlen(http_request), 0);
  188.     if (res == -1) {
  189.         ESP_LOGE(TAG, "Send GET request to server failed");
  190.         task_fatal_error();
  191.     } else {
  192.         ESP_LOGI(TAG, "Send GET request to server succeeded");
  193.     }

  194.     update_partition = esp_ota_get_next_update_partition(NULL);
  195.     ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
  196.              update_partition->subtype, update_partition->address);
  197.     assert(update_partition != NULL);

  198.     err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
  199.     if (err != ESP_OK) {
  200.         ESP_LOGE(TAG, "esp_ota_begin failed, error=%d", err);
  201.         task_fatal_error();
  202.     }
  203.     ESP_LOGI(TAG, "esp_ota_begin succeeded");

  204.     bool resp_body_start = false, flag = true;
  205.     /*deal with all receive packet*/
  206.     while (flag) {
  207.         memset(text, 0, TEXT_BUFFSIZE);
  208.         memset(ota_write_data, 0, BUFFSIZE);
  209.         int buff_len = recv(socket_id, text, TEXT_BUFFSIZE, 0);
  210.         if (buff_len < 0) { /*receive error*/
  211.             ESP_LOGE(TAG, "Error: receive data error! errno=%d", errno);
  212.             task_fatal_error();
  213.         } else if (buff_len > 0 && !resp_body_start) { /*deal with response header*/
  214.             memcpy(ota_write_data, text, buff_len);
  215.             resp_body_start = read_past_http_header(text, buff_len, update_handle);
  216.         } else if (buff_len > 0 && resp_body_start) { /*deal with response body*/
  217.             memcpy(ota_write_data, text, buff_len);
  218.             err = esp_ota_write( update_handle, (const void *)ota_write_data, buff_len);
  219.             if (err != ESP_OK) {
  220.                 ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%x", err);
  221.                 task_fatal_error();
  222.             }
  223.             binary_file_length += buff_len;
  224.             ESP_LOGI(TAG, "Have written image length %d", binary_file_length);
  225.         } else if (buff_len == 0) {  /*packet over*/
  226.             flag = false;
  227.             ESP_LOGI(TAG, "Connection closed, all packets received");
  228.             close(socket_id);
  229.         } else {
  230.             ESP_LOGE(TAG, "Unexpected recv result");
  231.         }
  232.         vTaskDelay(20 / portTICK_RATE_MS);
  233.     }

  234.     ESP_LOGI(TAG, "Total Write binary data length : %d", binary_file_length);

  235.     if (esp_ota_end(update_handle) != ESP_OK) {
  236.         ESP_LOGE(TAG, "esp_ota_end failed!");
  237.         task_fatal_error();
  238.     }
  239.     err = esp_ota_set_boot_partition(update_partition);
  240.     if (err != ESP_OK) {
  241.         ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%x", err);
  242.         task_fatal_error();
  243.     }
  244.     ESP_LOGI(TAG, "Prepare to restart system!");
  245.     esp_restart();
  246.     return ;
  247. }

  248. void app_main()
  249. {
  250.     // Initialize NVS.
  251.     esp_err_t err = nvs_flash_init();
  252.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  253.         // OTA app partition table has a smaller NVS partition size than the non-OTA
  254.         // partition table. This size mismatch may cause NVS initialization to fail.
  255.         // If this happens, we erase NVS partition and initialize NVS again.
  256.         const esp_partition_t* nvs_partition = esp_partition_find_first(
  257.                 ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
  258.         assert(nvs_partition && "partition table must have an NVS partition");
  259.         ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
  260.         err = nvs_flash_init();
  261.     }
  262.     ESP_ERROR_CHECK( err );

  263.     initialise_wifi();
  264.     xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
  265. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|四博智联 Inc. ( 粤ICP备15034758号-1

GMT+8, 2019-6-19 17:05 , Processed in 0.054956 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表