Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
m2mserver.cpp
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include <cstdio> 00017 #include "mbed-client/m2mserver.h" 00018 #include "mbed-client/m2mconstants.h" 00019 #include "mbed-client/m2mobject.h" 00020 #include "mbed-client/m2mobjectinstance.h" 00021 #include "mbed-client/m2mresource.h" 00022 00023 00024 M2MServer::M2MServer() 00025 : M2MObject(M2M_SERVER_ID) 00026 { 00027 M2MObject::create_object_instance(); 00028 00029 _server_instance = object_instance(); 00030 00031 if(_server_instance) { 00032 00033 M2MResource* res = _server_instance->create_dynamic_resource(SERVER_SHORT_SERVER_ID, 00034 OMA_RESOURCE_TYPE, 00035 M2MResourceInstance::INTEGER, 00036 true); 00037 if(res) { 00038 res->set_operation(M2MBase::GET_PUT_ALLOWED); 00039 } 00040 res = _server_instance->create_dynamic_resource(SERVER_LIFETIME, 00041 OMA_RESOURCE_TYPE, 00042 M2MResourceInstance::INTEGER, 00043 true); 00044 if(res) { 00045 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00046 } 00047 res = _server_instance->create_dynamic_resource(SERVER_NOTIFICATION_STORAGE, 00048 OMA_RESOURCE_TYPE, 00049 M2MResourceInstance::BOOLEAN, 00050 true); 00051 if(res) { 00052 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00053 } 00054 res = _server_instance->create_dynamic_resource(SERVER_BINDING, 00055 OMA_RESOURCE_TYPE, 00056 M2MResourceInstance::STRING, 00057 true); 00058 if(res) { 00059 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00060 } 00061 res = _server_instance->create_dynamic_resource(SERVER_REGISTRATION_UPDATE, 00062 OMA_RESOURCE_TYPE, 00063 M2MResourceInstance::OPAQUE, 00064 false); 00065 if(res) { 00066 res->set_operation(M2MBase::POST_ALLOWED); 00067 } 00068 } 00069 } 00070 00071 M2MServer::~M2MServer() 00072 { 00073 00074 } 00075 00076 M2MResource* M2MServer::create_resource(ServerResource resource, uint32_t value) 00077 { 00078 M2MResource* res = NULL; 00079 String server_id = ""; 00080 if(!is_resource_present(resource)) { 00081 switch(resource) { 00082 case DefaultMinPeriod: 00083 server_id = SERVER_DEFAULT_MIN_PERIOD; 00084 break; 00085 case DefaultMaxPeriod: 00086 server_id = SERVER_DEFAULT_MAX_PERIOD; 00087 break; 00088 case DisableTimeout: 00089 server_id = SERVER_DISABLE_TIMEOUT; 00090 break; 00091 default: 00092 break; 00093 } 00094 } 00095 if(!server_id.empty()) { 00096 if(_server_instance) { 00097 res = _server_instance->create_dynamic_resource(server_id, 00098 OMA_RESOURCE_TYPE, 00099 M2MResourceInstance::INTEGER, 00100 true); 00101 if(res) { 00102 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00103 // If resource is created then set the value. 00104 char *buffer = (char*)malloc(20); 00105 if(buffer) { 00106 int size = snprintf(buffer, 20,"%ld",(long int)value); 00107 res->set_value((const uint8_t*)buffer,(uint32_t)size); 00108 free(buffer); 00109 } 00110 } 00111 } 00112 } 00113 return res; 00114 } 00115 00116 M2MResource* M2MServer::create_resource(ServerResource resource) 00117 { 00118 M2MResource* res = NULL; 00119 if(!is_resource_present(resource)) { 00120 if(M2MServer::Disable == resource) { 00121 if(_server_instance) { 00122 res = _server_instance->create_dynamic_resource(SERVER_DISABLE, 00123 OMA_RESOURCE_TYPE, 00124 M2MResourceInstance::OPAQUE, 00125 false); 00126 if(res) { 00127 res->set_operation(M2MBase::POST_ALLOWED); 00128 } 00129 } 00130 } 00131 } 00132 return res; 00133 } 00134 00135 bool M2MServer::delete_resource(ServerResource resource) 00136 { 00137 bool success = false; 00138 String server_id = ""; 00139 switch(resource) { 00140 case DefaultMinPeriod: 00141 server_id = SERVER_DEFAULT_MIN_PERIOD; 00142 break; 00143 case DefaultMaxPeriod: 00144 server_id = SERVER_DEFAULT_MAX_PERIOD; 00145 break; 00146 case Disable: 00147 server_id = SERVER_DISABLE; 00148 break; 00149 case DisableTimeout: 00150 server_id = SERVER_DISABLE_TIMEOUT; 00151 break; 00152 default: 00153 break; 00154 } 00155 if(!server_id.empty()) { 00156 if(_server_instance) { 00157 success = _server_instance->remove_resource(server_id); 00158 } 00159 } 00160 return success; 00161 } 00162 00163 bool M2MServer::set_resource_value(ServerResource resource, 00164 const String &value) 00165 { 00166 bool success = false; 00167 M2MResource* res = get_resource(resource); 00168 if(res && (M2MServer::Binding == resource)) { 00169 success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); 00170 } 00171 return success; 00172 } 00173 00174 bool M2MServer::set_resource_value(ServerResource resource, 00175 uint32_t value) 00176 { 00177 bool success = false; 00178 M2MResource* res = get_resource(resource); 00179 if(res) { 00180 if(M2MServer::ShortServerID == resource || 00181 M2MServer::Lifetime == resource || 00182 M2MServer::DefaultMinPeriod == resource || 00183 M2MServer::DefaultMaxPeriod == resource || 00184 M2MServer::DisableTimeout == resource || 00185 M2MServer::NotificationStorage == resource) { 00186 // If it is any of the above resource 00187 // set the value of the resource. 00188 char *buffer = (char*)malloc(20); 00189 if(buffer) { 00190 int size = snprintf(buffer, 20,"%ld",(long int)value); 00191 success = res->set_value((const uint8_t*)buffer, 00192 (uint32_t)size); 00193 free(buffer); 00194 } 00195 } 00196 } 00197 return success; 00198 } 00199 00200 String M2MServer::resource_value_string(ServerResource resource) const 00201 { 00202 String value = ""; 00203 M2MResource* res = get_resource(resource); 00204 if(res && (M2MServer::Binding == resource)) { 00205 uint8_t* buffer = NULL; 00206 uint32_t length = 0; 00207 res->get_value(buffer,length); 00208 00209 char *char_buffer = (char*)malloc(length+1); 00210 if(char_buffer) { 00211 memset(char_buffer,0,length+1); 00212 memcpy(char_buffer,(char*)buffer,length); 00213 00214 String s_name(char_buffer); 00215 value = s_name; 00216 if(char_buffer) { 00217 free(char_buffer); 00218 } 00219 } 00220 if(buffer) { 00221 free(buffer); 00222 } 00223 } 00224 return value; 00225 } 00226 00227 00228 uint32_t M2MServer::resource_value_int(ServerResource resource) const 00229 { 00230 uint32_t value = 0; 00231 M2MResource* res = get_resource(resource); 00232 if(res) { 00233 if(M2MServer::ShortServerID == resource || 00234 M2MServer::Lifetime == resource || 00235 M2MServer::DefaultMinPeriod == resource || 00236 M2MServer::DefaultMaxPeriod == resource || 00237 M2MServer::DisableTimeout == resource || 00238 M2MServer::NotificationStorage == resource) { 00239 // Get the value and convert it into integer 00240 uint8_t* buffer = NULL; 00241 uint32_t length = 0; 00242 res->get_value(buffer,length); 00243 if(buffer) { 00244 value = atoi((const char*)buffer); 00245 free(buffer); 00246 } 00247 } 00248 } 00249 return value; 00250 } 00251 00252 bool M2MServer::is_resource_present(ServerResource resource) const 00253 { 00254 bool success = false; 00255 M2MResource *res = get_resource(resource); 00256 if(res) { 00257 success = true; 00258 } 00259 return success; 00260 } 00261 00262 uint16_t M2MServer::total_resource_count() const 00263 { 00264 uint16_t total_count = 0; 00265 if(_server_instance) { 00266 total_count = _server_instance->resources().size(); 00267 } 00268 return total_count; 00269 } 00270 00271 M2MResource* M2MServer::get_resource(ServerResource res) const 00272 { 00273 M2MResource* res_object = NULL; 00274 String res_name = ""; 00275 switch(res) { 00276 case ShortServerID: 00277 res_name = SERVER_SHORT_SERVER_ID; 00278 break; 00279 case Lifetime: 00280 res_name = SERVER_LIFETIME; 00281 break; 00282 case DefaultMinPeriod: 00283 res_name = SERVER_DEFAULT_MIN_PERIOD; 00284 break; 00285 case DefaultMaxPeriod: 00286 res_name = SERVER_DEFAULT_MAX_PERIOD; 00287 break; 00288 case Disable: 00289 res_name = SERVER_DISABLE; 00290 break; 00291 case DisableTimeout: 00292 res_name = SERVER_DISABLE_TIMEOUT; 00293 break; 00294 case NotificationStorage: 00295 res_name = SERVER_NOTIFICATION_STORAGE; 00296 break; 00297 case Binding: 00298 res_name = SERVER_BINDING; 00299 break; 00300 case RegistrationUpdate: 00301 res_name = SERVER_REGISTRATION_UPDATE; 00302 break; 00303 } 00304 if(!res_name.empty()) { 00305 if(_server_instance) { 00306 res_object = _server_instance->resource(res_name); 00307 } 00308 } 00309 return res_object; 00310 }
Generated on Tue Jul 12 2022 13:53:46 by
