buffering fix on http OTA - release

This commit is contained in:
Sebastien
2020-02-29 09:41:13 -05:00
parent e2d77684e3
commit 4944210ef1

View File

@@ -68,7 +68,6 @@ static struct {
ota_type_t ota_type;
char * ota_write_data;
char * bin_data;
char * http_client_write_buf;
bool bOTAStarted;
size_t buffer_size;
uint8_t lastpct;
@@ -83,7 +82,7 @@ static struct {
} ota_status;
struct timeval tv;
static esp_http_client_config_t ota_config;
static esp_http_client_config_t http_client_config;
void _printMemStats(){
ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
@@ -283,7 +282,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
}
esp_err_t init_config(ota_thread_parms_t * p_ota_thread_parms){
memset(&ota_config, 0x00, sizeof(ota_config));
memset(&http_client_config, 0x00, sizeof(http_client_config));
sendMessaging(MESSAGING_INFO,"Initializing...");
loc_displayer_progressbar(0);
ota_status.ota_type= OTA_TYPE_INVALID;
@@ -307,13 +306,14 @@ esp_err_t init_config(ota_thread_parms_t * p_ota_thread_parms){
}
switch (ota_status.ota_type) {
case OTA_TYPE_HTTP:
ota_config.cert_pem =get_certificate();
ota_config.event_handler = _http_event_handler;
ota_config.disable_auto_redirect=true;
ota_config.skip_cert_common_name_check = false;
ota_config.url = strdup(p_ota_thread_parms->url);
ota_config.max_redirection_count = 3;
ota_config.buffer_size = ota_status.buffer_size;
http_client_config.cert_pem =get_certificate();
http_client_config.event_handler = _http_event_handler;
http_client_config.disable_auto_redirect=true;
http_client_config.skip_cert_common_name_check = false;
http_client_config.url = strdup(p_ota_thread_parms->url);
http_client_config.max_redirection_count = 3;
// buffer size below is for http read chunks
http_client_config.buffer_size = 1024 ;
break;
case OTA_TYPE_BUFFER:
ota_status.bin_data = p_ota_thread_parms->bin;
@@ -430,15 +430,16 @@ static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client
ESP_LOGD(TAG, "Redirection done, checking if we need to read the data. ");
if (process_again(status_code)) {
//ESP_LOGD(TAG, "We have to read some more data. Allocating buffer size %u",ota_config.buffer_size+1);
char * local_buff = heap_caps_malloc(ota_status.buffer_size+1, (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
//char * local_buff = heap_caps_malloc(ota_status.buffer_size+1, (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
// if(local_buff==NULL){
// ESP_LOGE(TAG,"Failed to allocate internal memory buffer for http processing");
// return ESP_ERR_NO_MEM;
// }
if(local_buff==NULL){
ESP_LOGE(TAG,"Failed to allocate internal memory buffer for http processing");
return ESP_ERR_NO_MEM;
}
while (1) {
ESP_LOGD(TAG, "Buffer successfully allocated. Reading data chunk. ");
int data_read = esp_http_client_read(http_client, local_buff, ota_status.buffer_size);
ESP_LOGD(TAG, "Reading data chunk. ");
int data_read = esp_http_client_read(http_client, ota_status.ota_write_data, ota_status.buffer_size);
if (data_read < 0) {
ESP_LOGE(TAG, "Error: SSL data read error");
err= ESP_FAIL;
@@ -449,7 +450,7 @@ static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client
break;
}
}
FREE_RESET(local_buff);
//FREE_RESET(local_buff);
}
return err;
@@ -459,21 +460,21 @@ static esp_err_t _http_connect(esp_http_client_handle_t http_client)
esp_err_t err = ESP_FAIL;
int status_code, header_ret;
do {
ESP_LOGD(TAG, "connecting the http client. ");
ESP_LOGI(TAG, "connecting the http client. ");
err = esp_http_client_open(http_client, 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
sendMessaging(MESSAGING_ERROR,"Failed to open HTTP connection: %s", esp_err_to_name(err));
return err;
}
ESP_LOGD(TAG, "Fetching headers");
ESP_LOGI(TAG, "Fetching headers");
header_ret = esp_http_client_fetch_headers(http_client);
if (header_ret < 0) {
// Error found
sendMessaging(MESSAGING_ERROR,"Header fetch failed");
return header_ret;
}
ESP_LOGD(TAG, "HTTP Header fetch completed, found content length of %d",header_ret);
ESP_LOGI(TAG, "HTTP Header fetch completed, found content length of %d",header_ret);
status_code = esp_http_client_get_status_code(http_client);
ESP_LOGD(TAG, "HTTP status code was %d",status_code);
@@ -515,7 +516,7 @@ esp_err_t ota_buffer_all(){
esp_err_t err=ESP_OK;
if (ota_status.ota_type == OTA_TYPE_HTTP){
GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Downloading file");
ota_http_client = esp_http_client_init(&ota_config);
ota_http_client = esp_http_client_init(&http_client_config);
if (ota_http_client == NULL) {
sendMessaging(MESSAGING_ERROR,"Error: Failed to initialize HTTP connection.");
return ESP_FAIL;
@@ -567,7 +568,6 @@ esp_err_t ota_header_check(){
ota_status.configured = esp_ota_get_boot_partition();
ota_status.running = esp_ota_get_running_partition();
ota_status.update_partition = esp_ota_get_next_update_partition(NULL);
ota_status.last_invalid_app= esp_ota_get_last_invalid_partition();
ota_status.ota_partition = _get_ota_partition(ESP_PARTITION_SUBTYPE_APP_OTA_0);
@@ -624,6 +624,7 @@ void ota_task(void *pvParameter)
ESP_LOGD(TAG, "HTTP ota Thread started");
_printMemStats();
ota_status.update_partition = esp_ota_get_next_update_partition(NULL);
ESP_LOGI(TAG,"Initializing OTA configuration");
err = init_config(pvParameter);