leo hendrickson
/
S
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mserver.cpp@0:25fa8795676b, 2021-04-18 (annotated)
- Committer:
- leothedragon
- Date:
- Sun Apr 18 15:20:23 2021 +0000
- Revision:
- 0:25fa8795676b
DS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:25fa8795676b | 1 | /* |
leothedragon | 0:25fa8795676b | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:25fa8795676b | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:25fa8795676b | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:25fa8795676b | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:25fa8795676b | 6 | * You may obtain a copy of the License at |
leothedragon | 0:25fa8795676b | 7 | * |
leothedragon | 0:25fa8795676b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:25fa8795676b | 9 | * |
leothedragon | 0:25fa8795676b | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:25fa8795676b | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:25fa8795676b | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:25fa8795676b | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:25fa8795676b | 14 | * limitations under the License. |
leothedragon | 0:25fa8795676b | 15 | */ |
leothedragon | 0:25fa8795676b | 16 | |
leothedragon | 0:25fa8795676b | 17 | #include "mbed-client/m2mserver.h" |
leothedragon | 0:25fa8795676b | 18 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:25fa8795676b | 19 | #include "mbed-client/m2mobject.h" |
leothedragon | 0:25fa8795676b | 20 | #include "mbed-client/m2mobjectinstance.h" |
leothedragon | 0:25fa8795676b | 21 | #include "mbed-client/m2mresource.h" |
leothedragon | 0:25fa8795676b | 22 | #include "mbed-client/m2mstring.h" |
leothedragon | 0:25fa8795676b | 23 | |
leothedragon | 0:25fa8795676b | 24 | #define TRACE_GROUP "mClt" |
leothedragon | 0:25fa8795676b | 25 | |
leothedragon | 0:25fa8795676b | 26 | #define BUFFER_SIZE 21 |
leothedragon | 0:25fa8795676b | 27 | |
leothedragon | 0:25fa8795676b | 28 | M2MServer::M2MServer() |
leothedragon | 0:25fa8795676b | 29 | : M2MObject(M2M_SERVER_ID, stringdup(M2M_SERVER_ID)) |
leothedragon | 0:25fa8795676b | 30 | { |
leothedragon | 0:25fa8795676b | 31 | M2MObject::create_object_instance(); |
leothedragon | 0:25fa8795676b | 32 | |
leothedragon | 0:25fa8795676b | 33 | _server_instance = object_instance(); |
leothedragon | 0:25fa8795676b | 34 | |
leothedragon | 0:25fa8795676b | 35 | if(_server_instance) { |
leothedragon | 0:25fa8795676b | 36 | |
leothedragon | 0:25fa8795676b | 37 | M2MResource* res = _server_instance->create_dynamic_resource(SERVER_SHORT_SERVER_ID, |
leothedragon | 0:25fa8795676b | 38 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 39 | M2MResourceInstance::INTEGER, |
leothedragon | 0:25fa8795676b | 40 | true); |
leothedragon | 0:25fa8795676b | 41 | if(res) { |
leothedragon | 0:25fa8795676b | 42 | res->set_operation(M2MBase::GET_PUT_ALLOWED); |
leothedragon | 0:25fa8795676b | 43 | } |
leothedragon | 0:25fa8795676b | 44 | res = _server_instance->create_dynamic_resource(SERVER_LIFETIME, |
leothedragon | 0:25fa8795676b | 45 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 46 | M2MResourceInstance::INTEGER, |
leothedragon | 0:25fa8795676b | 47 | true); |
leothedragon | 0:25fa8795676b | 48 | if(res) { |
leothedragon | 0:25fa8795676b | 49 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
leothedragon | 0:25fa8795676b | 50 | } |
leothedragon | 0:25fa8795676b | 51 | res = _server_instance->create_dynamic_resource(SERVER_NOTIFICATION_STORAGE, |
leothedragon | 0:25fa8795676b | 52 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 53 | M2MResourceInstance::BOOLEAN, |
leothedragon | 0:25fa8795676b | 54 | true); |
leothedragon | 0:25fa8795676b | 55 | if(res) { |
leothedragon | 0:25fa8795676b | 56 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
leothedragon | 0:25fa8795676b | 57 | } |
leothedragon | 0:25fa8795676b | 58 | res = _server_instance->create_dynamic_resource(SERVER_BINDING, |
leothedragon | 0:25fa8795676b | 59 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 60 | M2MResourceInstance::STRING, |
leothedragon | 0:25fa8795676b | 61 | true); |
leothedragon | 0:25fa8795676b | 62 | if(res) { |
leothedragon | 0:25fa8795676b | 63 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
leothedragon | 0:25fa8795676b | 64 | } |
leothedragon | 0:25fa8795676b | 65 | res = _server_instance->create_dynamic_resource(SERVER_REGISTRATION_UPDATE, |
leothedragon | 0:25fa8795676b | 66 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 67 | M2MResourceInstance::OPAQUE, |
leothedragon | 0:25fa8795676b | 68 | false); |
leothedragon | 0:25fa8795676b | 69 | if(res) { |
leothedragon | 0:25fa8795676b | 70 | res->set_operation(M2MBase::POST_ALLOWED); |
leothedragon | 0:25fa8795676b | 71 | } |
leothedragon | 0:25fa8795676b | 72 | } |
leothedragon | 0:25fa8795676b | 73 | } |
leothedragon | 0:25fa8795676b | 74 | |
leothedragon | 0:25fa8795676b | 75 | M2MServer::~M2MServer() |
leothedragon | 0:25fa8795676b | 76 | { |
leothedragon | 0:25fa8795676b | 77 | |
leothedragon | 0:25fa8795676b | 78 | } |
leothedragon | 0:25fa8795676b | 79 | |
leothedragon | 0:25fa8795676b | 80 | M2MResource* M2MServer::create_resource(ServerResource resource, uint32_t value) |
leothedragon | 0:25fa8795676b | 81 | { |
leothedragon | 0:25fa8795676b | 82 | M2MResource* res = NULL; |
leothedragon | 0:25fa8795676b | 83 | const char* server_id_ptr = ""; |
leothedragon | 0:25fa8795676b | 84 | if(!is_resource_present(resource)) { |
leothedragon | 0:25fa8795676b | 85 | switch(resource) { |
leothedragon | 0:25fa8795676b | 86 | case DefaultMinPeriod: |
leothedragon | 0:25fa8795676b | 87 | server_id_ptr = SERVER_DEFAULT_MIN_PERIOD; |
leothedragon | 0:25fa8795676b | 88 | break; |
leothedragon | 0:25fa8795676b | 89 | case DefaultMaxPeriod: |
leothedragon | 0:25fa8795676b | 90 | server_id_ptr = SERVER_DEFAULT_MAX_PERIOD; |
leothedragon | 0:25fa8795676b | 91 | break; |
leothedragon | 0:25fa8795676b | 92 | case DisableTimeout: |
leothedragon | 0:25fa8795676b | 93 | server_id_ptr = SERVER_DISABLE_TIMEOUT; |
leothedragon | 0:25fa8795676b | 94 | break; |
leothedragon | 0:25fa8795676b | 95 | default: |
leothedragon | 0:25fa8795676b | 96 | break; |
leothedragon | 0:25fa8795676b | 97 | } |
leothedragon | 0:25fa8795676b | 98 | } |
leothedragon | 0:25fa8795676b | 99 | String server_id(server_id_ptr); |
leothedragon | 0:25fa8795676b | 100 | |
leothedragon | 0:25fa8795676b | 101 | if(!server_id.empty()) { |
leothedragon | 0:25fa8795676b | 102 | if(_server_instance) { |
leothedragon | 0:25fa8795676b | 103 | res = _server_instance->create_dynamic_resource(server_id, |
leothedragon | 0:25fa8795676b | 104 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 105 | M2MResourceInstance::INTEGER, |
leothedragon | 0:25fa8795676b | 106 | true); |
leothedragon | 0:25fa8795676b | 107 | if(res) { |
leothedragon | 0:25fa8795676b | 108 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
leothedragon | 0:25fa8795676b | 109 | |
leothedragon | 0:25fa8795676b | 110 | res->set_value(value); |
leothedragon | 0:25fa8795676b | 111 | } |
leothedragon | 0:25fa8795676b | 112 | } |
leothedragon | 0:25fa8795676b | 113 | } |
leothedragon | 0:25fa8795676b | 114 | return res; |
leothedragon | 0:25fa8795676b | 115 | } |
leothedragon | 0:25fa8795676b | 116 | |
leothedragon | 0:25fa8795676b | 117 | M2MResource* M2MServer::create_resource(ServerResource resource) |
leothedragon | 0:25fa8795676b | 118 | { |
leothedragon | 0:25fa8795676b | 119 | M2MResource* res = NULL; |
leothedragon | 0:25fa8795676b | 120 | if(!is_resource_present(resource)) { |
leothedragon | 0:25fa8795676b | 121 | if(M2MServer::Disable == resource) { |
leothedragon | 0:25fa8795676b | 122 | if(_server_instance) { |
leothedragon | 0:25fa8795676b | 123 | res = _server_instance->create_dynamic_resource(SERVER_DISABLE, |
leothedragon | 0:25fa8795676b | 124 | OMA_RESOURCE_TYPE, |
leothedragon | 0:25fa8795676b | 125 | M2MResourceInstance::OPAQUE, |
leothedragon | 0:25fa8795676b | 126 | false); |
leothedragon | 0:25fa8795676b | 127 | if(res) { |
leothedragon | 0:25fa8795676b | 128 | res->set_operation(M2MBase::POST_ALLOWED); |
leothedragon | 0:25fa8795676b | 129 | } |
leothedragon | 0:25fa8795676b | 130 | } |
leothedragon | 0:25fa8795676b | 131 | } |
leothedragon | 0:25fa8795676b | 132 | } |
leothedragon | 0:25fa8795676b | 133 | return res; |
leothedragon | 0:25fa8795676b | 134 | } |
leothedragon | 0:25fa8795676b | 135 | |
leothedragon | 0:25fa8795676b | 136 | bool M2MServer::delete_resource(ServerResource resource) |
leothedragon | 0:25fa8795676b | 137 | { |
leothedragon | 0:25fa8795676b | 138 | bool success = false; |
leothedragon | 0:25fa8795676b | 139 | const char* server_id_ptr; |
leothedragon | 0:25fa8795676b | 140 | switch(resource) { |
leothedragon | 0:25fa8795676b | 141 | case DefaultMinPeriod: |
leothedragon | 0:25fa8795676b | 142 | server_id_ptr = SERVER_DEFAULT_MIN_PERIOD; |
leothedragon | 0:25fa8795676b | 143 | break; |
leothedragon | 0:25fa8795676b | 144 | case DefaultMaxPeriod: |
leothedragon | 0:25fa8795676b | 145 | server_id_ptr = SERVER_DEFAULT_MAX_PERIOD; |
leothedragon | 0:25fa8795676b | 146 | break; |
leothedragon | 0:25fa8795676b | 147 | case Disable: |
leothedragon | 0:25fa8795676b | 148 | server_id_ptr = SERVER_DISABLE; |
leothedragon | 0:25fa8795676b | 149 | break; |
leothedragon | 0:25fa8795676b | 150 | case DisableTimeout: |
leothedragon | 0:25fa8795676b | 151 | server_id_ptr = SERVER_DISABLE_TIMEOUT; |
leothedragon | 0:25fa8795676b | 152 | break; |
leothedragon | 0:25fa8795676b | 153 | default: |
leothedragon | 0:25fa8795676b | 154 | server_id_ptr = NULL; |
leothedragon | 0:25fa8795676b | 155 | break; |
leothedragon | 0:25fa8795676b | 156 | } |
leothedragon | 0:25fa8795676b | 157 | |
leothedragon | 0:25fa8795676b | 158 | if(server_id_ptr) { |
leothedragon | 0:25fa8795676b | 159 | if(_server_instance) { |
leothedragon | 0:25fa8795676b | 160 | success = _server_instance->remove_resource(server_id_ptr); |
leothedragon | 0:25fa8795676b | 161 | } |
leothedragon | 0:25fa8795676b | 162 | } |
leothedragon | 0:25fa8795676b | 163 | return success; |
leothedragon | 0:25fa8795676b | 164 | } |
leothedragon | 0:25fa8795676b | 165 | |
leothedragon | 0:25fa8795676b | 166 | bool M2MServer::set_resource_value(ServerResource resource, |
leothedragon | 0:25fa8795676b | 167 | const String &value) |
leothedragon | 0:25fa8795676b | 168 | { |
leothedragon | 0:25fa8795676b | 169 | bool success = false; |
leothedragon | 0:25fa8795676b | 170 | M2MResource* res = get_resource(resource); |
leothedragon | 0:25fa8795676b | 171 | if(res && (M2MServer::Binding == resource)) { |
leothedragon | 0:25fa8795676b | 172 | success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); |
leothedragon | 0:25fa8795676b | 173 | } |
leothedragon | 0:25fa8795676b | 174 | return success; |
leothedragon | 0:25fa8795676b | 175 | } |
leothedragon | 0:25fa8795676b | 176 | |
leothedragon | 0:25fa8795676b | 177 | bool M2MServer::set_resource_value(ServerResource resource, |
leothedragon | 0:25fa8795676b | 178 | uint32_t value) |
leothedragon | 0:25fa8795676b | 179 | { |
leothedragon | 0:25fa8795676b | 180 | bool success = false; |
leothedragon | 0:25fa8795676b | 181 | M2MResource* res = get_resource(resource); |
leothedragon | 0:25fa8795676b | 182 | if(res) { |
leothedragon | 0:25fa8795676b | 183 | if(M2MServer::ShortServerID == resource || |
leothedragon | 0:25fa8795676b | 184 | M2MServer::Lifetime == resource || |
leothedragon | 0:25fa8795676b | 185 | M2MServer::DefaultMinPeriod == resource || |
leothedragon | 0:25fa8795676b | 186 | M2MServer::DefaultMaxPeriod == resource || |
leothedragon | 0:25fa8795676b | 187 | M2MServer::DisableTimeout == resource || |
leothedragon | 0:25fa8795676b | 188 | M2MServer::NotificationStorage == resource) { |
leothedragon | 0:25fa8795676b | 189 | // If it is any of the above resource |
leothedragon | 0:25fa8795676b | 190 | // set the value of the resource. |
leothedragon | 0:25fa8795676b | 191 | |
leothedragon | 0:25fa8795676b | 192 | success = res->set_value(value); |
leothedragon | 0:25fa8795676b | 193 | } |
leothedragon | 0:25fa8795676b | 194 | } |
leothedragon | 0:25fa8795676b | 195 | return success; |
leothedragon | 0:25fa8795676b | 196 | } |
leothedragon | 0:25fa8795676b | 197 | |
leothedragon | 0:25fa8795676b | 198 | String M2MServer::resource_value_string(ServerResource resource) const |
leothedragon | 0:25fa8795676b | 199 | { |
leothedragon | 0:25fa8795676b | 200 | String value = ""; |
leothedragon | 0:25fa8795676b | 201 | M2MResource* res = get_resource(resource); |
leothedragon | 0:25fa8795676b | 202 | if(res && (M2MServer::Binding == resource)) { |
leothedragon | 0:25fa8795676b | 203 | |
leothedragon | 0:25fa8795676b | 204 | value = res->get_value_string(); |
leothedragon | 0:25fa8795676b | 205 | } |
leothedragon | 0:25fa8795676b | 206 | return value; |
leothedragon | 0:25fa8795676b | 207 | } |
leothedragon | 0:25fa8795676b | 208 | |
leothedragon | 0:25fa8795676b | 209 | |
leothedragon | 0:25fa8795676b | 210 | uint32_t M2MServer::resource_value_int(ServerResource resource) const |
leothedragon | 0:25fa8795676b | 211 | { |
leothedragon | 0:25fa8795676b | 212 | uint32_t value = 0; |
leothedragon | 0:25fa8795676b | 213 | M2MResource* res = get_resource(resource); |
leothedragon | 0:25fa8795676b | 214 | if(res) { |
leothedragon | 0:25fa8795676b | 215 | if(M2MServer::ShortServerID == resource || |
leothedragon | 0:25fa8795676b | 216 | M2MServer::Lifetime == resource || |
leothedragon | 0:25fa8795676b | 217 | M2MServer::DefaultMinPeriod == resource || |
leothedragon | 0:25fa8795676b | 218 | M2MServer::DefaultMaxPeriod == resource || |
leothedragon | 0:25fa8795676b | 219 | M2MServer::DisableTimeout == resource || |
leothedragon | 0:25fa8795676b | 220 | M2MServer::NotificationStorage == resource) { |
leothedragon | 0:25fa8795676b | 221 | |
leothedragon | 0:25fa8795676b | 222 | value = res->get_value_int(); |
leothedragon | 0:25fa8795676b | 223 | } |
leothedragon | 0:25fa8795676b | 224 | } |
leothedragon | 0:25fa8795676b | 225 | return value; |
leothedragon | 0:25fa8795676b | 226 | } |
leothedragon | 0:25fa8795676b | 227 | |
leothedragon | 0:25fa8795676b | 228 | bool M2MServer::is_resource_present(ServerResource resource) const |
leothedragon | 0:25fa8795676b | 229 | { |
leothedragon | 0:25fa8795676b | 230 | bool success = false; |
leothedragon | 0:25fa8795676b | 231 | M2MResource *res = get_resource(resource); |
leothedragon | 0:25fa8795676b | 232 | if(res) { |
leothedragon | 0:25fa8795676b | 233 | success = true; |
leothedragon | 0:25fa8795676b | 234 | } |
leothedragon | 0:25fa8795676b | 235 | return success; |
leothedragon | 0:25fa8795676b | 236 | } |
leothedragon | 0:25fa8795676b | 237 | |
leothedragon | 0:25fa8795676b | 238 | uint16_t M2MServer::total_resource_count() const |
leothedragon | 0:25fa8795676b | 239 | { |
leothedragon | 0:25fa8795676b | 240 | uint16_t total_count = 0; |
leothedragon | 0:25fa8795676b | 241 | if(_server_instance) { |
leothedragon | 0:25fa8795676b | 242 | total_count = _server_instance->resources().size(); |
leothedragon | 0:25fa8795676b | 243 | } |
leothedragon | 0:25fa8795676b | 244 | return total_count; |
leothedragon | 0:25fa8795676b | 245 | } |
leothedragon | 0:25fa8795676b | 246 | |
leothedragon | 0:25fa8795676b | 247 | M2MResource* M2MServer::get_resource(ServerResource res) const |
leothedragon | 0:25fa8795676b | 248 | { |
leothedragon | 0:25fa8795676b | 249 | M2MResource* res_object = NULL; |
leothedragon | 0:25fa8795676b | 250 | const char* res_name_ptr = NULL; |
leothedragon | 0:25fa8795676b | 251 | switch(res) { |
leothedragon | 0:25fa8795676b | 252 | case ShortServerID: |
leothedragon | 0:25fa8795676b | 253 | res_name_ptr = SERVER_SHORT_SERVER_ID; |
leothedragon | 0:25fa8795676b | 254 | break; |
leothedragon | 0:25fa8795676b | 255 | case Lifetime: |
leothedragon | 0:25fa8795676b | 256 | res_name_ptr = SERVER_LIFETIME; |
leothedragon | 0:25fa8795676b | 257 | break; |
leothedragon | 0:25fa8795676b | 258 | case DefaultMinPeriod: |
leothedragon | 0:25fa8795676b | 259 | res_name_ptr = SERVER_DEFAULT_MIN_PERIOD; |
leothedragon | 0:25fa8795676b | 260 | break; |
leothedragon | 0:25fa8795676b | 261 | case DefaultMaxPeriod: |
leothedragon | 0:25fa8795676b | 262 | res_name_ptr = SERVER_DEFAULT_MAX_PERIOD; |
leothedragon | 0:25fa8795676b | 263 | break; |
leothedragon | 0:25fa8795676b | 264 | case Disable: |
leothedragon | 0:25fa8795676b | 265 | res_name_ptr = SERVER_DISABLE; |
leothedragon | 0:25fa8795676b | 266 | break; |
leothedragon | 0:25fa8795676b | 267 | case DisableTimeout: |
leothedragon | 0:25fa8795676b | 268 | res_name_ptr = SERVER_DISABLE_TIMEOUT; |
leothedragon | 0:25fa8795676b | 269 | break; |
leothedragon | 0:25fa8795676b | 270 | case NotificationStorage: |
leothedragon | 0:25fa8795676b | 271 | res_name_ptr = SERVER_NOTIFICATION_STORAGE; |
leothedragon | 0:25fa8795676b | 272 | break; |
leothedragon | 0:25fa8795676b | 273 | case Binding: |
leothedragon | 0:25fa8795676b | 274 | res_name_ptr = SERVER_BINDING; |
leothedragon | 0:25fa8795676b | 275 | break; |
leothedragon | 0:25fa8795676b | 276 | case RegistrationUpdate: |
leothedragon | 0:25fa8795676b | 277 | res_name_ptr = SERVER_REGISTRATION_UPDATE; |
leothedragon | 0:25fa8795676b | 278 | break; |
leothedragon | 0:25fa8795676b | 279 | } |
leothedragon | 0:25fa8795676b | 280 | |
leothedragon | 0:25fa8795676b | 281 | if(res_name_ptr) { |
leothedragon | 0:25fa8795676b | 282 | if(_server_instance) { |
leothedragon | 0:25fa8795676b | 283 | res_object = _server_instance->resource(res_name_ptr); |
leothedragon | 0:25fa8795676b | 284 | } |
leothedragon | 0:25fa8795676b | 285 | } |
leothedragon | 0:25fa8795676b | 286 | return res_object; |
leothedragon | 0:25fa8795676b | 287 | } |