This is my code.
Code: Select all
void ota_clear(void)
{
ESP_LOGI(TAG_OTA, "-----OTA clear-----");
close(ota_socket_id);
binary_file_length = 0;
xEventGroupClearBits(wifi_event_group, OTA_BIT);
}
static void ota_timer_callback(TimerHandle_t timer)
{
ESP_LOGI(TAG_OTA, "-----ota failed because bad network! Prepare to restart system!-----");
ota_reboot_system();
}
static void ota_start_timer_timeout(void)
{
int count = 0;
if(ota_timer != NULL)
{
xTimerReset(ota_timer,0);
}
else
{
ota_timer = xTimerCreate("ota_timer", 60000 / portTICK_PERIOD_MS, pdFALSE,(void *)&count, ota_timer_callback);
xTimerStart(ota_timer, 0);
}
}
static void ota_task(void *pvParameter)
{
esp_err_t err;
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
esp_ota_handle_t update_handle = 0 ;
const esp_partition_t *update_partition = NULL;
char ota_http_request[64] = {0};
/*an ota data write buffer ready to write to the flash*/
//char ota_write_data[OTA_BUFFSIZE + 1] = { 0 };
/*an packet receive buffer*/
char ota_text[OTA_TEXT_BUFFSIZE + 1] = { 0 };
uint8_t ota_recv_end = 0;
ESP_LOGI(TAG_OTA, "-----Starting OTA...-----");
const esp_partition_t *configured = esp_ota_get_boot_partition();
const esp_partition_t *running = esp_ota_get_running_partition();
assert(configured == running); /* fresh from reset, should be running from configured boot partition */
ESP_LOGI(TAG_OTA, "-----Running partition type %d subtype %d (offset 0x%08x)-----",
configured->type, configured->subtype, configured->address);
/* Wait for the callback to set the CONNECTED_BIT in the
event group.
*/
OTA_RESTART:
xEventGroupWaitBits(wifi_event_group, OTA_BIT,
false, true, portMAX_DELAY);
ESP_LOGI(TAG_OTA, "-----Connect to Wifi ! Start to Connect to Server....-----");
/*connect to http server*/
if (connect_to_ota_http_server()) {
ESP_LOGI(TAG_OTA, "-----OTA Connected to http server-----");
} else {
ESP_LOGE(TAG_OTA, "-----OTA Connect to http server failed!-----");
//task_fatal_error();
ota_clear();//---OK!!!it was verified by jiawei. 20170622
goto OTA_RESTART;
}
int res = -1;
/*send GET request to http server*/
sprintf(ota_http_request, "GET %s HTTP/1.1\r\nHost: %s:%d \r\n\r\n", OTA_FILE_PATH, OTA_HTTP_IP, OTA_HTTP_PORT);
res = send(ota_socket_id, ota_http_request, strlen(ota_http_request), 0);
if (res == -1) {
ESP_LOGE(TAG_OTA, "-----OTA Send GET request to server failed-----");
//task_fatal_error();
ota_clear();
goto OTA_RESTART;
} else {
ESP_LOGI(TAG_OTA, "-----OTA Send GET request to server succeeded-----");
}
update_partition = esp_ota_get_next_update_partition(NULL);
ESP_LOGI(TAG_OTA, "-----Writing to partition subtype %d at offset 0x%x-----",
update_partition->subtype, update_partition->address);
assert(update_partition != NULL);
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG_OTA, "-----esp_ota_begin failed, error=%d-----", err);
//task_fatal_error();
ota_reboot_system();
//goto OTA_RESTART;
}
ESP_LOGI(TAG_OTA, "-----esp_ota_begin succeeded-----");
bool resp_body_start = false, flag = true;
/*deal with all receive packet*/
while (flag) {
ota_start_timer_timeout();
memset(ota_text, 0, OTA_TEXT_BUFFSIZE);
memset(ota_write_data, 0, OTA_BUFFSIZE);
int buff_len = recv(ota_socket_id, ota_text, OTA_TEXT_BUFFSIZE, 0);
printf("OTA_NOW:buff_len = %d,resp_body_start = %d",buff_len,resp_body_start);
if (buff_len < 0) { /*receive error*/
ESP_LOGE(TAG_OTA, "-----Error: receive data error! errno=%d-----", errno);
//task_fatal_error();
ota_recv_end = 0;
ota_reboot_system();
//goto OTA_RESTART;
} else if (buff_len > 0 && !resp_body_start) { /*deal with response header*/
memcpy(ota_write_data, ota_text, buff_len);
resp_body_start = read_past_http_header(ota_text, buff_len, update_handle);
} else if (buff_len > 0 && resp_body_start) { /*deal with response body*/
memcpy(ota_write_data, ota_text, buff_len);
err = esp_ota_write( update_handle, (const void *)ota_write_data, buff_len);
if (err != ESP_OK) {
ESP_LOGE(TAG_OTA, "-----Error: esp_ota_write failed! err=0x%x-----", err);
ota_recv_end = 0;
ota_reboot_system();
ota again when ota written half,ota can succeed!!!
//goto OTA_RESTART;
}
binary_file_length += buff_len;
ESP_LOGI(TAG_OTA, "-----Have written image length %d-----", binary_file_length);
} else if (buff_len == 0) { /*packet over*/
flag = false;
ESP_LOGI(TAG_OTA, "-----Connection closed, all packets received-----");
ota_recv_end = 1;
close(ota_socket_id);
} else {
ESP_LOGE(TAG_OTA, "-----Unexpected recv result-----");
}
}
ESP_LOGI(TAG_OTA, "-----Total Write binary data length : %d-----", binary_file_length);
if (esp_ota_end(update_handle) != ESP_OK) {
ota_recv_end = 0;
ESP_LOGE(TAG_OTA, "-----esp_ota_end failed!-----");
//task_fatal_error();
}
err = esp_ota_set_boot_partition(update_partition);
if (err != ESP_OK) {
ota_recv_end = 0;
ESP_LOGE(TAG_OTA, "-----esp_ota_set_boot_partition failed! err=0x%x-----", err);
//task_fatal_error();
}
#ifndef __FACTORY_APP_BIN__
if(ota_recv_end)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
post_uart_status_to_queue(UART_ESP32_OTA_OVER);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
#endif
ota_reboot_system();
return ;