scheme_generic_httpd.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "sdkconfig.h"
  7. #include <esp_log.h>
  8. #include <esp_err.h>
  9. #include <wifi_provisioning/manager.h>
  10. #include <protocomm.h>
  11. #include <protocomm_httpd.h>
  12. static const char *TAG = "wifi_prov_scheme_httpd";
  13. static esp_err_t prov_start(protocomm_t *pc, void *config)
  14. {
  15. if (!pc) {
  16. ESP_LOGE(TAG, "Protocomm handle cannot be null");
  17. return ESP_ERR_INVALID_ARG;
  18. }
  19. if (!config) {
  20. ESP_LOGE(TAG, "Cannot start with null configuration");
  21. return ESP_ERR_INVALID_ARG;
  22. }
  23. protocomm_httpd_config_t default_config = {
  24. .data = {
  25. .config = PROTOCOMM_HTTPD_DEFAULT_CONFIG()
  26. }
  27. };
  28. /* Start protocomm server on top of HTTP */
  29. esp_err_t err = protocomm_httpd_start(pc, &default_config);
  30. if (err != ESP_OK) {
  31. ESP_LOGE(TAG, "Failed to start protocomm HTTP server");
  32. return err;
  33. }
  34. return ESP_OK;
  35. }
  36. static esp_err_t prov_stop(protocomm_t *pc)
  37. {
  38. esp_err_t err = protocomm_httpd_stop(pc);
  39. if (err != ESP_OK) {
  40. ESP_LOGW(TAG, "Error occurred while stopping protocomm_httpd");
  41. }
  42. return err;
  43. }
  44. /**
  45. * @brief Creates a configuration for this custom provisioning scheme.
  46. *
  47. * We don't need to pass any config option at this moment, so we create
  48. * a dummy configuration since provisioning manager check for non-nullptr.
  49. * If needed we can extend this scheme to provide some options for httpd
  50. * or wifi provisioning.
  51. */
  52. static void *new_config(void)
  53. {
  54. return (void *)1;
  55. }
  56. static void delete_config(void *config)
  57. {
  58. }
  59. static esp_err_t set_config_service(void *config, const char *service_name, const char *service_key)
  60. {
  61. return ESP_OK;
  62. }
  63. static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, uint16_t uuid)
  64. {
  65. return ESP_OK;
  66. }
  67. /**
  68. * @brief Creating a generic HTTPD scheme
  69. */
  70. const wifi_prov_scheme_t wifi_prov_scheme_httpd = {
  71. .prov_start = prov_start,
  72. .prov_stop = prov_stop,
  73. .new_config = new_config,
  74. .delete_config = delete_config,
  75. .set_config_service = set_config_service,
  76. .set_config_endpoint = set_config_endpoint,
  77. .wifi_mode = WIFI_MODE_STA
  78. };