diff --git a/coap-server.c b/coap-server.c index 6dca3c90..9192051c 100644 --- a/coap-server.c +++ b/coap-server.c @@ -68,10 +68,9 @@ init_coap() { void hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, - coap_address_t *peer, coap_pdu_t *request, coap_tid_t id) { + coap_address_t *peer, coap_pdu_t *request, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *token; - coap_pdu_t *response; size_t size = sizeof(coap_hdr_t) + 32; int type; unsigned char buf[2]; @@ -84,13 +83,14 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, /* if my_clock_base was deleted, we pretend to have no such resource */ code = my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404); - +#if 0 if (request->hdr->type == COAP_MESSAGE_CON) type = COAP_MESSAGE_ACK; else type = COAP_MESSAGE_NON; - +#endif token = coap_check_option(request, COAP_OPTION_TOKEN, &opt_iter); +#if 0 if (token) size += COAP_OPT_SIZE(token); @@ -100,7 +100,8 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, debug("cannot create response for message %d\n", request->hdr->id); return; } - +#endif + response->hdr->code = code; if (my_clock_base) coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); @@ -134,11 +135,13 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, "%u", (unsigned int)now); } +#if 0 if (coap_send(ctx, peer, response) == COAP_INVALID_TID) { debug("hnd_get_time: cannot send response for message %d\n", request->hdr->id); coap_delete_pdu(response); } +#endif } void diff --git a/net.c b/net.c index 8305e231..8646e795 100644 --- a/net.c +++ b/net.c @@ -821,7 +821,22 @@ handle_request(coap_context_t *context, coap_queue_t *node) { if (h) { debug("call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]); - h(context, resource, &node->remote, node->pdu, node->id); + response = coap_pdu_init(node->pdu->hdr->type == COAP_MESSAGE_CON + ? COAP_MESSAGE_ACK + : COAP_MESSAGE_NON, + 0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE); + if (response) { + h(context, resource, &node->remote, node->pdu, response); + if (response->hdr->type != COAP_MESSAGE_NON || + response->hdr->code >= 64) { + 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 { + warn("cannot generate response\r\n"); + } } else { if (WANT_WKC(node->pdu, key)) { debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN); diff --git a/resource.h b/resource.h index 7be52949..f2ce1e8a 100644 --- a/resource.h +++ b/resource.h @@ -35,7 +35,7 @@ /** Definition of message handler function (@sa coap_resource_t). */ typedef void (*coap_method_handler_t) (coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *, - coap_tid_t); + coap_pdu_t *); typedef struct coap_attr_t { struct coap_attr_t *next;