main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Persistent Sockets Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <esp_wifi.h>
  8. #include <esp_event.h>
  9. #include <esp_log.h>
  10. #include <esp_system.h>
  11. #include <nvs_flash.h>
  12. #include "esp_netif.h"
  13. #include "esp_eth.h"
  14. #include "protocol_examples_common.h"
  15. #include <esp_http_server.h>
  16. /* An example to demonstrate persistent sockets, with context maintained across
  17. * multiple requests on that socket.
  18. */
  19. static const char *TAG = "example";
  20. /* Function to free context */
  21. static void adder_free_func(void *ctx)
  22. {
  23. ESP_LOGI(TAG, "/adder Free Context function called");
  24. free(ctx);
  25. }
  26. /* This handler keeps accumulating data that is posted to it into a per
  27. * socket/session context. And returns the result.
  28. */
  29. static esp_err_t adder_post_handler(httpd_req_t *req)
  30. {
  31. /* Log total visitors */
  32. unsigned *visitors = (unsigned *)req->user_ctx;
  33. ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
  34. char buf[10];
  35. char outbuf[50];
  36. int ret;
  37. /* Read data received in the request */
  38. ret = httpd_req_recv(req, buf, sizeof(buf));
  39. if (ret <= 0) {
  40. if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  41. httpd_resp_send_408(req);
  42. }
  43. return ESP_FAIL;
  44. }
  45. buf[ret] = '\0';
  46. int val = atoi(buf);
  47. ESP_LOGI(TAG, "/adder handler read %d", val);
  48. /* Create session's context if not already available */
  49. if (! req->sess_ctx) {
  50. ESP_LOGI(TAG, "/adder allocating new session");
  51. req->sess_ctx = malloc(sizeof(int));
  52. req->free_ctx = adder_free_func;
  53. *(int *)req->sess_ctx = 0;
  54. }
  55. /* Add the received data to the context */
  56. int *adder = (int *)req->sess_ctx;
  57. *adder += val;
  58. /* Respond with the accumulated value */
  59. snprintf(outbuf, sizeof(outbuf),"%d", *adder);
  60. httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
  61. return ESP_OK;
  62. }
  63. /* This handler gets the present value of the accumulator */
  64. static esp_err_t adder_get_handler(httpd_req_t *req)
  65. {
  66. /* Log total visitors */
  67. unsigned *visitors = (unsigned *)req->user_ctx;
  68. ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
  69. char outbuf[50];
  70. /* Create session's context if not already available */
  71. if (! req->sess_ctx) {
  72. ESP_LOGI(TAG, "/adder GET allocating new session");
  73. req->sess_ctx = malloc(sizeof(int));
  74. req->free_ctx = adder_free_func;
  75. *(int *)req->sess_ctx = 0;
  76. }
  77. ESP_LOGI(TAG, "/adder GET handler send %d", *(int *)req->sess_ctx);
  78. /* Respond with the accumulated value */
  79. snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
  80. httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
  81. return ESP_OK;
  82. }
  83. #if CONFIG_EXAMPLE_SESSION_CTX_HANDLERS
  84. // login and logout handler are created to test the server functionality to delete the older sess_ctx if it is changed from another handler.
  85. // login handler creates a new sess_ctx
  86. static esp_err_t login_handler(httpd_req_t *req)
  87. {
  88. /* Log total visitors */
  89. unsigned *visitors = (unsigned *)req->user_ctx;
  90. ESP_LOGI(TAG, "/login visitor count = %d", ++(*visitors));
  91. char outbuf[50];
  92. /* Create session's context if not already available */
  93. if (! req->sess_ctx) {
  94. ESP_LOGI(TAG, "/login GET allocating new session");
  95. req->sess_ctx = malloc(sizeof(int));
  96. if (!req->sess_ctx) {
  97. return ESP_ERR_NO_MEM;
  98. }
  99. *(int *)req->sess_ctx = 1;
  100. }
  101. ESP_LOGI(TAG, "/login GET handler send %d", *(int *)req->sess_ctx);
  102. /* Respond with the accumulated value */
  103. snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
  104. httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
  105. return ESP_OK;
  106. }
  107. // This handler sets sess_ctx to NULL.
  108. static esp_err_t logout_handler(httpd_req_t *req)
  109. {
  110. ESP_LOGI(TAG, "Logging out");
  111. // Setting sess_ctx to NULL here. This is done to test the server functionality to free the older sesss_ctx if it is changed by some handler.
  112. req->sess_ctx = NULL;
  113. char outbuf[50];
  114. snprintf(outbuf, sizeof(outbuf),"%d", 1);
  115. httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
  116. return ESP_OK;
  117. }
  118. #endif // CONFIG_EXAMPLE_SESSION_CTX_HANDLERS
  119. /* This handler resets the value of the accumulator */
  120. static esp_err_t adder_put_handler(httpd_req_t *req)
  121. {
  122. /* Log total visitors */
  123. unsigned *visitors = (unsigned *)req->user_ctx;
  124. ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
  125. char buf[10];
  126. char outbuf[50];
  127. int ret;
  128. /* Read data received in the request */
  129. ret = httpd_req_recv(req, buf, sizeof(buf));
  130. if (ret <= 0) {
  131. if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  132. httpd_resp_send_408(req);
  133. }
  134. return ESP_FAIL;
  135. }
  136. buf[ret] = '\0';
  137. int val = atoi(buf);
  138. ESP_LOGI(TAG, "/adder PUT handler read %d", val);
  139. /* Create session's context if not already available */
  140. if (! req->sess_ctx) {
  141. ESP_LOGI(TAG, "/adder PUT allocating new session");
  142. req->sess_ctx = malloc(sizeof(int));
  143. req->free_ctx = adder_free_func;
  144. }
  145. *(int *)req->sess_ctx = val;
  146. /* Respond with the reset value */
  147. snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
  148. httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
  149. return ESP_OK;
  150. }
  151. /* Maintain a variable which stores the number of times
  152. * the "/adder" URI has been visited */
  153. static unsigned visitors = 0;
  154. static const httpd_uri_t adder_post = {
  155. .uri = "/adder",
  156. .method = HTTP_POST,
  157. .handler = adder_post_handler,
  158. .user_ctx = &visitors
  159. };
  160. static const httpd_uri_t adder_get = {
  161. .uri = "/adder",
  162. .method = HTTP_GET,
  163. .handler = adder_get_handler,
  164. .user_ctx = &visitors
  165. };
  166. #if CONFIG_EXAMPLE_SESSION_CTX_HANDLERS
  167. static const httpd_uri_t login = {
  168. .uri = "/login",
  169. .method = HTTP_GET,
  170. .handler = login_handler,
  171. .user_ctx = &visitors
  172. };
  173. static const httpd_uri_t logout = {
  174. .uri = "/logout",
  175. .method = HTTP_GET,
  176. .handler = logout_handler,
  177. .user_ctx = &visitors
  178. };
  179. #endif // CONFIG_EXAMPLE_SESSION_CTX_HANDLERS
  180. static const httpd_uri_t adder_put = {
  181. .uri = "/adder",
  182. .method = HTTP_PUT,
  183. .handler = adder_put_handler,
  184. .user_ctx = &visitors
  185. };
  186. static httpd_handle_t start_webserver(void)
  187. {
  188. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  189. // Start the httpd server
  190. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  191. httpd_handle_t server;
  192. if (httpd_start(&server, &config) == ESP_OK) {
  193. // Set URI handlers
  194. ESP_LOGI(TAG, "Registering URI handlers");
  195. httpd_register_uri_handler(server, &adder_get);
  196. #if CONFIG_EXAMPLE_SESSION_CTX_HANDLERS
  197. httpd_register_uri_handler(server, &login);
  198. httpd_register_uri_handler(server, &logout);
  199. #endif // CONFIG_EXAMPLE_SESSION_CTX_HANDLERS
  200. httpd_register_uri_handler(server, &adder_put);
  201. httpd_register_uri_handler(server, &adder_post);
  202. return server;
  203. }
  204. ESP_LOGI(TAG, "Error starting server!");
  205. return NULL;
  206. }
  207. static esp_err_t stop_webserver(httpd_handle_t server)
  208. {
  209. // Stop the httpd server
  210. return httpd_stop(server);
  211. }
  212. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  213. int32_t event_id, void* event_data)
  214. {
  215. httpd_handle_t* server = (httpd_handle_t*) arg;
  216. if (*server) {
  217. ESP_LOGI(TAG, "Stopping webserver");
  218. if (stop_webserver(*server) == ESP_OK) {
  219. *server = NULL;
  220. } else {
  221. ESP_LOGE(TAG, "Failed to stop http server");
  222. }
  223. }
  224. }
  225. static void connect_handler(void* arg, esp_event_base_t event_base,
  226. int32_t event_id, void* event_data)
  227. {
  228. httpd_handle_t* server = (httpd_handle_t*) arg;
  229. if (*server == NULL) {
  230. ESP_LOGI(TAG, "Starting webserver");
  231. *server = start_webserver();
  232. }
  233. }
  234. void app_main(void)
  235. {
  236. static httpd_handle_t server = NULL;
  237. ESP_ERROR_CHECK(nvs_flash_init());
  238. ESP_ERROR_CHECK(esp_netif_init());
  239. ESP_ERROR_CHECK(esp_event_loop_create_default());
  240. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  241. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  242. * examples/protocols/README.md for more information about this function.
  243. */
  244. ESP_ERROR_CHECK(example_connect());
  245. /* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
  246. * and re-start it upon connection.
  247. */
  248. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  249. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  250. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  251. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  252. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  253. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  254. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  255. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  256. /* Start the server for the first time */
  257. server = start_webserver();
  258. }