Lines Matching +full:- +full:- +full:allow +full:- +full:dirty
1 /* coap_resource.c -- generic resource handling
3 * Copyright (C) 2010--2023 Olaf Bergmann <bergmann@tzi.org>
5 * SPDX-License-Identifier: BSD-2-Clause
44 (Offset)--; \
59 * characters are skipped. Output may be truncated to Bufend - Buf
76 if (text->length < pattern->length) in match()
80 const uint8_t *next_token = text->s; in match()
81 size_t remaining_length = text->length; in match()
88 token_length = next_token - token; in match()
89 remaining_length -= (token_length + 1); in match()
96 if ((match_prefix || pattern->length == token_length) && in match()
97 memcmp(token, pattern->s, pattern->length) == 0) in match()
103 return (match_prefix || pattern->length == text->length) && in match()
104 memcmp(text->s, pattern->s, pattern->length) == 0; in match()
116 * set to the length of the well-known response on return.
121 …uery according to <a href="http://tools.ietf.org/html/draft-ietf-core-link-format-11#section-4.1">…
162 resource_param.s = query_filter->s;
163 while (resource_param.length < query_filter->length &&
167 if (resource_param.length < query_filter->length) {
173 for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
174 if (resource_param.length == rt_attributes->length &&
175 memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
181 /* rest is query-pattern */
183 query_filter->s + resource_param.length + 1;
185 assert((resource_param.length + 1) <= query_filter->length);
187 query_filter->length - (resource_param.length + 1);
191 query_pattern.length--;
195 query_pattern.s[query_pattern.length-1] == '*') {
196 query_pattern.length--;
203 RESOURCES_ITER(context->resources, r) {
209 if (!match(r->uri_path, &query_pattern, (flags & MATCH_PREFIX) != 0,
216 if (!attr || !attr->value)
218 unquoted_val = *attr->value;
219 …if (attr->value->s[0] == '"') { /* if attribute has a quoted value, remove double quotes …
220 unquoted_val.length -= 2;
237 left = bufend - p; /* calculate available space */
251 output_length = p - buf;
259 if (result + old_offset - offset < *buflen) {
279 uri_path = coap_new_str_const(uri_path->s, uri_path->length);
281 uri_path = coap_new_str_const(null_path->s, null_path->length);
284 uri_path = coap_new_str_const(null_path->s, null_path->length);
288 r->uri_path = uri_path;
290 r->flags = flags;
291 r->observe = 2;
300 "- Unknown -";
309 r->is_unknown = 1;
311 … r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
312 r->flags = flags & COAP_RESOURCE_FLAGS_MCAST_LIST;
327 "- Proxy URI -";
343 r->is_proxy_uri = 1;
345 r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1);
347 for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
348 r->handler[i] = handler;
351 r->proxy_name_list = coap_malloc_type(COAP_STRING, host_name_count *
353 if (r->proxy_name_list) {
355 r->proxy_name_list[i] =
358 if (!r->proxy_name_list[i]) {
361 coap_free_type(COAP_STRING, r->proxy_name_list);
362 r->proxy_name_list = NULL;
367 r->proxy_name_count = i;
370 r->flags = flags & COAP_RESOURCE_FLAGS_MCAST_LIST;
399 name = coap_new_str_const(name->s, name->length);
401 attr->name = name;
405 val = coap_new_str_const(val->s, val->length);
408 attr->value = val;
410 attr->flags = flags;
413 LL_PREPEND(resource->link_attr, attr);
429 LL_FOREACH(resource->link_attr, attr) {
430 if (attr->name->length == name->length &&
431 memcmp(attr->name->s, name->s, name->length) == 0)
441 return attr->value;
449 coap_delete_str_const(attr->name);
450 if (attr->value) {
451 coap_delete_str_const(attr->value);
472 if (!resource->context->observe_no_clear) {
474 coap_notify_observers(resource->context, resource, COAP_DELETING_RESOURCE);
477 if (resource->context->resource_deleted)
478 resource->context->resource_deleted(resource->context, resource->uri_path,
479 resource->context->observe_user_data);
481 if (resource->context->release_userdata && resource->user_data)
482 resource->context->release_userdata(resource->user_data);
485 LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
487 /* Either the application provided or libcoap copied - need to delete it */
488 coap_delete_str_const(resource->uri_path);
490 /* free all elements from resource->subscribers */
491 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
492 if (resource->context->observe_deleted)
493 resource->context->observe_deleted(obs->session, obs,
494 resource->context->observe_user_data);
495 coap_session_release(obs->session);
496 coap_delete_pdu(obs->pdu);
497 coap_delete_cache_key(obs->cache_key);
500 if (resource->proxy_name_count && resource->proxy_name_list) {
503 for (i = 0; i < resource->proxy_name_count; i++) {
504 coap_delete_str_const(resource->proxy_name_list[i]);
506 coap_free_type(COAP_STRING, resource->proxy_name_list);
514 if (resource->is_unknown) {
515 if (context->unknown_resource)
516 coap_free_resource(context->unknown_resource);
517 context->unknown_resource = resource;
518 } else if (resource->is_proxy_uri) {
519 if (context->proxy_uri_resource)
520 coap_free_resource(context->proxy_uri_resource);
521 context->proxy_uri_resource = resource;
524 resource->uri_path);
528 (int)resource->uri_path->length, (int)resource->uri_path->length,
529 resource->uri_path->s);
532 RESOURCES_ADD(context->resources, resource);
534 if (context->unknown_pdu && context->dyn_resource_save_file &&
535 context->dyn_resource_added && resource->observable) {
538 raw_packet.s = context->unknown_pdu->token -
539 context->unknown_pdu->hdr_size;
540 raw_packet.length = context->unknown_pdu->used_size +
541 context->unknown_pdu->hdr_size;
542 context->dyn_resource_added(context->unknown_session, resource->uri_path,
543 &raw_packet, context->observe_user_data);
547 assert(resource->context == NULL);
548 resource->context = context;
559 context = resource->context;
561 if (resource->is_unknown) {
562 if (context && context->unknown_resource == resource) {
563 context->unknown_resource = NULL;
565 } else if (resource->is_proxy_uri) {
566 if (context && context->proxy_uri_resource == resource) {
567 context->proxy_uri_resource = NULL;
571 RESOURCES_DELETE(context->resources, resource);
588 HASH_ITER(hh, context->resources, res, rtmp) {
589 HASH_DELETE(hh, context->resources, res);
593 context->resources = NULL;
595 if (context->unknown_resource) {
596 coap_free_resource(context->unknown_resource);
597 context->unknown_resource = NULL;
599 if (context->proxy_uri_resource) {
600 coap_free_resource(context->proxy_uri_resource);
601 context->proxy_uri_resource = NULL;
609 RESOURCES_FIND(context->resources, uri_path, result);
629 resource->uri_path->s, resource->uri_path->length, *len);
633 LL_FOREACH(resource->link_attr, attr) {
638 attr->name->s, attr->name->length, *len);
640 if (attr->value && attr->value->s) {
644 attr->value->s, attr->value->length, *len);
648 if (resource->observable) {
654 if (resource->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY)
658 output_length = p - buf;
666 if (result + old_offset - *offset < *len) {
685 assert(method > 0 && (size_t)(method-1) <
686 sizeof(resource->handler)/sizeof(coap_method_handler_t));
687 resource->handler[method-1] = handler;
698 LL_FOREACH(resource->subscribers, s) {
699 if (s->session == session &&
700 (!token || coap_binary_equal(token, &s->pdu->actual_token)))
715 LL_FOREACH(resource->subscribers, s) {
716 if (s->session == session
717 && (memcmp(cache_key, s->cache_key, sizeof(coap_cache_key_t)) == 0))
733 /* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
744 * Cannot allow a duplicate to be created for the same query as application
756 coap_delete_observer(resource, session, &s->pdu->actual_token);
770 LL_COUNT(resource->subscribers, s, subscriber_count);
785 s->pdu = coap_pdu_duplicate(request, session, token->length,
786 token->s, NULL);
787 if (s->pdu == NULL) {
794 s->pdu->max_size = 0;
795 coap_add_data(s->pdu, len, data);
803 coap_delete_pdu(s->pdu);
809 s->cache_key = cache_key;
810 s->session = coap_session_reference(session);
813 LL_PREPEND(resource->subscribers, s);
816 (void *)s, s->cache_key->key[0], s->cache_key->key[1],
817 s->cache_key->key[2], s->cache_key->key[3]);
819 if (session->context->observe_added && session->proto == COAP_PROTO_UDP) {
825 if (session->recipient_ctx && session->recipient_ctx->recipient_id) {
843 coap_bin_const_t ctoken = { token->length, token->s };
848 session->recipient_ctx->recipient_id->s,
849 session->recipient_ctx->recipient_id->length);
850 if (session->recipient_ctx->osc_ctx &&
851 session->recipient_ctx->osc_ctx->id_context) {
854 session->recipient_ctx->osc_ctx->id_context->s,
855 session->recipient_ctx->osc_ctx->id_context->length);
861 if (association->aad) {
864 association->aad->s,
865 association->aad->length);
869 if (association->partial_iv) {
872 association->partial_iv->s,
873 association->partial_iv->length);
877 if (association->nonce) {
880 association->nonce->s,
881 association->nonce->length);
893 /* s->pdu header is not currently encoded */
894 memcpy(s->pdu->token - request->hdr_size,
895 request->token - request->hdr_size, request->hdr_size);
896 raw_packet.s = s->pdu->token - request->hdr_size;
897 raw_packet.length = s->pdu->used_size + request->hdr_size;
898 session->context->observe_added(session, s, session->proto,
899 &session->endpoint->bind_addr,
900 &session->addr_info,
903 session->context->observe_user_data);
908 if (resource->context->track_observe_value) {
910 resource->context->track_observe_value(resource->context,resource->uri_path,
911 resource->observe,
912 resource->context->observe_user_data);
923 RESOURCES_ITER(context->resources, r) {
926 s->fail_cnt = 0;
942 for (i = 0; i < s->pdu->actual_token.length; i++) {
945 snprintf(&outbuf[size], sizeof(outbuf)-size, "%02x",
946 s->pdu->actual_token.s[i]);
949 (void *)s, outbuf, s->cache_key->key[0], s->cache_key->key[1],
950 s->cache_key->key[2], s-> cache_key->key[3]);
952 if (s && session->context->observe_deleted)
953 session->context->observe_deleted(session, s,
954 session->context->observe_user_data);
956 if (resource->subscribers && s) {
957 LL_DELETE(resource->subscribers, s);
959 coap_delete_pdu(s->pdu);
960 coap_delete_cache_key(s->cache_key);
969 RESOURCES_ITER(context->resources, resource) {
971 LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
972 if (s->session == session) {
973 if (context->observe_deleted)
974 context->observe_deleted(session, s, context->observe_user_data);
975 LL_DELETE(resource->subscribers, s);
977 coap_delete_pdu(s->pdu);
978 coap_delete_cache_key(s->cache_key);
997 if (r->observable && (r->dirty || r->partiallydirty)) {
998 r->partiallydirty = 0;
1000 LL_FOREACH_SAFE(r->subscribers, obs, otmp) {
1001 obs_session = obs->session;
1002 if (r->dirty == 0 && obs->dirty == 0) {
1007 context->observe_pending = 1;
1010 if (obs->session->con_active >= COAP_NSTART(obs->session) &&
1011 ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) ||
1012 (obs->non_cnt >= COAP_OBS_MAX_NON))) {
1014 r->partiallydirty = 1;
1015 obs->dirty = 1;
1016 context->observe_pending = 1;
1020 if (obs->session->lg_xmit && obs->session->lg_xmit->last_all_sent == 0 &&
1021 obs->session->lg_xmit->last_obs &&
1022 (obs->session->lg_xmit->last_obs + 2*COAP_TICKS_PER_SECOND) > now) {
1024 r->partiallydirty = 1;
1025 obs->dirty = 1;
1026 context->observe_pending = 1;
1031 obs->dirty = 0;
1033 response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, coap_session_max_pdu_size(obs->session));
1035 obs->dirty = 1;
1036 r->partiallydirty = 1;
1037 context->observe_pending = 1;
1039 "partially dirty\n");
1043 if (!coap_add_token(response, obs->pdu->actual_token.length,
1044 obs->pdu->actual_token.s)) {
1045 obs->dirty = 1;
1046 r->partiallydirty = 1;
1047 context->observe_pending = 1;
1049 "partially dirty\n");
1054 obs->pdu->mid = response->mid = coap_new_message_id(obs->session);
1056 if (COAP_PROTO_NOT_RELIABLE(obs->session->proto) &&
1057 (r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 &&
1058 ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS) ||
1059 obs->non_cnt < COAP_OBS_MAX_NON)) {
1060 response->type = COAP_MESSAGE_NON;
1062 response->type = COAP_MESSAGE_CON;
1066 /* fill with observer-specific data */
1069 r->observe),
1071 if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_BLOCK2,
1082 else if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_Q_BLOCK2,
1094 h = r->handler[obs->pdu->code - 1];
1095 assert(h); /* we do not allow subscriptions if no
1097 query = coap_get_query(obs->pdu);
1099 coap_show_pdu(COAP_LOG_DEBUG, obs->pdu);
1101 (int)r->uri_path->length, (int)r->uri_path->length,
1102 r->uri_path->s);
1103 h(r, obs->session, obs->pdu, query, response);
1105 coap_check_code_lg_xmit(obs->session, obs->pdu, response, r, query);
1107 if (COAP_RESPONSE_CLASS(response->code) != 2) {
1110 if (COAP_RESPONSE_CLASS(response->code) > 2) {
1111 coap_delete_observer(r, obs->session, &obs->pdu->actual_token);
1118 response->type = COAP_MESSAGE_NON;
1119 response->code = COAP_RESPONSE_CODE(404);
1124 if (response->type == COAP_MESSAGE_CON ||
1125 (r->flags & COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS)) {
1126 obs->non_cnt = 0;
1128 obs->non_cnt++;
1132 if (response->code == COAP_RESPONSE_CODE(205) &&
1133 coap_get_block_b(obs->session, response, COAP_OPTION_Q_BLOCK2,
1136 query = coap_get_query(obs->pdu);
1137 mid = coap_send_q_block2(obs->session, r, query, obs->pdu->code,
1152 "partially dirty\n");
1153 LL_FOREACH(r->subscribers, s) {
1156 obs->dirty = 1;
1160 r->partiallydirty = 1;
1161 context->observe_pending = 1;
1165 r->dirty = 0;
1176 if (!r->observable)
1178 if (!r->subscribers)
1180 r->dirty = 1;
1183 r->observe = (r->observe + 1) & 0xFFFFFF;
1185 assert(r->context);
1187 if (r->context->track_observe_value) {
1189 if ((r->observe % r->context->observe_save_freq) == 0)
1190 r->context->track_observe_value(r->context, r->uri_path,
1191 r->observe,
1192 r->context->observe_user_data);
1195 r->context->observe_pending = 1;
1197 coap_update_epoll_timer(r->context, 0);
1204 resource->flags = (resource->flags &
1211 resource->user_data = data;
1216 return resource->user_data;
1222 context->release_userdata = callback;
1227 resource->observable = mode ? 1 : 0;
1233 return resource->uri_path;
1240 if (context->observe_pending) {
1241 context->observe_pending = 0;
1242 RESOURCES_ITER(context->resources, r) {
1254 resource->observe = start_observe_no & 0xffffff;
1274 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
1275 if (obs->session == session &&
1276 coap_binary_equal(token, &obs->pdu->actual_token)) {
1279 obs->fail_cnt++;
1280 if (obs->fail_cnt >= COAP_OBS_MAX_FAIL) {
1281 coap_cancel_all_messages(context, obs->session,
1282 &obs->pdu->actual_token);
1295 RESOURCES_ITER(context->resources, r) {