added max-age and content-format

Fork of mbedConnectorInterface by Doug Anson

Committer:
ansond
Date:
Sun Apr 12 04:00:53 2015 +0000
Revision:
47:e75750ddc413
Parent:
46:cc6076ac5026
Child:
48:4b9ee3e32f93
another fix for options builder

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 42:20c375e74e8e 143 this->m_static_resources.push_back((StaticResource *)resource);
ansond 42:20c375e74e8e 144 }
sam_grove 2:853f9ecc12df 145 return *this;
sam_grove 2:853f9ecc12df 146 }
sam_grove 2:853f9ecc12df 147
sam_grove 2:853f9ecc12df 148 // add dynamic resource
ansond 46:cc6076ac5026 149 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
ansond 46:cc6076ac5026 150 {
ansond 46:cc6076ac5026 151 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 46:cc6076ac5026 152 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,!(((DynamicResource *)resource)->implementsObservation()));
ansond 46:cc6076ac5026 153 }
ansond 46:cc6076ac5026 154
ansond 46:cc6076ac5026 155 // add dynamic resource
ansond 47:e75750ddc413 156 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time)
ansond 47:e75750ddc413 157 {
ansond 47:e75750ddc413 158 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 47:e75750ddc413 159 return this->addResource(resource,sleep_time,!(((DynamicResource *)resource)->implementsObservation()));
ansond 47:e75750ddc413 160 }
ansond 47:e75750ddc413 161
ansond 47:e75750ddc413 162 // add dynamic resource
ansond 45:65351ebb7c42 163 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const bool use_observer)
ansond 45:65351ebb7c42 164 {
ansond 45:65351ebb7c42 165 // ensure that the boolean isn't mistaken by the compiler for the obs period...
ansond 45:65351ebb7c42 166 return this->addResource(resource,NSP_DEFAULT_OBS_PERIOD,use_observer);
ansond 45:65351ebb7c42 167 }
ansond 45:65351ebb7c42 168
ansond 45:65351ebb7c42 169 // add dynamic resource
ansond 42:20c375e74e8e 170 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
sam_grove 2:853f9ecc12df 171 {
ansond 42:20c375e74e8e 172 if (resource != NULL) {
ansond 42:20c375e74e8e 173 this->m_dynamic_resources.push_back((DynamicResource *)resource);
ansond 42:20c375e74e8e 174 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
ansond 42:20c375e74e8e 175 #ifdef CONNECTOR_USING_THREADS
ansond 42:20c375e74e8e 176 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 42:20c375e74e8e 177 #else
ansond 42:20c375e74e8e 178 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 42:20c375e74e8e 179 #endif
ansond 42:20c375e74e8e 180 this->m_resource_observers.push_back(observer);
ansond 42:20c375e74e8e 181 observer->beginObservation();
ansond 42:20c375e74e8e 182 }
ansond 42:20c375e74e8e 183 }
sam_grove 2:853f9ecc12df 184 return *this;
sam_grove 2:853f9ecc12df 185 }
sam_grove 2:853f9ecc12df 186
ansond 15:8ada80242804 187 // set WiFi SSID
ansond 15:8ada80242804 188 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
ansond 15:8ada80242804 189 {
ansond 15:8ada80242804 190 this->m_wifi_ssid = string(ssid);
ansond 15:8ada80242804 191 return *this;
ansond 15:8ada80242804 192 }
ansond 15:8ada80242804 193
ansond 15:8ada80242804 194 // set WiFi AuthType
ansond 15:8ada80242804 195 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
ansond 15:8ada80242804 196 {
ansond 15:8ada80242804 197 this->m_wifi_auth_type = auth_type;
ansond 15:8ada80242804 198 return *this;
ansond 15:8ada80242804 199 }
ansond 15:8ada80242804 200
ansond 15:8ada80242804 201 // set WiFi AuthKey
ansond 15:8ada80242804 202 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
ansond 15:8ada80242804 203 {
ansond 15:8ada80242804 204 this->m_wifi_auth_key = string(auth_key);
ansond 15:8ada80242804 205 return *this;
ansond 15:8ada80242804 206 }
ansond 15:8ada80242804 207
ansond 19:e2cbaeeea509 208 // set 802.15.4 Network ID
ansond 19:e2cbaeeea509 209 OptionsBuilder &OptionsBuilder::setNetworkID(char *network_id)
ansond 19:e2cbaeeea509 210 {
ansond 19:e2cbaeeea509 211 this->m_network_id = string(network_id);
ansond 19:e2cbaeeea509 212 return *this;
ansond 19:e2cbaeeea509 213 }
ansond 19:e2cbaeeea509 214
ansond 19:e2cbaeeea509 215 // set 802.15.4 Radio Channel
ansond 19:e2cbaeeea509 216 OptionsBuilder &OptionsBuilder::setRadioChannel(int channel)
ansond 19:e2cbaeeea509 217 {
ansond 19:e2cbaeeea509 218 this->m_channel = (uint8_t)channel;
ansond 19:e2cbaeeea509 219 return *this;
ansond 19:e2cbaeeea509 220 }
ansond 19:e2cbaeeea509 221
sam_grove 2:853f9ecc12df 222 // build out our immutable self
sam_grove 2:853f9ecc12df 223 Options *OptionsBuilder::build()
sam_grove 2:853f9ecc12df 224 {
sam_grove 2:853f9ecc12df 225 return (Options *)this;
sam_grove 2:853f9ecc12df 226 }
sam_grove 2:853f9ecc12df 227
ansond 42:20c375e74e8e 228 } // namespace Connector