A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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