leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "mbed-client/m2mserver.h"
leothedragon 0:8f0bb79ddd48 18 #include "mbed-client/m2mconstants.h"
leothedragon 0:8f0bb79ddd48 19 #include "mbed-client/m2mobject.h"
leothedragon 0:8f0bb79ddd48 20 #include "mbed-client/m2mobjectinstance.h"
leothedragon 0:8f0bb79ddd48 21 #include "mbed-client/m2mresource.h"
leothedragon 0:8f0bb79ddd48 22 #include "mbed-client/m2mstring.h"
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 #define TRACE_GROUP "mClt"
leothedragon 0:8f0bb79ddd48 25
leothedragon 0:8f0bb79ddd48 26 #define BUFFER_SIZE 21
leothedragon 0:8f0bb79ddd48 27
leothedragon 0:8f0bb79ddd48 28 M2MServer::M2MServer()
leothedragon 0:8f0bb79ddd48 29 : M2MObject(M2M_SERVER_ID, stringdup(M2M_SERVER_ID))
leothedragon 0:8f0bb79ddd48 30 {
leothedragon 0:8f0bb79ddd48 31 M2MObject::create_object_instance();
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 _server_instance = object_instance();
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 if(_server_instance) {
leothedragon 0:8f0bb79ddd48 36
leothedragon 0:8f0bb79ddd48 37 M2MResource* res = _server_instance->create_dynamic_resource(SERVER_SHORT_SERVER_ID,
leothedragon 0:8f0bb79ddd48 38 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 39 M2MResourceInstance::INTEGER,
leothedragon 0:8f0bb79ddd48 40 true);
leothedragon 0:8f0bb79ddd48 41 if(res) {
leothedragon 0:8f0bb79ddd48 42 res->set_operation(M2MBase::GET_PUT_ALLOWED);
leothedragon 0:8f0bb79ddd48 43 }
leothedragon 0:8f0bb79ddd48 44 res = _server_instance->create_dynamic_resource(SERVER_LIFETIME,
leothedragon 0:8f0bb79ddd48 45 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 46 M2MResourceInstance::INTEGER,
leothedragon 0:8f0bb79ddd48 47 true);
leothedragon 0:8f0bb79ddd48 48 if(res) {
leothedragon 0:8f0bb79ddd48 49 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51 res = _server_instance->create_dynamic_resource(SERVER_NOTIFICATION_STORAGE,
leothedragon 0:8f0bb79ddd48 52 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 53 M2MResourceInstance::BOOLEAN,
leothedragon 0:8f0bb79ddd48 54 true);
leothedragon 0:8f0bb79ddd48 55 if(res) {
leothedragon 0:8f0bb79ddd48 56 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
leothedragon 0:8f0bb79ddd48 57 }
leothedragon 0:8f0bb79ddd48 58 res = _server_instance->create_dynamic_resource(SERVER_BINDING,
leothedragon 0:8f0bb79ddd48 59 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 60 M2MResourceInstance::STRING,
leothedragon 0:8f0bb79ddd48 61 true);
leothedragon 0:8f0bb79ddd48 62 if(res) {
leothedragon 0:8f0bb79ddd48 63 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
leothedragon 0:8f0bb79ddd48 64 }
leothedragon 0:8f0bb79ddd48 65 res = _server_instance->create_dynamic_resource(SERVER_REGISTRATION_UPDATE,
leothedragon 0:8f0bb79ddd48 66 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 67 M2MResourceInstance::OPAQUE,
leothedragon 0:8f0bb79ddd48 68 false);
leothedragon 0:8f0bb79ddd48 69 if(res) {
leothedragon 0:8f0bb79ddd48 70 res->set_operation(M2MBase::POST_ALLOWED);
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72 }
leothedragon 0:8f0bb79ddd48 73 }
leothedragon 0:8f0bb79ddd48 74
leothedragon 0:8f0bb79ddd48 75 M2MServer::~M2MServer()
leothedragon 0:8f0bb79ddd48 76 {
leothedragon 0:8f0bb79ddd48 77
leothedragon 0:8f0bb79ddd48 78 }
leothedragon 0:8f0bb79ddd48 79
leothedragon 0:8f0bb79ddd48 80 M2MResource* M2MServer::create_resource(ServerResource resource, uint32_t value)
leothedragon 0:8f0bb79ddd48 81 {
leothedragon 0:8f0bb79ddd48 82 M2MResource* res = NULL;
leothedragon 0:8f0bb79ddd48 83 const char* server_id_ptr = "";
leothedragon 0:8f0bb79ddd48 84 if(!is_resource_present(resource)) {
leothedragon 0:8f0bb79ddd48 85 switch(resource) {
leothedragon 0:8f0bb79ddd48 86 case DefaultMinPeriod:
leothedragon 0:8f0bb79ddd48 87 server_id_ptr = SERVER_DEFAULT_MIN_PERIOD;
leothedragon 0:8f0bb79ddd48 88 break;
leothedragon 0:8f0bb79ddd48 89 case DefaultMaxPeriod:
leothedragon 0:8f0bb79ddd48 90 server_id_ptr = SERVER_DEFAULT_MAX_PERIOD;
leothedragon 0:8f0bb79ddd48 91 break;
leothedragon 0:8f0bb79ddd48 92 case DisableTimeout:
leothedragon 0:8f0bb79ddd48 93 server_id_ptr = SERVER_DISABLE_TIMEOUT;
leothedragon 0:8f0bb79ddd48 94 break;
leothedragon 0:8f0bb79ddd48 95 default:
leothedragon 0:8f0bb79ddd48 96 break;
leothedragon 0:8f0bb79ddd48 97 }
leothedragon 0:8f0bb79ddd48 98 }
leothedragon 0:8f0bb79ddd48 99 String server_id(server_id_ptr);
leothedragon 0:8f0bb79ddd48 100
leothedragon 0:8f0bb79ddd48 101 if(!server_id.empty()) {
leothedragon 0:8f0bb79ddd48 102 if(_server_instance) {
leothedragon 0:8f0bb79ddd48 103 res = _server_instance->create_dynamic_resource(server_id,
leothedragon 0:8f0bb79ddd48 104 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 105 M2MResourceInstance::INTEGER,
leothedragon 0:8f0bb79ddd48 106 true);
leothedragon 0:8f0bb79ddd48 107 if(res) {
leothedragon 0:8f0bb79ddd48 108 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
leothedragon 0:8f0bb79ddd48 109
leothedragon 0:8f0bb79ddd48 110 res->set_value(value);
leothedragon 0:8f0bb79ddd48 111 }
leothedragon 0:8f0bb79ddd48 112 }
leothedragon 0:8f0bb79ddd48 113 }
leothedragon 0:8f0bb79ddd48 114 return res;
leothedragon 0:8f0bb79ddd48 115 }
leothedragon 0:8f0bb79ddd48 116
leothedragon 0:8f0bb79ddd48 117 M2MResource* M2MServer::create_resource(ServerResource resource)
leothedragon 0:8f0bb79ddd48 118 {
leothedragon 0:8f0bb79ddd48 119 M2MResource* res = NULL;
leothedragon 0:8f0bb79ddd48 120 if(!is_resource_present(resource)) {
leothedragon 0:8f0bb79ddd48 121 if(M2MServer::Disable == resource) {
leothedragon 0:8f0bb79ddd48 122 if(_server_instance) {
leothedragon 0:8f0bb79ddd48 123 res = _server_instance->create_dynamic_resource(SERVER_DISABLE,
leothedragon 0:8f0bb79ddd48 124 OMA_RESOURCE_TYPE,
leothedragon 0:8f0bb79ddd48 125 M2MResourceInstance::OPAQUE,
leothedragon 0:8f0bb79ddd48 126 false);
leothedragon 0:8f0bb79ddd48 127 if(res) {
leothedragon 0:8f0bb79ddd48 128 res->set_operation(M2MBase::POST_ALLOWED);
leothedragon 0:8f0bb79ddd48 129 }
leothedragon 0:8f0bb79ddd48 130 }
leothedragon 0:8f0bb79ddd48 131 }
leothedragon 0:8f0bb79ddd48 132 }
leothedragon 0:8f0bb79ddd48 133 return res;
leothedragon 0:8f0bb79ddd48 134 }
leothedragon 0:8f0bb79ddd48 135
leothedragon 0:8f0bb79ddd48 136 bool M2MServer::delete_resource(ServerResource resource)
leothedragon 0:8f0bb79ddd48 137 {
leothedragon 0:8f0bb79ddd48 138 bool success = false;
leothedragon 0:8f0bb79ddd48 139 const char* server_id_ptr;
leothedragon 0:8f0bb79ddd48 140 switch(resource) {
leothedragon 0:8f0bb79ddd48 141 case DefaultMinPeriod:
leothedragon 0:8f0bb79ddd48 142 server_id_ptr = SERVER_DEFAULT_MIN_PERIOD;
leothedragon 0:8f0bb79ddd48 143 break;
leothedragon 0:8f0bb79ddd48 144 case DefaultMaxPeriod:
leothedragon 0:8f0bb79ddd48 145 server_id_ptr = SERVER_DEFAULT_MAX_PERIOD;
leothedragon 0:8f0bb79ddd48 146 break;
leothedragon 0:8f0bb79ddd48 147 case Disable:
leothedragon 0:8f0bb79ddd48 148 server_id_ptr = SERVER_DISABLE;
leothedragon 0:8f0bb79ddd48 149 break;
leothedragon 0:8f0bb79ddd48 150 case DisableTimeout:
leothedragon 0:8f0bb79ddd48 151 server_id_ptr = SERVER_DISABLE_TIMEOUT;
leothedragon 0:8f0bb79ddd48 152 break;
leothedragon 0:8f0bb79ddd48 153 default:
leothedragon 0:8f0bb79ddd48 154 server_id_ptr = NULL;
leothedragon 0:8f0bb79ddd48 155 break;
leothedragon 0:8f0bb79ddd48 156 }
leothedragon 0:8f0bb79ddd48 157
leothedragon 0:8f0bb79ddd48 158 if(server_id_ptr) {
leothedragon 0:8f0bb79ddd48 159 if(_server_instance) {
leothedragon 0:8f0bb79ddd48 160 success = _server_instance->remove_resource(server_id_ptr);
leothedragon 0:8f0bb79ddd48 161 }
leothedragon 0:8f0bb79ddd48 162 }
leothedragon 0:8f0bb79ddd48 163 return success;
leothedragon 0:8f0bb79ddd48 164 }
leothedragon 0:8f0bb79ddd48 165
leothedragon 0:8f0bb79ddd48 166 bool M2MServer::set_resource_value(ServerResource resource,
leothedragon 0:8f0bb79ddd48 167 const String &value)
leothedragon 0:8f0bb79ddd48 168 {
leothedragon 0:8f0bb79ddd48 169 bool success = false;
leothedragon 0:8f0bb79ddd48 170 M2MResource* res = get_resource(resource);
leothedragon 0:8f0bb79ddd48 171 if(res && (M2MServer::Binding == resource)) {
leothedragon 0:8f0bb79ddd48 172 success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length());
leothedragon 0:8f0bb79ddd48 173 }
leothedragon 0:8f0bb79ddd48 174 return success;
leothedragon 0:8f0bb79ddd48 175 }
leothedragon 0:8f0bb79ddd48 176
leothedragon 0:8f0bb79ddd48 177 bool M2MServer::set_resource_value(ServerResource resource,
leothedragon 0:8f0bb79ddd48 178 uint32_t value)
leothedragon 0:8f0bb79ddd48 179 {
leothedragon 0:8f0bb79ddd48 180 bool success = false;
leothedragon 0:8f0bb79ddd48 181 M2MResource* res = get_resource(resource);
leothedragon 0:8f0bb79ddd48 182 if(res) {
leothedragon 0:8f0bb79ddd48 183 if(M2MServer::ShortServerID == resource ||
leothedragon 0:8f0bb79ddd48 184 M2MServer::Lifetime == resource ||
leothedragon 0:8f0bb79ddd48 185 M2MServer::DefaultMinPeriod == resource ||
leothedragon 0:8f0bb79ddd48 186 M2MServer::DefaultMaxPeriod == resource ||
leothedragon 0:8f0bb79ddd48 187 M2MServer::DisableTimeout == resource ||
leothedragon 0:8f0bb79ddd48 188 M2MServer::NotificationStorage == resource) {
leothedragon 0:8f0bb79ddd48 189 // If it is any of the above resource
leothedragon 0:8f0bb79ddd48 190 // set the value of the resource.
leothedragon 0:8f0bb79ddd48 191
leothedragon 0:8f0bb79ddd48 192 success = res->set_value(value);
leothedragon 0:8f0bb79ddd48 193 }
leothedragon 0:8f0bb79ddd48 194 }
leothedragon 0:8f0bb79ddd48 195 return success;
leothedragon 0:8f0bb79ddd48 196 }
leothedragon 0:8f0bb79ddd48 197
leothedragon 0:8f0bb79ddd48 198 String M2MServer::resource_value_string(ServerResource resource) const
leothedragon 0:8f0bb79ddd48 199 {
leothedragon 0:8f0bb79ddd48 200 String value = "";
leothedragon 0:8f0bb79ddd48 201 M2MResource* res = get_resource(resource);
leothedragon 0:8f0bb79ddd48 202 if(res && (M2MServer::Binding == resource)) {
leothedragon 0:8f0bb79ddd48 203
leothedragon 0:8f0bb79ddd48 204 value = res->get_value_string();
leothedragon 0:8f0bb79ddd48 205 }
leothedragon 0:8f0bb79ddd48 206 return value;
leothedragon 0:8f0bb79ddd48 207 }
leothedragon 0:8f0bb79ddd48 208
leothedragon 0:8f0bb79ddd48 209
leothedragon 0:8f0bb79ddd48 210 uint32_t M2MServer::resource_value_int(ServerResource resource) const
leothedragon 0:8f0bb79ddd48 211 {
leothedragon 0:8f0bb79ddd48 212 uint32_t value = 0;
leothedragon 0:8f0bb79ddd48 213 M2MResource* res = get_resource(resource);
leothedragon 0:8f0bb79ddd48 214 if(res) {
leothedragon 0:8f0bb79ddd48 215 if(M2MServer::ShortServerID == resource ||
leothedragon 0:8f0bb79ddd48 216 M2MServer::Lifetime == resource ||
leothedragon 0:8f0bb79ddd48 217 M2MServer::DefaultMinPeriod == resource ||
leothedragon 0:8f0bb79ddd48 218 M2MServer::DefaultMaxPeriod == resource ||
leothedragon 0:8f0bb79ddd48 219 M2MServer::DisableTimeout == resource ||
leothedragon 0:8f0bb79ddd48 220 M2MServer::NotificationStorage == resource) {
leothedragon 0:8f0bb79ddd48 221
leothedragon 0:8f0bb79ddd48 222 value = res->get_value_int();
leothedragon 0:8f0bb79ddd48 223 }
leothedragon 0:8f0bb79ddd48 224 }
leothedragon 0:8f0bb79ddd48 225 return value;
leothedragon 0:8f0bb79ddd48 226 }
leothedragon 0:8f0bb79ddd48 227
leothedragon 0:8f0bb79ddd48 228 bool M2MServer::is_resource_present(ServerResource resource) const
leothedragon 0:8f0bb79ddd48 229 {
leothedragon 0:8f0bb79ddd48 230 bool success = false;
leothedragon 0:8f0bb79ddd48 231 M2MResource *res = get_resource(resource);
leothedragon 0:8f0bb79ddd48 232 if(res) {
leothedragon 0:8f0bb79ddd48 233 success = true;
leothedragon 0:8f0bb79ddd48 234 }
leothedragon 0:8f0bb79ddd48 235 return success;
leothedragon 0:8f0bb79ddd48 236 }
leothedragon 0:8f0bb79ddd48 237
leothedragon 0:8f0bb79ddd48 238 uint16_t M2MServer::total_resource_count() const
leothedragon 0:8f0bb79ddd48 239 {
leothedragon 0:8f0bb79ddd48 240 uint16_t total_count = 0;
leothedragon 0:8f0bb79ddd48 241 if(_server_instance) {
leothedragon 0:8f0bb79ddd48 242 total_count = _server_instance->resources().size();
leothedragon 0:8f0bb79ddd48 243 }
leothedragon 0:8f0bb79ddd48 244 return total_count;
leothedragon 0:8f0bb79ddd48 245 }
leothedragon 0:8f0bb79ddd48 246
leothedragon 0:8f0bb79ddd48 247 M2MResource* M2MServer::get_resource(ServerResource res) const
leothedragon 0:8f0bb79ddd48 248 {
leothedragon 0:8f0bb79ddd48 249 M2MResource* res_object = NULL;
leothedragon 0:8f0bb79ddd48 250 const char* res_name_ptr = NULL;
leothedragon 0:8f0bb79ddd48 251 switch(res) {
leothedragon 0:8f0bb79ddd48 252 case ShortServerID:
leothedragon 0:8f0bb79ddd48 253 res_name_ptr = SERVER_SHORT_SERVER_ID;
leothedragon 0:8f0bb79ddd48 254 break;
leothedragon 0:8f0bb79ddd48 255 case Lifetime:
leothedragon 0:8f0bb79ddd48 256 res_name_ptr = SERVER_LIFETIME;
leothedragon 0:8f0bb79ddd48 257 break;
leothedragon 0:8f0bb79ddd48 258 case DefaultMinPeriod:
leothedragon 0:8f0bb79ddd48 259 res_name_ptr = SERVER_DEFAULT_MIN_PERIOD;
leothedragon 0:8f0bb79ddd48 260 break;
leothedragon 0:8f0bb79ddd48 261 case DefaultMaxPeriod:
leothedragon 0:8f0bb79ddd48 262 res_name_ptr = SERVER_DEFAULT_MAX_PERIOD;
leothedragon 0:8f0bb79ddd48 263 break;
leothedragon 0:8f0bb79ddd48 264 case Disable:
leothedragon 0:8f0bb79ddd48 265 res_name_ptr = SERVER_DISABLE;
leothedragon 0:8f0bb79ddd48 266 break;
leothedragon 0:8f0bb79ddd48 267 case DisableTimeout:
leothedragon 0:8f0bb79ddd48 268 res_name_ptr = SERVER_DISABLE_TIMEOUT;
leothedragon 0:8f0bb79ddd48 269 break;
leothedragon 0:8f0bb79ddd48 270 case NotificationStorage:
leothedragon 0:8f0bb79ddd48 271 res_name_ptr = SERVER_NOTIFICATION_STORAGE;
leothedragon 0:8f0bb79ddd48 272 break;
leothedragon 0:8f0bb79ddd48 273 case Binding:
leothedragon 0:8f0bb79ddd48 274 res_name_ptr = SERVER_BINDING;
leothedragon 0:8f0bb79ddd48 275 break;
leothedragon 0:8f0bb79ddd48 276 case RegistrationUpdate:
leothedragon 0:8f0bb79ddd48 277 res_name_ptr = SERVER_REGISTRATION_UPDATE;
leothedragon 0:8f0bb79ddd48 278 break;
leothedragon 0:8f0bb79ddd48 279 }
leothedragon 0:8f0bb79ddd48 280
leothedragon 0:8f0bb79ddd48 281 if(res_name_ptr) {
leothedragon 0:8f0bb79ddd48 282 if(_server_instance) {
leothedragon 0:8f0bb79ddd48 283 res_object = _server_instance->resource(res_name_ptr);
leothedragon 0:8f0bb79ddd48 284 }
leothedragon 0:8f0bb79ddd48 285 }
leothedragon 0:8f0bb79ddd48 286 return res_object;
leothedragon 0:8f0bb79ddd48 287 }