use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Fri Feb 19 17:32:14 2016 +0000
Revision:
0:1f1f55e73248
Child:
8:f950fb1b78c0
initial checkin

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 0:1f1f55e73248 23 #include "mbed-connector-interface/OptionsBuilder.h"
ansond 0:1f1f55e73248 24
ansond 0:1f1f55e73248 25 // external included configuration file for the endpoint...
ansond 0:1f1f55e73248 26 #include "mbed-connector-interface/configuration.h"
ansond 0:1f1f55e73248 27
ansond 0:1f1f55e73248 28 // ResourceObserver support
ansond 0:1f1f55e73248 29 #include "mbed-connector-interface/ThreadedResourceObserver.h"
ansond 0:1f1f55e73248 30 #include "mbed-connector-interface/TickerResourceObserver.h"
ansond 0:1f1f55e73248 31
ansond 0:1f1f55e73248 32 // DEBUG
ansond 0:1f1f55e73248 33 #ifndef NDEBUG
ansond 0:1f1f55e73248 34 #define DEBUG_OUT(...) { printf(__VA_ARGS__); }
ansond 0:1f1f55e73248 35 #else
ansond 0:1f1f55e73248 36 #define DEBUG_OUT(...) /* nothing */
ansond 0:1f1f55e73248 37 #endif
ansond 0:1f1f55e73248 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 0:1f1f55e73248 45 this->m_domain = NSP_DOMAIN;
ansond 0:1f1f55e73248 46 this->m_endpoint_type = NSP_ENDPOINT_TYPE;
ansond 0:1f1f55e73248 47 this->m_node_name = NODE_NAME;
ansond 0:1f1f55e73248 48 this->m_channel_list = NODE_CHANNEL_LIST;
ansond 0:1f1f55e73248 49 this->m_reg_update_period = REG_UPDATE_PERIOD_MS;
ansond 0:1f1f55e73248 50 this->m_lifetime = REG_LIFETIME_SEC;
ansond 0:1f1f55e73248 51 this->m_connector_url = string(CONNECTOR_URL);
ansond 0:1f1f55e73248 52 memset(this->m_mac_address,0,NODE_MAC_ADDRESS_LENGTH);
ansond 0:1f1f55e73248 53 this->m_device_resources.clear();
ansond 0:1f1f55e73248 54 this->m_static_resources.clear();
ansond 0:1f1f55e73248 55 this->m_dynamic_resources.clear();
ansond 0:1f1f55e73248 56 this->m_resource_observers.clear();
ansond 0:1f1f55e73248 57 }
ansond 0:1f1f55e73248 58
ansond 0:1f1f55e73248 59 // Copy Constructor
ansond 0:1f1f55e73248 60 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob) : Options(ob)
ansond 0:1f1f55e73248 61 {
ansond 0:1f1f55e73248 62 this->m_lifetime = ob.m_lifetime;
ansond 0:1f1f55e73248 63 this->m_domain = ob.m_domain;
ansond 0:1f1f55e73248 64 this->m_endpoint_type = ob.m_endpoint_type;
ansond 0:1f1f55e73248 65 this->m_device_resources = ob.m_device_resources;
ansond 0:1f1f55e73248 66 this->m_static_resources = ob.m_static_resources;
ansond 0:1f1f55e73248 67 this->m_dynamic_resources = ob.m_dynamic_resources;
ansond 0:1f1f55e73248 68 this->m_resource_observers = ob.m_resource_observers;
ansond 0:1f1f55e73248 69 }
ansond 0:1f1f55e73248 70
ansond 0:1f1f55e73248 71 // Destructor
ansond 0:1f1f55e73248 72 OptionsBuilder::~OptionsBuilder()
ansond 0:1f1f55e73248 73 {
ansond 0:1f1f55e73248 74 this->m_device_resources.clear();
ansond 0:1f1f55e73248 75 this->m_static_resources.clear();
ansond 0:1f1f55e73248 76 this->m_dynamic_resources.clear();
ansond 0:1f1f55e73248 77 this->m_resource_observers.clear();
ansond 0:1f1f55e73248 78 }
ansond 0:1f1f55e73248 79
ansond 0:1f1f55e73248 80 // set lifetime
ansond 0:1f1f55e73248 81 OptionsBuilder &OptionsBuilder::setLifetime(int lifetime)
ansond 0:1f1f55e73248 82 {
ansond 0:1f1f55e73248 83 this->m_lifetime = lifetime;
ansond 0:1f1f55e73248 84 return *this;
ansond 0:1f1f55e73248 85 }
ansond 0:1f1f55e73248 86
ansond 0:1f1f55e73248 87 // set domain
ansond 0:1f1f55e73248 88 OptionsBuilder &OptionsBuilder::setDomain(const char *domain)
ansond 0:1f1f55e73248 89 {
ansond 0:1f1f55e73248 90 this->m_domain = string(domain);
ansond 0:1f1f55e73248 91 return *this;
ansond 0:1f1f55e73248 92 }
ansond 0:1f1f55e73248 93
ansond 0:1f1f55e73248 94 // set endpoint nodename
ansond 0:1f1f55e73248 95 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name)
ansond 0:1f1f55e73248 96 {
ansond 0:1f1f55e73248 97 this->m_node_name = string(node_name);
ansond 0:1f1f55e73248 98 return *this;
ansond 0:1f1f55e73248 99 }
ansond 0:1f1f55e73248 100
ansond 0:1f1f55e73248 101 // set lifetime
ansond 0:1f1f55e73248 102 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type)
ansond 0:1f1f55e73248 103 {
ansond 0:1f1f55e73248 104 this->m_endpoint_type = string(endpoint_type);
ansond 0:1f1f55e73248 105 return *this;
ansond 0:1f1f55e73248 106 }
ansond 0:1f1f55e73248 107
ansond 0:1f1f55e73248 108 // set low level radio channel list
ansond 0:1f1f55e73248 109 OptionsBuilder &OptionsBuilder::setRadioChannelList(const uint32_t channel_list)
ansond 0:1f1f55e73248 110 {
ansond 0:1f1f55e73248 111 this->m_channel_list = channel_list;
ansond 0:1f1f55e73248 112 return *this;
ansond 0:1f1f55e73248 113 }
ansond 0:1f1f55e73248 114
ansond 0:1f1f55e73248 115 // set mbed registration update period
ansond 0:1f1f55e73248 116 OptionsBuilder &OptionsBuilder::setRegUpdatePeriod(const int reg_update_period)
ansond 0:1f1f55e73248 117 {
ansond 0:1f1f55e73248 118 this->m_reg_update_period = reg_update_period;
ansond 0:1f1f55e73248 119 return *this;
ansond 0:1f1f55e73248 120 }
ansond 0:1f1f55e73248 121
ansond 0:1f1f55e73248 122 // set Connector URL
ansond 0:1f1f55e73248 123 OptionsBuilder &OptionsBuilder::setConnectorURL(const char *connector_url)
ansond 0:1f1f55e73248 124 {
ansond 0:1f1f55e73248 125 if (connector_url != NULL) {
ansond 0:1f1f55e73248 126 this->m_connector_url = string(connector_url);
ansond 0:1f1f55e73248 127 }
ansond 0:1f1f55e73248 128 return *this;
ansond 0:1f1f55e73248 129 }
ansond 0:1f1f55e73248 130
ansond 0:1f1f55e73248 131 // set MAC address
ansond 0:1f1f55e73248 132 OptionsBuilder &OptionsBuilder::setMACAddress(const uint8_t *mac_address,const int mac_address_length)
ansond 0:1f1f55e73248 133 {
ansond 0:1f1f55e73248 134 if (mac_address != NULL && mac_address_length > 0) {
ansond 0:1f1f55e73248 135 int length = mac_address_length;
ansond 0:1f1f55e73248 136 if (length > NODE_MAC_ADDRESS_LENGTH) length = NODE_MAC_ADDRESS_LENGTH;
ansond 0:1f1f55e73248 137 for(int i=0; i<length; ++i) this->m_mac_address[i] = mac_address[i];
ansond 0:1f1f55e73248 138 }
ansond 0:1f1f55e73248 139 return *this;
ansond 0:1f1f55e73248 140 }
ansond 0:1f1f55e73248 141
ansond 0:1f1f55e73248 142 // add a device resource
ansond 0:1f1f55e73248 143 OptionsBuilder &OptionsBuilder::addResource(const DeviceResource *resource) {
ansond 0:1f1f55e73248 144 if (resource != NULL) {
ansond 0:1f1f55e73248 145 ((DeviceResource *)resource)->setOptions(this);
ansond 0:1f1f55e73248 146 this->m_device_resources.push_back((DeviceResource *)resource);
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 0:1f1f55e73248 165 return this->addResource(resource,OBS_PERIOD_MS,!(((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 0:1f1f55e73248 179 return this->addResource(resource,OBS_PERIOD_MS,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 0:1f1f55e73248 188 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
ansond 0:1f1f55e73248 189 #ifdef CONNECTOR_USING_THREADS
ansond 0:1f1f55e73248 190 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 0:1f1f55e73248 191 #else
ansond 0:1f1f55e73248 192 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 0:1f1f55e73248 193 #endif
ansond 0:1f1f55e73248 194 this->m_resource_observers.push_back(observer);
ansond 0:1f1f55e73248 195
ansond 0:1f1f55e73248 196 // immedate observation enablement option
ansond 0:1f1f55e73248 197 if (this->immedateObservationEnabled()) {
ansond 0:1f1f55e73248 198 observer->beginObservation();
ansond 0:1f1f55e73248 199 }
ansond 0:1f1f55e73248 200 }
ansond 0:1f1f55e73248 201 }
ansond 0:1f1f55e73248 202 return *this;
ansond 0:1f1f55e73248 203 }
ansond 0:1f1f55e73248 204
ansond 0:1f1f55e73248 205 // set WiFi SSID
ansond 0:1f1f55e73248 206 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
ansond 0:1f1f55e73248 207 {
ansond 0:1f1f55e73248 208 this->m_wifi_ssid = string(ssid);
ansond 0:1f1f55e73248 209 return *this;
ansond 0:1f1f55e73248 210 }
ansond 0:1f1f55e73248 211
ansond 0:1f1f55e73248 212 // set WiFi AuthType
ansond 0:1f1f55e73248 213 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
ansond 0:1f1f55e73248 214 {
ansond 0:1f1f55e73248 215 this->m_wifi_auth_type = auth_type;
ansond 0:1f1f55e73248 216 return *this;
ansond 0:1f1f55e73248 217 }
ansond 0:1f1f55e73248 218
ansond 0:1f1f55e73248 219 // set WiFi AuthKey
ansond 0:1f1f55e73248 220 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
ansond 0:1f1f55e73248 221 {
ansond 0:1f1f55e73248 222 this->m_wifi_auth_key = string(auth_key);
ansond 0:1f1f55e73248 223 return *this;
ansond 0:1f1f55e73248 224 }
ansond 0:1f1f55e73248 225
ansond 0:1f1f55e73248 226 // set 802.15.4 Network ID
ansond 0:1f1f55e73248 227 OptionsBuilder &OptionsBuilder::setNetworkID(char *network_id)
ansond 0:1f1f55e73248 228 {
ansond 0:1f1f55e73248 229 this->m_network_id = string(network_id);
ansond 0:1f1f55e73248 230 return *this;
ansond 0:1f1f55e73248 231 }
ansond 0:1f1f55e73248 232
ansond 0:1f1f55e73248 233 // set 802.15.4 Radio Channel
ansond 0:1f1f55e73248 234 OptionsBuilder &OptionsBuilder::setRadioChannel(int channel)
ansond 0:1f1f55e73248 235 {
ansond 0:1f1f55e73248 236 this->m_channel = (uint8_t)channel;
ansond 0:1f1f55e73248 237 return *this;
ansond 0:1f1f55e73248 238 }
ansond 0:1f1f55e73248 239
ansond 0:1f1f55e73248 240 // build out our immutable self
ansond 0:1f1f55e73248 241 Options *OptionsBuilder::build()
ansond 0:1f1f55e73248 242 {
ansond 0:1f1f55e73248 243 return (Options *)this;
ansond 0:1f1f55e73248 244 }
ansond 0:1f1f55e73248 245
ansond 0:1f1f55e73248 246 // Enable/Disable immediate observationing
ansond 0:1f1f55e73248 247 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) {
ansond 0:1f1f55e73248 248 this->m_enable_immediate_observation = enable;
ansond 0:1f1f55e73248 249 return *this;
ansond 0:1f1f55e73248 250 }
ansond 0:1f1f55e73248 251
ansond 0:1f1f55e73248 252 // Enable/Disable GET-based control of observations
ansond 0:1f1f55e73248 253 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) {
ansond 0:1f1f55e73248 254 this->m_enable_get_obs_control = enable;
ansond 0:1f1f55e73248 255 return *this;
ansond 0:1f1f55e73248 256 }
ansond 0:1f1f55e73248 257
ansond 0:1f1f55e73248 258 // set the server certificate
ansond 0:1f1f55e73248 259 OptionsBuilder &OptionsBuilder::setServerCertificate(uint8_t cert[],int cert_size) {
ansond 0:1f1f55e73248 260 memset(this->m_server_cert,0,MAX_SERVER_CERT_LENGTH);
ansond 0:1f1f55e73248 261 int length = cert_size;
ansond 0:1f1f55e73248 262 if (length > MAX_SERVER_CERT_LENGTH) {
ansond 0:1f1f55e73248 263 length = MAX_SERVER_CERT_LENGTH;
ansond 0:1f1f55e73248 264 DEBUG_OUT("WARNING: Truncated Server Certificate: orig: %d bytes (trunc: %d bytes)\r\n",cert_size,length);
ansond 0:1f1f55e73248 265 }
ansond 0:1f1f55e73248 266 memcpy(this->m_server_cert,cert,length);
ansond 0:1f1f55e73248 267 this->m_server_cert_length = length;
ansond 0:1f1f55e73248 268 return *this;
ansond 0:1f1f55e73248 269 }
ansond 0:1f1f55e73248 270
ansond 0:1f1f55e73248 271 // set the client certificate
ansond 0:1f1f55e73248 272 OptionsBuilder &OptionsBuilder::setClientCertificate(uint8_t cert[],int cert_size) {
ansond 0:1f1f55e73248 273 memset(this->m_client_cert,0,MAX_CLIENT_CERT_LENGTH);
ansond 0:1f1f55e73248 274 int length = cert_size;
ansond 0:1f1f55e73248 275 if (length > MAX_CLIENT_CERT_LENGTH) {
ansond 0:1f1f55e73248 276 length = MAX_CLIENT_CERT_LENGTH;
ansond 0:1f1f55e73248 277 DEBUG_OUT("WARNING: Truncated Client Certificate: orig: %d bytes (trunc: %d bytes)\r\n",cert_size,length);
ansond 0:1f1f55e73248 278 }
ansond 0:1f1f55e73248 279 memcpy(this->m_client_cert,cert,length);
ansond 0:1f1f55e73248 280 this->m_client_cert_length = length;
ansond 0:1f1f55e73248 281 return *this;
ansond 0:1f1f55e73248 282 }
ansond 0:1f1f55e73248 283
ansond 0:1f1f55e73248 284 // set the client key
ansond 0:1f1f55e73248 285 OptionsBuilder &OptionsBuilder::setClientKey(uint8_t key[],int key_size) {
ansond 0:1f1f55e73248 286 memset(this->m_client_key,0,MAX_CLIENT_KEY_LENGTH);
ansond 0:1f1f55e73248 287 int length = key_size;
ansond 0:1f1f55e73248 288 if (length > MAX_CLIENT_KEY_LENGTH) {
ansond 0:1f1f55e73248 289 length = MAX_CLIENT_KEY_LENGTH;
ansond 0:1f1f55e73248 290 DEBUG_OUT("WARNING: Truncated Client Key: orig: %d bytes (trunc: %d bytes)\r\n",key_size,length);
ansond 0:1f1f55e73248 291 }
ansond 0:1f1f55e73248 292 memcpy(this->m_client_key,key,length);
ansond 0:1f1f55e73248 293 this->m_client_key_length = length;
ansond 0:1f1f55e73248 294 return *this;
ansond 0:1f1f55e73248 295 }
ansond 0:1f1f55e73248 296
ansond 0:1f1f55e73248 297 } // namespace Connector