manual_config.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <string.h>
  7. #include "esp_wifi.h"
  8. #include "esp_log.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_http_server.h"
  12. #include "dns_server.h"
  13. static const char *TAG = "NCM_configuration";
  14. static httpd_handle_t s_web_server = NULL;
  15. static EventGroupHandle_t *s_flags = NULL;
  16. static int s_success_bit;
  17. bool is_provisioned(void)
  18. {
  19. wifi_config_t wifi_cfg;
  20. if (esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) != ESP_OK) {
  21. return false;
  22. }
  23. if (strlen((const char *) wifi_cfg.sta.ssid)) {
  24. return true;
  25. }
  26. return false;
  27. }
  28. static esp_err_t http_get_handler(httpd_req_t *req)
  29. {
  30. const char page[] = "<form action=\"/\" method=\"get\"><br><br>\n"
  31. "SSID: <input type=\"text\" id=\"ssid\" name=\"ssid\"><br><br>\n"
  32. "Password: <input type=\"text\" id=\"password\" name=\"password\"><br><br>\n"
  33. " <input type=\"submit\" value=\"Connect\">"
  34. "</form>";
  35. char *buf = NULL;
  36. size_t buf_len;
  37. buf_len = httpd_req_get_url_query_len(req) + 1;
  38. if (buf_len > 1) {
  39. buf = malloc(buf_len);
  40. if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
  41. ESP_LOGI(TAG, "Found URL query => %s", buf);
  42. char param[32];
  43. wifi_config_t wifi_cfg = {};
  44. if (httpd_query_key_value(buf, "ssid", param, sizeof(param)) == ESP_OK) {
  45. ESP_LOGI(TAG, "ssid=%s", param);
  46. strncpy((char *)wifi_cfg.sta.ssid, param, sizeof(wifi_cfg.sta.ssid));
  47. }
  48. if (httpd_query_key_value(buf, "password", param, sizeof(param)) == ESP_OK) {
  49. ESP_LOGI(TAG, "password=%s", param);
  50. strncpy((char *)wifi_cfg.sta.password, param, sizeof(wifi_cfg.sta.password));
  51. }
  52. if (strlen((char *)wifi_cfg.sta.ssid) > 0 && strlen((char *)wifi_cfg.sta.password)) {
  53. httpd_resp_set_type(req, "text/html");
  54. esp_wifi_set_mode(WIFI_MODE_STA);
  55. if (esp_wifi_set_storage(WIFI_STORAGE_FLASH) == ESP_OK &&
  56. esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg) == ESP_OK) {
  57. const char wifi_configured[] = "<h1>Connecting...</h1>";
  58. ESP_LOGI(TAG, "WiFi settings accepted!");
  59. httpd_resp_send(req, wifi_configured, strlen(wifi_configured));
  60. } else {
  61. const char wifi_config_failed[] = "<h1>Failed to configure WiFi settings</h1>";
  62. ESP_LOGE(TAG, "Failed to set WiFi config to flash");
  63. httpd_resp_send(req, wifi_config_failed, strlen(wifi_config_failed));
  64. }
  65. free(buf);
  66. if (s_flags) {
  67. xEventGroupSetBits(*s_flags, s_success_bit);
  68. }
  69. return ESP_OK;
  70. }
  71. }
  72. free(buf);
  73. }
  74. httpd_resp_set_type(req, "text/html");
  75. httpd_resp_send(req, page, sizeof(page));
  76. return ESP_OK;
  77. }
  78. static const httpd_uri_t root = {
  79. .uri = "/",
  80. .method = HTTP_GET,
  81. .handler = http_get_handler,
  82. };
  83. static void start_webserver(void)
  84. {
  85. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  86. config.max_open_sockets = 3;
  87. config.lru_purge_enable = true;
  88. // Start the httpd server
  89. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  90. if (httpd_start(&s_web_server, &config) == ESP_OK) {
  91. // Set URI handlers
  92. ESP_LOGI(TAG, "Registering URI handlers");
  93. httpd_register_uri_handler(s_web_server, &root);
  94. }
  95. }
  96. esp_err_t start_provisioning(EventGroupHandle_t *flags, int success_bit, int fail_bit)
  97. {
  98. start_webserver();
  99. // Start the DNS server that will reply to "wifi.settings" with "usb" network interface address
  100. dns_server_config_t config = DNS_SERVER_CONFIG_SINGLE("wifi.settings" /* name */, "wired" /* USB netif ID */);
  101. start_dns_server(&config);
  102. s_flags = flags;
  103. s_success_bit = success_bit;
  104. return ESP_OK;
  105. }