2016-12-10 00:48:38 -05:00
|
|
|
/* CoAP server Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2017-01-06 05:42:46 -05:00
|
|
|
#include <sys/socket.h>
|
2016-12-10 00:48:38 -05:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_wifi.h"
|
2018-11-20 11:43:52 -05:00
|
|
|
#include "esp_event.h"
|
2016-12-10 00:48:38 -05:00
|
|
|
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2018-11-20 11:43:52 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2017-01-06 05:42:46 -05:00
|
|
|
|
2018-11-20 11:43:52 -05:00
|
|
|
#include "coap.h"
|
2017-01-06 05:42:46 -05:00
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
/* Set this to 9 to get verbose logging from within libcoap */
|
|
|
|
#define COAP_LOGGING_LEVEL 0
|
2016-12-10 00:48:38 -05:00
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
static char espressif_data[100];
|
|
|
|
static int espressif_data_len = 0;
|
2016-12-10 00:48:38 -05:00
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
/*
|
|
|
|
* The resource handler
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
hnd_espressif_get(coap_context_t *ctx, coap_resource_t *resource,
|
|
|
|
coap_session_t *session,
|
|
|
|
coap_pdu_t *request, coap_binary_t *token,
|
|
|
|
coap_string_t *query, coap_pdu_t *response)
|
|
|
|
{
|
|
|
|
coap_add_data_blocked_response(resource, session, request, response, token,
|
|
|
|
COAP_MEDIATYPE_TEXT_PLAIN, 0,
|
|
|
|
(size_t)espressif_data_len,
|
|
|
|
(const u_char *)espressif_data);
|
|
|
|
}
|
2016-12-10 00:48:38 -05:00
|
|
|
|
|
|
|
static void
|
2019-03-08 15:22:17 -05:00
|
|
|
hnd_espressif_put(coap_context_t *ctx,
|
|
|
|
coap_resource_t *resource,
|
|
|
|
coap_session_t *session,
|
|
|
|
coap_pdu_t *request,
|
|
|
|
coap_binary_t *token,
|
|
|
|
coap_string_t *query,
|
|
|
|
coap_pdu_t *response)
|
2016-12-10 00:48:38 -05:00
|
|
|
{
|
2019-03-08 15:22:17 -05:00
|
|
|
size_t size;
|
|
|
|
unsigned char *data;
|
|
|
|
|
|
|
|
coap_resource_notify_observers(resource, NULL);
|
|
|
|
|
|
|
|
if (strcmp (espressif_data, "no data") == 0) {
|
|
|
|
response->code = COAP_RESPONSE_CODE(201);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
response->code = COAP_RESPONSE_CODE(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* coap_get_data() sets size to 0 on error */
|
|
|
|
(void)coap_get_data(request, &size, &data);
|
2017-01-06 05:42:46 -05:00
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
if (size == 0) { /* re-init */
|
|
|
|
snprintf(espressif_data, sizeof(espressif_data), "no data");
|
|
|
|
espressif_data_len = strlen(espressif_data);
|
|
|
|
} else {
|
|
|
|
espressif_data_len = size > sizeof (espressif_data) ? sizeof (espressif_data) : size;
|
|
|
|
memcpy (espressif_data, data, espressif_data_len);
|
2017-01-06 05:42:46 -05:00
|
|
|
}
|
2016-12-10 00:48:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-03-08 15:22:17 -05:00
|
|
|
hnd_espressif_delete(coap_context_t *ctx,
|
|
|
|
coap_resource_t *resource,
|
|
|
|
coap_session_t *session,
|
|
|
|
coap_pdu_t *request,
|
|
|
|
coap_binary_t *token,
|
|
|
|
coap_string_t *query,
|
|
|
|
coap_pdu_t *response)
|
2016-12-10 00:48:38 -05:00
|
|
|
{
|
2019-03-08 15:22:17 -05:00
|
|
|
coap_resource_notify_observers(resource, NULL);
|
|
|
|
snprintf(espressif_data, sizeof(espressif_data), "no data");
|
|
|
|
espressif_data_len = strlen(espressif_data);
|
|
|
|
response->code = COAP_RESPONSE_CODE(202);
|
2016-12-10 00:48:38 -05:00
|
|
|
}
|
|
|
|
|
2017-03-22 00:36:11 -04:00
|
|
|
static void coap_example_thread(void *p)
|
2016-12-10 00:48:38 -05:00
|
|
|
{
|
2019-03-08 15:22:17 -05:00
|
|
|
coap_context_t *ctx = NULL;
|
2017-01-06 05:42:46 -05:00
|
|
|
coap_address_t serv_addr;
|
2019-03-08 15:22:17 -05:00
|
|
|
coap_resource_t *resource = NULL;
|
2017-01-06 05:42:46 -05:00
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
snprintf(espressif_data, sizeof(espressif_data), "no data");
|
|
|
|
espressif_data_len = strlen(espressif_data);
|
|
|
|
coap_set_log_level(COAP_LOGGING_LEVEL);
|
2017-01-06 05:42:46 -05:00
|
|
|
while (1) {
|
2019-03-08 15:22:17 -05:00
|
|
|
coap_endpoint_t *ep_udp = NULL;
|
|
|
|
coap_endpoint_t *ep_tcp = NULL;
|
|
|
|
unsigned wait_ms;
|
|
|
|
|
2017-01-06 05:42:46 -05:00
|
|
|
/* Prepare the CoAP server socket */
|
|
|
|
coap_address_init(&serv_addr);
|
|
|
|
serv_addr.addr.sin.sin_family = AF_INET;
|
|
|
|
serv_addr.addr.sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
serv_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
ctx = coap_new_context(NULL);
|
|
|
|
if (!ctx) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ep_udp = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_UDP);
|
|
|
|
if (!ep_udp) {
|
|
|
|
goto clean_up;
|
|
|
|
}
|
|
|
|
ep_tcp = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP);
|
|
|
|
if (!ep_tcp) {
|
|
|
|
goto clean_up;
|
|
|
|
}
|
|
|
|
resource = coap_resource_init(coap_make_str_const("Espressif"), 0);
|
|
|
|
if (!resource) {
|
|
|
|
goto clean_up;
|
|
|
|
}
|
|
|
|
coap_register_handler(resource, COAP_REQUEST_GET, hnd_espressif_get);
|
|
|
|
coap_register_handler(resource, COAP_REQUEST_PUT, hnd_espressif_put);
|
|
|
|
coap_register_handler(resource, COAP_REQUEST_DELETE, hnd_espressif_delete);
|
|
|
|
/* We possibly want to Observe the GETs */
|
|
|
|
coap_resource_set_get_observable(resource, 1);
|
|
|
|
coap_add_resource(ctx, resource);
|
|
|
|
|
|
|
|
wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int result = coap_run_once(ctx, wait_ms);
|
|
|
|
if (result < 0) {
|
|
|
|
break;
|
|
|
|
} else if (result && (unsigned)result < wait_ms) {
|
|
|
|
/* decrement if there is a result wait time returned */
|
|
|
|
wait_ms -= result;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
/* result must have been >= wait_ms, so reset wait_ms */
|
|
|
|
wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;
|
|
|
|
}
|
2017-01-06 05:42:46 -05:00
|
|
|
}
|
2016-12-10 00:48:38 -05:00
|
|
|
}
|
2019-03-08 15:22:17 -05:00
|
|
|
clean_up:
|
|
|
|
coap_free_context(ctx);
|
|
|
|
coap_cleanup();
|
2017-01-06 05:42:46 -05:00
|
|
|
|
|
|
|
vTaskDelete(NULL);
|
2016-12-10 00:48:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2017-03-14 09:39:44 -04:00
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2018-11-20 11:43:52 -05:00
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2017-01-06 05:42:46 -05:00
|
|
|
|
2019-03-14 08:15:26 -04:00
|
|
|
xTaskCreate(coap_example_thread, "coap", 1024 * 5, NULL, 5, NULL);
|
2016-12-10 00:48:38 -05:00
|
|
|
}
|