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/m2mresourcebase.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 | |
leothedragon | 0:8f0bb79ddd48 | 17 | // Needed for PRIu64 on FreeRTOS |
leothedragon | 0:8f0bb79ddd48 | 18 | #include <stdio.h> |
leothedragon | 0:8f0bb79ddd48 | 19 | // Note: this macro is needed on armcc to get the the limit macros like UINT16_MAX |
leothedragon | 0:8f0bb79ddd48 | 20 | #ifndef __STDC_LIMIT_MACROS |
leothedragon | 0:8f0bb79ddd48 | 21 | #define __STDC_LIMIT_MACROS |
leothedragon | 0:8f0bb79ddd48 | 22 | #endif |
leothedragon | 0:8f0bb79ddd48 | 23 | |
leothedragon | 0:8f0bb79ddd48 | 24 | // Note: this macro is needed on armcc to get the the PRI*32 macros |
leothedragon | 0:8f0bb79ddd48 | 25 | // from inttypes.h in a C++ code. |
leothedragon | 0:8f0bb79ddd48 | 26 | #ifndef __STDC_FORMAT_MACROS |
leothedragon | 0:8f0bb79ddd48 | 27 | #define __STDC_FORMAT_MACROS |
leothedragon | 0:8f0bb79ddd48 | 28 | #endif |
leothedragon | 0:8f0bb79ddd48 | 29 | |
leothedragon | 0:8f0bb79ddd48 | 30 | #include <stdlib.h> |
leothedragon | 0:8f0bb79ddd48 | 31 | #include "mbed-client/m2mresourcebase.h" |
leothedragon | 0:8f0bb79ddd48 | 32 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:8f0bb79ddd48 | 33 | #include "mbed-client/m2mobservationhandler.h" |
leothedragon | 0:8f0bb79ddd48 | 34 | #include "mbed-client/m2mobject.h" |
leothedragon | 0:8f0bb79ddd48 | 35 | #include "mbed-client/m2mobjectinstance.h" |
leothedragon | 0:8f0bb79ddd48 | 36 | #include "include/m2mcallbackstorage.h" |
leothedragon | 0:8f0bb79ddd48 | 37 | #include "include/m2mreporthandler.h" |
leothedragon | 0:8f0bb79ddd48 | 38 | #include "include/nsdllinker.h" |
leothedragon | 0:8f0bb79ddd48 | 39 | #include "include/m2mtlvserializer.h" |
leothedragon | 0:8f0bb79ddd48 | 40 | #include "mbed-client/m2mblockmessage.h" |
leothedragon | 0:8f0bb79ddd48 | 41 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:8f0bb79ddd48 | 42 | |
leothedragon | 0:8f0bb79ddd48 | 43 | #define TRACE_GROUP "mClt" |
leothedragon | 0:8f0bb79ddd48 | 44 | |
leothedragon | 0:8f0bb79ddd48 | 45 | // -9223372036854775808 - +9223372036854775807 |
leothedragon | 0:8f0bb79ddd48 | 46 | // max length of int64_t string is 20 bytes + nil |
leothedragon | 0:8f0bb79ddd48 | 47 | #define REGISTRY_INT64_STRING_MAX_LEN 21 |
leothedragon | 0:8f0bb79ddd48 | 48 | // (space needed for -3.402823 × 10^38) + (magic decimal 6 digits added as no precision is added to "%f") + trailing zero |
leothedragon | 0:8f0bb79ddd48 | 49 | #define REGISTRY_FLOAT_STRING_MAX_LEN 48 |
leothedragon | 0:8f0bb79ddd48 | 50 | |
leothedragon | 0:8f0bb79ddd48 | 51 | |
leothedragon | 0:8f0bb79ddd48 | 52 | |
leothedragon | 0:8f0bb79ddd48 | 53 | M2MResourceBase::M2MResourceBase( |
leothedragon | 0:8f0bb79ddd48 | 54 | const String &res_name, |
leothedragon | 0:8f0bb79ddd48 | 55 | M2MBase::Mode resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 56 | const String &resource_type, |
leothedragon | 0:8f0bb79ddd48 | 57 | M2MBase::DataType type, |
leothedragon | 0:8f0bb79ddd48 | 58 | char* path, |
leothedragon | 0:8f0bb79ddd48 | 59 | bool external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 60 | bool multiple_instance) |
leothedragon | 0:8f0bb79ddd48 | 61 | : M2MBase(res_name, |
leothedragon | 0:8f0bb79ddd48 | 62 | resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 63 | #ifndef DISABLE_RESOURCE_TYPE |
leothedragon | 0:8f0bb79ddd48 | 64 | resource_type, |
leothedragon | 0:8f0bb79ddd48 | 65 | #endif |
leothedragon | 0:8f0bb79ddd48 | 66 | path, |
leothedragon | 0:8f0bb79ddd48 | 67 | external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 68 | multiple_instance, |
leothedragon | 0:8f0bb79ddd48 | 69 | type) |
leothedragon | 0:8f0bb79ddd48 | 70 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 71 | ,_block_message_data(NULL), |
leothedragon | 0:8f0bb79ddd48 | 72 | #endif |
leothedragon | 0:8f0bb79ddd48 | 73 | _notification_status(M2MResourceBase::INIT) |
leothedragon | 0:8f0bb79ddd48 | 74 | { |
leothedragon | 0:8f0bb79ddd48 | 75 | } |
leothedragon | 0:8f0bb79ddd48 | 76 | |
leothedragon | 0:8f0bb79ddd48 | 77 | M2MResourceBase::M2MResourceBase( |
leothedragon | 0:8f0bb79ddd48 | 78 | const String &res_name, |
leothedragon | 0:8f0bb79ddd48 | 79 | M2MBase::Mode resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 80 | const String &resource_type, |
leothedragon | 0:8f0bb79ddd48 | 81 | M2MBase::DataType type, |
leothedragon | 0:8f0bb79ddd48 | 82 | const uint8_t *value, |
leothedragon | 0:8f0bb79ddd48 | 83 | const uint8_t value_length, |
leothedragon | 0:8f0bb79ddd48 | 84 | char* path, |
leothedragon | 0:8f0bb79ddd48 | 85 | bool external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 86 | bool multiple_instance) |
leothedragon | 0:8f0bb79ddd48 | 87 | : M2MBase(res_name, |
leothedragon | 0:8f0bb79ddd48 | 88 | resource_mode, |
leothedragon | 0:8f0bb79ddd48 | 89 | #ifndef DISABLE_RESOURCE_TYPE |
leothedragon | 0:8f0bb79ddd48 | 90 | resource_type, |
leothedragon | 0:8f0bb79ddd48 | 91 | #endif |
leothedragon | 0:8f0bb79ddd48 | 92 | path, |
leothedragon | 0:8f0bb79ddd48 | 93 | external_blockwise_store, |
leothedragon | 0:8f0bb79ddd48 | 94 | multiple_instance, |
leothedragon | 0:8f0bb79ddd48 | 95 | type) |
leothedragon | 0:8f0bb79ddd48 | 96 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 97 | ,_block_message_data(NULL), |
leothedragon | 0:8f0bb79ddd48 | 98 | #endif |
leothedragon | 0:8f0bb79ddd48 | 99 | _notification_status(M2MResourceBase::INIT) |
leothedragon | 0:8f0bb79ddd48 | 100 | { |
leothedragon | 0:8f0bb79ddd48 | 101 | M2MBase::set_base_type(M2MBase::ResourceInstance); |
leothedragon | 0:8f0bb79ddd48 | 102 | if( value != NULL && value_length > 0 ) { |
leothedragon | 0:8f0bb79ddd48 | 103 | sn_nsdl_dynamic_resource_parameters_s* res = get_nsdl_resource(); |
leothedragon | 0:8f0bb79ddd48 | 104 | res->resource = alloc_string_copy(value, value_length); |
leothedragon | 0:8f0bb79ddd48 | 105 | res->resource_len = value_length; |
leothedragon | 0:8f0bb79ddd48 | 106 | } |
leothedragon | 0:8f0bb79ddd48 | 107 | } |
leothedragon | 0:8f0bb79ddd48 | 108 | |
leothedragon | 0:8f0bb79ddd48 | 109 | M2MResourceBase::M2MResourceBase( |
leothedragon | 0:8f0bb79ddd48 | 110 | const lwm2m_parameters_s* s, |
leothedragon | 0:8f0bb79ddd48 | 111 | M2MBase::DataType /*type*/) |
leothedragon | 0:8f0bb79ddd48 | 112 | : M2MBase(s) |
leothedragon | 0:8f0bb79ddd48 | 113 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 114 | ,_block_message_data(NULL), |
leothedragon | 0:8f0bb79ddd48 | 115 | #endif |
leothedragon | 0:8f0bb79ddd48 | 116 | _notification_status(M2MResourceBase::INIT) |
leothedragon | 0:8f0bb79ddd48 | 117 | { |
leothedragon | 0:8f0bb79ddd48 | 118 | // we are not there yet for this check as this is called from M2MResource(): assert(base_type() == M2MBase::ResourceInstance); |
leothedragon | 0:8f0bb79ddd48 | 119 | } |
leothedragon | 0:8f0bb79ddd48 | 120 | |
leothedragon | 0:8f0bb79ddd48 | 121 | M2MResourceBase::~M2MResourceBase() |
leothedragon | 0:8f0bb79ddd48 | 122 | { |
leothedragon | 0:8f0bb79ddd48 | 123 | execute_callback* callback = (execute_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 124 | M2MCallbackAssociation::M2MResourceInstanceExecuteCallback); |
leothedragon | 0:8f0bb79ddd48 | 125 | delete callback; |
leothedragon | 0:8f0bb79ddd48 | 126 | |
leothedragon | 0:8f0bb79ddd48 | 127 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback2); |
leothedragon | 0:8f0bb79ddd48 | 128 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 129 | incoming_block_message_callback *in_callback = (incoming_block_message_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 130 | M2MCallbackAssociation::M2MResourceInstanceIncomingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 131 | delete in_callback; |
leothedragon | 0:8f0bb79ddd48 | 132 | |
leothedragon | 0:8f0bb79ddd48 | 133 | outgoing_block_message_callback *out_callback = (outgoing_block_message_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 134 | M2MCallbackAssociation::M2MResourceInstanceOutgoingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 135 | delete out_callback; |
leothedragon | 0:8f0bb79ddd48 | 136 | #endif |
leothedragon | 0:8f0bb79ddd48 | 137 | |
leothedragon | 0:8f0bb79ddd48 | 138 | notification_sent_callback *notif_callback = (notification_sent_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 139 | M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback); |
leothedragon | 0:8f0bb79ddd48 | 140 | delete notif_callback; |
leothedragon | 0:8f0bb79ddd48 | 141 | |
leothedragon | 0:8f0bb79ddd48 | 142 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback2); |
leothedragon | 0:8f0bb79ddd48 | 143 | |
leothedragon | 0:8f0bb79ddd48 | 144 | notification_status_callback *notif_status_callback = (notification_status_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 145 | M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback); |
leothedragon | 0:8f0bb79ddd48 | 146 | delete notif_status_callback; |
leothedragon | 0:8f0bb79ddd48 | 147 | |
leothedragon | 0:8f0bb79ddd48 | 148 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback2); |
leothedragon | 0:8f0bb79ddd48 | 149 | |
leothedragon | 0:8f0bb79ddd48 | 150 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueReadCallback); |
leothedragon | 0:8f0bb79ddd48 | 151 | |
leothedragon | 0:8f0bb79ddd48 | 152 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueWriteCallback); |
leothedragon | 0:8f0bb79ddd48 | 153 | |
leothedragon | 0:8f0bb79ddd48 | 154 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 155 | delete _block_message_data; |
leothedragon | 0:8f0bb79ddd48 | 156 | #endif |
leothedragon | 0:8f0bb79ddd48 | 157 | } |
leothedragon | 0:8f0bb79ddd48 | 158 | |
leothedragon | 0:8f0bb79ddd48 | 159 | M2MResourceBase::ResourceType M2MResourceBase::resource_instance_type() const |
leothedragon | 0:8f0bb79ddd48 | 160 | { |
leothedragon | 0:8f0bb79ddd48 | 161 | M2MBase::lwm2m_parameters_s* param = M2MBase::get_lwm2m_parameters(); |
leothedragon | 0:8f0bb79ddd48 | 162 | M2MBase::DataType type = param->data_type; |
leothedragon | 0:8f0bb79ddd48 | 163 | return convert_data_type(type); |
leothedragon | 0:8f0bb79ddd48 | 164 | } |
leothedragon | 0:8f0bb79ddd48 | 165 | |
leothedragon | 0:8f0bb79ddd48 | 166 | |
leothedragon | 0:8f0bb79ddd48 | 167 | bool M2MResourceBase::set_execute_function(execute_callback callback) |
leothedragon | 0:8f0bb79ddd48 | 168 | { |
leothedragon | 0:8f0bb79ddd48 | 169 | execute_callback* old_callback = (execute_callback*)M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback); |
leothedragon | 0:8f0bb79ddd48 | 170 | delete old_callback; |
leothedragon | 0:8f0bb79ddd48 | 171 | // XXX: create a copy of the copy of callback object. Perhaps it would better to |
leothedragon | 0:8f0bb79ddd48 | 172 | // give a reference as parameter and just store that, as it would save some memory. |
leothedragon | 0:8f0bb79ddd48 | 173 | execute_callback* new_callback = new execute_callback(callback); |
leothedragon | 0:8f0bb79ddd48 | 174 | |
leothedragon | 0:8f0bb79ddd48 | 175 | return M2MCallbackStorage::add_callback(*this, new_callback, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback); |
leothedragon | 0:8f0bb79ddd48 | 176 | } |
leothedragon | 0:8f0bb79ddd48 | 177 | |
leothedragon | 0:8f0bb79ddd48 | 178 | bool M2MResourceBase::set_execute_function(execute_callback_2 callback) |
leothedragon | 0:8f0bb79ddd48 | 179 | { |
leothedragon | 0:8f0bb79ddd48 | 180 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback2); |
leothedragon | 0:8f0bb79ddd48 | 181 | |
leothedragon | 0:8f0bb79ddd48 | 182 | return M2MCallbackStorage::add_callback(*this, (void*)callback, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback2); |
leothedragon | 0:8f0bb79ddd48 | 183 | } |
leothedragon | 0:8f0bb79ddd48 | 184 | |
leothedragon | 0:8f0bb79ddd48 | 185 | bool M2MResourceBase::set_resource_read_callback(read_resource_value_callback callback, void *client_args) |
leothedragon | 0:8f0bb79ddd48 | 186 | { |
leothedragon | 0:8f0bb79ddd48 | 187 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueReadCallback); |
leothedragon | 0:8f0bb79ddd48 | 188 | M2MBase::lwm2m_parameters_s* param = M2MBase::get_lwm2m_parameters(); |
leothedragon | 0:8f0bb79ddd48 | 189 | param->read_write_callback_set = true; |
leothedragon | 0:8f0bb79ddd48 | 190 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 191 | (void*)callback, |
leothedragon | 0:8f0bb79ddd48 | 192 | M2MCallbackAssociation::M2MResourceBaseValueReadCallback, |
leothedragon | 0:8f0bb79ddd48 | 193 | client_args); |
leothedragon | 0:8f0bb79ddd48 | 194 | |
leothedragon | 0:8f0bb79ddd48 | 195 | } |
leothedragon | 0:8f0bb79ddd48 | 196 | |
leothedragon | 0:8f0bb79ddd48 | 197 | bool M2MResourceBase::set_resource_write_callback(write_resource_value_callback callback, void *client_args) |
leothedragon | 0:8f0bb79ddd48 | 198 | { |
leothedragon | 0:8f0bb79ddd48 | 199 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueWriteCallback); |
leothedragon | 0:8f0bb79ddd48 | 200 | M2MBase::lwm2m_parameters_s* param = M2MBase::get_lwm2m_parameters(); |
leothedragon | 0:8f0bb79ddd48 | 201 | param->read_write_callback_set = true; |
leothedragon | 0:8f0bb79ddd48 | 202 | |
leothedragon | 0:8f0bb79ddd48 | 203 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 204 | (void*)callback, |
leothedragon | 0:8f0bb79ddd48 | 205 | M2MCallbackAssociation::M2MResourceBaseValueWriteCallback, |
leothedragon | 0:8f0bb79ddd48 | 206 | client_args); |
leothedragon | 0:8f0bb79ddd48 | 207 | } |
leothedragon | 0:8f0bb79ddd48 | 208 | |
leothedragon | 0:8f0bb79ddd48 | 209 | void M2MResourceBase::clear_value() |
leothedragon | 0:8f0bb79ddd48 | 210 | { |
leothedragon | 0:8f0bb79ddd48 | 211 | tr_debug("M2MResourceBase::clear_value"); |
leothedragon | 0:8f0bb79ddd48 | 212 | |
leothedragon | 0:8f0bb79ddd48 | 213 | sn_nsdl_dynamic_resource_parameters_s* res = get_nsdl_resource(); |
leothedragon | 0:8f0bb79ddd48 | 214 | free(res->resource); |
leothedragon | 0:8f0bb79ddd48 | 215 | res->resource = NULL; |
leothedragon | 0:8f0bb79ddd48 | 216 | res->resource_len = 0; |
leothedragon | 0:8f0bb79ddd48 | 217 | |
leothedragon | 0:8f0bb79ddd48 | 218 | report(); |
leothedragon | 0:8f0bb79ddd48 | 219 | } |
leothedragon | 0:8f0bb79ddd48 | 220 | |
leothedragon | 0:8f0bb79ddd48 | 221 | bool M2MResourceBase::set_value_float(float value) |
leothedragon | 0:8f0bb79ddd48 | 222 | { |
leothedragon | 0:8f0bb79ddd48 | 223 | bool success; |
leothedragon | 0:8f0bb79ddd48 | 224 | char buffer[REGISTRY_FLOAT_STRING_MAX_LEN]; |
leothedragon | 0:8f0bb79ddd48 | 225 | |
leothedragon | 0:8f0bb79ddd48 | 226 | // Convert value to string |
leothedragon | 0:8f0bb79ddd48 | 227 | /* write the float value to a decimal number string and copy it into a buffer allocated for caller */ |
leothedragon | 0:8f0bb79ddd48 | 228 | uint32_t size = snprintf(buffer, REGISTRY_FLOAT_STRING_MAX_LEN, "%f", value); |
leothedragon | 0:8f0bb79ddd48 | 229 | |
leothedragon | 0:8f0bb79ddd48 | 230 | success = set_value((const uint8_t*)buffer, size); |
leothedragon | 0:8f0bb79ddd48 | 231 | |
leothedragon | 0:8f0bb79ddd48 | 232 | return success; |
leothedragon | 0:8f0bb79ddd48 | 233 | } |
leothedragon | 0:8f0bb79ddd48 | 234 | |
leothedragon | 0:8f0bb79ddd48 | 235 | bool M2MResourceBase::set_value(int64_t value) |
leothedragon | 0:8f0bb79ddd48 | 236 | { |
leothedragon | 0:8f0bb79ddd48 | 237 | bool success; |
leothedragon | 0:8f0bb79ddd48 | 238 | char buffer[REGISTRY_INT64_STRING_MAX_LEN]; |
leothedragon | 0:8f0bb79ddd48 | 239 | uint32_t size = m2m::itoa_c(value, buffer); |
leothedragon | 0:8f0bb79ddd48 | 240 | |
leothedragon | 0:8f0bb79ddd48 | 241 | success = set_value((const uint8_t*)buffer, size); |
leothedragon | 0:8f0bb79ddd48 | 242 | |
leothedragon | 0:8f0bb79ddd48 | 243 | return success; |
leothedragon | 0:8f0bb79ddd48 | 244 | } |
leothedragon | 0:8f0bb79ddd48 | 245 | |
leothedragon | 0:8f0bb79ddd48 | 246 | bool M2MResourceBase::set_value(const uint8_t *value, |
leothedragon | 0:8f0bb79ddd48 | 247 | const uint32_t value_length) |
leothedragon | 0:8f0bb79ddd48 | 248 | { |
leothedragon | 0:8f0bb79ddd48 | 249 | tr_debug("M2MResourceBase::set_value()"); |
leothedragon | 0:8f0bb79ddd48 | 250 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 251 | if( value != NULL && value_length > 0 ) { |
leothedragon | 0:8f0bb79ddd48 | 252 | M2MBase::lwm2m_parameters_s* param = M2MBase::get_lwm2m_parameters(); |
leothedragon | 0:8f0bb79ddd48 | 253 | if (param->read_write_callback_set) { |
leothedragon | 0:8f0bb79ddd48 | 254 | return write_resource_value(*this, value, value_length); |
leothedragon | 0:8f0bb79ddd48 | 255 | } else { |
leothedragon | 0:8f0bb79ddd48 | 256 | uint8_t *value_copy = alloc_string_copy(value, value_length); |
leothedragon | 0:8f0bb79ddd48 | 257 | if (value_copy) { |
leothedragon | 0:8f0bb79ddd48 | 258 | value_set_callback callback = (value_set_callback)M2MCallbackStorage::get_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueSetCallback); |
leothedragon | 0:8f0bb79ddd48 | 259 | if (callback) { |
leothedragon | 0:8f0bb79ddd48 | 260 | (*callback)((const M2MResourceBase*)this, value_copy, value_length); |
leothedragon | 0:8f0bb79ddd48 | 261 | } |
leothedragon | 0:8f0bb79ddd48 | 262 | else { |
leothedragon | 0:8f0bb79ddd48 | 263 | update_value(value_copy, value_length); |
leothedragon | 0:8f0bb79ddd48 | 264 | } |
leothedragon | 0:8f0bb79ddd48 | 265 | success = true; |
leothedragon | 0:8f0bb79ddd48 | 266 | } |
leothedragon | 0:8f0bb79ddd48 | 267 | } |
leothedragon | 0:8f0bb79ddd48 | 268 | } |
leothedragon | 0:8f0bb79ddd48 | 269 | return success; |
leothedragon | 0:8f0bb79ddd48 | 270 | } |
leothedragon | 0:8f0bb79ddd48 | 271 | |
leothedragon | 0:8f0bb79ddd48 | 272 | bool M2MResourceBase::set_value_raw(uint8_t *value, |
leothedragon | 0:8f0bb79ddd48 | 273 | const uint32_t value_length) |
leothedragon | 0:8f0bb79ddd48 | 274 | |
leothedragon | 0:8f0bb79ddd48 | 275 | { |
leothedragon | 0:8f0bb79ddd48 | 276 | tr_debug("M2MResourceBase::set_value_raw()"); |
leothedragon | 0:8f0bb79ddd48 | 277 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 278 | if( value != NULL && value_length > 0 ) { |
leothedragon | 0:8f0bb79ddd48 | 279 | success = true; |
leothedragon | 0:8f0bb79ddd48 | 280 | value_set_callback callback = (value_set_callback)M2MCallbackStorage::get_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueSetCallback); |
leothedragon | 0:8f0bb79ddd48 | 281 | if (callback) { |
leothedragon | 0:8f0bb79ddd48 | 282 | (*callback)((const M2MResourceBase*)this, value, value_length); |
leothedragon | 0:8f0bb79ddd48 | 283 | } |
leothedragon | 0:8f0bb79ddd48 | 284 | else { |
leothedragon | 0:8f0bb79ddd48 | 285 | update_value(value, value_length); |
leothedragon | 0:8f0bb79ddd48 | 286 | } |
leothedragon | 0:8f0bb79ddd48 | 287 | } |
leothedragon | 0:8f0bb79ddd48 | 288 | return success; |
leothedragon | 0:8f0bb79ddd48 | 289 | } |
leothedragon | 0:8f0bb79ddd48 | 290 | |
leothedragon | 0:8f0bb79ddd48 | 291 | void M2MResourceBase::update_value(uint8_t *value, const uint32_t value_length) |
leothedragon | 0:8f0bb79ddd48 | 292 | { |
leothedragon | 0:8f0bb79ddd48 | 293 | bool changed = has_value_changed(value,value_length); |
leothedragon | 0:8f0bb79ddd48 | 294 | sn_nsdl_dynamic_resource_parameters_s* res = get_nsdl_resource(); |
leothedragon | 0:8f0bb79ddd48 | 295 | free(res->resource); |
leothedragon | 0:8f0bb79ddd48 | 296 | res->resource = value; |
leothedragon | 0:8f0bb79ddd48 | 297 | res->resource_len = value_length; |
leothedragon | 0:8f0bb79ddd48 | 298 | if (changed) { |
leothedragon | 0:8f0bb79ddd48 | 299 | report_value_change(); |
leothedragon | 0:8f0bb79ddd48 | 300 | } |
leothedragon | 0:8f0bb79ddd48 | 301 | } |
leothedragon | 0:8f0bb79ddd48 | 302 | |
leothedragon | 0:8f0bb79ddd48 | 303 | void M2MResourceBase::report() |
leothedragon | 0:8f0bb79ddd48 | 304 | { |
leothedragon | 0:8f0bb79ddd48 | 305 | M2MBase::Observation observation_level = M2MBase::observation_level(); |
leothedragon | 0:8f0bb79ddd48 | 306 | tr_debug("M2MResourceBase::report() - level %d", observation_level); |
leothedragon | 0:8f0bb79ddd48 | 307 | |
leothedragon | 0:8f0bb79ddd48 | 308 | // We must combine the parent object/objectinstance/resource observation information |
leothedragon | 0:8f0bb79ddd48 | 309 | // when determining if there is observation set or not. |
leothedragon | 0:8f0bb79ddd48 | 310 | M2MObjectInstance& object_instance = get_parent_resource().get_parent_object_instance(); |
leothedragon | 0:8f0bb79ddd48 | 311 | int parent_observation_level = (int)object_instance.observation_level(); |
leothedragon | 0:8f0bb79ddd48 | 312 | |
leothedragon | 0:8f0bb79ddd48 | 313 | parent_observation_level |= (int)object_instance.get_parent_object().observation_level(); |
leothedragon | 0:8f0bb79ddd48 | 314 | parent_observation_level |= (int)get_parent_resource().observation_level(); |
leothedragon | 0:8f0bb79ddd48 | 315 | parent_observation_level |= (int)observation_level; |
leothedragon | 0:8f0bb79ddd48 | 316 | |
leothedragon | 0:8f0bb79ddd48 | 317 | tr_debug("M2MResourceBase::report() - combined level %d", parent_observation_level); |
leothedragon | 0:8f0bb79ddd48 | 318 | |
leothedragon | 0:8f0bb79ddd48 | 319 | if((M2MBase::O_Attribute & parent_observation_level) == M2MBase::O_Attribute || |
leothedragon | 0:8f0bb79ddd48 | 320 | (M2MBase::OI_Attribute & parent_observation_level) == M2MBase::OI_Attribute) { |
leothedragon | 0:8f0bb79ddd48 | 321 | tr_debug("M2MResourceBase::report() -- object/instance level"); |
leothedragon | 0:8f0bb79ddd48 | 322 | object_instance.notification_update((M2MBase::Observation)parent_observation_level); |
leothedragon | 0:8f0bb79ddd48 | 323 | } |
leothedragon | 0:8f0bb79ddd48 | 324 | |
leothedragon | 0:8f0bb79ddd48 | 325 | if(M2MBase::Dynamic == mode() && |
leothedragon | 0:8f0bb79ddd48 | 326 | (M2MBase::R_Attribute & parent_observation_level) == M2MBase::R_Attribute) { |
leothedragon | 0:8f0bb79ddd48 | 327 | tr_debug("M2MResourceBase::report() - resource level"); |
leothedragon | 0:8f0bb79ddd48 | 328 | |
leothedragon | 0:8f0bb79ddd48 | 329 | if (((resource_instance_type() != M2MResourceBase::STRING) && |
leothedragon | 0:8f0bb79ddd48 | 330 | (resource_instance_type() != M2MResourceBase::OPAQUE)) && |
leothedragon | 0:8f0bb79ddd48 | 331 | (observation_level != M2MBase::None)) { |
leothedragon | 0:8f0bb79ddd48 | 332 | M2MReportHandler *report_handler = M2MBase::report_handler(); |
leothedragon | 0:8f0bb79ddd48 | 333 | if (report_handler && (is_observable() || is_auto_observable())) { |
leothedragon | 0:8f0bb79ddd48 | 334 | if (resource_instance_type() == M2MResourceBase::FLOAT) { |
leothedragon | 0:8f0bb79ddd48 | 335 | const float float_value = get_value_float(); |
leothedragon | 0:8f0bb79ddd48 | 336 | report_handler->set_value_float(float_value); |
leothedragon | 0:8f0bb79ddd48 | 337 | } else { |
leothedragon | 0:8f0bb79ddd48 | 338 | const int64_t int_value = get_value_int(); |
leothedragon | 0:8f0bb79ddd48 | 339 | report_handler->set_value_int(int_value); |
leothedragon | 0:8f0bb79ddd48 | 340 | } |
leothedragon | 0:8f0bb79ddd48 | 341 | } |
leothedragon | 0:8f0bb79ddd48 | 342 | } |
leothedragon | 0:8f0bb79ddd48 | 343 | else { |
leothedragon | 0:8f0bb79ddd48 | 344 | if (base_type() == M2MBase::ResourceInstance) { |
leothedragon | 0:8f0bb79ddd48 | 345 | const M2MResource& parent_resource = get_parent_resource(); |
leothedragon | 0:8f0bb79ddd48 | 346 | M2MReportHandler *report_handler = parent_resource.report_handler(); |
leothedragon | 0:8f0bb79ddd48 | 347 | if(report_handler && (parent_resource.is_observable() || parent_resource.is_auto_observable())) { |
leothedragon | 0:8f0bb79ddd48 | 348 | report_handler->set_notification_trigger(parent_resource.get_parent_object_instance().instance_id()); |
leothedragon | 0:8f0bb79ddd48 | 349 | } |
leothedragon | 0:8f0bb79ddd48 | 350 | } |
leothedragon | 0:8f0bb79ddd48 | 351 | } |
leothedragon | 0:8f0bb79ddd48 | 352 | } else if(M2MBase::Static == mode()) { |
leothedragon | 0:8f0bb79ddd48 | 353 | M2MObservationHandler *obs_handler = observation_handler(); |
leothedragon | 0:8f0bb79ddd48 | 354 | if(obs_handler) { |
leothedragon | 0:8f0bb79ddd48 | 355 | obs_handler->value_updated(this); |
leothedragon | 0:8f0bb79ddd48 | 356 | } |
leothedragon | 0:8f0bb79ddd48 | 357 | } else { |
leothedragon | 0:8f0bb79ddd48 | 358 | if (is_observable() || is_auto_observable()) { |
leothedragon | 0:8f0bb79ddd48 | 359 | tr_debug("M2MResourceBase::report() - resource %s is observable but not yet subscribed!", uri_path()); |
leothedragon | 0:8f0bb79ddd48 | 360 | } |
leothedragon | 0:8f0bb79ddd48 | 361 | tr_debug("M2MResourceBase::report() - mode = %d, is_observable = %d", mode(), is_observable()); |
leothedragon | 0:8f0bb79ddd48 | 362 | } |
leothedragon | 0:8f0bb79ddd48 | 363 | } |
leothedragon | 0:8f0bb79ddd48 | 364 | |
leothedragon | 0:8f0bb79ddd48 | 365 | bool M2MResourceBase::has_value_changed(const uint8_t* value, const uint32_t value_len) |
leothedragon | 0:8f0bb79ddd48 | 366 | { |
leothedragon | 0:8f0bb79ddd48 | 367 | bool changed = false; |
leothedragon | 0:8f0bb79ddd48 | 368 | sn_nsdl_dynamic_resource_parameters_s* res = get_nsdl_resource(); |
leothedragon | 0:8f0bb79ddd48 | 369 | |
leothedragon | 0:8f0bb79ddd48 | 370 | if(value_len != res->resource_len) { |
leothedragon | 0:8f0bb79ddd48 | 371 | changed = true; |
leothedragon | 0:8f0bb79ddd48 | 372 | } else if(value && !res->resource) { |
leothedragon | 0:8f0bb79ddd48 | 373 | changed = true; |
leothedragon | 0:8f0bb79ddd48 | 374 | } else if(res->resource && !value) { |
leothedragon | 0:8f0bb79ddd48 | 375 | changed = true; |
leothedragon | 0:8f0bb79ddd48 | 376 | } else { |
leothedragon | 0:8f0bb79ddd48 | 377 | if (res->resource) { |
leothedragon | 0:8f0bb79ddd48 | 378 | if (memcmp(value, res->resource, res->resource_len) != 0) { |
leothedragon | 0:8f0bb79ddd48 | 379 | changed = true; |
leothedragon | 0:8f0bb79ddd48 | 380 | } |
leothedragon | 0:8f0bb79ddd48 | 381 | } |
leothedragon | 0:8f0bb79ddd48 | 382 | } |
leothedragon | 0:8f0bb79ddd48 | 383 | return changed; |
leothedragon | 0:8f0bb79ddd48 | 384 | } |
leothedragon | 0:8f0bb79ddd48 | 385 | |
leothedragon | 0:8f0bb79ddd48 | 386 | void M2MResourceBase::report_value_change() |
leothedragon | 0:8f0bb79ddd48 | 387 | { |
leothedragon | 0:8f0bb79ddd48 | 388 | if (resource_instance_type() == M2MResourceBase::STRING || |
leothedragon | 0:8f0bb79ddd48 | 389 | resource_instance_type() == M2MResourceBase::OPAQUE) { |
leothedragon | 0:8f0bb79ddd48 | 390 | M2MReportHandler *report_handler = M2MBase::report_handler(); |
leothedragon | 0:8f0bb79ddd48 | 391 | if(report_handler && is_under_observation()) { |
leothedragon | 0:8f0bb79ddd48 | 392 | report_handler->set_notification_trigger(); |
leothedragon | 0:8f0bb79ddd48 | 393 | } |
leothedragon | 0:8f0bb79ddd48 | 394 | } |
leothedragon | 0:8f0bb79ddd48 | 395 | report(); |
leothedragon | 0:8f0bb79ddd48 | 396 | } |
leothedragon | 0:8f0bb79ddd48 | 397 | |
leothedragon | 0:8f0bb79ddd48 | 398 | void M2MResourceBase::execute(void *arguments) |
leothedragon | 0:8f0bb79ddd48 | 399 | { |
leothedragon | 0:8f0bb79ddd48 | 400 | // XXX: this line is expected by seven testcases and until this code hits master branch |
leothedragon | 0:8f0bb79ddd48 | 401 | // the testcases can not be modified and we need to print the false information too. |
leothedragon | 0:8f0bb79ddd48 | 402 | tr_debug("M2MResourceBase::execute"); |
leothedragon | 0:8f0bb79ddd48 | 403 | |
leothedragon | 0:8f0bb79ddd48 | 404 | execute_callback* callback = (execute_callback*)M2MCallbackStorage::get_callback(*this, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback); |
leothedragon | 0:8f0bb79ddd48 | 405 | |
leothedragon | 0:8f0bb79ddd48 | 406 | if (callback) { |
leothedragon | 0:8f0bb79ddd48 | 407 | (*callback)(arguments); |
leothedragon | 0:8f0bb79ddd48 | 408 | } |
leothedragon | 0:8f0bb79ddd48 | 409 | |
leothedragon | 0:8f0bb79ddd48 | 410 | execute_callback_2 callback2 = (execute_callback_2)M2MCallbackStorage::get_callback(*this, M2MCallbackAssociation::M2MResourceInstanceExecuteCallback2); |
leothedragon | 0:8f0bb79ddd48 | 411 | if (callback2) { |
leothedragon | 0:8f0bb79ddd48 | 412 | (*callback2)(arguments); |
leothedragon | 0:8f0bb79ddd48 | 413 | } |
leothedragon | 0:8f0bb79ddd48 | 414 | } |
leothedragon | 0:8f0bb79ddd48 | 415 | |
leothedragon | 0:8f0bb79ddd48 | 416 | int M2MResourceBase::read_resource_value(const M2MResourceBase &resource, void *buffer, size_t *buffer_len) |
leothedragon | 0:8f0bb79ddd48 | 417 | { |
leothedragon | 0:8f0bb79ddd48 | 418 | tr_debug("M2MResourceBase::read_resource_value"); |
leothedragon | 0:8f0bb79ddd48 | 419 | |
leothedragon | 0:8f0bb79ddd48 | 420 | M2MCallbackAssociation* item = M2MCallbackStorage::get_association_item(resource, |
leothedragon | 0:8f0bb79ddd48 | 421 | M2MCallbackAssociation::M2MResourceBaseValueReadCallback); |
leothedragon | 0:8f0bb79ddd48 | 422 | |
leothedragon | 0:8f0bb79ddd48 | 423 | if (item) { |
leothedragon | 0:8f0bb79ddd48 | 424 | read_resource_value_callback callback = (read_resource_value_callback)item->_callback; |
leothedragon | 0:8f0bb79ddd48 | 425 | assert(callback); |
leothedragon | 0:8f0bb79ddd48 | 426 | return (*callback)(resource, buffer, buffer_len, item->_client_args); |
leothedragon | 0:8f0bb79ddd48 | 427 | } else { |
leothedragon | 0:8f0bb79ddd48 | 428 | if (value_length() > *buffer_len) { |
leothedragon | 0:8f0bb79ddd48 | 429 | return -1; |
leothedragon | 0:8f0bb79ddd48 | 430 | } else { |
leothedragon | 0:8f0bb79ddd48 | 431 | memcpy(buffer, value(), value_length()); |
leothedragon | 0:8f0bb79ddd48 | 432 | *buffer_len = value_length(); |
leothedragon | 0:8f0bb79ddd48 | 433 | return 0; |
leothedragon | 0:8f0bb79ddd48 | 434 | } |
leothedragon | 0:8f0bb79ddd48 | 435 | } |
leothedragon | 0:8f0bb79ddd48 | 436 | } |
leothedragon | 0:8f0bb79ddd48 | 437 | |
leothedragon | 0:8f0bb79ddd48 | 438 | bool M2MResourceBase::write_resource_value(const M2MResourceBase &resource, const uint8_t *buffer, const size_t buffer_size) |
leothedragon | 0:8f0bb79ddd48 | 439 | { |
leothedragon | 0:8f0bb79ddd48 | 440 | tr_debug("M2MResourceBase::write_resource_value"); |
leothedragon | 0:8f0bb79ddd48 | 441 | M2MCallbackAssociation* item = M2MCallbackStorage::get_association_item(resource, |
leothedragon | 0:8f0bb79ddd48 | 442 | M2MCallbackAssociation::M2MResourceBaseValueWriteCallback); |
leothedragon | 0:8f0bb79ddd48 | 443 | if (item) { |
leothedragon | 0:8f0bb79ddd48 | 444 | write_resource_value_callback callback = (write_resource_value_callback)item->_callback; |
leothedragon | 0:8f0bb79ddd48 | 445 | if (callback) { |
leothedragon | 0:8f0bb79ddd48 | 446 | return (*callback)(resource, buffer, buffer_size, item->_client_args); |
leothedragon | 0:8f0bb79ddd48 | 447 | } |
leothedragon | 0:8f0bb79ddd48 | 448 | } |
leothedragon | 0:8f0bb79ddd48 | 449 | |
leothedragon | 0:8f0bb79ddd48 | 450 | return false; |
leothedragon | 0:8f0bb79ddd48 | 451 | } |
leothedragon | 0:8f0bb79ddd48 | 452 | |
leothedragon | 0:8f0bb79ddd48 | 453 | void M2MResourceBase::get_value(uint8_t *&value, uint32_t &value_length) |
leothedragon | 0:8f0bb79ddd48 | 454 | { |
leothedragon | 0:8f0bb79ddd48 | 455 | value_length = 0; |
leothedragon | 0:8f0bb79ddd48 | 456 | if(value) { |
leothedragon | 0:8f0bb79ddd48 | 457 | free(value); |
leothedragon | 0:8f0bb79ddd48 | 458 | value = NULL; |
leothedragon | 0:8f0bb79ddd48 | 459 | } |
leothedragon | 0:8f0bb79ddd48 | 460 | |
leothedragon | 0:8f0bb79ddd48 | 461 | sn_nsdl_dynamic_resource_parameters_s* res = get_nsdl_resource(); |
leothedragon | 0:8f0bb79ddd48 | 462 | if(res->resource && res->resource_len > 0) { |
leothedragon | 0:8f0bb79ddd48 | 463 | value = alloc_string_copy(res->resource, res->resource_len); |
leothedragon | 0:8f0bb79ddd48 | 464 | if(value) { |
leothedragon | 0:8f0bb79ddd48 | 465 | value_length = res->resource_len; |
leothedragon | 0:8f0bb79ddd48 | 466 | } |
leothedragon | 0:8f0bb79ddd48 | 467 | } |
leothedragon | 0:8f0bb79ddd48 | 468 | } |
leothedragon | 0:8f0bb79ddd48 | 469 | |
leothedragon | 0:8f0bb79ddd48 | 470 | int64_t M2MResourceBase::get_value_int() const |
leothedragon | 0:8f0bb79ddd48 | 471 | { |
leothedragon | 0:8f0bb79ddd48 | 472 | int64_t value_int = 0; |
leothedragon | 0:8f0bb79ddd48 | 473 | |
leothedragon | 0:8f0bb79ddd48 | 474 | const char *value_string = (char *)value(); |
leothedragon | 0:8f0bb79ddd48 | 475 | const uint32_t value_len = value_length(); |
leothedragon | 0:8f0bb79ddd48 | 476 | |
leothedragon | 0:8f0bb79ddd48 | 477 | if ((value_string) && (value_len <= REGISTRY_INT64_STRING_MAX_LEN)) { |
leothedragon | 0:8f0bb79ddd48 | 478 | |
leothedragon | 0:8f0bb79ddd48 | 479 | // -9223372036854775808 - +9223372036854775807 |
leothedragon | 0:8f0bb79ddd48 | 480 | // max length of int64_t string is 20 bytes + nil |
leothedragon | 0:8f0bb79ddd48 | 481 | // The +1 here is there in case the string was already zero terminated. |
leothedragon | 0:8f0bb79ddd48 | 482 | |
leothedragon | 0:8f0bb79ddd48 | 483 | |
leothedragon | 0:8f0bb79ddd48 | 484 | bool success = String::convert_ascii_to_int(value_string, value_len, value_int); |
leothedragon | 0:8f0bb79ddd48 | 485 | if (!success) { |
leothedragon | 0:8f0bb79ddd48 | 486 | // note: the convert_ascii_to_int() actually allows one to pass the conversion |
leothedragon | 0:8f0bb79ddd48 | 487 | // onwards, but this get_value_int() does not. Lets just dump error to log, but |
leothedragon | 0:8f0bb79ddd48 | 488 | // do not log the value as that might be part of a attack. Same reason (valid or not) |
leothedragon | 0:8f0bb79ddd48 | 489 | // is behind the selection of log level |
leothedragon | 0:8f0bb79ddd48 | 490 | tr_warn("M2MResourceBase::get_value_int(): conversion failed"); |
leothedragon | 0:8f0bb79ddd48 | 491 | } |
leothedragon | 0:8f0bb79ddd48 | 492 | } |
leothedragon | 0:8f0bb79ddd48 | 493 | return value_int; |
leothedragon | 0:8f0bb79ddd48 | 494 | } |
leothedragon | 0:8f0bb79ddd48 | 495 | |
leothedragon | 0:8f0bb79ddd48 | 496 | String M2MResourceBase::get_value_string() const |
leothedragon | 0:8f0bb79ddd48 | 497 | { |
leothedragon | 0:8f0bb79ddd48 | 498 | // XXX: do a better constructor to avoid pointless malloc |
leothedragon | 0:8f0bb79ddd48 | 499 | String value; |
leothedragon | 0:8f0bb79ddd48 | 500 | if (get_nsdl_resource()->resource) { |
leothedragon | 0:8f0bb79ddd48 | 501 | value.append_raw((char*)get_nsdl_resource()->resource, get_nsdl_resource()->resource_len); |
leothedragon | 0:8f0bb79ddd48 | 502 | } |
leothedragon | 0:8f0bb79ddd48 | 503 | return value; |
leothedragon | 0:8f0bb79ddd48 | 504 | } |
leothedragon | 0:8f0bb79ddd48 | 505 | |
leothedragon | 0:8f0bb79ddd48 | 506 | float M2MResourceBase::get_value_float() const |
leothedragon | 0:8f0bb79ddd48 | 507 | { |
leothedragon | 0:8f0bb79ddd48 | 508 | float value_float = 0; |
leothedragon | 0:8f0bb79ddd48 | 509 | |
leothedragon | 0:8f0bb79ddd48 | 510 | const char *value_string = (char *)value(); |
leothedragon | 0:8f0bb79ddd48 | 511 | const uint32_t value_len = value_length(); |
leothedragon | 0:8f0bb79ddd48 | 512 | |
leothedragon | 0:8f0bb79ddd48 | 513 | if ((value_string) && (value_len <= REGISTRY_FLOAT_STRING_MAX_LEN)) { |
leothedragon | 0:8f0bb79ddd48 | 514 | |
leothedragon | 0:8f0bb79ddd48 | 515 | // (space needed for -3.402823 × 10^38) + (magic decimal 6 digits added as no precision is added to "%f") + trailing zero |
leothedragon | 0:8f0bb79ddd48 | 516 | // The +1 here is there in case the string was already zero terminated. |
leothedragon | 0:8f0bb79ddd48 | 517 | char temp[REGISTRY_FLOAT_STRING_MAX_LEN + 1]; |
leothedragon | 0:8f0bb79ddd48 | 518 | |
leothedragon | 0:8f0bb79ddd48 | 519 | memcpy(temp, value_string, value_len); |
leothedragon | 0:8f0bb79ddd48 | 520 | temp[value_len] = 0; |
leothedragon | 0:8f0bb79ddd48 | 521 | |
leothedragon | 0:8f0bb79ddd48 | 522 | value_float = atof(temp); |
leothedragon | 0:8f0bb79ddd48 | 523 | } |
leothedragon | 0:8f0bb79ddd48 | 524 | |
leothedragon | 0:8f0bb79ddd48 | 525 | return value_float; |
leothedragon | 0:8f0bb79ddd48 | 526 | } |
leothedragon | 0:8f0bb79ddd48 | 527 | |
leothedragon | 0:8f0bb79ddd48 | 528 | uint8_t* M2MResourceBase::value() const |
leothedragon | 0:8f0bb79ddd48 | 529 | { |
leothedragon | 0:8f0bb79ddd48 | 530 | return get_nsdl_resource()->resource; |
leothedragon | 0:8f0bb79ddd48 | 531 | } |
leothedragon | 0:8f0bb79ddd48 | 532 | |
leothedragon | 0:8f0bb79ddd48 | 533 | uint32_t M2MResourceBase::value_length() const |
leothedragon | 0:8f0bb79ddd48 | 534 | { |
leothedragon | 0:8f0bb79ddd48 | 535 | return get_nsdl_resource()->resource_len; |
leothedragon | 0:8f0bb79ddd48 | 536 | } |
leothedragon | 0:8f0bb79ddd48 | 537 | |
leothedragon | 0:8f0bb79ddd48 | 538 | void M2MResourceBase::set_value_set_callback(value_set_callback callback) |
leothedragon | 0:8f0bb79ddd48 | 539 | { |
leothedragon | 0:8f0bb79ddd48 | 540 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceBaseValueSetCallback); |
leothedragon | 0:8f0bb79ddd48 | 541 | M2MCallbackStorage::add_callback(*this, (void*)callback, M2MCallbackAssociation::M2MResourceBaseValueSetCallback); |
leothedragon | 0:8f0bb79ddd48 | 542 | } |
leothedragon | 0:8f0bb79ddd48 | 543 | |
leothedragon | 0:8f0bb79ddd48 | 544 | sn_coap_hdr_s* M2MResourceBase::handle_get_request(nsdl_s *nsdl, |
leothedragon | 0:8f0bb79ddd48 | 545 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 546 | M2MObservationHandler *observation_handler) |
leothedragon | 0:8f0bb79ddd48 | 547 | { |
leothedragon | 0:8f0bb79ddd48 | 548 | tr_info("M2MResourceBase::handle_get_request()"); |
leothedragon | 0:8f0bb79ddd48 | 549 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT; |
leothedragon | 0:8f0bb79ddd48 | 550 | sn_coap_hdr_s *coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:8f0bb79ddd48 | 551 | received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 552 | msg_code); |
leothedragon | 0:8f0bb79ddd48 | 553 | if (received_coap_header) { |
leothedragon | 0:8f0bb79ddd48 | 554 | // process the GET if we have registered a callback for it |
leothedragon | 0:8f0bb79ddd48 | 555 | if ((operation() & M2MBase::GET_ALLOWED) != 0) { |
leothedragon | 0:8f0bb79ddd48 | 556 | if (coap_response) { |
leothedragon | 0:8f0bb79ddd48 | 557 | bool content_type_present = false; |
leothedragon | 0:8f0bb79ddd48 | 558 | if (received_coap_header->options_list_ptr && |
leothedragon | 0:8f0bb79ddd48 | 559 | received_coap_header->options_list_ptr->accept != COAP_CT_NONE) { |
leothedragon | 0:8f0bb79ddd48 | 560 | content_type_present = true; |
leothedragon | 0:8f0bb79ddd48 | 561 | coap_response->content_format = received_coap_header->options_list_ptr->accept; |
leothedragon | 0:8f0bb79ddd48 | 562 | set_coap_content_type(coap_response->content_format); |
leothedragon | 0:8f0bb79ddd48 | 563 | } |
leothedragon | 0:8f0bb79ddd48 | 564 | if (!content_type_present) { |
leothedragon | 0:8f0bb79ddd48 | 565 | if (resource_instance_type() == M2MResourceInstance::OPAQUE) { |
leothedragon | 0:8f0bb79ddd48 | 566 | coap_response->content_format = sn_coap_content_format_e(COAP_CONTENT_OMA_OPAQUE_TYPE); |
leothedragon | 0:8f0bb79ddd48 | 567 | } else { |
leothedragon | 0:8f0bb79ddd48 | 568 | coap_response->content_format = sn_coap_content_format_e(COAP_CONTENT_OMA_PLAIN_TEXT_TYPE); |
leothedragon | 0:8f0bb79ddd48 | 569 | } |
leothedragon | 0:8f0bb79ddd48 | 570 | } |
leothedragon | 0:8f0bb79ddd48 | 571 | // fill in the CoAP response payload |
leothedragon | 0:8f0bb79ddd48 | 572 | coap_response->payload_ptr = NULL; |
leothedragon | 0:8f0bb79ddd48 | 573 | uint32_t payload_len = 0; |
leothedragon | 0:8f0bb79ddd48 | 574 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 575 | //If handler exists it means that resource value is stored in application side |
leothedragon | 0:8f0bb79ddd48 | 576 | if (block_message() && block_message()->is_block_message()) { |
leothedragon | 0:8f0bb79ddd48 | 577 | outgoing_block_message_callback* outgoing_block_message_cb = (outgoing_block_message_callback*)M2MCallbackStorage::get_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 578 | M2MCallbackAssociation::M2MResourceInstanceOutgoingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 579 | if (outgoing_block_message_cb) { |
leothedragon | 0:8f0bb79ddd48 | 580 | String name = ""; |
leothedragon | 0:8f0bb79ddd48 | 581 | if (received_coap_header->uri_path_ptr != NULL && |
leothedragon | 0:8f0bb79ddd48 | 582 | received_coap_header->uri_path_len > 0) { |
leothedragon | 0:8f0bb79ddd48 | 583 | name.append_raw((char *)received_coap_header->uri_path_ptr, received_coap_header->uri_path_len); |
leothedragon | 0:8f0bb79ddd48 | 584 | } |
leothedragon | 0:8f0bb79ddd48 | 585 | (*outgoing_block_message_cb)(name, coap_response->payload_ptr, payload_len); |
leothedragon | 0:8f0bb79ddd48 | 586 | } |
leothedragon | 0:8f0bb79ddd48 | 587 | } else { |
leothedragon | 0:8f0bb79ddd48 | 588 | #endif |
leothedragon | 0:8f0bb79ddd48 | 589 | if (coap_response->content_format == COAP_CONTENT_OMA_TLV_TYPE || |
leothedragon | 0:8f0bb79ddd48 | 590 | coap_response->content_format == COAP_CONTENT_OMA_TLV_TYPE_OLD) { |
leothedragon | 0:8f0bb79ddd48 | 591 | coap_response->payload_ptr = M2MTLVSerializer::serialize(&get_parent_resource(), payload_len); |
leothedragon | 0:8f0bb79ddd48 | 592 | } else { |
leothedragon | 0:8f0bb79ddd48 | 593 | get_value(coap_response->payload_ptr,payload_len); |
leothedragon | 0:8f0bb79ddd48 | 594 | } |
leothedragon | 0:8f0bb79ddd48 | 595 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 596 | } |
leothedragon | 0:8f0bb79ddd48 | 597 | #endif |
leothedragon | 0:8f0bb79ddd48 | 598 | tr_debug("M2MResourceBase::handle_get_request() - Request Content-type: %d", coap_response->content_format); |
leothedragon | 0:8f0bb79ddd48 | 599 | coap_response->payload_len = payload_len; |
leothedragon | 0:8f0bb79ddd48 | 600 | coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response); |
leothedragon | 0:8f0bb79ddd48 | 601 | if (coap_response->options_list_ptr) { |
leothedragon | 0:8f0bb79ddd48 | 602 | coap_response->options_list_ptr->max_age = max_age(); |
leothedragon | 0:8f0bb79ddd48 | 603 | } |
leothedragon | 0:8f0bb79ddd48 | 604 | |
leothedragon | 0:8f0bb79ddd48 | 605 | if (received_coap_header->options_list_ptr) { |
leothedragon | 0:8f0bb79ddd48 | 606 | if(received_coap_header->options_list_ptr->observe != -1) { |
leothedragon | 0:8f0bb79ddd48 | 607 | handle_observation(nsdl, *received_coap_header, *coap_response, observation_handler, msg_code); |
leothedragon | 0:8f0bb79ddd48 | 608 | } |
leothedragon | 0:8f0bb79ddd48 | 609 | } |
leothedragon | 0:8f0bb79ddd48 | 610 | } |
leothedragon | 0:8f0bb79ddd48 | 611 | } else { |
leothedragon | 0:8f0bb79ddd48 | 612 | tr_error("M2MResourceBase::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED"); |
leothedragon | 0:8f0bb79ddd48 | 613 | // Operation is not allowed. |
leothedragon | 0:8f0bb79ddd48 | 614 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 615 | } |
leothedragon | 0:8f0bb79ddd48 | 616 | } else { |
leothedragon | 0:8f0bb79ddd48 | 617 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 618 | } |
leothedragon | 0:8f0bb79ddd48 | 619 | |
leothedragon | 0:8f0bb79ddd48 | 620 | if (coap_response) { |
leothedragon | 0:8f0bb79ddd48 | 621 | coap_response->msg_code = msg_code; |
leothedragon | 0:8f0bb79ddd48 | 622 | } |
leothedragon | 0:8f0bb79ddd48 | 623 | |
leothedragon | 0:8f0bb79ddd48 | 624 | return coap_response; |
leothedragon | 0:8f0bb79ddd48 | 625 | } |
leothedragon | 0:8f0bb79ddd48 | 626 | |
leothedragon | 0:8f0bb79ddd48 | 627 | sn_coap_hdr_s* M2MResourceBase::handle_put_request(nsdl_s *nsdl, |
leothedragon | 0:8f0bb79ddd48 | 628 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 629 | M2MObservationHandler *observation_handler, |
leothedragon | 0:8f0bb79ddd48 | 630 | bool &execute_value_updated) |
leothedragon | 0:8f0bb79ddd48 | 631 | { |
leothedragon | 0:8f0bb79ddd48 | 632 | tr_info("M2MResourceBase::handle_put_request()"); |
leothedragon | 0:8f0bb79ddd48 | 633 | |
leothedragon | 0:8f0bb79ddd48 | 634 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04 |
leothedragon | 0:8f0bb79ddd48 | 635 | sn_coap_hdr_s *coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:8f0bb79ddd48 | 636 | received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 637 | msg_code); |
leothedragon | 0:8f0bb79ddd48 | 638 | // process the PUT if we have registered a callback for it |
leothedragon | 0:8f0bb79ddd48 | 639 | if(received_coap_header && coap_response) { |
leothedragon | 0:8f0bb79ddd48 | 640 | uint16_t coap_content_type = 0; |
leothedragon | 0:8f0bb79ddd48 | 641 | if(received_coap_header->content_format != COAP_CT_NONE) { |
leothedragon | 0:8f0bb79ddd48 | 642 | coap_content_type = received_coap_header->content_format; |
leothedragon | 0:8f0bb79ddd48 | 643 | } |
leothedragon | 0:8f0bb79ddd48 | 644 | if(received_coap_header->options_list_ptr && |
leothedragon | 0:8f0bb79ddd48 | 645 | received_coap_header->options_list_ptr->uri_query_ptr) { |
leothedragon | 0:8f0bb79ddd48 | 646 | char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr, |
leothedragon | 0:8f0bb79ddd48 | 647 | received_coap_header->options_list_ptr->uri_query_len); |
leothedragon | 0:8f0bb79ddd48 | 648 | if (query){ |
leothedragon | 0:8f0bb79ddd48 | 649 | tr_info("M2MResourceBase::handle_put_request() - query %s", query); |
leothedragon | 0:8f0bb79ddd48 | 650 | |
leothedragon | 0:8f0bb79ddd48 | 651 | // if anything was updated, re-initialize the stored notification attributes |
leothedragon | 0:8f0bb79ddd48 | 652 | if (!handle_observation_attribute(query)){ |
leothedragon | 0:8f0bb79ddd48 | 653 | tr_error("M2MResourceBase::handle_put_request() - Invalid query"); |
leothedragon | 0:8f0bb79ddd48 | 654 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; |
leothedragon | 0:8f0bb79ddd48 | 655 | } |
leothedragon | 0:8f0bb79ddd48 | 656 | free(query); |
leothedragon | 0:8f0bb79ddd48 | 657 | } |
leothedragon | 0:8f0bb79ddd48 | 658 | else { |
leothedragon | 0:8f0bb79ddd48 | 659 | // memory allocation for query fails |
leothedragon | 0:8f0bb79ddd48 | 660 | tr_error("M2MResourceBase::handle_put_request() - Out of memory !!!"); |
leothedragon | 0:8f0bb79ddd48 | 661 | msg_code = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR; // 4.00 |
leothedragon | 0:8f0bb79ddd48 | 662 | } |
leothedragon | 0:8f0bb79ddd48 | 663 | } else if ((operation() & M2MBase::PUT_ALLOWED) != 0) { |
leothedragon | 0:8f0bb79ddd48 | 664 | tr_debug("M2MResourceBase::handle_put_request() - Request Content-type: %d", coap_content_type); |
leothedragon | 0:8f0bb79ddd48 | 665 | |
leothedragon | 0:8f0bb79ddd48 | 666 | if(COAP_CONTENT_OMA_OPAQUE_TYPE != coap_content_type && |
leothedragon | 0:8f0bb79ddd48 | 667 | COAP_CONTENT_OMA_PLAIN_TEXT_TYPE != coap_content_type) { |
leothedragon | 0:8f0bb79ddd48 | 668 | msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; |
leothedragon | 0:8f0bb79ddd48 | 669 | } else { |
leothedragon | 0:8f0bb79ddd48 | 670 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 671 | if (block_message()) { |
leothedragon | 0:8f0bb79ddd48 | 672 | block_message()->set_message_info(received_coap_header); |
leothedragon | 0:8f0bb79ddd48 | 673 | if (block_message()->is_block_message()) { |
leothedragon | 0:8f0bb79ddd48 | 674 | incoming_block_message_callback* incoming_block_message_cb = (incoming_block_message_callback*)M2MCallbackStorage::get_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 675 | M2MCallbackAssociation::M2MResourceInstanceIncomingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 676 | if (incoming_block_message_cb) { |
leothedragon | 0:8f0bb79ddd48 | 677 | (*incoming_block_message_cb)(_block_message_data); |
leothedragon | 0:8f0bb79ddd48 | 678 | } |
leothedragon | 0:8f0bb79ddd48 | 679 | if (block_message()->is_last_block()) { |
leothedragon | 0:8f0bb79ddd48 | 680 | block_message()->clear_values(); |
leothedragon | 0:8f0bb79ddd48 | 681 | coap_response->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED; |
leothedragon | 0:8f0bb79ddd48 | 682 | } else { |
leothedragon | 0:8f0bb79ddd48 | 683 | coap_response->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING; |
leothedragon | 0:8f0bb79ddd48 | 684 | } |
leothedragon | 0:8f0bb79ddd48 | 685 | if (block_message()->error_code() != M2MBlockMessage::ErrorNone) { |
leothedragon | 0:8f0bb79ddd48 | 686 | block_message()->clear_values(); |
leothedragon | 0:8f0bb79ddd48 | 687 | } |
leothedragon | 0:8f0bb79ddd48 | 688 | } |
leothedragon | 0:8f0bb79ddd48 | 689 | } |
leothedragon | 0:8f0bb79ddd48 | 690 | #endif |
leothedragon | 0:8f0bb79ddd48 | 691 | // Firmware object uri path is limited to be max 255 bytes |
leothedragon | 0:8f0bb79ddd48 | 692 | if ((strcmp(uri_path(), FIRMAWARE_PACKAGE_URI_PATH) == 0) && |
leothedragon | 0:8f0bb79ddd48 | 693 | received_coap_header->payload_len > 255) { |
leothedragon | 0:8f0bb79ddd48 | 694 | msg_code = COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE; |
leothedragon | 0:8f0bb79ddd48 | 695 | } else if ((strcmp(uri_path(), SERVER_LIFETIME_PATH) == 0)) { |
leothedragon | 0:8f0bb79ddd48 | 696 | // Check that lifetime can't go below 60s |
leothedragon | 0:8f0bb79ddd48 | 697 | if(received_coap_header->payload_ptr) { |
leothedragon | 0:8f0bb79ddd48 | 698 | int64_t lifetime; |
leothedragon | 0:8f0bb79ddd48 | 699 | bool success = String::convert_ascii_to_int((char*)received_coap_header->payload_ptr, received_coap_header->payload_len, lifetime); |
leothedragon | 0:8f0bb79ddd48 | 700 | if ((success == false) || (lifetime < MINIMUM_REGISTRATION_TIME)) { |
leothedragon | 0:8f0bb79ddd48 | 701 | tr_error("M2MResourceBase::handle_put_request() - lifetime value % " PRId32 " not acceptable", lifetime); |
leothedragon | 0:8f0bb79ddd48 | 702 | msg_code = COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE; |
leothedragon | 0:8f0bb79ddd48 | 703 | } |
leothedragon | 0:8f0bb79ddd48 | 704 | } else { |
leothedragon | 0:8f0bb79ddd48 | 705 | tr_error("M2MResourceBase::handle_put_request() - empty lifetime payload not acceptable"); |
leothedragon | 0:8f0bb79ddd48 | 706 | msg_code = COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE; |
leothedragon | 0:8f0bb79ddd48 | 707 | } |
leothedragon | 0:8f0bb79ddd48 | 708 | } |
leothedragon | 0:8f0bb79ddd48 | 709 | |
leothedragon | 0:8f0bb79ddd48 | 710 | // Do not update resource value in error case. |
leothedragon | 0:8f0bb79ddd48 | 711 | if ((received_coap_header->payload_ptr) && (msg_code == COAP_MSG_CODE_RESPONSE_CHANGED)) { |
leothedragon | 0:8f0bb79ddd48 | 712 | execute_value_updated = true; |
leothedragon | 0:8f0bb79ddd48 | 713 | } |
leothedragon | 0:8f0bb79ddd48 | 714 | } |
leothedragon | 0:8f0bb79ddd48 | 715 | } else { |
leothedragon | 0:8f0bb79ddd48 | 716 | // Operation is not allowed. |
leothedragon | 0:8f0bb79ddd48 | 717 | tr_error("M2MResourceBase::handle_put_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED"); |
leothedragon | 0:8f0bb79ddd48 | 718 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 719 | } |
leothedragon | 0:8f0bb79ddd48 | 720 | } else { |
leothedragon | 0:8f0bb79ddd48 | 721 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 722 | } |
leothedragon | 0:8f0bb79ddd48 | 723 | if(coap_response) { |
leothedragon | 0:8f0bb79ddd48 | 724 | coap_response->msg_code = msg_code; |
leothedragon | 0:8f0bb79ddd48 | 725 | } |
leothedragon | 0:8f0bb79ddd48 | 726 | |
leothedragon | 0:8f0bb79ddd48 | 727 | return coap_response; |
leothedragon | 0:8f0bb79ddd48 | 728 | } |
leothedragon | 0:8f0bb79ddd48 | 729 | |
leothedragon | 0:8f0bb79ddd48 | 730 | |
leothedragon | 0:8f0bb79ddd48 | 731 | #ifndef DISABLE_BLOCK_MESSAGE |
leothedragon | 0:8f0bb79ddd48 | 732 | |
leothedragon | 0:8f0bb79ddd48 | 733 | M2MBlockMessage* M2MResourceBase::block_message() const |
leothedragon | 0:8f0bb79ddd48 | 734 | { |
leothedragon | 0:8f0bb79ddd48 | 735 | return _block_message_data; |
leothedragon | 0:8f0bb79ddd48 | 736 | } |
leothedragon | 0:8f0bb79ddd48 | 737 | |
leothedragon | 0:8f0bb79ddd48 | 738 | bool M2MResourceBase::set_incoming_block_message_callback(incoming_block_message_callback callback) |
leothedragon | 0:8f0bb79ddd48 | 739 | { |
leothedragon | 0:8f0bb79ddd48 | 740 | incoming_block_message_callback* old_callback = (incoming_block_message_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 741 | M2MCallbackAssociation::M2MResourceInstanceIncomingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 742 | delete old_callback; |
leothedragon | 0:8f0bb79ddd48 | 743 | |
leothedragon | 0:8f0bb79ddd48 | 744 | // copy the callback object. This will change on next version to be a direct pointer to a interface class, |
leothedragon | 0:8f0bb79ddd48 | 745 | // this FPn<> is just too heavy for this usage. |
leothedragon | 0:8f0bb79ddd48 | 746 | incoming_block_message_callback* new_callback = new incoming_block_message_callback(callback); |
leothedragon | 0:8f0bb79ddd48 | 747 | |
leothedragon | 0:8f0bb79ddd48 | 748 | delete _block_message_data; |
leothedragon | 0:8f0bb79ddd48 | 749 | _block_message_data = NULL; |
leothedragon | 0:8f0bb79ddd48 | 750 | _block_message_data = new M2MBlockMessage(); |
leothedragon | 0:8f0bb79ddd48 | 751 | |
leothedragon | 0:8f0bb79ddd48 | 752 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 753 | new_callback, |
leothedragon | 0:8f0bb79ddd48 | 754 | M2MCallbackAssociation::M2MResourceInstanceIncomingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 755 | } |
leothedragon | 0:8f0bb79ddd48 | 756 | |
leothedragon | 0:8f0bb79ddd48 | 757 | bool M2MResourceBase::set_outgoing_block_message_callback(outgoing_block_message_callback callback) |
leothedragon | 0:8f0bb79ddd48 | 758 | { |
leothedragon | 0:8f0bb79ddd48 | 759 | outgoing_block_message_callback *old_callback = (outgoing_block_message_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 760 | M2MCallbackAssociation::M2MResourceInstanceOutgoingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 761 | delete old_callback; |
leothedragon | 0:8f0bb79ddd48 | 762 | |
leothedragon | 0:8f0bb79ddd48 | 763 | outgoing_block_message_callback *new_callback = new outgoing_block_message_callback(callback); |
leothedragon | 0:8f0bb79ddd48 | 764 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 765 | new_callback, |
leothedragon | 0:8f0bb79ddd48 | 766 | M2MCallbackAssociation::M2MResourceInstanceOutgoingBlockMessageCallback); |
leothedragon | 0:8f0bb79ddd48 | 767 | } |
leothedragon | 0:8f0bb79ddd48 | 768 | #endif |
leothedragon | 0:8f0bb79ddd48 | 769 | |
leothedragon | 0:8f0bb79ddd48 | 770 | bool M2MResourceBase::set_notification_sent_callback(notification_sent_callback callback) |
leothedragon | 0:8f0bb79ddd48 | 771 | { |
leothedragon | 0:8f0bb79ddd48 | 772 | notification_sent_callback *old_callback = (notification_sent_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 773 | M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback); |
leothedragon | 0:8f0bb79ddd48 | 774 | delete old_callback; |
leothedragon | 0:8f0bb79ddd48 | 775 | |
leothedragon | 0:8f0bb79ddd48 | 776 | notification_sent_callback *new_callback = new notification_sent_callback(callback); |
leothedragon | 0:8f0bb79ddd48 | 777 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 778 | new_callback, |
leothedragon | 0:8f0bb79ddd48 | 779 | M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback); |
leothedragon | 0:8f0bb79ddd48 | 780 | } |
leothedragon | 0:8f0bb79ddd48 | 781 | |
leothedragon | 0:8f0bb79ddd48 | 782 | bool M2MResourceBase::set_notification_sent_callback(notification_sent_callback_2 callback) |
leothedragon | 0:8f0bb79ddd48 | 783 | { |
leothedragon | 0:8f0bb79ddd48 | 784 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback2); |
leothedragon | 0:8f0bb79ddd48 | 785 | |
leothedragon | 0:8f0bb79ddd48 | 786 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 787 | (void*)callback, |
leothedragon | 0:8f0bb79ddd48 | 788 | M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback2); |
leothedragon | 0:8f0bb79ddd48 | 789 | } |
leothedragon | 0:8f0bb79ddd48 | 790 | |
leothedragon | 0:8f0bb79ddd48 | 791 | bool M2MResourceBase::set_notification_status_callback(notification_status_callback callback) |
leothedragon | 0:8f0bb79ddd48 | 792 | { |
leothedragon | 0:8f0bb79ddd48 | 793 | notification_status_callback *old_callback = (notification_status_callback*)M2MCallbackStorage::remove_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 794 | M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback); |
leothedragon | 0:8f0bb79ddd48 | 795 | delete old_callback; |
leothedragon | 0:8f0bb79ddd48 | 796 | |
leothedragon | 0:8f0bb79ddd48 | 797 | notification_status_callback *new_callback = new notification_status_callback(callback); |
leothedragon | 0:8f0bb79ddd48 | 798 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 799 | new_callback, |
leothedragon | 0:8f0bb79ddd48 | 800 | M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback); |
leothedragon | 0:8f0bb79ddd48 | 801 | } |
leothedragon | 0:8f0bb79ddd48 | 802 | |
leothedragon | 0:8f0bb79ddd48 | 803 | bool M2MResourceBase::set_notification_status_callback(notification_status_callback_2 callback) |
leothedragon | 0:8f0bb79ddd48 | 804 | { |
leothedragon | 0:8f0bb79ddd48 | 805 | M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback2); |
leothedragon | 0:8f0bb79ddd48 | 806 | |
leothedragon | 0:8f0bb79ddd48 | 807 | return M2MCallbackStorage::add_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 808 | (void*)callback, |
leothedragon | 0:8f0bb79ddd48 | 809 | M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback2); |
leothedragon | 0:8f0bb79ddd48 | 810 | } |
leothedragon | 0:8f0bb79ddd48 | 811 | |
leothedragon | 0:8f0bb79ddd48 | 812 | void M2MResourceBase::notification_sent() |
leothedragon | 0:8f0bb79ddd48 | 813 | { |
leothedragon | 0:8f0bb79ddd48 | 814 | // Now we will call both callbacks, if they are set. This is different from original behavior. |
leothedragon | 0:8f0bb79ddd48 | 815 | notification_sent_callback* callback = |
leothedragon | 0:8f0bb79ddd48 | 816 | (notification_sent_callback*)M2MCallbackStorage::get_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 817 | M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback); |
leothedragon | 0:8f0bb79ddd48 | 818 | if (callback) { |
leothedragon | 0:8f0bb79ddd48 | 819 | (*callback)(); |
leothedragon | 0:8f0bb79ddd48 | 820 | } |
leothedragon | 0:8f0bb79ddd48 | 821 | |
leothedragon | 0:8f0bb79ddd48 | 822 | notification_sent_callback_2 callback2 = |
leothedragon | 0:8f0bb79ddd48 | 823 | (notification_sent_callback_2)M2MCallbackStorage::get_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 824 | M2MCallbackAssociation::M2MResourceInstanceNotificationSentCallback2); |
leothedragon | 0:8f0bb79ddd48 | 825 | if (callback2) { |
leothedragon | 0:8f0bb79ddd48 | 826 | (*callback2)(); |
leothedragon | 0:8f0bb79ddd48 | 827 | } |
leothedragon | 0:8f0bb79ddd48 | 828 | } |
leothedragon | 0:8f0bb79ddd48 | 829 | |
leothedragon | 0:8f0bb79ddd48 | 830 | void M2MResourceBase::notification_status(const uint16_t msg_id, const NotificationStatus status) |
leothedragon | 0:8f0bb79ddd48 | 831 | { |
leothedragon | 0:8f0bb79ddd48 | 832 | if (_notification_status != status) { |
leothedragon | 0:8f0bb79ddd48 | 833 | _notification_status = status; |
leothedragon | 0:8f0bb79ddd48 | 834 | // Now we will call both callbacks, if they are set. This is different from original behavior. |
leothedragon | 0:8f0bb79ddd48 | 835 | notification_status_callback* callback = |
leothedragon | 0:8f0bb79ddd48 | 836 | (notification_status_callback*)M2MCallbackStorage::get_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 837 | M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback); |
leothedragon | 0:8f0bb79ddd48 | 838 | if (callback) { |
leothedragon | 0:8f0bb79ddd48 | 839 | (*callback)(msg_id, status); |
leothedragon | 0:8f0bb79ddd48 | 840 | } |
leothedragon | 0:8f0bb79ddd48 | 841 | |
leothedragon | 0:8f0bb79ddd48 | 842 | notification_status_callback_2 callback2 = |
leothedragon | 0:8f0bb79ddd48 | 843 | (notification_status_callback_2)M2MCallbackStorage::get_callback(*this, |
leothedragon | 0:8f0bb79ddd48 | 844 | M2MCallbackAssociation::M2MResourceInstanceNotificationStatusCallback2); |
leothedragon | 0:8f0bb79ddd48 | 845 | if (callback2) { |
leothedragon | 0:8f0bb79ddd48 | 846 | (*callback2)(msg_id, status); |
leothedragon | 0:8f0bb79ddd48 | 847 | } |
leothedragon | 0:8f0bb79ddd48 | 848 | } |
leothedragon | 0:8f0bb79ddd48 | 849 | } |
leothedragon | 0:8f0bb79ddd48 | 850 | |
leothedragon | 0:8f0bb79ddd48 | 851 | M2MResourceBase::ResourceType M2MResourceBase::convert_data_type(M2MBase::DataType type) const |
leothedragon | 0:8f0bb79ddd48 | 852 | { |
leothedragon | 0:8f0bb79ddd48 | 853 | M2MResourceBase::ResourceType res_type = M2MResourceBase::OBJLINK; |
leothedragon | 0:8f0bb79ddd48 | 854 | switch(type) { |
leothedragon | 0:8f0bb79ddd48 | 855 | case M2MBase::STRING: |
leothedragon | 0:8f0bb79ddd48 | 856 | res_type = M2MResourceBase::STRING; |
leothedragon | 0:8f0bb79ddd48 | 857 | break; |
leothedragon | 0:8f0bb79ddd48 | 858 | case M2MBase::INTEGER: |
leothedragon | 0:8f0bb79ddd48 | 859 | res_type = M2MResourceBase::INTEGER; |
leothedragon | 0:8f0bb79ddd48 | 860 | break; |
leothedragon | 0:8f0bb79ddd48 | 861 | case M2MBase::FLOAT: |
leothedragon | 0:8f0bb79ddd48 | 862 | res_type = M2MResourceBase::FLOAT; |
leothedragon | 0:8f0bb79ddd48 | 863 | break; |
leothedragon | 0:8f0bb79ddd48 | 864 | case M2MBase::OPAQUE: |
leothedragon | 0:8f0bb79ddd48 | 865 | res_type = M2MResourceBase::OPAQUE; |
leothedragon | 0:8f0bb79ddd48 | 866 | break; |
leothedragon | 0:8f0bb79ddd48 | 867 | case M2MBase::BOOLEAN: |
leothedragon | 0:8f0bb79ddd48 | 868 | res_type = M2MResourceBase::BOOLEAN; |
leothedragon | 0:8f0bb79ddd48 | 869 | break; |
leothedragon | 0:8f0bb79ddd48 | 870 | case M2MBase::TIME: |
leothedragon | 0:8f0bb79ddd48 | 871 | res_type = M2MResourceBase::TIME; |
leothedragon | 0:8f0bb79ddd48 | 872 | break; |
leothedragon | 0:8f0bb79ddd48 | 873 | case M2MBase::OBJLINK: |
leothedragon | 0:8f0bb79ddd48 | 874 | res_type = M2MResourceBase::OBJLINK; |
leothedragon | 0:8f0bb79ddd48 | 875 | break; |
leothedragon | 0:8f0bb79ddd48 | 876 | } |
leothedragon | 0:8f0bb79ddd48 | 877 | return res_type; |
leothedragon | 0:8f0bb79ddd48 | 878 | } |
leothedragon | 0:8f0bb79ddd48 | 879 | |
leothedragon | 0:8f0bb79ddd48 | 880 | M2MResourceBase::NotificationStatus M2MResourceBase::notification_status() const |
leothedragon | 0:8f0bb79ddd48 | 881 | { |
leothedragon | 0:8f0bb79ddd48 | 882 | return _notification_status; |
leothedragon | 0:8f0bb79ddd48 | 883 | } |
leothedragon | 0:8f0bb79ddd48 | 884 | |
leothedragon | 0:8f0bb79ddd48 | 885 | void M2MResourceBase::clear_notification_status() |
leothedragon | 0:8f0bb79ddd48 | 886 | { |
leothedragon | 0:8f0bb79ddd48 | 887 | _notification_status = M2MResourceBase::INIT; |
leothedragon | 0:8f0bb79ddd48 | 888 | } |
leothedragon | 0:8f0bb79ddd48 | 889 | |
leothedragon | 0:8f0bb79ddd48 | 890 | void M2MResourceBase::publish_value_in_registration_msg(bool publish_value) |
leothedragon | 0:8f0bb79ddd48 | 891 | { |
leothedragon | 0:8f0bb79ddd48 | 892 | M2MBase::lwm2m_parameters_s* param = M2MBase::get_lwm2m_parameters(); |
leothedragon | 0:8f0bb79ddd48 | 893 | assert(param->data_type == M2MBase::INTEGER || |
leothedragon | 0:8f0bb79ddd48 | 894 | param->data_type == M2MBase::STRING || |
leothedragon | 0:8f0bb79ddd48 | 895 | param->data_type == M2MBase::FLOAT || |
leothedragon | 0:8f0bb79ddd48 | 896 | param->data_type == M2MBase::BOOLEAN || |
leothedragon | 0:8f0bb79ddd48 | 897 | param->data_type == M2MBase::OPAQUE); |
leothedragon | 0:8f0bb79ddd48 | 898 | |
leothedragon | 0:8f0bb79ddd48 | 899 | uint8_t pub_value = publish_value; |
leothedragon | 0:8f0bb79ddd48 | 900 | |
leothedragon | 0:8f0bb79ddd48 | 901 | if (param->data_type == M2MBase::OPAQUE) { |
leothedragon | 0:8f0bb79ddd48 | 902 | pub_value = 2; |
leothedragon | 0:8f0bb79ddd48 | 903 | } else { |
leothedragon | 0:8f0bb79ddd48 | 904 | pub_value = (uint8_t)publish_value; |
leothedragon | 0:8f0bb79ddd48 | 905 | } |
leothedragon | 0:8f0bb79ddd48 | 906 | param->dynamic_resource_params->publish_value = pub_value; |
leothedragon | 0:8f0bb79ddd48 | 907 | } |