mbed demo / simple-mbed-client

Fork of simple-mbed-client by sandbox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed-client-wrapper.h Source File

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