Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mresourceinstance.cpp@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | /* |
leothedragon | 0:8f0bb79ddd48 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:8f0bb79ddd48 | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:8f0bb79ddd48 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:8f0bb79ddd48 | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 6 | * You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 7 | * |
leothedragon | 0:8f0bb79ddd48 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 9 | * |
leothedragon | 0:8f0bb79ddd48 | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:8f0bb79ddd48 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 14 | * limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 15 | */ |
leothedragon | 0:8f0bb79ddd48 | 16 | #include <stdlib.h> |
leothedragon | 0:8f0bb79ddd48 | 17 | #include "mbed-client/m2mresource.h" |
leothedragon | 0:8f0bb79ddd48 | 18 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:8f0bb79ddd48 | 19 | #include "mbed-client/m2mobservationhandler.h" |
leothedragon | 0:8f0bb79ddd48 | 20 | #include "mbed-client/m2mobject.h" |
leothedragon | 0:8f0bb79ddd48 | 21 | #include "mbed-client/m2mobjectinstance.h" |
leothedragon | 0:8f0bb79ddd48 | 22 | #include "include/m2mcallbackstorage.h" |
leothedragon | 0:8f0bb79ddd48 | 23 | #include "include/m2mreporthandler.h" |
leothedragon | 0:8f0bb79ddd48 | 24 | #include "mbed-client/m2mblockmessage.h" |
leothedragon | 0:8f0bb79ddd48 | 25 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:8f0bb79ddd48 | 26 | |
leothedragon | 0:8f0bb79ddd48 | 27 | #define TRACE_GROUP "mClt" |
leothedragon | 0:8f0bb79ddd48 | 28 | |
leothedragon | 0:8f0bb79ddd48 | 29 | M2MResourceInstance::M2MResourceInstance(M2MResource &parent, |
leothedragon | 0:8f0bb79ddd48 | 30 | const String &res_name, |
leothedragon | 0:8f0bb79ddd48 | 31 | M2MBase::Mode resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 32 | const String &resource_type, |
leothedragon | 0:8f0bb79ddd48 | 33 | M2MBase::DataType type, |
leothedragon | 0:8f0bb79ddd48 | 34 | char* path, |
leothedragon | 0:8f0bb79ddd48 | 35 | bool external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 36 | bool multiple_instance) |
leothedragon | 0:8f0bb79ddd48 | 37 | : M2MResourceBase(res_name, |
leothedragon | 0:8f0bb79ddd48 | 38 | resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 39 | resource_type, |
leothedragon | 0:8f0bb79ddd48 | 40 | type, |
leothedragon | 0:8f0bb79ddd48 | 41 | path, |
leothedragon | 0:8f0bb79ddd48 | 42 | external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 43 | multiple_instance |
leothedragon | 0:8f0bb79ddd48 | 44 | ), |
leothedragon | 0:8f0bb79ddd48 | 45 | _parent_resource(parent) |
leothedragon | 0:8f0bb79ddd48 | 46 | { |
leothedragon | 0:8f0bb79ddd48 | 47 | set_base_type(M2MBase::ResourceInstance); |
leothedragon | 0:8f0bb79ddd48 | 48 | } |
leothedragon | 0:8f0bb79ddd48 | 49 | |
leothedragon | 0:8f0bb79ddd48 | 50 | M2MResourceInstance::M2MResourceInstance(M2MResource &parent, |
leothedragon | 0:8f0bb79ddd48 | 51 | const String &res_name, |
leothedragon | 0:8f0bb79ddd48 | 52 | M2MBase::Mode resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 53 | const String &resource_type, |
leothedragon | 0:8f0bb79ddd48 | 54 | M2MBase::DataType type, |
leothedragon | 0:8f0bb79ddd48 | 55 | const uint8_t *value, |
leothedragon | 0:8f0bb79ddd48 | 56 | const uint8_t value_length, |
leothedragon | 0:8f0bb79ddd48 | 57 | char* path, |
leothedragon | 0:8f0bb79ddd48 | 58 | bool external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 59 | bool multiple_instance) |
leothedragon | 0:8f0bb79ddd48 | 60 | : M2MResourceBase(res_name, |
leothedragon | 0:8f0bb79ddd48 | 61 | resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 62 | resource_type, |
leothedragon | 0:8f0bb79ddd48 | 63 | type, |
leothedragon | 0:8f0bb79ddd48 | 64 | value, |
leothedragon | 0:8f0bb79ddd48 | 65 | value_length, |
leothedragon | 0:8f0bb79ddd48 | 66 | path, |
leothedragon | 0:8f0bb79ddd48 | 67 | external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 68 | multiple_instance), |
leothedragon | 0:8f0bb79ddd48 | 69 | _parent_resource(parent) |
leothedragon | 0:8f0bb79ddd48 | 70 | { |
leothedragon | 0:8f0bb79ddd48 | 71 | set_base_type(M2MBase::ResourceInstance); |
leothedragon | 0:8f0bb79ddd48 | 72 | } |
leothedragon | 0:8f0bb79ddd48 | 73 | |
leothedragon | 0:8f0bb79ddd48 | 74 | M2MResourceInstance::M2MResourceInstance(M2MResource &parent, |
leothedragon | 0:8f0bb79ddd48 | 75 | const lwm2m_parameters_s* s, |
leothedragon | 0:8f0bb79ddd48 | 76 | M2MBase::DataType type) |
leothedragon | 0:8f0bb79ddd48 | 77 | : M2MResourceBase(s, type), |
leothedragon | 0:8f0bb79ddd48 | 78 | _parent_resource(parent) |
leothedragon | 0:8f0bb79ddd48 | 79 | { |
leothedragon | 0:8f0bb79ddd48 | 80 | |
leothedragon | 0:8f0bb79ddd48 | 81 | assert(base_type() == M2MBase::ResourceInstance); |
leothedragon | 0:8f0bb79ddd48 | 82 | } |
leothedragon | 0:8f0bb79ddd48 | 83 | |
leothedragon | 0:8f0bb79ddd48 | 84 | M2MResourceInstance::~M2MResourceInstance() |
leothedragon | 0:8f0bb79ddd48 | 85 | { |
leothedragon | 0:8f0bb79ddd48 | 86 | free_resources(); |
leothedragon | 0:8f0bb79ddd48 | 87 | } |
leothedragon | 0:8f0bb79ddd48 | 88 | |
leothedragon | 0:8f0bb79ddd48 | 89 | M2MObservationHandler* M2MResourceInstance::observation_handler() const |
leothedragon | 0:8f0bb79ddd48 | 90 | { |
leothedragon | 0:8f0bb79ddd48 | 91 | const M2MResource& parent_resource = get_parent_resource(); |
leothedragon | 0:8f0bb79ddd48 | 92 | |
leothedragon | 0:8f0bb79ddd48 | 93 | // XXX: need to check the flag too |
leothedragon | 0:8f0bb79ddd48 | 94 | return parent_resource.observation_handler(); |
leothedragon | 0:8f0bb79ddd48 | 95 | } |
leothedragon | 0:8f0bb79ddd48 | 96 | |
leothedragon | 0:8f0bb79ddd48 | 97 | void M2MResourceInstance::set_observation_handler(M2MObservationHandler *handler) |
leothedragon | 0:8f0bb79ddd48 | 98 | { |
leothedragon | 0:8f0bb79ddd48 | 99 | M2MResource& parent_resource = get_parent_resource(); |
leothedragon | 0:8f0bb79ddd48 | 100 | |
leothedragon | 0:8f0bb79ddd48 | 101 | // XXX: need to set the flag too |
leothedragon | 0:8f0bb79ddd48 | 102 | parent_resource.set_observation_handler(handler); |
leothedragon | 0:8f0bb79ddd48 | 103 | } |
leothedragon | 0:8f0bb79ddd48 | 104 | |
leothedragon | 0:8f0bb79ddd48 | 105 | bool M2MResourceInstance::handle_observation_attribute(const char *query) |
leothedragon | 0:8f0bb79ddd48 | 106 | { |
leothedragon | 0:8f0bb79ddd48 | 107 | tr_debug("M2MResourceInstance::handle_observation_attribute - is_under_observation(%d)", is_under_observation()); |
leothedragon | 0:8f0bb79ddd48 | 108 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 109 | |
leothedragon | 0:8f0bb79ddd48 | 110 | M2MReportHandler *handler = M2MBase::report_handler(); |
leothedragon | 0:8f0bb79ddd48 | 111 | if (!handler) { |
leothedragon | 0:8f0bb79ddd48 | 112 | handler = M2MBase::create_report_handler(); |
leothedragon | 0:8f0bb79ddd48 | 113 | } |
leothedragon | 0:8f0bb79ddd48 | 114 | |
leothedragon | 0:8f0bb79ddd48 | 115 | if (handler) { |
leothedragon | 0:8f0bb79ddd48 | 116 | success = handler->parse_notification_attribute(query, |
leothedragon | 0:8f0bb79ddd48 | 117 | M2MBase::base_type(), resource_instance_type()); |
leothedragon | 0:8f0bb79ddd48 | 118 | if(success) { |
leothedragon | 0:8f0bb79ddd48 | 119 | if (is_under_observation()) { |
leothedragon | 0:8f0bb79ddd48 | 120 | handler->set_under_observation(true); |
leothedragon | 0:8f0bb79ddd48 | 121 | } |
leothedragon | 0:8f0bb79ddd48 | 122 | } else { |
leothedragon | 0:8f0bb79ddd48 | 123 | handler->set_default_values(); |
leothedragon | 0:8f0bb79ddd48 | 124 | } |
leothedragon | 0:8f0bb79ddd48 | 125 | } |
leothedragon | 0:8f0bb79ddd48 | 126 | return success; |
leothedragon | 0:8f0bb79ddd48 | 127 | } |
leothedragon | 0:8f0bb79ddd48 | 128 | |
leothedragon | 0:8f0bb79ddd48 | 129 | uint16_t M2MResourceInstance::object_instance_id() const |
leothedragon | 0:8f0bb79ddd48 | 130 | { |
leothedragon | 0:8f0bb79ddd48 | 131 | const M2MObjectInstance& parent_object_instance = get_parent_resource().get_parent_object_instance(); |
leothedragon | 0:8f0bb79ddd48 | 132 | return parent_object_instance.instance_id(); |
leothedragon | 0:8f0bb79ddd48 | 133 | } |
leothedragon | 0:8f0bb79ddd48 | 134 | |
leothedragon | 0:8f0bb79ddd48 | 135 | M2MResource& M2MResourceInstance::get_parent_resource() const |
leothedragon | 0:8f0bb79ddd48 | 136 | { |
leothedragon | 0:8f0bb79ddd48 | 137 | return _parent_resource; |
leothedragon | 0:8f0bb79ddd48 | 138 | } |
leothedragon | 0:8f0bb79ddd48 | 139 | |
leothedragon | 0:8f0bb79ddd48 | 140 | M2MBase *M2MResourceInstance::get_parent() const |
leothedragon | 0:8f0bb79ddd48 | 141 | { |
leothedragon | 0:8f0bb79ddd48 | 142 | return (M2MBase *) &get_parent_resource(); |
leothedragon | 0:8f0bb79ddd48 | 143 | } |
leothedragon | 0:8f0bb79ddd48 | 144 | |
leothedragon | 0:8f0bb79ddd48 | 145 | const char* M2MResourceInstance::object_name() const |
leothedragon | 0:8f0bb79ddd48 | 146 | { |
leothedragon | 0:8f0bb79ddd48 | 147 | const M2MObjectInstance& parent_object_instance = _parent_resource.get_parent_object_instance(); |
leothedragon | 0:8f0bb79ddd48 | 148 | const M2MObject& parent_object = parent_object_instance.get_parent_object(); |
leothedragon | 0:8f0bb79ddd48 | 149 | |
leothedragon | 0:8f0bb79ddd48 | 150 | return parent_object.name(); |
leothedragon | 0:8f0bb79ddd48 | 151 | } |
leothedragon | 0:8f0bb79ddd48 | 152 |