diff --git a/coap-observer.c b/coap-observer.c index 1ffb635a..e4b2fe5c 100644 --- a/coap-observer.c +++ b/coap-observer.c @@ -169,7 +169,8 @@ PROCESS_THREAD(coap_server_process, ev, data) coap_add_option(request, COAP_OPTION_TOKEN, 2, buf); } - coap_send_confirmed(coap_context, &dst, request); + if (COAP_INVALID_TID == coap_send_confirmed(coap_context, &dst, request)) + coap_delete_pdu(request); while(1) { PROCESS_YIELD(); diff --git a/examples/client.c b/examples/client.c index d9711bd2..3e4dd10d 100644 --- a/examples/client.c +++ b/examples/client.c @@ -190,7 +190,8 @@ clear_obs(coap_context_t *ctx, const coap_address_t *remote) { if (tid == COAP_INVALID_TID) { debug("clear_obs: error sending new request"); coap_delete_pdu(pdu); - } + } else if (pdu->hdr->type != COAP_MESSAGE_CON) + coap_delete_pdu(pdu); } return tid; } @@ -387,9 +388,13 @@ message_handler(struct coap_context_t *ctx, if (tid == COAP_INVALID_TID) { debug("message_handler: error sending new request"); - coap_delete_pdu(pdu); - } else + coap_delete_pdu(pdu); + } else { set_timeout(&max_wait, wait_seconds); + if (received->hdr->type != COAP_MESSAGE_CON) + coap_delete_pdu(pdu); + } + return; } } @@ -413,8 +418,8 @@ message_handler(struct coap_context_t *ctx, /* finally send new request, if needed */ if (pdu && coap_send(ctx, remote, pdu) == COAP_INVALID_TID) { debug("message_handler: error sending response"); - coap_delete_pdu(pdu); } + coap_delete_pdu(pdu); /* our job is done, we can exit at any time */ ready = coap_check_option(received, COAP_OPTION_SUBSCRIPTION, &opt_iter) == NULL; @@ -908,6 +913,7 @@ main(int argc, char **argv) { int opt, res; char *group = NULL; coap_log_t log_level = LOG_WARN; + coap_tid_t tid = COAP_INVALID_TID; while ((opt = getopt(argc, argv, "Nb:e:f:g:m:p:s:t:o:v:A:B:O:P:T:")) != -1) { switch (opt) { @@ -1065,9 +1071,12 @@ main(int argc, char **argv) { #endif if (pdu->hdr->type == COAP_MESSAGE_CON) - coap_send_confirmed(ctx, &dst, pdu); + tid = coap_send_confirmed(ctx, &dst, pdu); else - coap_send(ctx, &dst, pdu); + tid = coap_send(ctx, &dst, pdu); + + if (pdu->hdr->type != COAP_MESSAGE_CON || tid == COAP_INVALID_TID) + coap_delete_pdu(pdu); set_timeout(&max_wait, wait_seconds); debug("timeout is set to %d seconds\n", wait_seconds); diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index 20460d9b..5f7fe2a0 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -503,8 +503,8 @@ check_async(coap_context_t *ctx, coap_tick_t now) { if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) { debug("check_async: cannot send response for message %d\n", response->hdr->id); - coap_delete_pdu(response); } + coap_delete_pdu(response); coap_remove_async(ctx, async->id, &tmp); coap_free_async(async); diff --git a/examples/rd.c b/examples/rd.c index 5f611fae..4605d640 100644 --- a/examples/rd.c +++ b/examples/rd.c @@ -181,8 +181,8 @@ hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource, if (coap_send(ctx, peer, response) == COAP_INVALID_TID) { debug("hnd_get_rd: cannot send response for message %d\n", request->hdr->id); - coap_delete_pdu(response); } + coap_delete_pdu(response); #endif } diff --git a/examples/server.c b/examples/server.c index 7ea0b301..d910358d 100644 --- a/examples/server.c +++ b/examples/server.c @@ -234,8 +234,8 @@ check_async(coap_context_t *ctx, coap_tick_t now) { if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) { debug("check_async: cannot send response for message %d\n", response->hdr->id); - coap_delete_pdu(response); } + coap_delete_pdu(response); coap_remove_async(ctx, async->id, &tmp); coap_free_async(async); diff --git a/examples/tiny.c b/examples/tiny.c index b309ce45..86cef841 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -135,6 +135,7 @@ main(int argc, char **argv) { return -1; coap_send( ctx, (struct sockaddr *)&dst, sizeof(dst), pdu ); + coap_delete_pdu(pdu); tv.tv_sec = 5; tv.tv_usec = 0; diff --git a/net.c b/net.c index 0313aafd..1dd587fc 100644 --- a/net.c +++ b/net.c @@ -398,8 +398,7 @@ coap_send_ack(coap_context_t *context, sizeof(coap_pdu_t)); if (response) { result = coap_send(context, dst, response); - if (result == COAP_INVALID_TID) - coap_delete_pdu(response); + coap_delete_pdu(response); } } return result; @@ -410,7 +409,7 @@ coap_send_ack(coap_context_t *context, coap_tid_t coap_send_impl(coap_context_t *context, const coap_address_t *dst, - coap_pdu_t *pdu, int free_pdu) { + coap_pdu_t *pdu) { ssize_t bytes_written; coap_tid_t id = COAP_INVALID_TID; @@ -426,9 +425,6 @@ coap_send_impl(coap_context_t *context, coap_log(LOG_CRIT, "coap_send: sendto"); } - if ( free_pdu ) - coap_delete_pdu( pdu ); - return id; } #else /* WITH_CONTIKI */ @@ -436,7 +432,7 @@ coap_send_impl(coap_context_t *context, coap_tid_t coap_send_impl(coap_context_t *context, const coap_address_t *dst, - coap_pdu_t *pdu, int free_pdu) { + coap_pdu_t *pdu) { coap_tid_t id = COAP_INVALID_TID; if ( !context || !dst || !pdu ) @@ -448,9 +444,6 @@ coap_send_impl(coap_context_t *context, coap_transaction_id(dst, pdu, &id); - if (free_pdu) - coap_delete_pdu(pdu); - return id; } #endif /* WITH_CONTIKI */ @@ -459,7 +452,7 @@ coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu) { - return coap_send_impl(context, dst, pdu, 1); + return coap_send_impl(context, dst, pdu); } coap_tid_t @@ -477,8 +470,7 @@ coap_send_error(coap_context_t *context, response = coap_new_error_response(request, code, opts); if (response) { result = coap_send(context, dst, response); - if (result == COAP_INVALID_TID) - coap_delete_pdu(response); + coap_delete_pdu(response); } return result; @@ -496,8 +488,7 @@ coap_send_message_type(coap_context_t *context, response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t)); if (response) { result = coap_send(context, dst, response); - if (result == COAP_INVALID_TID) - coap_delete_pdu(response); + coap_delete_pdu(response); } } return result; @@ -553,7 +544,7 @@ coap_send_confirmed(coap_context_t *context, } #endif /* WITH_CONTIKI */ - node->id = coap_send_impl(context, dst, pdu, 0); + node->id = coap_send_impl(context, dst, pdu); return node->id; } @@ -576,7 +567,7 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { node->retransmit_cnt, uip_ntohs(node->pdu->hdr->id)); #endif /* WITH_CONTIKI */ - node->id = coap_send_impl(context, &node->remote, node->pdu, 0); + node->id = coap_send_impl(context, &node->remote, node->pdu); return node->id; } @@ -936,8 +927,8 @@ handle_request(coap_context_t *context, coap_queue_t *node) { if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID) { warn("cannot send response for transaction %u\n", node->id); - coap_delete_pdu(response); } + coap_delete_pdu(response); return; } @@ -969,10 +960,9 @@ handle_request(coap_context_t *context, coap_queue_t *node) { && !coap_is_mcast(&node->local))) { if (coap_send(context, &node->remote, response) == COAP_INVALID_TID) { debug("cannot send response for message %d\n", node->pdu->hdr->id); - coap_delete_pdu(response); } - } else - coap_delete_pdu(response); + } + coap_delete_pdu(response); } else { warn("cannot generate response\r\n"); } @@ -987,8 +977,8 @@ handle_request(coap_context_t *context, coap_queue_t *node) { if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID)) { debug("cannot send response for transaction %u\n", node->id); - coap_delete_pdu(response); } + coap_delete_pdu(response); } } @@ -1093,8 +1083,8 @@ coap_dispatch( coap_context_t *context ) { if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID) { warn("coap_dispatch: error sending reponse\n"); - coap_delete_pdu(response); } + coap_delete_pdu(response); } goto cleanup; diff --git a/net.h b/net.h index d5cb3f8e..2f0a557d 100644 --- a/net.h +++ b/net.h @@ -180,9 +180,8 @@ void coap_free_context( coap_context_t *context ); /** * Sends a confirmed CoAP message to given destination. The memory - * that is allocated by pdu will be released by - * coap_send_confirmed(). The caller must not make any assumption on - * the lifetime of pdu. + * that is allocated by pdu will not be released by + * coap_send_confirmed(). The caller must release the memory. * * @param context The CoAP context to use. * @param dst The address to send to. @@ -215,8 +214,8 @@ coap_pdu_t *coap_new_error_response(coap_pdu_t *request, coap_opt_filter_t opts); /** * Sends a non-confirmed CoAP message to given destination. The memory - * that is allocated by pdu will be released by coap_send(). The - * caller must not make any assumption on the lifetime of pdu. + * that is allocated by pdu will not be released by coap_send(). + * The caller must release the memory. * * @param context The CoAP context to use. * @param dst The address to send to. diff --git a/resource.c b/resource.c index a10fb9a9..d447929a 100644 --- a/resource.c +++ b/resource.c @@ -487,30 +487,31 @@ coap_check_notify(coap_context_t *context) { if (r->observeable && r->dirty && list_head(r->subscribers)) { #endif /* WITH_CONTIKI */ coap_method_handler_t h; + coap_subscription_t *obs; + str token; /* retrieve GET handler, prepare response */ h = r->handler[COAP_REQUEST_GET - 1]; assert(h); /* we do not allow subscriptions if no * GET handler is defined */ - /* FIXME: provide CON/NON flag in coap_subscription_t */ - response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE); - if (response) { - coap_subscription_t *obs; - str token; - #ifndef WITH_CONTIKI /* FIXME: */ - LL_FOREACH(r->subscribers, obs) { + LL_FOREACH(r->subscribers, obs) { #else /* WITH_CONTIKI */ for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs)) { #endif /* WITH_CONTIKI */ - /* re-initialize response */ + coap_tid_t tid = COAP_INVALID_TID; + /* initialize response */ + response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE); + if (!response) { + debug("pdu init failed\n"); + continue; + } token.length = obs->token_length; token.s = obs->token; - coap_pdu_clear(response, response->max_size); response->hdr->id = coap_new_message_id(context); if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) response->hdr->type = COAP_MESSAGE_NON; @@ -521,18 +522,19 @@ coap_check_notify(coap_context_t *context) { h(context, r, &obs->subscriber, NULL, &token, response); if (response->hdr->type == COAP_MESSAGE_CON) { - coap_send_confirmed(context, &obs->subscriber, response); + tid = coap_send_confirmed(context, &obs->subscriber, response); obs->non_cnt = 0; } else { - coap_send(context, &obs->subscriber, response); + tid = coap_send(context, &obs->subscriber, response); obs->non_cnt++; } + + if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON) + coap_delete_pdu(response); } - coap_delete_pdu(response); /* Increment value for next Observe use. */ context->observe++; - } } r->dirty = 0; }