observe updates
Fork of mbedConnectorInterface by
Embed:
(wiki syntax)
Show/hide line numbers
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 #include "OptionsBuilder.h" 00024 00025 // external included configuration file for the endpoint... 00026 #include "configuration.h" 00027 00028 // Connector namespace 00029 namespace Connector { 00030 00031 // Constructor 00032 OptionsBuilder::OptionsBuilder() 00033 { 00034 this->m_lifetime = NSP_LIFE_TIME; 00035 this->m_domain = NSP_DOMAIN; 00036 this->m_endpoint_type = NSP_ENDPOINT_TYPE; 00037 this->m_node_name = NODE_NAME; 00038 this->m_channel_list = NODE_CHANNEL_LIST; 00039 this->m_rd_update_period = NSP_RD_UPDATE_PERIOD; 00040 this->m_nsp_port = NSP_COAP_UDP_PORT; 00041 00042 memset(this->m_nsp_address,0,NSP_IP_ADDRESS_LENGTH); 00043 memset(this->m_mac_address,0,NODE_MAC_ADDRESS_LENGTH); 00044 this->m_static_resources.clear(); 00045 this->m_dynamic_resources.clear(); 00046 this->m_resource_observers.clear(); 00047 } 00048 00049 // Copy Constructor 00050 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob) 00051 { 00052 this->m_lifetime = ob.m_lifetime; 00053 this->m_domain = ob.m_domain; 00054 this->m_endpoint_type = ob.m_endpoint_type; 00055 this->m_static_resources = ob.m_static_resources; 00056 this->m_dynamic_resources = ob.m_dynamic_resources; 00057 this->m_resource_observers = ob.m_resource_observers; 00058 } 00059 00060 // Destructor 00061 OptionsBuilder::~OptionsBuilder() 00062 { 00063 this->m_static_resources.clear(); 00064 this->m_dynamic_resources.clear(); 00065 this->m_resource_observers.clear(); 00066 } 00067 00068 // set lifetime 00069 OptionsBuilder &OptionsBuilder::setLifetime(const char *lifetime) 00070 { 00071 this->m_lifetime = (char *)lifetime; 00072 return *this; 00073 } 00074 00075 // set domain 00076 OptionsBuilder &OptionsBuilder::setDomain(const char *domain) 00077 { 00078 this->m_domain = string(domain); 00079 return *this; 00080 } 00081 00082 // set endpoint nodename 00083 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name) 00084 { 00085 this->m_node_name = string(node_name); 00086 return *this; 00087 } 00088 00089 // set lifetime 00090 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type) 00091 { 00092 this->m_endpoint_type = string(endpoint_type); 00093 return *this; 00094 } 00095 00096 // set NSP port number 00097 OptionsBuilder &OptionsBuilder::setNSPPortNumber(const int port_num) 00098 { 00099 this->m_nsp_port = port_num; 00100 return *this; 00101 } 00102 00103 // set low level radio channel list 00104 OptionsBuilder &OptionsBuilder::setRadioChannelList(const uint32_t channel_list) 00105 { 00106 this->m_channel_list = channel_list; 00107 return *this; 00108 } 00109 00110 // set NSP read update period 00111 OptionsBuilder &OptionsBuilder::setReadUpdatePeriod(const int rd_update_period) 00112 { 00113 this->m_rd_update_period = rd_update_period; 00114 return *this; 00115 } 00116 00117 // set NSP address 00118 OptionsBuilder &OptionsBuilder::setNSPAddress(const uint8_t *nsp_address,const int nsp_address_length) 00119 { 00120 if (nsp_address != NULL && nsp_address_length > 0) { 00121 int length = nsp_address_length; 00122 if (length > NSP_IP_ADDRESS_LENGTH) length = NSP_IP_ADDRESS_LENGTH; 00123 for(int i=0; i<length; ++i) this->m_nsp_address[i] = nsp_address[i]; 00124 } 00125 return *this; 00126 } 00127 00128 // set MAC address 00129 OptionsBuilder &OptionsBuilder::setMACAddress(const uint8_t *mac_address,const int mac_address_length) 00130 { 00131 if (mac_address != NULL && mac_address_length > 0) { 00132 int length = mac_address_length; 00133 if (length > NODE_MAC_ADDRESS_LENGTH) length = NODE_MAC_ADDRESS_LENGTH; 00134 for(int i=0; i<length; ++i) this->m_mac_address[i] = mac_address[i]; 00135 } 00136 return *this; 00137 } 00138 00139 // add static resource 00140 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource) 00141 { 00142 if (resource != NULL) { 00143 this->m_static_resources.push_back((StaticResource *)resource); 00144 } 00145 return *this; 00146 } 00147 00148 // add dynamic resource 00149 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource) 00150 { 00151 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00152 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation())); 00153 } 00154 00155 // add dynamic resource 00156 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time) 00157 { 00158 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00159 return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation())); 00160 } 00161 00162 // add dynamic resource 00163 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer) 00164 { 00165 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00166 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,use_observer); 00167 } 00168 00169 // add dynamic resource 00170 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer) 00171 { 00172 if (resource != NULL) { 00173 this->m_dynamic_resources.push_back((DynamicResource *)resource); 00174 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) { 00175 #ifdef CONNECTOR_USING_THREADS 00176 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time); 00177 #else 00178 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time); 00179 #endif 00180 this->m_resource_observers.push_back(observer); 00181 #ifdef ENABLE_OBS_ON_START 00182 observer->beginObservation(); 00183 #endif 00184 } 00185 } 00186 return *this; 00187 } 00188 00189 // set WiFi SSID 00190 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid) 00191 { 00192 this->m_wifi_ssid = string(ssid); 00193 return *this; 00194 } 00195 00196 // set WiFi AuthType 00197 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type) 00198 { 00199 this->m_wifi_auth_type = auth_type; 00200 return *this; 00201 } 00202 00203 // set WiFi AuthKey 00204 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key) 00205 { 00206 this->m_wifi_auth_key = string(auth_key); 00207 return *this; 00208 } 00209 00210 // set 802.15.4 Network ID 00211 OptionsBuilder &OptionsBuilder::setNetworkID(char *network_id) 00212 { 00213 this->m_network_id = string(network_id); 00214 return *this; 00215 } 00216 00217 // set 802.15.4 Radio Channel 00218 OptionsBuilder &OptionsBuilder::setRadioChannel(int channel) 00219 { 00220 this->m_channel = (uint8_t)channel; 00221 return *this; 00222 } 00223 00224 // build out our immutable self 00225 Options *OptionsBuilder::build() 00226 { 00227 return (Options *)this; 00228 } 00229 00230 } // namespace Connector
Generated on Wed Jul 13 2022 02:11:49 by 1.7.2