You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.2 KiB
C

#include "esp_tls.h"
#include "esp_log.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_crt_bundle.h"
static const char *TAG = "https";
#define WEB_SERVER "bsvg.efa.de"
#define WEB_PORT "443"
#define WEB_URL "/bsvagstd/XML_DM_REQUEST?outputFormat=JSON&stateless=1&locationServerActive=1&type_dm=stop&name_dm=\"Freiastr,Braunschweig\"&mode=direct&ptOptionsActive=1&useRealtime=1"
//#define WEB_SERVER "mustbehax.de"
//#define WEB_PORT "443"
//#define WEB_URL "/web/reduced.json"
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";
esp_err_t https_get_request_using_crt_bundle(char* buf, size_t bufsize)
{
memset(buf, 0, bufsize);
ESP_LOGI(TAG, "https_request using crt bundle");
esp_tls_cfg_t cfg = {
.crt_bundle_attach = esp_crt_bundle_attach,
};
//https_get_request(cfg, "https://"WEB_SERVER""WEB_URL, HOWSMYSSL_REQUEST);
//char buf[47000];
int ret, len;
esp_tls_t *tls = esp_tls_init();
if (!tls) {
ESP_LOGE(TAG, "Failed to allocate esp_tls handle!");
return -1;
}
if (esp_tls_conn_http_new_sync("https://"WEB_SERVER""WEB_URL, &cfg, tls) == 1) {
ESP_LOGI(TAG, "Connection established...");
} else {
ESP_LOGE(TAG, "Connection failed...");
int esp_tls_code = 0, esp_tls_flags = 0;
esp_tls_error_handle_t tls_e = NULL;
esp_tls_get_error_handle(tls, &tls_e);
/* Try to get TLS stack level error and certificate failure flags, if any */
ret = esp_tls_get_and_clear_last_error(tls_e, &esp_tls_code, &esp_tls_flags);
if (ret == ESP_OK) {
ESP_LOGE(TAG, "TLS error = -0x%x, TLS flags = -0x%x", esp_tls_code, esp_tls_flags);
}
esp_tls_conn_destroy(tls);
return -2;
}
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
/* The TLS session is successfully established, now saving the session ctx for reuse */
if (save_client_session) {
esp_tls_free_client_session(tls_client_session);
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,
strlen(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));
esp_tls_conn_destroy(tls);
return -3;
}
} while (written_bytes < strlen(REQUEST));
uint32_t write_offset = 0;
memset(buf, 0x00, bufsize);
ESP_LOGI(TAG, "Reading HTTP response...");
do {
len = bufsize - write_offset - 1;
if(len > 6000)
len = 6000;
ret = esp_tls_conn_read(tls, &buf[write_offset], len);
if (ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ) {
continue;
} else if (ret < 0) {
ESP_LOGE(TAG, "esp_tls_conn_read returned [-0x%02X](%s)", -ret, esp_err_to_name(ret));
break;
} else if (ret == 0) {
ESP_LOGI(TAG, "connection closed");
break;
}
write_offset += ret;
len = ret;
ESP_LOGI(TAG, "%d bytes read", len);
//if(!strstr(buf, "departureList")){
// ESP_LOGI(TAG,"tossing ... strstr: %ld", (uint32_t)strstr(buf, "departureList"));
// write_offset=0;
//}else
// ESP_LOGI(TAG,"keeping ... strstr: %ld", (uint32_t)strstr(buf, "departureList"));
} while (1);
buf[write_offset+len+1] = 0;
esp_tls_conn_destroy(tls);
return 0;
///* Print response directly to stdout as it is read */
//for (int i = 0; i < sizeof(buf); i++) {
// putchar(buf[i]);
// if(i%100 == 0)
// vTaskDelay(1);
//}
//putchar('\n'); // JSON output doesn't have a newline at end
}