Maggie Mei / mbedConnectorInterfaceWithDM

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OptionsBuilder.cpp Source File

OptionsBuilder.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    OptionsBuilder.cpp
00003  * @brief   mbed CoAP OptionsBuilder class implementation
00004  * @author  Doug Anson/Chris Paola
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2014
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022 
00023 // Class support
00024 #include "mbed-connector-interface/OptionsBuilder.h"
00025 
00026 // ResourceObserver support
00027 #include "mbed-connector-interface/ThreadedResourceObserver.h"
00028 #include "mbed-connector-interface/TickerResourceObserver.h"
00029 #include "mbed-connector-interface/MinarResourceObserver.h"
00030 
00031 #ifdef ENABLE_MBED_CLOUD_SUPPORT
00032 extern "C" char gIdcDevSecurityAccountId[33];
00033 #endif
00034 
00035 // Connector namespace
00036 namespace Connector {
00037 
00038 // Constructor
00039 OptionsBuilder::OptionsBuilder()
00040 {
00041     this->m_endpoint                = NULL;
00042     this->m_domain                  = DEFAULT_DOMAIN;
00043     this->m_endpoint_type           = DEFAULT_ENDPOINT_TYPE;
00044 #ifdef ENABLE_MBED_CLOUD_SUPPORT
00045     this->m_node_name               = (char *)gIdcDevSecurityAccountId;
00046 #else
00047     this->m_node_name               = NODE_NAME;
00048 #endif
00049     this->m_lifetime                = REG_LIFETIME_SEC;
00050     this->m_connector_url           = string(CONNECTOR_URL);
00051     this->m_server_cert             = NULL;
00052     this->m_server_cert_length      = 0;
00053     this->m_client_cert             = NULL;
00054     this->m_client_cert_length      = 0;
00055     this->m_client_key              = NULL;
00056     this->m_client_key_length       = 0;
00057     this->m_device_resources_object = NULL;
00058     this->m_firmware_resources_object = NULL;
00059     this->m_static_resources.clear();
00060     this->m_dynamic_resources.clear();
00061     this->m_resource_observers.clear();
00062 }
00063 
00064 // Copy Constructor
00065 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob)  : Options(ob)
00066 {
00067     this->m_endpoint = ob.m_endpoint;
00068     this->m_domain = ob.m_domain;
00069     this->m_endpoint_type = ob.m_endpoint_type;
00070     this->m_node_name = ob.m_node_name;
00071     this->m_lifetime = ob.m_lifetime; 
00072     this->m_connector_url  = ob.m_connector_url;  
00073     this->m_server_cert = ob.m_server_cert;
00074     this->m_server_cert_length = ob.m_server_cert_length;
00075     this->m_client_cert = ob.m_client_cert;
00076     this->m_client_cert_length = ob.m_client_cert_length;
00077     this->m_client_key = ob.m_client_key;
00078     this->m_client_key_length= ob.m_client_key_length;
00079     this->m_device_resources_object = ob.m_device_resources_object;
00080     this->m_firmware_resources_object = ob.m_firmware_resources_object;
00081     this->m_static_resources = ob.m_static_resources;
00082     this->m_dynamic_resources = ob.m_dynamic_resources;
00083     this->m_resource_observers = ob.m_resource_observers;
00084     this->m_wifi_ssid = ob.m_wifi_ssid;
00085     this->m_wifi_auth_key = ob.m_wifi_auth_key;
00086     this->m_wifi_auth_type = ob.m_wifi_auth_type;
00087     this->m_coap_connection_type = ob.m_coap_connection_type;
00088     this->m_ip_address_type = ob.m_ip_address_type;
00089     this->m_enable_immediate_observation = ob.m_enable_immediate_observation;
00090     this->m_enable_get_obs_control = ob.m_enable_get_obs_control;
00091     this->m_endpoint = ob.m_endpoint;
00092 }
00093 
00094 // Destructor
00095 OptionsBuilder::~OptionsBuilder()
00096 {
00097     this->m_device_resources_object = NULL;
00098     this->m_firmware_resources_object = NULL;
00099     this->m_static_resources.clear();
00100     this->m_dynamic_resources.clear();
00101     this->m_resource_observers.clear();
00102 }
00103 
00104 // set lifetime
00105 OptionsBuilder &OptionsBuilder::setLifetime(int lifetime)
00106 {
00107     this->m_lifetime = lifetime;
00108     return *this;
00109 }
00110 
00111 // set domain
00112 OptionsBuilder &OptionsBuilder::setDomain(const char *domain)
00113 {
00114     this->m_domain = string(domain);
00115     return *this;
00116 }
00117 
00118 // set endpoint nodename
00119 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name)
00120 {
00121     this->m_node_name = string(node_name);
00122     return *this;
00123 }
00124 
00125 // set lifetime
00126 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type)
00127 {
00128     this->m_endpoint_type = string(endpoint_type);
00129     return *this;
00130 }
00131 
00132 // set Connector URL
00133 OptionsBuilder &OptionsBuilder::setConnectorURL(const char *connector_url)
00134 {
00135     if (connector_url != NULL) {
00136         this->m_connector_url  = string(connector_url);
00137     }
00138     return *this;
00139 }
00140 
00141 // add the device resources object
00142 OptionsBuilder &OptionsBuilder::setDeviceResourcesObject(const void *device_resources_object) 
00143 {
00144     if (device_resources_object != NULL) {
00145         this->m_device_resources_object = (void *)device_resources_object;
00146     }
00147     return *this;
00148 }
00149 
00150 // add the firmware resources object
00151 OptionsBuilder &OptionsBuilder::setFirmwareResourcesObject(const void *firmware_resources_object) 
00152 {
00153     if (firmware_resources_object != NULL) {
00154         this->m_firmware_resources_object = (void *)firmware_resources_object;
00155     }
00156     return *this;
00157 }
00158 
00159 // add static resource
00160 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource)
00161 {
00162     if (resource != NULL) {
00163         ((StaticResource *)resource)->setOptions(this);
00164         this->m_static_resources.push_back((StaticResource *)resource);
00165     }
00166     return *this;
00167 }
00168 
00169 // add dynamic resource
00170 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
00171 {
00172     // ensure that the boolean isn't mistaken by the compiler for the obs period...
00173     return this->addResource(resource,DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation()));
00174 }
00175 
00176 // add dynamic resource
00177 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time)
00178 {
00179     // ensure that the boolean isn't mistaken by the compiler for the obs period...
00180     return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation()));
00181 }
00182 
00183 // add dynamic resource
00184 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer)
00185 {
00186     // ensure that the boolean isn't mistaken by the compiler for the obs period...
00187     return this->addResource(resource,DEFAULT_OBS_PERIOD,use_observer);
00188 }
00189 
00190 // add dynamic resource
00191 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
00192 {
00193     if (resource != NULL) {
00194         this->m_dynamic_resources.push_back((DynamicResource *)resource);
00195         ((DynamicResource *)resource)->setOptions(this);
00196         ((DynamicResource *)resource)->setEndpoint((const void *)this->getEndpoint());
00197         if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
00198             // Establish the appropriate ResourceObserver
00199 #if defined (MCI_MINAR_SCHEDULER) 
00200             // Minar-based Scheduler ResourceObserver
00201             MinarResourceObserver *observer = new MinarResourceObserver((DynamicResource *)resource,(int)sleep_time);
00202 #else
00203     #ifdef CONNECTOR_USING_THREADS
00204             // mbedOS RTOS Thread ResourceObserver
00205             ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
00206     #endif
00207     #ifdef CONNECTOR_USING_TICKER
00208             // mbed Ticker ResourceObserver
00209             TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
00210     #endif
00211 #endif
00212             // If no observer type is set in mbed-connector-interface/configuration.h (EndpointNetwork lib), then "observer" will be unresolved
00213             this->m_resource_observers.push_back(observer);
00214 
00215             // immedate observation enablement option
00216             if (this->immedateObservationEnabled()) {
00217                 observer->beginObservation();
00218             }
00219         }
00220     }
00221     return *this;
00222 }
00223 
00224 // set WiFi SSID
00225 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
00226 {
00227     this->m_wifi_ssid = string(ssid);
00228     return *this;
00229 }
00230 
00231 // set WiFi AuthType
00232 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
00233 {
00234     this->m_wifi_auth_type = auth_type;
00235     return *this;
00236 }
00237 
00238 // set WiFi AuthKey
00239 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
00240 {
00241     this->m_wifi_auth_key = string(auth_key);
00242     return *this;
00243 }
00244 
00245 // set the CoAP Connection Type
00246 OptionsBuilder &OptionsBuilder::setCoAPConnectionType(CoAPConnectionTypes coap_connection_type)
00247 {
00248     this->m_coap_connection_type = coap_connection_type;
00249     return *this;
00250 }
00251 
00252 // set the IP Address Type
00253 OptionsBuilder &OptionsBuilder::setIPAddressType(IPAddressTypes ip_address_type)
00254 {
00255     this->m_ip_address_type = ip_address_type;
00256     return *this;
00257 }
00258 
00259 // build out our immutable self
00260 Options *OptionsBuilder::build()
00261 {
00262     return (Options *)this;
00263 }
00264 
00265 // Enable/Disable immediate observationing
00266 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) {
00267     this->m_enable_immediate_observation = enable;
00268     return *this;
00269 }
00270 
00271 // Enable/Disable GET-based control of observations
00272 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) {
00273     this->m_enable_get_obs_control = enable;
00274     return *this;
00275 }
00276 
00277 // set the server certificate
00278 OptionsBuilder &OptionsBuilder::setServerCertificate(uint8_t *cert,int cert_size) {
00279     this->m_server_cert = cert;
00280     this->m_server_cert_length = cert_size;
00281     return *this;
00282 }
00283 
00284 // set the client certificate
00285 OptionsBuilder &OptionsBuilder::setClientCertificate(uint8_t *cert,int cert_size) {
00286     this->m_client_cert = cert;
00287     this->m_client_cert_length = cert_size;
00288     return *this;
00289 }
00290 
00291 // set the client key
00292 OptionsBuilder &OptionsBuilder::setClientKey(uint8_t *key,int key_size) {
00293     this->m_client_key = key;
00294     this->m_client_key_length = key_size;
00295     return *this;
00296 }
00297 
00298 // set our endpoint
00299 void OptionsBuilder::setEndpoint(void *endpoint) {
00300     this->m_endpoint = endpoint;
00301 }
00302 
00303 } // namespace Connector