use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
Maggie17
Date:
Mon Nov 28 15:52:56 2016 +0000
Revision:
90:b738617379a6
Parent:
80:8b3bff9cc598
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:1f1f55e73248 1 /**
ansond 0:1f1f55e73248 2 * @file OptionsBuilder.cpp
ansond 0:1f1f55e73248 3 * @brief mbed CoAP OptionsBuilder class implementation
ansond 0:1f1f55e73248 4 * @author Doug Anson/Chris Paola
ansond 0:1f1f55e73248 5 * @version 1.0
ansond 0:1f1f55e73248 6 * @see
ansond 0:1f1f55e73248 7 *
ansond 0:1f1f55e73248 8 * Copyright (c) 2014
ansond 0:1f1f55e73248 9 *
ansond 0:1f1f55e73248 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:1f1f55e73248 11 * you may not use this file except in compliance with the License.
ansond 0:1f1f55e73248 12 * You may obtain a copy of the License at
ansond 0:1f1f55e73248 13 *
ansond 0:1f1f55e73248 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:1f1f55e73248 15 *
ansond 0:1f1f55e73248 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:1f1f55e73248 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:1f1f55e73248 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:1f1f55e73248 19 * See the License for the specific language governing permissions and
ansond 0:1f1f55e73248 20 * limitations under the License.
ansond 0:1f1f55e73248 21 */
ansond 0:1f1f55e73248 22
ansond 33:1d0b855df5a5 23 // Class support
ansond 0:1f1f55e73248 24 #include "mbed-connector-interface/OptionsBuilder.h"
ansond 0:1f1f55e73248 25
ansond 54:dfee8691c83a 26 // ResourceObserver support
ansond 0:1f1f55e73248 27 #include "mbed-connector-interface/ThreadedResourceObserver.h"
ansond 0:1f1f55e73248 28 #include "mbed-connector-interface/TickerResourceObserver.h"
ansond 27:b8aaf7dc7023 29 #include "mbed-connector-interface/MinarResourceObserver.h"
ansond 0:1f1f55e73248 30
ansond 80:8b3bff9cc598 31 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 80:8b3bff9cc598 32 extern "C" char gIdcDevSecurityAccountId[33];
ansond 80:8b3bff9cc598 33 #endif
ansond 80:8b3bff9cc598 34
ansond 0:1f1f55e73248 35 // Connector namespace
ansond 0:1f1f55e73248 36 namespace Connector {
ansond 0:1f1f55e73248 37
ansond 0:1f1f55e73248 38 // Constructor
ansond 0:1f1f55e73248 39 OptionsBuilder::OptionsBuilder()
ansond 0:1f1f55e73248 40 {
ansond 52:8abdfa69b511 41 this->m_endpoint = NULL;
ansond 54:dfee8691c83a 42 this->m_domain = DEFAULT_DOMAIN;
ansond 54:dfee8691c83a 43 this->m_endpoint_type = DEFAULT_ENDPOINT_TYPE;
ansond 71:5069a202e892 44 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 71:5069a202e892 45 this->m_node_name = (char *)gIdcDevSecurityAccountId;
ansond 72:6b1d37b5420a 46 #else
ansond 27:b8aaf7dc7023 47 this->m_node_name = NODE_NAME;
ansond 71:5069a202e892 48 #endif
ansond 27:b8aaf7dc7023 49 this->m_lifetime = REG_LIFETIME_SEC;
ansond 27:b8aaf7dc7023 50 this->m_connector_url = string(CONNECTOR_URL);
ansond 50:4f04727cbf91 51 this->m_server_cert = NULL;
ansond 49:c603a22495bb 52 this->m_server_cert_length = 0;
ansond 49:c603a22495bb 53 this->m_client_cert = NULL;
ansond 49:c603a22495bb 54 this->m_client_cert_length = 0;
ansond 49:c603a22495bb 55 this->m_client_key = NULL;
ansond 49:c603a22495bb 56 this->m_client_key_length = 0;
ansond 27:b8aaf7dc7023 57 this->m_device_resources_object = NULL;
ansond 27:b8aaf7dc7023 58 this->m_firmware_resources_object = NULL;
ansond 0:1f1f55e73248 59 this->m_static_resources.clear();
ansond 0:1f1f55e73248 60 this->m_dynamic_resources.clear();
ansond 0:1f1f55e73248 61 this->m_resource_observers.clear();
ansond 0:1f1f55e73248 62 }
ansond 0:1f1f55e73248 63
ansond 0:1f1f55e73248 64 // Copy Constructor
ansond 0:1f1f55e73248 65 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob) : Options(ob)
ansond 0:1f1f55e73248 66 {
ansond 54:dfee8691c83a 67 this->m_endpoint = ob.m_endpoint;
ansond 8:f950fb1b78c0 68 this->m_domain = ob.m_domain;
ansond 0:1f1f55e73248 69 this->m_endpoint_type = ob.m_endpoint_type;
ansond 8:f950fb1b78c0 70 this->m_node_name = ob.m_node_name;
ansond 8:f950fb1b78c0 71 this->m_lifetime = ob.m_lifetime;
ansond 8:f950fb1b78c0 72 this->m_connector_url = ob.m_connector_url;
ansond 50:4f04727cbf91 73 this->m_server_cert = ob.m_server_cert;
ansond 49:c603a22495bb 74 this->m_server_cert_length = ob.m_server_cert_length;
ansond 49:c603a22495bb 75 this->m_client_cert = ob.m_client_cert;
ansond 49:c603a22495bb 76 this->m_client_cert_length = ob.m_client_cert_length;
ansond 49:c603a22495bb 77 this->m_client_key = ob.m_client_key;
ansond 49:c603a22495bb 78 this->m_client_key_length= ob.m_client_key_length;
ansond 27:b8aaf7dc7023 79 this->m_device_resources_object = ob.m_device_resources_object;
ansond 27:b8aaf7dc7023 80 this->m_firmware_resources_object = ob.m_firmware_resources_object;
ansond 0:1f1f55e73248 81 this->m_static_resources = ob.m_static_resources;
ansond 0:1f1f55e73248 82 this->m_dynamic_resources = ob.m_dynamic_resources;
ansond 0:1f1f55e73248 83 this->m_resource_observers = ob.m_resource_observers;
ansond 52:8abdfa69b511 84 this->m_wifi_ssid = ob.m_wifi_ssid;
ansond 52:8abdfa69b511 85 this->m_wifi_auth_key = ob.m_wifi_auth_key;
ansond 52:8abdfa69b511 86 this->m_wifi_auth_type = ob.m_wifi_auth_type;
ansond 52:8abdfa69b511 87 this->m_coap_connection_type = ob.m_coap_connection_type;
ansond 52:8abdfa69b511 88 this->m_ip_address_type = ob.m_ip_address_type;
ansond 52:8abdfa69b511 89 this->m_enable_immediate_observation = ob.m_enable_immediate_observation;
ansond 52:8abdfa69b511 90 this->m_enable_get_obs_control = ob.m_enable_get_obs_control;
ansond 52:8abdfa69b511 91 this->m_endpoint = ob.m_endpoint;
ansond 0:1f1f55e73248 92 }
ansond 0:1f1f55e73248 93
ansond 0:1f1f55e73248 94 // Destructor
ansond 0:1f1f55e73248 95 OptionsBuilder::~OptionsBuilder()
ansond 0:1f1f55e73248 96 {
ansond 38:bb6d2be4d54c 97 this->m_device_resources_object = NULL;
ansond 38:bb6d2be4d54c 98 this->m_firmware_resources_object = NULL;
ansond 0:1f1f55e73248 99 this->m_static_resources.clear();
ansond 0:1f1f55e73248 100 this->m_dynamic_resources.clear();
ansond 0:1f1f55e73248 101 this->m_resource_observers.clear();
ansond 0:1f1f55e73248 102 }
ansond 0:1f1f55e73248 103
ansond 0:1f1f55e73248 104 // set lifetime
ansond 0:1f1f55e73248 105 OptionsBuilder &OptionsBuilder::setLifetime(int lifetime)
ansond 0:1f1f55e73248 106 {
ansond 0:1f1f55e73248 107 this->m_lifetime = lifetime;
ansond 0:1f1f55e73248 108 return *this;
ansond 0:1f1f55e73248 109 }
ansond 0:1f1f55e73248 110
ansond 0:1f1f55e73248 111 // set domain
ansond 0:1f1f55e73248 112 OptionsBuilder &OptionsBuilder::setDomain(const char *domain)
ansond 0:1f1f55e73248 113 {
ansond 0:1f1f55e73248 114 this->m_domain = string(domain);
ansond 0:1f1f55e73248 115 return *this;
ansond 0:1f1f55e73248 116 }
ansond 0:1f1f55e73248 117
ansond 0:1f1f55e73248 118 // set endpoint nodename
ansond 0:1f1f55e73248 119 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name)
ansond 0:1f1f55e73248 120 {
ansond 0:1f1f55e73248 121 this->m_node_name = string(node_name);
ansond 0:1f1f55e73248 122 return *this;
ansond 0:1f1f55e73248 123 }
ansond 0:1f1f55e73248 124
ansond 0:1f1f55e73248 125 // set lifetime
ansond 0:1f1f55e73248 126 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type)
ansond 0:1f1f55e73248 127 {
ansond 0:1f1f55e73248 128 this->m_endpoint_type = string(endpoint_type);
ansond 0:1f1f55e73248 129 return *this;
ansond 0:1f1f55e73248 130 }
ansond 0:1f1f55e73248 131
ansond 0:1f1f55e73248 132 // set Connector URL
ansond 0:1f1f55e73248 133 OptionsBuilder &OptionsBuilder::setConnectorURL(const char *connector_url)
ansond 0:1f1f55e73248 134 {
ansond 0:1f1f55e73248 135 if (connector_url != NULL) {
ansond 0:1f1f55e73248 136 this->m_connector_url = string(connector_url);
ansond 0:1f1f55e73248 137 }
ansond 0:1f1f55e73248 138 return *this;
ansond 0:1f1f55e73248 139 }
ansond 0:1f1f55e73248 140
ansond 27:b8aaf7dc7023 141 // add the device resources object
ansond 27:b8aaf7dc7023 142 OptionsBuilder &OptionsBuilder::setDeviceResourcesObject(const void *device_resources_object)
ansond 27:b8aaf7dc7023 143 {
ansond 27:b8aaf7dc7023 144 if (device_resources_object != NULL) {
ansond 27:b8aaf7dc7023 145 this->m_device_resources_object = (void *)device_resources_object;
ansond 27:b8aaf7dc7023 146 }
ansond 27:b8aaf7dc7023 147 return *this;
ansond 27:b8aaf7dc7023 148 }
ansond 27:b8aaf7dc7023 149
ansond 27:b8aaf7dc7023 150 // add the firmware resources object
ansond 27:b8aaf7dc7023 151 OptionsBuilder &OptionsBuilder::setFirmwareResourcesObject(const void *firmware_resources_object)
ansond 27:b8aaf7dc7023 152 {
ansond 27:b8aaf7dc7023 153 if (firmware_resources_object != NULL) {
ansond 27:b8aaf7dc7023 154 this->m_firmware_resources_object = (void *)firmware_resources_object;
ansond 0:1f1f55e73248 155 }
ansond 0:1f1f55e73248 156 return *this;
ansond 0:1f1f55e73248 157 }
ansond 0:1f1f55e73248 158
ansond 0:1f1f55e73248 159 // add static resource
ansond 0:1f1f55e73248 160 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource)
ansond 0:1f1f55e73248 161 {
ansond 0:1f1f55e73248 162 if (resource != NULL) {
ansond 0:1f1f55e73248 163 ((StaticResource *)resource)->setOptions(this);
ansond 0:1f1f55e73248 164 this->m_static_resources.push_back((StaticResource *)resource);
ansond 0:1f1f55e73248 165 }
ansond 0:1f1f55e73248 166 return *this;
ansond 0:1f1f55e73248 167 }
ansond 0:1f1f55e73248 168
ansond 0:1f1f55e73248 169 // add dynamic resource
ansond 0:1f1f55e73248 170 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
ansond 0:1f1f55e73248 171 {
ansond 0:1f1f55e73248 172 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 54:dfee8691c83a 173 return this->addResource(resource,DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation()));
ansond 0:1f1f55e73248 174 }
ansond 0:1f1f55e73248 175
ansond 0:1f1f55e73248 176 // add dynamic resource
ansond 0:1f1f55e73248 177 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time)
ansond 0:1f1f55e73248 178 {
ansond 0:1f1f55e73248 179 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 0:1f1f55e73248 180 return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation()));
ansond 0:1f1f55e73248 181 }
ansond 0:1f1f55e73248 182
ansond 0:1f1f55e73248 183 // add dynamic resource
ansond 0:1f1f55e73248 184 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer)
ansond 0:1f1f55e73248 185 {
ansond 0:1f1f55e73248 186 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 54:dfee8691c83a 187 return this->addResource(resource,DEFAULT_OBS_PERIOD,use_observer);
ansond 0:1f1f55e73248 188 }
ansond 0:1f1f55e73248 189
ansond 0:1f1f55e73248 190 // add dynamic resource
ansond 0:1f1f55e73248 191 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
ansond 0:1f1f55e73248 192 {
ansond 0:1f1f55e73248 193 if (resource != NULL) {
ansond 0:1f1f55e73248 194 this->m_dynamic_resources.push_back((DynamicResource *)resource);
ansond 0:1f1f55e73248 195 ((DynamicResource *)resource)->setOptions(this);
ansond 17:defb680f8fce 196 ((DynamicResource *)resource)->setEndpoint((const void *)this->getEndpoint());
ansond 0:1f1f55e73248 197 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
ansond 34:a10d65907549 198 // Establish the appropriate ResourceObserver
ansond 27:b8aaf7dc7023 199 #if defined (MCI_MINAR_SCHEDULER)
ansond 34:a10d65907549 200 // Minar-based Scheduler ResourceObserver
ansond 27:b8aaf7dc7023 201 MinarResourceObserver *observer = new MinarResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 27:b8aaf7dc7023 202 #else
ansond 27:b8aaf7dc7023 203 #ifdef CONNECTOR_USING_THREADS
ansond 34:a10d65907549 204 // mbedOS RTOS Thread ResourceObserver
ansond 0:1f1f55e73248 205 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 34:a10d65907549 206 #endif
ansond 34:a10d65907549 207 #ifdef CONNECTOR_USING_TICKER
ansond 34:a10d65907549 208 // mbed Ticker ResourceObserver
ansond 0:1f1f55e73248 209 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 27:b8aaf7dc7023 210 #endif
ansond 0:1f1f55e73248 211 #endif
ansond 34:a10d65907549 212 // If no observer type is set in mbed-connector-interface/configuration.h (EndpointNetwork lib), then "observer" will be unresolved
ansond 0:1f1f55e73248 213 this->m_resource_observers.push_back(observer);
ansond 0:1f1f55e73248 214
ansond 0:1f1f55e73248 215 // immedate observation enablement option
ansond 0:1f1f55e73248 216 if (this->immedateObservationEnabled()) {
ansond 0:1f1f55e73248 217 observer->beginObservation();
ansond 0:1f1f55e73248 218 }
ansond 0:1f1f55e73248 219 }
ansond 0:1f1f55e73248 220 }
ansond 0:1f1f55e73248 221 return *this;
ansond 0:1f1f55e73248 222 }
ansond 0:1f1f55e73248 223
ansond 0:1f1f55e73248 224 // set WiFi SSID
ansond 0:1f1f55e73248 225 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
ansond 0:1f1f55e73248 226 {
ansond 0:1f1f55e73248 227 this->m_wifi_ssid = string(ssid);
ansond 0:1f1f55e73248 228 return *this;
ansond 0:1f1f55e73248 229 }
ansond 0:1f1f55e73248 230
ansond 0:1f1f55e73248 231 // set WiFi AuthType
ansond 0:1f1f55e73248 232 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
ansond 0:1f1f55e73248 233 {
ansond 0:1f1f55e73248 234 this->m_wifi_auth_type = auth_type;
ansond 0:1f1f55e73248 235 return *this;
ansond 0:1f1f55e73248 236 }
ansond 0:1f1f55e73248 237
ansond 0:1f1f55e73248 238 // set WiFi AuthKey
ansond 0:1f1f55e73248 239 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
ansond 0:1f1f55e73248 240 {
ansond 0:1f1f55e73248 241 this->m_wifi_auth_key = string(auth_key);
ansond 0:1f1f55e73248 242 return *this;
ansond 0:1f1f55e73248 243 }
ansond 0:1f1f55e73248 244
ansond 10:3f79b5e67c22 245 // set the CoAP Connection Type
ansond 10:3f79b5e67c22 246 OptionsBuilder &OptionsBuilder::setCoAPConnectionType(CoAPConnectionTypes coap_connection_type)
ansond 10:3f79b5e67c22 247 {
ansond 10:3f79b5e67c22 248 this->m_coap_connection_type = coap_connection_type;
ansond 10:3f79b5e67c22 249 return *this;
ansond 10:3f79b5e67c22 250 }
ansond 10:3f79b5e67c22 251
ansond 10:3f79b5e67c22 252 // set the IP Address Type
ansond 10:3f79b5e67c22 253 OptionsBuilder &OptionsBuilder::setIPAddressType(IPAddressTypes ip_address_type)
ansond 10:3f79b5e67c22 254 {
ansond 10:3f79b5e67c22 255 this->m_ip_address_type = ip_address_type;
ansond 10:3f79b5e67c22 256 return *this;
ansond 10:3f79b5e67c22 257 }
ansond 10:3f79b5e67c22 258
ansond 0:1f1f55e73248 259 // build out our immutable self
ansond 0:1f1f55e73248 260 Options *OptionsBuilder::build()
ansond 0:1f1f55e73248 261 {
ansond 0:1f1f55e73248 262 return (Options *)this;
ansond 0:1f1f55e73248 263 }
ansond 0:1f1f55e73248 264
ansond 0:1f1f55e73248 265 // Enable/Disable immediate observationing
ansond 0:1f1f55e73248 266 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) {
ansond 0:1f1f55e73248 267 this->m_enable_immediate_observation = enable;
ansond 0:1f1f55e73248 268 return *this;
ansond 0:1f1f55e73248 269 }
ansond 0:1f1f55e73248 270
ansond 0:1f1f55e73248 271 // Enable/Disable GET-based control of observations
ansond 0:1f1f55e73248 272 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) {
ansond 0:1f1f55e73248 273 this->m_enable_get_obs_control = enable;
ansond 0:1f1f55e73248 274 return *this;
ansond 0:1f1f55e73248 275 }
ansond 0:1f1f55e73248 276
ansond 0:1f1f55e73248 277 // set the server certificate
ansond 49:c603a22495bb 278 OptionsBuilder &OptionsBuilder::setServerCertificate(uint8_t *cert,int cert_size) {
ansond 49:c603a22495bb 279 this->m_server_cert = cert;
ansond 49:c603a22495bb 280 this->m_server_cert_length = cert_size;
ansond 0:1f1f55e73248 281 return *this;
ansond 0:1f1f55e73248 282 }
ansond 0:1f1f55e73248 283
ansond 0:1f1f55e73248 284 // set the client certificate
ansond 49:c603a22495bb 285 OptionsBuilder &OptionsBuilder::setClientCertificate(uint8_t *cert,int cert_size) {
ansond 49:c603a22495bb 286 this->m_client_cert = cert;
ansond 49:c603a22495bb 287 this->m_client_cert_length = cert_size;
ansond 0:1f1f55e73248 288 return *this;
ansond 0:1f1f55e73248 289 }
ansond 0:1f1f55e73248 290
ansond 0:1f1f55e73248 291 // set the client key
ansond 49:c603a22495bb 292 OptionsBuilder &OptionsBuilder::setClientKey(uint8_t *key,int key_size) {
ansond 49:c603a22495bb 293 this->m_client_key = key;
ansond 49:c603a22495bb 294 this->m_client_key_length = key_size;
ansond 0:1f1f55e73248 295 return *this;
ansond 0:1f1f55e73248 296 }
ansond 0:1f1f55e73248 297
ansond 22:68fcdc40b397 298 // set our endpoint
ansond 22:68fcdc40b397 299 void OptionsBuilder::setEndpoint(void *endpoint) {
ansond 22:68fcdc40b397 300 this->m_endpoint = endpoint;
ansond 22:68fcdc40b397 301 }
ansond 22:68fcdc40b397 302
ansond 0:1f1f55e73248 303 } // namespace Connector