mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Tue Apr 25 21:28:41 2017 +0000
Revision:
107:997c932c5cd4
Parent:
105:aeaaee8fbb1d
Child:
108:df7102357164
updates for R1.2

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 105:aeaaee8fbb1d 32 // R1.2+: external references for mbed_cloud_client_credentials.c
ansond 95:6a5c130be6c1 33 extern "C" char *MBED_CLOUD_DEV_ACCOUNT_ID;
ansond 105:aeaaee8fbb1d 34 extern "C" char *MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI;
ansond 105:aeaaee8fbb1d 35 extern "C" char *MBED_CLOUD_DEV_BOOTSTRAP_ENDPOINT_NAME;
ansond 105:aeaaee8fbb1d 36 extern "C" char *MBED_CLOUD_DEV_DEVICE_TYPE;
ansond 80:8b3bff9cc598 37 #endif
ansond 80:8b3bff9cc598 38
ansond 0:1f1f55e73248 39 // Connector namespace
ansond 0:1f1f55e73248 40 namespace Connector {
ansond 0:1f1f55e73248 41
ansond 0:1f1f55e73248 42 // Constructor
ansond 0:1f1f55e73248 43 OptionsBuilder::OptionsBuilder()
ansond 0:1f1f55e73248 44 {
ansond 52:8abdfa69b511 45 this->m_endpoint = NULL;
ansond 54:dfee8691c83a 46 this->m_domain = DEFAULT_DOMAIN;
ansond 71:5069a202e892 47 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 105:aeaaee8fbb1d 48 this->m_endpoint_type = (char *)MBED_CLOUD_DEV_DEVICE_TYPE;
ansond 105:aeaaee8fbb1d 49 #else
ansond 105:aeaaee8fbb1d 50 this->m_endpoint_type = DEFAULT_ENDPOINT_TYPE;
ansond 105:aeaaee8fbb1d 51 #endif
ansond 105:aeaaee8fbb1d 52 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 105:aeaaee8fbb1d 53 this->m_node_name = (char *)MBED_CLOUD_DEV_BOOTSTRAP_ENDPOINT_NAME;
ansond 72:6b1d37b5420a 54 #else
ansond 27:b8aaf7dc7023 55 this->m_node_name = NODE_NAME;
ansond 71:5069a202e892 56 #endif
ansond 27:b8aaf7dc7023 57 this->m_lifetime = REG_LIFETIME_SEC;
ansond 105:aeaaee8fbb1d 58 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 107:997c932c5cd4 59 this->m_connector_url = (char *)MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI;
ansond 105:aeaaee8fbb1d 60 #else
ansond 107:997c932c5cd4 61 this->m_connector_url = CONNECTOR_URL;
ansond 105:aeaaee8fbb1d 62 #endif
ansond 50:4f04727cbf91 63 this->m_server_cert = NULL;
ansond 49:c603a22495bb 64 this->m_server_cert_length = 0;
ansond 49:c603a22495bb 65 this->m_client_cert = NULL;
ansond 49:c603a22495bb 66 this->m_client_cert_length = 0;
ansond 49:c603a22495bb 67 this->m_client_key = NULL;
ansond 49:c603a22495bb 68 this->m_client_key_length = 0;
ansond 27:b8aaf7dc7023 69 this->m_device_resources_object = NULL;
ansond 27:b8aaf7dc7023 70 this->m_firmware_resources_object = NULL;
ansond 0:1f1f55e73248 71 this->m_static_resources.clear();
ansond 0:1f1f55e73248 72 this->m_dynamic_resources.clear();
ansond 0:1f1f55e73248 73 this->m_resource_observers.clear();
ansond 0:1f1f55e73248 74 }
ansond 0:1f1f55e73248 75
ansond 0:1f1f55e73248 76 // Copy Constructor
ansond 0:1f1f55e73248 77 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob) : Options(ob)
ansond 0:1f1f55e73248 78 {
ansond 54:dfee8691c83a 79 this->m_endpoint = ob.m_endpoint;
ansond 8:f950fb1b78c0 80 this->m_domain = ob.m_domain;
ansond 0:1f1f55e73248 81 this->m_endpoint_type = ob.m_endpoint_type;
ansond 8:f950fb1b78c0 82 this->m_node_name = ob.m_node_name;
ansond 8:f950fb1b78c0 83 this->m_lifetime = ob.m_lifetime;
ansond 8:f950fb1b78c0 84 this->m_connector_url = ob.m_connector_url;
ansond 50:4f04727cbf91 85 this->m_server_cert = ob.m_server_cert;
ansond 49:c603a22495bb 86 this->m_server_cert_length = ob.m_server_cert_length;
ansond 49:c603a22495bb 87 this->m_client_cert = ob.m_client_cert;
ansond 49:c603a22495bb 88 this->m_client_cert_length = ob.m_client_cert_length;
ansond 49:c603a22495bb 89 this->m_client_key = ob.m_client_key;
ansond 49:c603a22495bb 90 this->m_client_key_length= ob.m_client_key_length;
ansond 27:b8aaf7dc7023 91 this->m_device_resources_object = ob.m_device_resources_object;
ansond 27:b8aaf7dc7023 92 this->m_firmware_resources_object = ob.m_firmware_resources_object;
ansond 0:1f1f55e73248 93 this->m_static_resources = ob.m_static_resources;
ansond 0:1f1f55e73248 94 this->m_dynamic_resources = ob.m_dynamic_resources;
ansond 0:1f1f55e73248 95 this->m_resource_observers = ob.m_resource_observers;
ansond 52:8abdfa69b511 96 this->m_wifi_ssid = ob.m_wifi_ssid;
ansond 52:8abdfa69b511 97 this->m_wifi_auth_key = ob.m_wifi_auth_key;
ansond 52:8abdfa69b511 98 this->m_wifi_auth_type = ob.m_wifi_auth_type;
ansond 52:8abdfa69b511 99 this->m_coap_connection_type = ob.m_coap_connection_type;
ansond 52:8abdfa69b511 100 this->m_ip_address_type = ob.m_ip_address_type;
ansond 52:8abdfa69b511 101 this->m_enable_immediate_observation = ob.m_enable_immediate_observation;
ansond 52:8abdfa69b511 102 this->m_enable_get_obs_control = ob.m_enable_get_obs_control;
ansond 52:8abdfa69b511 103 this->m_endpoint = ob.m_endpoint;
ansond 0:1f1f55e73248 104 }
ansond 0:1f1f55e73248 105
ansond 0:1f1f55e73248 106 // Destructor
ansond 0:1f1f55e73248 107 OptionsBuilder::~OptionsBuilder()
ansond 0:1f1f55e73248 108 {
ansond 38:bb6d2be4d54c 109 this->m_device_resources_object = NULL;
ansond 38:bb6d2be4d54c 110 this->m_firmware_resources_object = NULL;
ansond 0:1f1f55e73248 111 this->m_static_resources.clear();
ansond 0:1f1f55e73248 112 this->m_dynamic_resources.clear();
ansond 0:1f1f55e73248 113 this->m_resource_observers.clear();
ansond 0:1f1f55e73248 114 }
ansond 0:1f1f55e73248 115
ansond 0:1f1f55e73248 116 // set lifetime
ansond 0:1f1f55e73248 117 OptionsBuilder &OptionsBuilder::setLifetime(int lifetime)
ansond 0:1f1f55e73248 118 {
ansond 0:1f1f55e73248 119 this->m_lifetime = lifetime;
ansond 0:1f1f55e73248 120 return *this;
ansond 0:1f1f55e73248 121 }
ansond 0:1f1f55e73248 122
ansond 0:1f1f55e73248 123 // set domain
ansond 0:1f1f55e73248 124 OptionsBuilder &OptionsBuilder::setDomain(const char *domain)
ansond 0:1f1f55e73248 125 {
ansond 0:1f1f55e73248 126 this->m_domain = string(domain);
ansond 0:1f1f55e73248 127 return *this;
ansond 0:1f1f55e73248 128 }
ansond 0:1f1f55e73248 129
ansond 0:1f1f55e73248 130 // set endpoint nodename
ansond 0:1f1f55e73248 131 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name)
ansond 0:1f1f55e73248 132 {
ansond 0:1f1f55e73248 133 this->m_node_name = string(node_name);
ansond 0:1f1f55e73248 134 return *this;
ansond 0:1f1f55e73248 135 }
ansond 0:1f1f55e73248 136
ansond 0:1f1f55e73248 137 // set lifetime
ansond 0:1f1f55e73248 138 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type)
ansond 0:1f1f55e73248 139 {
ansond 0:1f1f55e73248 140 this->m_endpoint_type = string(endpoint_type);
ansond 0:1f1f55e73248 141 return *this;
ansond 0:1f1f55e73248 142 }
ansond 0:1f1f55e73248 143
ansond 0:1f1f55e73248 144 // set Connector URL
ansond 0:1f1f55e73248 145 OptionsBuilder &OptionsBuilder::setConnectorURL(const char *connector_url)
ansond 0:1f1f55e73248 146 {
ansond 0:1f1f55e73248 147 if (connector_url != NULL) {
ansond 0:1f1f55e73248 148 this->m_connector_url = string(connector_url);
ansond 0:1f1f55e73248 149 }
ansond 0:1f1f55e73248 150 return *this;
ansond 0:1f1f55e73248 151 }
ansond 0:1f1f55e73248 152
ansond 27:b8aaf7dc7023 153 // add the device resources object
ansond 27:b8aaf7dc7023 154 OptionsBuilder &OptionsBuilder::setDeviceResourcesObject(const void *device_resources_object)
ansond 27:b8aaf7dc7023 155 {
ansond 27:b8aaf7dc7023 156 if (device_resources_object != NULL) {
ansond 27:b8aaf7dc7023 157 this->m_device_resources_object = (void *)device_resources_object;
ansond 27:b8aaf7dc7023 158 }
ansond 27:b8aaf7dc7023 159 return *this;
ansond 27:b8aaf7dc7023 160 }
ansond 27:b8aaf7dc7023 161
ansond 27:b8aaf7dc7023 162 // add the firmware resources object
ansond 27:b8aaf7dc7023 163 OptionsBuilder &OptionsBuilder::setFirmwareResourcesObject(const void *firmware_resources_object)
ansond 27:b8aaf7dc7023 164 {
ansond 27:b8aaf7dc7023 165 if (firmware_resources_object != NULL) {
ansond 27:b8aaf7dc7023 166 this->m_firmware_resources_object = (void *)firmware_resources_object;
ansond 0:1f1f55e73248 167 }
ansond 0:1f1f55e73248 168 return *this;
ansond 0:1f1f55e73248 169 }
ansond 0:1f1f55e73248 170
ansond 0:1f1f55e73248 171 // add static resource
ansond 0:1f1f55e73248 172 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource)
ansond 0:1f1f55e73248 173 {
ansond 0:1f1f55e73248 174 if (resource != NULL) {
ansond 0:1f1f55e73248 175 ((StaticResource *)resource)->setOptions(this);
ansond 0:1f1f55e73248 176 this->m_static_resources.push_back((StaticResource *)resource);
ansond 0:1f1f55e73248 177 }
ansond 0:1f1f55e73248 178 return *this;
ansond 0:1f1f55e73248 179 }
ansond 0:1f1f55e73248 180
ansond 0:1f1f55e73248 181 // add dynamic resource
ansond 0:1f1f55e73248 182 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
ansond 0:1f1f55e73248 183 {
ansond 0:1f1f55e73248 184 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 54:dfee8691c83a 185 return this->addResource(resource,DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation()));
ansond 0:1f1f55e73248 186 }
ansond 0:1f1f55e73248 187
ansond 0:1f1f55e73248 188 // add dynamic resource
ansond 0:1f1f55e73248 189 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time)
ansond 0:1f1f55e73248 190 {
ansond 0:1f1f55e73248 191 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 0:1f1f55e73248 192 return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation()));
ansond 0:1f1f55e73248 193 }
ansond 0:1f1f55e73248 194
ansond 0:1f1f55e73248 195 // add dynamic resource
ansond 0:1f1f55e73248 196 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer)
ansond 0:1f1f55e73248 197 {
ansond 0:1f1f55e73248 198 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 54:dfee8691c83a 199 return this->addResource(resource,DEFAULT_OBS_PERIOD,use_observer);
ansond 0:1f1f55e73248 200 }
ansond 0:1f1f55e73248 201
ansond 0:1f1f55e73248 202 // add dynamic resource
ansond 0:1f1f55e73248 203 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
ansond 0:1f1f55e73248 204 {
ansond 0:1f1f55e73248 205 if (resource != NULL) {
ansond 0:1f1f55e73248 206 this->m_dynamic_resources.push_back((DynamicResource *)resource);
ansond 0:1f1f55e73248 207 ((DynamicResource *)resource)->setOptions(this);
ansond 17:defb680f8fce 208 ((DynamicResource *)resource)->setEndpoint((const void *)this->getEndpoint());
ansond 0:1f1f55e73248 209 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
ansond 34:a10d65907549 210 // Establish the appropriate ResourceObserver
ansond 27:b8aaf7dc7023 211 #if defined (MCI_MINAR_SCHEDULER)
ansond 34:a10d65907549 212 // Minar-based Scheduler ResourceObserver
ansond 27:b8aaf7dc7023 213 MinarResourceObserver *observer = new MinarResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 27:b8aaf7dc7023 214 #else
ansond 27:b8aaf7dc7023 215 #ifdef CONNECTOR_USING_THREADS
ansond 34:a10d65907549 216 // mbedOS RTOS Thread ResourceObserver
ansond 0:1f1f55e73248 217 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 34:a10d65907549 218 #endif
ansond 34:a10d65907549 219 #ifdef CONNECTOR_USING_TICKER
ansond 34:a10d65907549 220 // mbed Ticker ResourceObserver
ansond 0:1f1f55e73248 221 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 27:b8aaf7dc7023 222 #endif
ansond 0:1f1f55e73248 223 #endif
ansond 34:a10d65907549 224 // If no observer type is set in mbed-connector-interface/configuration.h (EndpointNetwork lib), then "observer" will be unresolved
ansond 0:1f1f55e73248 225 this->m_resource_observers.push_back(observer);
ansond 0:1f1f55e73248 226
ansond 0:1f1f55e73248 227 // immedate observation enablement option
ansond 0:1f1f55e73248 228 if (this->immedateObservationEnabled()) {
ansond 0:1f1f55e73248 229 observer->beginObservation();
ansond 0:1f1f55e73248 230 }
ansond 0:1f1f55e73248 231 }
ansond 0:1f1f55e73248 232 }
ansond 0:1f1f55e73248 233 return *this;
ansond 0:1f1f55e73248 234 }
ansond 0:1f1f55e73248 235
ansond 0:1f1f55e73248 236 // set WiFi SSID
ansond 0:1f1f55e73248 237 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
ansond 0:1f1f55e73248 238 {
ansond 0:1f1f55e73248 239 this->m_wifi_ssid = string(ssid);
ansond 0:1f1f55e73248 240 return *this;
ansond 0:1f1f55e73248 241 }
ansond 0:1f1f55e73248 242
ansond 0:1f1f55e73248 243 // set WiFi AuthType
ansond 0:1f1f55e73248 244 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
ansond 0:1f1f55e73248 245 {
ansond 0:1f1f55e73248 246 this->m_wifi_auth_type = auth_type;
ansond 0:1f1f55e73248 247 return *this;
ansond 0:1f1f55e73248 248 }
ansond 0:1f1f55e73248 249
ansond 0:1f1f55e73248 250 // set WiFi AuthKey
ansond 0:1f1f55e73248 251 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
ansond 0:1f1f55e73248 252 {
ansond 0:1f1f55e73248 253 this->m_wifi_auth_key = string(auth_key);
ansond 0:1f1f55e73248 254 return *this;
ansond 0:1f1f55e73248 255 }
ansond 0:1f1f55e73248 256
ansond 10:3f79b5e67c22 257 // set the CoAP Connection Type
ansond 10:3f79b5e67c22 258 OptionsBuilder &OptionsBuilder::setCoAPConnectionType(CoAPConnectionTypes coap_connection_type)
ansond 10:3f79b5e67c22 259 {
ansond 10:3f79b5e67c22 260 this->m_coap_connection_type = coap_connection_type;
ansond 10:3f79b5e67c22 261 return *this;
ansond 10:3f79b5e67c22 262 }
ansond 10:3f79b5e67c22 263
ansond 10:3f79b5e67c22 264 // set the IP Address Type
ansond 10:3f79b5e67c22 265 OptionsBuilder &OptionsBuilder::setIPAddressType(IPAddressTypes ip_address_type)
ansond 10:3f79b5e67c22 266 {
ansond 10:3f79b5e67c22 267 this->m_ip_address_type = ip_address_type;
ansond 10:3f79b5e67c22 268 return *this;
ansond 10:3f79b5e67c22 269 }
ansond 10:3f79b5e67c22 270
ansond 0:1f1f55e73248 271 // build out our immutable self
ansond 0:1f1f55e73248 272 Options *OptionsBuilder::build()
ansond 0:1f1f55e73248 273 {
ansond 0:1f1f55e73248 274 return (Options *)this;
ansond 0:1f1f55e73248 275 }
ansond 0:1f1f55e73248 276
ansond 0:1f1f55e73248 277 // Enable/Disable immediate observationing
ansond 0:1f1f55e73248 278 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) {
ansond 0:1f1f55e73248 279 this->m_enable_immediate_observation = enable;
ansond 0:1f1f55e73248 280 return *this;
ansond 0:1f1f55e73248 281 }
ansond 0:1f1f55e73248 282
ansond 0:1f1f55e73248 283 // Enable/Disable GET-based control of observations
ansond 0:1f1f55e73248 284 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) {
ansond 0:1f1f55e73248 285 this->m_enable_get_obs_control = enable;
ansond 0:1f1f55e73248 286 return *this;
ansond 0:1f1f55e73248 287 }
ansond 0:1f1f55e73248 288
ansond 0:1f1f55e73248 289 // set the server certificate
ansond 49:c603a22495bb 290 OptionsBuilder &OptionsBuilder::setServerCertificate(uint8_t *cert,int cert_size) {
ansond 49:c603a22495bb 291 this->m_server_cert = cert;
ansond 49:c603a22495bb 292 this->m_server_cert_length = cert_size;
ansond 0:1f1f55e73248 293 return *this;
ansond 0:1f1f55e73248 294 }
ansond 0:1f1f55e73248 295
ansond 0:1f1f55e73248 296 // set the client certificate
ansond 49:c603a22495bb 297 OptionsBuilder &OptionsBuilder::setClientCertificate(uint8_t *cert,int cert_size) {
ansond 49:c603a22495bb 298 this->m_client_cert = cert;
ansond 49:c603a22495bb 299 this->m_client_cert_length = cert_size;
ansond 0:1f1f55e73248 300 return *this;
ansond 0:1f1f55e73248 301 }
ansond 0:1f1f55e73248 302
ansond 0:1f1f55e73248 303 // set the client key
ansond 49:c603a22495bb 304 OptionsBuilder &OptionsBuilder::setClientKey(uint8_t *key,int key_size) {
ansond 49:c603a22495bb 305 this->m_client_key = key;
ansond 49:c603a22495bb 306 this->m_client_key_length = key_size;
ansond 0:1f1f55e73248 307 return *this;
ansond 0:1f1f55e73248 308 }
ansond 0:1f1f55e73248 309
ansond 22:68fcdc40b397 310 // set our endpoint
ansond 22:68fcdc40b397 311 void OptionsBuilder::setEndpoint(void *endpoint) {
ansond 22:68fcdc40b397 312 this->m_endpoint = endpoint;
ansond 22:68fcdc40b397 313 }
ansond 22:68fcdc40b397 314
ansond 0:1f1f55e73248 315 } // namespace Connector