Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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