diff --git a/examples/coap-client.c b/examples/coap-client.c index f11826f0..3ef55c9b 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -261,7 +261,7 @@ resolve_address(const coap_str_const_t *server, struct sockaddr *dst) { (Pdu)->hdr->code == COAP_RESPONSE_CODE_CHANGED)) static inline int -check_token(coap_pdu_t *received) { +check_token(const coap_pdu_t *received) { coap_bin_const_t token = coap_pdu_get_token(received); return token.length == the_token.length && @@ -269,9 +269,8 @@ check_token(coap_pdu_t *received) { } static int -event_handler(coap_context_t *ctx COAP_UNUSED, - coap_event_t event, - coap_session_t *session COAP_UNUSED) { +event_handler(coap_session_t *session COAP_UNUSED, + const coap_event_t event) { switch(event) { case COAP_EVENT_DTLS_CLOSED: @@ -286,10 +285,9 @@ event_handler(coap_context_t *ctx COAP_UNUSED, } static void -nack_handler(coap_context_t *context COAP_UNUSED, - coap_session_t *session COAP_UNUSED, - coap_pdu_t *sent COAP_UNUSED, - coap_nack_reason_t reason, +nack_handler(coap_session_t *session COAP_UNUSED, + const coap_pdu_t *sent COAP_UNUSED, + const coap_nack_reason_t reason, const coap_mid_t id COAP_UNUSED) { switch(reason) { @@ -310,10 +308,9 @@ nack_handler(coap_context_t *context COAP_UNUSED, * Response handler used for coap_send_large() responses */ static coap_response_t -message_handler(coap_context_t *ctx COAP_UNUSED, - coap_session_t *session COAP_UNUSED, - coap_pdu_t *sent, - coap_pdu_t *received, +message_handler(coap_session_t *session COAP_UNUSED, + const coap_pdu_t *sent, + const coap_pdu_t *received, const coap_mid_t id COAP_UNUSED) { coap_opt_t *block_opt; diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 10f830d6..cfe5d302 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -111,12 +111,10 @@ handle_sigint(int signum COAP_UNUSED) { } static void -hnd_get_resource(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_get_resource(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { rd_t *rd = NULL; unsigned char buf[3]; @@ -141,12 +139,10 @@ hnd_get_resource(coap_context_t *ctx COAP_UNUSED, } static void -hnd_put_resource(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource COAP_UNUSED, +hnd_put_resource(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { #if 1 coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_IMPLEMENTED); @@ -220,12 +216,10 @@ hnd_put_resource(coap_context_t *ctx COAP_UNUSED, } static void -hnd_delete_resource(coap_context_t *ctx, - coap_resource_t *resource, +hnd_delete_resource(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { rd_t *rd = NULL; coap_str_const_t* uri_path = coap_resource_get_uri_path(resource); @@ -238,18 +232,16 @@ hnd_delete_resource(coap_context_t *ctx, } /* FIXME: link attributes for resource have been created dynamically * using coap_malloc() and must be released. */ - coap_delete_resource(ctx, resource); + coap_delete_resource(coap_session_get_context(session), resource); coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED); } static void -hnd_get_rd(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource COAP_UNUSED, +hnd_get_rd(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { unsigned char buf[3]; @@ -376,7 +368,7 @@ add_source_address(coap_resource_t *resource, } static rd_t * -make_rd(coap_pdu_t *pdu) { +make_rd(const coap_pdu_t *pdu) { rd_t *rd; const uint8_t *data; coap_opt_iterator_t opt_iter; @@ -409,12 +401,10 @@ make_rd(coap_pdu_t *pdu) { } static void -hnd_post_rd(coap_context_t *ctx, - coap_resource_t *resource COAP_UNUSED, +hnd_post_rd(coap_resource_t *resource COAP_UNUSED, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_resource_t *r; #define LOCSIZE 68 @@ -535,7 +525,7 @@ hnd_post_rd(coap_context_t *ctx, } } - coap_add_resource(ctx, r); + coap_add_resource(coap_session_get_context(session), r); /* create response */ diff --git a/examples/coap-server.c b/examples/coap-server.c index 904118c8..d50c9547 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -222,28 +222,24 @@ reference_resource_data(transient_value_t *entry) { "Copyright (C) 2010--2021 Olaf Bergmann and others\n\n" static void -hnd_get_index(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_get_index(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); coap_add_data_large_response(resource, session, request, response, - token, query, COAP_MEDIATYPE_TEXT_PLAIN, + query, COAP_MEDIATYPE_TEXT_PLAIN, 0x2ffff, 0, strlen(INDEX), (const uint8_t *)INDEX, NULL, NULL); } static void -hnd_get_time(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_get_time(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query, + const coap_pdu_t *request, + const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; @@ -275,7 +271,7 @@ hnd_get_time(coap_context_t *ctx COAP_UNUSED, } } coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, COAP_MEDIATYPE_TEXT_PLAIN, 1, 0, len, buf, NULL, NULL); @@ -287,12 +283,10 @@ hnd_get_time(coap_context_t *ctx COAP_UNUSED, } static void -hnd_put_time(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_put_time(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_tick_t t; size_t size; @@ -337,12 +331,10 @@ hnd_put_time(coap_context_t *ctx COAP_UNUSED, } static void -hnd_delete_time(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource COAP_UNUSED, +hnd_delete_time(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response COAP_UNUSED) { my_clock_base = 0; /* mark clock as "deleted" */ @@ -355,54 +347,54 @@ hnd_delete_time(coap_context_t *ctx COAP_UNUSED, * "separate" response (empty ACK followed by data response at a later stage). */ static void -hnd_get_async(coap_context_t *context, - coap_resource_t *resource, +hnd_get_async(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query, + const coap_pdu_t *request, + const coap_string_t *query, coap_pdu_t *response) { unsigned long delay = 5; size_t size; - coap_async_t *async; - coap_binary_t zero_token = {0, NULL}; /* * See if this is the initial, or delayed request */ - async = coap_find_async(context, session, token ? *token : zero_token); - if (!async) { - /* Set up an async request to trigger delay in the future */ - if (query) { - const uint8_t *p = query->s; + if (request) { + coap_async_t *async; + coap_bin_const_t token = coap_pdu_get_token(request); - delay = 0; - for (size = query->length; size; --size, ++p) - delay = delay * 10 + (*p - '0'); - if (delay == 0) { - coap_log(LOG_INFO, "async: delay of 0 not supported\n"); - coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST); + async = coap_find_async(session, token); + if (!async) { + /* Set up an async request to trigger delay in the future */ + if (query) { + const uint8_t *p = query->s; + + delay = 0; + for (size = query->length; size; --size, ++p) + delay = delay * 10 + (*p - '0'); + if (delay == 0) { + coap_log(LOG_INFO, "async: delay of 0 not supported\n"); + coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST); + return; + } + } + async = coap_register_async(session, + request, + COAP_TICKS_PER_SECOND * delay); + if (async == NULL) { + coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); return; } - } - async = coap_register_async(context, - session, - request, - COAP_TICKS_PER_SECOND * delay); - if (async == NULL) { - coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); + /* + * Not setting response code will cause empty ACK to be sent + * if Confirmable + */ return; } - /* - * Not setting response code will cause empty ACK to be sent - * if Confirmable - */ - return; } - /* async set up, so this is the delayed request */ + /* no request (observe) or async set up, so this is the delayed request */ /* Send back the appropriate data */ - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, COAP_MEDIATYPE_TEXT_PLAIN, -1, 0, 4, (const uint8_t *)"done", NULL, NULL); coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); @@ -415,12 +407,10 @@ hnd_get_async(coap_context_t *context, */ static void -hnd_get_example_data(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_get_example_data(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query, + const coap_pdu_t *request, + const coap_string_t *query, coap_pdu_t *response ) { coap_binary_t body; @@ -443,7 +433,7 @@ hnd_get_example_data(coap_context_t *ctx COAP_UNUSED, } coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); body = reference_resource_data(example_data_value); - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, example_data_media_type, -1, 0, body.length, body.s, @@ -461,12 +451,10 @@ cache_free_app_data(void *data) { */ static void -hnd_put_example_data(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_put_example_data(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response ) { size_t size; @@ -581,7 +569,7 @@ hnd_put_example_data(coap_context_t *ctx COAP_UNUSED, coap_binary_t body; body = reference_resource_data(example_data_value); - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, example_data_media_type, -1, 0, body.length, body.s, @@ -654,7 +642,7 @@ static size_t proxy_list_count = 0; static coap_resource_t *proxy_resource = NULL; static int -get_uri_proxy_scheme_info(coap_pdu_t *request, +get_uri_proxy_scheme_info(const coap_pdu_t *request, coap_opt_t *opt, coap_uri_t *uri, coap_string_t **uri_path, @@ -774,7 +762,7 @@ setup_cpsk(char *client_sni) { static proxy_list_t * get_proxy_session(coap_session_t *session, coap_pdu_t *response, - coap_binary_t *token, coap_string_t *query) { + const coap_bin_const_t *token, const coap_string_t *query) { size_t i; proxy_list_t *new_proxy_list; @@ -877,9 +865,9 @@ remove_proxy_association(coap_session_t *session, int send_failure) { static coap_session_t * -get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session, - coap_pdu_t *response, coap_binary_t *token, - coap_string_t *query, coap_uri_t *uri) { +get_ongoing_proxy_session(coap_session_t *session, + coap_pdu_t *response, const coap_bin_const_t *token, + const coap_string_t *query, const coap_uri_t *uri) { coap_address_t dst; coap_uri_scheme_t scheme; @@ -887,6 +875,7 @@ get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session, coap_str_const_t server; uint16_t port = COAP_DEFAULT_PORT; proxy_list_t *new_proxy_list; + coap_context_t *context = coap_session_get_context(session); new_proxy_list = get_proxy_session(session, response, token, query); if (!new_proxy_list) @@ -925,7 +914,7 @@ get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session, case COAP_URI_SCHEME_COAP: case COAP_URI_SCHEME_COAP_TCP: new_proxy_list->ongoing = - coap_new_client_session(ctx, NULL, &dst, + coap_new_client_session(context, NULL, &dst, scheme == COAP_URI_SCHEME_COAP ? COAP_PROTO_UDP : COAP_PROTO_TCP); break; @@ -940,10 +929,10 @@ get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session, if (!key_defined) { /* Use our defined PKI certs (or NULL) */ - coap_dtls_pki_t *dtls_pki = setup_pki(ctx, COAP_DTLS_ROLE_CLIENT, + coap_dtls_pki_t *dtls_pki = setup_pki(context, COAP_DTLS_ROLE_CLIENT, client_sni); new_proxy_list->ongoing = - coap_new_client_session_pki(ctx, NULL, &dst, + coap_new_client_session_pki(context, NULL, &dst, scheme == COAP_URI_SCHEME_COAPS ? COAP_PROTO_DTLS : COAP_PROTO_TLS, dtls_pki); @@ -953,7 +942,7 @@ get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session, coap_dtls_cpsk_t *dtls_cpsk = setup_cpsk(client_sni); new_proxy_list->ongoing = - coap_new_client_session_psk2(ctx, NULL, &dst, + coap_new_client_session_psk2(context, NULL, &dst, scheme == COAP_URI_SCHEME_COAPS ? COAP_PROTO_DTLS : COAP_PROTO_TLS, dtls_cpsk); @@ -980,12 +969,10 @@ release_proxy_body_data(coap_session_t *session COAP_UNUSED, } static void -hnd_proxy_uri(coap_context_t *ctx, - coap_resource_t *resource COAP_UNUSED, +hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query, + const coap_pdu_t *request, + const coap_string_t *query, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *opt; @@ -1010,6 +997,7 @@ hnd_proxy_uri(coap_context_t *ctx, unsigned char *buf = _buf; size_t buflen; int res; + coap_bin_const_t token = coap_pdu_get_token(request); memset(&uri, 0, sizeof(uri)); /* @@ -1067,7 +1055,7 @@ hnd_proxy_uri(coap_context_t *ctx, uri.scheme == COAP_URI_SCHEME_COAPS_TCP) { coap_pdu_code_t req_code = coap_pdu_get_code(request); - tmp_proxy_list = get_proxy_session(session, response, token, query); + tmp_proxy_list = get_proxy_session(session, response, &token, query); if (!tmp_proxy_list) goto cleanup; body_data = tmp_proxy_list->body_data; @@ -1094,7 +1082,7 @@ hnd_proxy_uri(coap_context_t *ctx, /* Send data on (opening session if appropriate) now that it is all in */ - ongoing = get_ongoing_proxy_session(ctx, session, response, token, + ongoing = get_ongoing_proxy_session(session, response, &token, query, &uri); /* * Build up the ongoing PDU that we are going to send @@ -1107,7 +1095,7 @@ hnd_proxy_uri(coap_context_t *ctx, goto cleanup; } - if (token && !coap_add_token(pdu, token->length, token->s)) { + if (!coap_add_token(pdu, token.length, token.s)) { coap_log(LOG_DEBUG, "cannot add token to proxy request\n"); coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR); coap_delete_pdu(pdu); @@ -1234,12 +1222,10 @@ static dynamic_resource_t *dynamic_entry = NULL; */ static void -hnd_delete(coap_context_t *ctx, - coap_resource_t *resource, +hnd_delete(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response ) { int i; @@ -1268,7 +1254,7 @@ hnd_delete(coap_context_t *ctx, } /* Dynamic resource no longer required - delete it */ - coap_delete_resource(ctx, resource); + coap_delete_resource(coap_session_get_context(session), resource); coap_delete_string(uri_path); coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED); } @@ -1279,12 +1265,10 @@ hnd_delete(coap_context_t *ctx, */ static void -hnd_get(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_get(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query, + const coap_pdu_t *request, + const coap_string_t *query, coap_pdu_t *response ) { coap_str_const_t *uri_path; @@ -1317,7 +1301,7 @@ hnd_get(coap_context_t *ctx COAP_UNUSED, coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); body = reference_resource_data(resource_entry->value); - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, resource_entry->media_type, -1, 0, body.length, body.s, @@ -1330,12 +1314,10 @@ hnd_get(coap_context_t *ctx COAP_UNUSED, */ static void -hnd_put(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_put(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response ) { coap_string_t *uri_path; @@ -1522,7 +1504,7 @@ hnd_put(coap_context_t *ctx COAP_UNUSED, coap_binary_t body; body = reference_resource_data(resource_entry->value); - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, resource_entry->media_type, -1, 0, body.length, body.s, @@ -1535,12 +1517,10 @@ hnd_put(coap_context_t *ctx COAP_UNUSED, */ static void -hnd_unknown_put(coap_context_t *ctx, - coap_resource_t *resource COAP_UNUSED, +hnd_unknown_put(coap_resource_t *resource COAP_UNUSED, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token, - coap_string_t *query, + const coap_pdu_t *request, + const coap_string_t *query, coap_pdu_t *response ) { coap_resource_t *r; @@ -1571,17 +1551,16 @@ hnd_unknown_put(coap_context_t *ctx, /* We possibly want to Observe the GETs */ coap_resource_set_get_observable(r, 1); coap_register_handler(r, COAP_REQUEST_GET, hnd_get); - coap_add_resource(ctx, r); + coap_add_resource(coap_session_get_context(session), r); /* Do the PUT for this first call */ - hnd_put(ctx, r, session, request, token, query, response); + hnd_put(r, session, request, query, response); } #if SERVER_CAN_PROXY static int -proxy_event_handler(coap_context_t *ctx COAP_UNUSED, - coap_event_t event, - coap_session_t *session) { +proxy_event_handler(coap_session_t *session, + coap_event_t event) { switch(event) { case COAP_EVENT_DTLS_CLOSED: @@ -1597,10 +1576,9 @@ proxy_event_handler(coap_context_t *ctx COAP_UNUSED, } static coap_response_t -proxy_message_handler(coap_context_t *ctx COAP_UNUSED, - coap_session_t *session, - coap_pdu_t *sent COAP_UNUSED, - coap_pdu_t *received, +proxy_message_handler(coap_session_t *session, + const coap_pdu_t *sent COAP_UNUSED, + const coap_pdu_t *received, const coap_mid_t id COAP_UNUSED) { coap_pdu_t *pdu = NULL; @@ -1705,7 +1683,7 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED, if (size > 0) { coap_add_data_large_response(proxy_resource, incoming, NULL, pdu, - proxy_entry->token, proxy_entry->query, + proxy_entry->query, media_type, maxage, etag, size, data, release_proxy_body_data, proxy_entry->body_data); @@ -1720,10 +1698,9 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED, } static void -proxy_nack_handler(coap_context_t *context COAP_UNUSED, - coap_session_t *session, - coap_pdu_t *sent COAP_UNUSED, - coap_nack_reason_t reason, +proxy_nack_handler(coap_session_t *session, + const coap_pdu_t *sent COAP_UNUSED, + const coap_nack_reason_t reason, const coap_mid_t id COAP_UNUSED) { switch(reason) { diff --git a/examples/contiki/server.c b/examples/contiki/server.c index 4101658f..c469a167 100644 --- a/examples/contiki/server.c +++ b/examples/contiki/server.c @@ -91,10 +91,8 @@ init_coap_server(coap_context_t **ctx) { #endif void -hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, - coap_session_t *session, - coap_pdu_t *request, coap_binary_t *token, - coap_string_t *query, +hnd_get_time(coap_resource_t *resource, coap_session_t *session, + const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; @@ -125,7 +123,7 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp); } } - coap_add_data_blocked_response(resource, session, request, response, token, + coap_add_data_blocked_response(request, response, COAP_MEDIATYPE_TEXT_PLAIN, 1, len, buf); diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index 040e5f3c..0c991ec1 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -90,11 +90,9 @@ coap_free_userdata(void *data) { #if 0 static void -hnd_get_index(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource COAP_UNUSED, +hnd_get_index(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { unsigned char buf[3]; @@ -114,12 +112,10 @@ hnd_get_index(coap_context_t *ctx COAP_UNUSED, #endif static void -hnd_get_resource(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_get_resource(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_payload_t *test_payload; @@ -132,7 +128,7 @@ hnd_get_resource(coap_context_t *ctx COAP_UNUSED, coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); - coap_add_data_blocked_response(resource, session, request, response, token, + coap_add_data_blocked_response(request, response, test_payload->media_type, -1, test_payload->length, test_payload->data); @@ -141,12 +137,10 @@ hnd_get_resource(coap_context_t *ctx COAP_UNUSED, /* DELETE handler for dynamic resources created by POST /test */ static void -hnd_delete_resource(coap_context_t *ctx, - coap_resource_t *resource, +hnd_delete_resource(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_payload_t *payload; @@ -155,18 +149,16 @@ hnd_delete_resource(coap_context_t *ctx, if (payload) coap_delete_payload(resource); - coap_delete_resource(ctx, resource); + coap_delete_resource(coap_session_get_context(session), resource); coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED); } static void -hnd_post_test(coap_context_t *ctx, - coap_resource_t *resource COAP_UNUSED, +hnd_post_test(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *option; @@ -210,7 +202,7 @@ hnd_post_test(coap_context_t *ctx, coap_decode_var_bytes(coap_opt_value(option), coap_opt_length(option)); } - coap_add_resource(ctx, r); + coap_add_resource(coap_session_get_context(session), r); coap_add_payload(r, test_payload); /* add Location-Path */ @@ -229,12 +221,10 @@ hnd_post_test(coap_context_t *ctx, } static void -hnd_put_test(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource, +hnd_put_test(coap_resource_t *resource, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *option; @@ -284,12 +274,10 @@ hnd_put_test(coap_context_t *ctx COAP_UNUSED, } static void -hnd_delete_test(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource COAP_UNUSED, +hnd_delete_test(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request COAP_UNUSED, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { /* the ETSI validation tool does not like empty resources... */ #if 0 @@ -304,12 +292,10 @@ hnd_delete_test(coap_context_t *ctx COAP_UNUSED, } static void -hnd_get_query(coap_context_t *ctx COAP_UNUSED, - coap_resource_t *resource COAP_UNUSED, +hnd_get_query(coap_resource_t *resource COAP_UNUSED, coap_session_t *session COAP_UNUSED, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_filter_t f; @@ -348,60 +334,61 @@ hnd_get_query(coap_context_t *ctx COAP_UNUSED, /* handler for TD_COAP_CORE_16 */ static void -hnd_get_separate(coap_context_t *ctx, - coap_resource_t *resource COAP_UNUSED, +hnd_get_separate(coap_resource_t *resource COAP_UNUSED, coap_session_t *session, - coap_pdu_t *request, - coap_binary_t *token COAP_UNUSED, - coap_string_t *query COAP_UNUSED, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *option; coap_opt_filter_t f; unsigned long delay = 5; - coap_async_t *async; - coap_binary_t zero_token = {0, NULL}; - async = coap_find_async(ctx, session, token ? *token : zero_token); - if (!async) { - /* Set up an async request to trigger delay in the future */ + if (request) { + coap_async_t *async; + coap_bin_const_t token = coap_pdu_get_token(request); - /* search for option delay in query list */ - coap_option_filter_clear(&f); - coap_option_filter_set(&f, COAP_OPTION_URI_QUERY); + async = coap_find_async(session, token); - coap_option_iterator_init(request, &opt_iter, &f); + if (!async) { + /* Set up an async request to trigger delay in the future */ - while ((option = coap_option_next(&opt_iter))) { - if (strncmp("delay=", (const char *)coap_opt_value(option), 6) == 0) { - unsigned int i; - unsigned long d = 0; + /* search for option delay in query list */ + coap_option_filter_clear(&f); + coap_option_filter_set(&f, COAP_OPTION_URI_QUERY); - for (i = 6; i < coap_opt_length(option); ++i) - d = d * 10 + coap_opt_value(option)[i] - '0'; + coap_option_iterator_init(request, &opt_iter, &f); - /* don't allow delay to be less than COAP_RESOURCE_CHECK_TIME*/ - delay = d < COAP_RESOURCE_CHECK_TIME_SEC - ? COAP_RESOURCE_CHECK_TIME_SEC - : d; - coap_log(LOG_DEBUG, "set delay to %lu\n", delay); - break; + while ((option = coap_option_next(&opt_iter))) { + if (strncmp("delay=", (const char *)coap_opt_value(option), 6) == 0) { + unsigned int i; + unsigned long d = 0; + + for (i = 6; i < coap_opt_length(option); ++i) + d = d * 10 + coap_opt_value(option)[i] - '0'; + + /* don't allow delay to be less than COAP_RESOURCE_CHECK_TIME*/ + delay = d < COAP_RESOURCE_CHECK_TIME_SEC + ? COAP_RESOURCE_CHECK_TIME_SEC + : d; + coap_log(LOG_DEBUG, "set delay to %lu\n", delay); + break; + } } - } - async = coap_register_async(ctx, - session, - request, - COAP_TICKS_PER_SECOND * delay); - if (async == NULL) { - coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); + async = coap_register_async(session, + request, + COAP_TICKS_PER_SECOND * delay); + if (async == NULL) { + coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); + return; + } + /* Not setting response code will cause empty ACK to be sent + if Confirmable */ return; } - /* Not setting response code will cause empty ACK to be sent - if Confirmable */ - return; } - /* This is the delayed response. Send back the appropriate data */ + /* no request (observe) or async set up, so this is the delayed request */ coap_add_data(response, 4, (const uint8_t *)"done"); coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); diff --git a/examples/lwip/server-coap.c b/examples/lwip/server-coap.c index 0faeb42c..6beae533 100644 --- a/examples/lwip/server-coap.c +++ b/examples/lwip/server-coap.c @@ -13,10 +13,8 @@ static coap_resource_t *time_resource = NULL; /* just for testing */ #endif void -hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, - coap_session_t *session, - coap_pdu_t *request, coap_binary_t *token, - coap_string_t *query, +hnd_get_time(coap_resource_t *resource, coap_session_t *session, + const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; @@ -30,13 +28,6 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, response->code = my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404); - if (coap_find_observer(resource, session, token)) { - coap_add_option(response, COAP_OPTION_OBSERVE, - coap_encode_var_safe(buf, sizeof(buf), - resource->observe), - buf); - } - if (my_clock_base) coap_add_option(response, COAP_OPTION_CONTENT_FORMAT, coap_encode_var_safe(buf, sizeof(buf), diff --git a/include/coap3/async.h b/include/coap3/async.h index 115765ae..f0029e8b 100644 --- a/include/coap3/async.h +++ b/include/coap3/async.h @@ -42,7 +42,6 @@ int coap_async_is_supported(void); * When the delay expires, a copy of the @p request will get sent to the * appropriate request handler. * - * @param context The context to use. * @param session The session that is used for asynchronous transmissions. * @param request The request that is handled asynchronously. * @param delay The amount of time to delay before sending response, 0 means @@ -52,9 +51,8 @@ int coap_async_is_supported(void); * NULL in case of an error. */ coap_async_t * -coap_register_async(coap_context_t *context, - coap_session_t *session, - coap_pdu_t *request, +coap_register_async(coap_session_t *session, + const coap_pdu_t *request, coap_tick_t delay); /** @@ -71,30 +69,27 @@ void coap_async_set_delay(coap_async_t *async, coap_tick_t delay); /** - * Releases the memory that was allocated by coap_async_state_init() for the + * Releases the memory that was allocated by coap_register_async() for the * object @p async. * - * @param context The context to use. + * @param session The session to use. * @param async The object to delete. */ void -coap_free_async(coap_context_t *context, coap_async_t *async); +coap_free_async(coap_session_t *session, coap_async_t *async); /** * Retrieves the object identified by @p token from the list of asynchronous * transactions that are registered with @p context. This function returns a * pointer to that object or @c NULL if not found. * - * @param context The context where the asynchronous objects are registered - * with. * @param session The session that is used for asynchronous transmissions. * @param token The PDU's token of the object to retrieve. * * @return A pointer to the object identified by @p token or @c NULL if * not found. */ -coap_async_t *coap_find_async(coap_context_t *context, - coap_session_t *session, coap_binary_t token); +coap_async_t *coap_find_async(coap_session_t *session, coap_bin_const_t token); /** * Set the application data pointer held in @p async. This overwrites any diff --git a/include/coap3/block.h b/include/coap3/block.h index d75a9d9c..188129cc 100644 --- a/include/coap3/block.h +++ b/include/coap3/block.h @@ -93,7 +93,7 @@ coap_opt_block_set_m(coap_opt_t *block_opt, int m) { * * @return @c 1 on success, @c 0 otherwise. */ -int coap_get_block(coap_pdu_t *pdu, coap_option_num_t number, +int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block); /** @@ -166,11 +166,8 @@ coap_block_build_body(coap_binary_t *body_data, size_t length, * Note: The application will get called for every packet of a large body to * process. Consider using coap_add_data_response_large() instead. * - * @param resource The resource the data is associated with. - * @param session The coap session. * @param request The requesting pdu. * @param response The response pdu. - * @param token The token taken from the (original) requesting pdu. * @param media_type The format of the data. * @param maxage The maxmimum life of the data. If @c -1, then there * is no maxage. @@ -179,11 +176,8 @@ coap_block_build_body(coap_binary_t *body_data, size_t length, * */ void -coap_add_data_blocked_response(coap_resource_t *resource, - coap_session_t *session, - coap_pdu_t *request, +coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, - const coap_binary_t *token, uint16_t media_type, int maxage, size_t length, @@ -282,7 +276,6 @@ int coap_add_data_large_request(coap_session_t *session, * @param session The coap session. * @param request The requesting pdu. * @param response The response pdu. - * @param token The token taken from the (original) requesting pdu. * @param query The query taken from the (original) requesting pdu. * @param media_type The format of the data. * @param maxage The maxmimum life of the data. If @c -1, then there @@ -300,9 +293,8 @@ int coap_add_data_large_request(coap_session_t *session, int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, + const coap_pdu_t *request, coap_pdu_t *response, - const coap_binary_t *token, const coap_string_t *query, uint16_t media_type, int maxage, diff --git a/include/coap3/coap_block_internal.h b/include/coap3/coap_block_internal.h index 8603d0f5..fab20696 100644 --- a/include/coap3/coap_block_internal.h +++ b/include/coap3/coap_block_internal.h @@ -174,7 +174,6 @@ int coap_handle_request_put_block(coap_context_t *context, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, - coap_binary_t *token, coap_string_t *query, coap_method_handler_t h, int *added_block); diff --git a/include/coap3/coap_event.h b/include/coap3/coap_event.h index b009b3b7..77f21175 100644 --- a/include/coap3/coap_event.h +++ b/include/coap3/coap_event.h @@ -54,14 +54,12 @@ /** * Type for event handler functions that can be registered with a CoAP * context using the unction coap_set_event_handler(). When called by - * the library, the first argument will be the coap_context_t object - * where the handler function has been registered. The second argument - * is the event type that may be complemented by event-specific data - * passed as the third argument. + * the library, the first argument will be the current coap_session_t object + * which is associated with the original CoAP context. The second parameter + * is the event type. */ -typedef int (*coap_event_handler_t)(coap_context_t *, - coap_event_t event, - coap_session_t *session); +typedef int (*coap_event_handler_t)(coap_session_t *session, + const coap_event_t event); /** * Registers the function @p hnd as callback for events from the given diff --git a/include/coap3/net.h b/include/coap3/net.h index a4fec3e1..59778a20 100644 --- a/include/coap3/net.h +++ b/include/coap3/net.h @@ -41,60 +41,52 @@ typedef enum coap_response_t { /** * Response handler that is used as callback in coap_context_t. * - * @param context CoAP session. * @param session CoAP session. * @param sent The PDU that was transmitted. * @param received The PDU that was received. - * @param id CoAP transaction ID. + * @param mid CoAP transaction ID. * @return @c COAP_RESPONSE_OK if successful, else @c COAP_RESPONSE_FAIL which * triggers sending a RST packet. */ -typedef coap_response_t (*coap_response_handler_t)(coap_context_t *context, - coap_session_t *session, - coap_pdu_t *sent, - coap_pdu_t *received, - const coap_mid_t id); +typedef coap_response_t (*coap_response_handler_t)(coap_session_t *session, + const coap_pdu_t *sent, + const coap_pdu_t *received, + const coap_mid_t mid); /** * Negative Acknowedge handler that is used as callback in coap_context_t. * - * @param context CoAP session. * @param session CoAP session. * @param sent The PDU that was transmitted. * @param reason The reason for the NACK. * @param id CoAP message ID. */ -typedef void (*coap_nack_handler_t)(coap_context_t *context, - coap_session_t *session, - coap_pdu_t *sent, - coap_nack_reason_t reason, +typedef void (*coap_nack_handler_t)(coap_session_t *session, + const coap_pdu_t *sent, + const coap_nack_reason_t reason, const coap_mid_t id); /** * Received Ping handler that is used as callback in coap_context_t. * - * @param context CoAP session. * @param session CoAP session. * @param received The PDU that was received. * @param id CoAP message ID. */ -typedef void (*coap_ping_handler_t)(coap_context_t *context, - coap_session_t *session, - coap_pdu_t *received, +typedef void (*coap_ping_handler_t)(coap_session_t *session, + const coap_pdu_t *received, const coap_mid_t id); /** * Received Pong handler that is used as callback in coap_context_t. * - * @param context CoAP session. * @param session CoAP session. * @param received The PDU that was received. * @param id CoAP message ID. */ -typedef void (*coap_pong_handler_t)(coap_context_t *context, - coap_session_t *session, - coap_pdu_t *received, +typedef void (*coap_pong_handler_t)(coap_session_t *session, + const coap_pdu_t *received, const coap_mid_t id); /** diff --git a/include/coap3/resource.h b/include/coap3/resource.h index 02233ad0..19260939 100644 --- a/include/coap3/resource.h +++ b/include/coap3/resource.h @@ -38,12 +38,10 @@ * Definition of message handler function */ typedef void (*coap_method_handler_t) - (coap_context_t *, - coap_resource_t *, + (coap_resource_t *, coap_session_t *, - coap_pdu_t *, - coap_binary_t * /* token */, - coap_string_t * /* query string */, + const coap_pdu_t * /* request */, + const coap_string_t * /* query string */, coap_pdu_t * /* response */); #define COAP_ATTR_FLAGS_RELEASE_NAME 0x1 diff --git a/man/coap_async.txt.in b/man/coap_async.txt.in index a9919304..1b0669aa 100644 --- a/man/coap_async.txt.in +++ b/man/coap_async.txt.in @@ -23,15 +23,15 @@ SYNOPSIS -------- *#include * -*coap_async_t *coap_register_async(coap_context_t *_context_, -coap_session_t *_session_, coap_pdu_t *_request_, coap_tick_t _delay_);* +*coap_async_t *coap_register_async(coap_session_t *_session_, +const coap_pdu_t *_request_, coap_tick_t _delay_);* *void coap_async_set_delay(coap_async_t *_async_, coap_tick_t _delay_);* -*void coap_free_async(coap_context_t *_context_, coap_async_t *_async_);* +*void coap_free_async(coap_session_t *_session_, coap_async_t *_async_);* -*coap_async_t *coap_find_async(coap_context_t *_context_, -coap_session_t *_session_, coap_binary_t _token_);* +*coap_async_t *coap_find_async(coap_session_t *_session_, +coap_bin_const_t _token_);* *void coap_async_set_app_data(coap_async_t *_async_, void *_app_data_);* @@ -59,7 +59,7 @@ Usually responses are piggybacked, but this man page focuses on separate (async) support. The *coap_register_async*() function is used to set up an asynchronous delayed -request for the _request_ PDU associated with the _context_ and _session_. The +request for the _request_ PDU associated with the _session_. The application request handler will get called with a copy of _request_ after _delay_ ticks which will then cause a response to be sent. If _delay_ is 0, then the application request handler will not get called. @@ -75,7 +75,7 @@ then when the response is ready at an indeterminate point in the future, The *coap_free_async*() function is used to delete an _async_ definition. The *coap_find_async*() function is used to determine if there is an async -definition based on the _context_, _session_ and token _token_. +definition based on the _session_ and token _token_. The *coap_async_set_app_data*() function is used to add in user defined _app_data_ to the _async_ definition. It is the responsibility of the @@ -105,54 +105,54 @@ EXAMPLES * response (empty ACK followed by data response at a later stage). */ static void -hnd_get_with_delay(coap_context_t *context, +hnd_get_with_delay(coap_session_t *session, coap_resource_t *resource, - coap_session_t *session, coap_pdu_t *request, - coap_binary_t *token, coap_string_t *query, coap_pdu_t *response) { unsigned long delay = 5; size_t size; - coap_async_t *async; - coap_binary_t zero_token = {0, NULL}; /* * See if this is the initial, or delayed request */ - async = coap_find_async(context, session, token ? *token : zero_token); - if (!async) { - /* Set up an async request to trigger delay in the future */ - if (query) { - const uint8_t *p = query->s; + if (request) { + coap_async_t *async; + coap_bin_const_t token = coap_pdu_get_token(request); - delay = 0; - for (size = query->length; size; --size, ++p) - delay = delay * 10 + (*p - '0'); - if (delay == 0) { - coap_log(LOG_INFO, "async: delay of 0 not supported\n"); - coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST); + async = coap_find_async(session, token); + if (!async) { + /* Set up an async request to trigger delay in the future */ + if (query) { + const uint8_t *p = query->s; + + delay = 0; + for (size = query->length; size; --size, ++p) + delay = delay * 10 + (*p - '0'); + if (delay == 0) { + coap_log(LOG_INFO, "async: delay of 0 not supported\n"); + coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST); + return; + } + } + async = coap_register_async(session, + request, + COAP_TICKS_PER_SECOND * delay); + if (async == NULL) { + coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); return; } - } - async = coap_register_async(context, - session, - request, - COAP_TICKS_PER_SECOND * delay); - if (async == NULL) { - coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); + /* + * Not setting response code will cause empty ACK to be sent + * if Confirmable + */ return; } - /* - * Not setting response code will cause empty ACK to be sent - * if Confirmable - */ - return; } - /* async set up, so this is the delayed request */ + /* no request (observe) or async set up, so this is the delayed request */ /* Send back the appropriate data */ - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, COAP_MEDIATYPE_TEXT_PLAIN, -1, 0, 4, (const uint8_t *)"done", NULL, NULL); coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); diff --git a/man/coap_block.txt.in b/man/coap_block.txt.in index ec3f5d44..faf00ce6 100644 --- a/man/coap_block.txt.in +++ b/man/coap_block.txt.in @@ -26,14 +26,14 @@ SYNOPSIS *void coap_context_set_block_mode(coap_context_t *_context_, uint8_t _block_mode_);* -*int coap_add_data_large_request(coap_session_t *_session_, coap_pdu_t *_pdu_, -size_t _length_, const uint8_t *_data_, +*int coap_add_data_large_request(coap_session_t *_session_, +coap_pdu_t *_pdu_, size_t _length_, const uint8_t *_data_, coap_release_large_data_t _release_func_, void *_app_ptr_);* *int coap_add_data_large_response(coap_resource_t *_resource_, -coap_session_t *_session_, coap_pdu_t *_request_, coap_pdu_t *_response_, -const coap_binary_t *_token_, const coap_string_t *query, uint16_t _media_type_, -int _maxage_, uint64_t etag, size_t _length_, const uint8_t *_data_, +coap_session_t *_session_, const coap_pdu_t *_request_, coap_pdu_t *_response_, +const coap_string_t *query, uint16_t _media_type_, int _maxage_, +uint64_t etag, size_t _length_, const uint8_t *_data_, coap_release_large_data_t _release_func_, void *_app_ptr_);* *int coap_get_data_large(const coap_pdu_t *_pdu_, size_t *_length, @@ -143,7 +143,7 @@ _length_ to the PDU _pdu_. _release_func_ (if not NULL) and _app_ptr_ are used for releasing the data when the body transfer is complete. It also adds in the appropriate CoAP options such as BLOCK2, SIZE2 and ETAG to handle Block-Wise transfer if the data does not fit into a single PDU. -_resource_, _query_, _session_, _request_, _response_ and _token_ are the same +_resource_, _query_, _session_, _request_, and _response_ are the same parameters as in the called resource handler that invokes *coap_add_data_large_response*(). If _etag_ is 0, then a unique ETag value will be generated, else is the ETag value to use. @@ -333,20 +333,15 @@ int main(int argc, char *argv[]) { #include static void -hnd_get_time(coap_context_t *context, coap_resource_t *resource, -coap_session_t *session, coap_pdu_t *request, coap_binary_t *token, -coap_string_t *query, coap_pdu_t *response) { +hnd_get_time(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; time_t now; - /* Remove (void) definition if variable is used */ - (void)context; /* Note that request may be NULL if triggered by an observe response */ - /* Note that token is already in the response pdu */ - /* ... Additional analysis code for resource, request pdu etc. ... */ /* After analysis, generate a failure response and return if needed */ @@ -381,7 +376,7 @@ coap_string_t *query, coap_pdu_t *response) { * SIZE2 Option added internally * ETAG Option added internally */ - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, COAP_MEDIATYPE_TEXT_PLAIN, 1, 0, len, buf, diff --git a/man/coap_handler.txt.in b/man/coap_handler.txt.in index 079ad51e..eaf96e9d 100644 --- a/man/coap_handler.txt.in +++ b/man/coap_handler.txt.in @@ -208,22 +208,17 @@ EXAMPLES #include static void -hnd_get_time(coap_context_t *context, coap_resource_t *resource, -coap_session_t *session, coap_pdu_t *request, coap_binary_t *token, -coap_string_t *query, coap_pdu_t *response) { +hnd_get_time(coap_resource_t *resource, coap_session_t *session, +coap_pdu_t *request, coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; time_t now; - /* Remove (void) definition if variable is used */ - (void)context; /* ... Additional analysis code for resource, request pdu etc. ... */ /* After analysis, generate a suitable response */ - /* Note that token, if set, is already in the response pdu */ - now = time(NULL); if (query != NULL && coap_string_equal(query, coap_make_str_const("secs"))) { @@ -252,7 +247,7 @@ coap_string_t *query, coap_pdu_t *response) { * BLOCK2 Option added internally if output too large * ETAG Option added internally */ - coap_add_data_large_response(resource, session, request, response, token, + coap_add_data_large_response(resource, session, request, response, query, COAP_MEDIATYPE_TEXT_PLAIN, 1, 0, len, buf, NULL, 0); diff --git a/man/coap_observe.txt.in b/man/coap_observe.txt.in index a5dfd983..3f06a8bf 100644 --- a/man/coap_observe.txt.in +++ b/man/coap_observe.txt.in @@ -139,15 +139,14 @@ coap_resource_t *time_resource = NULL; /* specific GET "time" handler, called from hnd_get_generic() */ static void -hnd_get_time(coap_context_t *context, coap_resource_t *resource, -coap_session_t *session, coap_pdu_t *request, coap_binary_t *token, -coap_string_t *query, coap_pdu_t *response) { +hnd_get_time(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; time_t now; - /* Remove (void) definition if variable is used */ - (void)context; + (void)resource; + (void)session; /* ... Additional analysis code for resource, request pdu etc. ... */ @@ -184,18 +183,15 @@ coap_string_t *query, coap_pdu_t *response) { * SIZE2 Option added internally * ETAG Option added internally */ - coap_add_data_blocked_response(resource, session, request, response, token, - COAP_MEDIATYPE_TEXT_PLAIN, 1, - len, - buf); + coap_add_data_blocked_response(request, response, COAP_MEDIATYPE_TEXT_PLAIN, + 1, len, buf); } /* Generic GET handler */ static void -hnd_get_generic(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) { +hnd_get_generic(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { coap_str_const_t *uri_path = coap_resource_get_uri_path(resource); @@ -207,8 +203,7 @@ coap_string_t *query, coap_pdu_t *response) { /* Is this the "time" resource" ? */ if (coap_string_equal(uri_path, coap_make_str_const("time"))) { - hnd_get_time(ctx, resource, session, request, token, query, - response); + hnd_get_time(resource, session, request, query, response); return; } diff --git a/man/coap_pdu_setup.txt.in b/man/coap_pdu_setup.txt.in index 7bc0f28f..1a0652fa 100644 --- a/man/coap_pdu_setup.txt.in +++ b/man/coap_pdu_setup.txt.in @@ -73,10 +73,9 @@ const uint8_t *_data_);* *int coap_add_data(coap_pdu_t *_pdu_, size_t _length_, const uint8_t *_data_);* -*void coap_add_data_blocked_response(coap_resource_t *_resource_, -coap_session_t *_session_, coap_pdu_t *_request_, coap_pdu_t *_response_, -const coap_binary_t *_token_, uint16_t _media_type_, int _maxage_, -size_t _length_, const uint8_t *_data_);* +*void coap_add_data_blocked_response(const coap_pdu_t *_request_, +coap_pdu_t *_response_, uint16_t _media_type_, int _maxage_, size_t _length_, +const uint8_t *_data_);* *coap_mid_t coap_send(coap_session_t *_session_, coap_pdu_t *_pdu_);* @@ -361,9 +360,8 @@ of the payload _data_ of length _length_ to the PDU _pdu_. It should be used as a direct replacement for *coap_add_data*() if it is possible that the data will not fit into a single pdu. It also adds in the appropriate CoAP options to handle Block-Wise transfer. This function is usually used for -a server's GET / FETCH response. The _resource_, _session_, _request_, -_response_ and _token_ are the same parameters for the registered GET / FETCH -resource handler. +a server's GET / FETCH response. The _request_ and _response_ are the same +parameters for the registered GET / FETCH resource handler. The _media_type_ is for the format of the _data_ and _maxage_ defines the lifetime of the response. If set to -1, then the MAXAGE option does not get included. This function must only be called once per _pdu_. @@ -512,22 +510,19 @@ error: #include static void -hnd_get_time(coap_context_t *context, coap_resource_t *resource, -coap_session_t *session, coap_pdu_t *request, coap_binary_t *token, -coap_string_t *query, coap_pdu_t *response) { +hnd_get_time(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[40]; size_t len; time_t now; - /* Remove (void) definition if variable is used */ - (void)context; + (void)resource; + (void)session; /* ... Additional analysis code for resource, request pdu etc. ... */ /* After analysis, generate a suitable response */ - /* Note that token is already in the response pdu */ - now = time(NULL); if (query != NULL && coap_string_equal(query, coap_make_str_const("secs"))) { @@ -557,10 +552,8 @@ coap_string_t *query, coap_pdu_t *response) { * SIZE2 Option added internally * ETAG Option added internally */ - coap_add_data_blocked_response(resource, session, request, response, token, - COAP_MEDIATYPE_TEXT_PLAIN, 1, - len, - buf); + coap_add_data_blocked_response(request, response, COAP_MEDIATYPE_TEXT_PLAIN, + 1, len, buf); } ---- diff --git a/man/coap_resource.txt.in b/man/coap_resource.txt.in index 3e3a4953..8d855d49 100644 --- a/man/coap_resource.txt.in +++ b/man/coap_resource.txt.in @@ -195,16 +195,13 @@ EXAMPLES #define INDEX "This is an example server using libcoap\n" static void -hnd_get_index(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) { +hnd_get_index(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { unsigned char buf[3]; /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; (void)session; (void)request; - (void)token; (void)query; coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); @@ -225,15 +222,12 @@ coap_string_t *query, coap_pdu_t *response) { } static void -hnd_delete_time(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) { +hnd_delete_time(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; (void)session; (void)request; - (void)token; (void)query; /* .. code .. */ @@ -242,15 +236,12 @@ coap_string_t *query, coap_pdu_t *response) { } static void -hnd_get_time(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) { +hnd_get_time(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; (void)session; (void)request; - (void)token; (void)query; (void)response; @@ -260,15 +251,12 @@ coap_string_t *query, coap_pdu_t *response) { } static void -hnd_put_time(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) { +hnd_put_time(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; (void)session; (void)request; - (void)token; (void)query; (void)response; @@ -325,20 +313,18 @@ init_resources(coap_context_t *ctx) { * Unknown Resource PUT handler */ static void -hnd_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) { +hnd_delete(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { /* Remove (void) definition if variable is used */ (void)session; (void)request; - (void)token; (void)query; (void)response; /* .. code .. */ /* Dynamic resource no longer required - delete it */ - coap_delete_resource(ctx, resource); + coap_delete_resource(coap_session_get_context(session), resource); coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED); } @@ -347,17 +333,14 @@ coap_string_t *query, coap_pdu_t *response) { * Unknown Resource PUT handler */ static void -hnd_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) { +hnd_get(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { coap_str_const_t *get_uri_path; /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; (void)session; (void)request; - (void)token; (void)query; (void)response; @@ -378,14 +361,11 @@ coap_string_t *query, coap_pdu_t *response) { * Unknown Resource PUT handler */ static void -hnd_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) { +hnd_put(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; (void)session; - (void)token; (void)query; coap_string_t *put_uri_path; @@ -430,11 +410,9 @@ check_url_fn(coap_string_t *uri_path, uint8_t code) { /* Unknown Resource PUT handler */ static void -hnd_unknown_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) { +hnd_unknown_put(coap_resource_t *resource, coap_session_t *session, +const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { /* Remove (void) definition if variable is used */ - (void)ctx; (void)resource; coap_pdu_code_t req_code = coap_pdu_get_code(request); @@ -466,10 +444,10 @@ coap_string_t *query, coap_pdu_t *response) { /* We possibly want to Observe the GETs */ coap_resource_set_get_observable(r, 1); coap_register_handler(r, COAP_REQUEST_GET, hnd_get); - coap_add_resource(ctx, r); + coap_add_resource(coap_session_get_context(session), r); /* Do the PUT for this first call */ - hnd_put(ctx, r, session, request, token, query, response); + hnd_put(r, session, request, query, response); return; diff --git a/src/async.c b/src/async.c index d6e41a6f..8ef4ca91 100644 --- a/src/async.c +++ b/src/async.c @@ -33,8 +33,8 @@ coap_async_is_supported(void) { } coap_async_t * -coap_register_async(coap_context_t *context, coap_session_t *session, - coap_pdu_t *request, coap_tick_t delay) { +coap_register_async(coap_session_t *session, + const coap_pdu_t *request, coap_tick_t delay) { coap_async_t *s; coap_mid_t mid = request->mid; size_t len; @@ -43,7 +43,7 @@ coap_register_async(coap_context_t *context, coap_session_t *session, if (!COAP_PDU_IS_REQUEST(request)) return NULL; - SEARCH_PAIR(context->async_state, s, + SEARCH_PAIR(session->context->async_state, s, session, session, pdu->token_length, request->token_length, pdu->token, request->token); @@ -66,7 +66,7 @@ coap_register_async(coap_context_t *context, coap_session_t *session, s->pdu = coap_pdu_duplicate(request, session, request->token_length, request->token, NULL); if (s->pdu == NULL) { - coap_free_async(context, s); + coap_free_async(session, s); coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n"); return NULL; } @@ -80,7 +80,7 @@ coap_register_async(coap_context_t *context, coap_session_t *session, coap_async_set_delay(s, delay); - LL_PREPEND(context->async_state, s); + LL_PREPEND(session->context->async_state, s); return s; } @@ -104,18 +104,17 @@ coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { coap_async_t * -coap_find_async(coap_context_t *context, coap_session_t *session, - coap_binary_t token) { +coap_find_async(coap_session_t *session, coap_bin_const_t token) { coap_async_t *tmp; - SEARCH_PAIR(context->async_state, tmp, + SEARCH_PAIR(session->context->async_state, tmp, session, session, pdu->token_length, token.length, pdu->token, token.s); return tmp; } -void -coap_free_async(coap_context_t *context, coap_async_t *s) { +static void +coap_free_async_sub(coap_context_t *context, coap_async_t *s) { if (s) { LL_DELETE(context->async_state,s); if (s->session) { @@ -129,12 +128,17 @@ coap_free_async(coap_context_t *context, coap_async_t *s) { } } +void +coap_free_async(coap_session_t *session, coap_async_t *s) { + coap_free_async_sub(session->context, s); +} + void coap_delete_all_async(coap_context_t *context) { coap_async_t *astate, *tmp; LL_FOREACH_SAFE(context->async_state, astate, tmp) { - coap_free_async(context, astate); + coap_free_async_sub(context, astate); } context->async_state = NULL; } @@ -183,7 +187,7 @@ coap_free_async(coap_context_t *context, coap_async_t *async) { coap_async_t * coap_find_async(coap_context_t *context, coap_session_t *session, - coap_binary_t token) { + coap_bin_const_t token) { (void)context; (void)session; (void)token; diff --git a/src/block.c b/src/block.c index f1c0aa63..384b1fd3 100644 --- a/src/block.c +++ b/src/block.c @@ -32,7 +32,8 @@ coap_opt_block_num(const coap_opt_t *block_opt) { } int -coap_get_block(coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block) { +coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, + coap_block_t *block) { coap_opt_iterator_t opt_iter; coap_opt_t *option; @@ -111,10 +112,10 @@ coap_write_block_opt(coap_block_t *block, coap_option_num_t number, } /* to re-encode the block option */ - coap_add_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf), - ((block->num << 4) | - (block->m << 3) | - block->szx)), + coap_insert_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf), + ((block->num << 4) | + (block->m << 3) | + block->szx)), buf); return 1; @@ -138,11 +139,8 @@ coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, * Note that the COAP_OPTION_ have to be added in the correct order */ void -coap_add_data_blocked_response(coap_resource_t *resource, - coap_session_t *session, - coap_pdu_t *request, +coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, - const coap_binary_t *token, uint16_t media_type, int maxage, size_t length, @@ -152,8 +150,6 @@ coap_add_data_blocked_response(coap_resource_t *resource, unsigned char buf[4]; coap_block_t block2 = { 0, 0, 0 }; int block2_requested = 0; - coap_subscription_t *subscription = coap_find_observer(resource, - session, token); /* * Need to check that a valid block is getting asked for so that the @@ -171,11 +167,6 @@ coap_add_data_blocked_response(coap_resource_t *resource, } } } - else if (subscription && subscription->has_block2) { - block2 = subscription->block; - block2.num = 0; - block2_requested = 1; - } response->code = COAP_RESPONSE_CODE(205); /* add etag for the resource */ @@ -183,20 +174,13 @@ coap_add_data_blocked_response(coap_resource_t *resource, coap_hash(data, length, etag); coap_add_option(response, COAP_OPTION_ETAG, sizeof(etag), etag); - if ((block2.num == 0) && subscription) { - coap_add_option(response, COAP_OPTION_OBSERVE, - coap_encode_var_safe(buf, sizeof (buf), - resource->observe), - buf); - } - - coap_add_option(response, COAP_OPTION_CONTENT_FORMAT, + coap_insert_option(response, COAP_OPTION_CONTENT_FORMAT, coap_encode_var_safe(buf, sizeof(buf), media_type), buf); if (maxage >= 0) { - coap_add_option(response, + coap_insert_option(response, COAP_OPTION_MAXAGE, coap_encode_var_safe(buf, sizeof(buf), maxage), buf); } @@ -639,9 +623,8 @@ coap_add_data_large_request(coap_session_t *session, int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, - coap_pdu_t *request, + const coap_pdu_t *request, coap_pdu_t *response, - const coap_binary_t *token, const coap_string_t *query, uint16_t media_type, int maxage, @@ -654,8 +637,6 @@ coap_add_data_large_response(coap_resource_t *resource, unsigned char buf[4]; coap_block_t block = { 0, 0, 0 }; int block_requested = 0; - coap_subscription_t *subscription = coap_find_observer(resource, session, - token); uint16_t block_opt = COAP_OPTION_BLOCK2; /* @@ -674,29 +655,16 @@ coap_add_data_large_response(coap_resource_t *resource, } } } - else if (subscription && subscription->has_block2) { - block = subscription->block; - block.num = 0; - block_requested = 1; - block_opt = COAP_OPTION_BLOCK2; - } - if ((block.num == 0) && subscription) { - coap_add_option(response, COAP_OPTION_OBSERVE, - coap_encode_var_safe(buf, sizeof (buf), - resource->observe), - buf); - } - - coap_add_option(response, COAP_OPTION_CONTENT_TYPE, - coap_encode_var_safe(buf, sizeof(buf), - media_type), - buf); + coap_insert_option(response, COAP_OPTION_CONTENT_TYPE, + coap_encode_var_safe(buf, sizeof(buf), + media_type), + buf); if (maxage >= 0) { - coap_add_option(response, - COAP_OPTION_MAXAGE, - coap_encode_var_safe(buf, sizeof(buf), maxage), buf); + coap_insert_option(response, + COAP_OPTION_MAXAGE, + coap_encode_var_safe(buf, sizeof(buf), maxage), buf); } if (block_requested) { @@ -1244,7 +1212,6 @@ coap_handle_request_put_block(coap_context_t *context, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, - coap_binary_t *token, coap_string_t *query, coap_method_handler_t h, int *added_block) { @@ -1389,7 +1356,7 @@ coap_handle_request_put_block(coap_context_t *context, coap_log(LOG_DEBUG, "Server app vesion of updated PDU\n"); coap_show_pdu(LOG_DEBUG, pdu); /* Need to do this here as we need to free off p */ - h(context, resource, session, pdu, token, query, response); + h(resource, session, pdu, query, response); /* Check if lg_xmit generated and update PDU code if so */ coap_check_code_lg_xmit(session, response, resource, query); /* Last chunk - free off shortly */ @@ -1414,7 +1381,7 @@ coap_handle_request_put_block(coap_context_t *context, (block.m << 3) | block.szx), buf); - h(context, resource, session, pdu, token, query, response); + h(resource, session, pdu, query, response); /* Check if lg_xmit generated and update PDU code if so */ coap_check_code_lg_xmit(session, response, resource, query); if (COAP_RESPONSE_CLASS(response->code) == 2) { @@ -1852,8 +1819,7 @@ coap_handle_response_get_block(coap_context_t *context, coap_log(LOG_DEBUG, "Client app vesion of updated PDU\n"); coap_show_pdu(LOG_DEBUG, rcvd); } - context->response_handler(context, session, sent, rcvd, - rcvd->mid); + context->response_handler(session, sent, rcvd, rcvd->mid); } app_has_response = 1; /* Set up for the next data body if observing */ @@ -1890,8 +1856,7 @@ block_mode: if (context->response_handler) { coap_log(LOG_DEBUG, "Client app vesion of updated PDU\n"); coap_show_pdu(LOG_DEBUG, rcvd); - context->response_handler(context, session, sent, rcvd, - rcvd->mid); + context->response_handler(session, sent, rcvd, rcvd->mid); } app_has_response = 1; } diff --git a/src/coap_session.c b/src/coap_session.c index 818ce28a..75d4906a 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -207,7 +207,7 @@ void coap_session_mfree(coap_session_t *session) { } LL_FOREACH_SAFE(session->delayqueue, q, tmp) { if (q->pdu->type==COAP_MESSAGE_CON && session->context && session->context->nack_handler) - session->context->nack_handler(session->context, session, q->pdu, session->proto == COAP_PROTO_DTLS ? COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, q->id); + session->context->nack_handler(session, q->pdu, session->proto == COAP_PROTO_DTLS ? COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, q->id); coap_delete_node(q); } LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) { @@ -496,8 +496,7 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso /* Make sure that we try a re-transmit later on ICMP error */ if (coap_wait_ack(session->context, session, q) >= 0) { if (session->context->nack_handler) { - session->context->nack_handler(session->context, session, q->pdu, - reason, q->id); + session->context->nack_handler(session, q->pdu, reason, q->id); } q = NULL; } @@ -505,8 +504,7 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso if (q && q->pdu->type == COAP_MESSAGE_CON && session->context->nack_handler) { - session->context->nack_handler(session->context, session, q->pdu, - reason, q->id); + session->context->nack_handler(session, q->pdu, reason, q->id); } if (q) coap_delete_node(q); @@ -518,8 +516,7 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso coap_queue_t *q = session->context->sendqueue; while (q) { if (q->session == session) { - session->context->nack_handler(session->context, session, q->pdu, - reason, q->id); + session->context->nack_handler(session, q->pdu, reason, q->id); } q = q->next; } diff --git a/src/net.c b/src/net.c index b28da8a9..125f2ed4 100644 --- a/src/net.c +++ b/src/net.c @@ -1411,7 +1411,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { /* And finally delete the node */ if (node->pdu->type == COAP_MESSAGE_CON && context->nack_handler) - context->nack_handler(context, node->session, node->pdu, COAP_NACK_TOO_MANY_RETRIES, node->id); + context->nack_handler(node->session, node->pdu, COAP_NACK_TOO_MANY_RETRIES, node->id); coap_delete_node(node); return COAP_INVALID_MID; } @@ -1986,7 +1986,7 @@ coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n", coap_session_str(session), q->id); if (q->pdu->type == COAP_MESSAGE_CON && context->nack_handler) - context->nack_handler(context, session, q->pdu, reason, q->id); + context->nack_handler(session, q->pdu, reason, q->id); coap_delete_node(q); } @@ -2002,7 +2002,7 @@ coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n", coap_session_str(session), q->id); if (q->pdu->type == COAP_MESSAGE_CON && context->nack_handler) - context->nack_handler(context, session, q->pdu, reason, q->id); + context->nack_handler(session, q->pdu, reason, q->id); coap_delete_node(q); q = p->next; } else { @@ -2496,6 +2496,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu int skip_hop_limit_check = 0; int resp; coap_binary_t token = { pdu->token_length, pdu->token }; + coap_bin_const_t tokenc = { pdu->token_length, pdu->token }; #ifndef WITHOUT_ASYNC coap_async_t *async; #endif /* WITHOUT_ASYNC */ @@ -2507,7 +2508,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu } } #ifndef WITHOUT_ASYNC - async = coap_find_async(context, session, token); + async = coap_find_async(session, tokenc); if (async) { coap_tick_t now; @@ -2529,7 +2530,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu coap_prng(&r, sizeof(r)); delay = (COAP_DEFAULT_LEISURE * COAP_TICKS_PER_SECOND * r) / 256; /* Register request to be internally re-transmitted after delay */ - if (coap_register_async(context, session, pdu, delay)) + if (coap_register_async(session, pdu, delay)) return; } #endif /* WITHOUT_ASYNC */ @@ -2772,16 +2773,26 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu if (coap_get_block(pdu, COAP_OPTION_BLOCK2, &block)) { has_block2 = 1; + if (block.num != 0) { + response->code = COAP_RESPONSE_CODE(400); + goto skip_handler; + } } subscription = coap_add_observer(resource, session, &token, query, has_block2, block, pdu->code); if (subscription) { + uint8_t buf[4]; + /* Ownership of query is taken by subscription if not * NULL. In this case, we must not delete query here * hence owns_query is cleared. */ owns_query = 0; coap_touch_observer(context, session, &token); + coap_add_option(response, COAP_OPTION_OBSERVE, + coap_encode_var_safe(buf, sizeof (buf), + resource->observe), + buf); } } else if (observe_action == COAP_OBSERVE_CANCEL) { @@ -2795,7 +2806,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) { if (coap_handle_request_put_block(context, session, pdu, response, - resource, uri_path, observe, &token, + resource, uri_path, observe, query, h, &added_block)) { goto skip_handler; } @@ -2809,7 +2820,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu /* * Call the request handler with everything set up */ - h(context, resource, session, pdu, &token, query, response); + h(resource, session, pdu, query, response); /* Check if lg_xmit generated and update PDU code if so */ coap_check_code_lg_xmit(session, response, resource, query); @@ -2914,7 +2925,7 @@ handle_response(coap_context_t *context, coap_session_t *session, /* Call application-specific response handler when available. */ if (context->response_handler) { - if (context->response_handler(context, session, sent, rcvd, + if (context->response_handler(session, sent, rcvd, rcvd->mid) == COAP_RESPONSE_FAIL) coap_send_rst(session, rcvd); else @@ -2949,7 +2960,7 @@ handle_signaling(coap_context_t *context, coap_session_t *session, } else if (pdu->code == COAP_SIGNALING_CODE_PING) { coap_pdu_t *pong = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PONG, 0, 1); if (context->ping_handler) { - context->ping_handler(context, session, pdu, pdu->mid); + context->ping_handler(session, pdu, pdu->mid); } if (pong) { coap_add_option(pong, COAP_SIGNALING_OPTION_CUSTODY, 0, NULL); @@ -2958,7 +2969,7 @@ handle_signaling(coap_context_t *context, coap_session_t *session, } else if (pdu->code == COAP_SIGNALING_CODE_PONG) { session->last_pong = session->last_rx_tx; if (context->pong_handler) { - context->pong_handler(context, session, pdu, pdu->mid); + context->pong_handler(session, pdu, pdu->mid); } } else if (pdu->code == COAP_SIGNALING_CODE_RELEASE || pdu->code == COAP_SIGNALING_CODE_ABORT) { @@ -3047,12 +3058,12 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, if (!is_ping_rst) { if(sent->pdu->type==COAP_MESSAGE_CON && context->nack_handler) - context->nack_handler(context, sent->session, sent->pdu, + context->nack_handler(sent->session, sent->pdu, COAP_NACK_RST, sent->id); } else { if (context->pong_handler) { - context->pong_handler(context, session, pdu, pdu->mid); + context->pong_handler(session, pdu, pdu->mid); } session->last_pong = session->last_rx_tx; session->last_ping_mid = COAP_INVALID_MID; @@ -3122,8 +3133,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, else { if (COAP_PDU_IS_EMPTY(pdu)) { if (context->ping_handler) { - context->ping_handler(context, session, - pdu, pdu->mid); + context->ping_handler(session, pdu, pdu->mid); } } coap_log(LOG_DEBUG, "dropped message with invalid code (%d.%02d)\n", @@ -3156,7 +3166,7 @@ coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *s coap_log(LOG_DEBUG, "***EVENT: 0x%04x\n", event); if (context->handle_event) { - return context->handle_event(context, event, session); + return context->handle_event(session, event); } else { return 0; } @@ -3198,7 +3208,7 @@ coap_check_async(coap_context_t *context, coap_tick_t now) { handle_request(context, async->session, async->pdu); /* Remove this async entry as it has now fired */ - coap_free_async(context, async); + coap_free_async(async->session, async); } else { if (next_due == 0 || next_due > async->delay - now) diff --git a/src/resource.c b/src/resource.c index b2273701..9ea29088 100644 --- a/src/resource.c +++ b/src/resource.c @@ -849,6 +849,7 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, coap_subscription_t *obs; coap_binary_t token; coap_pdu_t *response; + uint8_t buf[4]; if (r->observable && (r->dirty || r->partiallydirty)) { r->partiallydirty = 0; @@ -910,11 +911,24 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, switch (deleting) { case COAP_NOT_DELETING_RESOURCE: /* fill with observer-specific data */ + coap_add_option(response, COAP_OPTION_OBSERVE, + coap_encode_var_safe(buf, sizeof (buf), + r->observe), + buf); + if (obs->has_block2) { + /* Will get updated later (e.g. M bit) if appropriate */ + coap_add_option(response, COAP_OPTION_BLOCK2, + coap_encode_var_safe(buf, sizeof(buf), + ((0 << 4) | + (0 << 3) | + obs->block.szx)), + buf); + } h = r->handler[obs->code - 1]; assert(h); /* we do not allow subscriptions if no * GET/FETCH handler is defined */ - h(context, r, obs->session, NULL, &token, obs->query, response); + h(r, obs->session, NULL, obs->query, response); /* Check if lg_xmit generated and update PDU code if so */ coap_check_code_lg_xmit(obs->session, response, r, obs->query); if (COAP_RESPONSE_CLASS(response->code) > 2) {