_esp_http_server.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef __ESP_HTTP_SERVER_H_
  15. #define __ESP_HTTP_SERVER_H_
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <freertos/FreeRTOS.h>
  19. #include <freertos/task.h>
  20. #include <http_parser.h>
  21. #include <sdkconfig.h>
  22. #include <esp_err.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * @brief Starts the web server
  28. *
  29. * Create an instance of HTTP server and allocate memory/resources for it
  30. * depending upon the specified configuration.
  31. *
  32. * Example usage:
  33. * @code{c}
  34. *
  35. * //Function for starting the webserver
  36. * httpd_handle_t start_webserver(void)
  37. * {
  38. * // Generate default configuration
  39. * httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  40. *
  41. * // Empty handle to http_server
  42. * httpd_handle_t server = NULL;
  43. *
  44. * // Start the httpd server
  45. * if (httpd_start(&server, &config) == ESP_OK) {
  46. * // Register URI handlers
  47. * httpd_register_uri_handler(server, &uri_get);
  48. * httpd_register_uri_handler(server, &uri_post);
  49. * }
  50. * // If server failed to start, handle will be NULL
  51. * return server;
  52. * }
  53. *
  54. * @endcode
  55. *
  56. * @param[in] config Configuration for new instance of the server
  57. * @param[out] handle Handle to newly created instance of the server. NULL on error
  58. * @return
  59. * - ESP_OK : Instance created successfully
  60. * - ESP_ERR_INVALID_ARG : Null argument(s)
  61. * - ESP_ERR_HTTPD_ALLOC_MEM : Failed to allocate memory for instance
  62. * - ESP_ERR_HTTPD_TASK : Failed to launch server task
  63. */
  64. esp_err_t __httpd_start(httpd_handle_t *handle, const httpd_config_t *config);
  65. static inline int __httpd_os_thread_create_static(TaskHandle_t *thread,
  66. const char *name, uint16_t stacksize, int prio,
  67. void (*thread_routine)(void *arg), void *arg,
  68. BaseType_t core_id)
  69. {
  70. StaticTask_t *xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
  71. StackType_t *xStack = heap_caps_malloc(stacksize,(MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT));
  72. //
  73. *thread = xTaskCreateStaticPinnedToCore(thread_routine, name, stacksize, arg, prio, xStack,xTaskBuffer,core_id);
  74. if (*thread) {
  75. return ESP_OK;
  76. }
  77. return ESP_FAIL;
  78. }
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif /* ! _ESP_HTTP_SERVER_H_ */