55#include <stdio.h>
66
77#include "esp_http_client.h"
8+ #include "esp_partition.h"
9+ #include "esp_crt_bundle.h"
810
911#define MAX_HTTP_RECV_BUFFER 128
1012
@@ -18,10 +20,38 @@ int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
1820 }
1921 esp_http_client_config_t config = {
2022 .url = url ,
21- .cert_pem = cert_pem ,
2223 .timeout_ms = 20000 ,
2324 };
2425
26+ spi_flash_mmap_handle_t handle ;
27+ const unsigned char * certs_data = NULL ;
28+
29+ if (cert_pem != NULL ) {
30+ config .cert_pem = cert_pem ;
31+ } else {
32+ config .crt_bundle_attach = esp_crt_bundle_attach ;
33+
34+ const esp_partition_t * part = esp_partition_find_first (ESP_PARTITION_TYPE_DATA , ESP_PARTITION_SUBTYPE_ANY , "certs" );
35+ if (part == NULL ) {
36+ return 0 ;
37+ }
38+
39+ int ret = esp_partition_mmap (part , 0 , part -> size , SPI_FLASH_MMAP_DATA , (const void * * )& certs_data , & handle );
40+ if (ret != ESP_OK ) {
41+ return 0 ;
42+ }
43+
44+ ret = esp_crt_bundle_attach (& config );
45+ if (ret != ESP_OK ) {
46+ return 0 ;
47+ }
48+
49+ ret = esp_crt_bundle_set (certs_data , CRT_BUNDLE_SIZE );
50+ if (ret != ESP_OK ) {
51+ return 0 ;
52+ }
53+ }
54+
2555 esp_http_client_handle_t client = esp_http_client_init (& config );
2656 esp_err_t err ;
2757 if ((err = esp_http_client_open (client , 0 )) != ESP_OK ) {
@@ -43,7 +73,7 @@ int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
4373 ESP_LOGV (TAG , "connection closed, cleaning up, total %d bytes received" , total_read_len );
4474 esp_http_client_close (client );
4575 esp_http_client_cleanup (client );
46- free (buffer );
76+ free (buffer );
4777
4878 return 0 ;
4979}
0 commit comments