main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Simple HTTP + SSL Server 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 <esp_wifi.h>
  8. #include <esp_event.h>
  9. #include <esp_log.h>
  10. #include <esp_system.h>
  11. #include <nvs_flash.h>
  12. #include <sys/param.h>
  13. #include "esp_netif.h"
  14. #include "esp_eth.h"
  15. #include "protocol_examples_common.h"
  16. #include <esp_https_server.h>
  17. #include "esp_tls.h"
  18. /* A simple example that demonstrates how to create GET and POST
  19. * handlers and start an HTTPS server.
  20. */
  21. static const char *TAG = "example";
  22. /* An HTTP GET handler */
  23. static esp_err_t root_get_handler(httpd_req_t *req)
  24. {
  25. httpd_resp_set_type(req, "text/html");
  26. httpd_resp_send(req, "<h1>Hello Secure World!</h1>", HTTPD_RESP_USE_STRLEN);
  27. return ESP_OK;
  28. }
  29. #if CONFIG_EXAMPLE_ENABLE_HTTPS_USER_CALLBACK
  30. /**
  31. * Example callback function to get the certificate of connected clients,
  32. * whenever a new SSL connection is created
  33. *
  34. * Can also be used to other information like Socket FD, Connection state, etc.
  35. */
  36. void https_server_user_callback(esp_https_server_user_cb_arg_t *user_cb)
  37. {
  38. ESP_LOGI(TAG, "Session Created!");
  39. const mbedtls_x509_crt *cert;
  40. const size_t buf_size = 1024;
  41. char *buf = calloc(buf_size, sizeof(char));
  42. if (buf == NULL) {
  43. ESP_LOGE(TAG, "Out of memory - Callback execution failed!");
  44. return;
  45. }
  46. cert = mbedtls_ssl_get_peer_cert(&user_cb->tls->ssl);
  47. if (cert != NULL) {
  48. mbedtls_x509_crt_info((char *) buf, buf_size - 1, " ", cert);
  49. ESP_LOGI(TAG, "Peer certificate info:\n%s", buf);
  50. } else {
  51. ESP_LOGW(TAG, "Could not obtain the peer certificate!");
  52. }
  53. free(buf);
  54. }
  55. #endif
  56. static const httpd_uri_t root = {
  57. .uri = "/",
  58. .method = HTTP_GET,
  59. .handler = root_get_handler
  60. };
  61. static httpd_handle_t start_webserver(void)
  62. {
  63. httpd_handle_t server = NULL;
  64. // Start the httpd server
  65. ESP_LOGI(TAG, "Starting server");
  66. httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
  67. extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
  68. extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
  69. conf.cacert_pem = cacert_pem_start;
  70. conf.cacert_len = cacert_pem_end - cacert_pem_start;
  71. extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
  72. extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
  73. conf.prvtkey_pem = prvtkey_pem_start;
  74. conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
  75. #if CONFIG_EXAMPLE_ENABLE_HTTPS_USER_CALLBACK
  76. conf.user_cb = https_server_user_callback;
  77. #endif
  78. esp_err_t ret = httpd_ssl_start(&server, &conf);
  79. if (ESP_OK != ret) {
  80. ESP_LOGI(TAG, "Error starting server!");
  81. return NULL;
  82. }
  83. // Set URI handlers
  84. ESP_LOGI(TAG, "Registering URI handlers");
  85. httpd_register_uri_handler(server, &root);
  86. return server;
  87. }
  88. static void stop_webserver(httpd_handle_t server)
  89. {
  90. // Stop the httpd server
  91. httpd_ssl_stop(server);
  92. }
  93. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  94. int32_t event_id, void* event_data)
  95. {
  96. httpd_handle_t* server = (httpd_handle_t*) arg;
  97. if (*server) {
  98. stop_webserver(*server);
  99. *server = NULL;
  100. }
  101. }
  102. static void connect_handler(void* arg, esp_event_base_t event_base,
  103. int32_t event_id, void* event_data)
  104. {
  105. httpd_handle_t* server = (httpd_handle_t*) arg;
  106. if (*server == NULL) {
  107. *server = start_webserver();
  108. }
  109. }
  110. void app_main(void)
  111. {
  112. static httpd_handle_t server = NULL;
  113. ESP_ERROR_CHECK(nvs_flash_init());
  114. ESP_ERROR_CHECK(esp_netif_init());
  115. ESP_ERROR_CHECK(esp_event_loop_create_default());
  116. /* Register event handlers to start server when Wi-Fi or Ethernet is connected,
  117. * and stop server when disconnection happens.
  118. */
  119. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  120. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  121. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  122. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  123. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  124. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  125. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  126. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  127. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  128. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  129. * examples/protocols/README.md for more information about this function.
  130. */
  131. ESP_ERROR_CHECK(example_connect());
  132. }