2016-09-21 02:44:27 -04:00
|
|
|
/* HTTPS GET Example using plain mbedTLS sockets
|
|
|
|
*
|
|
|
|
* Contacts the howsmyssl.com API via TLS v1.2 and reads a JSON
|
|
|
|
* response.
|
|
|
|
*
|
|
|
|
* Adapted from the ssl_client1 example in mbedtls.
|
|
|
|
*
|
|
|
|
* Original Copyright (C) 2006-2016, ARM Limited, All Rights Reserved, Apache 2.0 License.
|
|
|
|
* Additions Copyright (C) Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD, Apache 2.0 License.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
2016-11-23 16:08:09 -05:00
|
|
|
#include <stdlib.h>
|
2016-09-21 02:44:27 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2016-09-26 01:48:36 -04:00
|
|
|
#include "freertos/event_groups.h"
|
2016-09-21 02:44:27 -04:00
|
|
|
#include "esp_wifi.h"
|
2019-04-11 06:54:26 -04:00
|
|
|
#include "esp_event.h"
|
2016-09-21 02:44:27 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
2016-09-26 21:16:40 -04:00
|
|
|
#include "nvs_flash.h"
|
2019-04-11 06:54:26 -04:00
|
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include "tcpip_adapter.h"
|
2016-09-21 02:44:27 -04:00
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/netdb.h"
|
|
|
|
#include "lwip/dns.h"
|
|
|
|
|
2018-02-14 08:21:26 -05:00
|
|
|
#include "esp_tls.h"
|
2018-02-12 13:11:31 -05:00
|
|
|
|
2016-09-21 02:44:27 -04:00
|
|
|
/* Constants that aren't configurable in menuconfig */
|
|
|
|
#define WEB_SERVER "www.howsmyssl.com"
|
|
|
|
#define WEB_PORT "443"
|
|
|
|
#define WEB_URL "https://www.howsmyssl.com/a/check"
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
2017-04-11 06:37:16 -04:00
|
|
|
static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
|
|
|
|
"Host: "WEB_SERVER"\r\n"
|
|
|
|
"User-Agent: esp-idf/1.0 esp32\r\n"
|
|
|
|
"\r\n";
|
2016-09-21 02:44:27 -04:00
|
|
|
|
2016-10-05 21:51:47 -04:00
|
|
|
/* Root cert for howsmyssl.com, taken from server_root_cert.pem
|
|
|
|
|
|
|
|
The PEM file was extracted from the output of this command:
|
|
|
|
openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
|
|
|
|
|
|
|
|
The CA root cert is the last cert given in the chain of certs.
|
|
|
|
|
|
|
|
To embed it in the app binary, the PEM file is named
|
|
|
|
in the component.mk COMPONENT_EMBED_TXTFILES variable.
|
|
|
|
*/
|
|
|
|
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");
|
2016-09-21 02:44:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
static void https_get_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
char buf[512];
|
2018-02-12 13:11:31 -05:00
|
|
|
int ret, len;
|
2016-09-21 02:44:27 -04:00
|
|
|
|
|
|
|
while(1) {
|
2018-02-14 04:45:50 -05:00
|
|
|
esp_tls_cfg_t cfg = {
|
2019-08-02 03:20:02 -04:00
|
|
|
.cacert_buf = server_root_cert_pem_start,
|
|
|
|
.cacert_bytes = server_root_cert_pem_end - server_root_cert_pem_start,
|
2018-02-12 13:11:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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...");
|
2018-02-26 05:34:47 -05:00
|
|
|
goto exit;
|
2016-09-21 02:44:27 -04:00
|
|
|
}
|
2018-02-12 13:11:31 -05:00
|
|
|
|
2017-08-18 00:27:36 -04:00
|
|
|
size_t written_bytes = 0;
|
|
|
|
do {
|
2018-02-12 13:11:31 -05:00
|
|
|
ret = esp_tls_conn_write(tls,
|
2018-02-14 04:45:50 -05:00
|
|
|
REQUEST + written_bytes,
|
2018-02-12 13:11:31 -05:00
|
|
|
strlen(REQUEST) - written_bytes);
|
2017-08-18 00:27:36 -04:00
|
|
|
if (ret >= 0) {
|
|
|
|
ESP_LOGI(TAG, "%d bytes written", ret);
|
|
|
|
written_bytes += ret;
|
2018-02-28 07:39:43 -05:00
|
|
|
} else if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2018-02-14 04:45:50 -05:00
|
|
|
ESP_LOGE(TAG, "esp_tls_conn_write returned 0x%x", ret);
|
2016-09-21 02:44:27 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
2017-08-18 00:27:36 -04:00
|
|
|
} while(written_bytes < strlen(REQUEST));
|
2016-09-21 02:44:27 -04:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Reading HTTP response...");
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
bzero(buf, sizeof(buf));
|
2018-02-12 13:11:31 -05:00
|
|
|
ret = esp_tls_conn_read(tls, (char *)buf, len);
|
|
|
|
|
2018-02-28 07:39:43 -05:00
|
|
|
if(ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ)
|
2016-09-21 02:44:27 -04:00
|
|
|
continue;
|
2018-02-14 04:45:50 -05:00
|
|
|
|
2016-09-21 02:44:27 -04:00
|
|
|
if(ret < 0)
|
2018-02-28 07:39:43 -05:00
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "esp_tls_conn_read returned -0x%x", -ret);
|
2016-09-21 02:44:27 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret == 0)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "connection closed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
2017-08-17 03:25:39 -04:00
|
|
|
ESP_LOGD(TAG, "%d bytes read", len);
|
2016-09-21 02:44:27 -04:00
|
|
|
/* Print response directly to stdout as it is read */
|
|
|
|
for(int i = 0; i < len; i++) {
|
|
|
|
putchar(buf[i]);
|
|
|
|
}
|
|
|
|
} while(1);
|
|
|
|
|
|
|
|
exit:
|
2018-02-12 13:11:31 -05:00
|
|
|
esp_tls_conn_delete(tls);
|
2017-08-17 03:25:39 -04:00
|
|
|
putchar('\n'); // JSON output doesn't have a newline at end
|
|
|
|
|
|
|
|
static int request_count;
|
|
|
|
ESP_LOGI(TAG, "Completed %d requests", ++request_count);
|
|
|
|
|
2016-09-21 02:44:27 -04:00
|
|
|
for(int countdown = 10; countdown >= 0; countdown--) {
|
|
|
|
ESP_LOGI(TAG, "%d...", countdown);
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-09-21 02:44:27 -04:00
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Starting again!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2016-09-21 02:44:27 -04:00
|
|
|
{
|
2017-03-14 09:39:44 -04:00
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2019-04-11 06:54:26 -04:00
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
2016-09-21 02:44:27 -04:00
|
|
|
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
|
|
|
|
}
|