Lines Matching +full:resource +full:- +full:id
4 * SPDX-License-Identifier: Apache-2.0
32 /* Values as per RFC 7252, section-3.1.
34 * Option Delta/Length: 4-bit unsigned integer. A value between 0 and
37 * 13: An 8-bit unsigned integer precedes the Option Value and indicates
39 * 14: A 16-bit unsigned integer in network byte order precedes the
56 /* The CoAP message ID that is incremented each time coap_next_id() is called. */
73 cpkt->data[offset] = data; in encode_u8()
74 ++cpkt->offset; in encode_u8()
79 cpkt->data[offset] = data >> 8; in encode_be16()
80 cpkt->data[offset + 1] = (uint8_t)data; in encode_be16()
81 cpkt->offset += 2; in encode_be16()
87 memcpy(cpkt->data + offset, data, len); in encode_buffer()
88 cpkt->offset += len; in encode_buffer()
93 return (cpkt != NULL) && (cpkt->max_len - cpkt->offset >= bytes_to_add); in enough_space()
102 encode_u8(cpkt, cpkt->offset, data); in append_u8()
113 memmove(&cpkt->data[offset + 1], &cpkt->data[offset], cpkt->offset - offset); in insert_u8()
126 encode_be16(cpkt, cpkt->offset, data); in append_be16()
137 memmove(&cpkt->data[offset + 2], &cpkt->data[offset], cpkt->offset - offset); in insert_be16()
139 encode_be16(cpkt, cpkt->offset, data); in insert_be16()
150 encode_buffer(cpkt, cpkt->offset, data, len); in append()
162 memmove(&cpkt->data[offset + len], &cpkt->data[offset], cpkt->offset - offset); in insert()
171 const uint8_t *token, uint8_t code, uint16_t id) in coap_packet_init() argument
177 return -EINVAL; in coap_packet_init()
182 cpkt->data = data; in coap_packet_init()
183 cpkt->offset = 0U; in coap_packet_init()
184 cpkt->max_len = max_len; in coap_packet_init()
185 cpkt->delta = 0U; in coap_packet_init()
193 return -EINVAL; in coap_packet_init()
198 return -EINVAL; in coap_packet_init()
201 res = append_be16(cpkt, id); in coap_packet_init()
203 return -EINVAL; in coap_packet_init()
209 return -EINVAL; in coap_packet_init()
213 /* Header length : (version + type + tkl) + code + id + [token] */ in coap_packet_init()
214 cpkt->hdr_len = 1 + 1 + 2 + token_len; in coap_packet_init()
222 uint16_t id; in coap_ack_init() local
228 id = coap_header_get_id(req); in coap_ack_init()
232 token, code, id); in coap_ack_init()
238 uint16_t id; in coap_rst_init() local
242 id = coap_header_get_id(req); in coap_rst_init()
245 NULL, 0, id); in coap_rst_init()
267 *ext = num - COAP_OPTION_EXT_13; in encode_extended_option()
273 *ext = num - COAP_OPTION_EXT_269; in encode_extended_option()
302 return -EINVAL; in encode_option()
309 return -EINVAL; in encode_option()
315 return -EINVAL; in encode_option()
323 return -EINVAL; in encode_option()
329 return -EINVAL; in encode_option()
337 return -EINVAL; in encode_option()
350 return -EINVAL; in coap_packet_append_option()
354 return -EINVAL; in coap_packet_append_option()
357 if (code < cpkt->delta) { in coap_packet_append_option()
363 if (cpkt->opt_len) { in coap_packet_append_option()
364 code = (code == cpkt->delta) ? 0 : code - cpkt->delta; in coap_packet_append_option()
367 r = encode_option(cpkt, code, value, len, cpkt->hdr_len + cpkt->opt_len); in coap_packet_append_option()
369 return -EINVAL; in coap_packet_append_option()
372 cpkt->opt_len += r; in coap_packet_append_option()
373 cpkt->delta += code; in coap_packet_append_option()
406 switch (option->len) { in coap_option_value_to_int()
410 return option->value[0]; in coap_option_value_to_int()
412 return (option->value[1] << 0) | (option->value[0] << 8); in coap_option_value_to_int()
414 return (option->value[2] << 0) | (option->value[1] << 8) | in coap_option_value_to_int()
415 (option->value[0] << 16); in coap_option_value_to_int()
417 return (option->value[3] << 0) | (option->value[2] << 8) | in coap_option_value_to_int()
418 (option->value[1] << 16) | (option->value[0] << 24); in coap_option_value_to_int()
428 return append_u8(cpkt, COAP_MARKER) ? 0 : -EINVAL; in coap_packet_append_payload_marker()
434 return append(cpkt, payload, payload_len) ? 0 : -EINVAL; in coap_packet_append_payload()
459 if (max_len - offset < 1) { in read_u8()
460 return -EINVAL; in read_u8()
466 return max_len - offset; in read_u8()
472 if (max_len - offset < 2) { in read_be16()
473 return -EINVAL; in read_be16()
480 return max_len - offset; in read_be16()
486 if (max_len - offset < len) { in read()
487 return -EINVAL; in read()
494 return max_len - offset; in read()
509 return -EINVAL; in decode_delta()
520 return -EINVAL; in decode_delta()
525 return -EINVAL; in decode_delta()
551 return r > 0 ? 0 : -EINVAL; in parse_option()
564 return -EINVAL; in parse_option()
572 return -EINVAL; in parse_option()
576 return -EINVAL; in parse_option()
585 return -EINVAL; in parse_option()
589 return -EINVAL; in parse_option()
595 return -EINVAL; in parse_option()
603 return -EINVAL; in parse_option()
614 if (len > sizeof(option->value)) { in parse_option()
615 NET_ERR("%u is > sizeof(coap_option->value)(%zu)!", in parse_option()
616 len, sizeof(option->value)); in parse_option()
617 return -EINVAL; in parse_option()
620 option->delta = *opt_delta; in parse_option()
621 option->len = len; in parse_option()
622 r = read(data, *pos, pos, max_len, len, &option->value[0]); in parse_option()
624 return -EINVAL; in parse_option()
628 return -EINVAL; in parse_option()
631 r = max_len - *pos; in parse_option()
644 const uint16_t move_size = from_offset - to_offset; in remove_option_data()
646 memmove(cpkt->data + to_offset, cpkt->data + from_offset, cpkt->offset - from_offset); in remove_option_data()
647 cpkt->opt_len -= move_size; in remove_option_data()
648 cpkt->offset -= move_size; in remove_option_data()
665 r = parse_option(cpkt->data, offset, &offset, cpkt->hdr_len + cpkt->opt_len, in remove_middle_option()
668 return -EILSEQ; in remove_middle_option()
675 r = encode_option(cpkt, option.delta - previous_code, option.value, option.len, in remove_middle_option()
678 return -EINVAL; in remove_middle_option()
680 cpkt->opt_len += r; in remove_middle_option()
695 return -EINVAL; in coap_packet_remove_option()
698 if (cpkt->opt_len == 0) { in coap_packet_remove_option()
702 if (code > cpkt->delta) { in coap_packet_remove_option()
706 offset = cpkt->hdr_len; in coap_packet_remove_option()
707 previous_offset = cpkt->hdr_len; in coap_packet_remove_option()
710 while (offset < cpkt->hdr_len + cpkt->opt_len) { in coap_packet_remove_option()
711 r = parse_option(cpkt->data, offset, &offset, cpkt->hdr_len + cpkt->opt_len, in coap_packet_remove_option()
714 return -EILSEQ; in coap_packet_remove_option()
730 if (cpkt->opt_len > opt_len) { in coap_packet_remove_option()
738 remove_option_data(cpkt, previous_offset, cpkt->hdr_len + cpkt->opt_len); in coap_packet_remove_option()
739 cpkt->delta = previous_code; in coap_packet_remove_option()
756 return -EINVAL; in coap_packet_parse()
760 return -EINVAL; in coap_packet_parse()
767 cpkt->data = data; in coap_packet_parse()
768 cpkt->offset = len; in coap_packet_parse()
769 cpkt->max_len = len; in coap_packet_parse()
770 cpkt->opt_len = 0U; in coap_packet_parse()
771 cpkt->hdr_len = 0U; in coap_packet_parse()
772 cpkt->delta = 0U; in coap_packet_parse()
774 /* Token lengths 9-15 are reserved. */ in coap_packet_parse()
775 tkl = cpkt->data[0] & 0x0f; in coap_packet_parse()
777 return -EBADMSG; in coap_packet_parse()
780 cpkt->hdr_len = BASIC_HEADER_SIZE + tkl; in coap_packet_parse()
781 if (cpkt->hdr_len > len) { in coap_packet_parse()
782 return -EBADMSG; in coap_packet_parse()
785 if (cpkt->hdr_len == len) { in coap_packet_parse()
789 offset = cpkt->hdr_len; in coap_packet_parse()
798 ret = parse_option(cpkt->data, offset, &offset, cpkt->max_len, in coap_packet_parse()
801 return -EILSEQ; in coap_packet_parse()
807 cpkt->opt_len = opt_len; in coap_packet_parse()
808 cpkt->delta = delta; in coap_packet_parse()
831 path_end - path_start); in coap_packet_set_path()
851 path_end - path_start); in coap_packet_set_path()
876 path_end - path_start); in coap_packet_set_path()
899 path_end - path_start + 1); in coap_packet_set_path()
903 path_end - path_start + 1); in coap_packet_set_path()
925 if (cpkt->hdr_len == cpkt->max_len) { in coap_find_options()
929 offset = cpkt->hdr_len; in coap_find_options()
935 r = parse_option(cpkt->data, offset, &offset, in coap_find_options()
936 cpkt->max_len, &delta, &opt_len, in coap_find_options()
939 return -EINVAL; in coap_find_options()
956 if (!cpkt || !cpkt->data) { in coap_header_get_version()
960 return (cpkt->data[0] & 0xC0) >> 6; in coap_header_get_version()
965 if (!cpkt || !cpkt->data) { in coap_header_get_type()
969 return (cpkt->data[0] & 0x30) >> 4; in coap_header_get_type()
974 if (!cpkt || !cpkt->data) { in __coap_header_get_code()
978 return cpkt->data[1]; in __coap_header_get_code()
983 if (!cpkt || !cpkt->data) { in coap_header_set_code()
984 return -EINVAL; in coap_header_set_code()
987 cpkt->data[1] = code; in coap_header_set_code()
995 if (!cpkt || !cpkt->data) { in coap_header_get_token()
999 tkl = cpkt->data[0] & 0x0f; in coap_header_get_token()
1005 memcpy(token, cpkt->data + BASIC_HEADER_SIZE, tkl); in coap_header_get_token()
1062 if (!cpkt || !cpkt->data) { in coap_header_get_id()
1066 return (cpkt->data[2] << 8) | cpkt->data[3]; in coap_header_get_id()
1077 payload_len = cpkt->offset - cpkt->hdr_len - cpkt->opt_len; in coap_packet_get_payload()
1079 *len = payload_len - 1; /* subtract payload marker length */ in coap_packet_get_payload()
1085 cpkt->data + cpkt->hdr_len + cpkt->opt_len + 1; in coap_packet_get_payload()
1102 /* Single-level wildcard */ in coap_uri_path_match()
1106 /* Multi-level wildcard */ in coap_uri_path_match()
1135 static int method_from_code(const struct coap_resource *resource, in method_from_code() argument
1140 *method = resource->get; in method_from_code()
1143 *method = resource->post; in method_from_code()
1146 *method = resource->put; in method_from_code()
1149 *method = resource->del; in method_from_code()
1152 *method = resource->fetch; in method_from_code()
1155 *method = resource->patch; in method_from_code()
1158 *method = resource->ipatch; in method_from_code()
1161 return -EINVAL; in method_from_code()
1200 return -ENOTSUP; in coap_handle_request_len()
1204 return -EPERM; in coap_handle_request_len()
1210 return -ENOENT; in coap_handle_request_len()
1220 struct coap_resource *resource; in coap_handle_request() local
1222 for (resource = resources; resource && resource->path; resource++) { in coap_handle_request()
1234 ctx->block_size = block_size; in coap_block_transfer_init()
1235 ctx->total_size = total_size; in coap_block_transfer_init()
1236 ctx->current = 0; in coap_block_transfer_init()
1295 uint16_t bytes = coap_block_size_to_bytes(ctx->block_size); in coap_append_block1_option()
1300 SET_BLOCK_SIZE(val, ctx->block_size); in coap_append_block1_option()
1301 SET_MORE(val, ctx->current + bytes < ctx->total_size); in coap_append_block1_option()
1302 SET_NUM(val, ctx->current / bytes); in coap_append_block1_option()
1304 SET_BLOCK_SIZE(val, ctx->block_size); in coap_append_block1_option()
1305 SET_NUM(val, ctx->current / bytes); in coap_append_block1_option()
1317 uint16_t bytes = coap_block_size_to_bytes(ctx->block_size); in coap_append_block2_option()
1320 SET_BLOCK_SIZE(val, ctx->block_size); in coap_append_block2_option()
1321 SET_NUM(val, ctx->current / bytes); in coap_append_block2_option()
1323 SET_BLOCK_SIZE(val, ctx->block_size); in coap_append_block2_option()
1324 SET_MORE(val, ctx->current + bytes < ctx->total_size); in coap_append_block2_option()
1325 SET_NUM(val, ctx->current / bytes); in coap_append_block2_option()
1336 return coap_append_option_int(cpkt, COAP_OPTION_SIZE1, ctx->total_size); in coap_append_size1_option()
1342 return coap_append_option_int(cpkt, COAP_OPTION_SIZE2, ctx->total_size); in coap_append_size2_option()
1353 return -ENOENT; in coap_get_option_int()
1392 uint16_t offset = cpkt->hdr_len; in insert_option()
1396 uint16_t last_offset = cpkt->hdr_len; in insert_option()
1400 while (offset < cpkt->hdr_len + cpkt->opt_len) { in insert_option()
1401 r = parse_option(cpkt->data, offset, &offset, cpkt->hdr_len + cpkt->opt_len, in insert_option()
1404 return -EILSEQ; in insert_option()
1415 const uint16_t option_size = offset - last_offset; in insert_option()
1417 memmove(cpkt->data + last_offset, cpkt->data + offset, cpkt->offset - offset); in insert_option()
1418 cpkt->opt_len -= option_size; in insert_option()
1419 cpkt->offset -= option_size; in insert_option()
1422 const uint16_t new_option_delta = code - last_opt; in insert_option()
1426 return -EINVAL; in insert_option()
1428 cpkt->opt_len += r; in insert_option()
1431 r = encode_option(cpkt, option.delta - code, option.value, option.len, last_offset + r); in insert_option()
1433 return -EINVAL; in insert_option()
1435 cpkt->opt_len += r; in insert_option()
1445 if (block == -ENOENT) { in update_descriptive_block()
1449 if (size && ctx->total_size && ctx->total_size != size) { in update_descriptive_block()
1450 return -EINVAL; in update_descriptive_block()
1453 if (ctx->current > 0 && GET_BLOCK_SIZE(block) > ctx->block_size) { in update_descriptive_block()
1454 return -EINVAL; in update_descriptive_block()
1457 if (ctx->total_size && new_current > ctx->total_size) { in update_descriptive_block()
1458 return -EINVAL; in update_descriptive_block()
1462 ctx->total_size = size; in update_descriptive_block()
1464 ctx->current = new_current; in update_descriptive_block()
1465 ctx->block_size = MIN(GET_BLOCK_SIZE(block), ctx->block_size); in update_descriptive_block()
1475 if (block == -ENOENT) { in update_control_block1()
1479 if (new_current != ctx->current) { in update_control_block1()
1480 return -EINVAL; in update_control_block1()
1483 if (GET_BLOCK_SIZE(block) > ctx->block_size) { in update_control_block1()
1484 return -EINVAL; in update_control_block1()
1487 ctx->block_size = GET_BLOCK_SIZE(block); in update_control_block1()
1490 ctx->total_size = size; in update_control_block1()
1501 if (block == -ENOENT) { in update_control_block2()
1506 return -EINVAL; in update_control_block2()
1509 if (GET_NUM(block) > 0 && GET_BLOCK_SIZE(block) != ctx->block_size) { in update_control_block2()
1510 return -EINVAL; in update_control_block2()
1513 ctx->current = new_current; in update_control_block2()
1514 ctx->block_size = MIN(GET_BLOCK_SIZE(block), ctx->block_size); in update_control_block2()
1535 return update_descriptive_block(ctx, block1, size1 == -ENOENT ? 0 : size1); in coap_update_from_block()
1543 return update_descriptive_block(ctx, block2, size2 == -ENOENT ? 0 : size2); in coap_update_from_block()
1554 return -EINVAL; in coap_next_block_for_option()
1565 if ((ctx->total_size > 0) && in coap_next_block_for_option()
1566 (ctx->total_size < (ctx->current + block_len))) { in coap_next_block_for_option()
1567 return -EMSGSIZE; in coap_next_block_for_option()
1569 ctx->current += block_len; in coap_next_block_for_option()
1575 return (int)ctx->current; in coap_next_block_for_option()
1597 pending->id = coap_header_get_id(request); in coap_pending_init()
1599 memcpy(&pending->addr, addr, sizeof(*addr)); in coap_pending_init()
1602 pending->params = *params; in coap_pending_init()
1604 pending->params = coap_transmission_params; in coap_pending_init()
1607 pending->data = request->data; in coap_pending_init()
1608 pending->len = request->offset; in coap_pending_init()
1609 pending->t0 = k_uptime_get(); in coap_pending_init()
1610 pending->retries = pending->params.max_retransmission; in coap_pending_init()
1622 if (p->data == 0) { in coap_pending_next_unused()
1637 if (!r->reply) { in coap_reply_next_unused()
1647 if (addr->sa_family == AF_UNSPEC) { in is_addr_unspecified()
1651 if (addr->sa_family == AF_INET6) { in is_addr_unspecified()
1653 &(net_sin6(addr)->sin6_addr)); in is_addr_unspecified()
1654 } else if (addr->sa_family == AF_INET) { in is_addr_unspecified()
1655 return net_sin(addr)->sin_addr.s4_addr32[0] == 0U; in is_addr_unspecified()
1668 if (is_addr_unspecified(&o->addr)) { in coap_observer_next_unused()
1685 if (!p->timeout) { in coap_pending_received()
1689 if (resp_id != p->id) { in coap_pending_received()
1707 if (!p->timeout) { in coap_pending_next_to_expire()
1711 expiry = p->t0 + p->timeout; in coap_pending_next_to_expire()
1725 const uint16_t random_percent = params->ack_random_percent ? params->ack_random_percent in init_ack_timeout()
1727 const uint32_t max_ack = params->ack_timeout * random_percent / 100U; in init_ack_timeout()
1728 const uint32_t min_ack = params->ack_timeout; in init_ack_timeout()
1732 * Ref: https://tools.ietf.org/html/rfc7252#section-4.8 in init_ack_timeout()
1734 return min_ack + (sys_rand32_get() % (max_ack - min_ack)); in init_ack_timeout()
1736 return params->ack_timeout; in init_ack_timeout()
1742 if (pending->timeout == 0) { in coap_pending_cycle()
1744 pending->timeout = init_ack_timeout(&pending->params); in coap_pending_cycle()
1748 if (pending->retries == 0) { in coap_pending_cycle()
1752 pending->t0 += pending->timeout; in coap_pending_cycle()
1753 pending->timeout = pending->timeout * pending->params.coap_backoff_percent / 100; in coap_pending_cycle()
1754 pending->retries--; in coap_pending_cycle()
1761 pending->timeout = 0; in coap_pending_clear()
1762 pending->data = NULL; in coap_pending_clear()
1781 if (p->timeout) { in coap_pendings_count()
1792 return (v1 < v2 && v2 - v1 < (1 << 23)) in coap_age_is_newer()
1793 || (v1 > v2 && v1 - v2 > (1 << 23)); in coap_age_is_newer()
1796 static inline void coap_observer_increment_age(struct coap_resource *resource) in coap_observer_increment_age() argument
1798 resource->age++; in coap_observer_increment_age()
1799 if (resource->age > COAP_OBSERVE_MAX_AGE) { in coap_observer_increment_age()
1800 resource->age = COAP_OBSERVE_FIRST_OFFSET; in coap_observer_increment_age()
1811 uint16_t id; in coap_response_received() local
1820 id = coap_header_get_id(response); in coap_response_received()
1826 if ((r->id == 0U) && (r->tkl == 0U)) { in coap_response_received()
1830 /* Piggybacked must match id when token is empty */ in coap_response_received()
1831 if ((r->id != id) && (tkl == 0U)) { in coap_response_received()
1835 if (tkl > 0 && memcmp(r->token, token, tkl)) { in coap_response_received()
1841 if (age == -ENOENT || coap_age_is_newer(r->age, age)) { in coap_response_received()
1842 r->age = age; in coap_response_received()
1844 r->reply(response, r, from); in coap_response_received()
1860 reply->id = coap_header_get_id(request); in coap_reply_init()
1864 memcpy(reply->token, token, tkl); in coap_reply_init()
1867 reply->tkl = tkl; in coap_reply_init()
1870 reply->age = -1; in coap_reply_init()
1888 int coap_resource_notify(struct coap_resource *resource) in coap_resource_notify() argument
1892 if (!resource->notify) { in coap_resource_notify()
1893 return -ENOENT; in coap_resource_notify()
1896 if (sys_slist_is_empty(&resource->observers)) { in coap_resource_notify()
1900 coap_observer_increment_age(resource); in coap_resource_notify()
1902 SYS_SLIST_FOR_EACH_CONTAINER(&resource->observers, o, list) { in coap_resource_notify()
1903 resource->notify(resource, o); in coap_resource_notify()
1918 observer->tkl = coap_header_get_token(request, observer->token); in coap_observer_init()
1920 net_ipaddr_copy(&observer->addr, addr); in coap_observer_init()
1923 static inline void coap_observer_raise_event(struct coap_resource *resource, in coap_observer_raise_event() argument
1929 .resource = resource, in coap_observer_raise_event()
1935 ARG_UNUSED(resource); in coap_observer_raise_event()
1942 bool coap_register_observer(struct coap_resource *resource, in coap_register_observer() argument
1947 sys_slist_append(&resource->observers, &observer->list); in coap_register_observer()
1949 first = resource->age == 0; in coap_register_observer()
1951 resource->age = COAP_OBSERVE_FIRST_OFFSET; in coap_register_observer()
1954 coap_observer_raise_event(resource, observer, NET_EVENT_COAP_OBSERVER_ADDED); in coap_register_observer()
1959 bool coap_remove_observer(struct coap_resource *resource, in coap_remove_observer() argument
1962 if (!sys_slist_find_and_remove(&resource->observers, &observer->list)) { in coap_remove_observer()
1966 coap_observer_raise_event(resource, observer, NET_EVENT_COAP_OBSERVER_REMOVED); in coap_remove_observer()
1974 /* FIXME: Should we consider ipv6-mapped ipv4 addresses as equal to in sockaddr_equal()
1977 if (a->sa_family != b->sa_family) { in sockaddr_equal()
1981 if (a->sa_family == AF_INET) { in sockaddr_equal()
1985 if (a4->sin_port != b4->sin_port) { in sockaddr_equal()
1989 return net_ipv4_addr_cmp(&a4->sin_addr, &b4->sin_addr); in sockaddr_equal()
1992 if (b->sa_family == AF_INET6) { in sockaddr_equal()
1996 if (a6->sin6_port != b6->sin6_port) { in sockaddr_equal()
2000 return net_ipv6_addr_cmp(&a6->sin6_addr, &b6->sin6_addr); in sockaddr_equal()
2019 if (o->tkl == token_len && in coap_find_observer()
2020 memcmp(o->token, token, token_len) == 0 && in coap_find_observer()
2021 sockaddr_equal(&o->addr, addr)) { in coap_find_observer()
2038 if (sockaddr_equal(&o->addr, addr)) { in coap_find_observer_by_addr()
2057 if (o->tkl == token_len && memcmp(o->token, token, token_len) == 0) { in coap_find_observer_by_token()