diff --git a/resource.c b/resource.c index e4348d35..086058ab 100644 --- a/resource.c +++ b/resource.c @@ -570,18 +570,28 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r) { assert(h); /* we do not allow subscriptions if no * GET handler is defined */ - if (r->observable && r->dirty) { + if (r->observable && (r->dirty || r->partiallydirty)) { + r->partiallydirty = 0; for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs)) { + if (r->dirty == 0 && obs->dirty == 0) + /* running this resource due to partiallydirty, but this observation's notification was already enqueued */ + continue; + coap_tid_t tid = COAP_INVALID_TID; + obs->dirty = 0; /* initialize response */ response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE); if (!response) { - debug("coap_check_notify: pdu init failed\n"); + obs->dirty = 1; + r->partiallydirty = 1; + debug("coap_check_notify: pdu init failed, resource stays partially dirty\n"); continue; } if (!coap_add_token(response, obs->token_length, obs->token)) { - debug("coap_check_notify: cannot add token\n"); + obs->dirty = 1; + r->partiallydirty = 1; + debug("coap_check_notify: cannot add token, resource stays partially dirty\n"); coap_delete_pdu(response); continue; } @@ -608,6 +618,13 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r) { if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON) coap_delete_pdu(response); + if (COAP_INVALID_TID == tid) + { + debug("coap_check_notify: sending failed, resource stays partially dirty\n"); + obs->dirty = 1; + r->partiallydirty = 1; + } + } /* Increment value for next Observe use. */ diff --git a/resource.h b/resource.h index cad813fd..5dc71703 100644 --- a/resource.h +++ b/resource.h @@ -56,6 +56,7 @@ typedef struct coap_attr_t { typedef struct coap_resource_t { unsigned int dirty:1; /**< set to 1 if resource has changed */ + unsigned int partiallydirty:1; /**< set to 1 if some subscribers have not yet been notified of the last change */ unsigned int observable:1; /**< can be observed */ unsigned int cacheable:1; /**< can be cached */ diff --git a/subscribe.h b/subscribe.h index 31f33fc3..7eda74ff 100644 --- a/subscribe.h +++ b/subscribe.h @@ -45,6 +45,7 @@ typedef struct coap_subscription_t { 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 sent (in that case, the resource's partiallydirty flag is set too) */ size_t token_length; /**< actual length of token */ unsigned char token[8]; /**< token used for subscription */