protocomm_httpd.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <protocomm.h>
  16. #define PROTOCOMM_HTTPD_DEFAULT_CONFIG() { \
  17. .port = 80, \
  18. .stack_size = 4096, \
  19. .task_priority = tskIDLE_PRIORITY + 5, \
  20. }
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * @brief Config parameters for protocomm HTTP server
  26. */
  27. typedef struct {
  28. uint16_t port; /*!< Port on which the HTTP server will listen */
  29. /**
  30. * Stack size of server task, adjusted depending
  31. * upon stack usage of endpoint handler
  32. */
  33. size_t stack_size;
  34. unsigned task_priority; /*!< Priority of server task */
  35. } protocomm_http_server_config_t; /*!< HTTP Server Configuration, if HTTP Server has not been started already */
  36. /** Protocomm HTTPD Configuration Data
  37. */
  38. typedef union {
  39. /** HTTP Server Handle, if ext_handle_provided is set to true
  40. */
  41. void *handle;
  42. /** HTTP Server Configuration, if a server is not already active
  43. */
  44. protocomm_http_server_config_t config;
  45. } protocomm_httpd_config_data_t;
  46. /**
  47. * @brief Config parameters for protocomm HTTP server
  48. */
  49. typedef struct {
  50. /** Flag to indicate of an external HTTP Server Handle has been provided.
  51. * In such as case, protocomm will use the same HTTP Server and not start
  52. * a new one internally.
  53. */
  54. bool ext_handle_provided;
  55. /** Protocomm HTTPD Configuration Data */
  56. protocomm_httpd_config_data_t data;
  57. } protocomm_httpd_config_t;
  58. /**
  59. * @brief Start HTTPD protocomm transport
  60. *
  61. * This API internally creates a framework to allow endpoint registration and security
  62. * configuration for the protocomm.
  63. *
  64. * @note This is a singleton. ie. Protocomm can have multiple instances, but only
  65. * one instance can be bound to an HTTP transport layer.
  66. *
  67. * @param[in] pc Protocomm instance pointer obtained from protocomm_new()
  68. * @param[in] config Pointer to config structure for initializing HTTP server
  69. *
  70. * @return
  71. * - ESP_OK : Success
  72. * - ESP_ERR_INVALID_ARG : Null arguments
  73. * - ESP_ERR_NOT_SUPPORTED : Transport layer bound to another protocomm instance
  74. * - ESP_ERR_INVALID_STATE : Transport layer already bound to this protocomm instance
  75. * - ESP_ERR_NO_MEM : Memory allocation for server resource failed
  76. * - ESP_ERR_HTTPD_* : HTTP server error on start
  77. */
  78. esp_err_t protocomm_httpd_start(protocomm_t *pc, const protocomm_httpd_config_t *config);
  79. /**
  80. * @brief Stop HTTPD protocomm transport
  81. *
  82. * This API cleans up the HTTPD transport protocomm and frees all the handlers registered
  83. * with the protocomm.
  84. *
  85. * @param[in] pc Same protocomm instance that was passed to protocomm_httpd_start()
  86. *
  87. * @return
  88. * - ESP_OK : Success
  89. * - ESP_ERR_INVALID_ARG : Null / incorrect protocomm instance pointer
  90. */
  91. esp_err_t protocomm_httpd_stop(protocomm_t *pc);
  92. #ifdef __cplusplus
  93. }
  94. #endif