Mbed Client sample for GR-LYCHEE where ZXing is incorporated.

Dependencies:   DisplayApp AsciiFont

Fork of GR-PEACH_mbed-os-client-ZXingSample by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers simpleclient.h Source File

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