use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Wed Jun 15 19:40:08 2016 +0000
Revision:
44:7c73baf9f4c1
Parent:
43:3fb57c4fba34
Child:
45:db754b994deb
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:1f1f55e73248 1 /**
ansond 0:1f1f55e73248 2 * @file ConnectorEndpoint.cpp
ansond 0:1f1f55e73248 3 * @brief mbed CoAP Endpoint base class
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 // Lower level Network
ansond 0:1f1f55e73248 24 #include "mbed-connector-interface/mbedEndpointNetwork.h"
ansond 0:1f1f55e73248 25
ansond 0:1f1f55e73248 26 // ConnectorEndpoint
ansond 0:1f1f55e73248 27 #include "mbed-connector-interface/ConnectorEndpoint.h"
ansond 0:1f1f55e73248 28
ansond 0:1f1f55e73248 29 // Utils support
ansond 0:1f1f55e73248 30 #include "mbed-connector-interface/Utils.h"
ansond 0:1f1f55e73248 31
ansond 27:b8aaf7dc7023 32 // Device Manager support
ansond 27:b8aaf7dc7023 33 #include "mbed-connector-interface/DeviceManager.h"
ansond 27:b8aaf7dc7023 34
ansond 0:1f1f55e73248 35 // DEBUG
ansond 0:1f1f55e73248 36 #ifndef NDEBUG
ansond 0:1f1f55e73248 37 #define DEBUG_OUT(...) { printf(__VA_ARGS__); }
ansond 0:1f1f55e73248 38 #else
ansond 0:1f1f55e73248 39 #define DEBUG_OUT(...) /* nothing */
ansond 0:1f1f55e73248 40 #endif
ansond 0:1f1f55e73248 41
ansond 1:16f0fb5b8d97 42 // our endpoint instance
ansond 1:16f0fb5b8d97 43 static Connector::Endpoint *__endpoint = NULL;
ansond 0:1f1f55e73248 44
ansond 27:b8aaf7dc7023 45 // LWIP_INTERFACE
ansond 27:b8aaf7dc7023 46 #if defined(MCI_LWIP_INTERFACE)
ansond 27:b8aaf7dc7023 47 void *__network_interface = NULL;
ansond 27:b8aaf7dc7023 48 #endif
ansond 27:b8aaf7dc7023 49
ansond 0:1f1f55e73248 50 // Connector namespace
ansond 0:1f1f55e73248 51 namespace Connector {
ansond 0:1f1f55e73248 52
ansond 0:1f1f55e73248 53 // STATIC: Plumb the network
ansond 13:9edad7677211 54 void Endpoint::plumbNetwork(void *device_manager,bool canActAsRouterNode) {
ansond 1:16f0fb5b8d97 55 if (__endpoint == NULL) {
ansond 1:16f0fb5b8d97 56 // initialize our endpoint instance
ansond 1:16f0fb5b8d97 57 DEBUG_OUT("Endpoint::plumbNetwork: initializing endpoint instance...\r\n");
ansond 1:16f0fb5b8d97 58 __endpoint = (Connector::Endpoint *)utils_init_endpoint(canActAsRouterNode);
ansond 1:16f0fb5b8d97 59 }
ansond 13:9edad7677211 60
ansond 13:9edad7677211 61 // set the device manager
ansond 13:9edad7677211 62 if (device_manager != NULL) {
ansond 13:9edad7677211 63 // device manager has been supplied
ansond 13:9edad7677211 64 DEBUG_OUT("Endpoint::plumbNetwork: setting a device manager...\r\n");
ansond 13:9edad7677211 65 __endpoint->setDeviceManager(device_manager);
ansond 13:9edad7677211 66 }
ansond 13:9edad7677211 67 else {
ansond 13:9edad7677211 68 // no device manager supplied
ansond 13:9edad7677211 69 DEBUG_OUT("Endpoint::plumbNetwork: no device manager supplied (OK)\r\n");
ansond 13:9edad7677211 70 }
ansond 8:f950fb1b78c0 71
ansond 0:1f1f55e73248 72 // configure the endpoint now...
ansond 0:1f1f55e73248 73 DEBUG_OUT("Endpoint::plumbNetwork: configuring endpoint...\r\n");
ansond 1:16f0fb5b8d97 74 utils_configure_endpoint((void *)__endpoint);
ansond 2:1a7a292555d1 75
ansond 2:1a7a292555d1 76 // plumb network
ansond 2:1a7a292555d1 77 DEBUG_OUT("Endpoint::plumbNetwork: plumbing network...\r\n");
ansond 8:f950fb1b78c0 78 net_plumb_network((void *)__endpoint);
ansond 0:1f1f55e73248 79 }
ansond 0:1f1f55e73248 80
ansond 0:1f1f55e73248 81 // STATIC: Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
ansond 8:f950fb1b78c0 82 void Endpoint::start()
ansond 8:f950fb1b78c0 83 {
ansond 8:f950fb1b78c0 84 // complete setup of our endpoint...
ansond 8:f950fb1b78c0 85 DEBUG_OUT("Endpoint::start: building out endpoint...\r\n");
ansond 8:f950fb1b78c0 86 utils_build_endpoint((void *)__endpoint);
ansond 0:1f1f55e73248 87
ansond 8:f950fb1b78c0 88 // register the endpoint
ansond 8:f950fb1b78c0 89 DEBUG_OUT("Endpoint::start: completing endpoint build out..\r\n");
ansond 8:f950fb1b78c0 90 net_perform_endpoint_registration(__endpoint);
ansond 0:1f1f55e73248 91 }
ansond 0:1f1f55e73248 92
ansond 43:3fb57c4fba34 93 // STATIC: Set the ConnectionStatusInterface Implementation instance
ansond 43:3fb57c4fba34 94 void Endpoint::setConnectionStatusInterface(ConnectionStatusInterface *csi) {
ansond 43:3fb57c4fba34 95 if (__endpoint != NULL) {
ansond 44:7c73baf9f4c1 96 __endpoint->setConnectionStatusInterfaceImpl(csi);
ansond 43:3fb57c4fba34 97 }
ansond 43:3fb57c4fba34 98 }
ansond 43:3fb57c4fba34 99
ansond 0:1f1f55e73248 100 // Constructor
ansond 0:1f1f55e73248 101 Endpoint::Endpoint(const Logger *logger, const Options *options) : M2MInterfaceObserver()
ansond 0:1f1f55e73248 102 {
ansond 0:1f1f55e73248 103 this->m_logger = (Logger *)logger;
ansond 0:1f1f55e73248 104 this->m_options = (Options *)options;
ansond 13:9edad7677211 105 this->m_device_manager = NULL;
ansond 13:9edad7677211 106 this->m_connected = false;
ansond 15:c11dbe4d354c 107 this->m_registered = false;
ansond 27:b8aaf7dc7023 108 this->m_csi = NULL;
ansond 27:b8aaf7dc7023 109 this->m_oim = NULL;
ansond 0:1f1f55e73248 110 }
ansond 0:1f1f55e73248 111
ansond 0:1f1f55e73248 112 // Copy Constructor
ansond 0:1f1f55e73248 113 Endpoint::Endpoint(const Endpoint &ep)
ansond 0:1f1f55e73248 114 {
ansond 0:1f1f55e73248 115 this->m_logger = ep.m_logger;
ansond 0:1f1f55e73248 116 this->m_options = ep.m_options;
ansond 0:1f1f55e73248 117 this->m_interface = ep.m_interface;
ansond 0:1f1f55e73248 118 this->m_server_instance = ep.m_server_instance;
ansond 0:1f1f55e73248 119 this->m_object_list = ep.m_object_list;
ansond 0:1f1f55e73248 120 this->m_device_object = ep.m_device_object;
ansond 13:9edad7677211 121 this->m_device_manager = ep.m_device_manager;
ansond 13:9edad7677211 122 this->m_connected = ep.m_connected;
ansond 15:c11dbe4d354c 123 this->m_registered = ep.m_registered;
ansond 27:b8aaf7dc7023 124 this->m_csi = ep.m_csi;
ansond 27:b8aaf7dc7023 125 this->m_oim = ep.m_oim;
ansond 0:1f1f55e73248 126 }
ansond 0:1f1f55e73248 127
ansond 0:1f1f55e73248 128 // Destructor
ansond 0:1f1f55e73248 129 Endpoint::~Endpoint() {
ansond 0:1f1f55e73248 130 if (this->m_interface != NULL)
ansond 0:1f1f55e73248 131 delete this->m_interface;
ansond 0:1f1f55e73248 132 if (this->m_server_instance != NULL)
ansond 0:1f1f55e73248 133 delete this->m_server_instance;
ansond 0:1f1f55e73248 134 }
ansond 0:1f1f55e73248 135
ansond 13:9edad7677211 136 // set the device manager
ansond 13:9edad7677211 137 void Endpoint::setDeviceManager(void *device_manager) {
ansond 13:9edad7677211 138 this->m_device_manager = device_manager;
ansond 13:9edad7677211 139 }
ansond 13:9edad7677211 140
ansond 13:9edad7677211 141 // get the device manager
ansond 13:9edad7677211 142 void *Endpoint::getDeviceManager(void) {
ansond 13:9edad7677211 143 return this->m_device_manager;
ansond 13:9edad7677211 144 }
ansond 13:9edad7677211 145
ansond 1:16f0fb5b8d97 146 // router node behavior setting
ansond 1:16f0fb5b8d97 147 void Endpoint::asRouterNode(bool canActAsRouterNode) {
ansond 1:16f0fb5b8d97 148 this->m_canActAsRouterNode = canActAsRouterNode;
ansond 1:16f0fb5b8d97 149 }
ansond 1:16f0fb5b8d97 150
ansond 1:16f0fb5b8d97 151 // set our Options
ansond 1:16f0fb5b8d97 152 void Endpoint::setOptions(Options *options) {
ansond 1:16f0fb5b8d97 153 this->m_options = options;
ansond 1:16f0fb5b8d97 154 }
ansond 1:16f0fb5b8d97 155
ansond 0:1f1f55e73248 156 // get our Options
ansond 0:1f1f55e73248 157 Options *Endpoint::getOptions() {
ansond 0:1f1f55e73248 158 return this->m_options;
ansond 0:1f1f55e73248 159 }
ansond 0:1f1f55e73248 160
ansond 0:1f1f55e73248 161 // get our Server
ansond 0:1f1f55e73248 162 M2MSecurity *Endpoint::getServer() {
ansond 0:1f1f55e73248 163 return this->m_server_instance;
ansond 0:1f1f55e73248 164 }
ansond 0:1f1f55e73248 165
ansond 0:1f1f55e73248 166 // get our ObjectList
ansond 0:1f1f55e73248 167 M2MObjectList Endpoint::getObjectList() {
ansond 0:1f1f55e73248 168 return this->m_object_list;
ansond 0:1f1f55e73248 169 }
ansond 0:1f1f55e73248 170
ansond 0:1f1f55e73248 171 // mbed-client: create our interface
ansond 0:1f1f55e73248 172 void Endpoint::create_interface() {
ansond 10:3f79b5e67c22 173 // get the CoAP listening port
ansond 10:3f79b5e67c22 174 uint16_t listening_port = (uint16_t)this->m_options->getConnectorPort();
ansond 10:3f79b5e67c22 175
ansond 10:3f79b5e67c22 176 // randomize the port if we are using certificates...
ansond 10:3f79b5e67c22 177 if (this->m_options->getServerCertificateSize() > 0) {
ansond 0:1f1f55e73248 178 // Randomizing listening port for Certificate mode connectivity
ansond 0:1f1f55e73248 179 srand(time(NULL));
ansond 10:3f79b5e67c22 180 listening_port = rand() % 65535 + 12345;
ansond 10:3f79b5e67c22 181 }
ansond 10:3f79b5e67c22 182
ansond 10:3f79b5e67c22 183 // Binding Mode - TCP or UDP
ansond 10:3f79b5e67c22 184 M2MInterface::BindingMode network_protocol = M2MInterface::UDP;
ansond 10:3f79b5e67c22 185 if (this->m_options->getCoAPConnectionType() == COAP_TCP) network_protocol = M2MInterface::TCP;
ansond 10:3f79b5e67c22 186
ansond 10:3f79b5e67c22 187 // Network Type IPv4 or IPv6
ansond 10:3f79b5e67c22 188 M2MInterface::NetworkStack ip_address_type = M2MInterface::LwIP_IPv4;
ansond 10:3f79b5e67c22 189 if (this->m_options->getIPAddressType() == IP_ADDRESS_TYPE_IPV6) ip_address_type = M2MInterface::LwIP_IPv6;
ansond 10:3f79b5e67c22 190
ansond 10:3f79b5e67c22 191 // DEBUG
ansond 10:3f79b5e67c22 192 if (network_protocol == M2MInterface::TCP) this->logger()->log("Endpoint: Underlying Protocol: TCP");
ansond 10:3f79b5e67c22 193 if (network_protocol == M2MInterface::UDP) this->logger()->log("Endpoint: Underlying Protocol: UDP");
ansond 10:3f79b5e67c22 194 if (ip_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Endpoint: IP Address Type: IPv4");
ansond 10:3f79b5e67c22 195 if (ip_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Endpoint: IP Address Type: IPv6");
ansond 10:3f79b5e67c22 196
ansond 10:3f79b5e67c22 197 // Create the M2M Interface instance
ansond 10:3f79b5e67c22 198 this->m_interface = M2MInterfaceFactory::create_interface(*this,
ansond 10:3f79b5e67c22 199 (char *)this->m_options->getEndpointNodename().c_str(),
ansond 10:3f79b5e67c22 200 (char *)this->m_options->getEndpointType().c_str(),
ansond 10:3f79b5e67c22 201 (int32_t)this->m_options->getLifetime(),
ansond 10:3f79b5e67c22 202 listening_port, // listening port
ansond 10:3f79b5e67c22 203 (char *)this->m_options->getDomain().c_str(),
ansond 10:3f79b5e67c22 204 network_protocol, // CoAP over UDP or TCP...
ansond 10:3f79b5e67c22 205 ip_address_type); // IPv4 addressing or IPv6 addressing
ansond 27:b8aaf7dc7023 206
ansond 27:b8aaf7dc7023 207 #if defined (MCI_LWIP_INTERFACE)
ansond 27:b8aaf7dc7023 208 if (__network_interface != NULL && this->m_interface != NULL) {
ansond 27:b8aaf7dc7023 209 this->logger()->log("Endpoint: Morpheus: binding network...");
ansond 27:b8aaf7dc7023 210 this->m_interface->set_platform_network_handler(__network_interface);
ansond 27:b8aaf7dc7023 211 }
ansond 27:b8aaf7dc7023 212 #endif
ansond 0:1f1f55e73248 213 }
ansond 0:1f1f55e73248 214
ansond 0:1f1f55e73248 215 // mbed-client: create_server_instance()
ansond 0:1f1f55e73248 216 M2MSecurity *Endpoint::create_server_instance() {
ansond 0:1f1f55e73248 217 // Creates register server object with mbed device server address and other parameters
ansond 0:1f1f55e73248 218 M2MSecurity *server = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
ansond 0:1f1f55e73248 219 if (server != NULL) {
ansond 0:1f1f55e73248 220 const String url = this->m_options->getConnectorURL();
ansond 0:1f1f55e73248 221 server->set_resource_value(M2MSecurity::M2MServerUri, url);
ansond 38:bb6d2be4d54c 222 server->set_resource_value(M2MSecurity::BootstrapServer, false);
ansond 0:1f1f55e73248 223 server->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
ansond 0:1f1f55e73248 224 server->set_resource_value(M2MSecurity::ServerPublicKey,this->m_options->getServerCertificate(),this->m_options->getServerCertificateSize());
ansond 0:1f1f55e73248 225 server->set_resource_value(M2MSecurity::PublicKey,this->m_options->getClientCertificate(),this->m_options->getClientCertificateSize());
ansond 0:1f1f55e73248 226 server->set_resource_value(M2MSecurity::Secretkey,this->m_options->getClientKey(),this->m_options->getClientKeySize());
ansond 0:1f1f55e73248 227 }
ansond 0:1f1f55e73248 228 return server;
ansond 0:1f1f55e73248 229 }
ansond 0:1f1f55e73248 230
ansond 0:1f1f55e73248 231 // mbed-client: Callback from mbed client stack if any error is encountered
ansond 0:1f1f55e73248 232 void Endpoint::error(M2MInterface::Error error) {
ansond 0:1f1f55e73248 233 switch(error){
ansond 0:1f1f55e73248 234 case M2MInterface::AlreadyExists:
ansond 0:1f1f55e73248 235 DEBUG_OUT("[ERROR:] M2MInterface::AlreadyExists");
ansond 0:1f1f55e73248 236 break;
ansond 0:1f1f55e73248 237 case M2MInterface::BootstrapFailed:
ansond 0:1f1f55e73248 238 DEBUG_OUT("[ERROR:] M2MInterface::BootstrapFailed");
ansond 0:1f1f55e73248 239 break;
ansond 0:1f1f55e73248 240 case M2MInterface::InvalidParameters:
ansond 0:1f1f55e73248 241 DEBUG_OUT("[ERROR:] M2MInterface::InvalidParameters");
ansond 0:1f1f55e73248 242 break;
ansond 0:1f1f55e73248 243 case M2MInterface::NotRegistered:
ansond 0:1f1f55e73248 244 DEBUG_OUT("[ERROR:] M2MInterface::NotRegistered");
ansond 0:1f1f55e73248 245 break;
ansond 0:1f1f55e73248 246 case M2MInterface::Timeout:
ansond 0:1f1f55e73248 247 DEBUG_OUT("[ERROR:] M2MInterface::Timeout");
ansond 0:1f1f55e73248 248 break;
ansond 0:1f1f55e73248 249 case M2MInterface::NetworkError:
ansond 0:1f1f55e73248 250 DEBUG_OUT("[ERROR:] M2MInterface::NetworkError");
ansond 0:1f1f55e73248 251 break;
ansond 0:1f1f55e73248 252 case M2MInterface::ResponseParseFailed:
ansond 0:1f1f55e73248 253 DEBUG_OUT("[ERROR:] M2MInterface::ResponseParseFailed");
ansond 0:1f1f55e73248 254 break;
ansond 0:1f1f55e73248 255 case M2MInterface::UnknownError:
ansond 0:1f1f55e73248 256 DEBUG_OUT("[ERROR:] M2MInterface::UnknownError");
ansond 0:1f1f55e73248 257 break;
ansond 0:1f1f55e73248 258 case M2MInterface::MemoryFail:
ansond 0:1f1f55e73248 259 DEBUG_OUT("[ERROR:] M2MInterface::MemoryFail");
ansond 0:1f1f55e73248 260 break;
ansond 0:1f1f55e73248 261 case M2MInterface::NotAllowed:
ansond 0:1f1f55e73248 262 DEBUG_OUT("[ERROR:] M2MInterface::NotAllowed");
ansond 0:1f1f55e73248 263 break;
ansond 0:1f1f55e73248 264 default:
ansond 0:1f1f55e73248 265 break;
ansond 0:1f1f55e73248 266 }
ansond 0:1f1f55e73248 267 }
ansond 0:1f1f55e73248 268
ansond 8:f950fb1b78c0 269 // register the endpoint
ansond 8:f950fb1b78c0 270 void Endpoint::register_endpoint(M2MSecurity *server_instance, M2MObjectList resources) {
ansond 23:5852c0884714 271 if (this->m_interface != NULL && server_instance != NULL && resources.size() > 0) {
ansond 23:5852c0884714 272 // register endpoint
ansond 23:5852c0884714 273 this->logger()->log("Registering endpoint...");
ansond 23:5852c0884714 274 this->m_interface->register_object(server_instance, resources);
ansond 23:5852c0884714 275 }
ansond 0:1f1f55e73248 276 }
ansond 0:1f1f55e73248 277
ansond 8:f950fb1b78c0 278 // re-register the endpoint
ansond 8:f950fb1b78c0 279 void Endpoint::re_register_endpoint() {
ansond 23:5852c0884714 280 if (this->m_interface != NULL) {
ansond 23:5852c0884714 281 this->m_interface->update_registration(this->m_server_instance, this->m_options->getLifetime());
ansond 23:5852c0884714 282 }
ansond 8:f950fb1b78c0 283 }
ansond 8:f950fb1b78c0 284
ansond 8:f950fb1b78c0 285 // de-register endpoint
ansond 8:f950fb1b78c0 286 void Endpoint::de_register_endpoint(void) {
ansond 0:1f1f55e73248 287 if (this->m_interface != NULL) {
ansond 0:1f1f55e73248 288 // de-register endpoint
ansond 0:1f1f55e73248 289 this->logger()->log("De-registering endpoint...");
ansond 0:1f1f55e73248 290 this->m_interface->unregister_object(NULL);
ansond 0:1f1f55e73248 291 }
ansond 0:1f1f55e73248 292 }
ansond 0:1f1f55e73248 293
ansond 8:f950fb1b78c0 294 // object registered
ansond 27:b8aaf7dc7023 295 void Endpoint::object_registered(M2MSecurity *security, const M2MServer &server) {
ansond 43:3fb57c4fba34 296 this->logger()->log("Endpoint registered...");
ansond 15:c11dbe4d354c 297 this->m_connected = true;
ansond 15:c11dbe4d354c 298 this->m_registered = true;
ansond 27:b8aaf7dc7023 299
ansond 27:b8aaf7dc7023 300 #if defined (ENDPOINT_EARLY_CONFIG_DEREG_BUTTON)
ansond 27:b8aaf7dc7023 301 // setup the shutdown button (if enabled for a given platform...)
ansond 27:b8aaf7dc7023 302 this->logger()->log("Configuring optional de-registration button...");
ansond 27:b8aaf7dc7023 303 net_setup_deregistration_button((void *)this);
ansond 27:b8aaf7dc7023 304 #endif
ansond 27:b8aaf7dc7023 305
ansond 27:b8aaf7dc7023 306 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 307 this->m_csi->object_registered((void *)this,(void *)security,(void *)&server);
ansond 27:b8aaf7dc7023 308 }
ansond 8:f950fb1b78c0 309 }
ansond 8:f950fb1b78c0 310
ansond 8:f950fb1b78c0 311 // registration updated
ansond 27:b8aaf7dc7023 312 void Endpoint::registration_updated(M2MSecurity *security, const M2MServer &server) {
ansond 43:3fb57c4fba34 313 this->logger()->log("Endpoint re-registered...");
ansond 15:c11dbe4d354c 314 this->m_connected = true;
ansond 15:c11dbe4d354c 315 this->m_registered = true;
ansond 27:b8aaf7dc7023 316 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 317 this->m_csi->registration_updated((void *)this,(void *)security,(void *)&server);
ansond 27:b8aaf7dc7023 318 }
ansond 8:f950fb1b78c0 319 }
ansond 8:f950fb1b78c0 320
ansond 8:f950fb1b78c0 321 // object unregistered
ansond 27:b8aaf7dc7023 322 void Endpoint::object_unregistered(M2MSecurity *server) {
ansond 8:f950fb1b78c0 323 // ready to exit
ansond 8:f950fb1b78c0 324 this->logger()->log("Endpoint de-registered... Ready to exit...");
ansond 15:c11dbe4d354c 325 this->m_registered = false;
ansond 27:b8aaf7dc7023 326 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 327 this->m_csi->object_unregistered((void *)this,(void *)server);
ansond 27:b8aaf7dc7023 328 }
ansond 0:1f1f55e73248 329 }
ansond 0:1f1f55e73248 330
ansond 0:1f1f55e73248 331 // bootstrap done
ansond 27:b8aaf7dc7023 332 void Endpoint::bootstrap_done(M2MSecurity *server) {
ansond 43:3fb57c4fba34 333 this->logger()->log("Endpoint bootstrapped...");
ansond 27:b8aaf7dc7023 334 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 335 this->m_csi->bootstrapped((void *)this,(void *)server);
ansond 27:b8aaf7dc7023 336 }
ansond 0:1f1f55e73248 337 }
ansond 0:1f1f55e73248 338
ansond 0:1f1f55e73248 339 // resource value updated
ansond 0:1f1f55e73248 340 void Endpoint::value_updated(M2MBase *base, M2MBase::BaseType type) {
ansond 29:be035befb437 341 // Lookup the resource and invoke process() on it...
ansond 0:1f1f55e73248 342 DynamicResource *target_res = this->lookupDynamicResource(base);
ansond 29:be035befb437 343 if (target_res != NULL) {
ansond 29:be035befb437 344 // DEBUG
ansond 43:3fb57c4fba34 345 //this->logger()->log("Value Updated (Custom Resource)");
ansond 29:be035befb437 346
ansond 29:be035befb437 347 // its a custom resource...
ansond 29:be035befb437 348 target_res->process(base->operation(),type);
ansond 29:be035befb437 349 }
ansond 29:be035befb437 350 else {
ansond 29:be035befb437 351 // DEBUG
ansond 43:3fb57c4fba34 352 //this->logger()->log("Value Updated (Device Manager)");
ansond 29:be035befb437 353
ansond 29:be035befb437 354 // let DeviceManager handle it
ansond 29:be035befb437 355 ((DeviceManager *)this->m_device_manager)->process(base,type);
ansond 29:be035befb437 356 }
ansond 29:be035befb437 357
ansond 29:be035befb437 358 // CSI
ansond 27:b8aaf7dc7023 359 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 360 this->m_csi->value_updated((void *)this,(void *)base,(int)type);
ansond 27:b8aaf7dc7023 361 }
ansond 0:1f1f55e73248 362 }
ansond 0:1f1f55e73248 363
ansond 0:1f1f55e73248 364 // lookup which DynamicResource cooresponds to a given M2MBase instance...
ansond 0:1f1f55e73248 365 DynamicResource *Endpoint::lookupDynamicResource(M2MBase *base) {
ansond 0:1f1f55e73248 366 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 29:be035befb437 367 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 29:be035befb437 368 M2MBase *t = (M2MBase *)dynamic_resources->at(i)->getResource();
ansond 0:1f1f55e73248 369 if (t == base) {
ansond 29:be035befb437 370 return dynamic_resources->at(i);
ansond 0:1f1f55e73248 371 }
ansond 0:1f1f55e73248 372 }
ansond 29:be035befb437 373 return NULL;
ansond 0:1f1f55e73248 374 }
ansond 0:1f1f55e73248 375
ansond 8:f950fb1b78c0 376 // build out the endpoint
ansond 8:f950fb1b78c0 377 void Endpoint::build_endpoint()
ansond 0:1f1f55e73248 378 {
ansond 27:b8aaf7dc7023 379 // initialize as an mbed-client
ansond 27:b8aaf7dc7023 380 this->create_interface();
ansond 0:1f1f55e73248 381
ansond 27:b8aaf7dc7023 382 // Create our server instance
ansond 27:b8aaf7dc7023 383 this->m_server_instance = this->create_server_instance();
ansond 27:b8aaf7dc7023 384
ansond 27:b8aaf7dc7023 385 // We now have to bind our device resources
ansond 27:b8aaf7dc7023 386 if (this->m_device_manager != NULL) {
ansond 40:5c039dcbd7b2 387 // DEBUG
ansond 40:5c039dcbd7b2 388 this->logger()->log("Endpoint::build(): plumbing the device management objects and resources...");
ansond 40:5c039dcbd7b2 389
ansond 27:b8aaf7dc7023 390 // bind the device manager
ansond 27:b8aaf7dc7023 391 ((DeviceManager *)this->m_device_manager)->bind();
ansond 27:b8aaf7dc7023 392
ansond 27:b8aaf7dc7023 393 // push back the Device Resources Object
ansond 27:b8aaf7dc7023 394 if (this->m_options->getDeviceResourcesObject() != NULL) {
ansond 27:b8aaf7dc7023 395 // DEBUG
ansond 27:b8aaf7dc7023 396 this->logger()->log("Endpoint::build(): plumbing device resources object...");
ansond 27:b8aaf7dc7023 397
ansond 27:b8aaf7dc7023 398 // push back the device resources object
ansond 27:b8aaf7dc7023 399 this->m_object_list.push_back((M2MObject *)this->m_options->getDeviceResourcesObject());
ansond 27:b8aaf7dc7023 400 }
ansond 27:b8aaf7dc7023 401 else {
ansond 27:b8aaf7dc7023 402 // unable to plumb device manager
ansond 27:b8aaf7dc7023 403 this->logger()->log("Endpoint::build(): Unable to plumb device resources. Not installing device resource object...");
ansond 27:b8aaf7dc7023 404 }
ansond 27:b8aaf7dc7023 405
ansond 27:b8aaf7dc7023 406 // push back the Firmware Resources Object
ansond 27:b8aaf7dc7023 407 if (this->m_options->getFirmwareResourcesObject() != NULL) {
ansond 27:b8aaf7dc7023 408 // DEBUG
ansond 27:b8aaf7dc7023 409 this->logger()->log("Endpoint::build(): plumbing firmware resources object...");
ansond 27:b8aaf7dc7023 410
ansond 27:b8aaf7dc7023 411 // push back the firmware resources object
ansond 27:b8aaf7dc7023 412 this->m_object_list.push_back((M2MObject *)this->m_options->getFirmwareResourcesObject());
ansond 27:b8aaf7dc7023 413 }
ansond 27:b8aaf7dc7023 414 else {
ansond 27:b8aaf7dc7023 415 // unable to plumb firmware manager
ansond 27:b8aaf7dc7023 416 this->logger()->log("Endpoint::build(): Unable to plumb firmware resources. Not installing firmware resource object...");
ansond 27:b8aaf7dc7023 417 }
ansond 27:b8aaf7dc7023 418 }
ansond 27:b8aaf7dc7023 419 else {
ansond 27:b8aaf7dc7023 420 // no device manager installed
ansond 27:b8aaf7dc7023 421 this->logger()->log("Endpoint::build(): No device manager installed.");
ansond 0:1f1f55e73248 422 }
ansond 0:1f1f55e73248 423
ansond 0:1f1f55e73248 424 // Loop through Static Resources and bind each of them...
ansond 8:f950fb1b78c0 425 this->logger()->log("Endpoint::build(): adding static resources...");
ansond 0:1f1f55e73248 426 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
ansond 0:1f1f55e73248 427 for(int i=0; i<(int)static_resources->size(); ++i) {
ansond 8:f950fb1b78c0 428 this->logger()->log("Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str());
ansond 27:b8aaf7dc7023 429 static_resources->at(i)->bind(this);
ansond 0:1f1f55e73248 430 }
ansond 0:1f1f55e73248 431
ansond 0:1f1f55e73248 432 // Loop through Dynamic Resources and bind each of them...
ansond 8:f950fb1b78c0 433 this->logger()->log("Endpoint::build(): adding dynamic resources...");
ansond 0:1f1f55e73248 434 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 0:1f1f55e73248 435 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 8:f950fb1b78c0 436 this->logger()->log("Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
ansond 27:b8aaf7dc7023 437 dynamic_resources->at(i)->bind(this);
ansond 27:b8aaf7dc7023 438 }
ansond 27:b8aaf7dc7023 439
ansond 27:b8aaf7dc7023 440 // Get the ObjectList from the ObjectInstanceManager...
ansond 27:b8aaf7dc7023 441 NamedPointerList list = this->getObjectInstanceManager()->getObjectList();
ansond 27:b8aaf7dc7023 442
ansond 27:b8aaf7dc7023 443 // DEBUG
ansond 27:b8aaf7dc7023 444 this->logger()->log("Endpoint::build(): All Resources bound. Number of Objects in list: %d",list.size());
ansond 27:b8aaf7dc7023 445
ansond 27:b8aaf7dc7023 446 // add all of the object instances we have created...
ansond 38:bb6d2be4d54c 447 for(int i=0;i<(int)list.size();++i) {
ansond 27:b8aaf7dc7023 448 // DEBUG
ansond 27:b8aaf7dc7023 449 this->logger()->log("Endpoint::build(): adding Object Instance with ObjID: %s...",list.at(i).name().c_str());
ansond 27:b8aaf7dc7023 450
ansond 27:b8aaf7dc7023 451 // push back the object instance...
ansond 27:b8aaf7dc7023 452 this->m_object_list.push_back((M2MObject *)(list.at(i).ptr()));
ansond 0:1f1f55e73248 453 }
ansond 0:1f1f55e73248 454 }
ansond 0:1f1f55e73248 455
ansond 13:9edad7677211 456 // underlying network is connected (SET)
ansond 13:9edad7677211 457 void Endpoint::isConnected(bool connected) {
ansond 13:9edad7677211 458 this->m_connected = connected;
ansond 13:9edad7677211 459 }
ansond 13:9edad7677211 460
ansond 13:9edad7677211 461 // underlying network is connected (GET)
ansond 13:9edad7677211 462 bool Endpoint::isConnected() {
ansond 13:9edad7677211 463 return this->m_connected;
ansond 13:9edad7677211 464 }
ansond 13:9edad7677211 465
ansond 15:c11dbe4d354c 466 // Registered with mDC/mDS
ansond 15:c11dbe4d354c 467 bool Endpoint::isRegistered() {
ansond 15:c11dbe4d354c 468 return this->m_registered;
ansond 15:c11dbe4d354c 469 }
ansond 15:c11dbe4d354c 470
ansond 43:3fb57c4fba34 471 // Set the ConnectionStatusInterface
ansond 27:b8aaf7dc7023 472 void Endpoint::setConnectionStatusInterfaceImpl(ConnectionStatusInterface *csi) {
ansond 43:3fb57c4fba34 473 this->m_csi = csi;
ansond 27:b8aaf7dc7023 474 }
ansond 27:b8aaf7dc7023 475
ansond 27:b8aaf7dc7023 476 // Set our ObjectInstanceManager
ansond 27:b8aaf7dc7023 477 void Endpoint::setObjectInstanceManager(ObjectInstanceManager *oim) {
ansond 27:b8aaf7dc7023 478 this->m_oim = oim;
ansond 27:b8aaf7dc7023 479 }
ansond 27:b8aaf7dc7023 480
ansond 27:b8aaf7dc7023 481 // Get our ObjectInstanceManager
ansond 27:b8aaf7dc7023 482 ObjectInstanceManager *Endpoint::getObjectInstanceManager() {
ansond 27:b8aaf7dc7023 483 return this->m_oim;
ansond 27:b8aaf7dc7023 484 }
ansond 27:b8aaf7dc7023 485
ansond 0:1f1f55e73248 486 // our logger
ansond 0:1f1f55e73248 487 Logger *Endpoint::logger()
ansond 0:1f1f55e73248 488 {
ansond 0:1f1f55e73248 489 return this->m_logger;
ansond 0:1f1f55e73248 490 }
ansond 0:1f1f55e73248 491
ansond 0:1f1f55e73248 492 } // namespace Connector