/*-----------------------------------------------------------------------------------*/ void coap_observe_handler(resource_t *resource, void *request, void *response) { coap_packet_t *const coap_req = (coap_packet_t *) request; coap_packet_t *const coap_res = (coap_packet_t *) response; static char content[16]; if (coap_req->code==COAP_GET && coap_res->code<128) /* GET request and response without error code */ { if (IS_OPTION(coap_req, COAP_OPTION_OBSERVE)) { if (coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len, resource->url)) { coap_set_header_observe(coap_res, 0); /* * For demonstration purposes only. A subscription should return the same representation as a normal GET. * TODO: Comment the following line for any real application. */ coap_set_payload(coap_res, content, snprintf(content, sizeof(content), "Added %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS)); } else { coap_res->code = SERVICE_UNAVAILABLE_5_03; coap_set_payload(coap_res, "TooManyObservers", 16); } /* if (added observer) */ } else /* if (observe) */ { /* Remove client if it is currently observing. */ coap_remove_observer_by_url(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, resource->url); } /* if (observe) */ } }
/*-----------------------------------------------------------------------------------*/ void coap_observe_handler(resource_t *resource, void *request, void *response) { static char content[26]; if (response && ((coap_packet_t *)response)->code<128) /* response without error code */ { if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_OBSERVE)) { if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_TOKEN)) { if (coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, ((coap_packet_t *)request)->token, ((coap_packet_t *)request)->token_len, resource->url)) { coap_set_header_observe(response, 0); coap_set_payload(response, (uint8_t *)content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS)); } else { ((coap_packet_t *)response)->code = SERVICE_UNAVAILABLE_503; coap_set_payload(response, (uint8_t *)"Too many observers", 18); } /* if (added observer) */ } else /* if (token) */ { ((coap_packet_t *)response)->code = TOKEN_OPTION_REQUIRED; coap_set_payload(response, (uint8_t *)"Observing requires token", 24); } /* if (token) */ } else /* if (observe) */ { /* Remove client if it is currently observing. */ coap_remove_observer_by_client(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport); } /* if (observe) */ } }
/*---------------------------------------------------------------------------*/ void coap_observe_handler(resource_t *resource, void *request, void *response) { coap_packet_t *const coap_req = (coap_packet_t *)request; coap_packet_t *const coap_res = (coap_packet_t *)response; coap_observer_t * obs; if(coap_req->code == COAP_GET && coap_res->code < 128) { /* GET request and response without error code */ if(IS_OPTION(coap_req, COAP_OPTION_OBSERVE)) { if(coap_req->observe == 0) { obs = coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len, resource->url); if(obs) { coap_set_header_observe(coap_res, (obs->obs_counter)++); /* * Following payload is for demonstration purposes only. * A subscription should return the same representation as a normal GET. * Uncomment if you want an information about the avaiable observers. */ #if 0 static char content[16]; coap_set_payload(coap_res, content, snprintf(content, sizeof(content), "Added %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS)); #endif } else { coap_res->code = SERVICE_UNAVAILABLE_5_03; coap_set_payload(coap_res, "TooManyObservers", 16); } } else if(coap_req->observe == 1) { /* remove client if it is currently observe */ coap_remove_observer_by_token(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len); } } } }
void hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *option; unsigned char buf[40]; 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. */ /* if my_clock_base was deleted, we pretend to have no such 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) 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); coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x01), buf); if (my_clock_base) { /* calculate current time */ coap_ticks(&t); now = my_clock_base + (t / COAP_TICKS_PER_SECOND); if (request != NULL && (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter)) && memcmp(COAP_OPT_VALUE(option), "ticks", min(5, COAP_OPT_LENGTH(option))) == 0) { /* output ticks */ len = snprintf((char *)buf, min(sizeof(buf), response->max_size - response->length), "%u", (unsigned int)now); coap_add_data(response, len, buf); } else { /* output human-readable time */ struct tm *tmp; tmp = gmtime(&now); len = strftime((char *)buf, min(sizeof(buf), response->max_size - response->length), "%b %d %H:%M:%S", tmp); coap_add_data(response, len, buf); } } }