custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Committer:
ansond
Date:
Sat Apr 11 21:14:28 2015 +0000
Revision:
42:20c375e74e8e
Parent:
19:e2cbaeeea509
Child:
45:65351ebb7c42
internalized resource observers

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 42:20c375e74e8e 149 OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
sam_grove 2:853f9ecc12df 150 {
ansond 42:20c375e74e8e 151 if (resource != NULL) {
ansond 42:20c375e74e8e 152 this->m_dynamic_resources.push_back((DynamicResource *)resource);
ansond 42:20c375e74e8e 153 if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
ansond 42:20c375e74e8e 154 #ifdef CONNECTOR_USING_THREADS
ansond 42:20c375e74e8e 155 ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 42:20c375e74e8e 156 #else
ansond 42:20c375e74e8e 157 TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
ansond 42:20c375e74e8e 158 #endif
ansond 42:20c375e74e8e 159 this->m_resource_observers.push_back(observer);
ansond 42:20c375e74e8e 160 observer->beginObservation();
ansond 42:20c375e74e8e 161 }
ansond 42:20c375e74e8e 162 }
sam_grove 2:853f9ecc12df 163 return *this;
sam_grove 2:853f9ecc12df 164 }
sam_grove 2:853f9ecc12df 165
ansond 15:8ada80242804 166 // set WiFi SSID
ansond 15:8ada80242804 167 OptionsBuilder &OptionsBuilder::setWiFiSSID(char *ssid)
ansond 15:8ada80242804 168 {
ansond 15:8ada80242804 169 this->m_wifi_ssid = string(ssid);
ansond 15:8ada80242804 170 return *this;
ansond 15:8ada80242804 171 }
ansond 15:8ada80242804 172
ansond 15:8ada80242804 173 // set WiFi AuthType
ansond 15:8ada80242804 174 OptionsBuilder &OptionsBuilder::setWiFiAuthType(WiFiAuthTypes auth_type)
ansond 15:8ada80242804 175 {
ansond 15:8ada80242804 176 this->m_wifi_auth_type = auth_type;
ansond 15:8ada80242804 177 return *this;
ansond 15:8ada80242804 178 }
ansond 15:8ada80242804 179
ansond 15:8ada80242804 180 // set WiFi AuthKey
ansond 15:8ada80242804 181 OptionsBuilder &OptionsBuilder::setWiFiAuthKey(char *auth_key)
ansond 15:8ada80242804 182 {
ansond 15:8ada80242804 183 this->m_wifi_auth_key = string(auth_key);
ansond 15:8ada80242804 184 return *this;
ansond 15:8ada80242804 185 }
ansond 15:8ada80242804 186
ansond 19:e2cbaeeea509 187 // set 802.15.4 Network ID
ansond 19:e2cbaeeea509 188 OptionsBuilder &OptionsBuilder::setNetworkID(char *network_id)
ansond 19:e2cbaeeea509 189 {
ansond 19:e2cbaeeea509 190 this->m_network_id = string(network_id);
ansond 19:e2cbaeeea509 191 return *this;
ansond 19:e2cbaeeea509 192 }
ansond 19:e2cbaeeea509 193
ansond 19:e2cbaeeea509 194 // set 802.15.4 Radio Channel
ansond 19:e2cbaeeea509 195 OptionsBuilder &OptionsBuilder::setRadioChannel(int channel)
ansond 19:e2cbaeeea509 196 {
ansond 19:e2cbaeeea509 197 this->m_channel = (uint8_t)channel;
ansond 19:e2cbaeeea509 198 return *this;
ansond 19:e2cbaeeea509 199 }
ansond 19:e2cbaeeea509 200
sam_grove 2:853f9ecc12df 201 // build out our immutable self
sam_grove 2:853f9ecc12df 202 Options *OptionsBuilder::build()
sam_grove 2:853f9ecc12df 203 {
sam_grove 2:853f9ecc12df 204 return (Options *)this;
sam_grove 2:853f9ecc12df 205 }
sam_grove 2:853f9ecc12df 206
ansond 42:20c375e74e8e 207 } // namespace Connector