From 683337890e6fbab1eaa0ef0f2038194fb62b7fa2 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 22 Jul 2015 01:51:39 +0200 Subject: [PATCH] bugfix: add flag for CON/NON notification messages Two new flags COAP_RESOURCE_FLAGS_NOTIFY_NON and COAP_RESOURCE_FLAGS_NOTIFY_CON have been added to control the message type for notifications to send. The default type is COAP_RESOURCE_FLAGS_NOTIFY_NON which can be changed with the function coap_resource_set_mode(). The observer-specific flag has been removed. --- examples/coap-server.c | 2 +- include/coap/resource.h | 15 ++++++++++++++- include/coap/subscribe.h | 1 - src/net.c | 1 - src/resource.c | 5 +++-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index f0aa59f3..f474d344 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -299,7 +299,7 @@ init_resources(coap_context_t *ctx) { /* store clock base to use in /time */ my_clock_base = clock_offset; - r = coap_resource_init((unsigned char *)"time", 4, 0); + r = coap_resource_init((unsigned char *)"time", 4, COAP_RESOURCE_FLAGS_NOTIFY_CON); coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time); coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time); coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time); diff --git a/include/coap/resource.h b/include/coap/resource.h index 2947691f..e6a55ec4 100644 --- a/include/coap/resource.h +++ b/include/coap/resource.h @@ -1,6 +1,6 @@ /* resource.h -- generic resource handling * - * Copyright (C) 2010,2011,2014 Olaf Bergmann + * Copyright (C) 2010,2011,2014,2015 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -59,6 +59,8 @@ typedef struct coap_attr_t { } coap_attr_t; #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1 +#define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0 +#define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2 typedef struct coap_resource_t { unsigned int dirty:1; /**< set to 1 if resource has changed */ @@ -109,6 +111,17 @@ typedef struct coap_resource_t { coap_resource_t *coap_resource_init(const unsigned char *uri, size_t len, int flags); + +/** + * Sets the notification message type of resource @p r to given + * @p mode which must be one of @c COAP_RESOURCE_FLAGS_NOTIFY_NON + * or @c COAP_RESOURCE_FLAGS_NOTIFY_CON. + */ +static inline void +coap_resource_set_mode(coap_resource_t *r, int mode) { + r->flags = (r->flags & !COAP_RESOURCE_FLAGS_NOTIFY_CON) | mode; +} + /** * Registers the given @p resource for @p context. The resource must have been * created by coap_resource_init(), the storage allocated for the resource will diff --git a/include/coap/subscribe.h b/include/coap/subscribe.h index ca2f44b8..103dc5cd 100644 --- a/include/coap/subscribe.h +++ b/include/coap/subscribe.h @@ -55,7 +55,6 @@ typedef struct coap_subscription_t { coap_endpoint_t local_if; /**< local communication interface */ coap_address_t subscriber; /**< address and port of subscriber */ - unsigned int non:1; /**< send non-confirmable notifies if @c 1 */ unsigned int non_cnt:4; /**< up to 15 non-confirmable notifies allowed */ unsigned int fail_cnt:2; /**< up to 3 confirmable notifies can fail */ unsigned int dirty:1; /**< set if the notification temporarily could not be diff --git a/src/net.c b/src/net.c index 83d178eb..15de94da 100644 --- a/src/net.c +++ b/src/net.c @@ -1429,7 +1429,6 @@ handle_request(coap_context_t *context, coap_queue_t *node) { subscription = coap_add_observer(resource, &node->local_if, &node->remote, &token); if (subscription) { - subscription->non = node->pdu->hdr->type == COAP_MESSAGE_NON; coap_touch_observer(context, &node->remote, &token); } } diff --git a/src/resource.c b/src/resource.c index bca512c8..95f51e74 100644 --- a/src/resource.c +++ b/src/resource.c @@ -1,6 +1,6 @@ /* resource.c -- generic resource handling * - * Copyright (C) 2010--2014 Olaf Bergmann + * Copyright (C) 2010--2015 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -646,7 +646,8 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r) { token.s = obs->token; response->hdr->id = coap_new_message_id(context); - if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) { + if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 + && obs->non_cnt < COAP_OBS_MAX_NON) { response->hdr->type = COAP_MESSAGE_NON; } else { response->hdr->type = COAP_MESSAGE_CON;