Lines Matching refs:cpkt

68 static int insert_option(struct coap_packet *cpkt, uint16_t code, const uint8_t *value,
71 static inline void encode_u8(struct coap_packet *cpkt, uint16_t offset, uint8_t data) in encode_u8() argument
73 cpkt->data[offset] = data; in encode_u8()
74 ++cpkt->offset; in encode_u8()
77 static inline void encode_be16(struct coap_packet *cpkt, uint16_t offset, uint16_t data) in encode_be16() argument
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()
84 static inline void encode_buffer(struct coap_packet *cpkt, uint16_t offset, const uint8_t *data, in encode_buffer() argument
87 memcpy(cpkt->data + offset, data, len); in encode_buffer()
88 cpkt->offset += len; in encode_buffer()
91 static bool enough_space(struct coap_packet *cpkt, const uint16_t bytes_to_add) in enough_space() argument
93 return (cpkt != NULL) && (cpkt->max_len - cpkt->offset >= bytes_to_add); in enough_space()
96 static inline bool append_u8(struct coap_packet *cpkt, uint8_t data) in append_u8() argument
98 if (!enough_space(cpkt, 1)) { in append_u8()
102 encode_u8(cpkt, cpkt->offset, data); in append_u8()
107 static inline bool insert_u8(struct coap_packet *cpkt, uint8_t data, uint16_t offset) in insert_u8() argument
109 if (!enough_space(cpkt, 1)) { in insert_u8()
113 memmove(&cpkt->data[offset + 1], &cpkt->data[offset], cpkt->offset - offset); in insert_u8()
115 encode_u8(cpkt, offset, data); in insert_u8()
120 static inline bool append_be16(struct coap_packet *cpkt, uint16_t data) in append_be16() argument
122 if (!enough_space(cpkt, 2)) { in append_be16()
126 encode_be16(cpkt, cpkt->offset, data); in append_be16()
131 static inline bool insert_be16(struct coap_packet *cpkt, uint16_t data, size_t offset) in insert_be16() argument
133 if (!enough_space(cpkt, 2)) { in insert_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()
144 static inline bool append(struct coap_packet *cpkt, const uint8_t *data, uint16_t len) in append() argument
146 if (data == NULL || !enough_space(cpkt, len)) { in append()
150 encode_buffer(cpkt, cpkt->offset, data, len); in append()
155 static inline bool insert(struct coap_packet *cpkt, const uint8_t *data, uint16_t len, in insert() argument
158 if (data == NULL || !enough_space(cpkt, len)) { in insert()
162 memmove(&cpkt->data[offset + len], &cpkt->data[offset], cpkt->offset - offset); in insert()
164 encode_buffer(cpkt, offset, data, len); in insert()
169 int coap_packet_init(struct coap_packet *cpkt, uint8_t *data, uint16_t max_len, in coap_packet_init() argument
176 if (!cpkt || !data || !max_len) { in coap_packet_init()
180 memset(cpkt, 0, sizeof(*cpkt)); 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()
191 res = append_u8(cpkt, hdr); in coap_packet_init()
196 res = append_u8(cpkt, code); in coap_packet_init()
201 res = append_be16(cpkt, id); in coap_packet_init()
207 res = append(cpkt, token, token_len); in coap_packet_init()
214 cpkt->hdr_len = 1 + 1 + 2 + token_len; in coap_packet_init()
219 int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req, in coap_ack_init() argument
231 return coap_packet_init(cpkt, data, max_len, ver, COAP_TYPE_ACK, tkl, in coap_ack_init()
235 int coap_rst_init(struct coap_packet *cpkt, const struct coap_packet *req, in coap_rst_init() argument
244 return coap_packet_init(cpkt, data, max_len, ver, COAP_TYPE_RESET, 0, in coap_rst_init()
281 static int encode_option(struct coap_packet *cpkt, uint16_t code, const uint8_t *value, in encode_option() argument
299 res = insert_u8(cpkt, opt, offset); in encode_option()
306 res = insert_u8(cpkt, (uint8_t)delta_ext, offset); in encode_option()
312 res = insert_be16(cpkt, delta_ext, offset); in encode_option()
320 res = insert_u8(cpkt, (uint8_t)len_ext, offset); in encode_option()
326 res = insert_be16(cpkt, len_ext, offset); in encode_option()
334 res = insert(cpkt, value, len, offset); in encode_option()
344 int coap_packet_append_option(struct coap_packet *cpkt, uint16_t code, in coap_packet_append_option() argument
349 if (!cpkt) { in coap_packet_append_option()
357 if (code < cpkt->delta) { in coap_packet_append_option()
359 return insert_option(cpkt, code, value, len); 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()
372 cpkt->opt_len += r; in coap_packet_append_option()
373 cpkt->delta += code; in coap_packet_append_option()
378 int coap_append_option_int(struct coap_packet *cpkt, uint16_t code, in coap_append_option_int() argument
401 return coap_packet_append_option(cpkt, code, data, len); in coap_append_option_int()
426 int coap_packet_append_payload_marker(struct coap_packet *cpkt) in coap_packet_append_payload_marker() argument
428 return append_u8(cpkt, COAP_MARKER) ? 0 : -EINVAL; in coap_packet_append_payload_marker()
431 int coap_packet_append_payload(struct coap_packet *cpkt, const uint8_t *payload, in coap_packet_append_payload() argument
434 return append(cpkt, payload, payload_len) ? 0 : -EINVAL; in coap_packet_append_payload()
640 static void remove_option_data(struct coap_packet *cpkt, in remove_option_data() argument
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()
654 static int remove_middle_option(struct coap_packet *cpkt, in remove_middle_option() argument
665 r = parse_option(cpkt->data, offset, &offset, cpkt->hdr_len + cpkt->opt_len, in remove_middle_option()
672 remove_option_data(cpkt, previous_offset, offset); in remove_middle_option()
675 r = encode_option(cpkt, option.delta - previous_code, option.value, option.len, in remove_middle_option()
680 cpkt->opt_len += r; in remove_middle_option()
684 int coap_packet_remove_option(struct coap_packet *cpkt, uint16_t code) in coap_packet_remove_option() argument
694 if (!cpkt) { 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()
730 if (cpkt->opt_len > opt_len) { in coap_packet_remove_option()
732 r = remove_middle_option(cpkt, offset, opt_delta, previous_offset, previous_code); 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()
745 int coap_packet_parse(struct coap_packet *cpkt, uint8_t *data, uint16_t len, in coap_packet_parse() argument
755 if (!cpkt || !data) { 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()
775 tkl = cpkt->data[0] & 0x0f; 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()
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()
807 cpkt->opt_len = opt_len; in coap_packet_parse()
808 cpkt->delta = delta; in coap_packet_parse()
813 int coap_packet_set_path(struct coap_packet *cpkt, const char *path) in coap_packet_set_path() argument
829 ret = coap_packet_append_option(cpkt, COAP_OPTION_URI_PATH, in coap_packet_set_path()
849 ret = coap_packet_append_option(cpkt, COAP_OPTION_URI_PATH, in coap_packet_set_path()
874 ret = coap_packet_append_option(cpkt, COAP_OPTION_URI_QUERY, in coap_packet_set_path()
897 ret = coap_packet_append_option(cpkt, COAP_OPTION_URI_QUERY, in coap_packet_set_path()
901 ret = coap_packet_append_option(cpkt, COAP_OPTION_URI_PATH, in coap_packet_set_path()
915 int coap_find_options(const struct coap_packet *cpkt, uint16_t code, in coap_find_options() argument
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()
954 uint8_t coap_header_get_version(const struct coap_packet *cpkt) in coap_header_get_version() argument
956 if (!cpkt || !cpkt->data) { in coap_header_get_version()
960 return (cpkt->data[0] & 0xC0) >> 6; in coap_header_get_version()
963 uint8_t coap_header_get_type(const struct coap_packet *cpkt) in coap_header_get_type() argument
965 if (!cpkt || !cpkt->data) { in coap_header_get_type()
969 return (cpkt->data[0] & 0x30) >> 4; in coap_header_get_type()
972 static uint8_t __coap_header_get_code(const struct coap_packet *cpkt) in __coap_header_get_code() argument
974 if (!cpkt || !cpkt->data) { in __coap_header_get_code()
978 return cpkt->data[1]; in __coap_header_get_code()
981 int coap_header_set_code(const struct coap_packet *cpkt, uint8_t code) in coap_header_set_code() argument
983 if (!cpkt || !cpkt->data) { in coap_header_set_code()
987 cpkt->data[1] = code; in coap_header_set_code()
991 uint8_t coap_header_get_token(const struct coap_packet *cpkt, uint8_t *token) in coap_header_get_token() argument
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()
1011 uint8_t coap_header_get_code(const struct coap_packet *cpkt) in coap_header_get_code() argument
1013 uint8_t code = __coap_header_get_code(cpkt); in coap_header_get_code()
1060 uint16_t coap_header_get_id(const struct coap_packet *cpkt) in coap_header_get_id() argument
1062 if (!cpkt || !cpkt->data) { in coap_header_get_id()
1066 return (cpkt->data[2] << 8) | cpkt->data[3]; in coap_header_get_id()
1069 const uint8_t *coap_packet_get_payload(const struct coap_packet *cpkt, uint16_t *len) in coap_packet_get_payload() argument
1073 if (!cpkt || !len) { in coap_packet_get_payload()
1077 payload_len = cpkt->offset - cpkt->hdr_len - cpkt->opt_len; in coap_packet_get_payload()
1085 cpkt->data + cpkt->hdr_len + cpkt->opt_len + 1; in coap_packet_get_payload()
1166 static inline bool is_empty_message(const struct coap_packet *cpkt) in is_empty_message() argument
1168 return __coap_header_get_code(cpkt) == COAP_CODE_EMPTY; in is_empty_message()
1171 bool coap_packet_is_request(const struct coap_packet *cpkt) in coap_packet_is_request() argument
1173 uint8_t code = coap_header_get_code(cpkt); in coap_packet_is_request()
1178 int coap_handle_request_len(struct coap_packet *cpkt, in coap_handle_request_len() argument
1185 if (!coap_packet_is_request(cpkt)) { in coap_handle_request_len()
1198 code = coap_header_get_code(cpkt); in coap_handle_request_len()
1207 return method(&resources[i], cpkt, addr, addr_len); in coap_handle_request_len()
1213 int coap_handle_request(struct coap_packet *cpkt, in coap_handle_request() argument
1226 return coap_handle_request_len(cpkt, resources, resources_len, options, opt_num, addr, in coap_handle_request()
1249 int coap_append_descriptive_block_option(struct coap_packet *cpkt, struct coap_block_context *ctx) in coap_append_descriptive_block_option() argument
1251 if (coap_packet_is_request(cpkt)) { in coap_append_descriptive_block_option()
1252 return coap_append_block1_option(cpkt, ctx); in coap_append_descriptive_block_option()
1254 return coap_append_block2_option(cpkt, ctx); in coap_append_descriptive_block_option()
1258 bool coap_has_descriptive_block_option(struct coap_packet *cpkt) in coap_has_descriptive_block_option() argument
1260 if (coap_packet_is_request(cpkt)) { in coap_has_descriptive_block_option()
1261 return coap_get_option_int(cpkt, COAP_OPTION_BLOCK1) >= 0; in coap_has_descriptive_block_option()
1263 return coap_get_option_int(cpkt, COAP_OPTION_BLOCK2) >= 0; in coap_has_descriptive_block_option()
1267 int coap_remove_descriptive_block_option(struct coap_packet *cpkt) in coap_remove_descriptive_block_option() argument
1269 if (coap_packet_is_request(cpkt)) { in coap_remove_descriptive_block_option()
1270 return coap_packet_remove_option(cpkt, COAP_OPTION_BLOCK1); in coap_remove_descriptive_block_option()
1272 return coap_packet_remove_option(cpkt, COAP_OPTION_BLOCK2); in coap_remove_descriptive_block_option()
1276 bool coap_block_has_more(struct coap_packet *cpkt) in coap_block_has_more() argument
1281 if (coap_packet_is_request(cpkt)) { in coap_block_has_more()
1282 opt = coap_get_option_int(cpkt, COAP_OPTION_BLOCK1); in coap_block_has_more()
1284 opt = coap_get_option_int(cpkt, COAP_OPTION_BLOCK2); in coap_block_has_more()
1292 int coap_append_block1_option(struct coap_packet *cpkt, in coap_append_block1_option() argument
1299 if (coap_packet_is_request(cpkt)) { in coap_append_block1_option()
1308 r = coap_append_option_int(cpkt, COAP_OPTION_BLOCK1, val); in coap_append_block1_option()
1313 int coap_append_block2_option(struct coap_packet *cpkt, in coap_append_block2_option() argument
1319 if (coap_packet_is_request(cpkt)) { in coap_append_block2_option()
1328 r = coap_append_option_int(cpkt, COAP_OPTION_BLOCK2, val); in coap_append_block2_option()
1333 int coap_append_size1_option(struct coap_packet *cpkt, in coap_append_size1_option() argument
1336 return coap_append_option_int(cpkt, COAP_OPTION_SIZE1, ctx->total_size); in coap_append_size1_option()
1339 int coap_append_size2_option(struct coap_packet *cpkt, in coap_append_size2_option() argument
1342 return coap_append_option_int(cpkt, COAP_OPTION_SIZE2, ctx->total_size); in coap_append_size2_option()
1345 int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code) in coap_get_option_int() argument
1351 count = coap_find_options(cpkt, code, &option, count); in coap_get_option_int()
1361 int coap_get_block1_option(const struct coap_packet *cpkt, bool *has_more, uint32_t *block_number) in coap_get_block1_option() argument
1363 int ret = coap_get_option_int(cpkt, COAP_OPTION_BLOCK1); in coap_get_block1_option()
1375 int coap_get_block2_option(const struct coap_packet *cpkt, bool *has_more, in coap_get_block2_option() argument
1378 int ret = coap_get_option_int(cpkt, COAP_OPTION_BLOCK2); in coap_get_block2_option()
1390 int insert_option(struct coap_packet *cpkt, uint16_t code, const uint8_t *value, uint16_t len) in insert_option() argument
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()
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()
1424 r = encode_option(cpkt, new_option_delta, value, len, last_offset); 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()
1435 cpkt->opt_len += r; in insert_option()
1519 int coap_update_from_block(const struct coap_packet *cpkt, in coap_update_from_block() argument
1524 block1 = coap_get_option_int(cpkt, COAP_OPTION_BLOCK1); in coap_update_from_block()
1525 block2 = coap_get_option_int(cpkt, COAP_OPTION_BLOCK2); in coap_update_from_block()
1526 size1 = coap_get_option_int(cpkt, COAP_OPTION_SIZE1); in coap_update_from_block()
1527 size2 = coap_get_option_int(cpkt, COAP_OPTION_SIZE2); in coap_update_from_block()
1529 if (coap_packet_is_request(cpkt)) { in coap_update_from_block()
1546 int coap_next_block_for_option(const struct coap_packet *cpkt, in coap_next_block_for_option() argument
1557 block = coap_get_option_int(cpkt, option); in coap_next_block_for_option()
1563 coap_packet_get_payload(cpkt, &block_len); in coap_next_block_for_option()
1578 size_t coap_next_block(const struct coap_packet *cpkt, in coap_next_block() argument
1584 option = coap_packet_is_request(cpkt) ? COAP_OPTION_BLOCK1 : COAP_OPTION_BLOCK2; in coap_next_block()
1585 ret = coap_next_block_for_option(cpkt, ctx, option); in coap_next_block()