use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Fri Nov 04 16:29:17 2016 +0000
Revision:
71:5069a202e892
Parent:
62:e5882bd28210
Child:
72:6b1d37b5420a
updates for new version of mbed Connector

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