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 6_songs-from-the-cloud by
simpleclient.h
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 00017 #ifndef __SIMPLECLIENT_H__ 00018 #define __SIMPLECLIENT_H__ 00019 00020 //#include "sockets/UDPSocket.h" 00021 #include "EthernetInterface.h" 00022 //#include "mbed-drivers/test_env.h" 00023 #include "mbed-client/m2minterfacefactory.h" 00024 #include "mbed-client/m2mdevice.h" 00025 #include "mbed-client/m2minterfaceobserver.h" 00026 #include "mbed-client/m2minterface.h" 00027 #include "mbed-client/m2mobject.h" 00028 #include "mbed-client/m2mobjectinstance.h" 00029 #include "mbed-client/m2mresource.h" 00030 //#include "minar/minar.h" 00031 #include "security.h" 00032 00033 DigitalOut green(LED_GREEN); 00034 00035 //Select binding mode: UDP or TCP 00036 M2MInterface::BindingMode SOCKET_MODE = M2MInterface::UDP; 00037 00038 // This is address to mbed Device Connector 00039 const String &MBED_SERVER_ADDRESS = "coap://api.connector.mbed.com:5684"; 00040 00041 // These come from the security.h file copied from connector.mbed.com 00042 const String &MBED_USER_NAME_DOMAIN = MBED_DOMAIN; 00043 const String &ENDPOINT_NAME = MBED_ENDPOINT_NAME; 00044 00045 struct MbedClientDevice { 00046 const char* Manufacturer; 00047 const char* Type; 00048 const char* ModelNumber; 00049 const char* SerialNumber; 00050 }; 00051 00052 /* 00053 * Wrapper for mbed client stack that handles all callbacks, error handling, and 00054 * other schenanigans to make the mbed client stack easier to use. 00055 * 00056 * The end user should only have to care about configuring the parameters at the 00057 * top of this file and making sure they add the security.h file correctly. 00058 * To add resources you can copy the _TODO__ function and add as many instances as 00059 * you want. 00060 * 00061 */ 00062 class MbedClient: public M2MInterfaceObserver { 00063 public: 00064 00065 // constructor for MbedClient object, initialize private variables 00066 MbedClient(struct MbedClientDevice device) { 00067 _interface = NULL; 00068 _bootstrapped = false; 00069 _error = false; 00070 _registered = false; 00071 _unregistered = false; 00072 _register_security = NULL; 00073 _value = 0; 00074 _object = NULL; 00075 _device = device; 00076 } 00077 00078 // de-constructor for MbedClient object, you can ignore this 00079 ~MbedClient() { 00080 if(_interface) { 00081 delete _interface; 00082 } 00083 if(_register_security){ 00084 delete _register_security; 00085 } 00086 } 00087 00088 // debug printf function 00089 void trace_printer(const char* str) { 00090 printf("\r\n%s\r\n", str); 00091 } 00092 00093 /* 00094 * Creates M2MInterface using which endpoint can 00095 * setup its name, resource type, life time, connection mode, 00096 * Currently only LwIPv4 is supported. 00097 */ 00098 void create_interface() { 00099 // Randomizing listening port for Certificate mode connectivity 00100 srand(time(NULL)); 00101 uint16_t port = rand() % 65535 + 12345; 00102 00103 // create mDS interface object, this is the base object everything else attaches to 00104 _interface = M2MInterfaceFactory::create_interface(*this, 00105 ENDPOINT_NAME, // endpoint name string 00106 "instrument", // endpoint type string 00107 100, // lifetime 00108 port, // listen port 00109 MBED_USER_NAME_DOMAIN, // domain string 00110 SOCKET_MODE, // binding mode 00111 M2MInterface::LwIP_IPv4, // network stack 00112 ""); // context address string 00113 String binding_mode; 00114 (SOCKET_MODE == M2MInterface::UDP) ? binding_mode = "UDP" : binding_mode = "TCP"; 00115 printf("\r\nSOCKET_MODE : %s\r\n", binding_mode.c_str()); 00116 printf("Connecting to %s\r\n", MBED_SERVER_ADDRESS.c_str()); 00117 } 00118 00119 /* 00120 * check private variable to see if the registration was sucessful or not 00121 */ 00122 bool register_successful() { 00123 return _registered; 00124 } 00125 00126 /* 00127 * check private variable to see if un-registration was sucessful or not 00128 */ 00129 bool unregister_successful() { 00130 return _unregistered; 00131 } 00132 00133 /* 00134 * Creates register server object with mbed device server address and other parameters 00135 * required for client to connect to mbed device server. 00136 */ 00137 M2MSecurity* create_register_object() { 00138 // create security object using the interface factory. 00139 // this will generate a security ObjectID and ObjectInstance 00140 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); 00141 00142 // make sure security ObjectID/ObjectInstance was created successfully 00143 if(security) { 00144 // Add ResourceID's and values to the security ObjectID/ObjectInstance 00145 security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_ADDRESS); 00146 security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); 00147 security->set_resource_value(M2MSecurity::ServerPublicKey, SERVER_CERT, sizeof(SERVER_CERT)); 00148 security->set_resource_value(M2MSecurity::PublicKey, CERT, sizeof(CERT)); 00149 security->set_resource_value(M2MSecurity::Secretkey, KEY, sizeof(KEY)); 00150 } 00151 return security; 00152 } 00153 00154 /* 00155 * Creates device object which contains mandatory resources linked with 00156 * device endpoint. 00157 */ 00158 M2MDevice* create_device_object() { 00159 // create device objectID/ObjectInstance 00160 M2MDevice *device = M2MInterfaceFactory::create_device(); 00161 // make sure device object was created successfully 00162 if(device) { 00163 // add resourceID's to device objectID/ObjectInstance 00164 device->create_resource(M2MDevice::Manufacturer, _device.Manufacturer); 00165 device->create_resource(M2MDevice::DeviceType, _device.Type); 00166 device->create_resource(M2MDevice::ModelNumber, _device.ModelNumber); 00167 device->create_resource(M2MDevice::SerialNumber, _device.SerialNumber); 00168 } 00169 return device; 00170 } 00171 00172 /* 00173 * register an object 00174 */ 00175 void test_register(M2MSecurity *register_object, M2MObjectList object_list){ 00176 if(_interface) { 00177 // Register function 00178 _interface->register_object(register_object, object_list); 00179 } 00180 } 00181 00182 /* 00183 * unregister all objects 00184 */ 00185 void test_unregister() { 00186 if(_interface) { 00187 // Unregister function 00188 _interface->unregister_object(NULL); // NULL will unregister all objects 00189 } 00190 } 00191 00192 //Callback from mbed client stack when the bootstrap 00193 // is successful, it returns the mbed Device Server object 00194 // which will be used for registering the resources to 00195 // mbed Device server. 00196 void bootstrap_done(M2MSecurity *server_object){ 00197 if(server_object) { 00198 _bootstrapped = true; 00199 _error = false; 00200 trace_printer("Bootstrapped"); 00201 } 00202 } 00203 00204 //Callback from mbed client stack when the registration 00205 // is successful, it returns the mbed Device Server object 00206 // to which the resources are registered and registered objects. 00207 void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ 00208 _registered = true; 00209 _unregistered = false; 00210 green = 0; //turn led on 00211 trace_printer("Registered object successfully!"); 00212 } 00213 00214 //Callback from mbed client stack when the unregistration 00215 // is successful, it returns the mbed Device Server object 00216 // to which the resources were unregistered. 00217 void object_unregistered(M2MSecurity */*server_object*/){ 00218 trace_printer("Unregistered Object Successfully"); 00219 _unregistered = true; 00220 _registered = false; 00221 green = 1; //turn led off 00222 // notify_completion(_unregistered); 00223 trace_printer("\nUnregistered\n"); 00224 } 00225 00226 /* 00227 * Callback from mbed client stack when registration is updated 00228 */ 00229 void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){ 00230 /* The registration is updated automatically and frequently by the 00231 * mbed client stack. This print statement is turned off because it 00232 * tends to happen alot. 00233 */ 00234 //trace_printer("\r\nRegistration Updated\r\n"); 00235 } 00236 00237 // Callback from mbed client stack if any error is encountered 00238 // during any of the LWM2M operations. Error type is passed in 00239 // the callback. 00240 void error(M2MInterface::Error error){ 00241 _error = true; 00242 switch(error){ 00243 case M2MInterface::AlreadyExists: 00244 trace_printer("[ERROR:] M2MInterface::AlreadyExist"); 00245 break; 00246 case M2MInterface::BootstrapFailed: 00247 trace_printer("[ERROR:] M2MInterface::BootstrapFailed"); 00248 break; 00249 case M2MInterface::InvalidParameters: 00250 trace_printer("[ERROR:] M2MInterface::InvalidParameters"); 00251 break; 00252 case M2MInterface::NotRegistered: 00253 trace_printer("[ERROR:] M2MInterface::NotRegistered"); 00254 break; 00255 case M2MInterface::Timeout: 00256 trace_printer("[ERROR:] M2MInterface::Timeout"); 00257 break; 00258 case M2MInterface::NetworkError: 00259 trace_printer("[ERROR:] M2MInterface::NetworkError"); 00260 break; 00261 case M2MInterface::ResponseParseFailed: 00262 trace_printer("[ERROR:] M2MInterface::ResponseParseFailed"); 00263 break; 00264 case M2MInterface::UnknownError: 00265 trace_printer("[ERROR:] M2MInterface::UnknownError"); 00266 break; 00267 case M2MInterface::MemoryFail: 00268 trace_printer("[ERROR:] M2MInterface::MemoryFail"); 00269 break; 00270 case M2MInterface::NotAllowed: 00271 trace_printer("[ERROR:] M2MInterface::NotAllowed"); 00272 break; 00273 default: 00274 break; 00275 } 00276 } 00277 00278 /* Callback from mbed client stack if any value has changed 00279 * during PUT operation. Object and its type is passed in 00280 * the callback. 00281 * BaseType enum from m2mbase.h 00282 * Object = 0x0, Resource = 0x1, ObjectInstance = 0x2, ResourceInstance = 0x3 00283 */ 00284 void value_updated(M2MBase *base, M2MBase::BaseType type) { 00285 printf("\r\nPUT Request Received!"); 00286 printf("\r\nName :'%s', \r\nType : '%d' (0 for Object, 1 for Resource), \r\nType : '%s'\r\n", 00287 base->name().c_str(), 00288 type, 00289 base->resource_type().c_str() 00290 ); 00291 } 00292 00293 /* 00294 * update the registration period 00295 */ 00296 void test_update_register() { 00297 if (_registered) { 00298 _interface->update_registration(_register_security, 100); 00299 } 00300 } 00301 00302 /* 00303 * manually configure the security object private variable 00304 */ 00305 void set_register_object(M2MSecurity *register_object) { 00306 if (_register_security == NULL) { 00307 _register_security = register_object; 00308 } 00309 } 00310 00311 private: 00312 00313 /* 00314 * Private variables used in class 00315 */ 00316 M2MInterface *_interface; 00317 M2MSecurity *_register_security; 00318 M2MObject *_object; 00319 volatile bool _bootstrapped; 00320 volatile bool _error; 00321 volatile bool _registered; 00322 volatile bool _unregistered; 00323 int _value; 00324 struct MbedClientDevice _device; 00325 }; 00326 00327 #endif // __SIMPLECLIENT_H__
Generated on Tue Jul 12 2022 12:47:49 by
1.7.2
