use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Fri Nov 04 16:54:32 2016 +0000
Revision:
73:f12a767bc300
Parent:
72:6b1d37b5420a
Child:
74:6abfb2a03020
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 1:16f0fb5b8d97 35 // our endpoint instance
ansond 1:16f0fb5b8d97 36 static Connector::Endpoint *__endpoint = NULL;
ansond 0:1f1f55e73248 37
ansond 54:dfee8691c83a 38 // LWIP Network interface instance
ansond 60:0d9e607dd678 39 NetworkInterface *__network_interface = NULL;
ansond 27:b8aaf7dc7023 40
ansond 0:1f1f55e73248 41 // Connector namespace
ansond 0:1f1f55e73248 42 namespace Connector {
ansond 0:1f1f55e73248 43
ansond 0:1f1f55e73248 44 // STATIC: Plumb the network
ansond 13:9edad7677211 45 void Endpoint::plumbNetwork(void *device_manager,bool canActAsRouterNode) {
ansond 54:dfee8691c83a 46 // create our endpoint instance...
ansond 1:16f0fb5b8d97 47 if (__endpoint == NULL) {
ansond 1:16f0fb5b8d97 48 // initialize our endpoint instance
ansond 54:dfee8691c83a 49 printf("Connector::Endpoint::plumbNetwork: initializing endpoint instance...\r\n");
ansond 1:16f0fb5b8d97 50 __endpoint = (Connector::Endpoint *)utils_init_endpoint(canActAsRouterNode);
ansond 1:16f0fb5b8d97 51 }
ansond 13:9edad7677211 52
ansond 13:9edad7677211 53 // set the device manager
ansond 13:9edad7677211 54 if (device_manager != NULL) {
ansond 13:9edad7677211 55 // device manager has been supplied
ansond 54:dfee8691c83a 56 printf("Connector::Endpoint::plumbNetwork: setting a device manager...\r\n");
ansond 13:9edad7677211 57 __endpoint->setDeviceManager(device_manager);
ansond 13:9edad7677211 58 }
ansond 13:9edad7677211 59 else {
ansond 13:9edad7677211 60 // no device manager supplied
ansond 54:dfee8691c83a 61 printf("Connector::Endpoint::plumbNetwork: no device manager supplied (OK)\r\n");
ansond 13:9edad7677211 62 }
ansond 8:f950fb1b78c0 63
ansond 54:dfee8691c83a 64 // configure the endpoint...
ansond 54:dfee8691c83a 65 printf("Connector::Endpoint::plumbNetwork: configuring endpoint...\r\n");
ansond 1:16f0fb5b8d97 66 utils_configure_endpoint((void *)__endpoint);
ansond 2:1a7a292555d1 67
ansond 54:dfee8691c83a 68 // plumb the endpoint's network...
ansond 54:dfee8691c83a 69 printf("Connector::Endpoint::plumbNetwork: plumbing network...\r\n");
ansond 8:f950fb1b78c0 70 net_plumb_network((void *)__endpoint);
ansond 0:1f1f55e73248 71 }
ansond 0:1f1f55e73248 72
ansond 0:1f1f55e73248 73 // STATIC: Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
ansond 8:f950fb1b78c0 74 void Endpoint::start()
ansond 8:f950fb1b78c0 75 {
ansond 54:dfee8691c83a 76 // build out the endpoint with its configuration...
ansond 54:dfee8691c83a 77 printf("Connector::Endpoint::start: building out endpoint...\r\n");
ansond 8:f950fb1b78c0 78 utils_build_endpoint((void *)__endpoint);
ansond 0:1f1f55e73248 79
ansond 54:dfee8691c83a 80 // finalize the endpoint and start its main loop
ansond 54:dfee8691c83a 81 printf("Endpoint::start: finalize and run the endpoint main loop..\r\n");
ansond 54:dfee8691c83a 82 net_finalize_and_run_endpoint_main_loop((void *)__endpoint);
ansond 0:1f1f55e73248 83 }
ansond 0:1f1f55e73248 84
ansond 43:3fb57c4fba34 85 // STATIC: Set the ConnectionStatusInterface Implementation instance
ansond 43:3fb57c4fba34 86 void Endpoint::setConnectionStatusInterface(ConnectionStatusInterface *csi) {
ansond 43:3fb57c4fba34 87 if (__endpoint != NULL) {
ansond 44:7c73baf9f4c1 88 __endpoint->setConnectionStatusInterfaceImpl(csi);
ansond 43:3fb57c4fba34 89 }
ansond 43:3fb57c4fba34 90 }
ansond 43:3fb57c4fba34 91
ansond 0:1f1f55e73248 92 // Constructor
ansond 0:1f1f55e73248 93 Endpoint::Endpoint(const Logger *logger, const Options *options) : M2MInterfaceObserver()
ansond 0:1f1f55e73248 94 {
ansond 0:1f1f55e73248 95 this->m_logger = (Logger *)logger;
ansond 0:1f1f55e73248 96 this->m_options = (Options *)options;
ansond 13:9edad7677211 97 this->m_device_manager = NULL;
ansond 13:9edad7677211 98 this->m_connected = false;
ansond 15:c11dbe4d354c 99 this->m_registered = false;
ansond 27:b8aaf7dc7023 100 this->m_csi = NULL;
ansond 27:b8aaf7dc7023 101 this->m_oim = NULL;
ansond 0:1f1f55e73248 102 }
ansond 0:1f1f55e73248 103
ansond 0:1f1f55e73248 104 // Copy Constructor
ansond 0:1f1f55e73248 105 Endpoint::Endpoint(const Endpoint &ep)
ansond 0:1f1f55e73248 106 {
ansond 0:1f1f55e73248 107 this->m_logger = ep.m_logger;
ansond 0:1f1f55e73248 108 this->m_options = ep.m_options;
ansond 46:62da4ce20276 109 this->m_endpoint_interface = ep.m_endpoint_interface;
ansond 73:f12a767bc300 110 #ifndef ENABLE_MBED_CLOUD_SUPPORT
ansond 46:62da4ce20276 111 this->m_endpoint_security = ep.m_endpoint_security;
ansond 73:f12a767bc300 112 #endif
ansond 46:62da4ce20276 113 this->m_endpoint_object_list = ep.m_endpoint_object_list;
ansond 13:9edad7677211 114 this->m_device_manager = ep.m_device_manager;
ansond 13:9edad7677211 115 this->m_connected = ep.m_connected;
ansond 15:c11dbe4d354c 116 this->m_registered = ep.m_registered;
ansond 27:b8aaf7dc7023 117 this->m_csi = ep.m_csi;
ansond 27:b8aaf7dc7023 118 this->m_oim = ep.m_oim;
ansond 0:1f1f55e73248 119 }
ansond 0:1f1f55e73248 120
ansond 0:1f1f55e73248 121 // Destructor
ansond 0:1f1f55e73248 122 Endpoint::~Endpoint() {
ansond 46:62da4ce20276 123 if (this->m_endpoint_interface != NULL)
ansond 46:62da4ce20276 124 delete this->m_endpoint_interface;
ansond 73:f12a767bc300 125
ansond 73:f12a767bc300 126 #ifndef ENABLE_MBED_CLOUD_SUPPORT
ansond 46:62da4ce20276 127 if (this->m_endpoint_security != NULL)
ansond 46:62da4ce20276 128 delete this->m_endpoint_security;
ansond 73:f12a767bc300 129 #endif
ansond 0:1f1f55e73248 130 }
ansond 0:1f1f55e73248 131
ansond 13:9edad7677211 132 // set the device manager
ansond 13:9edad7677211 133 void Endpoint::setDeviceManager(void *device_manager) {
ansond 13:9edad7677211 134 this->m_device_manager = device_manager;
ansond 13:9edad7677211 135 }
ansond 13:9edad7677211 136
ansond 13:9edad7677211 137 // get the device manager
ansond 13:9edad7677211 138 void *Endpoint::getDeviceManager(void) {
ansond 13:9edad7677211 139 return this->m_device_manager;
ansond 13:9edad7677211 140 }
ansond 13:9edad7677211 141
ansond 1:16f0fb5b8d97 142 // router node behavior setting
ansond 1:16f0fb5b8d97 143 void Endpoint::asRouterNode(bool canActAsRouterNode) {
ansond 1:16f0fb5b8d97 144 this->m_canActAsRouterNode = canActAsRouterNode;
ansond 1:16f0fb5b8d97 145 }
ansond 1:16f0fb5b8d97 146
ansond 1:16f0fb5b8d97 147 // set our Options
ansond 1:16f0fb5b8d97 148 void Endpoint::setOptions(Options *options) {
ansond 1:16f0fb5b8d97 149 this->m_options = options;
ansond 1:16f0fb5b8d97 150 }
ansond 1:16f0fb5b8d97 151
ansond 0:1f1f55e73248 152 // get our Options
ansond 0:1f1f55e73248 153 Options *Endpoint::getOptions() {
ansond 0:1f1f55e73248 154 return this->m_options;
ansond 0:1f1f55e73248 155 }
ansond 0:1f1f55e73248 156
ansond 73:f12a767bc300 157 #ifndef ENABLE_MBED_CLOUD_SUPPORT
ansond 0:1f1f55e73248 158 // get our Server
ansond 46:62da4ce20276 159 M2MSecurity *Endpoint::getEndpointSecurity() {
ansond 46:62da4ce20276 160 return this->m_endpoint_security;
ansond 0:1f1f55e73248 161 }
ansond 73:f12a767bc300 162 #endif
ansond 0:1f1f55e73248 163
ansond 0:1f1f55e73248 164 // get our ObjectList
ansond 46:62da4ce20276 165 M2MObjectList Endpoint::getEndpointObjectList() {
ansond 46:62da4ce20276 166 return this->m_endpoint_object_list;
ansond 46:62da4ce20276 167 }
ansond 46:62da4ce20276 168
ansond 73:f12a767bc300 169 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 73:f12a767bc300 170 // get our endpoint interface
ansond 73:f12a767bc300 171 MbedCloudClient *Endpoint::getEndpointInterface() {
ansond 73:f12a767bc300 172 return this->m_endpoint_interface;
ansond 73:f12a767bc300 173 }
ansond 73:f12a767bc300 174 #else
ansond 46:62da4ce20276 175 // get our endpoint interface
ansond 46:62da4ce20276 176 M2MInterface *Endpoint::getEndpointInterface() {
ansond 46:62da4ce20276 177 return this->m_endpoint_interface;
ansond 0:1f1f55e73248 178 }
ansond 73:f12a767bc300 179 #endif
ansond 0:1f1f55e73248 180
ansond 71:5069a202e892 181 // Connector::Endpoint: create our interface
ansond 71:5069a202e892 182 void Endpoint::createEndpointInterface() {
ansond 71:5069a202e892 183 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 72:6b1d37b5420a 184 this->createCloudEndpointInterface();
ansond 71:5069a202e892 185 #else
ansond 72:6b1d37b5420a 186 this->createConnectorEndpointInterface();
ansond 71:5069a202e892 187 #endif
ansond 71:5069a202e892 188 }
ansond 71:5069a202e892 189
ansond 71:5069a202e892 190 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 71:5069a202e892 191 // mbedCloudClient: create our interface
ansond 71:5069a202e892 192 void Endpoint::createCloudEndpointInterface() {
ansond 71:5069a202e892 193 this->m_endpoint_interface = new MbedCloudClient();
ansond 71:5069a202e892 194
ansond 71:5069a202e892 195 // bind LWIP network interface pointer...
ansond 71:5069a202e892 196 if (__network_interface != NULL && this->m_endpoint_interface != NULL) {
ansond 71:5069a202e892 197 this->logger()->log("Connector::Endpoint: binding LWIP network instance (Cloud)...");
ansond 71:5069a202e892 198 this->m_endpoint_interface->setup(__network_interface);
ansond 71:5069a202e892 199 }
ansond 71:5069a202e892 200 }
ansond 71:5069a202e892 201 #endif
ansond 71:5069a202e892 202
ansond 73:f12a767bc300 203 #ifndef ENABLE_MBED_CLOUD_SUPPORT
ansond 0:1f1f55e73248 204 // mbed-client: create our interface
ansond 71:5069a202e892 205 void Endpoint::createConnectorEndpointInterface() {
ansond 10:3f79b5e67c22 206 // get the CoAP listening port
ansond 10:3f79b5e67c22 207 uint16_t listening_port = (uint16_t)this->m_options->getConnectorPort();
ansond 10:3f79b5e67c22 208
ansond 10:3f79b5e67c22 209 // randomize the port if we are using certificates...
ansond 10:3f79b5e67c22 210 if (this->m_options->getServerCertificateSize() > 0) {
ansond 0:1f1f55e73248 211 // Randomizing listening port for Certificate mode connectivity
ansond 0:1f1f55e73248 212 srand(time(NULL));
ansond 10:3f79b5e67c22 213 listening_port = rand() % 65535 + 12345;
ansond 10:3f79b5e67c22 214 }
ansond 10:3f79b5e67c22 215
ansond 60:0d9e607dd678 216 // DEBUG
ansond 61:d02cd5e2bb26 217 //this->logger()->log("Connector::Endpoint: listening port: %d",listening_port);
ansond 60:0d9e607dd678 218
ansond 61:d02cd5e2bb26 219 // Socket protocol type: TCP or UDP
ansond 61:d02cd5e2bb26 220 M2MInterface::BindingMode socket_protocol_type = M2MInterface::UDP;
ansond 61:d02cd5e2bb26 221 if (this->m_options->getCoAPConnectionType() == COAP_TCP) socket_protocol_type = M2MInterface::TCP;
ansond 10:3f79b5e67c22 222
ansond 61:d02cd5e2bb26 223 // Socket address type: IPv4 or IPv6
ansond 61:d02cd5e2bb26 224 M2MInterface::NetworkStack socket_address_type = M2MInterface::LwIP_IPv4;
ansond 62:e5882bd28210 225 if (this->m_options->getIPAddressType() == IP_ADDRESS_TYPE_IPV6) {
ansond 61:d02cd5e2bb26 226 // IPv6 mode for the socket addressing type...
ansond 61:d02cd5e2bb26 227 socket_address_type = M2MInterface::LwIP_IPv6;
ansond 59:dd395412bd19 228
ansond 59:dd395412bd19 229 #if defined (IPV4_OVERRIDE)
ansond 61:d02cd5e2bb26 230 // OVERRIDE (until patched...)
ansond 61:d02cd5e2bb26 231 this->logger()->log("Connector::Endpoint: Socket Address Type: IPv4 (IPv6 OVERRIDE)");
ansond 61:d02cd5e2bb26 232 socket_address_type = M2MInterface::LwIP_IPv4;
ansond 59:dd395412bd19 233 #endif
ansond 59:dd395412bd19 234 }
ansond 10:3f79b5e67c22 235
ansond 10:3f79b5e67c22 236 // DEBUG
ansond 61:d02cd5e2bb26 237 if (socket_protocol_type == M2MInterface::TCP) this->logger()->log("Connector::Endpoint: Socket Protocol: TCP");
ansond 61:d02cd5e2bb26 238 if (socket_protocol_type == M2MInterface::UDP) this->logger()->log("Connector::Endpoint: Socket Protocol: UDP");
ansond 61:d02cd5e2bb26 239 if (socket_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Connector::Endpoint: Socket Address Type: IPv4");
ansond 61:d02cd5e2bb26 240 if (socket_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Connector::Endpoint: Socket Address Type: IPv6");
ansond 10:3f79b5e67c22 241
ansond 61:d02cd5e2bb26 242 // Create the endpoint M2MInterface instance
ansond 46:62da4ce20276 243 this->m_endpoint_interface = M2MInterfaceFactory::create_interface(*this,
ansond 61:d02cd5e2bb26 244 (char *)this->m_options->getEndpointNodename().c_str(), // endpoint name
ansond 61:d02cd5e2bb26 245 (char *)this->m_options->getEndpointType().c_str(), // endpoint type
ansond 61:d02cd5e2bb26 246 (int32_t)this->m_options->getLifetime(), // registration lifetime (in seconds)
ansond 61:d02cd5e2bb26 247 listening_port, // listening port (ephemeral...)
ansond 61:d02cd5e2bb26 248 (char *)this->m_options->getDomain().c_str(), // endpoint domain
ansond 61:d02cd5e2bb26 249 socket_protocol_type, // Socket protocol type: UDP or TCP...
ansond 61:d02cd5e2bb26 250 socket_address_type, // Socket addressing type: IPv4 or IPv6
ansond 61:d02cd5e2bb26 251 CONTEXT_ADDRESS_STRING // context address string (mbedConnectorInterface.h)
ansond 60:0d9e607dd678 252 );
ansond 61:d02cd5e2bb26 253
ansond 61:d02cd5e2bb26 254 // bind LWIP network interface pointer...
ansond 46:62da4ce20276 255 if (__network_interface != NULL && this->m_endpoint_interface != NULL) {
ansond 71:5069a202e892 256 this->logger()->log("Connector::Endpoint: binding LWIP network instance (Connector)...");
ansond 60:0d9e607dd678 257 this->m_endpoint_interface->set_platform_network_handler((void *)__network_interface);
ansond 27:b8aaf7dc7023 258 }
ansond 0:1f1f55e73248 259 }
ansond 0:1f1f55e73248 260
ansond 46:62da4ce20276 261 // mbed-client: createEndpointInstance()
ansond 46:62da4ce20276 262 M2MSecurity *Endpoint::createEndpointInstance() {
ansond 0:1f1f55e73248 263 // Creates register server object with mbed device server address and other parameters
ansond 0:1f1f55e73248 264 M2MSecurity *server = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
ansond 0:1f1f55e73248 265 if (server != NULL) {
ansond 0:1f1f55e73248 266 const String url = this->m_options->getConnectorURL();
ansond 0:1f1f55e73248 267 server->set_resource_value(M2MSecurity::M2MServerUri, url);
ansond 38:bb6d2be4d54c 268 server->set_resource_value(M2MSecurity::BootstrapServer, false);
ansond 0:1f1f55e73248 269 server->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
ansond 0:1f1f55e73248 270 server->set_resource_value(M2MSecurity::ServerPublicKey,this->m_options->getServerCertificate(),this->m_options->getServerCertificateSize());
ansond 0:1f1f55e73248 271 server->set_resource_value(M2MSecurity::PublicKey,this->m_options->getClientCertificate(),this->m_options->getClientCertificateSize());
ansond 0:1f1f55e73248 272 server->set_resource_value(M2MSecurity::Secretkey,this->m_options->getClientKey(),this->m_options->getClientKeySize());
ansond 0:1f1f55e73248 273 }
ansond 0:1f1f55e73248 274 return server;
ansond 0:1f1f55e73248 275 }
ansond 71:5069a202e892 276 #endif
ansond 0:1f1f55e73248 277
ansond 0:1f1f55e73248 278 // mbed-client: Callback from mbed client stack if any error is encountered
ansond 0:1f1f55e73248 279 void Endpoint::error(M2MInterface::Error error) {
ansond 0:1f1f55e73248 280 switch(error){
ansond 0:1f1f55e73248 281 case M2MInterface::AlreadyExists:
ansond 54:dfee8691c83a 282 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::AlreadyExists");
ansond 0:1f1f55e73248 283 break;
ansond 0:1f1f55e73248 284 case M2MInterface::BootstrapFailed:
ansond 54:dfee8691c83a 285 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::BootstrapFailed");
ansond 0:1f1f55e73248 286 break;
ansond 0:1f1f55e73248 287 case M2MInterface::InvalidParameters:
ansond 54:dfee8691c83a 288 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::InvalidParameters");
ansond 0:1f1f55e73248 289 break;
ansond 0:1f1f55e73248 290 case M2MInterface::NotRegistered:
ansond 54:dfee8691c83a 291 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotRegistered");
ansond 0:1f1f55e73248 292 break;
ansond 0:1f1f55e73248 293 case M2MInterface::Timeout:
ansond 54:dfee8691c83a 294 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::Timeout");
ansond 0:1f1f55e73248 295 break;
ansond 0:1f1f55e73248 296 case M2MInterface::NetworkError:
ansond 54:dfee8691c83a 297 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NetworkError");
ansond 0:1f1f55e73248 298 break;
ansond 0:1f1f55e73248 299 case M2MInterface::ResponseParseFailed:
ansond 54:dfee8691c83a 300 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::ResponseParseFailed");
ansond 0:1f1f55e73248 301 break;
ansond 0:1f1f55e73248 302 case M2MInterface::UnknownError:
ansond 54:dfee8691c83a 303 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::UnknownError");
ansond 0:1f1f55e73248 304 break;
ansond 0:1f1f55e73248 305 case M2MInterface::MemoryFail:
ansond 54:dfee8691c83a 306 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::MemoryFail");
ansond 0:1f1f55e73248 307 break;
ansond 0:1f1f55e73248 308 case M2MInterface::NotAllowed:
ansond 54:dfee8691c83a 309 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotAllowed");
ansond 0:1f1f55e73248 310 break;
ansond 0:1f1f55e73248 311 default:
ansond 0:1f1f55e73248 312 break;
ansond 0:1f1f55e73248 313 }
ansond 0:1f1f55e73248 314 }
ansond 0:1f1f55e73248 315
ansond 8:f950fb1b78c0 316 // register the endpoint
ansond 46:62da4ce20276 317 void Endpoint::register_endpoint(M2MSecurity *endpoint_security, M2MObjectList endpoint_objects) {
ansond 46:62da4ce20276 318 if (this->m_endpoint_interface != NULL && endpoint_security != NULL && endpoint_objects.size() > 0) {
ansond 23:5852c0884714 319 // register endpoint
ansond 54:dfee8691c83a 320 this->logger()->log("Connector::Endpoint: registering endpoint...");
ansond 46:62da4ce20276 321 this->m_endpoint_interface->register_object(endpoint_security, endpoint_objects);
ansond 23:5852c0884714 322 }
ansond 0:1f1f55e73248 323 }
ansond 0:1f1f55e73248 324
ansond 8:f950fb1b78c0 325 // re-register the endpoint
ansond 8:f950fb1b78c0 326 void Endpoint::re_register_endpoint() {
ansond 46:62da4ce20276 327 if (this->m_endpoint_interface != NULL) {
ansond 46:62da4ce20276 328 this->m_endpoint_interface->update_registration(this->m_endpoint_security,this->m_options->getLifetime());
ansond 23:5852c0884714 329 }
ansond 8:f950fb1b78c0 330 }
ansond 8:f950fb1b78c0 331
ansond 8:f950fb1b78c0 332 // de-register endpoint
ansond 8:f950fb1b78c0 333 void Endpoint::de_register_endpoint(void) {
ansond 46:62da4ce20276 334 if (this->m_endpoint_interface != NULL) {
ansond 0:1f1f55e73248 335 // de-register endpoint
ansond 54:dfee8691c83a 336 this->logger()->log("Connector::Endpoint: de-registering endpoint...");
ansond 46:62da4ce20276 337 this->m_endpoint_interface->unregister_object(NULL);
ansond 46:62da4ce20276 338 if (this->m_csi != NULL) {
ansond 46:62da4ce20276 339 this->m_csi->begin_object_unregistering((void *)this);
ansond 46:62da4ce20276 340 }
ansond 0:1f1f55e73248 341 }
ansond 0:1f1f55e73248 342 }
ansond 0:1f1f55e73248 343
ansond 8:f950fb1b78c0 344 // object registered
ansond 27:b8aaf7dc7023 345 void Endpoint::object_registered(M2MSecurity *security, const M2MServer &server) {
ansond 54:dfee8691c83a 346 this->logger()->log("Connector::Endpoint: endpoint registered.");
ansond 15:c11dbe4d354c 347 this->m_connected = true;
ansond 15:c11dbe4d354c 348 this->m_registered = true;
ansond 27:b8aaf7dc7023 349 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 350 this->m_csi->object_registered((void *)this,(void *)security,(void *)&server);
ansond 27:b8aaf7dc7023 351 }
ansond 8:f950fb1b78c0 352 }
ansond 8:f950fb1b78c0 353
ansond 8:f950fb1b78c0 354 // registration updated
ansond 27:b8aaf7dc7023 355 void Endpoint::registration_updated(M2MSecurity *security, const M2MServer &server) {
ansond 54:dfee8691c83a 356 this->logger()->log("Connector::Endpoint: endpoint re-registered.");
ansond 15:c11dbe4d354c 357 this->m_connected = true;
ansond 15:c11dbe4d354c 358 this->m_registered = true;
ansond 27:b8aaf7dc7023 359 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 360 this->m_csi->registration_updated((void *)this,(void *)security,(void *)&server);
ansond 27:b8aaf7dc7023 361 }
ansond 8:f950fb1b78c0 362 }
ansond 8:f950fb1b78c0 363
ansond 8:f950fb1b78c0 364 // object unregistered
ansond 27:b8aaf7dc7023 365 void Endpoint::object_unregistered(M2MSecurity *server) {
ansond 54:dfee8691c83a 366 // DEBUG
ansond 54:dfee8691c83a 367 this->logger()->log("Connector::Endpoint: endpoint de-registered.");
ansond 54:dfee8691c83a 368
ansond 54:dfee8691c83a 369 // no longer connected/registered
ansond 15:c11dbe4d354c 370 this->m_registered = false;
ansond 54:dfee8691c83a 371 this->m_connected = false;
ansond 54:dfee8691c83a 372
ansond 54:dfee8691c83a 373 // stop all observers...
ansond 54:dfee8691c83a 374 this->stopObservations();
ansond 54:dfee8691c83a 375
ansond 54:dfee8691c83a 376 // invoke ConnectionHandler if we have one...
ansond 27:b8aaf7dc7023 377 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 378 this->m_csi->object_unregistered((void *)this,(void *)server);
ansond 27:b8aaf7dc7023 379 }
ansond 54:dfee8691c83a 380
ansond 54:dfee8691c83a 381 // halt the main event loop... we are done.
ansond 54:dfee8691c83a 382 net_shutdown_endpoint();
ansond 0:1f1f55e73248 383 }
ansond 0:1f1f55e73248 384
ansond 0:1f1f55e73248 385 // bootstrap done
ansond 27:b8aaf7dc7023 386 void Endpoint::bootstrap_done(M2MSecurity *server) {
ansond 54:dfee8691c83a 387 this->logger()->log("Connector::Endpoint: endpoint bootstrapped.");
ansond 27:b8aaf7dc7023 388 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 389 this->m_csi->bootstrapped((void *)this,(void *)server);
ansond 27:b8aaf7dc7023 390 }
ansond 0:1f1f55e73248 391 }
ansond 0:1f1f55e73248 392
ansond 0:1f1f55e73248 393 // resource value updated
ansond 0:1f1f55e73248 394 void Endpoint::value_updated(M2MBase *base, M2MBase::BaseType type) {
ansond 29:be035befb437 395 // Lookup the resource and invoke process() on it...
ansond 0:1f1f55e73248 396 DynamicResource *target_res = this->lookupDynamicResource(base);
ansond 29:be035befb437 397 if (target_res != NULL) {
ansond 29:be035befb437 398 // DEBUG
ansond 43:3fb57c4fba34 399 //this->logger()->log("Value Updated (Custom Resource)");
ansond 29:be035befb437 400
ansond 29:be035befb437 401 // its a custom resource...
ansond 29:be035befb437 402 target_res->process(base->operation(),type);
ansond 29:be035befb437 403 }
ansond 29:be035befb437 404 else {
ansond 29:be035befb437 405 // DEBUG
ansond 43:3fb57c4fba34 406 //this->logger()->log("Value Updated (Device Manager)");
ansond 29:be035befb437 407
ansond 29:be035befb437 408 // let DeviceManager handle it
ansond 29:be035befb437 409 ((DeviceManager *)this->m_device_manager)->process(base,type);
ansond 29:be035befb437 410 }
ansond 29:be035befb437 411
ansond 29:be035befb437 412 // CSI
ansond 27:b8aaf7dc7023 413 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 414 this->m_csi->value_updated((void *)this,(void *)base,(int)type);
ansond 27:b8aaf7dc7023 415 }
ansond 0:1f1f55e73248 416 }
ansond 0:1f1f55e73248 417
ansond 0:1f1f55e73248 418 // lookup which DynamicResource cooresponds to a given M2MBase instance...
ansond 0:1f1f55e73248 419 DynamicResource *Endpoint::lookupDynamicResource(M2MBase *base) {
ansond 0:1f1f55e73248 420 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 29:be035befb437 421 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 29:be035befb437 422 M2MBase *t = (M2MBase *)dynamic_resources->at(i)->getResource();
ansond 0:1f1f55e73248 423 if (t == base) {
ansond 29:be035befb437 424 return dynamic_resources->at(i);
ansond 0:1f1f55e73248 425 }
ansond 0:1f1f55e73248 426 }
ansond 29:be035befb437 427 return NULL;
ansond 0:1f1f55e73248 428 }
ansond 0:1f1f55e73248 429
ansond 8:f950fb1b78c0 430 // build out the endpoint
ansond 46:62da4ce20276 431 void Endpoint::buildEndpoint()
ansond 0:1f1f55e73248 432 {
ansond 27:b8aaf7dc7023 433 // initialize as an mbed-client
ansond 46:62da4ce20276 434 this->createEndpointInterface();
ansond 71:5069a202e892 435
ansond 71:5069a202e892 436 #ifndef ENABLE_MBED_CLOUD_SUPPORT
ansond 27:b8aaf7dc7023 437 // Create our server instance
ansond 46:62da4ce20276 438 this->m_endpoint_security = this->createEndpointInstance();
ansond 71:5069a202e892 439 #endif
ansond 27:b8aaf7dc7023 440
ansond 27:b8aaf7dc7023 441 // We now have to bind our device resources
ansond 27:b8aaf7dc7023 442 if (this->m_device_manager != NULL) {
ansond 40:5c039dcbd7b2 443 // DEBUG
ansond 54:dfee8691c83a 444 this->logger()->log("Connector::Endpoint::build(): plumbing the device management objects and resources...");
ansond 40:5c039dcbd7b2 445
ansond 27:b8aaf7dc7023 446 // bind the device manager
ansond 27:b8aaf7dc7023 447 ((DeviceManager *)this->m_device_manager)->bind();
ansond 27:b8aaf7dc7023 448
ansond 27:b8aaf7dc7023 449 // push back the Device Resources Object
ansond 27:b8aaf7dc7023 450 if (this->m_options->getDeviceResourcesObject() != NULL) {
ansond 27:b8aaf7dc7023 451 // DEBUG
ansond 54:dfee8691c83a 452 this->logger()->log("Connector::Endpoint::build(): plumbing device resources object...");
ansond 27:b8aaf7dc7023 453
ansond 27:b8aaf7dc7023 454 // push back the device resources object
ansond 46:62da4ce20276 455 this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getDeviceResourcesObject());
ansond 27:b8aaf7dc7023 456 }
ansond 27:b8aaf7dc7023 457 else {
ansond 27:b8aaf7dc7023 458 // unable to plumb device manager
ansond 54:dfee8691c83a 459 this->logger()->log("Connector::Endpoint::build(): Unable to plumb device resources. Not installing device resource object...");
ansond 27:b8aaf7dc7023 460 }
ansond 27:b8aaf7dc7023 461
ansond 27:b8aaf7dc7023 462 // push back the Firmware Resources Object
ansond 27:b8aaf7dc7023 463 if (this->m_options->getFirmwareResourcesObject() != NULL) {
ansond 27:b8aaf7dc7023 464 // DEBUG
ansond 54:dfee8691c83a 465 this->logger()->log("Connector::Endpoint::build(): plumbing firmware resources object...");
ansond 27:b8aaf7dc7023 466
ansond 27:b8aaf7dc7023 467 // push back the firmware resources object
ansond 46:62da4ce20276 468 this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getFirmwareResourcesObject());
ansond 27:b8aaf7dc7023 469 }
ansond 27:b8aaf7dc7023 470 else {
ansond 27:b8aaf7dc7023 471 // unable to plumb firmware manager
ansond 54:dfee8691c83a 472 this->logger()->log("Connector::Endpoint::build(): Unable to plumb firmware resources. Not installing firmware resource object...");
ansond 27:b8aaf7dc7023 473 }
ansond 27:b8aaf7dc7023 474 }
ansond 27:b8aaf7dc7023 475 else {
ansond 27:b8aaf7dc7023 476 // no device manager installed
ansond 54:dfee8691c83a 477 this->logger()->log("Connector::Endpoint::build(): No device manager installed.");
ansond 0:1f1f55e73248 478 }
ansond 0:1f1f55e73248 479
ansond 0:1f1f55e73248 480 // Loop through Static Resources and bind each of them...
ansond 54:dfee8691c83a 481 this->logger()->log("Connector::Endpoint::build(): adding static resources...");
ansond 0:1f1f55e73248 482 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
ansond 0:1f1f55e73248 483 for(int i=0; i<(int)static_resources->size(); ++i) {
ansond 54:dfee8691c83a 484 this->logger()->log("Connector::Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str());
ansond 27:b8aaf7dc7023 485 static_resources->at(i)->bind(this);
ansond 0:1f1f55e73248 486 }
ansond 0:1f1f55e73248 487
ansond 0:1f1f55e73248 488 // Loop through Dynamic Resources and bind each of them...
ansond 54:dfee8691c83a 489 this->logger()->log("Connector::Endpoint::build(): adding dynamic resources...");
ansond 0:1f1f55e73248 490 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 0:1f1f55e73248 491 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 54:dfee8691c83a 492 this->logger()->log("Connector::Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
ansond 27:b8aaf7dc7023 493 dynamic_resources->at(i)->bind(this);
ansond 27:b8aaf7dc7023 494 }
ansond 27:b8aaf7dc7023 495
ansond 27:b8aaf7dc7023 496 // Get the ObjectList from the ObjectInstanceManager...
ansond 27:b8aaf7dc7023 497 NamedPointerList list = this->getObjectInstanceManager()->getObjectList();
ansond 27:b8aaf7dc7023 498
ansond 27:b8aaf7dc7023 499 // DEBUG
ansond 45:db754b994deb 500 //this->logger()->log("Endpoint::build(): All Resources bound. Number of Objects in list: %d",list.size());
ansond 27:b8aaf7dc7023 501
ansond 27:b8aaf7dc7023 502 // add all of the object instances we have created...
ansond 38:bb6d2be4d54c 503 for(int i=0;i<(int)list.size();++i) {
ansond 27:b8aaf7dc7023 504 // DEBUG
ansond 45:db754b994deb 505 //this->logger()->log("Endpoint::build(): adding Object Instance with ObjID: %s...",list.at(i).name().c_str());
ansond 27:b8aaf7dc7023 506
ansond 27:b8aaf7dc7023 507 // push back the object instance...
ansond 46:62da4ce20276 508 this->m_endpoint_object_list.push_back((M2MObject *)(list.at(i).ptr()));
ansond 0:1f1f55e73248 509 }
ansond 0:1f1f55e73248 510 }
ansond 0:1f1f55e73248 511
ansond 54:dfee8691c83a 512 // stop underlying observation mechanisms
ansond 54:dfee8691c83a 513 void Endpoint::stopObservations() {
ansond 54:dfee8691c83a 514 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 54:dfee8691c83a 515 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 54:dfee8691c83a 516 if (dynamic_resources->at(i)->isObservable() == true) {
ansond 54:dfee8691c83a 517 ResourceObserver *observer = (ResourceObserver *)dynamic_resources->at(i)->getObserver();
ansond 54:dfee8691c83a 518 if (observer != NULL) {
ansond 54:dfee8691c83a 519 this->logger()->log("Connector::Endpoint::stopObservations(): stopping resource observer for: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
ansond 54:dfee8691c83a 520 observer->halt();
ansond 54:dfee8691c83a 521 }
ansond 54:dfee8691c83a 522 }
ansond 54:dfee8691c83a 523 }
ansond 54:dfee8691c83a 524 }
ansond 54:dfee8691c83a 525
ansond 13:9edad7677211 526 // underlying network is connected (SET)
ansond 13:9edad7677211 527 void Endpoint::isConnected(bool connected) {
ansond 13:9edad7677211 528 this->m_connected = connected;
ansond 13:9edad7677211 529 }
ansond 13:9edad7677211 530
ansond 13:9edad7677211 531 // underlying network is connected (GET)
ansond 13:9edad7677211 532 bool Endpoint::isConnected() {
ansond 13:9edad7677211 533 return this->m_connected;
ansond 13:9edad7677211 534 }
ansond 13:9edad7677211 535
ansond 15:c11dbe4d354c 536 // Registered with mDC/mDS
ansond 15:c11dbe4d354c 537 bool Endpoint::isRegistered() {
ansond 15:c11dbe4d354c 538 return this->m_registered;
ansond 15:c11dbe4d354c 539 }
ansond 15:c11dbe4d354c 540
ansond 43:3fb57c4fba34 541 // Set the ConnectionStatusInterface
ansond 27:b8aaf7dc7023 542 void Endpoint::setConnectionStatusInterfaceImpl(ConnectionStatusInterface *csi) {
ansond 43:3fb57c4fba34 543 this->m_csi = csi;
ansond 27:b8aaf7dc7023 544 }
ansond 27:b8aaf7dc7023 545
ansond 27:b8aaf7dc7023 546 // Set our ObjectInstanceManager
ansond 27:b8aaf7dc7023 547 void Endpoint::setObjectInstanceManager(ObjectInstanceManager *oim) {
ansond 27:b8aaf7dc7023 548 this->m_oim = oim;
ansond 27:b8aaf7dc7023 549 }
ansond 27:b8aaf7dc7023 550
ansond 27:b8aaf7dc7023 551 // Get our ObjectInstanceManager
ansond 27:b8aaf7dc7023 552 ObjectInstanceManager *Endpoint::getObjectInstanceManager() {
ansond 27:b8aaf7dc7023 553 return this->m_oim;
ansond 27:b8aaf7dc7023 554 }
ansond 27:b8aaf7dc7023 555
ansond 0:1f1f55e73248 556 // our logger
ansond 0:1f1f55e73248 557 Logger *Endpoint::logger()
ansond 0:1f1f55e73248 558 {
ansond 0:1f1f55e73248 559 return this->m_logger;
ansond 0:1f1f55e73248 560 }
ansond 0:1f1f55e73248 561
ansond 0:1f1f55e73248 562 } // namespace Connector