Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2minterfacefactory.cpp Source File

m2minterfacefactory.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 
00017 // Note: this macro is needed on armcc to get the the PRI*32 macros
00018 // from inttypes.h in a C++ code.
00019 #ifndef __STDC_FORMAT_MACROS
00020 #define __STDC_FORMAT_MACROS
00021 #endif
00022 
00023 #include "mbed-client/m2minterfacefactory.h"
00024 #include "mbed-client/m2mserver.h"
00025 #include "mbed-client/m2mdevice.h"
00026 #include "mbed-client/m2mfirmware.h"
00027 #include "mbed-client/m2mobject.h"
00028 #include "mbed-client/m2mendpoint.h"
00029 #include "mbed-client/m2mconstants.h"
00030 #include "mbed-client/m2mconfig.h"
00031 #include "include/m2minterfaceimpl.h"
00032 #include "mbed-trace/mbed_trace.h"
00033 
00034 #include <inttypes.h>
00035 
00036 #define TRACE_GROUP "mClt"
00037 
00038 M2MInterface* M2MInterfaceFactory::create_interface(M2MInterfaceObserver &observer,
00039                                                     const String &endpoint_name,
00040                                                     const String &endpoint_type,
00041                                                     const int32_t life_time,
00042                                                     const uint16_t listen_port,
00043                                                     const String &domain,
00044                                                     M2MInterface::BindingMode mode,
00045                                                     M2MInterface::NetworkStack stack,
00046                                                     const String &context_address)
00047 {
00048     tr_debug("M2MInterfaceFactory::create_interface - IN");
00049     tr_info("M2MInterfaceFactory::create_interface - parameters endpoint name : %s",endpoint_name.c_str());
00050     tr_info("M2MInterfaceFactory::create_interface - parameters endpoint type : %s",endpoint_type.c_str());
00051     tr_info("M2MInterfaceFactory::create_interface - parameters life time(in secs): %" PRId32,life_time);
00052     tr_info("M2MInterfaceFactory::create_interface - parameters Listen Port : %d",listen_port);
00053     tr_info("M2MInterfaceFactory::create_interface - parameters Binding Mode : %d",(int)mode);
00054     tr_info("M2MInterfaceFactory::create_interface - parameters NetworkStack : %d",(int)stack);
00055     M2MInterfaceImpl *interface = NULL;
00056 
00057 
00058     bool endpoint_type_valid = true;
00059     if(!endpoint_type.empty()) {
00060         if(endpoint_type.size() > MAX_ALLOWED_STRING_LENGTH){
00061             endpoint_type_valid = false;
00062         }
00063     }
00064 
00065     bool domain_valid = true;
00066     if(!domain.empty()) {
00067         if(domain.size() > MAX_ALLOWED_STRING_LENGTH){
00068             domain_valid = false;
00069         }
00070     }
00071 
00072     if(((life_time == -1) || (life_time >= MINIMUM_REGISTRATION_TIME)) &&
00073        !endpoint_name.empty() && (endpoint_name.size() <= MAX_ALLOWED_STRING_LENGTH) &&
00074        endpoint_type_valid && domain_valid) {
00075         tr_debug("M2MInterfaceFactory::create_interface - Creating M2MInterfaceImpl");
00076         interface = new M2MInterfaceImpl(observer, endpoint_name,
00077                                          endpoint_type, life_time,
00078                                          listen_port, domain, mode,
00079                                          stack, context_address);
00080 
00081     }
00082     tr_debug("M2MInterfaceFactory::create_interface - OUT");
00083     return interface;
00084 }
00085 
00086 M2MSecurity* M2MInterfaceFactory::create_security(M2MSecurity::ServerType server_type)
00087 {
00088     tr_debug("M2MInterfaceFactory::create_security");
00089     M2MSecurity *security = M2MSecurity::get_instance();
00090     return security;
00091 }
00092 
00093 M2MServer* M2MInterfaceFactory::create_server()
00094 {
00095     tr_debug("M2MInterfaceFactory::create_server");
00096     M2MServer *server = new M2MServer();
00097     return server;
00098 }
00099 
00100 M2MDevice* M2MInterfaceFactory::create_device()
00101 {
00102     tr_debug("M2MInterfaceFactory::create_device");
00103     M2MDevice* device = M2MDevice::get_instance();
00104     return device;
00105 }
00106 
00107 M2MFirmware* M2MInterfaceFactory::create_firmware()
00108 {
00109     tr_debug("M2MInterfaceFactory::create_firmware");
00110     M2MFirmware* firmware = M2MFirmware::get_instance();
00111     return firmware;
00112 }
00113 
00114 M2MObject* M2MInterfaceFactory::create_object(const String &name)
00115 {
00116     tr_debug("M2MInterfaceFactory::create_object : Name : %s", name.c_str());
00117     if(name.size() > MAX_ALLOWED_STRING_LENGTH || name.empty()){
00118         return NULL;
00119     }
00120 
00121     M2MObject *object = NULL;
00122     char *name_copy = M2MBase::stringdup(name.c_str());
00123     if (name_copy) {
00124         object = new M2MObject(name, name_copy);
00125     }
00126     return object;
00127 }
00128 
00129 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
00130 M2MEndpoint* M2MInterfaceFactory::create_endpoint(const String &name)
00131 {
00132     tr_debug("M2MInterfaceFactory::create_endpoint : Name : %s", name.c_str());
00133     if(name.size() > MAX_ALLOWED_STRING_LENGTH || name.empty()){
00134         return NULL;
00135     }
00136 
00137     M2MEndpoint *object = NULL;
00138     char *path = (char*)malloc(2 + name.size() + 1);
00139     if (path) {
00140         // Prepend path with directory prefix "d/" so that all endpoints will be under common path
00141         path[0] = 'd';
00142         path[1] = '/';
00143         memcpy(&path[2], name.c_str(), name.size());
00144         path[name.size() + 2] = '\0';
00145         object = new M2MEndpoint(name, path);
00146     }
00147     return object;
00148 }
00149 #endif