mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-09 04:25:32 +00:00
Update component/coap to libcoap version release-4.2.0
This takes the code up to the latest released version of libcoap. As there have been API changes, coap_client and coap_server in examples/protocols have been updated to use the new APIs. Further information on the new libcoap APIs can be found at https://libcoap.net/doc/reference/4.2.0/ coap_client has been updated to handle BLOCK2 responses from "coap://californium.eclipse.org" coap_client has been modified to only send out one request (and wait for the response) coap_server has been updated to support Observe subscriptions, and well as adding in PUT and DELETE handlers to work on the Espressif resource coap_server and coap_client have had their stack sizes increased. port/coap_io.c has been added, a copy of libcoap/src/coap_io.c with support added for systems that do not have RFC 3542 section 20 support. port/coap_io_socket.c has been removed as a lot of the code is now replicated in different libcoap files. Once this PR is place, then adding in DTLS will be a lot simpler (as a separate PR) Signed-off-by: Jitin George <jitin@espressif.com> Merges https://github.com/espressif/esp-idf/pull/3148
This commit is contained in:
@@ -31,8 +31,8 @@
|
||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||
|
||||
#define COAP_DEFAULT_TIME_SEC 5
|
||||
#define COAP_DEFAULT_TIME_USEC 0
|
||||
/* Set this to 9 to get verbose logging from within libcoap */
|
||||
#define COAP_LOGGING_LEVEL 0
|
||||
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
@@ -42,53 +42,86 @@ static EventGroupHandle_t wifi_event_group;
|
||||
const static int CONNECTED_BIT = BIT0;
|
||||
|
||||
const static char *TAG = "CoAP_server";
|
||||
|
||||
static coap_async_state_t *async = NULL;
|
||||
|
||||
static void
|
||||
send_async_response(coap_context_t *ctx, const coap_endpoint_t *local_if)
|
||||
{
|
||||
coap_pdu_t *response;
|
||||
unsigned char buf[3];
|
||||
const char* response_data = "Hello World!";
|
||||
response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, COAP_MAX_PDU_SIZE);
|
||||
response->hdr->id = coap_new_message_id(ctx);
|
||||
if (async->tokenlen)
|
||||
coap_add_token(response, async->tokenlen, async->token);
|
||||
coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
coap_add_data (response, strlen(response_data), (unsigned char *)response_data);
|
||||
|
||||
if (coap_send(ctx, local_if, &async->peer, response) == COAP_INVALID_TID) {
|
||||
|
||||
}
|
||||
coap_delete_pdu(response);
|
||||
coap_async_state_t *tmp;
|
||||
coap_remove_async(ctx, async->id, &tmp);
|
||||
coap_free_async(async);
|
||||
async = NULL;
|
||||
}
|
||||
static char espressif_data[100];
|
||||
static int espressif_data_len = 0;
|
||||
|
||||
/*
|
||||
* The resource handler
|
||||
*/
|
||||
static void
|
||||
async_handler(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
const coap_endpoint_t *local_interface, coap_address_t *peer,
|
||||
coap_pdu_t *request, str *token, coap_pdu_t *response)
|
||||
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)
|
||||
{
|
||||
async = coap_register_async(ctx, peer, request, COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM, (void*)"no data");
|
||||
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);
|
||||
}
|
||||
|
||||
static void
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
static void coap_example_thread(void *p)
|
||||
{
|
||||
coap_context_t* ctx = NULL;
|
||||
coap_context_t *ctx = NULL;
|
||||
coap_address_t serv_addr;
|
||||
coap_resource_t* resource = NULL;
|
||||
fd_set readfds;
|
||||
struct timeval tv;
|
||||
int flags = 0;
|
||||
coap_resource_t *resource = NULL;
|
||||
|
||||
snprintf(espressif_data, sizeof(espressif_data), "no data");
|
||||
espressif_data_len = strlen(espressif_data);
|
||||
coap_set_log_level(COAP_LOGGING_LEVEL);
|
||||
while (1) {
|
||||
coap_endpoint_t *ep_udp = NULL;
|
||||
coap_endpoint_t *ep_tcp = NULL;
|
||||
unsigned wait_ms;
|
||||
|
||||
/* Wait for the callback to set the CONNECTED_BIT in the
|
||||
event group.
|
||||
*/
|
||||
@@ -101,43 +134,49 @@ static void coap_example_thread(void *p)
|
||||
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);
|
||||
ctx = coap_new_context(&serv_addr);
|
||||
if (ctx) {
|
||||
flags = fcntl(ctx->sockfd, F_GETFL, 0);
|
||||
fcntl(ctx->sockfd, F_SETFL, flags|O_NONBLOCK);
|
||||
|
||||
tv.tv_usec = COAP_DEFAULT_TIME_USEC;
|
||||
tv.tv_sec = COAP_DEFAULT_TIME_SEC;
|
||||
/* Initialize the resource */
|
||||
resource = coap_resource_init((unsigned char *)"Espressif", 9, 0);
|
||||
if (resource){
|
||||
coap_register_handler(resource, COAP_REQUEST_GET, async_handler);
|
||||
coap_add_resource(ctx, resource);
|
||||
/*For incoming connections*/
|
||||
for (;;) {
|
||||
FD_ZERO(&readfds);
|
||||
FD_CLR( ctx->sockfd, &readfds);
|
||||
FD_SET( ctx->sockfd, &readfds);
|
||||
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);
|
||||
|
||||
int result = select( ctx->sockfd+1, &readfds, 0, 0, &tv );
|
||||
if (result > 0){
|
||||
if (FD_ISSET( ctx->sockfd, &readfds ))
|
||||
coap_read(ctx);
|
||||
} else if (result < 0){
|
||||
break;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "select timeout");
|
||||
}
|
||||
wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;
|
||||
|
||||
if (async) {
|
||||
send_async_response(ctx, ctx->endpoint);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
coap_free_context(ctx);
|
||||
}
|
||||
}
|
||||
clean_up:
|
||||
coap_free_context(ctx);
|
||||
coap_cleanup();
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
@@ -187,5 +226,5 @@ void app_main(void)
|
||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
||||
wifi_conn_init();
|
||||
|
||||
xTaskCreate(coap_example_thread, "coap", 2048, NULL, 5, NULL);
|
||||
xTaskCreate(coap_example_thread, "coap", 10240, NULL, 5, NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user