mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-14 12:19:43 +03:00
Support cancellation of observe relationships using GET w/ Observe: 1.
Adding and removal of observes is now handled by libcoap. The application's GET handler only needs to add the Observe option (see server.c:hnd_get_time()).
This commit is contained in:
1 parent
1b3aa704e6
commit
a3fae3ce3a
7 files changed
+108
-34
No files matched your search
+45
-5
@@ -165,13 +165,14 @@ coap_new_request(coap_context_t *ctx, method_t m, coap_list_t *options ) {
|
||||
coap_tid_t
|
||||
clear_obs(coap_context_t *ctx, const coap_address_t *remote) {
|
||||
coap_pdu_t *pdu;
|
||||
coap_list_t *option;
|
||||
coap_tid_t tid = COAP_INVALID_TID;
|
||||
unsigned char buf[2];
|
||||
|
||||
/* create bare PDU w/o any option */
|
||||
coap_log(LOG_INFO, "response code 7.31 is %d\n", COAP_RESPONSE_CODE(731));
|
||||
pdu = coap_pdu_init(msgtype, COAP_RESPONSE_CODE(731),
|
||||
pdu = coap_pdu_init(msgtype, COAP_REQUEST_GET,
|
||||
coap_new_message_id(ctx),
|
||||
sizeof(coap_hdr_t) + the_token.length);
|
||||
COAP_MAX_PDU_SIZE);
|
||||
|
||||
if (!pdu) {
|
||||
return tid;
|
||||
@@ -179,9 +180,44 @@ clear_obs(coap_context_t *ctx, const coap_address_t *remote) {
|
||||
|
||||
if (!coap_add_token(pdu, the_token.length, the_token.s)) {
|
||||
coap_log(LOG_CRIT, "cannot add token");
|
||||
coap_delete_pdu(pdu);
|
||||
return tid;
|
||||
goto error;
|
||||
}
|
||||
|
||||
for (option = optlist; option; option = option->next ) {
|
||||
if (COAP_OPTION_KEY(*(coap_option *)option->data)
|
||||
== COAP_OPTION_URI_HOST) {
|
||||
if (!coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)option->data),
|
||||
COAP_OPTION_LENGTH(*(coap_option *)option->data),
|
||||
COAP_OPTION_DATA(*(coap_option *)option->data))) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!coap_add_option(pdu, COAP_OPTION_OBSERVE,
|
||||
coap_encode_var_bytes(buf, COAP_OBSERVE_CANCEL),
|
||||
buf)) {
|
||||
coap_log(LOG_CRIT, "cannot add option Observe: %u", COAP_OBSERVE_CANCEL);
|
||||
goto error;
|
||||
}
|
||||
|
||||
for (option = optlist; option; option = option->next ) {
|
||||
switch (COAP_OPTION_KEY(*(coap_option *)option->data)) {
|
||||
case COAP_OPTION_URI_PORT :
|
||||
case COAP_OPTION_URI_PATH :
|
||||
case COAP_OPTION_URI_QUERY :
|
||||
if (!coap_add_option (pdu, COAP_OPTION_KEY(*(coap_option *)option->data),
|
||||
COAP_OPTION_LENGTH(*(coap_option *)option->data),
|
||||
COAP_OPTION_DATA(*(coap_option *)option->data))) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
coap_show_pdu(pdu);
|
||||
|
||||
if (pdu->hdr->type == COAP_MESSAGE_CON)
|
||||
@@ -196,6 +232,10 @@ clear_obs(coap_context_t *ctx, const coap_address_t *remote) {
|
||||
coap_delete_pdu(pdu);
|
||||
|
||||
return tid;
|
||||
error:
|
||||
|
||||
coap_delete_pdu(pdu);
|
||||
return tid;
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
+3
-11
@@ -83,7 +83,6 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
size_t len;
|
||||
time_t now;
|
||||
coap_tick_t t;
|
||||
coap_subscription_t *subscription;
|
||||
|
||||
/* FIXME: return time, e.g. in human-readable by default and ticks
|
||||
* when query ?ticks is given. */
|
||||
@@ -92,19 +91,12 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
response->hdr->code =
|
||||
my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);
|
||||
|
||||
if (request != NULL &&
|
||||
coap_check_option(request, COAP_OPTION_OBSERVE, &opt_iter)) {
|
||||
subscription = coap_add_observer(resource, peer, token);
|
||||
if (subscription) {
|
||||
subscription->non = request->hdr->type == COAP_MESSAGE_NON;
|
||||
coap_add_option(response, COAP_OPTION_OBSERVE, 0, NULL);
|
||||
}
|
||||
}
|
||||
if (resource->dirty == 1)
|
||||
if (coap_find_observer(resource, peer, token)) {
|
||||
/* FIXME: need to check for resource->dirty? */
|
||||
coap_add_option(response, COAP_OPTION_OBSERVE,
|
||||
coap_encode_var_bytes(buf, ctx->observe), buf);
|
||||
}
|
||||
|
||||
|
||||
if (my_clock_base)
|
||||
coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
|
||||
coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
|
||||
Reference in new issue
Block a user