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