Doug Anson / mbedConnectorInterfaceWithDM

Fork of mbedConnectorInterfaceV3 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/EventQueueResourceObserver.h"
00028 #include "mbed-connector-interface/ThreadedResourceObserver.h"
00029 #include "mbed-connector-interface/TickerResourceObserver.h"
00030 #include "mbed-connector-interface/MinarResourceObserver.h"
00031 
00032 // Connector namespace
00033 namespace Connector {
00034 
00035 // Constructor
00036 OptionsBuilder::OptionsBuilder()
00037 {
00038     this->m_endpoint                = NULL;
00039     this->m_domain                  = DEFAULT_DOMAIN;
00040     this->m_endpoint_type           = DEFAULT_ENDPOINT_TYPE;
00041     this->m_node_name               = NODE_NAME;
00042     this->m_lifetime                = REG_LIFETIME_SEC;
00043     this->m_connector_url           = CONNECTOR_URL;
00044     this->m_server_cert             = NULL;
00045     this->m_server_cert_length      = 0;
00046     this->m_client_cert             = NULL;
00047     this->m_client_cert_length      = 0;
00048     this->m_client_key              = NULL;
00049     this->m_client_key_length       = 0;
00050     this->m_device_resources_object = NULL;
00051     this->m_firmware_resources_object = NULL;
00052     this->m_static_resources.clear();
00053     this->m_dynamic_resources.clear();
00054     this->m_resource_observers.clear();
00055 }
00056 
00057 // Copy Constructor
00058 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob)  : Options(ob)
00059 {
00060     this->m_endpoint = ob.m_endpoint;
00061     this->m_domain = ob.m_domain;
00062     this->m_endpoint_type = ob.m_endpoint_type;
00063     this->m_node_name = ob.m_node_name;
00064     this->m_lifetime = ob.m_lifetime; 
00065     this->m_connector_url  = ob.m_connector_url;  
00066     this->m_server_cert = ob.m_server_cert;
00067     this->m_server_cert_length = ob.m_server_cert_length;
00068     this->m_client_cert = ob.m_client_cert;
00069     this->m_client_cert_length = ob.m_client_cert_length;
00070     this->m_client_key = ob.m_client_key;
00071     this->m_client_key_length= ob.m_client_key_length;
00072     this->m_device_resources_object = ob.m_device_resources_object;
00073     this->m_firmware_resources_object = ob.m_firmware_resources_object;
00074     this->m_static_resources = ob.m_static_resources;
00075     this->m_dynamic_resources = ob.m_dynamic_resources;
00076     this->m_resource_observers = ob.m_resource_observers;
00077     this->m_wifi_ssid = ob.m_wifi_ssid;
00078     this->m_wifi_auth_key = ob.m_wifi_auth_key;
00079     this->m_wifi_auth_type = ob.m_wifi_auth_type;
00080     this->m_coap_connection_type = ob.m_coap_connection_type;
00081     this->m_ip_address_type = ob.m_ip_address_type;
00082     this->m_enable_immediate_observation = ob.m_enable_immediate_observation;
00083     this->m_enable_get_obs_control = ob.m_enable_get_obs_control;
00084     this->m_endpoint = ob.m_endpoint;
00085 }
00086 
00087 // Destructor
00088 OptionsBuilder::~OptionsBuilder()
00089 {
00090     this->m_device_resources_object = NULL;
00091     this->m_firmware_resources_object = NULL;
00092     this->m_static_resources.clear();
00093     this->m_dynamic_resources.clear();
00094     this->m_resource_observers.clear();
00095 }
00096 
00097 // set lifetime
00098 OptionsBuilder &OptionsBuilder::setLifetime(int lifetime)
00099 {
00100     this->m_lifetime = lifetime;
00101     return *this;
00102 }
00103 
00104 // set domain
00105 OptionsBuilder &OptionsBuilder::setDomain(const char *domain)
00106 {
00107     this->m_domain = string(domain);
00108     return *this;
00109 }
00110 
00111 // set endpoint nodename
00112 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name)
00113 {
00114     this->m_node_name = string(node_name);
00115     return *this;
00116 }
00117 
00118 // set lifetime
00119 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type)
00120 {
00121     this->m_endpoint_type = string(endpoint_type);
00122     return *this;
00123 }
00124 
00125 // set Connector URL
00126 OptionsBuilder &OptionsBuilder::setConnectorURL(const char *connector_url)
00127 {
00128     if (connector_url != NULL) {
00129         this->m_connector_url  = string(connector_url);
00130     }
00131     return *this;
00132 }
00133 
00134 // add the device resources object
00135 OptionsBuilder &OptionsBuilder::setDeviceResourcesObject(const void *device_resources_object) 
00136 {
00137     if (device_resources_object != NULL) {
00138         this->m_device_resources_object = (void *)device_resources_object;
00139     }
00140     return *this;
00141 }
00142 
00143 // add the firmware resources object
00144 OptionsBuilder &OptionsBuilder::setFirmwareResourcesObject(const void *firmware_resources_object) 
00145 {
00146     if (firmware_resources_object != NULL) {
00147         this->m_firmware_resources_object = (void *)firmware_resources_object;
00148     }
00149     return *this;
00150 }
00151 
00152 // add static resource
00153 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource)
00154 {
00155     if (resource != NULL) {
00156         ((StaticResource *)resource)->setOptions(this);
00157         this->m_static_resources.push_back((StaticResource *)resource);
00158     }
00159     return *this;
00160 }
00161 
00162 // add dynamic resource
00163 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
00164 {
00165     // ensure that the boolean isn't mistaken by the compiler for the obs period...
00166     return this->addResource(resource,DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation()));
00167 }
00168 
00169 // add dynamic resource
00170 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time)
00171 {
00172     // ensure that the boolean isn't mistaken by the compiler for the obs period...
00173     return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation()));
00174 }
00175 
00176 // add dynamic resource
00177 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer)
00178 {
00179     // ensure that the boolean isn't mistaken by the compiler for the obs period...
00180     return this->addResource(resource,DEFAULT_OBS_PERIOD,use_observer);
00181 }
00182 
00183 // add dynamic resource
00184 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
00185 {
00186     if (resource != NULL) {
00187         this->m_dynamic_resources.push_back((DynamicResource *)resource);
00188         ((DynamicResource *)resource)->setOptions(this);
00189         ((DynamicResource *)resource)->setEndpoint((const void *)this->getEndpoint());
00190         if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
00191             //
00192             // Establish the appropriate ResourceObserver
00193             //
00194 #if defined (MCI_MINAR_SCHEDULER) 
00195             // mbedOS3 RTOS Minar-based Scheduler ResourceObserver
00196             MinarResourceObserver *observer = new MinarResourceObserver((DynamicResource *)resource,(int)sleep_time);
00197 #endif
00198 
00199 #ifdef CONNECTOR_USING_THREADS
00200             // mbedOS RTOS Thread ResourceObserver
00201             ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
00202 #endif
00203 
00204 #ifdef CONNECTOR_USING_TICKER
00205             // mbedOS RTOS Ticker ResourceObserver
00206             TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
00207 #endif
00208 
00209 #ifdef CONNECTOR_USING_EVENT_QUEUES
00210             // mbedOS RTOS EventQueue ResourceObserver
00211             EventQueueResourceObserver *observer = new EventQueueResourceObserver((DynamicResource *)resource,(int)sleep_time);
00212 #endif
00213 
00214             // If no observer type is set in mbed-connector-interface/configuration.h (EndpointNetwork lib), then "observer" will be unresolved
00215             this->m_resource_observers.push_back(observer);
00216 
00217             // immedate observation enablement option
00218             if (this->immedateObservationEnabled()) {
00219                 observer->beginObservation();
00220             }
00221         }
00222     }
00223     return *this;
00224 }
00225 
00226 // set WiFi SSID
00227 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
00228 {
00229     this->m_wifi_ssid = string(ssid);
00230     return *this;
00231 }
00232 
00233 // set WiFi AuthType
00234 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
00235 {
00236     this->m_wifi_auth_type = auth_type;
00237     return *this;
00238 }
00239 
00240 // set WiFi AuthKey
00241 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
00242 {
00243     this->m_wifi_auth_key = string(auth_key);
00244     return *this;
00245 }
00246 
00247 // set the CoAP Connection Type
00248 OptionsBuilder &OptionsBuilder::setCoAPConnectionType(CoAPConnectionTypes coap_connection_type)
00249 {
00250     this->m_coap_connection_type = coap_connection_type;
00251     return *this;
00252 }
00253 
00254 // set the IP Address Type
00255 OptionsBuilder &OptionsBuilder::setIPAddressType(IPAddressTypes ip_address_type)
00256 {
00257     this->m_ip_address_type = ip_address_type;
00258     return *this;
00259 }
00260 
00261 // build out our immutable self
00262 Options *OptionsBuilder::build()
00263 {
00264     return (Options *)this;
00265 }
00266 
00267 // Enable/Disable immediate observationing
00268 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) {
00269     this->m_enable_immediate_observation = enable;
00270     return *this;
00271 }
00272 
00273 // Enable/Disable GET-based control of observations
00274 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) {
00275     this->m_enable_get_obs_control = enable;
00276     return *this;
00277 }
00278 
00279 // set the server certificate
00280 OptionsBuilder &OptionsBuilder::setServerCertificate(uint8_t *cert,int cert_size) {
00281     this->m_server_cert = cert;
00282     this->m_server_cert_length = cert_size;
00283     return *this;
00284 }
00285 
00286 // set the client certificate
00287 OptionsBuilder &OptionsBuilder::setClientCertificate(uint8_t *cert,int cert_size) {
00288     this->m_client_cert = cert;
00289     this->m_client_cert_length = cert_size;
00290     return *this;
00291 }
00292 
00293 // set the client key
00294 OptionsBuilder &OptionsBuilder::setClientKey(uint8_t *key,int key_size) {
00295     this->m_client_key = key;
00296     this->m_client_key_length = key_size;
00297     return *this;
00298 }
00299 
00300 // set our endpoint
00301 void OptionsBuilder::setEndpoint(void *endpoint) {
00302     this->m_endpoint = endpoint;
00303 }
00304 
00305 } // namespace Connector