main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_loop.h>
  9. #include <esp_log.h>
  10. #include <esp_system.h>
  11. #include <nvs_flash.h>
  12. #include "tcpip_adapter.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, strlen(outbuf));
  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, strlen(outbuf));
  81. return ESP_OK;
  82. }
  83. /* This handler resets the value of the accumulator */
  84. static esp_err_t adder_put_handler(httpd_req_t *req)
  85. {
  86. /* Log total visitors */
  87. unsigned *visitors = (unsigned *)req->user_ctx;
  88. ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
  89. char buf[10];
  90. char outbuf[50];
  91. int ret;
  92. /* Read data received in the request */
  93. ret = httpd_req_recv(req, buf, sizeof(buf));
  94. if (ret <= 0) {
  95. if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  96. httpd_resp_send_408(req);
  97. }
  98. return ESP_FAIL;
  99. }
  100. buf[ret] = '\0';
  101. int val = atoi(buf);
  102. ESP_LOGI(TAG, "/adder PUT handler read %d", val);
  103. /* Create session's context if not already available */
  104. if (! req->sess_ctx) {
  105. ESP_LOGI(TAG, "/adder PUT allocating new session");
  106. req->sess_ctx = malloc(sizeof(int));
  107. req->free_ctx = adder_free_func;
  108. }
  109. *(int *)req->sess_ctx = val;
  110. /* Respond with the reset value */
  111. snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
  112. httpd_resp_send(req, outbuf, strlen(outbuf));
  113. return ESP_OK;
  114. }
  115. /* Maintain a variable which stores the number of times
  116. * the "/adder" URI has been visited */
  117. static unsigned visitors = 0;
  118. static const httpd_uri_t adder_post = {
  119. .uri = "/adder",
  120. .method = HTTP_POST,
  121. .handler = adder_post_handler,
  122. .user_ctx = &visitors
  123. };
  124. static const httpd_uri_t adder_get = {
  125. .uri = "/adder",
  126. .method = HTTP_GET,
  127. .handler = adder_get_handler,
  128. .user_ctx = &visitors
  129. };
  130. static const httpd_uri_t adder_put = {
  131. .uri = "/adder",
  132. .method = HTTP_PUT,
  133. .handler = adder_put_handler,
  134. .user_ctx = &visitors
  135. };
  136. static httpd_handle_t start_webserver(void)
  137. {
  138. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  139. // Start the httpd server
  140. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  141. httpd_handle_t server;
  142. if (httpd_start(&server, &config) == ESP_OK) {
  143. // Set URI handlers
  144. ESP_LOGI(TAG, "Registering URI handlers");
  145. httpd_register_uri_handler(server, &adder_get);
  146. httpd_register_uri_handler(server, &adder_put);
  147. httpd_register_uri_handler(server, &adder_post);
  148. return server;
  149. }
  150. ESP_LOGI(TAG, "Error starting server!");
  151. return NULL;
  152. }
  153. static void stop_webserver(httpd_handle_t server)
  154. {
  155. // Stop the httpd server
  156. httpd_stop(server);
  157. }
  158. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  159. int32_t event_id, void* event_data)
  160. {
  161. httpd_handle_t* server = (httpd_handle_t*) arg;
  162. if (*server) {
  163. ESP_LOGI(TAG, "Stopping webserver");
  164. stop_webserver(*server);
  165. *server = NULL;
  166. }
  167. }
  168. static void connect_handler(void* arg, esp_event_base_t event_base,
  169. int32_t event_id, void* event_data)
  170. {
  171. httpd_handle_t* server = (httpd_handle_t*) arg;
  172. if (*server == NULL) {
  173. ESP_LOGI(TAG, "Starting webserver");
  174. *server = start_webserver();
  175. }
  176. }
  177. void app_main()
  178. {
  179. static httpd_handle_t server = NULL;
  180. ESP_ERROR_CHECK(nvs_flash_init());
  181. tcpip_adapter_init();
  182. ESP_ERROR_CHECK(esp_event_loop_create_default());
  183. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  184. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  185. * examples/protocols/README.md for more information about this function.
  186. */
  187. ESP_ERROR_CHECK(example_connect());
  188. /* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
  189. * and re-start it upon connection.
  190. */
  191. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  192. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  193. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  194. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  195. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  196. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  197. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  198. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  199. /* Start the server for the first time */
  200. server = start_webserver();
  201. }