Lines Matching +full:current +full:- +full:limiting
3 * Copyright (c) 2018-2019 Foundries.io
5 * SPDX-License-Identifier: Apache-2.0
74 #define OUTPUT_CONTEXT_IN_USE_MARK (enum coap_block_size)(-1)
84 /* Shared set of in-flight LwM2M messages */
111 /* block-wise transfer functions */
132 return -EFAULT; in init_block_ctx()
145 if (timestamp - block1_contexts[i].timestamp > in init_block_ctx()
158 return -ENOMEM; in init_block_ctx()
161 memcpy(&(*ctx)->path, path, sizeof(struct lwm2m_obj_path)); in init_block_ctx()
162 coap_block_transfer_init(&(*ctx)->ctx, lwm2m_default_block_size(), 0); in init_block_ctx()
163 (*ctx)->timestamp = timestamp; in init_block_ctx()
164 (*ctx)->expected = 0; in init_block_ctx()
165 (*ctx)->last_block = false; in init_block_ctx()
166 memset(&(*ctx)->opaque, 0, sizeof((*ctx)->opaque)); in init_block_ctx()
179 return -EFAULT; in get_block_ctx()
190 (*ctx)->timestamp = k_uptime_get(); in get_block_ctx()
198 return -ENOENT; in get_block_ctx()
211 memset(&ctx->path, 0, sizeof(struct lwm2m_obj_path)); in free_block_ctx()
218 int ret = -ENOMEM; in request_output_block_ctx()
227 (*ctx)->block_size = OUTPUT_CONTEXT_IN_USE_MARK; in request_output_block_ctx()
291 coap_packet_get_payload(&msg->body_encode_buffer, &complete_payload_len); in build_msg_block_for_send()
295 NET_ASSERT(msg->msg_data == msg->cpkt.data, in build_msg_block_for_send()
299 return -EINVAL; in build_msg_block_for_send()
306 ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), msg->body_encode_buffer.data, in build_msg_block_for_send()
307 msg->body_encode_buffer.hdr_len); in build_msg_block_for_send()
311 msg->cpkt.hdr_len = msg->body_encode_buffer.hdr_len; in build_msg_block_for_send()
314 void *user_data = msg->reply ? msg->reply->user_data : NULL; in build_msg_block_for_send()
320 if (msg->type == COAP_TYPE_ACK) { in build_msg_block_for_send()
321 msg->mid = coap_header_get_id(msg->in.in_cpkt); in build_msg_block_for_send()
322 tkl = coap_header_get_token(msg->in.in_cpkt, token); in build_msg_block_for_send()
324 msg->mid = coap_next_id(); in build_msg_block_for_send()
327 msg->token = token; in build_msg_block_for_send()
328 msg->tkl = tkl; in build_msg_block_for_send()
335 if (msg->reply) { in build_msg_block_for_send()
336 msg->reply->user_data = user_data; in build_msg_block_for_send()
341 ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), in build_msg_block_for_send()
342 msg->body_encode_buffer.data + msg->body_encode_buffer.hdr_len, in build_msg_block_for_send()
343 msg->body_encode_buffer.opt_len); in build_msg_block_for_send()
347 msg->cpkt.opt_len = msg->body_encode_buffer.opt_len; in build_msg_block_for_send()
349 msg->cpkt.delta = msg->body_encode_buffer.delta; in build_msg_block_for_send()
352 ret = request_output_block_ctx(&msg->out.block_ctx); in build_msg_block_for_send()
357 ret = coap_block_transfer_init(msg->out.block_ctx, block_size, in build_msg_block_for_send()
362 if (msg->type == COAP_TYPE_ACK) { in build_msg_block_for_send()
365 msg->block_send = true; in build_msg_block_for_send()
368 msg->out.block_ctx->current = block_num * block_size_bytes; in build_msg_block_for_send()
369 msg->out.block_ctx->block_size = block_size; in build_msg_block_for_send()
372 ret = coap_append_descriptive_block_option(&msg->cpkt, msg->out.block_ctx); in build_msg_block_for_send()
377 ret = coap_packet_append_payload_marker(&msg->cpkt); in build_msg_block_for_send()
382 payload_size = MIN(complete_payload_len - block_num * block_size_bytes, block_size_bytes); in build_msg_block_for_send()
383 ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), in build_msg_block_for_send()
399 msg->body_encode_buffer = msg->cpkt; in prepare_msg_for_send()
402 msg->cpkt.data = msg->msg_data; in prepare_msg_for_send()
403 msg->cpkt.offset = 0; in prepare_msg_for_send()
404 msg->cpkt.max_len = MAX_PACKET_SIZE; in prepare_msg_for_send()
406 payload = coap_packet_get_payload(&msg->body_encode_buffer, &len); in prepare_msg_for_send()
410 ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), msg->body_encode_buffer.data, in prepare_msg_for_send()
411 msg->body_encode_buffer.offset); in prepare_msg_for_send()
416 msg->cpkt.hdr_len = msg->body_encode_buffer.hdr_len; in prepare_msg_for_send()
417 msg->cpkt.opt_len = msg->body_encode_buffer.opt_len; in prepare_msg_for_send()
420 release_body_encode_buffer(&msg->body_encode_buffer.data); in prepare_msg_for_send()
421 msg->body_encode_buffer.data = NULL; in prepare_msg_for_send()
423 NET_ASSERT(msg->out.block_ctx == NULL, "Expecting to have no context to release"); in prepare_msg_for_send()
431 coap_packet_append_option(&msg->body_encode_buffer, COAP_OPTION_ETAG, in prepare_msg_for_send()
456 while (!sys_slist_is_empty(&client_ctx->observer)) { in lwm2m_engine_context_close()
457 obs_node = sys_slist_get_not_empty(&client_ctx->observer); in lwm2m_engine_context_close()
463 if (msg->ctx == client_ctx) { in lwm2m_engine_context_close()
468 coap_pendings_clear(client_ctx->pendings, ARRAY_SIZE(client_ctx->pendings)); in lwm2m_engine_context_close()
469 coap_replies_clear(client_ctx->replies, ARRAY_SIZE(client_ctx->replies)); in lwm2m_engine_context_close()
471 client_ctx->connection_suspended = false; in lwm2m_engine_context_close()
473 client_ctx->buffer_client_messages = true; in lwm2m_engine_context_close()
480 sys_slist_init(&client_ctx->pending_sends); in lwm2m_engine_context_init()
481 sys_slist_init(&client_ctx->observer); in lwm2m_engine_context_init()
482 client_ctx->connection_suspended = false; in lwm2m_engine_context_init()
484 client_ctx->buffer_client_messages = true; in lwm2m_engine_context_init()
485 sys_slist_init(&client_ctx->queued_messages); in lwm2m_engine_context_init()
487 k_mutex_init(&client_ctx->lock); in lwm2m_engine_context_init()
495 *id[4] = {&path->obj_id, &path->obj_inst_id, &path->res_id, &path->res_inst_id}; in coap_options_to_path()
497 path->level = options_count; in coap_options_to_path()
502 path->level = i; in coap_options_to_path()
507 return options_count == path->level ? 0 : -EINVAL; in coap_options_to_path()
521 if (pending != NULL && msg->pending == pending) { in find_msg()
525 if (reply != NULL && msg->reply == reply) { in find_msg()
565 if (msg->pending) { in lm2m_message_clear_allocations()
566 coap_pending_clear(msg->pending); in lm2m_message_clear_allocations()
567 msg->pending = NULL; in lm2m_message_clear_allocations()
570 if (msg->reply) { in lm2m_message_clear_allocations()
572 coap_reply_clear(msg->reply); in lm2m_message_clear_allocations()
573 msg->reply = NULL; in lm2m_message_clear_allocations()
585 if (msg->ctx) { in lwm2m_reset_message()
586 lwm2m_client_lock(msg->ctx); in lwm2m_reset_message()
587 sys_slist_find_and_remove(&msg->ctx->pending_sends, &msg->node); in lwm2m_reset_message()
589 sys_slist_find_and_remove(&msg->ctx->queued_messages, &msg->node); in lwm2m_reset_message()
591 lwm2m_client_unlock(msg->ctx); in lwm2m_reset_message()
597 release_output_block_ctx(&msg->out.block_ctx); in lwm2m_reset_message()
598 release_body_encode_buffer(&msg->body_encode_buffer.data); in lwm2m_reset_message()
603 msg->message_timeout_cb = NULL; in lwm2m_reset_message()
604 (void)memset(&msg->cpkt, 0, sizeof(msg->cpkt)); in lwm2m_reset_message()
606 msg->cache_info = NULL; in lwm2m_reset_message()
619 if (!msg || !msg->ctx) { in lwm2m_init_message()
621 return -EINVAL; in lwm2m_init_message()
624 if (msg->tkl == LWM2M_MSG_TOKEN_GENERATE_NEW) { in lwm2m_init_message()
627 } else if (msg->token && msg->tkl != 0) { in lwm2m_init_message()
628 tokenlen = msg->tkl; in lwm2m_init_message()
629 token = msg->token; in lwm2m_init_message()
634 msg->cache_info = NULL; in lwm2m_init_message()
637 if (msg->body_encode_buffer.data == NULL) { in lwm2m_init_message()
645 msg->body_encode_buffer.data = body_data; in lwm2m_init_message()
649 body_data = msg->msg_data; in lwm2m_init_message()
650 body_data_max_len = sizeof(msg->msg_data); in lwm2m_init_message()
653 body_data = msg->msg_data; in lwm2m_init_message()
654 body_data_max_len = sizeof(msg->msg_data); in lwm2m_init_message()
656 r = coap_packet_init(&msg->cpkt, body_data, body_data_max_len, COAP_VERSION_1, msg->type, in lwm2m_init_message()
657 tokenlen, token, msg->code, msg->mid); in lwm2m_init_message()
664 if (msg->type != COAP_TYPE_CON) { in lwm2m_init_message()
668 lwm2m_client_lock(msg->ctx); in lwm2m_init_message()
670 msg->pending = coap_pending_next_unused(msg->ctx->pendings, ARRAY_SIZE(msg->ctx->pendings)); in lwm2m_init_message()
671 if (!msg->pending) { in lwm2m_init_message()
674 r = -ENOMEM; in lwm2m_init_message()
678 r = coap_pending_init(msg->pending, &msg->cpkt, &msg->ctx->remote_addr, NULL); in lwm2m_init_message()
686 if (msg->reply_cb) { in lwm2m_init_message()
687 msg->reply = in lwm2m_init_message()
688 coap_reply_next_unused(msg->ctx->replies, ARRAY_SIZE(msg->ctx->replies)); in lwm2m_init_message()
689 if (!msg->reply) { in lwm2m_init_message()
691 r = -ENOMEM; in lwm2m_init_message()
695 coap_reply_clear(msg->reply); in lwm2m_init_message()
696 coap_reply_init(msg->reply, &msg->cpkt); in lwm2m_init_message()
697 msg->reply->reply = msg->reply_cb; in lwm2m_init_message()
700 lwm2m_client_unlock(msg->ctx); in lwm2m_init_message()
705 lwm2m_client_unlock(msg->ctx); in lwm2m_init_message()
717 if (msg->body_encode_buffer.data == msg->cpkt.data) { in lwm2m_send_message_async()
727 int ret = lwm2m_rd_client_connection_resume(msg->ctx); in lwm2m_send_message_async()
729 if (ret && ret != -EPERM) { in lwm2m_send_message_async()
735 lwm2m_client_lock(msg->ctx); in lwm2m_send_message_async()
736 sys_slist_append(&msg->ctx->pending_sends, &msg->node); in lwm2m_send_message_async()
737 lwm2m_client_unlock(msg->ctx); in lwm2m_send_message_async()
751 ret = lwm2m_rd_client_connection_resume(msg->ctx); in lwm2m_information_interface_send()
758 lwm2m_client_lock(msg->ctx); in lwm2m_information_interface_send()
759 sys_slist_append(&msg->ctx->pending_sends, &msg->node); in lwm2m_information_interface_send()
760 lwm2m_client_unlock(msg->ctx); in lwm2m_information_interface_send()
762 lwm2m_engine_connection_resume(msg->ctx); in lwm2m_information_interface_send()
766 if (msg->ctx->buffer_client_messages) { in lwm2m_information_interface_send()
767 lwm2m_client_lock(msg->ctx); in lwm2m_information_interface_send()
768 sys_slist_append(&msg->ctx->queued_messages, &msg->node); in lwm2m_information_interface_send()
769 lwm2m_client_unlock(msg->ctx); in lwm2m_information_interface_send()
786 return -ENOMEM; in lwm2m_send_empty_ack()
789 msg->type = COAP_TYPE_ACK; in lwm2m_send_empty_ack()
790 msg->code = COAP_CODE_EMPTY; in lwm2m_send_empty_ack()
791 msg->mid = mid; in lwm2m_send_empty_ack()
798 ret = zsock_send(client_ctx->sock_fd, msg->cpkt.data, msg->cpkt.offset, 0); in lwm2m_send_empty_ack()
802 ret = -errno; in lwm2m_send_empty_ack()
814 if (client_ctx == NULL || client_ctx->processed_req == NULL) { in lwm2m_acknowledge()
818 request = (struct lwm2m_message *)client_ctx->processed_req; in lwm2m_acknowledge()
820 if (request->acknowledged) { in lwm2m_acknowledge()
824 if (lwm2m_send_empty_ack(client_ctx, request->mid) < 0) { in lwm2m_acknowledge()
828 request->acknowledged = true; in lwm2m_acknowledge()
839 ret = engine_put_begin(&msg->out, NULL); in lwm2m_register_payload_handler()
846 if (obj->obj_id == LWM2M_OBJECT_SECURITY_ID) { in lwm2m_register_payload_handler()
853 if (obj->instance_count == 0U || lwm2m_engine_shall_report_obj_version(obj)) { in lwm2m_register_payload_handler()
854 ret = engine_put_corelink(&msg->out, &LWM2M_OBJ(obj->obj_id)); in lwm2m_register_payload_handler()
859 if (obj->instance_count == 0U) { in lwm2m_register_payload_handler()
865 if (obj_inst->obj->obj_id == obj->obj_id) { in lwm2m_register_payload_handler()
867 &msg->out, in lwm2m_register_payload_handler()
868 &LWM2M_OBJ(obj_inst->obj->obj_id, obj_inst->obj_inst_id)); in lwm2m_register_payload_handler()
884 out->writer = &link_format_writer; in select_writer()
888 out->writer = &opaque_writer; in select_writer()
893 out->writer = &plain_text_writer; in select_writer()
899 out->writer = &oma_tlv_writer; in select_writer()
906 out->writer = &json_writer; in select_writer()
912 out->writer = &senml_json_writer; in select_writer()
918 out->writer = &cbor_writer; in select_writer()
924 out->writer = &senml_cbor_writer; in select_writer()
930 return -ECANCELED; in select_writer()
941 in->reader = &opaque_reader; in select_reader()
946 in->reader = &plain_text_reader; in select_reader()
952 in->reader = &oma_tlv_reader; in select_reader()
959 in->reader = &json_reader; in select_reader()
965 in->reader = &senml_json_reader; in select_reader()
971 in->reader = &cbor_reader; in select_reader()
977 in->reader = &senml_cbor_reader; in select_reader()
982 return -ENOMSG; in select_reader()
1002 if (msg->in.block_ctx != NULL) { in lwm2m_write_handler_opaque()
1003 last_block = msg->in.block_ctx->last_block; in lwm2m_write_handler_opaque()
1006 opaque_ctx = msg->in.block_ctx->opaque; in lwm2m_write_handler_opaque()
1014 if (res->validate_cb) { in lwm2m_write_handler_opaque()
1015 write_buf = msg->ctx->validate_buf; in lwm2m_write_handler_opaque()
1016 write_buf_len = sizeof(msg->ctx->validate_buf); in lwm2m_write_handler_opaque()
1025 len = engine_get_opaque(&msg->in, write_buf, MIN(data_len, write_buf_len), in lwm2m_write_handler_opaque()
1032 if (res->validate_cb) { in lwm2m_write_handler_opaque()
1033 ret = res->validate_cb(obj_inst->obj_inst_id, res->res_id, in lwm2m_write_handler_opaque()
1034 res_inst->res_inst_id, write_buf, len, in lwm2m_write_handler_opaque()
1036 msg->in.block_ctx->ctx.current); in lwm2m_write_handler_opaque()
1038 /* -EEXIST will generate Bad Request LWM2M response. */ in lwm2m_write_handler_opaque()
1039 return -EEXIST; in lwm2m_write_handler_opaque()
1046 if (res->post_write_cb) { in lwm2m_write_handler_opaque()
1047 ret = res->post_write_cb( in lwm2m_write_handler_opaque()
1048 obj_inst->obj_inst_id, res->res_id, res_inst->res_inst_id, data_ptr, in lwm2m_write_handler_opaque()
1050 (msg->in.block_ctx ? msg->in.block_ctx->ctx.current : 0)); in lwm2m_write_handler_opaque()
1055 if (msg->in.block_ctx && !last_pkt_block) { in lwm2m_write_handler_opaque()
1056 msg->in.block_ctx->ctx.current += len; in lwm2m_write_handler_opaque()
1060 if (msg->in.block_ctx != NULL) { in lwm2m_write_handler_opaque()
1061 msg->in.block_ctx->opaque = opaque_ctx; in lwm2m_write_handler_opaque()
1085 return -EINVAL; in lwm2m_write_handler()
1089 return -EACCES; in lwm2m_write_handler()
1093 data_ptr = res_inst->data_ptr; in lwm2m_write_handler()
1094 data_len = res_inst->max_data_len; in lwm2m_write_handler()
1097 if (res->pre_write_cb) { in lwm2m_write_handler()
1098 data_ptr = res->pre_write_cb(obj_inst->obj_inst_id, res->res_id, in lwm2m_write_handler()
1099 res_inst->res_inst_id, &data_len); in lwm2m_write_handler()
1102 if (msg->in.block_ctx != NULL) { in lwm2m_write_handler()
1104 total_size = msg->in.block_ctx->ctx.total_size; in lwm2m_write_handler()
1105 offset = msg->in.block_ctx->ctx.current; in lwm2m_write_handler()
1107 LOG_DBG("BLOCK1: total:%zu current:%zu" in lwm2m_write_handler()
1109 msg->in.block_ctx->ctx.total_size, msg->in.block_ctx->ctx.current, in lwm2m_write_handler()
1110 msg->in.block_ctx->last_block); in lwm2m_write_handler()
1114 * buffer sizes. The post-write callback handles assembling of the data in lwm2m_write_handler()
1116 if (!res->post_write_cb) { in lwm2m_write_handler()
1118 return -ENOMEM; in lwm2m_write_handler()
1120 data_len -= offset; in lwm2m_write_handler()
1129 if (res->validate_cb) { in lwm2m_write_handler()
1130 write_buf = msg->ctx->validate_buf; in lwm2m_write_handler()
1131 write_buf_len = sizeof(msg->ctx->validate_buf); in lwm2m_write_handler()
1140 switch (obj_field->data_type) { in lwm2m_write_handler()
1149 ret = engine_get_string(&msg->in, write_buf, write_buf_len); in lwm2m_write_handler()
1158 ret = engine_get_time(&msg->in, &temp_time); in lwm2m_write_handler()
1171 ret = -EINVAL; in lwm2m_write_handler()
1177 ret = engine_get_s64(&msg->in, &temp64); in lwm2m_write_handler()
1187 ret = engine_get_s32(&msg->in, &temp32); in lwm2m_write_handler()
1197 ret = engine_get_s32(&msg->in, &temp32); in lwm2m_write_handler()
1207 ret = engine_get_s64(&msg->in, (int64_t *)write_buf); in lwm2m_write_handler()
1212 ret = engine_get_s32(&msg->in, (int32_t *)write_buf); in lwm2m_write_handler()
1217 ret = engine_get_s32(&msg->in, &temp32); in lwm2m_write_handler()
1227 ret = engine_get_s32(&msg->in, &temp32); in lwm2m_write_handler()
1237 ret = engine_get_bool(&msg->in, (bool *)write_buf); in lwm2m_write_handler()
1242 ret = engine_get_float(&msg->in, (double *)write_buf); in lwm2m_write_handler()
1247 ret = engine_get_objlnk(&msg->in, (struct lwm2m_objlnk *)write_buf); in lwm2m_write_handler()
1252 LOG_ERR("unknown obj data_type %d", obj_field->data_type); in lwm2m_write_handler()
1253 return -EINVAL; in lwm2m_write_handler()
1260 return -ENOENT; in lwm2m_write_handler()
1263 if (obj_field->data_type != LWM2M_RES_TYPE_OPAQUE) { in lwm2m_write_handler()
1266 if (res->validate_cb) { in lwm2m_write_handler()
1267 ret = res->validate_cb(obj_inst->obj_inst_id, res->res_id, in lwm2m_write_handler()
1268 res_inst->res_inst_id, write_buf, len, last_block, in lwm2m_write_handler()
1271 /* -EEXIST will generate Bad Request LWM2M response. */ in lwm2m_write_handler()
1272 return -EEXIST; in lwm2m_write_handler()
1278 return -ENOMEM; in lwm2m_write_handler()
1281 if (obj_field->data_type == LWM2M_RES_TYPE_STRING) { in lwm2m_write_handler()
1289 if (res->post_write_cb) { in lwm2m_write_handler()
1290 ret = res->post_write_cb(obj_inst->obj_inst_id, res->res_id, in lwm2m_write_handler()
1291 res_inst->res_inst_id, data_ptr, len, last_block, in lwm2m_write_handler()
1296 res_inst->data_len = len; in lwm2m_write_handler()
1299 lwm2m_notify_observer_path(&msg->path); in lwm2m_write_handler()
1313 ret = engine_put_opaque(&msg->out, &msg->path, (uint8_t *)data_ptr, data_len); in lwm2m_read_resource_data()
1318 data_len -= 1; /* Remove the '\0' */ in lwm2m_read_resource_data()
1320 ret = engine_put_string(&msg->out, &msg->path, (uint8_t *)data_ptr, data_len); in lwm2m_read_resource_data()
1324 ret = engine_put_s64(&msg->out, &msg->path, (int64_t) *(uint32_t *)data_ptr); in lwm2m_read_resource_data()
1328 ret = engine_put_s32(&msg->out, &msg->path, (int32_t) *(uint16_t *)data_ptr); in lwm2m_read_resource_data()
1332 ret = engine_put_s16(&msg->out, &msg->path, (int16_t) *(uint8_t *)data_ptr); in lwm2m_read_resource_data()
1336 ret = engine_put_s64(&msg->out, &msg->path, *(int64_t *)data_ptr); in lwm2m_read_resource_data()
1340 ret = engine_put_s32(&msg->out, &msg->path, *(int32_t *)data_ptr); in lwm2m_read_resource_data()
1344 ret = engine_put_s16(&msg->out, &msg->path, *(int16_t *)data_ptr); in lwm2m_read_resource_data()
1348 ret = engine_put_s8(&msg->out, &msg->path, *(int8_t *)data_ptr); in lwm2m_read_resource_data()
1353 ret = engine_put_time(&msg->out, &msg->path, *(time_t *)data_ptr); in lwm2m_read_resource_data()
1355 ret = engine_put_time(&msg->out, &msg->path, in lwm2m_read_resource_data()
1359 ret = -EINVAL; in lwm2m_read_resource_data()
1365 ret = engine_put_bool(&msg->out, &msg->path, *(bool *)data_ptr); in lwm2m_read_resource_data()
1369 ret = engine_put_float(&msg->out, &msg->path, (double *)data_ptr); in lwm2m_read_resource_data()
1373 ret = engine_put_objlnk(&msg->out, &msg->path, (struct lwm2m_objlnk *)data_ptr); in lwm2m_read_resource_data()
1378 ret = -EINVAL; in lwm2m_read_resource_data()
1395 if (msg->cache_info) { in lwm2m_read_cached_data()
1396 read_info = &msg->cache_info->read_info[msg->cache_info->entry_size]; in lwm2m_read_cached_data()
1398 read_info->cache_data = cached_data; in lwm2m_read_cached_data()
1399 read_info->original_get_base = cached_data->rb.get_base; in lwm2m_read_cached_data()
1400 read_info->original_get_head = cached_data->rb.get_head; in lwm2m_read_cached_data()
1401 read_info->original_get_tail = cached_data->rb.get_tail; in lwm2m_read_cached_data()
1402 msg->cache_info->entry_size++; in lwm2m_read_cached_data()
1403 if (msg->cache_info->entry_limit) { in lwm2m_read_cached_data()
1404 length = MIN(length, msg->cache_info->entry_limit); in lwm2m_read_cached_data()
1413 return -ENOMEM; in lwm2m_read_cached_data()
1416 ret = engine_put_timestamp(&msg->out, buf.t); in lwm2m_read_cached_data()
1424 ret = engine_put_s64(&msg->out, &msg->path, (int64_t)buf.u32); in lwm2m_read_cached_data()
1428 ret = engine_put_s32(&msg->out, &msg->path, (int32_t)buf.u16); in lwm2m_read_cached_data()
1432 ret = engine_put_s16(&msg->out, &msg->path, (int16_t)buf.u8); in lwm2m_read_cached_data()
1436 ret = engine_put_s64(&msg->out, &msg->path, buf.i64); in lwm2m_read_cached_data()
1440 ret = engine_put_s32(&msg->out, &msg->path, buf.i32); in lwm2m_read_cached_data()
1444 ret = engine_put_s16(&msg->out, &msg->path, buf.i16); in lwm2m_read_cached_data()
1448 ret = engine_put_s8(&msg->out, &msg->path, buf.i8); in lwm2m_read_cached_data()
1452 ret = engine_put_bool(&msg->out, &msg->path, buf.b); in lwm2m_read_cached_data()
1456 ret = engine_put_time(&msg->out, &msg->path, buf.time); in lwm2m_read_cached_data()
1460 ret = engine_put_float(&msg->out, &msg->path, &buf.f); in lwm2m_read_cached_data()
1468 return -ENOMEM; in lwm2m_read_cached_data()
1474 return -ENOTSUP; in lwm2m_read_cached_data()
1482 if (cached_data && msg->cache_info && lwm2m_cache_size(cached_data) && in lwm2m_accept_timeseries_read()
1483 msg->out.writer->put_data_timestamp) { in lwm2m_accept_timeseries_read()
1502 return -EINVAL; in lwm2m_read_handler()
1504 temp_path.obj_id = obj_inst->obj->obj_id; in lwm2m_read_handler()
1506 temp_path.obj_inst_id = obj_inst->obj_inst_id; in lwm2m_read_handler()
1507 temp_path.res_id = obj_field->res_id; in lwm2m_read_handler()
1510 loop_max = res->res_inst_count; in lwm2m_read_handler()
1511 if (res->multi_res_inst) { in lwm2m_read_handler()
1514 if (res->res_instances[i].res_inst_id != RES_INSTANCE_NOT_CREATED) { in lwm2m_read_handler()
1521 return -ENOENT; in lwm2m_read_handler()
1524 ret = engine_put_begin_ri(&msg->out, &msg->path); in lwm2m_read_handler()
1529 res_inst_id_tmp = msg->path.res_inst_id; in lwm2m_read_handler()
1533 if (res->res_instances[i].res_inst_id == RES_INSTANCE_NOT_CREATED) { in lwm2m_read_handler()
1538 msg->path.level == LWM2M_PATH_LEVEL_RESOURCE_INST && in lwm2m_read_handler()
1539 msg->path.res_inst_id != res->res_instances[i].res_inst_id) { in lwm2m_read_handler()
1543 if (res->res_inst_count > 1) { in lwm2m_read_handler()
1544 msg->path.res_inst_id = res->res_instances[i].res_inst_id; in lwm2m_read_handler()
1546 if (res->multi_res_inst) { in lwm2m_read_handler()
1547 temp_path.res_inst_id = res->res_instances[i].res_inst_id; in lwm2m_read_handler()
1555 ret = lwm2m_read_cached_data(msg, cached_data, obj_field->data_type); in lwm2m_read_handler()
1558 data_ptr = res->res_instances[i].data_ptr; in lwm2m_read_handler()
1559 data_len = res->res_instances[i].data_len; in lwm2m_read_handler()
1562 if (res->read_cb) { in lwm2m_read_handler()
1564 res->read_cb(obj_inst->obj_inst_id, res->res_id, in lwm2m_read_handler()
1565 res->res_instances[i].res_inst_id, &data_len); in lwm2m_read_handler()
1569 return -ENOENT; in lwm2m_read_handler()
1573 if (obj_field->data_type != LWM2M_RES_TYPE_OPAQUE && in lwm2m_read_handler()
1574 obj_field->data_type != LWM2M_RES_TYPE_STRING) { in lwm2m_read_handler()
1575 return -ENOENT; in lwm2m_read_handler()
1585 obj_field->data_type); in lwm2m_read_handler()
1591 return -ENOMEM; in lwm2m_read_handler()
1595 if (res->multi_res_inst) { in lwm2m_read_handler()
1596 ret = engine_put_end_ri(&msg->out, &msg->path); in lwm2m_read_handler()
1601 msg->path.res_inst_id = res_inst_id_tmp; in lwm2m_read_handler()
1612 return -EINVAL; in lwm2m_delete_handler()
1618 if (msg->path.obj_id == LWM2M_OBJECT_SECURITY_ID || in lwm2m_delete_handler()
1619 msg->path.obj_id == LWM2M_OBJECT_DEVICE_ID) { in lwm2m_delete_handler()
1620 return -EPERM; in lwm2m_delete_handler()
1623 ret = lwm2m_delete_obj_inst(msg->path.obj_id, msg->path.obj_inst_id); in lwm2m_delete_handler()
1628 if (!msg->ctx->bootstrap_mode) { in lwm2m_delete_handler()
1674 LOG_ERR("Unsupported content-format: %u", content_format); in do_read_op()
1675 return -ENOMSG; in do_read_op()
1694 LOG_ERR("Unsupported content-format: %u", content_format); in do_composite_read_op()
1695 return -ENOMSG; in do_composite_read_op()
1708 if (!obj_inst->resources || obj_inst->resource_count == 0U) { in lwm2m_perform_read_object_instance()
1713 msg->path.obj_inst_id = obj_inst->obj_inst_id; in lwm2m_perform_read_object_instance()
1715 ret = engine_put_begin_oi(&msg->out, &msg->path); in lwm2m_perform_read_object_instance()
1720 for (int index = 0; index < obj_inst->resource_count; index++) { in lwm2m_perform_read_object_instance()
1721 if (msg->path.level > LWM2M_PATH_LEVEL_OBJECT_INST && in lwm2m_perform_read_object_instance()
1722 msg->path.res_id != obj_inst->resources[index].res_id) { in lwm2m_perform_read_object_instance()
1726 res = &obj_inst->resources[index]; in lwm2m_perform_read_object_instance()
1727 msg->path.res_id = res->res_id; in lwm2m_perform_read_object_instance()
1728 obj_field = lwm2m_get_engine_obj_field(obj_inst->obj, res->res_id); in lwm2m_perform_read_object_instance()
1730 ret = -ENOENT; in lwm2m_perform_read_object_instance()
1732 ret = -EPERM; in lwm2m_perform_read_object_instance()
1735 ret = engine_put_begin_r(&msg->out, &msg->path); in lwm2m_perform_read_object_instance()
1742 if (ret == -ENOMEM) { in lwm2m_perform_read_object_instance()
1749 if (msg->path.level > LWM2M_PATH_LEVEL_OBJECT_INST && in lwm2m_perform_read_object_instance()
1758 ret = engine_put_end_r(&msg->out, &msg->path); in lwm2m_perform_read_object_instance()
1765 if (ret < 0 && msg->path.level > LWM2M_PATH_LEVEL_OBJECT_INST) { in lwm2m_perform_read_object_instance()
1771 ret = engine_put_end_oi(&msg->out, &msg->path); in lwm2m_perform_read_object_instance()
1776 if (msg->path.level <= LWM2M_PATH_LEVEL_OBJECT) { in lwm2m_perform_read_object_instance()
1778 obj_inst = next_engine_obj_inst(msg->path.obj_id, obj_inst->obj_inst_id); in lwm2m_perform_read_object_instance()
1794 if (msg->path.level >= LWM2M_PATH_LEVEL_OBJECT_INST) { in lwm2m_perform_read_op()
1795 obj_inst = get_engine_obj_inst(msg->path.obj_id, msg->path.obj_inst_id); in lwm2m_perform_read_op()
1798 return -ENOENT; in lwm2m_perform_read_op()
1800 } else if (msg->path.level == LWM2M_PATH_LEVEL_OBJECT) { in lwm2m_perform_read_op()
1804 obj_inst = next_engine_obj_inst(msg->path.obj_id, -1); in lwm2m_perform_read_op()
1807 /* set output content-format */ in lwm2m_perform_read_op()
1808 ret = coap_append_option_int(msg->out.out_cpkt, COAP_OPTION_CONTENT_FORMAT, content_format); in lwm2m_perform_read_op()
1810 LOG_ERR("Error setting response content-format: %d", ret); in lwm2m_perform_read_op()
1814 ret = coap_packet_append_payload_marker(msg->out.out_cpkt); in lwm2m_perform_read_op()
1821 memcpy(&temp_path, &msg->path, sizeof(temp_path)); in lwm2m_perform_read_op()
1823 if (engine_put_begin(&msg->out, &msg->path) < 0) { in lwm2m_perform_read_op()
1824 return -ENOMEM; in lwm2m_perform_read_op()
1832 if (engine_put_end(&msg->out, &msg->path) < 0) { in lwm2m_perform_read_op()
1833 return -ENOMEM; in lwm2m_perform_read_op()
1837 memcpy(&msg->path, &temp_path, sizeof(temp_path)); in lwm2m_perform_read_op()
1839 /* did not read anything even if we should have - on single item */ in lwm2m_perform_read_op()
1841 if (msg->path.level == LWM2M_PATH_LEVEL_RESOURCE) { in lwm2m_perform_read_op()
1842 return -ENOENT; in lwm2m_perform_read_op()
1846 msg->path.level == LWM2M_PATH_LEVEL_RESOURCE_INST) { in lwm2m_perform_read_op()
1847 return -ENOENT; in lwm2m_perform_read_op()
1860 &msg->out, &LWM2M_OBJ(obj_inst->obj->obj_id, obj_inst->obj_inst_id, res->res_id)); in lwm2m_discover_add_res()
1866 if (IS_ENABLED(CONFIG_LWM2M_VERSION_1_1) && msg->path.level == LWM2M_PATH_LEVEL_RESOURCE && in lwm2m_discover_add_res()
1867 res->multi_res_inst) { in lwm2m_discover_add_res()
1868 for (int i = 0; i < res->res_inst_count; i++) { in lwm2m_discover_add_res()
1869 struct lwm2m_engine_res_inst *res_inst = &res->res_instances[i]; in lwm2m_discover_add_res()
1871 if (res_inst->res_inst_id == RES_INSTANCE_NOT_CREATED) { in lwm2m_discover_add_res()
1876 &msg->out, &LWM2M_OBJ(obj_inst->obj->obj_id, obj_inst->obj_inst_id, in lwm2m_discover_add_res()
1877 res->res_id, res_inst->res_inst_id)); in lwm2m_discover_add_res()
1897 if (!is_bootstrap && (msg->path.level == LWM2M_PATH_LEVEL_NONE || in lwm2m_discover_handler()
1898 msg->path.obj_id == LWM2M_OBJECT_SECURITY_ID)) { in lwm2m_discover_handler()
1899 return -EPERM; in lwm2m_discover_handler()
1903 if (is_bootstrap && msg->path.level > LWM2M_PATH_LEVEL_OBJECT) { in lwm2m_discover_handler()
1904 return -EPERM; in lwm2m_discover_handler()
1907 /* set output content-format */ in lwm2m_discover_handler()
1908 ret = coap_append_option_int(msg->out.out_cpkt, COAP_OPTION_CONTENT_FORMAT, in lwm2m_discover_handler()
1911 LOG_ERR("Error setting response content-format: %d", ret); in lwm2m_discover_handler()
1915 ret = coap_packet_append_payload_marker(msg->out.out_cpkt); in lwm2m_discover_handler()
1924 ret = engine_put_begin(&msg->out, &msg->path); in lwm2m_discover_handler()
1931 if (msg->path.level > 0 && msg->path.obj_id != obj->obj_id) { in lwm2m_discover_handler()
1942 (obj->instance_count == 0U || lwm2m_engine_shall_report_obj_version(obj))) || in lwm2m_discover_handler()
1943 (!is_bootstrap && msg->path.level == LWM2M_PATH_LEVEL_OBJECT)) { in lwm2m_discover_handler()
1944 ret = engine_put_corelink(&msg->out, &LWM2M_OBJ(obj->obj_id)); in lwm2m_discover_handler()
1951 if (obj->instance_count == 0U) { in lwm2m_discover_handler()
1957 if (obj_inst->obj->obj_id != obj->obj_id) { in lwm2m_discover_handler()
1962 if (msg->path.level > LWM2M_PATH_LEVEL_OBJECT && in lwm2m_discover_handler()
1963 msg->path.obj_inst_id != obj_inst->obj_inst_id) { in lwm2m_discover_handler()
1970 if (msg->path.level <= LWM2M_PATH_LEVEL_OBJECT_INST) { in lwm2m_discover_handler()
1972 &msg->out, in lwm2m_discover_handler()
1973 &LWM2M_OBJ(obj_inst->obj->obj_id, obj_inst->obj_inst_id)); in lwm2m_discover_handler()
1986 for (int i = 0; i < obj_inst->resource_count; i++) { in lwm2m_discover_handler()
1988 if (msg->path.level == LWM2M_PATH_LEVEL_RESOURCE && in lwm2m_discover_handler()
1989 msg->path.res_id != obj_inst->resources[i].res_id) { in lwm2m_discover_handler()
1994 &obj_inst->resources[i]); in lwm2m_discover_handler()
2004 return reported ? 0 : -ENOENT; in lwm2m_discover_handler()
2011 return do_discover_op_link_format(msg, msg->ctx->bootstrap_mode); in do_discover_op()
2015 return -ENOMSG; in do_discover_op()
2068 r = -ENOMSG; in do_write_op()
2086 payload_start = coap_packet_get_payload(msg->in.in_cpkt, &payload_len); in parse_write_op()
2088 msg->in.offset = payload_start - msg->in.in_cpkt->data; in parse_write_op()
2090 msg->in.offset = msg->in.in_cpkt->offset; in parse_write_op()
2094 block_opt = coap_get_option_int(msg->in.in_cpkt, COAP_OPTION_BLOCK1); in parse_write_op()
2102 return -EFBIG; in parse_write_op()
2111 r = get_block_ctx(&msg->path, &block_ctx); in parse_write_op()
2116 r = init_block_ctx(&msg->path, &block_ctx); in parse_write_op()
2120 block_ctx->ctx.block_size = block_size; in parse_write_op()
2128 msg->in.block_ctx = block_ctx; in parse_write_op()
2130 if (block_num < block_ctx->expected) { in parse_write_op()
2132 block_ctx->expected); in parse_write_op()
2133 (void)coap_header_set_code(msg->out.out_cpkt, COAP_RESPONSE_CODE_CONTINUE); in parse_write_op()
2138 (void)coap_append_option_int(msg->out.out_cpkt, COAP_OPTION_BLOCK1, in parse_write_op()
2142 if (block_num > block_ctx->expected) { in parse_write_op()
2144 block_ctx->expected); in parse_write_op()
2145 r = -EFAULT; in parse_write_op()
2148 r = coap_update_from_block(msg->in.in_cpkt, &block_ctx->ctx); in parse_write_op()
2154 block_ctx->last_block = last_block; in parse_write_op()
2155 block_ctx->expected++; in parse_write_op()
2164 * As RFC7959 Section-2.3, More flag is off, because we have already in parse_write_op()
2167 r = coap_append_block1_option(msg->out.out_cpkt, &block_ctx->ctx); in parse_write_op()
2171 r = -EINVAL; in parse_write_op()
2174 r = coap_header_set_code(msg->out.out_cpkt, in parse_write_op()
2178 r = -EINVAL; in parse_write_op()
2197 payload_start = coap_packet_get_payload(msg->in.in_cpkt, &payload_len); in do_composite_write_op()
2199 msg->in.offset = payload_start - msg->in.in_cpkt->data; in do_composite_write_op()
2201 msg->in.offset = msg->in.in_cpkt->offset; in do_composite_write_op()
2204 if (coap_get_option_int(msg->in.in_cpkt, COAP_OPTION_BLOCK1) >= 0) { in do_composite_write_op()
2205 return -ENOTSUP; in do_composite_write_op()
2221 return -ENOMSG; in do_composite_write_op()
2265 return -ENOTSUP; in lwm2m_engine_default_content_format()
2272 return -ENOTSUP; in lwm2m_engine_default_content_format()
2287 return -EINVAL; in lwm2m_exec_handler()
2290 ret = path_to_objs(&msg->path, &obj_inst, NULL, &res, NULL); in lwm2m_exec_handler()
2295 args = (uint8_t *)coap_packet_get_payload(msg->in.in_cpkt, &args_len); in lwm2m_exec_handler()
2297 if (res->execute_cb) { in lwm2m_exec_handler()
2298 return res->execute_cb(obj_inst->obj_inst_id, args, args_len); in lwm2m_exec_handler()
2302 return -ENOENT; in lwm2m_exec_handler()
2314 int observe = -1; /* default to -1, 0 = ENABLE, 1 = DISABLE */ in handle_request()
2317 msg->in.in_cpkt = request; in handle_request()
2318 msg->out.out_cpkt = &msg->cpkt; in handle_request()
2321 msg->in.reader = &plain_text_reader; in handle_request()
2322 msg->out.writer = &plain_text_writer; in handle_request()
2324 code = coap_header_get_code(msg->in.in_cpkt); in handle_request()
2327 tkl = coap_header_get_token(msg->in.in_cpkt, token); in handle_request()
2329 msg->tkl = tkl; in handle_request()
2330 msg->token = token; in handle_request()
2337 } else if (r != -ENOENT) { in handle_request()
2343 r = coap_find_options(msg->in.in_cpkt, COAP_OPTION_URI_PATH, options, ARRAY_SIZE(options)); in handle_request()
2348 /* Treat empty URI path option as is there were no option - this will be in handle_request()
2355 if (r == 0 && lwm2m_engine_path_included(code, msg->ctx->bootstrap_mode)) { in handle_request()
2356 /* No URI path or empty URI path option - allowed only during in handle_request()
2360 r = -EPERM; in handle_request()
2365 /* check for bootstrap-finish */ in handle_request()
2370 msg->code = COAP_RESPONSE_CODE_CHANGED; in handle_request()
2381 r = coap_options_to_path(options, r, &msg->path); in handle_request()
2383 r = -ENOENT; in handle_request()
2388 r = coap_find_options(msg->in.in_cpkt, COAP_OPTION_CONTENT_FORMAT, options, 1); in handle_request()
2391 r = select_reader(&msg->in, format); in handle_request()
2398 r = coap_find_options(msg->in.in_cpkt, COAP_OPTION_ACCEPT, options, 1); in handle_request()
2409 r = select_writer(&msg->out, accept); in handle_request()
2415 if (lwm2m_engine_path_included(code, msg->ctx->bootstrap_mode)) { in handle_request()
2416 if (!(msg->ctx->bootstrap_mode && msg->path.level == LWM2M_PATH_LEVEL_NONE)) { in handle_request()
2418 obj = get_engine_obj(msg->path.obj_id); in handle_request()
2420 /* No matching object found - ignore request */ in handle_request()
2421 r = -ENOENT; in handle_request()
2432 * LwM2M V1_0_1-20170704-A, table 25, in handle_request()
2436 msg->operation = LWM2M_OP_DISCOVER; in handle_request()
2439 msg->operation = LWM2M_OP_READ; in handle_request()
2443 observe = coap_get_option_int(msg->in.in_cpkt, COAP_OPTION_OBSERVE); in handle_request()
2444 msg->code = COAP_RESPONSE_CODE_CONTENT; in handle_request()
2448 msg->operation = LWM2M_OP_READ; in handle_request()
2450 observe = coap_get_option_int(msg->in.in_cpkt, COAP_OPTION_OBSERVE); in handle_request()
2451 msg->code = COAP_RESPONSE_CODE_CONTENT; in handle_request()
2455 msg->operation = LWM2M_OP_WRITE; in handle_request()
2456 msg->code = COAP_RESPONSE_CODE_CHANGED; in handle_request()
2460 if (msg->path.level == 1U) { in handle_request()
2462 msg->operation = LWM2M_OP_CREATE; in handle_request()
2463 msg->code = COAP_RESPONSE_CODE_CREATED; in handle_request()
2464 } else if (msg->path.level == 2U) { in handle_request()
2466 msg->operation = LWM2M_OP_WRITE; in handle_request()
2467 msg->code = COAP_RESPONSE_CODE_CHANGED; in handle_request()
2469 msg->operation = LWM2M_OP_EXECUTE; in handle_request()
2470 msg->code = COAP_RESPONSE_CODE_CHANGED; in handle_request()
2476 /* write attributes if content-format is absent */ in handle_request()
2478 msg->operation = LWM2M_OP_WRITE_ATTR; in handle_request()
2480 msg->operation = LWM2M_OP_WRITE; in handle_request()
2483 msg->code = COAP_RESPONSE_CODE_CHANGED; in handle_request()
2487 msg->operation = LWM2M_OP_DELETE; in handle_request()
2488 msg->code = COAP_RESPONSE_CODE_DELETED; in handle_request()
2502 r = access_control_check_access(msg->path.obj_id, msg->path.obj_inst_id, in handle_request()
2503 msg->ctx->srv_obj_inst, msg->operation, in handle_request()
2504 msg->ctx->bootstrap_mode); in handle_request()
2506 LOG_ERR("Access denied - Server obj %u does not have proper access to " in handle_request()
2508 msg->ctx->srv_obj_inst); in handle_request()
2512 if (msg->path.level > LWM2M_PATH_LEVEL_NONE && in handle_request()
2513 msg->path.obj_id == LWM2M_OBJECT_SECURITY_ID && !msg->ctx->bootstrap_mode) { in handle_request()
2514 r = -EACCES; in handle_request()
2518 switch (msg->operation) { in handle_request()
2523 if (!msg->token) { in handle_request()
2525 r = -EINVAL; in handle_request()
2569 if (msg->operation == LWM2M_OP_CREATE && r >= 0) { in handle_request()
2570 access_control_add(msg->path.obj_id, msg->path.obj_inst_id, in handle_request()
2571 msg->ctx->srv_obj_inst); in handle_request()
2586 if (msg->ctx->bootstrap_mode) { in handle_request()
2595 LOG_ERR("Unknown operation: %u", msg->operation); in handle_request()
2596 r = -EINVAL; in handle_request()
2607 if (r == -ENOENT) { in handle_request()
2608 msg->code = COAP_RESPONSE_CODE_NOT_FOUND; in handle_request()
2609 } else if (r == -EPERM) { in handle_request()
2610 msg->code = COAP_RESPONSE_CODE_NOT_ALLOWED; in handle_request()
2611 } else if (r == -EEXIST) { in handle_request()
2612 msg->code = COAP_RESPONSE_CODE_BAD_REQUEST; in handle_request()
2613 } else if (r == -EFAULT) { in handle_request()
2614 msg->code = COAP_RESPONSE_CODE_INCOMPLETE; in handle_request()
2615 } else if (r == -EFBIG) { in handle_request()
2616 msg->code = COAP_RESPONSE_CODE_REQUEST_TOO_LARGE; in handle_request()
2617 } else if (r == -ENOTSUP) { in handle_request()
2618 msg->code = COAP_RESPONSE_CODE_NOT_IMPLEMENTED; in handle_request()
2619 } else if (r == -ENOMSG) { in handle_request()
2620 msg->code = COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT; in handle_request()
2621 } else if (r == -EACCES) { in handle_request()
2622 msg->code = COAP_RESPONSE_CODE_UNAUTHORIZED; in handle_request()
2623 } else if (r == -ECANCELED) { in handle_request()
2624 msg->code = COAP_RESPONSE_CODE_NOT_ACCEPTABLE; in handle_request()
2627 msg->code = COAP_RESPONSE_CODE_INTERNAL_ERROR; in handle_request()
2642 msg->type = COAP_TYPE_CON; in lwm2m_response_promote_to_con()
2643 msg->mid = coap_next_id(); in lwm2m_response_promote_to_con()
2647 * - CoAP message type (byte 0, bits 2 and 3) in lwm2m_response_promote_to_con()
2648 * - CoAP message id (bytes 2 and 3) in lwm2m_response_promote_to_con()
2650 msg->cpkt.data[0] &= ~(0x3 << 4); in lwm2m_response_promote_to_con()
2651 msg->cpkt.data[0] |= (msg->type & 0x3) << 4; in lwm2m_response_promote_to_con()
2652 msg->cpkt.data[2] = msg->mid >> 8; in lwm2m_response_promote_to_con()
2653 msg->cpkt.data[3] = (uint8_t)msg->mid; in lwm2m_response_promote_to_con()
2655 if (msg->pending) { in lwm2m_response_promote_to_con()
2656 coap_pending_clear(msg->pending); in lwm2m_response_promote_to_con()
2659 lwm2m_client_lock(msg->ctx); in lwm2m_response_promote_to_con()
2662 msg->pending = coap_pending_next_unused(msg->ctx->pendings, ARRAY_SIZE(msg->ctx->pendings)); in lwm2m_response_promote_to_con()
2663 if (!msg->pending) { in lwm2m_response_promote_to_con()
2666 lwm2m_client_unlock(msg->ctx); in lwm2m_response_promote_to_con()
2667 return -ENOMEM; in lwm2m_response_promote_to_con()
2670 ret = coap_pending_init(msg->pending, &msg->cpkt, &msg->ctx->remote_addr, NULL); in lwm2m_response_promote_to_con()
2677 lwm2m_client_unlock(msg->ctx); in lwm2m_response_promote_to_con()
2684 /* TODO: I could try to check if there is Request-Tags attached, and then match queries in find_ongoing_block2_tx()
2717 msg->in.in_cpkt = cpkt; in handle_ongoing_block2_tx()
2760 pending = coap_pending_received(&response, client_ctx->pendings, in lwm2m_udp_receive()
2761 ARRAY_SIZE(client_ctx->pendings)); in lwm2m_udp_receive()
2770 msg->acknowledged = true; in lwm2m_udp_receive()
2772 if (msg->reply == NULL) { in lwm2m_udp_receive()
2781 if ((msg->code >= COAP_METHOD_GET) && (msg->code <= COAP_METHOD_DELETE) && in lwm2m_udp_receive()
2788 reply = coap_response_received(&response, from_addr, client_ctx->replies, in lwm2m_udp_receive()
2789 ARRAY_SIZE(client_ctx->replies)); in lwm2m_udp_receive()
2821 last_block_num = msg->out.block_ctx->current / in lwm2m_udp_receive()
2851 /* skip release if reply->user_data has error condition */ in lwm2m_udp_receive()
2852 if (reply && reply->user_data == (void *)COAP_REPLY_STATUS_ERROR) { in lwm2m_udp_receive()
2853 /* reset reply->user_data for next time */ in lwm2m_udp_receive()
2854 reply->user_data = (void *)COAP_REPLY_STATUS_NONE; in lwm2m_udp_receive()
2889 msg->type = COAP_TYPE_ACK; in lwm2m_udp_receive()
2890 msg->code = coap_header_get_code(&response); in lwm2m_udp_receive()
2891 msg->mid = coap_header_get_id(&response); in lwm2m_udp_receive()
2893 msg->tkl = 0; in lwm2m_udp_receive()
2895 client_ctx->processed_req = msg; in lwm2m_udp_receive()
2905 if (msg->acknowledged) { in lwm2m_udp_receive()
2914 client_ctx->processed_req = NULL; in lwm2m_udp_receive()
2928 if (msg->ctx != NULL) { in notify_message_timeout_cb()
2930 struct lwm2m_ctx *client_ctx = msg->ctx; in notify_message_timeout_cb()
2933 obs = engine_observe_node_discover(&client_ctx->observer, &prev_node, NULL, in notify_message_timeout_cb()
2934 msg->token, msg->tkl); in notify_message_timeout_cb()
2937 obs->active_notify = NULL; in notify_message_timeout_cb()
2938 if (client_ctx->observe_cb) { in notify_message_timeout_cb()
2939 client_ctx->observe_cb(LWM2M_OBSERVE_EVENT_NOTIFY_TIMEOUT, in notify_message_timeout_cb()
2940 &msg->path, msg->reply->user_data); in notify_message_timeout_cb()
2955 return &entry->path; in lwm2m_read_first_path_ptr()
2964 SYS_SLIST_FOR_EACH_CONTAINER(&obs->path_list, entry, node) { in notify_cached_pending_data_trig()
2965 cached_data = lwm2m_cache_entry_get_by_object(&entry->path); in notify_cached_pending_data_trig()
2971 lwm2m_notify_observer_path(&entry->path); in notify_cached_pending_data_trig()
2990 sprint_token(reply->token, reply->tkl)); in notify_message_reply_cb()
2996 if (reply->tkl > 0) { in notify_message_reply_cb()
2997 ret = engine_remove_observer_by_token(msg->ctx, reply->token, reply->tkl); in notify_message_reply_cb()
3002 LOG_ERR("notify reply missing token -- ignored."); in notify_message_reply_cb()
3005 obs = engine_observe_node_discover(&msg->ctx->observer, &prev_node, NULL, in notify_message_reply_cb()
3006 reply->token, reply->tkl); in notify_message_reply_cb()
3009 obs->active_notify = NULL; in notify_message_reply_cb()
3010 if (msg->ctx->observe_cb) { in notify_message_reply_cb()
3011 msg->ctx->observe_cb(LWM2M_OBSERVE_EVENT_NOTIFY_ACK, in notify_message_reply_cb()
3012 lwm2m_read_first_path_ptr(&obs->path_list), in notify_message_reply_cb()
3013 reply->user_data); in notify_message_reply_cb()
3037 LOG_ERR("Unsupported content-format for /dp: %u", content_format); in do_send_op()
3038 return -ENOMSG; in do_send_op()
3047 if (error_code != -ENOMEM) { in lwm2m_timeseries_data_rebuild()
3051 cache_temp = msg->cache_info; in lwm2m_timeseries_data_rebuild()
3053 if (!cache_temp || !cache_temp->entry_size) { in lwm2m_timeseries_data_rebuild()
3058 for (int i = 0; i < cache_temp->entry_size; i++) { in lwm2m_timeseries_data_rebuild()
3059 cache_temp->read_info[i].cache_data->rb.get_head = in lwm2m_timeseries_data_rebuild()
3060 cache_temp->read_info[i].original_get_head; in lwm2m_timeseries_data_rebuild()
3061 cache_temp->read_info[i].cache_data->rb.get_tail = in lwm2m_timeseries_data_rebuild()
3062 cache_temp->read_info[i].original_get_tail; in lwm2m_timeseries_data_rebuild()
3063 cache_temp->read_info[i].cache_data->rb.get_base = in lwm2m_timeseries_data_rebuild()
3064 cache_temp->read_info[i].original_get_base; in lwm2m_timeseries_data_rebuild()
3067 if (cache_temp->entry_limit) { in lwm2m_timeseries_data_rebuild()
3072 /* Limit re-build entry count */ in lwm2m_timeseries_data_rebuild()
3073 cache_temp->entry_limit = LWM2M_LIMITED_TIMESERIES_RESOURCE_COUNT / cache_temp->entry_size; in lwm2m_timeseries_data_rebuild()
3074 cache_temp->entry_size = 0; in lwm2m_timeseries_data_rebuild()
3077 LOG_INF("Try re-buildbuild again with limited cache size %d", cache_temp->entry_limit); in lwm2m_timeseries_data_rebuild()
3100 return -ENOMEM; in generate_notify_message()
3104 if (!obs->composite) { in generate_notify_message()
3105 path = lwm2m_read_first_path_ptr(&obs->path_list); in generate_notify_message()
3108 ret = -EINVAL; in generate_notify_message()
3112 memcpy(&msg->path, path, sizeof(struct lwm2m_obj_path)); in generate_notify_message()
3114 obs->resource_update ? "MANUAL" : "AUTO", path->obj_id, path->obj_inst_id, in generate_notify_message()
3115 path->res_id, path->level, sprint_token(obs->token, obs->tkl), in generate_notify_message()
3116 lwm2m_sprint_ip_addr(&ctx->remote_addr), (long long)k_uptime_get()); in generate_notify_message()
3118 obj_inst = get_engine_obj_inst(path->obj_id, path->obj_inst_id); in generate_notify_message()
3120 LOG_ERR("unable to get engine obj for %u/%u", path->obj_id, in generate_notify_message()
3121 path->obj_inst_id); in generate_notify_message()
3122 ret = -EINVAL; in generate_notify_message()
3127 obs->resource_update ? "MANUAL" : "AUTO", in generate_notify_message()
3128 sprint_token(obs->token, obs->tkl), lwm2m_sprint_ip_addr(&ctx->remote_addr), in generate_notify_message()
3132 msg->operation = LWM2M_OP_READ; in generate_notify_message()
3133 msg->type = COAP_TYPE_CON; in generate_notify_message()
3134 msg->code = COAP_RESPONSE_CODE_CONTENT; in generate_notify_message()
3135 msg->mid = coap_next_id(); in generate_notify_message()
3136 msg->token = obs->token; in generate_notify_message()
3137 msg->tkl = obs->tkl; in generate_notify_message()
3138 msg->reply_cb = notify_message_reply_cb; in generate_notify_message()
3139 msg->message_timeout_cb = notify_message_timeout_cb; in generate_notify_message()
3140 msg->out.out_cpkt = &msg->cpkt; in generate_notify_message()
3148 msg->cache_info = &cache_temp_info; in generate_notify_message()
3152 msg->reply->user_data = user_data; in generate_notify_message()
3155 obs->counter++; in generate_notify_message()
3156 ret = coap_append_option_int(&msg->cpkt, COAP_OPTION_OBSERVE, obs->counter); in generate_notify_message()
3163 select_writer(&msg->out, obs->format); in generate_notify_message()
3164 if (obs->composite) { in generate_notify_message()
3166 ret = do_send_op(msg, obs->format, &obs->path_list); in generate_notify_message()
3168 ret = do_read_op(msg, obs->format); in generate_notify_message()
3174 * Try rebuild message again by limiting timeseries data entry lengths. in generate_notify_message()
3178 LOG_ERR("error in multi-format read (err:%d)", ret); in generate_notify_message()
3182 obs->active_notify = msg; in generate_notify_message()
3183 obs->resource_update = false; in generate_notify_message()
3186 msg->cache_info = NULL; in generate_notify_message()
3206 if (obj->obj_id == LWM2M_OBJECT_SECURITY_ID) { in lwm2m_perform_composite_read_root()
3210 msg->path.level = 1; in lwm2m_perform_composite_read_root()
3211 msg->path.obj_id = obj->obj_id; in lwm2m_perform_composite_read_root()
3213 obj_inst = next_engine_obj_inst(msg->path.obj_id, -1); in lwm2m_perform_composite_read_root()
3220 if (ret == -ENOMEM) { in lwm2m_perform_composite_read_root()
3235 /* set output content-format */ in lwm2m_perform_composite_read_op()
3236 ret = coap_append_option_int(msg->out.out_cpkt, COAP_OPTION_CONTENT_FORMAT, content_format); in lwm2m_perform_composite_read_op()
3238 LOG_ERR("Error setting response content-format: %d", ret); in lwm2m_perform_composite_read_op()
3242 ret = coap_packet_append_payload_marker(msg->out.out_cpkt); in lwm2m_perform_composite_read_op()
3249 engine_put_begin(&msg->out, &msg->path); in lwm2m_perform_composite_read_op()
3254 memcpy(&msg->path, &entry->path, sizeof(struct lwm2m_obj_path)); in lwm2m_perform_composite_read_op()
3256 if (msg->path.level >= LWM2M_PATH_LEVEL_OBJECT_INST) { in lwm2m_perform_composite_read_op()
3257 obj_inst = get_engine_obj_inst(msg->path.obj_id, msg->path.obj_inst_id); in lwm2m_perform_composite_read_op()
3258 } else if (msg->path.level == LWM2M_PATH_LEVEL_OBJECT) { in lwm2m_perform_composite_read_op()
3260 obj_inst = next_engine_obj_inst(msg->path.obj_id, -1); in lwm2m_perform_composite_read_op()
3264 if (ret == -ENOMEM) { in lwm2m_perform_composite_read_op()
3276 if (ret == -ENOMEM) { in lwm2m_perform_composite_read_op()
3280 /* did not read anything even if we should have - on single item */ in lwm2m_perform_composite_read_op()
3282 return -ENOENT; in lwm2m_perform_composite_read_op()
3286 if (engine_put_end(&msg->out, &msg->path) < 0) { in lwm2m_perform_composite_read_op()
3287 return -ENOMEM; in lwm2m_perform_composite_read_op()
3309 return -ENOTSUP; in lwm2m_parse_peerinfo()
3317 return -EPROTONOSUPPORT; in lwm2m_parse_peerinfo()
3321 client_ctx->use_dtls = false; in lwm2m_parse_peerinfo()
3324 client_ctx->use_dtls = true; in lwm2m_parse_peerinfo()
3326 return -EPROTONOSUPPORT; in lwm2m_parse_peerinfo()
3331 if (is_firmware_uri && client_ctx->use_dtls) { in lwm2m_parse_peerinfo()
3351 (void)memset(&client_ctx->remote_addr, 0, sizeof(client_ctx->remote_addr)); in lwm2m_parse_peerinfo()
3354 client_ctx->remote_addr.sa_family = AF_INET6; in lwm2m_parse_peerinfo()
3356 &((struct sockaddr_in6 *)&client_ctx->remote_addr)->sin6_addr); in lwm2m_parse_peerinfo()
3359 client_ctx->remote_addr.sa_family = AF_INET; in lwm2m_parse_peerinfo()
3361 &((struct sockaddr_in *)&client_ctx->remote_addr)->sin_addr); in lwm2m_parse_peerinfo()
3380 ret = -ENOENT; in lwm2m_parse_peerinfo()
3384 memcpy(&client_ctx->remote_addr, res->ai_addr, sizeof(client_ctx->remote_addr)); in lwm2m_parse_peerinfo()
3385 client_ctx->remote_addr.sa_family = res->ai_family; in lwm2m_parse_peerinfo()
3389 client_ctx->desthostname = url + off; in lwm2m_parse_peerinfo()
3390 client_ctx->desthostnamelen = len; in lwm2m_parse_peerinfo()
3399 if (client_ctx->remote_addr.sa_family == AF_INET6) { in lwm2m_parse_peerinfo()
3400 net_sin6(&client_ctx->remote_addr)->sin6_port = htons(parser.port); in lwm2m_parse_peerinfo()
3401 } else if (client_ctx->remote_addr.sa_family == AF_INET) { in lwm2m_parse_peerinfo()
3402 net_sin(&client_ctx->remote_addr)->sin_port = htons(parser.port); in lwm2m_parse_peerinfo()
3404 ret = -EPROTONOSUPPORT; in lwm2m_parse_peerinfo()
3420 if (entry->path.level > LWM2M_PATH_LEVEL_NONE && in do_composite_read_op_for_parsed_list()
3421 entry->path.obj_id == LWM2M_OBJECT_SECURITY_ID && !msg->ctx->bootstrap_mode) { in do_composite_read_op_for_parsed_list()
3422 return -EACCES; in do_composite_read_op_for_parsed_list()
3439 LOG_ERR("Unsupported content-format: %u", content_format); in do_composite_read_op_for_parsed_list()
3440 return -ENOMSG; in do_composite_read_op_for_parsed_list()
3449 struct lwm2m_message *msg = (struct lwm2m_message *)reply->user_data; in do_send_reply_cb()
3457 if (msg && msg->send_status_cb) { in do_send_reply_cb()
3458 msg->send_status_cb(LWM2M_SEND_STATUS_SUCCESS); in do_send_reply_cb()
3466 if (msg && msg->send_status_cb) { in do_send_reply_cb()
3467 msg->send_status_cb(LWM2M_SEND_STATUS_FAILURE); in do_send_reply_cb()
3475 if (msg->send_status_cb) { in do_send_timeout_cb()
3476 msg->send_status_cb(LWM2M_SEND_STATUS_TIMEOUT); in do_send_timeout_cb()
3479 lwm2m_rd_client_timeout(msg->ctx); in do_send_timeout_cb()
3491 for (int i = 0; i < cache_temp->entry_size; i++) { in init_next_pending_timeseries_data()
3492 if (ring_buf_is_empty(&cache_temp->read_info[i].cache_data->rb)) { in init_next_pending_timeseries_data()
3499 &cache_temp->read_info[i].cache_data->path)) { in init_next_pending_timeseries_data()
3503 bytes_available += ring_buf_size_get(&cache_temp->read_info[i].cache_data->rb); in init_next_pending_timeseries_data()
3511 cache_temp->entry_size = 0; in init_next_pending_timeseries_data()
3512 cache_temp->entry_limit = 0; in init_next_pending_timeseries_data()
3538 return -EPERM; in lwm2m_send_cb()
3541 if (lwm2m_server_get_mute_send(ctx->srv_obj_inst)) { in lwm2m_send_cb()
3543 return -EPERM; in lwm2m_send_cb()
3551 return -E2BIG; in lwm2m_send_cb()
3560 return -ENOTSUP; in lwm2m_send_cb()
3568 return -1; in lwm2m_send_cb()
3582 return -ENOMEM; in lwm2m_send_cb()
3587 msg->type = COAP_TYPE_CON; in lwm2m_send_cb()
3588 msg->reply_cb = do_send_reply_cb; in lwm2m_send_cb()
3589 msg->message_timeout_cb = do_send_timeout_cb; in lwm2m_send_cb()
3590 msg->code = COAP_METHOD_POST; in lwm2m_send_cb()
3591 msg->mid = coap_next_id(); in lwm2m_send_cb()
3592 msg->tkl = LWM2M_MSG_TOKEN_GENERATE_NEW; in lwm2m_send_cb()
3593 msg->out.out_cpkt = &msg->cpkt; in lwm2m_send_cb()
3600 msg->cache_info = &cache_temp_info; in lwm2m_send_cb()
3605 msg->reply->user_data = msg; in lwm2m_send_cb()
3606 msg->send_status_cb = reply_cb; in lwm2m_send_cb()
3609 ret = select_writer(&msg->out, content_format); in lwm2m_send_cb()
3614 ret = coap_packet_append_option(&msg->cpkt, COAP_OPTION_URI_PATH, LWM2M_DP_CLIENT_URI, in lwm2m_send_cb()
3625 * Try rebuild message again by limiting timeseries data entry lengths. in lwm2m_send_cb()
3634 msg->cache_info = NULL; in lwm2m_send_cb()
3660 return -ENOTSUP; in lwm2m_send_cb()