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:
Thu Jul 13 18:48:54 2017 +0000
Revision:
122:4072e03884e4
Parent:
80:8b3bff9cc598
updated with EventQueue resource observer

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