20 changed files with 4580 additions and 143 deletions
File diff suppressed because it is too large
@ -1,4 +1,5 @@ |
|||
file(GLOB_RECURSE SOURCES ./*.c ./*.cpp ./init/*.c ./init/*.cpp) |
|||
set(COMPONENT_SRCS ${SOURCES}) |
|||
set(COMPONENT_EMBED_TXTFILES server_root_cert.pem) |
|||
|
|||
register_component() |
|||
|
@ -0,0 +1,164 @@ |
|||
/**
|
|||
* @author: Pomin |
|||
* @date: 2022-10-20 00:41:56 |
|||
* @github: https://github.com/POMIN-163
|
|||
* @lastedit: 2022-10-20 01:01:28 |
|||
* @description: |
|||
**/ |
|||
#include "app_init.h" |
|||
#include "esp_tls.h" |
|||
#include "esp_crt_bundle.h" |
|||
#define CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS |
|||
|
|||
/* Constants that aren't configurable in menuconfig */ |
|||
#define WEB_SERVER "devapi.qweather.com" |
|||
#define WEB_URL "https://devapi.qweather.com/v7/weather/now?location=101200101&key=4eca11d06d914d56bf41c202b14c7d3e"
|
|||
|
|||
static const char *TAG = "example"; |
|||
|
|||
static const char REQUEST[] = "GET " WEB_URL " HTTP/1.1\r\n" |
|||
"Host: "WEB_SERVER"\r\n" |
|||
"User-Agent: esp-idf/1.0 esp32\r\n" |
|||
"\r\n"; |
|||
|
|||
extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start"); |
|||
extern const uint8_t server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end"); |
|||
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS |
|||
esp_tls_client_session_t *tls_client_session = NULL; |
|||
#endif |
|||
static void https_get_request(esp_tls_cfg_t cfg) |
|||
{ |
|||
char buf[512]; |
|||
int ret, len; |
|||
|
|||
struct esp_tls *tls = esp_tls_conn_http_new(WEB_URL, &cfg); |
|||
|
|||
if (tls != NULL) { |
|||
ESP_LOGI(TAG, "Connection established..."); |
|||
} else { |
|||
ESP_LOGE(TAG, "Connection failed..."); |
|||
goto exit; |
|||
} |
|||
|
|||
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS |
|||
/* The TLS session is successfully established, now saving the session ctx for reuse */ |
|||
if (tls_client_session == NULL) { |
|||
tls_client_session = esp_tls_get_client_session(tls); |
|||
} |
|||
#endif |
|||
size_t written_bytes = 0; |
|||
do { |
|||
ret = esp_tls_conn_write(tls, |
|||
REQUEST + written_bytes, |
|||
sizeof(REQUEST) - written_bytes); |
|||
if (ret >= 0) { |
|||
ESP_LOGI(TAG, "%d bytes written", ret); |
|||
written_bytes += ret; |
|||
} else if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) { |
|||
ESP_LOGE(TAG, "esp_tls_conn_write returned: [0x%02X](%s)", ret, esp_err_to_name(ret)); |
|||
goto exit; |
|||
} |
|||
} while (written_bytes < sizeof(REQUEST)); |
|||
|
|||
ESP_LOGI(TAG, "Reading HTTP response..."); |
|||
|
|||
do { |
|||
len = sizeof(buf) - 1; |
|||
bzero(buf, sizeof(buf)); |
|||
ret = esp_tls_conn_read(tls, (char *)buf, len); |
|||
|
|||
if (ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ) { |
|||
continue; |
|||
} |
|||
|
|||
if (ret < 0) { |
|||
ESP_LOGE(TAG, "esp_tls_conn_read returned [-0x%02X](%s)", -ret, esp_err_to_name(ret)); |
|||
break; |
|||
} |
|||
|
|||
if (ret == 0) { |
|||
ESP_LOGI(TAG, "connection closed"); |
|||
break; |
|||
} |
|||
|
|||
len = ret; |
|||
ESP_LOGD(TAG, "%d bytes read", len); |
|||
/* Print response directly to stdout as it is read */ |
|||
for (int i = 0; i < len; i++) { |
|||
putchar(buf[i]); |
|||
} |
|||
putchar('\n'); // JSON output doesn't have a newline at end
|
|||
} while (1); |
|||
|
|||
exit: |
|||
esp_tls_conn_delete(tls); |
|||
for (int countdown = 10; countdown >= 0; countdown--) { |
|||
ESP_LOGI(TAG, "%d...", countdown); |
|||
vTaskDelay(1000 / portTICK_PERIOD_MS); |
|||
} |
|||
} |
|||
|
|||
static void https_get_request_using_crt_bundle(void) |
|||
{ |
|||
ESP_LOGI(TAG, "https_request using crt bundle"); |
|||
esp_tls_cfg_t cfg = { |
|||
.crt_bundle_attach = esp_crt_bundle_attach, |
|||
}; |
|||
https_get_request(cfg); |
|||
} |
|||
|
|||
|
|||
|
|||
static void https_get_request_using_cacert_buf(void) |
|||
{ |
|||
ESP_LOGI(TAG, "https_request using cacert_buf"); |
|||
esp_tls_cfg_t cfg = { |
|||
.cacert_buf = (const unsigned char *) server_root_cert_pem_start, |
|||
.cacert_bytes = server_root_cert_pem_end - server_root_cert_pem_start, |
|||
}; |
|||
https_get_request(cfg); |
|||
} |
|||
|
|||
static void https_get_request_using_global_ca_store(void) |
|||
{ |
|||
esp_err_t esp_ret = ESP_FAIL; |
|||
ESP_LOGI(TAG, "https_request using global ca_store"); |
|||
esp_ret = esp_tls_set_global_ca_store(server_root_cert_pem_start, server_root_cert_pem_end - server_root_cert_pem_start); |
|||
if (esp_ret != ESP_OK) { |
|||
ESP_LOGE(TAG, "Error in setting the global ca store: [%02X] (%s),could not complete the https_request using global_ca_store", esp_ret, esp_err_to_name(esp_ret)); |
|||
return; |
|||
} |
|||
esp_tls_cfg_t cfg = { |
|||
.use_global_ca_store = true, |
|||
}; |
|||
https_get_request(cfg); |
|||
esp_tls_free_global_ca_store(); |
|||
} |
|||
|
|||
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS |
|||
static void https_get_request_using_already_saved_session(void) |
|||
{ |
|||
ESP_LOGI(TAG, "https_request using saved client session"); |
|||
esp_tls_cfg_t cfg = { |
|||
.client_session = tls_client_session, |
|||
}; |
|||
https_get_request(cfg); |
|||
free(tls_client_session); |
|||
tls_client_session = NULL; |
|||
} |
|||
#endif |
|||
|
|||
void https_init(void) |
|||
{ |
|||
ESP_LOGI(TAG, "Start https_request example"); |
|||
|
|||
https_get_request_using_crt_bundle(); |
|||
https_get_request_using_cacert_buf(); |
|||
https_get_request_using_global_ca_store(); |
|||
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS |
|||
https_get_request_using_already_saved_session(); |
|||
#endif |
|||
ESP_LOGI(TAG, "Finish https_request example"); |
|||
|
|||
} |
|||
|
@ -0,0 +1,73 @@ |
|||
/**
|
|||
* @author: Pomin |
|||
* @date: 2022-10-20 00:24:34 |
|||
* @github: https://github.com/POMIN-163
|
|||
* @lastedit: 2022-10-20 00:52:15 |
|||
* @description: |
|||
**/ |
|||
#include "app_init.h" |
|||
#include "esp_sntp.h" |
|||
|
|||
static const char *TAG = "ntp"; |
|||
|
|||
static void obtain_time(void); |
|||
static void initialize_sntp(void); |
|||
|
|||
void time_sync_notification_cb(struct timeval *tv) |
|||
{ |
|||
ESP_LOGI(TAG, "Notification of a time synchronization event"); |
|||
} |
|||
|
|||
/**
|
|||
* @brief ntp 初始化 |
|||
* |
|||
**/ |
|||
void ntp_init(void) { |
|||
time_t now; |
|||
struct tm timeinfo; |
|||
time(&now); |
|||
localtime_r(&now, &timeinfo); |
|||
// Is time set? If not, tm_year will be (1970 - 1900).
|
|||
if (timeinfo.tm_year < (2016 - 1900)) { |
|||
ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP."); |
|||
obtain_time(); |
|||
// update 'now' variable with current time
|
|||
time(&now); |
|||
} |
|||
|
|||
// Set timezone to China Standard Time
|
|||
setenv("TZ", "CST-8", 1); |
|||
tzset(); |
|||
} |
|||
static void obtain_time(void) |
|||
{ |
|||
#ifdef LWIP_DHCP_GET_NTP_SRV |
|||
sntp_servermode_dhcp(1); |
|||
#endif |
|||
|
|||
initialize_sntp(); |
|||
|
|||
// wait for time to be set
|
|||
time_t now = 0; |
|||
struct tm timeinfo = { 0 }; |
|||
int retry = 0; |
|||
const int retry_count = 10; |
|||
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) { |
|||
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); |
|||
vTaskDelay(2000 / portTICK_PERIOD_MS); |
|||
} |
|||
time(&now); |
|||
localtime_r(&now, &timeinfo); |
|||
// Set timezone to China Standard Time
|
|||
setenv("TZ", "CST-8", 1); |
|||
tzset(); |
|||
} |
|||
|
|||
static void initialize_sntp(void) |
|||
{ |
|||
ESP_LOGI(TAG, "Initializing SNTP"); |
|||
sntp_setoperatingmode(SNTP_OPMODE_POLL); |
|||
sntp_setservername(0, "pool.ntp.org"); |
|||
sntp_set_time_sync_notification_cb(time_sync_notification_cb); |
|||
sntp_init(); |
|||
} |
@ -0,0 +1,31 @@ |
|||
-----BEGIN CERTIFICATE----- |
|||
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw |
|||
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh |
|||
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4 |
|||
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu |
|||
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY |
|||
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc |
|||
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+ |
|||
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U |
|||
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW |
|||
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3q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