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