squeezelite-ota.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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_https_ota.h"
  13. #include "string.h"
  14. #include <stdbool.h>
  15. #include "nvs.h"
  16. #include "nvs_flash.h"
  17. #include "cmd_system.h"
  18. #include "esp_err.h"
  19. #include "squeezelite-ota.h"
  20. #include "esp_netif.h"
  21. #include "platform_config.h"
  22. #include <time.h>
  23. #include <sys/time.h>
  24. #include <stdarg.h>
  25. #include "esp_secure_boot.h"
  26. #include "esp_flash_encrypt.h"
  27. #include "esp_spi_flash.h"
  28. #include "sdkconfig.h"
  29. #include "messaging.h"
  30. #include "esp_ota_ops.h"
  31. #include "display.h"
  32. #include "gds.h"
  33. #include "gds_text.h"
  34. #include "gds_draw.h"
  35. #include "platform_esp32.h"
  36. #include "lwip/sockets.h"
  37. #include "globdefs.h"
  38. #include "tools.h"
  39. extern const char * get_certificate();
  40. #define IF_DISPLAY(x) if(display) { x; }
  41. #ifdef CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1
  42. #define OTA_CORE 0
  43. #else
  44. #define OTA_CORE 1
  45. #endif
  46. static const char *TAG = "squeezelite-ota";
  47. esp_http_client_handle_t ota_http_client = NULL;
  48. #define IMAGE_HEADER_SIZE sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1
  49. #define BUFFSIZE 4096
  50. #define HASH_LEN 32 /* SHA-256 digest length */
  51. typedef struct {
  52. char * url;
  53. char * bin;
  54. uint32_t length;
  55. } ota_thread_parms_t ;
  56. static ota_thread_parms_t ota_thread_parms;
  57. typedef enum {
  58. OTA_TYPE_HTTP,
  59. OTA_TYPE_BUFFER,
  60. OTA_TYPE_INVALID
  61. } ota_type_t;
  62. typedef struct {
  63. size_t actual_image_len;
  64. float downloaded_image_len;
  65. float total_image_len;
  66. float remain_image_len;
  67. ota_type_t ota_type;
  68. char * ota_write_data;
  69. char * bin_data;
  70. bool bOTAStarted;
  71. size_t buffer_size;
  72. uint8_t lastpct;
  73. uint8_t newpct;
  74. uint8_t newdownloadpct;
  75. struct timeval OTA_start;
  76. bool bOTAThreadStarted;
  77. const esp_partition_t *configured;
  78. const esp_partition_t *running;
  79. const esp_partition_t * update_partition;
  80. const esp_partition_t* last_invalid_app ;
  81. const esp_partition_t * ota_partition;
  82. } ota_status_t;
  83. ota_status_t * ota_status;
  84. struct timeval tv;
  85. static esp_http_client_config_t http_client_config;
  86. void _printMemStats(){
  87. ESP_LOGD(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu) dma:%zu (min:%zu)",
  88. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  89. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  90. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  91. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  92. heap_caps_get_free_size(MALLOC_CAP_DMA),
  93. heap_caps_get_minimum_free_size(MALLOC_CAP_DMA));
  94. }
  95. uint8_t ota_get_pct_complete(){
  96. return ota_status->total_image_len==0?0:
  97. (uint8_t)((float)ota_status->actual_image_len/ota_status->total_image_len*100.0f);
  98. }
  99. uint8_t ota_get_pct_downloaded(){
  100. return ota_status->total_image_len==0?0:
  101. (uint8_t)(ota_status->downloaded_image_len/ota_status->total_image_len*100.0f);
  102. }
  103. typedef struct {
  104. int x1,y1,x2,y2,width,height;
  105. } rect_t;
  106. typedef struct _progress {
  107. int border_thickness;
  108. int sides_margin;
  109. int vertical_margin;
  110. int bar_tot_height;
  111. int bar_fill_height;
  112. rect_t border;
  113. rect_t filler;
  114. } progress_t;
  115. static progress_t * loc_displayer_get_progress_dft(){
  116. int start_coord_offset=0;
  117. static progress_t def={
  118. .border_thickness = 2,
  119. .sides_margin = 2,
  120. .bar_tot_height = 7,
  121. };
  122. def.bar_fill_height= def.bar_tot_height-(def.border_thickness*2);
  123. def.border.x1=start_coord_offset+def.sides_margin;
  124. IF_DISPLAY(def.border.x2=GDS_GetWidth(display)-def.sides_margin);
  125. // progress bar will be drawn at the bottom of the display
  126. IF_DISPLAY( def.border.y2= GDS_GetHeight(display)-def.border_thickness);
  127. def.border.y1= def.border.y2-def.bar_tot_height;
  128. def.border.width=def.border.x2-def.border.x1;
  129. def.border.height=def.border.y2-def.border.y1;
  130. def.filler.x1= def.border.x1+def.border_thickness;
  131. def.filler.x2= def.border.x2-def.border_thickness;
  132. def.filler.y1= def.border.y1+def.border_thickness;
  133. def.filler.y2= def.border.y2-def.border_thickness;
  134. def.filler.width=def.filler.x2-def.filler.x1;
  135. def.filler.height=def.filler.y2-def.filler.y1;
  136. assert(def.filler.width>0);
  137. assert(def.filler.height>0);
  138. assert(def.border.width>0);
  139. assert(def.border.height>0);
  140. assert(def.border.width>def.filler.width);
  141. assert(def.border.height>def.filler.height);
  142. return &def;
  143. }
  144. static void loc_displayer_progressbar(uint8_t pct){
  145. static progress_t * progress_coordinates;
  146. if(!display){
  147. return;
  148. }
  149. if(!progress_coordinates) progress_coordinates = loc_displayer_get_progress_dft();
  150. int filler_x=progress_coordinates->filler.x1+(int)((float)progress_coordinates->filler.width*(float)pct/(float)100);
  151. ESP_LOGD(TAG,"Drawing %d,%d,%d,%d",progress_coordinates->border.x1,progress_coordinates->border.y1,progress_coordinates->border.x2,progress_coordinates->border.y2);
  152. GDS_DrawBox(display,progress_coordinates->border.x1,progress_coordinates->border.y1,progress_coordinates->border.x2,progress_coordinates->border.y2,GDS_COLOR_WHITE,false);
  153. ESP_LOGD(TAG,"Drawing %d,%d,%d,%d",progress_coordinates->filler.x1,progress_coordinates->filler.y1,filler_x,progress_coordinates->filler.y2);
  154. if(filler_x > progress_coordinates->filler.x1){
  155. GDS_DrawBox(display,progress_coordinates->filler.x1,progress_coordinates->filler.y1,filler_x,progress_coordinates->filler.y2,GDS_COLOR_WHITE,true);
  156. }
  157. else {
  158. // Clear the inner box
  159. GDS_DrawBox(display,progress_coordinates->filler.x1,progress_coordinates->filler.y1,progress_coordinates->filler.x2,progress_coordinates->filler.y2,GDS_COLOR_BLACK,true);
  160. }
  161. ESP_LOGD(TAG,"Updating Display");
  162. GDS_Update(display);
  163. }
  164. void sendMessaging(messaging_types type,const char * fmt, ...){
  165. va_list args;
  166. cJSON * msg = cJSON_CreateObject();
  167. size_t str_len=0;
  168. char * msg_str=NULL;
  169. va_start(args, fmt);
  170. str_len = vsnprintf(NULL,0,fmt,args)+1;
  171. if(str_len>0){
  172. msg_str = malloc_init_external(str_len);
  173. vsnprintf(msg_str,str_len,fmt,args);
  174. if(type == MESSAGING_WARNING){
  175. ESP_LOGW(TAG,"%s",msg_str);
  176. }
  177. else if (type == MESSAGING_ERROR){
  178. ESP_LOGE(TAG,"%s",msg_str);
  179. }
  180. else
  181. ESP_LOGI(TAG,"%s",msg_str);
  182. }
  183. else {
  184. ESP_LOGW(TAG, "Sending empty string message");
  185. }
  186. va_end(args);
  187. if(type!=MESSAGING_INFO){
  188. IF_DISPLAY(GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, msg_str));
  189. }
  190. cJSON_AddStringToObject(msg,"ota_dsc",str_or_unknown(msg_str));
  191. free(msg_str);
  192. cJSON_AddNumberToObject(msg,"ota_pct", ota_get_pct_complete() );
  193. char * json_msg = cJSON_PrintUnformatted(msg);
  194. messaging_post_message(type, MESSAGING_CLASS_OTA, json_msg);
  195. free(json_msg);
  196. cJSON_Delete(msg);
  197. _printMemStats();
  198. }
  199. static void __attribute__((noreturn)) task_fatal_error(void)
  200. {
  201. ESP_LOGE(TAG, "Exiting task due to fatal error...");
  202. (void)vTaskDelete(NULL);
  203. while (1) {
  204. ;
  205. }
  206. }
  207. esp_err_t handle_http_on_data(esp_http_client_event_t *evt){
  208. int http_status= esp_http_client_get_status_code(evt->client);
  209. static char * recv_ptr=NULL;
  210. if(http_status == 200){
  211. if(!ota_status->bOTAStarted)
  212. {
  213. sendMessaging(MESSAGING_INFO,"Downloading firmware");
  214. ota_status->bOTAStarted = true;
  215. ota_status->total_image_len=esp_http_client_get_content_length(evt->client);
  216. ota_status->downloaded_image_len = 0;
  217. ota_status->newdownloadpct = 0;
  218. ota_status->bin_data= malloc_init_external(ota_status->total_image_len);
  219. if(ota_status->bin_data==NULL){
  220. sendMessaging(MESSAGING_ERROR,"Error: buffer alloc error");
  221. return ESP_FAIL;
  222. }
  223. recv_ptr=ota_status->bin_data;
  224. }
  225. // we're downloading the binary data file
  226. if (!esp_http_client_is_chunked_response(evt->client)) {
  227. memcpy(recv_ptr,evt->data,evt->data_len);
  228. ota_status->downloaded_image_len +=evt->data_len;
  229. recv_ptr+=evt->data_len;
  230. }
  231. if(ota_get_pct_downloaded()%5 == 0 && ota_get_pct_downloaded()%5!=ota_status->newdownloadpct) {
  232. ota_status->newdownloadpct= ota_get_pct_downloaded();
  233. loc_displayer_progressbar(ota_status->newdownloadpct);
  234. gettimeofday(&tv, NULL);
  235. uint32_t elapsed_ms= (tv.tv_sec-ota_status->OTA_start.tv_sec )*1000+(tv.tv_usec-ota_status->OTA_start.tv_usec)/1000;
  236. ESP_LOGI(TAG,"OTA download progress : %f/%f (%d pct), %f KB/s", ota_status->downloaded_image_len, ota_status->total_image_len, ota_status->newdownloadpct, elapsed_ms>0?ota_status->downloaded_image_len*1000/elapsed_ms/1024:0);
  237. sendMessaging(MESSAGING_INFO,"Downloading firmware %%%3d.",ota_status->newdownloadpct);
  238. }
  239. }
  240. return ESP_OK;
  241. }
  242. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  243. {
  244. // --------------
  245. // Received parameters
  246. //
  247. // esp_http_client_event_id_tevent_id event_id, to know the cause of the event
  248. // esp_http_client_handle_t client
  249. // esp_http_client_handle_t context
  250. // void *data data of the event
  251. // int data_len - data length of data
  252. // void *user_data -- user_data context, from esp_http_client_config_t user_data
  253. // char *header_key For HTTP_EVENT_ON_HEADER event_id, it�s store current http header key
  254. // char *header_value For HTTP_EVENT_ON_HEADER event_id, it�s store current http header value
  255. // --------------
  256. switch (evt->event_id) {
  257. case HTTP_EVENT_ERROR:
  258. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  259. _printMemStats();
  260. break;
  261. case HTTP_EVENT_ON_CONNECTED:
  262. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  263. if(ota_status->bOTAStarted) sendMessaging(MESSAGING_INFO,"HTTP Connected");
  264. ota_status->total_image_len=0;
  265. ota_status->actual_image_len=0;
  266. ota_status->lastpct=0;
  267. ota_status->remain_image_len=0;
  268. ota_status->newpct=0;
  269. gettimeofday(&ota_status->OTA_start, NULL);
  270. break;
  271. case HTTP_EVENT_HEADER_SENT:
  272. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  273. break;
  274. case HTTP_EVENT_ON_HEADER:
  275. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s",evt->header_key, evt->header_value);
  276. // if (strcasecmp(evt->header_key, "location") == 0) {
  277. // ESP_LOGW(TAG,"OTA will redirect to url: %s",evt->header_value);
  278. // }
  279. // if (strcasecmp(evt->header_key, "content-length") == 0) {
  280. // ota_status->total_image_len = atol(evt->header_value);
  281. // ESP_LOGW(TAG, "Content length found: %s, parsed to %d", evt->header_value, ota_status->total_image_len);
  282. // }
  283. break;
  284. case HTTP_EVENT_ON_DATA:
  285. return handle_http_on_data(evt);
  286. break;
  287. case HTTP_EVENT_ON_FINISH:
  288. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  289. break;
  290. case HTTP_EVENT_DISCONNECTED:
  291. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  292. break;
  293. }
  294. return ESP_OK;
  295. }
  296. esp_err_t init_config(ota_thread_parms_t * p_ota_thread_parms){
  297. memset(&http_client_config, 0x00, sizeof(http_client_config));
  298. sendMessaging(MESSAGING_INFO,"Initializing...");
  299. loc_displayer_progressbar(0);
  300. ota_status->ota_type= OTA_TYPE_INVALID;
  301. if(p_ota_thread_parms->url !=NULL && strlen(p_ota_thread_parms->url)>0 ){
  302. ota_status->ota_type= OTA_TYPE_HTTP;
  303. }
  304. else if(p_ota_thread_parms->bin!=NULL && p_ota_thread_parms->length > 0) {
  305. ota_status->ota_type= OTA_TYPE_BUFFER;
  306. }
  307. if( ota_status->ota_type== OTA_TYPE_INVALID ){
  308. ESP_LOGE(TAG,"HTTP OTA called without a url or a binary buffer");
  309. return ESP_ERR_INVALID_ARG;
  310. }
  311. ota_status->buffer_size = BUFFSIZE;
  312. ota_status->ota_write_data = heap_caps_malloc(ota_status->buffer_size+1 , (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
  313. if(ota_status->ota_write_data== NULL){
  314. ESP_LOGE(TAG,"Error allocating the ota buffer");
  315. return ESP_ERR_NO_MEM;
  316. }
  317. switch (ota_status->ota_type) {
  318. case OTA_TYPE_HTTP:
  319. http_client_config.cert_pem =get_certificate();
  320. http_client_config.event_handler = _http_event_handler;
  321. http_client_config.disable_auto_redirect=false;
  322. http_client_config.skip_cert_common_name_check = false;
  323. http_client_config.url = strdup_psram(p_ota_thread_parms->url);
  324. http_client_config.max_redirection_count = 4;
  325. // buffer size below is for http read chunks
  326. http_client_config.buffer_size = 8192; //1024 ;
  327. http_client_config.buffer_size_tx = 8192;
  328. //http_client_config.timeout_ms = 5000;
  329. break;
  330. case OTA_TYPE_BUFFER:
  331. ota_status->bin_data = p_ota_thread_parms->bin;
  332. ota_status->total_image_len = p_ota_thread_parms->length;
  333. break;
  334. default:
  335. return ESP_FAIL;
  336. break;
  337. }
  338. return ESP_OK;
  339. }
  340. esp_partition_t * _get_ota_partition(esp_partition_subtype_t subtype){
  341. esp_partition_t *ota_partition=NULL;
  342. ESP_LOGD(TAG, "Looking for OTA partition.");
  343. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, subtype , NULL);
  344. if(it == NULL){
  345. ESP_LOGE(TAG,"Unable initialize partition iterator!");
  346. }
  347. else {
  348. ota_partition = (esp_partition_t *) esp_partition_get(it);
  349. if(ota_partition != NULL){
  350. ESP_LOGD(TAG, "Found OTA partition: %s.",ota_partition->label);
  351. }
  352. else {
  353. ESP_LOGE(TAG,"OTA partition not found! Unable update application.");
  354. }
  355. esp_partition_iterator_release(it);
  356. }
  357. return ota_partition;
  358. }
  359. esp_err_t _erase_last_boot_app_partition(const esp_partition_t *ota_partition)
  360. {
  361. uint16_t num_passes=0;
  362. uint16_t remain_size=0;
  363. uint32_t single_pass_size=0;
  364. esp_err_t err=ESP_OK;
  365. char * ota_erase_size=config_alloc_get(NVS_TYPE_STR, "ota_erase_blk");
  366. if(ota_erase_size!=NULL) {
  367. single_pass_size = atol(ota_erase_size);
  368. ESP_LOGD(TAG,"OTA Erase block size is %d (from string: %s)",single_pass_size, ota_erase_size );
  369. free(ota_erase_size);
  370. }
  371. else {
  372. ESP_LOGW(TAG,"OTA Erase block config not found");
  373. single_pass_size = OTA_FLASH_ERASE_BLOCK;
  374. }
  375. if(single_pass_size % SPI_FLASH_SEC_SIZE !=0){
  376. uint32_t temp_single_pass_size = single_pass_size-(single_pass_size % SPI_FLASH_SEC_SIZE);
  377. ESP_LOGW(TAG,"Invalid erase block size of %u. Value should be a multiple of %d and will be adjusted to %u.", single_pass_size, SPI_FLASH_SEC_SIZE,temp_single_pass_size);
  378. single_pass_size=temp_single_pass_size;
  379. }
  380. ESP_LOGD(TAG,"Erasing flash partition of size %u in blocks of %d bytes", ota_partition->size, single_pass_size);
  381. num_passes=ota_partition->size/single_pass_size;
  382. remain_size=ota_partition->size-(num_passes*single_pass_size);
  383. ESP_LOGI(TAG,"Erasing in %d passes with blocks of %d bytes ", num_passes,single_pass_size);
  384. for(uint16_t i=0;i<num_passes;i++){
  385. ESP_LOGD(TAG,"Erasing flash (%u%%)",i/num_passes);
  386. ESP_LOGD(TAG,"Pass %d of %d, with chunks of %d bytes, from %d to %d", i+1, num_passes,single_pass_size,i*single_pass_size,i*single_pass_size+single_pass_size);
  387. err=esp_partition_erase_range(ota_partition, i*single_pass_size, single_pass_size);
  388. if(err!=ESP_OK) return err;
  389. if(i%2) {
  390. loc_displayer_progressbar((int)(((float)i/(float)num_passes)*100.0f));
  391. sendMessaging(MESSAGING_INFO,"Erasing flash (%u/%u)",i,num_passes);
  392. }
  393. vTaskDelay(100/ portTICK_PERIOD_MS); // wait here for a short amount of time. This will help with reducing WDT errors
  394. }
  395. if(remain_size>0){
  396. err=esp_partition_erase_range(ota_partition, ota_partition->size-remain_size, remain_size);
  397. if(err!=ESP_OK) return err;
  398. }
  399. sendMessaging(MESSAGING_INFO,"Erasing flash complete.");
  400. loc_displayer_progressbar(100);
  401. vTaskDelay(200/ portTICK_PERIOD_MS);
  402. return ESP_OK;
  403. }
  404. void ota_task_cleanup(const char * message, ...){
  405. ota_status->bOTAThreadStarted=false;
  406. loc_displayer_progressbar(0);
  407. if(message!=NULL){
  408. va_list args;
  409. va_start(args, message);
  410. sendMessaging(MESSAGING_ERROR,message, args);
  411. va_end(args);
  412. }
  413. FREE_RESET(ota_status->ota_write_data);
  414. FREE_RESET(ota_status->bin_data);
  415. if(ota_http_client!=NULL) {
  416. esp_http_client_cleanup(ota_http_client);
  417. ota_http_client=NULL;
  418. }
  419. ota_status->bOTAStarted = false;
  420. task_fatal_error();
  421. }
  422. esp_err_t ota_buffer_all(){
  423. esp_err_t err=ESP_OK;
  424. if (ota_status->ota_type == OTA_TYPE_HTTP){
  425. IF_DISPLAY(GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Downloading file"));
  426. ota_http_client = esp_http_client_init(&http_client_config);
  427. if (ota_http_client == NULL) {
  428. sendMessaging(MESSAGING_ERROR,"Error: Failed to initialize HTTP connection.");
  429. return ESP_FAIL;
  430. }
  431. _printMemStats();
  432. err = esp_http_client_perform(ota_http_client);
  433. if (err != ESP_OK) {
  434. sendMessaging(MESSAGING_ERROR,"Error: Failed to execute HTTP download. %s",esp_err_to_name(err));
  435. return ESP_FAIL;
  436. }
  437. if(ota_status->total_image_len<=0){
  438. sendMessaging(MESSAGING_ERROR,"Error: Invalid image length");
  439. return ESP_FAIL;
  440. }
  441. sendMessaging(MESSAGING_INFO,"Download success");
  442. }
  443. else {
  444. gettimeofday(&ota_status->OTA_start, NULL);
  445. }
  446. ota_status->remain_image_len=ota_status->total_image_len;
  447. return err;
  448. }
  449. int ota_buffer_read(){
  450. int data_read=0;
  451. if(ota_status->remain_image_len >ota_status->buffer_size){
  452. data_read = ota_status->buffer_size;
  453. } else {
  454. data_read = ota_status->remain_image_len;
  455. }
  456. memcpy(ota_status->ota_write_data, &ota_status->bin_data[ota_status->actual_image_len], data_read);
  457. ota_status->actual_image_len += data_read;
  458. ota_status->remain_image_len -= data_read;
  459. return data_read;
  460. }
  461. esp_err_t ota_header_check(){
  462. esp_app_desc_t new_app_info;
  463. esp_app_desc_t running_app_info;
  464. ota_status->configured = esp_ota_get_boot_partition();
  465. ota_status->running = esp_ota_get_running_partition();
  466. ota_status->last_invalid_app= esp_ota_get_last_invalid_partition();
  467. ota_status->ota_partition = _get_ota_partition(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  468. ESP_LOGD(TAG, "Running partition [%s] type %d subtype %d (offset 0x%08x)", ota_status->running->label, ota_status->running->type, ota_status->running->subtype, ota_status->running->address);
  469. if (ota_status->total_image_len > ota_status->ota_partition->size){
  470. ota_task_cleanup("Error: Image size (%d) too large to fit in partition (%d).",ota_status->ota_partition->size,ota_status->total_image_len );
  471. return ESP_FAIL;
  472. }
  473. if(ota_status->ota_partition == NULL){
  474. ESP_LOGE(TAG,"Unable to locate OTA application partition. ");
  475. ota_task_cleanup("Error: OTA partition not found");
  476. return ESP_FAIL;
  477. }
  478. if (ota_status->configured != ota_status->running) {
  479. ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x", ota_status->configured->address, ota_status->running->address);
  480. ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
  481. }
  482. ESP_LOGD(TAG, "Next ota update partition is: [%s] subtype %d at offset 0x%x",
  483. ota_status->update_partition->label, ota_status->update_partition->subtype, ota_status->update_partition->address);
  484. if (ota_status->total_image_len >= IMAGE_HEADER_SIZE) {
  485. // check current version with downloading
  486. memcpy(&new_app_info, &ota_status->bin_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
  487. ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
  488. if (esp_ota_get_partition_description(ota_status->running, &running_app_info) == ESP_OK) {
  489. ESP_LOGD(TAG, "Running recovery version: %s", running_app_info.version);
  490. }
  491. sendMessaging(MESSAGING_INFO,"New version is : %s",new_app_info.version);
  492. esp_app_desc_t invalid_app_info;
  493. if (esp_ota_get_partition_description(ota_status->last_invalid_app, &invalid_app_info) == ESP_OK) {
  494. ESP_LOGD(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
  495. }
  496. if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
  497. ESP_LOGW(TAG, "Current running version is the same as a new.");
  498. }
  499. return ESP_OK;
  500. }
  501. else{
  502. ota_task_cleanup("Error: Binary file too small");
  503. }
  504. return ESP_FAIL;
  505. }
  506. void ota_task(void *pvParameter)
  507. {
  508. esp_err_t err = ESP_OK;
  509. int data_read = 0;
  510. IF_DISPLAY(GDS_TextSetFont(display,2,GDS_GetHeight(display)>32?&Font_droid_sans_fallback_15x17:&Font_droid_sans_fallback_11x13,-2))
  511. IF_DISPLAY( GDS_ClearExt(display, true));
  512. IF_DISPLAY(GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Firmware update"));
  513. IF_DISPLAY(GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Initializing"));
  514. loc_displayer_progressbar(0);
  515. ESP_LOGD(TAG, "HTTP ota Thread started");
  516. _printMemStats();
  517. ota_status->update_partition = esp_ota_get_next_update_partition(NULL);
  518. ESP_LOGD(TAG,"Initializing OTA configuration");
  519. err = init_config(pvParameter);
  520. if(err!=ESP_OK){
  521. ota_task_cleanup("Error: Failed to initialize OTA.");
  522. return;
  523. }
  524. _printMemStats();
  525. sendMessaging(MESSAGING_INFO,"Starting OTA...");
  526. err=ota_buffer_all();
  527. if(err!=ESP_OK){
  528. ota_task_cleanup(NULL);
  529. return;
  530. }
  531. if(ota_header_check()!=ESP_OK){
  532. ota_task_cleanup(NULL);
  533. return;
  534. }
  535. /* Locate and erase ota application partition */
  536. sendMessaging(MESSAGING_INFO,"Formatting OTA partition");
  537. ESP_LOGW(TAG,"**************** Expecting WATCHDOG errors below during flash erase. This is OK and not to worry about **************** ");
  538. IF_DISPLAY(GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Formatting partition"));
  539. _printMemStats();
  540. err=_erase_last_boot_app_partition(ota_status->ota_partition);
  541. if(err!=ESP_OK){
  542. ota_task_cleanup("Error: Unable to erase last APP partition. (%s)",esp_err_to_name(err));
  543. return;
  544. }
  545. loc_displayer_progressbar(0);
  546. _printMemStats();
  547. // Call OTA Begin with a small partition size - this minimizes the time spent in erasing partition,
  548. // which was already done above
  549. esp_ota_handle_t update_handle = 0 ;
  550. gettimeofday(&ota_status->OTA_start, NULL);
  551. err = esp_ota_begin(ota_status->ota_partition, 512, &update_handle);
  552. if (err != ESP_OK) {
  553. ota_task_cleanup("esp_ota_begin failed (%s)", esp_err_to_name(err));
  554. return;
  555. }
  556. ESP_LOGD(TAG, "esp_ota_begin succeeded");
  557. IF_DISPLAY(GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Writing image..."));
  558. while (ota_status->remain_image_len>0) {
  559. data_read = ota_buffer_read();
  560. if (data_read <= 0) {
  561. ota_task_cleanup("Error: Data read error");
  562. return;
  563. } else if (data_read > 0) {
  564. err = esp_ota_write( update_handle, (const void *)ota_status->ota_write_data, data_read);
  565. if (err != ESP_OK) {
  566. ota_task_cleanup("Error: OTA Partition write failure. (%s)",esp_err_to_name(err));
  567. return;
  568. }
  569. ESP_LOGD(TAG, "Written image length %d", ota_status->actual_image_len);
  570. if(ota_get_pct_complete()%5 == 0) ota_status->newpct = ota_get_pct_complete();
  571. if(ota_status->lastpct!=ota_status->newpct ) {
  572. loc_displayer_progressbar(ota_status->newpct);
  573. gettimeofday(&tv, NULL);
  574. uint32_t elapsed_ms= (tv.tv_sec-ota_status->OTA_start.tv_sec )*1000+(tv.tv_usec-ota_status->OTA_start.tv_usec)/1000;
  575. ESP_LOGI(TAG,"OTA progress : %d/%.0f (%d pct), %d KB/s", ota_status->actual_image_len, ota_status->total_image_len, ota_status->newpct, elapsed_ms>0?ota_status->actual_image_len*1000/elapsed_ms/1024:0);
  576. sendMessaging(MESSAGING_INFO,"Writing binary file %3d %%.",ota_status->newpct);
  577. ota_status->lastpct=ota_status->newpct;
  578. }
  579. taskYIELD();
  580. } else if (data_read == 0) {
  581. ESP_LOGD(TAG, "End of OTA data stream");
  582. break;
  583. }
  584. }
  585. ESP_LOGI(TAG, "Total Write binary data length: %d", ota_status->actual_image_len);
  586. if (ota_status->total_image_len != ota_status->actual_image_len) {
  587. ota_task_cleanup("Error: Error in receiving complete file");
  588. return;
  589. }
  590. _printMemStats();
  591. loc_displayer_progressbar(100);
  592. err = esp_ota_end(update_handle);
  593. if (err != ESP_OK) {
  594. ota_task_cleanup("Error: %s",esp_err_to_name(err));
  595. return;
  596. }
  597. _printMemStats();
  598. err = esp_ota_set_boot_partition(ota_status->ota_partition);
  599. if (err == ESP_OK) {
  600. ESP_LOGI(TAG,"OTA Process completed successfully!");
  601. sendMessaging(MESSAGING_INFO,"Success!");
  602. IF_DISPLAY(GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "Success!"));
  603. vTaskDelay(3500/ portTICK_PERIOD_MS); // wait here to give the UI a chance to refresh
  604. IF_DISPLAY(GDS_Clear(display,GDS_COLOR_BLACK));
  605. esp_restart();
  606. } else {
  607. ota_task_cleanup("Error: Unable to update boot partition [%s]",esp_err_to_name(err));
  608. return;
  609. }
  610. ota_task_cleanup(NULL);
  611. return;
  612. }
  613. esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length){
  614. int ret = 0;
  615. uint16_t stack_size, task_priority;
  616. if(ota_status && ota_status->bOTAThreadStarted){
  617. ESP_LOGE(TAG,"OTA Already started. ");
  618. return ESP_FAIL;
  619. }
  620. ota_status = malloc_init_external(sizeof(ota_status_t));
  621. ota_status->bOTAThreadStarted=true;
  622. if(bin_url){
  623. ota_thread_parms.url =strdup_psram(bin_url);
  624. ESP_LOGD(TAG, "Starting ota on core %u for : %s", OTA_CORE,ota_thread_parms.url);
  625. }
  626. else {
  627. ota_thread_parms.bin = bin_buffer;
  628. ota_thread_parms.length = length;
  629. ESP_LOGD(TAG, "Starting ota on core %u for file upload", OTA_CORE);
  630. }
  631. char * num_buffer=config_alloc_get(NVS_TYPE_STR, "ota_stack");
  632. if(num_buffer!=NULL) {
  633. stack_size= atol(num_buffer);
  634. FREE_AND_NULL(num_buffer);
  635. }
  636. else {
  637. ESP_LOGW(TAG,"OTA stack size config not found");
  638. stack_size = OTA_STACK_SIZE;
  639. }
  640. num_buffer=config_alloc_get(NVS_TYPE_STR, "ota_prio");
  641. if(num_buffer!=NULL) {
  642. task_priority= atol(num_buffer);
  643. FREE_AND_NULL(num_buffer);
  644. }
  645. else {
  646. ESP_LOGW(TAG,"OTA task priority not found");
  647. task_priority= OTA_TASK_PRIOTITY;
  648. }
  649. ESP_LOGD(TAG,"OTA task stack size %d, priority %d (%d %s ESP_TASK_MAIN_PRIO)",stack_size , task_priority, abs(task_priority-ESP_TASK_MAIN_PRIO), task_priority-ESP_TASK_MAIN_PRIO>0?"above":"below");
  650. // ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", stack_size , (void *)&ota_thread_parms, task_priority, NULL, OTA_CORE);
  651. ret=xTaskCreate(&ota_task, "ota_task", stack_size , (void *)&ota_thread_parms, task_priority, NULL);
  652. if (ret != pdPASS) {
  653. ESP_LOGE(TAG, "create thread %s failed", "ota_task");
  654. return ESP_FAIL;
  655. }
  656. return ESP_OK;
  657. }
  658. extern void set_lms_server_details(in_addr_t ip, u16_t hport, u16_t cport);
  659. in_addr_t discover_ota_server(int max) {
  660. struct sockaddr_in d;
  661. struct sockaddr_in s;
  662. char buf[32], port_d[] = "JSON", clip_d[] = "CLIP";
  663. struct pollfd pollinfo;
  664. uint8_t len;
  665. uint16_t hport=9000;
  666. uint16_t cport=9090;
  667. int disc_sock = socket(AF_INET, SOCK_DGRAM, 0);
  668. socklen_t enable = 1;
  669. setsockopt(disc_sock, SOL_SOCKET, SO_BROADCAST, (const void *)&enable, sizeof(enable));
  670. len = sprintf(buf,"e%s%c%s", port_d, '\0', clip_d) + 1;
  671. memset(&d, 0, sizeof(d));
  672. d.sin_family = AF_INET;
  673. d.sin_port = htons(3483);
  674. d.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  675. pollinfo.fd = disc_sock;
  676. pollinfo.events = POLLIN;
  677. do {
  678. ESP_LOGI(TAG,"sending LMS discovery for OTA Update");
  679. memset(&s, 0, sizeof(s));
  680. if (sendto(disc_sock, buf, len, 0, (struct sockaddr *)&d, sizeof(d)) < 0) {
  681. ESP_LOGE(TAG,"error sending discovery");
  682. }
  683. else {
  684. if (poll(&pollinfo, 1, 5000) == 1) {
  685. char readbuf[64], *p;
  686. socklen_t slen = sizeof(s);
  687. memset(readbuf, 0, sizeof(readbuf));
  688. recvfrom(disc_sock, readbuf, sizeof(readbuf) - 1, 0, (struct sockaddr *)&s, &slen);
  689. ESP_LOGI(TAG,"got response from: %s:%d - %s", inet_ntoa(s.sin_addr), ntohs(s.sin_port),readbuf);
  690. if ((p = strstr(readbuf, port_d)) != NULL) {
  691. p += strlen(port_d);
  692. hport = atoi(p + 1);
  693. }
  694. if ((p = strstr(readbuf, clip_d)) != NULL) {
  695. p += strlen(clip_d);
  696. cport = atoi(p + 1);
  697. }
  698. server_notify(s.sin_addr.s_addr, hport, cport);
  699. }
  700. }
  701. } while (s.sin_addr.s_addr == 0 && (!max || --max));
  702. closesocket(disc_sock);
  703. return s.sin_addr.s_addr;
  704. }