coap_server_example_main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* CoAP server 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 <string.h>
  8. #include <sys/socket.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/event_groups.h"
  12. #include "esp_log.h"
  13. #include "esp_wifi.h"
  14. #include "esp_event_loop.h"
  15. #include "nvs_flash.h"
  16. #include "coap.h"
  17. /* The examples use simple WiFi configuration that you can set via
  18. 'make menuconfig'.
  19. If you'd rather not, just change the below entries to strings with
  20. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  21. */
  22. #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
  23. #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
  24. /* Set this to 9 to get verbose logging from within libcoap */
  25. #define COAP_LOGGING_LEVEL 0
  26. static EventGroupHandle_t wifi_event_group;
  27. /* The event group allows multiple bits for each event,
  28. but we only care about one event - are we connected
  29. to the AP with an IP? */
  30. const static int CONNECTED_BIT = BIT0;
  31. const static char *TAG = "CoAP_server";
  32. static char espressif_data[100];
  33. static int espressif_data_len = 0;
  34. /*
  35. * The resource handler
  36. */
  37. static void
  38. hnd_espressif_get(coap_context_t *ctx, coap_resource_t *resource,
  39. coap_session_t *session,
  40. coap_pdu_t *request, coap_binary_t *token,
  41. coap_string_t *query, coap_pdu_t *response)
  42. {
  43. coap_add_data_blocked_response(resource, session, request, response, token,
  44. COAP_MEDIATYPE_TEXT_PLAIN, 0,
  45. (size_t)espressif_data_len,
  46. (const u_char *)espressif_data);
  47. }
  48. static void
  49. hnd_espressif_put(coap_context_t *ctx,
  50. coap_resource_t *resource,
  51. coap_session_t *session,
  52. coap_pdu_t *request,
  53. coap_binary_t *token,
  54. coap_string_t *query,
  55. coap_pdu_t *response)
  56. {
  57. size_t size;
  58. unsigned char *data;
  59. coap_resource_notify_observers(resource, NULL);
  60. if (strcmp (espressif_data, "no data") == 0) {
  61. response->code = COAP_RESPONSE_CODE(201);
  62. }
  63. else {
  64. response->code = COAP_RESPONSE_CODE(204);
  65. }
  66. /* coap_get_data() sets size to 0 on error */
  67. (void)coap_get_data(request, &size, &data);
  68. if (size == 0) { /* re-init */
  69. snprintf(espressif_data, sizeof(espressif_data), "no data");
  70. espressif_data_len = strlen(espressif_data);
  71. } else {
  72. espressif_data_len = size > sizeof (espressif_data) ? sizeof (espressif_data) : size;
  73. memcpy (espressif_data, data, espressif_data_len);
  74. }
  75. }
  76. static void
  77. hnd_espressif_delete(coap_context_t *ctx,
  78. coap_resource_t *resource,
  79. coap_session_t *session,
  80. coap_pdu_t *request,
  81. coap_binary_t *token,
  82. coap_string_t *query,
  83. coap_pdu_t *response)
  84. {
  85. coap_resource_notify_observers(resource, NULL);
  86. snprintf(espressif_data, sizeof(espressif_data), "no data");
  87. espressif_data_len = strlen(espressif_data);
  88. response->code = COAP_RESPONSE_CODE(202);
  89. }
  90. static void coap_example_thread(void *p)
  91. {
  92. coap_context_t *ctx = NULL;
  93. coap_address_t serv_addr;
  94. coap_resource_t *resource = NULL;
  95. snprintf(espressif_data, sizeof(espressif_data), "no data");
  96. espressif_data_len = strlen(espressif_data);
  97. coap_set_log_level(COAP_LOGGING_LEVEL);
  98. while (1) {
  99. coap_endpoint_t *ep_udp = NULL;
  100. coap_endpoint_t *ep_tcp = NULL;
  101. unsigned wait_ms;
  102. /* Wait for the callback to set the CONNECTED_BIT in the
  103. event group.
  104. */
  105. xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  106. false, true, portMAX_DELAY);
  107. ESP_LOGI(TAG, "Connected to AP");
  108. /* Prepare the CoAP server socket */
  109. coap_address_init(&serv_addr);
  110. serv_addr.addr.sin.sin_family = AF_INET;
  111. serv_addr.addr.sin.sin_addr.s_addr = INADDR_ANY;
  112. serv_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
  113. ctx = coap_new_context(NULL);
  114. if (!ctx) {
  115. continue;
  116. }
  117. ep_udp = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_UDP);
  118. if (!ep_udp) {
  119. goto clean_up;
  120. }
  121. ep_tcp = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP);
  122. if (!ep_tcp) {
  123. goto clean_up;
  124. }
  125. resource = coap_resource_init(coap_make_str_const("Espressif"), 0);
  126. if (!resource) {
  127. goto clean_up;
  128. }
  129. coap_register_handler(resource, COAP_REQUEST_GET, hnd_espressif_get);
  130. coap_register_handler(resource, COAP_REQUEST_PUT, hnd_espressif_put);
  131. coap_register_handler(resource, COAP_REQUEST_DELETE, hnd_espressif_delete);
  132. /* We possibly want to Observe the GETs */
  133. coap_resource_set_get_observable(resource, 1);
  134. coap_add_resource(ctx, resource);
  135. wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;
  136. while (1) {
  137. int result = coap_run_once(ctx, wait_ms);
  138. if (result < 0) {
  139. break;
  140. } else if (result && (unsigned)result < wait_ms) {
  141. /* decrement if there is a result wait time returned */
  142. wait_ms -= result;
  143. }
  144. if (result) {
  145. /* result must have been >= wait_ms, so reset wait_ms */
  146. wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;
  147. }
  148. }
  149. }
  150. clean_up:
  151. coap_free_context(ctx);
  152. coap_cleanup();
  153. vTaskDelete(NULL);
  154. }
  155. static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
  156. {
  157. switch(event->event_id) {
  158. case SYSTEM_EVENT_STA_START:
  159. esp_wifi_connect();
  160. break;
  161. case SYSTEM_EVENT_STA_GOT_IP:
  162. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  163. break;
  164. case SYSTEM_EVENT_STA_DISCONNECTED:
  165. /* This is a workaround as ESP32 WiFi libs don't currently
  166. auto-reassociate. */
  167. esp_wifi_connect();
  168. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  169. break;
  170. default:
  171. break;
  172. }
  173. return ESP_OK;
  174. }
  175. static void wifi_conn_init(void)
  176. {
  177. tcpip_adapter_init();
  178. wifi_event_group = xEventGroupCreate();
  179. ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
  180. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  181. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  182. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  183. wifi_config_t wifi_config = {
  184. .sta = {
  185. .ssid = EXAMPLE_WIFI_SSID,
  186. .password = EXAMPLE_WIFI_PASS,
  187. },
  188. };
  189. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  190. ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  191. ESP_ERROR_CHECK( esp_wifi_start() );
  192. }
  193. void app_main(void)
  194. {
  195. ESP_ERROR_CHECK( nvs_flash_init() );
  196. wifi_conn_init();
  197. xTaskCreate(coap_example_thread, "coap", 1024 * 5, NULL, 5, NULL);
  198. }