Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IoT_LED_demo ServoTest uWater_Project hackathon ... more
OptionsBuilder.cpp
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 ((StaticResource *)resource)->setOptions(this); 00144 this->m_static_resources.push_back((StaticResource *)resource); 00145 } 00146 return *this; 00147 } 00148 00149 // add dynamic resource 00150 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource) 00151 { 00152 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00153 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,((DynamicResource *)resource)->isObservable()); 00154 } 00155 00156 // add dynamic resource 00157 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time) 00158 { 00159 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00160 return this->addResource(resource,sleep_time,((DynamicResource *)resource)->isObservable()); 00161 } 00162 00163 // add dynamic resource 00164 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer) 00165 { 00166 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00167 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,use_observer); 00168 } 00169 00170 // add dynamic resource 00171 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer) 00172 { 00173 if (resource != NULL) { 00174 this->m_dynamic_resources.push_back((DynamicResource *)resource); 00175 ((DynamicResource *)resource)->setOptions(this); 00176 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) { 00177 #ifdef CONNECTOR_USING_THREADS 00178 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time); 00179 #else 00180 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time); 00181 #endif 00182 this->m_resource_observers.push_back(observer); 00183 00184 // immedate observation enablement option 00185 if (this->immedateObservationEnabled()) { 00186 observer->beginObservation(); 00187 } 00188 } 00189 } 00190 return *this; 00191 } 00192 00193 // set WiFi SSID 00194 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid) 00195 { 00196 this->m_wifi_ssid = string(ssid); 00197 return *this; 00198 } 00199 00200 // set WiFi AuthType 00201 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type) 00202 { 00203 this->m_wifi_auth_type = auth_type; 00204 return *this; 00205 } 00206 00207 // set WiFi AuthKey 00208 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key) 00209 { 00210 this->m_wifi_auth_key = string(auth_key); 00211 return *this; 00212 } 00213 00214 // set 802.15.4 Network ID 00215 OptionsBuilder &OptionsBuilder::setNetworkID(char *network_id) 00216 { 00217 this->m_network_id = string(network_id); 00218 return *this; 00219 } 00220 00221 // set 802.15.4 Radio Channel 00222 OptionsBuilder &OptionsBuilder::setRadioChannel(int channel) 00223 { 00224 this->m_channel = (uint8_t)channel; 00225 return *this; 00226 } 00227 00228 // build out our immutable self 00229 Options *OptionsBuilder::build() 00230 { 00231 return (Options *)this; 00232 } 00233 00234 // Enable/Disable immediate observationing 00235 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) { 00236 this->m_enable_immediate_observation = enable; 00237 return *this; 00238 } 00239 00240 // Enable/Disable GET-based control of observations 00241 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) { 00242 this->m_enable_get_obs_control = enable; 00243 return *this; 00244 } 00245 00246 } // namespace Connector
Generated on Tue Jul 12 2022 15:15:35 by
 1.7.2
 1.7.2