http_server_handlers.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright (c) 2017-2021 Sebastien L
  3. */
  4. #ifndef HTTP_SERVER_H_INCLUDED
  5. #define HTTP_SERVER_H_INCLUDED
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include "esp_http_server.h"
  11. #include "network_manager.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/event_groups.h"
  15. #include "esp_wifi.h"
  16. #include <esp_event.h>
  17. #include "nvs_flash.h"
  18. #include "esp_log.h"
  19. #include "driver/gpio.h"
  20. #include "mdns.h"
  21. #include "lwip/api.h"
  22. #include "lwip/err.h"
  23. #include "lwip/netdb.h"
  24. #include "lwip/opt.h"
  25. #include "lwip/memp.h"
  26. #include "lwip/ip.h"
  27. #include "lwip/raw.h"
  28. #include "lwip/udp.h"
  29. #include "lwip/priv/api_msg.h"
  30. #include "lwip/priv/tcp_priv.h"
  31. #include "lwip/priv/tcpip_priv.h"
  32. #include "esp_vfs.h"
  33. #include "esp_console.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #define ESP_LOGE_LOC(t,str, ...) ESP_LOGE(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  38. #define ESP_LOGI_LOC(t,str, ...) ESP_LOGI(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  39. #define ESP_LOGD_LOC(t,str, ...) ESP_LOGD(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  40. #define ESP_LOGW_LOC(t,str, ...) ESP_LOGW(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  41. #define ESP_LOGV_LOC(t,str, ...) ESP_LOGV(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  42. esp_err_t root_get_handler(httpd_req_t *req);
  43. esp_err_t resource_filehandler(httpd_req_t *req);
  44. esp_err_t ap_get_handler(httpd_req_t *req);
  45. esp_err_t config_get_handler(httpd_req_t *req);
  46. esp_err_t config_post_handler(httpd_req_t *req);
  47. esp_err_t connect_post_handler(httpd_req_t *req);
  48. esp_err_t connect_delete_handler(httpd_req_t *req);
  49. esp_err_t reboot_ota_post_handler(httpd_req_t *req);
  50. esp_err_t reboot_post_handler(httpd_req_t *req);
  51. esp_err_t recovery_post_handler(httpd_req_t *req);
  52. esp_err_t flash_post_handler(httpd_req_t *req);
  53. esp_err_t status_get_handler(httpd_req_t *req);
  54. esp_err_t messages_get_handler(httpd_req_t *req);
  55. esp_err_t console_cmd_get_handler(httpd_req_t *req);
  56. esp_err_t console_cmd_post_handler(httpd_req_t *req);
  57. esp_err_t ap_scan_handler(httpd_req_t *req);
  58. esp_err_t redirect_ev_handler(httpd_req_t *req);
  59. esp_err_t redirect_200_ev_handler(httpd_req_t *req);
  60. esp_err_t err_handler(httpd_req_t *req, httpd_err_code_t error);
  61. #define SCRATCH_BUFSIZE (10240)
  62. #define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
  63. typedef struct rest_server_context {
  64. char base_path[ESP_VFS_PATH_MAX + 1];
  65. char scratch[SCRATCH_BUFSIZE];
  66. } rest_server_context_t;
  67. /**
  68. * @brief RTOS task for the HTTP server. Do not start manually.
  69. * @see void http_server_start()
  70. */
  71. void http_server(void *pvParameters);
  72. /* @brief helper function that processes one HTTP request at a time */
  73. void http_server_netconn_serve(struct netconn *conn);
  74. /* @brief create the task for the http server */
  75. esp_err_t http_server_start();
  76. /**
  77. * @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
  78. *
  79. * For optimization purposes, no local copy is made. memcpy can then be used in coordination with len to extract the
  80. * data.
  81. *
  82. * @param request the full HTTP raw request.
  83. * @param header_name the header that is being searched.
  84. * @param len the size of the header value if found.
  85. * @return pointer to the beginning of the header value.
  86. */
  87. char* http_server_get_header(char *request, char *header_name, int *len);
  88. void strreplace(char *src, char *str, char *rep);
  89. /* @brief lock the json config object */
  90. bool http_server_lock_json_object(TickType_t xTicksToWait);
  91. /* @brief unlock the json config object */
  92. void http_server_unlock_json_object();
  93. httpd_handle_t http_get_server(int *port);
  94. #define PROTECTED_JSON_CALL(a) if(http_server_lock_json_object( portMAX_DELAY )){ \ a; http_server_unlocklock_json_object(); } else{ ESP_LOGE(TAG, "could not get access to json mutex in wifi_scan"); }
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif