added max-age and content-format

Fork of mbedConnectorInterface by Doug Anson

Committer:
michaeljkoster
Date:
Tue Apr 14 04:00:01 2015 +0000
Revision:
52:822611156e35
Parent:
48:4b9ee3e32f93
added interfaces for max-age and content-format

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:b438482ebbfc 1 /**
ansond 0:b438482ebbfc 2 * @file OptionsBuilder.cpp
ansond 0:b438482ebbfc 3 * @brief mbed CoAP OptionsBuilder class implementation
ansond 0:b438482ebbfc 4 * @author Doug Anson/Chris Paola
ansond 0:b438482ebbfc 5 * @version 1.0
sam_grove 2:853f9ecc12df 6 * @see
ansond 0:b438482ebbfc 7 *
ansond 0:b438482ebbfc 8 * Copyright (c) 2014
ansond 0:b438482ebbfc 9 *
ansond 0:b438482ebbfc 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:b438482ebbfc 11 * you may not use this file except in compliance with the License.
ansond 0:b438482ebbfc 12 * You may obtain a copy of the License at
ansond 0:b438482ebbfc 13 *
ansond 0:b438482ebbfc 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:b438482ebbfc 15 *
ansond 0:b438482ebbfc 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:b438482ebbfc 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:b438482ebbfc 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:b438482ebbfc 19 * See the License for the specific language governing permissions and
ansond 0:b438482ebbfc 20 * limitations under the License.
ansond 0:b438482ebbfc 21 */
sam_grove 2:853f9ecc12df 22
sam_grove 2:853f9ecc12df 23 #include "OptionsBuilder.h"
sam_grove 2:853f9ecc12df 24
sam_grove 2:853f9ecc12df 25 // external included configuration file for the endpoint...
sam_grove 2:853f9ecc12df 26 #include "configuration.h"
sam_grove 2:853f9ecc12df 27
sam_grove 2:853f9ecc12df 28 // Connector namespace
sam_grove 2:853f9ecc12df 29 namespace Connector {
sam_grove 2:853f9ecc12df 30
sam_grove 2:853f9ecc12df 31 // Constructor
sam_grove 2:853f9ecc12df 32 OptionsBuilder::OptionsBuilder()
sam_grove 2:853f9ecc12df 33 {
sam_grove 2:853f9ecc12df 34 this->m_lifetime = NSP_LIFE_TIME;
sam_grove 2:853f9ecc12df 35 this->m_domain = NSP_DOMAIN;
sam_grove 2:853f9ecc12df 36 this->m_endpoint_type = NSP_ENDPOINT_TYPE;
sam_grove 2:853f9ecc12df 37 this->m_node_name = NODE_NAME;
sam_grove 2:853f9ecc12df 38 this->m_channel_list = NODE_CHANNEL_LIST;
sam_grove 2:853f9ecc12df 39 this->m_rd_update_period = NSP_RD_UPDATE_PERIOD;
sam_grove 2:853f9ecc12df 40 this->m_nsp_port = NSP_COAP_UDP_PORT;
sam_grove 2:853f9ecc12df 41
sam_grove 2:853f9ecc12df 42 memset(this->m_nsp_address,0,NSP_IP_ADDRESS_LENGTH);
sam_grove 2:853f9ecc12df 43 memset(this->m_mac_address,0,NODE_MAC_ADDRESS_LENGTH);
sam_grove 2:853f9ecc12df 44 this->m_static_resources.clear();
sam_grove 2:853f9ecc12df 45 this->m_dynamic_resources.clear();
ansond 42:20c375e74e8e 46 this->m_resource_observers.clear();
sam_grove 2:853f9ecc12df 47 }
sam_grove 2:853f9ecc12df 48
sam_grove 2:853f9ecc12df 49 // Copy Constructor
sam_grove 2:853f9ecc12df 50 OptionsBuilder::OptionsBuilder(const OptionsBuilder &ob)
sam_grove 2:853f9ecc12df 51 {
sam_grove 2:853f9ecc12df 52 this->m_lifetime = ob.m_lifetime;
sam_grove 2:853f9ecc12df 53 this->m_domain = ob.m_domain;
sam_grove 2:853f9ecc12df 54 this->m_endpoint_type = ob.m_endpoint_type;
sam_grove 2:853f9ecc12df 55 this->m_static_resources = ob.m_static_resources;
sam_grove 2:853f9ecc12df 56 this->m_dynamic_resources = ob.m_dynamic_resources;
ansond 42:20c375e74e8e 57 this->m_resource_observers = ob.m_resource_observers;
sam_grove 2:853f9ecc12df 58 }
sam_grove 2:853f9ecc12df 59
sam_grove 2:853f9ecc12df 60 // Destructor
sam_grove 2:853f9ecc12df 61 OptionsBuilder::~OptionsBuilder()
sam_grove 2:853f9ecc12df 62 {
sam_grove 2:853f9ecc12df 63 this->m_static_resources.clear();
sam_grove 2:853f9ecc12df 64 this->m_dynamic_resources.clear();
ansond 42:20c375e74e8e 65 this->m_resource_observers.clear();
sam_grove 2:853f9ecc12df 66 }
sam_grove 2:853f9ecc12df 67
sam_grove 2:853f9ecc12df 68 // set lifetime
sam_grove 2:853f9ecc12df 69 OptionsBuilder &OptionsBuilder::setLifetime(const char *lifetime)
sam_grove 2:853f9ecc12df 70 {
sam_grove 2:853f9ecc12df 71 this->m_lifetime = (char *)lifetime;
sam_grove 2:853f9ecc12df 72 return *this;
sam_grove 2:853f9ecc12df 73 }
sam_grove 2:853f9ecc12df 74
sam_grove 2:853f9ecc12df 75 // set domain
sam_grove 2:853f9ecc12df 76 OptionsBuilder &OptionsBuilder::setDomain(const char *domain)
sam_grove 2:853f9ecc12df 77 {
sam_grove 2:853f9ecc12df 78 this->m_domain = string(domain);
sam_grove 2:853f9ecc12df 79 return *this;
sam_grove 2:853f9ecc12df 80 }
sam_grove 2:853f9ecc12df 81
sam_grove 2:853f9ecc12df 82 // set endpoint nodename
sam_grove 2:853f9ecc12df 83 OptionsBuilder &OptionsBuilder::setEndpointNodename(const char *node_name)
sam_grove 2:853f9ecc12df 84 {
sam_grove 2:853f9ecc12df 85 this->m_node_name = string(node_name);
sam_grove 2:853f9ecc12df 86 return *this;
sam_grove 2:853f9ecc12df 87 }
sam_grove 2:853f9ecc12df 88
sam_grove 2:853f9ecc12df 89 // set lifetime
sam_grove 2:853f9ecc12df 90 OptionsBuilder &OptionsBuilder::setEndpointType(const char *endpoint_type)
sam_grove 2:853f9ecc12df 91 {
sam_grove 2:853f9ecc12df 92 this->m_endpoint_type = string(endpoint_type);
sam_grove 2:853f9ecc12df 93 return *this;
sam_grove 2:853f9ecc12df 94 }
sam_grove 2:853f9ecc12df 95
sam_grove 2:853f9ecc12df 96 // set NSP port number
sam_grove 2:853f9ecc12df 97 OptionsBuilder &OptionsBuilder::setNSPPortNumber(const int port_num)
sam_grove 2:853f9ecc12df 98 {
sam_grove 2:853f9ecc12df 99 this->m_nsp_port = port_num;
sam_grove 2:853f9ecc12df 100 return *this;
sam_grove 2:853f9ecc12df 101 }
sam_grove 2:853f9ecc12df 102
sam_grove 2:853f9ecc12df 103 // set low level radio channel list
sam_grove 2:853f9ecc12df 104 OptionsBuilder &OptionsBuilder::setRadioChannelList(const uint32_t channel_list)
sam_grove 2:853f9ecc12df 105 {
sam_grove 2:853f9ecc12df 106 this->m_channel_list = channel_list;
sam_grove 2:853f9ecc12df 107 return *this;
sam_grove 2:853f9ecc12df 108 }
sam_grove 2:853f9ecc12df 109
sam_grove 2:853f9ecc12df 110 // set NSP read update period
sam_grove 2:853f9ecc12df 111 OptionsBuilder &OptionsBuilder::setReadUpdatePeriod(const int rd_update_period)
sam_grove 2:853f9ecc12df 112 {
sam_grove 2:853f9ecc12df 113 this->m_rd_update_period = rd_update_period;
sam_grove 2:853f9ecc12df 114 return *this;
sam_grove 2:853f9ecc12df 115 }
sam_grove 2:853f9ecc12df 116
sam_grove 2:853f9ecc12df 117 // set NSP address
sam_grove 2:853f9ecc12df 118 OptionsBuilder &OptionsBuilder::setNSPAddress(const uint8_t *nsp_address,const int nsp_address_length)
sam_grove 2:853f9ecc12df 119 {
sam_grove 2:853f9ecc12df 120 if (nsp_address != NULL && nsp_address_length > 0) {
sam_grove 2:853f9ecc12df 121 int length = nsp_address_length;
sam_grove 2:853f9ecc12df 122 if (length > NSP_IP_ADDRESS_LENGTH) length = NSP_IP_ADDRESS_LENGTH;
sam_grove 2:853f9ecc12df 123 for(int i=0; i<length; ++i) this->m_nsp_address[i] = nsp_address[i];
sam_grove 2:853f9ecc12df 124 }
sam_grove 2:853f9ecc12df 125 return *this;
sam_grove 2:853f9ecc12df 126 }
sam_grove 2:853f9ecc12df 127
sam_grove 2:853f9ecc12df 128 // set MAC address
sam_grove 2:853f9ecc12df 129 OptionsBuilder &OptionsBuilder::setMACAddress(const uint8_t *mac_address,const int mac_address_length)
sam_grove 2:853f9ecc12df 130 {
sam_grove 2:853f9ecc12df 131 if (mac_address != NULL && mac_address_length > 0) {
sam_grove 2:853f9ecc12df 132 int length = mac_address_length;
sam_grove 2:853f9ecc12df 133 if (length > NODE_MAC_ADDRESS_LENGTH) length = NODE_MAC_ADDRESS_LENGTH;
sam_grove 2:853f9ecc12df 134 for(int i=0; i<length; ++i) this->m_mac_address[i] = mac_address[i];
sam_grove 2:853f9ecc12df 135 }
sam_grove 2:853f9ecc12df 136 return *this;
sam_grove 2:853f9ecc12df 137 }
sam_grove 2:853f9ecc12df 138
sam_grove 2:853f9ecc12df 139 // add static resource
sam_grove 2:853f9ecc12df 140 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource)
sam_grove 2:853f9ecc12df 141 {
ansond 42:20c375e74e8e 142 if (resource != NULL) {
ansond 48:4b9ee3e32f93 143 ((StaticResource *)resource)->setOptions(this);
ansond 42:20c375e74e8e 144 this->m_static_resources.push_back((StaticResource *)resource);
ansond 42:20c375e74e8e 145 }
sam_grove 2:853f9ecc12df 146 return *this;
sam_grove 2:853f9ecc12df 147 }
sam_grove 2:853f9ecc12df 148
sam_grove 2:853f9ecc12df 149 // add dynamic resource
ansond 46:cc6076ac5026 150 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
ansond 46:cc6076ac5026 151 {
ansond 46:cc6076ac5026 152 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 46:cc6076ac5026 153 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation()));
ansond 46:cc6076ac5026 154 }
ansond 46:cc6076ac5026 155
ansond 46:cc6076ac5026 156 // add dynamic resource
ansond 47:e75750ddc413 157 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time)
ansond 47:e75750ddc413 158 {
ansond 47:e75750ddc413 159 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 47:e75750ddc413 160 return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation()));
ansond 47:e75750ddc413 161 }
ansond 47:e75750ddc413 162
ansond 47:e75750ddc413 163 // add dynamic resource
ansond 45:65351ebb7c42 164 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer)
ansond 45:65351ebb7c42 165 {
ansond 45:65351ebb7c42 166 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 45:65351ebb7c42 167 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,use_observer);
ansond 45:65351ebb7c42 168 }
ansond 45:65351ebb7c42 169
ansond 45:65351ebb7c42 170 // add dynamic resource
ansond 42:20c375e74e8e 171 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
sam_grove 2:853f9ecc12df 172 {
ansond 42:20c375e74e8e 173 if (resource != NULL) {
ansond 42:20c375e74e8e 174 this->m_dynamic_resources.push_back((DynamicResource *)resource);
ansond 48:4b9ee3e32f93 175 ((DynamicResource *)resource)->setOptions(this);
ansond 42:20c375e74e8e 176 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
ansond 42:20c375e74e8e 177 #ifdef CONNECTOR_USING_THREADS
ansond 42:20c375e74e8e 178 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 42:20c375e74e8e 179 #else
ansond 42:20c375e74e8e 180 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 42:20c375e74e8e 181 #endif
ansond 42:20c375e74e8e 182 this->m_resource_observers.push_back(observer);
ansond 48:4b9ee3e32f93 183
ansond 48:4b9ee3e32f93 184 // immedate observation enablement option
ansond 48:4b9ee3e32f93 185 if (this->immedateObservationEnabled()) {
ansond 48:4b9ee3e32f93 186 observer->beginObservation();
ansond 48:4b9ee3e32f93 187 }
ansond 42:20c375e74e8e 188 }
ansond 42:20c375e74e8e 189 }
sam_grove 2:853f9ecc12df 190 return *this;
sam_grove 2:853f9ecc12df 191 }
sam_grove 2:853f9ecc12df 192
ansond 15:8ada80242804 193 // set WiFi SSID
ansond 15:8ada80242804 194 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
ansond 15:8ada80242804 195 {
ansond 15:8ada80242804 196 this->m_wifi_ssid = string(ssid);
ansond 15:8ada80242804 197 return *this;
ansond 15:8ada80242804 198 }
ansond 15:8ada80242804 199
ansond 15:8ada80242804 200 // set WiFi AuthType
ansond 15:8ada80242804 201 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
ansond 15:8ada80242804 202 {
ansond 15:8ada80242804 203 this->m_wifi_auth_type = auth_type;
ansond 15:8ada80242804 204 return *this;
ansond 15:8ada80242804 205 }
ansond 15:8ada80242804 206
ansond 15:8ada80242804 207 // set WiFi AuthKey
ansond 15:8ada80242804 208 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
ansond 15:8ada80242804 209 {
ansond 15:8ada80242804 210 this->m_wifi_auth_key = string(auth_key);
ansond 15:8ada80242804 211 return *this;
ansond 15:8ada80242804 212 }
ansond 15:8ada80242804 213
ansond 19:e2cbaeeea509 214 // set 802.15.4 Network ID
ansond 19:e2cbaeeea509 215 OptionsBuilder &OptionsBuilder::setNetworkID(char *network_id)
ansond 19:e2cbaeeea509 216 {
ansond 19:e2cbaeeea509 217 this->m_network_id = string(network_id);
ansond 19:e2cbaeeea509 218 return *this;
ansond 19:e2cbaeeea509 219 }
ansond 19:e2cbaeeea509 220
ansond 19:e2cbaeeea509 221 // set 802.15.4 Radio Channel
ansond 19:e2cbaeeea509 222 OptionsBuilder &OptionsBuilder::setRadioChannel(int channel)
ansond 19:e2cbaeeea509 223 {
ansond 19:e2cbaeeea509 224 this->m_channel = (uint8_t)channel;
ansond 19:e2cbaeeea509 225 return *this;
ansond 19:e2cbaeeea509 226 }
ansond 19:e2cbaeeea509 227
sam_grove 2:853f9ecc12df 228 // build out our immutable self
sam_grove 2:853f9ecc12df 229 Options *OptionsBuilder::build()
sam_grove 2:853f9ecc12df 230 {
sam_grove 2:853f9ecc12df 231 return (Options *)this;
sam_grove 2:853f9ecc12df 232 }
sam_grove 2:853f9ecc12df 233
ansond 48:4b9ee3e32f93 234 // Enable/Disable immediate observationing
ansond 48:4b9ee3e32f93 235 OptionsBuilder &OptionsBuilder::setImmedateObservationEnabled(bool enable) {
ansond 48:4b9ee3e32f93 236 this->m_enable_immediate_observation = enable;
ansond 48:4b9ee3e32f93 237 return *this;
ansond 48:4b9ee3e32f93 238 }
ansond 48:4b9ee3e32f93 239
ansond 48:4b9ee3e32f93 240 // Enable/Disable GET-based control of observations
ansond 48:4b9ee3e32f93 241 OptionsBuilder &OptionsBuilder::setEnableGETObservationControl(bool enable) {
ansond 48:4b9ee3e32f93 242 this->m_enable_get_obs_control = enable;
ansond 48:4b9ee3e32f93 243 return *this;
ansond 48:4b9ee3e32f93 244 }
ansond 48:4b9ee3e32f93 245
ansond 42:20c375e74e8e 246 } // namespace Connector