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.
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 "mbed-connector-interface/OptionsBuilder.h" 00024 00025 // external included configuration file for the endpoint... 00026 #include "mbed-connector-interface/configuration.h" 00027 00028 // ResourceObserver support 00029 #include "mbed-connector-interface/ThreadedResourceObserver.h" 00030 #include "mbed-connector-interface/TickerResourceObserver.h" 00031 00032 // DEBUG 00033 #ifndef NDEBUG 00034 #define DEBUG_OUT(...) { printf(__VA_ARGS__); } 00035 #else 00036 #define DEBUG_OUT(...) /* nothing */ 00037 #endif 00038 00039 // Connector namespace 00040 namespace Connector { 00041 00042 // Constructor 00043 OptionsBuilder::OptionsBuilder() 00044 { 00045 this->m_domain = NSP_DOMAIN; 00046 this->m_endpoint_type = NSP_ENDPOINT_TYPE; 00047 this->m_node_name = NODE_NAME; 00048 this->m_reg_update_period = REG_UPDATE_PERIOD_MS; 00049 this->m_lifetime = REG_LIFETIME_SEC; 00050 this->m_connector_url = string(CONNECTOR_URL); 00051 this->m_device_resources.clear(); 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_domain = ob.m_domain; 00061 this->m_endpoint_type = ob.m_endpoint_type; 00062 this->m_node_name = ob.m_node_name; 00063 this->m_reg_update_period = ob.m_reg_update_period; 00064 this->m_lifetime = ob.m_lifetime; 00065 this->m_connector_url = ob.m_connector_url; 00066 this->m_device_resources = ob.m_device_resources; 00067 this->m_static_resources = ob.m_static_resources; 00068 this->m_dynamic_resources = ob.m_dynamic_resources; 00069 this->m_resource_observers = ob.m_resource_observers; 00070 } 00071 00072 // Destructor 00073 OptionsBuilder::~OptionsBuilder() 00074 { 00075 this->m_device_resources.clear(); 00076 this->m_static_resources.clear(); 00077 this->m_dynamic_resources.clear(); 00078 this->m_resource_observers.clear(); 00079 } 00080 00081 // set lifetime 00082 OptionsBuilder &OptionsBuilder::setLifetime(int lifetime) 00083 { 00084 this->m_lifetime = lifetime; 00085 return *this; 00086 } 00087 00088 // set domain 00089 OptionsBuilder &OptionsBuilder::setDomain(const char *domain) 00090 { 00091 this->m_domain = string(domain); 00092 return *this; 00093 } 00094 00095 // set endpoint nodename 00096 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name) 00097 { 00098 this->m_node_name = string(node_name); 00099 return *this; 00100 } 00101 00102 // set lifetime 00103 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type) 00104 { 00105 this->m_endpoint_type = string(endpoint_type); 00106 return *this; 00107 } 00108 00109 // set mbed registration update period 00110 OptionsBuilder &OptionsBuilder::setRegUpdatePeriod(const int reg_update_period) 00111 { 00112 this->m_reg_update_period = reg_update_period; 00113 return *this; 00114 } 00115 00116 // set Connector URL 00117 OptionsBuilder &OptionsBuilder::setConnectorURL(const char *connector_url) 00118 { 00119 if (connector_url != NULL) { 00120 this->m_connector_url = string(connector_url); 00121 } 00122 return *this; 00123 } 00124 00125 // add a device resource 00126 OptionsBuilder &OptionsBuilder::addResource(const DeviceResource *resource) { 00127 if (resource != NULL) { 00128 ((DeviceResource *)resource)->setOptions(this); 00129 this->m_device_resources.push_back((DeviceResource *)resource); 00130 } 00131 return *this; 00132 } 00133 00134 // add static resource 00135 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource) 00136 { 00137 if (resource != NULL) { 00138 ((StaticResource *)resource)->setOptions(this); 00139 this->m_static_resources.push_back((StaticResource *)resource); 00140 } 00141 return *this; 00142 } 00143 00144 // add dynamic resource 00145 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource) 00146 { 00147 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00148 return this->addResource(resource,OBS_PERIOD_MS,!(((DynamicResource *)resource)->implementsObservation())); 00149 } 00150 00151 // add dynamic resource 00152 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time) 00153 { 00154 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00155 return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation())); 00156 } 00157 00158 // add dynamic resource 00159 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer) 00160 { 00161 // ensure that the boolean isn't mistaken by the compiler for the obs period... 00162 return this->addResource(resource,OBS_PERIOD_MS,use_observer); 00163 } 00164 00165 // add dynamic resource 00166 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer) 00167 { 00168 if (resource != NULL) { 00169 this->m_dynamic_resources.push_back((DynamicResource *)resource); 00170 ((DynamicResource *)resource)->setOptions(this); 00171 ((DynamicResource *)resource)->setEndpoint((const void *)this->getEndpoint()); 00172 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) { 00173 #ifdef CONNECTOR_USING_THREADS 00174 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time); 00175 #else 00176 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time); 00177 #endif 00178 this->m_resource_observers.push_back(observer); 00179 00180 // immedate observation enablement option 00181 if (this->immedateObservationEnabled()) { 00182 observer->beginObservation(); 00183 } 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 Pre Shared Key 00211 OptionsBuilder &OptionsBuilder::setPreSharedKey(unsigned char psk[16]) 00212 { 00213 memset(this->m_psk,0,16); 00214 for(int i=0;i<16;++i) { 00215 this->m_psk[i] = psk[i]; 00216 } 00217 return *this; 00218 } 00219 00220 // set 802.15.4 Pre Shared Key Identity 00221 OptionsBuilder &OptionsBuilder::setPreSharedKeyIdentity(unsigned char psk_identity[2]) 00222 { 00223 memset(this->m_psk_identity,0,2); 00224 for(int i=0;i<2;++i) { 00225 this->m_psk_identity[i] = psk_identity[i]; 00226 } 00227 return *this; 00228 } 00229 00230 // set 802.15.4 Mesh Type 00231 OptionsBuilder &OptionsBuilder::setMeshType(MeshTypes mesh_type) 00232 { 00233 this->m_mesh_type = mesh_type; 00234 return *this; 00235 } 00236 00237 // set the CoAP Connection Type 00238 OptionsBuilder &OptionsBuilder::setCoAPConnectionType(CoAPConnectionTypes coap_connection_type) 00239 { 00240 this->m_coap_connection_type = coap_connection_type; 00241 return *this; 00242 } 00243 00244 // set the IP Address Type 00245 OptionsBuilder &OptionsBuilder::setIPAddressType(IPAddressTypes ip_address_type) 00246 { 00247 this->m_ip_address_type = ip_address_type; 00248 return *this; 00249 } 00250 00251 // build out our immutable self 00252 Options *OptionsBuilder::build() 00253 { 00254 return (Options *)this; 00255 } 00256 00257 // Enable/Disable immediate observationing 00258 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) { 00259 this->m_enable_immediate_observation = enable; 00260 return *this; 00261 } 00262 00263 // Enable/Disable GET-based control of observations 00264 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) { 00265 this->m_enable_get_obs_control = enable; 00266 return *this; 00267 } 00268 00269 // set the server certificate 00270 OptionsBuilder &OptionsBuilder::setServerCertificate(uint8_t cert[],int cert_size) { 00271 memset(this->m_server_cert,0,MAX_SERVER_CERT_LENGTH); 00272 int length = cert_size; 00273 if (length > MAX_SERVER_CERT_LENGTH) { 00274 length = MAX_SERVER_CERT_LENGTH; 00275 DEBUG_OUT("WARNING: Truncated Server Certificate: orig: %d bytes (trunc: %d bytes)\r\n",cert_size,length); 00276 } 00277 memcpy(this->m_server_cert,cert,length); 00278 this->m_server_cert_length = length; 00279 return *this; 00280 } 00281 00282 // set the client certificate 00283 OptionsBuilder &OptionsBuilder::setClientCertificate(uint8_t cert[],int cert_size) { 00284 memset(this->m_client_cert,0,MAX_CLIENT_CERT_LENGTH); 00285 int length = cert_size; 00286 if (length > MAX_CLIENT_CERT_LENGTH) { 00287 length = MAX_CLIENT_CERT_LENGTH; 00288 DEBUG_OUT("WARNING: Truncated Client Certificate: orig: %d bytes (trunc: %d bytes)\r\n",cert_size,length); 00289 } 00290 memcpy(this->m_client_cert,cert,length); 00291 this->m_client_cert_length = length; 00292 return *this; 00293 } 00294 00295 // set the client key 00296 OptionsBuilder &OptionsBuilder::setClientKey(uint8_t key[],int key_size) { 00297 memset(this->m_client_key,0,MAX_CLIENT_KEY_LENGTH); 00298 int length = key_size; 00299 if (length > MAX_CLIENT_KEY_LENGTH) { 00300 length = MAX_CLIENT_KEY_LENGTH; 00301 DEBUG_OUT("WARNING: Truncated Client Key: orig: %d bytes (trunc: %d bytes)\r\n",key_size,length); 00302 } 00303 memcpy(this->m_client_key,key,length); 00304 this->m_client_key_length = length; 00305 return *this; 00306 } 00307 00308 // set our endpoint 00309 void OptionsBuilder::setEndpoint(void *endpoint) { 00310 this->m_endpoint = endpoint; 00311 } 00312 00313 } // namespace Connector
Generated on Wed Jul 13 2022 21:59:32 by
1.7.2