FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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