Doug Anson / mbedConnectorInterfaceWithDM

Fork of mbedConnectorInterfaceV3 by Doug Anson

Committer:
ansond
Date:
Wed Apr 26 04:55:19 2017 +0000
Revision:
113:2a54570db601
Parent:
112:b7e348a3363c
Child:
114:08ed5d15ae51
updates for R1.2

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 96:73a85768f235 35 // factory storage and configurator support (mbed Cloud R1.2+)
ansond 96:73a85768f235 36 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 99:7f563f0a6c3c 37 // trace configuration
ansond 97:b141b6b8b37f 38 #include "mbed-trace/mbed_trace.h"
ansond 99:7f563f0a6c3c 39
ansond 99:7f563f0a6c3c 40 // updater support
ansond 99:7f563f0a6c3c 41 #include "update_ui_example.h"
ansond 99:7f563f0a6c3c 42
ansond 99:7f563f0a6c3c 43 // factory flow support
ansond 96:73a85768f235 44 #include "mbed_factory_configurator_client.h"
ansond 96:73a85768f235 45 #endif
ansond 96:73a85768f235 46
ansond 1:16f0fb5b8d97 47 // our endpoint instance
ansond 1:16f0fb5b8d97 48 static Connector::Endpoint *__endpoint = NULL;
ansond 0:1f1f55e73248 49
ansond 54:dfee8691c83a 50 // LWIP Network interface instance
ansond 60:0d9e607dd678 51 NetworkInterface *__network_interface = NULL;
ansond 27:b8aaf7dc7023 52
ansond 0:1f1f55e73248 53 // Connector namespace
ansond 0:1f1f55e73248 54 namespace Connector {
ansond 0:1f1f55e73248 55
ansond 0:1f1f55e73248 56 // STATIC: Plumb the network
ansond 13:9edad7677211 57 void Endpoint::plumbNetwork(void *device_manager,bool canActAsRouterNode) {
ansond 54:dfee8691c83a 58 // create our endpoint instance...
ansond 1:16f0fb5b8d97 59 if (__endpoint == NULL) {
ansond 1:16f0fb5b8d97 60 // initialize our endpoint instance
ansond 54:dfee8691c83a 61 printf("Connector::Endpoint::plumbNetwork: initializing endpoint instance...\r\n");
ansond 1:16f0fb5b8d97 62 __endpoint = (Connector::Endpoint *)utils_init_endpoint(canActAsRouterNode);
ansond 1:16f0fb5b8d97 63 }
ansond 13:9edad7677211 64
ansond 110:0af085ececd2 65 // make sure we have an endpoint...
ansond 112:b7e348a3363c 66 if (__endpoint != NULL) {
ansond 110:0af085ececd2 67 // set the device manager
ansond 110:0af085ececd2 68 if (device_manager != NULL) {
ansond 110:0af085ececd2 69 // device manager has been supplied
ansond 110:0af085ececd2 70 printf("Connector::Endpoint::plumbNetwork: setting a device manager...\r\n");
ansond 110:0af085ececd2 71 __endpoint->setDeviceManager(device_manager);
ansond 110:0af085ececd2 72 }
ansond 110:0af085ececd2 73
ansond 110:0af085ececd2 74 // configure the endpoint...
ansond 110:0af085ececd2 75 printf("Connector::Endpoint::plumbNetwork: configuring endpoint...\r\n");
ansond 110:0af085ececd2 76 utils_configure_endpoint((void *)__endpoint);
ansond 110:0af085ececd2 77
ansond 110:0af085ececd2 78 // plumb the endpoint's network...
ansond 110:0af085ececd2 79 printf("Connector::Endpoint::plumbNetwork: plumbing network...\r\n");
ansond 110:0af085ececd2 80 net_plumb_network((void *)__endpoint);
ansond 13:9edad7677211 81 }
ansond 0:1f1f55e73248 82 }
ansond 0:1f1f55e73248 83
ansond 0:1f1f55e73248 84 // STATIC: Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
ansond 8:f950fb1b78c0 85 void Endpoint::start()
ansond 8:f950fb1b78c0 86 {
ansond 112:b7e348a3363c 87 if (__endpoint != NULL) {
ansond 110:0af085ececd2 88 // build out the endpoint with its configuration...
ansond 110:0af085ececd2 89 printf("Connector::Endpoint::start: building out endpoint...\r\n");
ansond 110:0af085ececd2 90 utils_build_endpoint((void *)__endpoint);
ansond 110:0af085ececd2 91
ansond 112:b7e348a3363c 92 // make sure we have an endpoint interface before continuing...
ansond 113:2a54570db601 93 if (__endpoint != NULL && __endpoint->getEndpointInterface() != NULL) {
ansond 112:b7e348a3363c 94 // finalize the endpoint and start its main loop
ansond 112:b7e348a3363c 95 printf("Endpoint::start: finalize and run the endpoint main loop..\r\n");
ansond 112:b7e348a3363c 96 net_finalize_and_run_endpoint_main_loop((void *)__endpoint);
ansond 112:b7e348a3363c 97 }
ansond 112:b7e348a3363c 98 else {
ansond 112:b7e348a3363c 99 // not starting the endpoint due to errors
ansond 112:b7e348a3363c 100 printf("Connector::Endpoint::start: Not starting endpoint due to errors (no endpoint interface)... exiting...\r\n");
ansond 112:b7e348a3363c 101
ansond 112:b7e348a3363c 102 // end in error...
ansond 112:b7e348a3363c 103 while(true) {
ansond 112:b7e348a3363c 104 Thread::wait(1000);
ansond 112:b7e348a3363c 105 }
ansond 112:b7e348a3363c 106 }
ansond 110:0af085ececd2 107 }
ansond 110:0af085ececd2 108 else {
ansond 110:0af085ececd2 109 // not starting the endpoint due to errors
ansond 112:b7e348a3363c 110 printf("Connector::Endpoint::start: Not starting endpoint due to errors (no endpoint)... exiting...\r\n");
ansond 110:0af085ececd2 111
ansond 110:0af085ececd2 112 // end in error...
ansond 110:0af085ececd2 113 while(true) {
ansond 110:0af085ececd2 114 Thread::wait(1000);
ansond 110:0af085ececd2 115 }
ansond 110:0af085ececd2 116 }
ansond 0:1f1f55e73248 117 }
ansond 0:1f1f55e73248 118
ansond 43:3fb57c4fba34 119 // STATIC: Set the ConnectionStatusInterface Implementation instance
ansond 43:3fb57c4fba34 120 void Endpoint::setConnectionStatusInterface(ConnectionStatusInterface *csi) {
ansond 43:3fb57c4fba34 121 if (__endpoint != NULL) {
ansond 44:7c73baf9f4c1 122 __endpoint->setConnectionStatusInterfaceImpl(csi);
ansond 43:3fb57c4fba34 123 }
ansond 43:3fb57c4fba34 124 }
ansond 43:3fb57c4fba34 125
ansond 0:1f1f55e73248 126 // Constructor
ansond 87:00b3837986c7 127 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 87:00b3837986c7 128 Endpoint::Endpoint(const Logger *logger, const Options *options) : MbedCloudClientCallback(), M2MInterfaceObserver()
ansond 87:00b3837986c7 129 #else
ansond 0:1f1f55e73248 130 Endpoint::Endpoint(const Logger *logger, const Options *options) : M2MInterfaceObserver()
ansond 87:00b3837986c7 131 #endif
ansond 0:1f1f55e73248 132 {
ansond 0:1f1f55e73248 133 this->m_logger = (Logger *)logger;
ansond 0:1f1f55e73248 134 this->m_options = (Options *)options;
ansond 13:9edad7677211 135 this->m_device_manager = NULL;
ansond 13:9edad7677211 136 this->m_connected = false;
ansond 15:c11dbe4d354c 137 this->m_registered = false;
ansond 27:b8aaf7dc7023 138 this->m_csi = NULL;
ansond 27:b8aaf7dc7023 139 this->m_oim = NULL;
ansond 81:a2441163a06e 140 this->m_endpoint_security = NULL;
ansond 74:6abfb2a03020 141 this->m_endpoint_interface = NULL;
ansond 0:1f1f55e73248 142 }
ansond 0:1f1f55e73248 143
ansond 0:1f1f55e73248 144 // Copy Constructor
ansond 0:1f1f55e73248 145 Endpoint::Endpoint(const Endpoint &ep)
ansond 0:1f1f55e73248 146 {
ansond 0:1f1f55e73248 147 this->m_logger = ep.m_logger;
ansond 0:1f1f55e73248 148 this->m_options = ep.m_options;
ansond 46:62da4ce20276 149 this->m_endpoint_interface = ep.m_endpoint_interface;
ansond 46:62da4ce20276 150 this->m_endpoint_security = ep.m_endpoint_security;
ansond 46:62da4ce20276 151 this->m_endpoint_object_list = ep.m_endpoint_object_list;
ansond 13:9edad7677211 152 this->m_device_manager = ep.m_device_manager;
ansond 13:9edad7677211 153 this->m_connected = ep.m_connected;
ansond 15:c11dbe4d354c 154 this->m_registered = ep.m_registered;
ansond 27:b8aaf7dc7023 155 this->m_csi = ep.m_csi;
ansond 27:b8aaf7dc7023 156 this->m_oim = ep.m_oim;
ansond 0:1f1f55e73248 157 }
ansond 0:1f1f55e73248 158
ansond 0:1f1f55e73248 159 // Destructor
ansond 0:1f1f55e73248 160 Endpoint::~Endpoint() {
ansond 75:9152ea6b4c59 161 #ifndef ENABLE_MBED_CLOUD_SUPPORT
ansond 46:62da4ce20276 162 if (this->m_endpoint_interface != NULL)
ansond 46:62da4ce20276 163 delete this->m_endpoint_interface;
ansond 73:f12a767bc300 164
ansond 46:62da4ce20276 165 if (this->m_endpoint_security != NULL)
ansond 46:62da4ce20276 166 delete this->m_endpoint_security;
ansond 73:f12a767bc300 167 #endif
ansond 0:1f1f55e73248 168 }
ansond 0:1f1f55e73248 169
ansond 13:9edad7677211 170 // set the device manager
ansond 13:9edad7677211 171 void Endpoint::setDeviceManager(void *device_manager) {
ansond 13:9edad7677211 172 this->m_device_manager = device_manager;
ansond 13:9edad7677211 173 }
ansond 13:9edad7677211 174
ansond 13:9edad7677211 175 // get the device manager
ansond 13:9edad7677211 176 void *Endpoint::getDeviceManager(void) {
ansond 13:9edad7677211 177 return this->m_device_manager;
ansond 13:9edad7677211 178 }
ansond 13:9edad7677211 179
ansond 1:16f0fb5b8d97 180 // router node behavior setting
ansond 1:16f0fb5b8d97 181 void Endpoint::asRouterNode(bool canActAsRouterNode) {
ansond 1:16f0fb5b8d97 182 this->m_canActAsRouterNode = canActAsRouterNode;
ansond 1:16f0fb5b8d97 183 }
ansond 1:16f0fb5b8d97 184
ansond 1:16f0fb5b8d97 185 // set our Options
ansond 1:16f0fb5b8d97 186 void Endpoint::setOptions(Options *options) {
ansond 1:16f0fb5b8d97 187 this->m_options = options;
ansond 1:16f0fb5b8d97 188 }
ansond 1:16f0fb5b8d97 189
ansond 0:1f1f55e73248 190 // get our Options
ansond 0:1f1f55e73248 191 Options *Endpoint::getOptions() {
ansond 0:1f1f55e73248 192 return this->m_options;
ansond 0:1f1f55e73248 193 }
ansond 0:1f1f55e73248 194
ansond 81:a2441163a06e 195 // get our endpoint security instance
ansond 81:a2441163a06e 196 M2MSecurity *Endpoint::getSecurityInstance() {
ansond 46:62da4ce20276 197 return this->m_endpoint_security;
ansond 0:1f1f55e73248 198 }
ansond 81:a2441163a06e 199
ansond 81:a2441163a06e 200 // set our endpoint security instance
ansond 81:a2441163a06e 201 void Endpoint::setSecurityInstance(M2MSecurity *security) {
ansond 81:a2441163a06e 202 if (security != NULL) {
ansond 81:a2441163a06e 203 this->m_endpoint_security = security;
ansond 81:a2441163a06e 204 }
ansond 81:a2441163a06e 205 }
ansond 0:1f1f55e73248 206
ansond 0:1f1f55e73248 207 // get our ObjectList
ansond 46:62da4ce20276 208 M2MObjectList Endpoint::getEndpointObjectList() {
ansond 46:62da4ce20276 209 return this->m_endpoint_object_list;
ansond 46:62da4ce20276 210 }
ansond 46:62da4ce20276 211
ansond 73:f12a767bc300 212 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 73:f12a767bc300 213 // get our endpoint interface
ansond 73:f12a767bc300 214 MbedCloudClient *Endpoint::getEndpointInterface() {
ansond 73:f12a767bc300 215 return this->m_endpoint_interface;
ansond 73:f12a767bc300 216 }
ansond 73:f12a767bc300 217 #else
ansond 46:62da4ce20276 218 // get our endpoint interface
ansond 46:62da4ce20276 219 M2MInterface *Endpoint::getEndpointInterface() {
ansond 46:62da4ce20276 220 return this->m_endpoint_interface;
ansond 0:1f1f55e73248 221 }
ansond 73:f12a767bc300 222 #endif
ansond 0:1f1f55e73248 223
ansond 71:5069a202e892 224 // Connector::Endpoint: create our interface
ansond 71:5069a202e892 225 void Endpoint::createEndpointInterface() {
ansond 71:5069a202e892 226 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 98:56e429670fdb 227 this->createCloudEndpointInterface();
ansond 71:5069a202e892 228 #else
ansond 72:6b1d37b5420a 229 this->createConnectorEndpointInterface();
ansond 71:5069a202e892 230 #endif
ansond 71:5069a202e892 231 }
ansond 71:5069a202e892 232
ansond 71:5069a202e892 233 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 96:73a85768f235 234 // mbedCloudClient: initialize Storage
ansond 96:73a85768f235 235 bool Endpoint::initializeStorage() {
ansond 106:8c504a89c1dc 236 #ifdef MBED_CLOUD_STORAGE_INIT
ansond 96:73a85768f235 237 // initialize mbed-trace
ansond 96:73a85768f235 238 mbed_trace_init();
ansond 96:73a85768f235 239
ansond 96:73a85768f235 240 // Resets storage to an empty state.
ansond 96:73a85768f235 241 // Use this function when you want to clear SD card from all the factory-tool generated data and user data.
ansond 96:73a85768f235 242 // After this operation device must be injected again by using factory tool or developer certificate.
ansond 110:0af085ececd2 243 #ifdef MBED_RESET_STORAGE
ansond 96:73a85768f235 244 this->logger()->log("initializeStorage: Resetting storage to an empty state...");
ansond 96:73a85768f235 245 mfcc_status_e delete_status = mfcc_storage_delete();
ansond 96:73a85768f235 246 if (delete_status != MFCC_STATUS_SUCCESS) {
ansond 96:73a85768f235 247 this->logger()->log("initializeStorage: Failed to reset storage to an empty state. status=%d (OK)...", delete_status);
ansond 96:73a85768f235 248 }
ansond 96:73a85768f235 249 #endif
ansond 96:73a85768f235 250 mfcc_status_e status = mfcc_init();
ansond 96:73a85768f235 251 if(status != MFCC_STATUS_SUCCESS) {
ansond 96:73a85768f235 252 this->logger()->log("initializeStorage: ERROR: mfcc_init failed with status=%d...", status);
ansond 96:73a85768f235 253 return false;
ansond 96:73a85768f235 254 }
ansond 106:8c504a89c1dc 255 #else
ansond 106:8c504a89c1dc 256 // not enabled
ansond 106:8c504a89c1dc 257 this->logger()->log("initializeStorage: storage initialize disabled (OK)...");
ansond 106:8c504a89c1dc 258 #endif
ansond 96:73a85768f235 259 return true;
ansond 96:73a85768f235 260 }
ansond 96:73a85768f235 261
ansond 96:73a85768f235 262 // mbedCloudClient:: initialize factory flow
ansond 96:73a85768f235 263 bool Endpoint::initializeFactoryFlow() {
ansond 106:8c504a89c1dc 264 #ifdef MBED_CLOUD_DEV_FLOW_INIT
ansond 96:73a85768f235 265 #ifdef MBED_CONF_APP_DEVELOPER_MODE
ansond 96:73a85768f235 266 this->logger()->log("initializeFactoryFlow: Start developer flow...");
ansond 97:b141b6b8b37f 267 mfcc_status_e status = mfcc_developer_flow();
ansond 96:73a85768f235 268 if (status == MFCC_STATUS_KCM_FILE_EXIST_ERROR) {
ansond 96:73a85768f235 269 this->logger()->log("initializeFactoryFlow: Developer credentials already exists (OK)...");
ansond 96:73a85768f235 270 } else if (status != MFCC_STATUS_SUCCESS) {
ansond 96:73a85768f235 271 this->logger()->log("initializeFactoryFlow: ERROR: Failed to load developer credentials");
ansond 96:73a85768f235 272 return false;
ansond 96:73a85768f235 273 }
ansond 96:73a85768f235 274 status = mfcc_verify_device_configured_4mbed_cloud();
ansond 96:73a85768f235 275 if (status != MFCC_STATUS_SUCCESS) {
ansond 96:73a85768f235 276 this->logger()->log("initializeFactoryFlow: ERROR: Device not configured for mbed Cloud");
ansond 96:73a85768f235 277 return false;
ansond 96:73a85768f235 278 }
ansond 96:73a85768f235 279 return true;
ansond 96:73a85768f235 280 #else
ansond 96:73a85768f235 281 this->logger()->log("initializeFactoryFlow: non-developer factory flow chosen... continuing...");
ansond 96:73a85768f235 282 return true;
ansond 96:73a85768f235 283 #endif
ansond 106:8c504a89c1dc 284 #else
ansond 106:8c504a89c1dc 285 this->logger()->log("initializeFactoryFlow: developer flow init disabled (OK)...");
ansond 106:8c504a89c1dc 286 return true;
ansond 106:8c504a89c1dc 287 #endif
ansond 96:73a85768f235 288 }
ansond 96:73a85768f235 289
ansond 71:5069a202e892 290 // mbedCloudClient: create our interface
ansond 71:5069a202e892 291 void Endpoint::createCloudEndpointInterface() {
ansond 74:6abfb2a03020 292 if (this->m_endpoint_interface == NULL) {
ansond 109:e3fc9140cbbf 293 bool storage_init = this->initializeStorage();
ansond 109:e3fc9140cbbf 294 bool factory_flow_init = this->initializeFactoryFlow();
ansond 109:e3fc9140cbbf 295 if (storage_init && factory_flow_init) {
ansond 105:aeaaee8fbb1d 296 // create a new instance of mbedCloudClient
ansond 109:e3fc9140cbbf 297 this->logger()->log("createCloudEndpointInterface: creating mbed cloud client instance...");
ansond 105:aeaaee8fbb1d 298 this->m_endpoint_interface = new MbedCloudClient();
ansond 105:aeaaee8fbb1d 299 if (this->m_endpoint_interface == NULL) {
ansond 105:aeaaee8fbb1d 300 // unable to allocate the MbedCloudClient instance
ansond 105:aeaaee8fbb1d 301 this->logger()->log("createCloudEndpointInterface: ERROR: unable to allocate MbedCloudClient instance...");
ansond 105:aeaaee8fbb1d 302 }
ansond 105:aeaaee8fbb1d 303 else {
ansond 105:aeaaee8fbb1d 304 // enable hooks for Updater support (R1.2+) (if enabled)
ansond 105:aeaaee8fbb1d 305 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
ansond 105:aeaaee8fbb1d 306 update_ui_set_cloud_client(this->m_endpoint_interface);
ansond 105:aeaaee8fbb1d 307 this->m_endpoint_interface->set_update_authorize_handler(&Connector::Endpoint::update_authorize);
ansond 105:aeaaee8fbb1d 308 this->m_endpoint_interface->set_update_progress_handler(&Connector::Endpoint::update_progress);
ansond 105:aeaaee8fbb1d 309 #endif
ansond 105:aeaaee8fbb1d 310 }
ansond 98:56e429670fdb 311 }
ansond 109:e3fc9140cbbf 312 else {
ansond 109:e3fc9140cbbf 313 if (storage_init) {
ansond 109:e3fc9140cbbf 314 // unable to create mbed cloud client instance... (FAILED factory flow init)
ansond 109:e3fc9140cbbf 315 this->logger()->log("createCloudEndpointInterface: ERROR: unable to initialize factory flow...");
ansond 109:e3fc9140cbbf 316 }
ansond 109:e3fc9140cbbf 317 else {
ansond 109:e3fc9140cbbf 318 // unable to create mbed cloud client instance... (FAILED storage init)
ansond 109:e3fc9140cbbf 319 this->logger()->log("createCloudEndpointInterface: ERROR: unable to initialize storage...");
ansond 109:e3fc9140cbbf 320 }
ansond 109:e3fc9140cbbf 321 this->m_endpoint_interface = NULL;
ansond 109:e3fc9140cbbf 322 }
ansond 74:6abfb2a03020 323 }
ansond 74:6abfb2a03020 324
ansond 71:5069a202e892 325 // bind LWIP network interface pointer...
ansond 71:5069a202e892 326 if (__network_interface != NULL && this->m_endpoint_interface != NULL) {
ansond 71:5069a202e892 327 this->logger()->log("Connector::Endpoint: binding LWIP network instance (Cloud)...");
ansond 81:a2441163a06e 328 this->m_endpoint_interface->on_registered(&Connector::Endpoint::on_registered);
ansond 81:a2441163a06e 329 this->m_endpoint_interface->on_unregistered(&Connector::Endpoint::on_unregistered);
ansond 81:a2441163a06e 330 this->m_endpoint_interface->on_error(&Connector::Endpoint::on_error);
ansond 81:a2441163a06e 331 this->m_endpoint_interface->set_update_callback(this);
ansond 71:5069a202e892 332 }
ansond 109:e3fc9140cbbf 333 else {
ansond 109:e3fc9140cbbf 334 // skipping LWIP bind...
ansond 109:e3fc9140cbbf 335 this->logger()->log("Connector::Endpoint: ERROR (Cloud) skipping LWIP network instance bind due to previous error...");
ansond 109:e3fc9140cbbf 336 }
ansond 71:5069a202e892 337 }
ansond 81:a2441163a06e 338 #else
ansond 0:1f1f55e73248 339 // mbed-client: create our interface
ansond 71:5069a202e892 340 void Endpoint::createConnectorEndpointInterface() {
ansond 10:3f79b5e67c22 341 // get the CoAP listening port
ansond 10:3f79b5e67c22 342 uint16_t listening_port = (uint16_t)this->m_options->getConnectorPort();
ansond 10:3f79b5e67c22 343
ansond 10:3f79b5e67c22 344 // randomize the port if we are using certificates...
ansond 10:3f79b5e67c22 345 if (this->m_options->getServerCertificateSize() > 0) {
ansond 0:1f1f55e73248 346 // Randomizing listening port for Certificate mode connectivity
ansond 0:1f1f55e73248 347 srand(time(NULL));
ansond 10:3f79b5e67c22 348 listening_port = rand() % 65535 + 12345;
ansond 10:3f79b5e67c22 349 }
ansond 10:3f79b5e67c22 350
ansond 60:0d9e607dd678 351 // DEBUG
ansond 61:d02cd5e2bb26 352 //this->logger()->log("Connector::Endpoint: listening port: %d",listening_port);
ansond 60:0d9e607dd678 353
ansond 61:d02cd5e2bb26 354 // Socket protocol type: TCP or UDP
ansond 61:d02cd5e2bb26 355 M2MInterface::BindingMode socket_protocol_type = M2MInterface::UDP;
ansond 61:d02cd5e2bb26 356 if (this->m_options->getCoAPConnectionType() == COAP_TCP) socket_protocol_type = M2MInterface::TCP;
ansond 10:3f79b5e67c22 357
ansond 61:d02cd5e2bb26 358 // Socket address type: IPv4 or IPv6
ansond 61:d02cd5e2bb26 359 M2MInterface::NetworkStack socket_address_type = M2MInterface::LwIP_IPv4;
ansond 62:e5882bd28210 360 if (this->m_options->getIPAddressType() == IP_ADDRESS_TYPE_IPV6) {
ansond 61:d02cd5e2bb26 361 // IPv6 mode for the socket addressing type...
ansond 61:d02cd5e2bb26 362 socket_address_type = M2MInterface::LwIP_IPv6;
ansond 59:dd395412bd19 363
ansond 59:dd395412bd19 364 #if defined (IPV4_OVERRIDE)
ansond 61:d02cd5e2bb26 365 // OVERRIDE (until patched...)
ansond 61:d02cd5e2bb26 366 this->logger()->log("Connector::Endpoint: Socket Address Type: IPv4 (IPv6 OVERRIDE)");
ansond 61:d02cd5e2bb26 367 socket_address_type = M2MInterface::LwIP_IPv4;
ansond 59:dd395412bd19 368 #endif
ansond 59:dd395412bd19 369 }
ansond 10:3f79b5e67c22 370
ansond 10:3f79b5e67c22 371 // DEBUG
ansond 61:d02cd5e2bb26 372 if (socket_protocol_type == M2MInterface::TCP) this->logger()->log("Connector::Endpoint: Socket Protocol: TCP");
ansond 61:d02cd5e2bb26 373 if (socket_protocol_type == M2MInterface::UDP) this->logger()->log("Connector::Endpoint: Socket Protocol: UDP");
ansond 61:d02cd5e2bb26 374 if (socket_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Connector::Endpoint: Socket Address Type: IPv4");
ansond 61:d02cd5e2bb26 375 if (socket_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Connector::Endpoint: Socket Address Type: IPv6");
ansond 10:3f79b5e67c22 376
ansond 61:d02cd5e2bb26 377 // Create the endpoint M2MInterface instance
ansond 46:62da4ce20276 378 this->m_endpoint_interface = M2MInterfaceFactory::create_interface(*this,
ansond 61:d02cd5e2bb26 379 (char *)this->m_options->getEndpointNodename().c_str(), // endpoint name
ansond 61:d02cd5e2bb26 380 (char *)this->m_options->getEndpointType().c_str(), // endpoint type
ansond 61:d02cd5e2bb26 381 (int32_t)this->m_options->getLifetime(), // registration lifetime (in seconds)
ansond 61:d02cd5e2bb26 382 listening_port, // listening port (ephemeral...)
ansond 61:d02cd5e2bb26 383 (char *)this->m_options->getDomain().c_str(), // endpoint domain
ansond 61:d02cd5e2bb26 384 socket_protocol_type, // Socket protocol type: UDP or TCP...
ansond 61:d02cd5e2bb26 385 socket_address_type, // Socket addressing type: IPv4 or IPv6
ansond 61:d02cd5e2bb26 386 CONTEXT_ADDRESS_STRING // context address string (mbedConnectorInterface.h)
ansond 60:0d9e607dd678 387 );
ansond 61:d02cd5e2bb26 388
ansond 61:d02cd5e2bb26 389 // bind LWIP network interface pointer...
ansond 46:62da4ce20276 390 if (__network_interface != NULL && this->m_endpoint_interface != NULL) {
ansond 71:5069a202e892 391 this->logger()->log("Connector::Endpoint: binding LWIP network instance (Connector)...");
ansond 60:0d9e607dd678 392 this->m_endpoint_interface->set_platform_network_handler((void *)__network_interface);
ansond 27:b8aaf7dc7023 393 }
ansond 0:1f1f55e73248 394 }
ansond 81:a2441163a06e 395 #endif
ansond 0:1f1f55e73248 396
ansond 81:a2441163a06e 397 // mbed-client: createEndpointSecurityInstance()
ansond 81:a2441163a06e 398 M2MSecurity *Endpoint::createEndpointSecurityInstance() {
ansond 81:a2441163a06e 399 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 81:a2441163a06e 400 // internalized... not used.
ansond 81:a2441163a06e 401 return NULL;
ansond 81:a2441163a06e 402 #else
ansond 0:1f1f55e73248 403 // Creates register server object with mbed device server address and other parameters
ansond 0:1f1f55e73248 404 M2MSecurity *server = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
ansond 0:1f1f55e73248 405 if (server != NULL) {
ansond 0:1f1f55e73248 406 const String url = this->m_options->getConnectorURL();
ansond 0:1f1f55e73248 407 server->set_resource_value(M2MSecurity::M2MServerUri, url);
ansond 38:bb6d2be4d54c 408 server->set_resource_value(M2MSecurity::BootstrapServer, false);
ansond 0:1f1f55e73248 409 server->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
ansond 0:1f1f55e73248 410 server->set_resource_value(M2MSecurity::ServerPublicKey,this->m_options->getServerCertificate(),this->m_options->getServerCertificateSize());
ansond 0:1f1f55e73248 411 server->set_resource_value(M2MSecurity::PublicKey,this->m_options->getClientCertificate(),this->m_options->getClientCertificateSize());
ansond 0:1f1f55e73248 412 server->set_resource_value(M2MSecurity::Secretkey,this->m_options->getClientKey(),this->m_options->getClientKeySize());
ansond 0:1f1f55e73248 413 }
ansond 0:1f1f55e73248 414 return server;
ansond 81:a2441163a06e 415 #endif
ansond 0:1f1f55e73248 416 }
ansond 0:1f1f55e73248 417
ansond 81:a2441163a06e 418 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 81:a2441163a06e 419 // mbed-cloud-client: Callback from mbed client stack if any error is encountered
ansond 82:e085176f6719 420 void Endpoint::on_error(int error_code) {
ansond 85:ad357f0a48d2 421 char *error = (char *)"No Error";
ansond 81:a2441163a06e 422 switch(error_code) {
ansond 81:a2441163a06e 423 case 0x01:
ansond 84:e5f53e7492cb 424 error = (char *)"MbedCloudClient::IdentityError";
ansond 81:a2441163a06e 425 break;
ansond 81:a2441163a06e 426 case 0x02:
ansond 84:e5f53e7492cb 427 error = (char *)"MbedCloudClient::IdentityInvalidParameter";
ansond 81:a2441163a06e 428 break;
ansond 81:a2441163a06e 429 case 0x03:
ansond 84:e5f53e7492cb 430 error = (char *)"MbedCloudClient::IdentityOutofMemory";
ansond 81:a2441163a06e 431 break;
ansond 81:a2441163a06e 432 case 0x04:
ansond 84:e5f53e7492cb 433 error = (char *)"MbedCloudClient::IdentityProvisioningError";
ansond 81:a2441163a06e 434 break;
ansond 81:a2441163a06e 435 case 0x05:
ansond 84:e5f53e7492cb 436 error = (char *)"MbedCloudClient::IdentityInvalidSessionID";
ansond 81:a2441163a06e 437 break;
ansond 81:a2441163a06e 438 case 0x06:
ansond 84:e5f53e7492cb 439 error = (char *)"MbedCloudClient::IdentityNetworkError";
ansond 81:a2441163a06e 440 break;
ansond 81:a2441163a06e 441 case 0x07:
ansond 84:e5f53e7492cb 442 error = (char *)"MbedCloudClient::IdentityInvalidMessageType";
ansond 81:a2441163a06e 443 break;
ansond 81:a2441163a06e 444 case 0x08:
ansond 84:e5f53e7492cb 445 error = (char *)"MbedCloudClient::IdentityInvalidMessageSize";
ansond 81:a2441163a06e 446 break;
ansond 81:a2441163a06e 447 case 0x09:
ansond 84:e5f53e7492cb 448 error = (char *)"MbedCloudClient::IdentityCertOrKeyNotFound";
ansond 81:a2441163a06e 449 break;
ansond 81:a2441163a06e 450 case 0x0A:
ansond 84:e5f53e7492cb 451 error = (char *)"MbedCloudClient::IdentityRetransmissionError";
ansond 81:a2441163a06e 452 break;
ansond 81:a2441163a06e 453 case 0x30:
ansond 84:e5f53e7492cb 454 error = (char *)"MbedCloudClient::ConnectErrorNone";
ansond 81:a2441163a06e 455 break;
ansond 81:a2441163a06e 456 case 0x31:
ansond 84:e5f53e7492cb 457 error = (char *)"MbedCloudClient::ConnectAlreadyExists";
ansond 81:a2441163a06e 458 break;
ansond 81:a2441163a06e 459 case 0x32:
ansond 84:e5f53e7492cb 460 error = (char *)"MbedCloudClient::ConnectBootstrapFailed";
ansond 81:a2441163a06e 461 break;
ansond 81:a2441163a06e 462 case 0x33:
ansond 84:e5f53e7492cb 463 error = (char *)"MbedCloudClient::ConnectInvalidParameters";
ansond 81:a2441163a06e 464 break;
ansond 81:a2441163a06e 465 case 0x34:
ansond 84:e5f53e7492cb 466 error = (char *)"MbedCloudClient::ConnectNotRegistered";
ansond 81:a2441163a06e 467 break;
ansond 81:a2441163a06e 468 case 0x35:
ansond 84:e5f53e7492cb 469 error = (char *)"MbedCloudClient::ConnectTimeout";
ansond 81:a2441163a06e 470 break;
ansond 81:a2441163a06e 471 case 0x36:
ansond 84:e5f53e7492cb 472 error = (char *)"MbedCloudClient::ConnectNetworkError";
ansond 81:a2441163a06e 473 break;
ansond 81:a2441163a06e 474 case 0x37:
ansond 84:e5f53e7492cb 475 error = (char *)"MbedCloudClient::ConnectResponseParseFailed";
ansond 81:a2441163a06e 476 break;
ansond 81:a2441163a06e 477 case 0x38:
ansond 84:e5f53e7492cb 478 error = (char *)"MbedCloudClient::ConnectUnknownError";
ansond 81:a2441163a06e 479 break;
ansond 81:a2441163a06e 480 case 0x39:
ansond 84:e5f53e7492cb 481 error = (char *)"MbedCloudClient::ConnectMemoryFail";
ansond 81:a2441163a06e 482 break;
ansond 81:a2441163a06e 483 case 0x3A:
ansond 84:e5f53e7492cb 484 error = (char *)"MbedCloudClient::ConnectNotAllowed";
ansond 81:a2441163a06e 485 break;
ansond 81:a2441163a06e 486 case 0x3B:
ansond 84:e5f53e7492cb 487 error = (char *)"MbedCloudClient::ConnectSecureConnectionFailed";
ansond 81:a2441163a06e 488 break;
ansond 81:a2441163a06e 489 case 0x3C:
ansond 84:e5f53e7492cb 490 error = (char *)"MbedCloudClient::ConnectDnsResolvingFailed";
ansond 81:a2441163a06e 491 break;
ansond 81:a2441163a06e 492 default:
ansond 84:e5f53e7492cb 493 error = (char *)"UNKNOWN";
ansond 81:a2441163a06e 494 }
ansond 85:ad357f0a48d2 495 printf("Connector::Endpoint(Cloud) Error(%x): %s\r\n",error_code,error);
ansond 81:a2441163a06e 496 }
ansond 98:56e429670fdb 497
ansond 98:56e429670fdb 498 // mbed-cloud-client: update_authorized
ansond 98:56e429670fdb 499 void Endpoint::update_authorize(int32_t request) {
ansond 98:56e429670fdb 500 // simple debug for now...
ansond 101:305cc6527e20 501 printf("Connector::Endpoint(Cloud) Update Authorize: request: %d\n",(int)request);
ansond 98:56e429670fdb 502 }
ansond 98:56e429670fdb 503
ansond 98:56e429670fdb 504 // mbed-cloud-client: update_progress
ansond 98:56e429670fdb 505 void Endpoint::update_progress(uint32_t progress, uint32_t total) {
ansond 98:56e429670fdb 506 // simple debug for now...
ansond 101:305cc6527e20 507 printf("Connector::Endpoint(Cloud) Update Progress: (%d/%d)\n",(int)progress,(int)total);
ansond 98:56e429670fdb 508 }
ansond 89:ccd22d25f431 509 #endif
ansond 89:ccd22d25f431 510
ansond 0:1f1f55e73248 511 // mbed-client: Callback from mbed client stack if any error is encountered
ansond 0:1f1f55e73248 512 void Endpoint::error(M2MInterface::Error error) {
ansond 0:1f1f55e73248 513 switch(error){
ansond 0:1f1f55e73248 514 case M2MInterface::AlreadyExists:
ansond 54:dfee8691c83a 515 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::AlreadyExists");
ansond 0:1f1f55e73248 516 break;
ansond 0:1f1f55e73248 517 case M2MInterface::BootstrapFailed:
ansond 54:dfee8691c83a 518 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::BootstrapFailed");
ansond 0:1f1f55e73248 519 break;
ansond 0:1f1f55e73248 520 case M2MInterface::InvalidParameters:
ansond 54:dfee8691c83a 521 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::InvalidParameters");
ansond 0:1f1f55e73248 522 break;
ansond 0:1f1f55e73248 523 case M2MInterface::NotRegistered:
ansond 54:dfee8691c83a 524 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotRegistered");
ansond 0:1f1f55e73248 525 break;
ansond 0:1f1f55e73248 526 case M2MInterface::Timeout:
ansond 54:dfee8691c83a 527 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::Timeout");
ansond 0:1f1f55e73248 528 break;
ansond 0:1f1f55e73248 529 case M2MInterface::NetworkError:
ansond 54:dfee8691c83a 530 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NetworkError");
ansond 0:1f1f55e73248 531 break;
ansond 0:1f1f55e73248 532 case M2MInterface::ResponseParseFailed:
ansond 54:dfee8691c83a 533 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::ResponseParseFailed");
ansond 0:1f1f55e73248 534 break;
ansond 0:1f1f55e73248 535 case M2MInterface::UnknownError:
ansond 54:dfee8691c83a 536 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::UnknownError");
ansond 0:1f1f55e73248 537 break;
ansond 0:1f1f55e73248 538 case M2MInterface::MemoryFail:
ansond 54:dfee8691c83a 539 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::MemoryFail");
ansond 0:1f1f55e73248 540 break;
ansond 0:1f1f55e73248 541 case M2MInterface::NotAllowed:
ansond 54:dfee8691c83a 542 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotAllowed");
ansond 0:1f1f55e73248 543 break;
ansond 0:1f1f55e73248 544 default:
ansond 0:1f1f55e73248 545 break;
ansond 0:1f1f55e73248 546 }
ansond 0:1f1f55e73248 547 }
ansond 81:a2441163a06e 548
ansond 76:7f55e1c0635d 549 // re-register the endpoint
ansond 76:7f55e1c0635d 550 void Endpoint::re_register_endpoint() {
ansond 76:7f55e1c0635d 551 if (this->m_endpoint_interface != NULL) {
ansond 76:7f55e1c0635d 552 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 81:a2441163a06e 553 // DEBUG
ansond 81:a2441163a06e 554 this->logger()->log("Connector::Endpoint(Cloud): re-register endpoint...");
ansond 76:7f55e1c0635d 555 #else
ansond 76:7f55e1c0635d 556 this->m_endpoint_interface->update_registration(this->m_endpoint_security,this->m_options->getLifetime());
ansond 76:7f55e1c0635d 557 #endif
ansond 76:7f55e1c0635d 558 }
ansond 76:7f55e1c0635d 559 }
ansond 76:7f55e1c0635d 560
ansond 76:7f55e1c0635d 561 // de-register endpoint
ansond 76:7f55e1c0635d 562 void Endpoint::de_register_endpoint(void) {
ansond 76:7f55e1c0635d 563 if (this->m_endpoint_interface != NULL) {
ansond 76:7f55e1c0635d 564 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 81:a2441163a06e 565 // DEBUG
ansond 81:a2441163a06e 566 this->logger()->log("Connector::Endpoint(Cloud): de-registering endpoint...");
ansond 76:7f55e1c0635d 567 this->m_endpoint_interface->close();
ansond 76:7f55e1c0635d 568 #else
ansond 76:7f55e1c0635d 569 // de-register endpoint
ansond 76:7f55e1c0635d 570 this->logger()->log("Connector::Endpoint: de-registering endpoint...");
ansond 76:7f55e1c0635d 571 if (this->m_csi != NULL) {
ansond 76:7f55e1c0635d 572 this->m_csi->begin_object_unregistering((void *)this);
ansond 76:7f55e1c0635d 573 }
ansond 76:7f55e1c0635d 574 else {
ansond 76:7f55e1c0635d 575 this->m_endpoint_interface->unregister_object(NULL);
ansond 76:7f55e1c0635d 576 }
ansond 76:7f55e1c0635d 577 #endif
ansond 76:7f55e1c0635d 578 }
ansond 76:7f55e1c0635d 579 }
ansond 76:7f55e1c0635d 580
ansond 77:cee832ba6dd0 581 // register the endpoint
ansond 77:cee832ba6dd0 582 void Endpoint::register_endpoint(M2MSecurity *endpoint_security, M2MObjectList endpoint_objects) {
ansond 76:7f55e1c0635d 583 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 76:7f55e1c0635d 584 if (this->m_endpoint_interface != NULL) {
ansond 90:cff3317ad4b0 585 this->logger()->log("Connector::Endpoint(Cloud): adding objects to endpoint...");
ansond 90:cff3317ad4b0 586 this->m_endpoint_interface->add_objects(endpoint_objects);
ansond 90:cff3317ad4b0 587
ansond 81:a2441163a06e 588 this->logger()->log("Connector::Endpoint(Cloud): registering endpoint...");
ansond 90:cff3317ad4b0 589 this->m_endpoint_interface->setup(__network_interface);
ansond 76:7f55e1c0635d 590 }
ansond 76:7f55e1c0635d 591 #else
ansond 46:62da4ce20276 592 if (this->m_endpoint_interface != NULL && endpoint_security != NULL && endpoint_objects.size() > 0) {
ansond 23:5852c0884714 593 // register endpoint
ansond 54:dfee8691c83a 594 this->logger()->log("Connector::Endpoint: registering endpoint...");
ansond 46:62da4ce20276 595 this->m_endpoint_interface->register_object(endpoint_security, endpoint_objects);
ansond 23:5852c0884714 596 }
ansond 77:cee832ba6dd0 597 #endif
ansond 0:1f1f55e73248 598 }
ansond 0:1f1f55e73248 599
ansond 81:a2441163a06e 600 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 81:a2441163a06e 601 // object registered
ansond 83:8d856fa24fda 602 void Endpoint::on_registered() {
ansond 83:8d856fa24fda 603 if (__endpoint != NULL) {
ansond 87:00b3837986c7 604 printf("Connector::Endpoint(Cloud): on_registered()\r\n");
ansond 87:00b3837986c7 605 __endpoint->object_registered();
ansond 81:a2441163a06e 606 }
ansond 81:a2441163a06e 607 }
ansond 81:a2441163a06e 608
ansond 81:a2441163a06e 609 // registration updated
ansond 82:e085176f6719 610 void Endpoint::on_registration_updated() {
ansond 83:8d856fa24fda 611 if (__endpoint != NULL) {
ansond 87:00b3837986c7 612 printf("Connector::Endpoint(Cloud): on_registration_updated()\r\n");
ansond 87:00b3837986c7 613 __endpoint->registration_updated();
ansond 81:a2441163a06e 614 }
ansond 81:a2441163a06e 615 }
ansond 81:a2441163a06e 616
ansond 81:a2441163a06e 617 // object unregistered
ansond 82:e085176f6719 618 void Endpoint::on_unregistered() {
ansond 83:8d856fa24fda 619 if (__endpoint != NULL) {
ansond 87:00b3837986c7 620 printf("Connector::Endpoint(Cloud): on_unregistered()\r\n");
ansond 88:e78093f6334f 621 __endpoint->object_unregistered(__endpoint->getSecurityInstance());
ansond 81:a2441163a06e 622 }
ansond 81:a2441163a06e 623 }
ansond 82:e085176f6719 624 #endif
ansond 81:a2441163a06e 625
ansond 8:f950fb1b78c0 626 // object registered
ansond 87:00b3837986c7 627 void Endpoint::object_registered(M2MSecurity *security, const M2MServer &server) {
ansond 88:e78093f6334f 628 this->object_registered((void *)security,(void *)&server);
ansond 8:f950fb1b78c0 629 }
ansond 8:f950fb1b78c0 630
ansond 8:f950fb1b78c0 631 // registration updated
ansond 87:00b3837986c7 632 void Endpoint::registration_updated(M2MSecurity *security, const M2MServer &server) {
ansond 88:e78093f6334f 633 this->registration_updated((void *)security,(void *)&server);
ansond 8:f950fb1b78c0 634 }
ansond 8:f950fb1b78c0 635
ansond 8:f950fb1b78c0 636 // object unregistered
ansond 87:00b3837986c7 637 void Endpoint::object_unregistered(M2MSecurity *security) {
ansond 54:dfee8691c83a 638 // DEBUG
ansond 54:dfee8691c83a 639 this->logger()->log("Connector::Endpoint: endpoint de-registered.");
ansond 54:dfee8691c83a 640
ansond 54:dfee8691c83a 641 // no longer connected/registered
ansond 15:c11dbe4d354c 642 this->m_registered = false;
ansond 54:dfee8691c83a 643 this->m_connected = false;
ansond 54:dfee8691c83a 644
ansond 54:dfee8691c83a 645 // stop all observers...
ansond 54:dfee8691c83a 646 this->stopObservations();
ansond 54:dfee8691c83a 647
ansond 54:dfee8691c83a 648 // invoke ConnectionHandler if we have one...
ansond 27:b8aaf7dc7023 649 if (this->m_csi != NULL) {
ansond 87:00b3837986c7 650 this->m_csi->object_unregistered((void *)this,(void *)security);
ansond 27:b8aaf7dc7023 651 }
ansond 54:dfee8691c83a 652
ansond 54:dfee8691c83a 653 // halt the main event loop... we are done.
ansond 54:dfee8691c83a 654 net_shutdown_endpoint();
ansond 0:1f1f55e73248 655 }
ansond 0:1f1f55e73248 656
ansond 0:1f1f55e73248 657 // bootstrap done
ansond 87:00b3837986c7 658 void Endpoint::bootstrap_done(M2MSecurity *security) {
ansond 54:dfee8691c83a 659 this->logger()->log("Connector::Endpoint: endpoint bootstrapped.");
ansond 27:b8aaf7dc7023 660 if (this->m_csi != NULL) {
ansond 87:00b3837986c7 661 this->m_csi->bootstrapped((void *)this,(void *)security);
ansond 87:00b3837986c7 662 }
ansond 87:00b3837986c7 663 }
ansond 87:00b3837986c7 664
ansond 87:00b3837986c7 665 // object registered
ansond 87:00b3837986c7 666 void Endpoint::object_registered(void *security,void *server) {
ansond 87:00b3837986c7 667 this->logger()->log("Connector::Endpoint: endpoint registered.");
ansond 87:00b3837986c7 668 this->m_connected = true;
ansond 87:00b3837986c7 669 this->m_registered = true;
ansond 87:00b3837986c7 670 if (this->m_csi != NULL) {
ansond 87:00b3837986c7 671 this->m_csi->object_registered((void *)this,security,server);
ansond 87:00b3837986c7 672 }
ansond 87:00b3837986c7 673 }
ansond 87:00b3837986c7 674
ansond 87:00b3837986c7 675 // registration updated
ansond 87:00b3837986c7 676 void Endpoint::registration_updated(void *security,void *server) {
ansond 87:00b3837986c7 677 this->logger()->log("Connector::Endpoint: endpoint re-registered.");
ansond 87:00b3837986c7 678 this->m_connected = true;
ansond 87:00b3837986c7 679 this->m_registered = true;
ansond 87:00b3837986c7 680 if (this->m_csi != NULL) {
ansond 87:00b3837986c7 681 this->m_csi->registration_updated((void *)this,security,server);
ansond 27:b8aaf7dc7023 682 }
ansond 0:1f1f55e73248 683 }
ansond 0:1f1f55e73248 684
ansond 0:1f1f55e73248 685 // resource value updated
ansond 0:1f1f55e73248 686 void Endpoint::value_updated(M2MBase *base, M2MBase::BaseType type) {
ansond 29:be035befb437 687 // Lookup the resource and invoke process() on it...
ansond 0:1f1f55e73248 688 DynamicResource *target_res = this->lookupDynamicResource(base);
ansond 29:be035befb437 689 if (target_res != NULL) {
ansond 29:be035befb437 690 // DEBUG
ansond 43:3fb57c4fba34 691 //this->logger()->log("Value Updated (Custom Resource)");
ansond 29:be035befb437 692
ansond 29:be035befb437 693 // its a custom resource...
ansond 29:be035befb437 694 target_res->process(base->operation(),type);
ansond 29:be035befb437 695 }
ansond 29:be035befb437 696 else {
ansond 29:be035befb437 697 // DEBUG
ansond 43:3fb57c4fba34 698 //this->logger()->log("Value Updated (Device Manager)");
ansond 29:be035befb437 699
ansond 29:be035befb437 700 // let DeviceManager handle it
ansond 29:be035befb437 701 ((DeviceManager *)this->m_device_manager)->process(base,type);
ansond 29:be035befb437 702 }
ansond 29:be035befb437 703
ansond 29:be035befb437 704 // CSI
ansond 27:b8aaf7dc7023 705 if (this->m_csi != NULL) {
ansond 27:b8aaf7dc7023 706 this->m_csi->value_updated((void *)this,(void *)base,(int)type);
ansond 27:b8aaf7dc7023 707 }
ansond 0:1f1f55e73248 708 }
ansond 0:1f1f55e73248 709
ansond 0:1f1f55e73248 710 // lookup which DynamicResource cooresponds to a given M2MBase instance...
ansond 0:1f1f55e73248 711 DynamicResource *Endpoint::lookupDynamicResource(M2MBase *base) {
ansond 0:1f1f55e73248 712 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 29:be035befb437 713 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 29:be035befb437 714 M2MBase *t = (M2MBase *)dynamic_resources->at(i)->getResource();
ansond 0:1f1f55e73248 715 if (t == base) {
ansond 29:be035befb437 716 return dynamic_resources->at(i);
ansond 0:1f1f55e73248 717 }
ansond 0:1f1f55e73248 718 }
ansond 29:be035befb437 719 return NULL;
ansond 0:1f1f55e73248 720 }
ansond 0:1f1f55e73248 721
ansond 8:f950fb1b78c0 722 // build out the endpoint
ansond 46:62da4ce20276 723 void Endpoint::buildEndpoint()
ansond 0:1f1f55e73248 724 {
ansond 27:b8aaf7dc7023 725 // initialize as an mbed-client
ansond 46:62da4ce20276 726 this->createEndpointInterface();
ansond 81:a2441163a06e 727
ansond 112:b7e348a3363c 728 // make sure we have an endpoint interface...
ansond 112:b7e348a3363c 729 if (this->getEndpointInterface() != NULL) {
ansond 112:b7e348a3363c 730 // Create our server instance
ansond 112:b7e348a3363c 731 this->setSecurityInstance(this->createEndpointSecurityInstance());
ansond 27:b8aaf7dc7023 732
ansond 112:b7e348a3363c 733 // We now have to bind our device resources
ansond 112:b7e348a3363c 734 if (this->m_device_manager != NULL) {
ansond 27:b8aaf7dc7023 735 // DEBUG
ansond 112:b7e348a3363c 736 this->logger()->log("Connector::Endpoint::build(): plumbing the device management objects and resources...");
ansond 112:b7e348a3363c 737
ansond 112:b7e348a3363c 738 // bind the device manager
ansond 112:b7e348a3363c 739 ((DeviceManager *)this->m_device_manager)->bind();
ansond 112:b7e348a3363c 740
ansond 112:b7e348a3363c 741 // push back the Device Resources Object
ansond 112:b7e348a3363c 742 if (this->m_options->getDeviceResourcesObject() != NULL) {
ansond 112:b7e348a3363c 743 // DEBUG
ansond 112:b7e348a3363c 744 this->logger()->log("Connector::Endpoint::build(): plumbing device resources object...");
ansond 112:b7e348a3363c 745
ansond 112:b7e348a3363c 746 // push back the device resources object
ansond 112:b7e348a3363c 747 this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getDeviceResourcesObject());
ansond 112:b7e348a3363c 748 }
ansond 112:b7e348a3363c 749 else {
ansond 112:b7e348a3363c 750 // unable to plumb device manager
ansond 112:b7e348a3363c 751 this->logger()->log("Connector::Endpoint::build(): Unable to plumb device resources. Not installing device resource object...");
ansond 112:b7e348a3363c 752 }
ansond 112:b7e348a3363c 753
ansond 112:b7e348a3363c 754 // push back the Firmware Resources Object
ansond 112:b7e348a3363c 755 if (this->m_options->getFirmwareResourcesObject() != NULL) {
ansond 112:b7e348a3363c 756 // DEBUG
ansond 112:b7e348a3363c 757 this->logger()->log("Connector::Endpoint::build(): plumbing firmware resources object...");
ansond 112:b7e348a3363c 758
ansond 112:b7e348a3363c 759 // push back the firmware resources object
ansond 112:b7e348a3363c 760 this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getFirmwareResourcesObject());
ansond 112:b7e348a3363c 761 }
ansond 112:b7e348a3363c 762 else {
ansond 112:b7e348a3363c 763 // unable to plumb firmware manager
ansond 112:b7e348a3363c 764 this->logger()->log("Connector::Endpoint::build(): Unable to plumb firmware resources. Not installing firmware resource object...");
ansond 112:b7e348a3363c 765 }
ansond 27:b8aaf7dc7023 766 }
ansond 27:b8aaf7dc7023 767 else {
ansond 112:b7e348a3363c 768 // no device manager installed
ansond 112:b7e348a3363c 769 this->logger()->log("Connector::Endpoint::build(): No device manager installed.");
ansond 112:b7e348a3363c 770 }
ansond 112:b7e348a3363c 771
ansond 112:b7e348a3363c 772 // Loop through Static Resources and bind each of them...
ansond 112:b7e348a3363c 773 this->logger()->log("Connector::Endpoint::build(): adding static resources...");
ansond 112:b7e348a3363c 774 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
ansond 112:b7e348a3363c 775 for(int i=0; i<(int)static_resources->size(); ++i) {
ansond 112:b7e348a3363c 776 this->logger()->log("Connector::Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str());
ansond 112:b7e348a3363c 777 static_resources->at(i)->bind(this);
ansond 27:b8aaf7dc7023 778 }
ansond 112:b7e348a3363c 779
ansond 112:b7e348a3363c 780 // Loop through Dynamic Resources and bind each of them...
ansond 112:b7e348a3363c 781 this->logger()->log("Connector::Endpoint::build(): adding dynamic resources...");
ansond 112:b7e348a3363c 782 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 112:b7e348a3363c 783 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 112:b7e348a3363c 784 this->logger()->log("Connector::Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
ansond 112:b7e348a3363c 785 dynamic_resources->at(i)->bind(this);
ansond 112:b7e348a3363c 786 }
ansond 112:b7e348a3363c 787
ansond 112:b7e348a3363c 788 // Get the ObjectList from the ObjectInstanceManager...
ansond 112:b7e348a3363c 789 NamedPointerList list = this->getObjectInstanceManager()->getObjectList();
ansond 27:b8aaf7dc7023 790
ansond 112:b7e348a3363c 791 // DEBUG
ansond 112:b7e348a3363c 792 //this->logger()->log("Endpoint::build(): All Resources bound. Number of Objects in list: %d",list.size());
ansond 112:b7e348a3363c 793
ansond 112:b7e348a3363c 794 // add all of the object instances we have created...
ansond 112:b7e348a3363c 795 for(int i=0;i<(int)list.size();++i) {
ansond 27:b8aaf7dc7023 796 // DEBUG
ansond 112:b7e348a3363c 797 //this->logger()->log("Endpoint::build(): adding Object Instance with ObjID: %s...",list.at(i).name().c_str());
ansond 27:b8aaf7dc7023 798
ansond 112:b7e348a3363c 799 // push back the object instance...
ansond 112:b7e348a3363c 800 this->m_endpoint_object_list.push_back((M2MObject *)(list.at(i).ptr()));
ansond 27:b8aaf7dc7023 801 }
ansond 27:b8aaf7dc7023 802 }
ansond 27:b8aaf7dc7023 803 else {
ansond 112:b7e348a3363c 804 // no endpoint interface created
ansond 112:b7e348a3363c 805 this->logger()->log("Endpoint::build(): ERROR in creating the endpoint interface...");
ansond 0:1f1f55e73248 806 }
ansond 0:1f1f55e73248 807 }
ansond 0:1f1f55e73248 808
ansond 54:dfee8691c83a 809 // stop underlying observation mechanisms
ansond 54:dfee8691c83a 810 void Endpoint::stopObservations() {
ansond 54:dfee8691c83a 811 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 54:dfee8691c83a 812 for(int i=0; i<(int)dynamic_resources->size(); ++i) {
ansond 54:dfee8691c83a 813 if (dynamic_resources->at(i)->isObservable() == true) {
ansond 54:dfee8691c83a 814 ResourceObserver *observer = (ResourceObserver *)dynamic_resources->at(i)->getObserver();
ansond 54:dfee8691c83a 815 if (observer != NULL) {
ansond 54:dfee8691c83a 816 this->logger()->log("Connector::Endpoint::stopObservations(): stopping resource observer for: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
ansond 54:dfee8691c83a 817 observer->halt();
ansond 54:dfee8691c83a 818 }
ansond 54:dfee8691c83a 819 }
ansond 54:dfee8691c83a 820 }
ansond 54:dfee8691c83a 821 }
ansond 54:dfee8691c83a 822
ansond 13:9edad7677211 823 // underlying network is connected (SET)
ansond 13:9edad7677211 824 void Endpoint::isConnected(bool connected) {
ansond 13:9edad7677211 825 this->m_connected = connected;
ansond 13:9edad7677211 826 }
ansond 13:9edad7677211 827
ansond 13:9edad7677211 828 // underlying network is connected (GET)
ansond 13:9edad7677211 829 bool Endpoint::isConnected() {
ansond 13:9edad7677211 830 return this->m_connected;
ansond 13:9edad7677211 831 }
ansond 13:9edad7677211 832
ansond 15:c11dbe4d354c 833 // Registered with mDC/mDS
ansond 15:c11dbe4d354c 834 bool Endpoint::isRegistered() {
ansond 15:c11dbe4d354c 835 return this->m_registered;
ansond 15:c11dbe4d354c 836 }
ansond 15:c11dbe4d354c 837
ansond 43:3fb57c4fba34 838 // Set the ConnectionStatusInterface
ansond 27:b8aaf7dc7023 839 void Endpoint::setConnectionStatusInterfaceImpl(ConnectionStatusInterface *csi) {
ansond 43:3fb57c4fba34 840 this->m_csi = csi;
ansond 27:b8aaf7dc7023 841 }
ansond 27:b8aaf7dc7023 842
ansond 27:b8aaf7dc7023 843 // Set our ObjectInstanceManager
ansond 27:b8aaf7dc7023 844 void Endpoint::setObjectInstanceManager(ObjectInstanceManager *oim) {
ansond 27:b8aaf7dc7023 845 this->m_oim = oim;
ansond 27:b8aaf7dc7023 846 }
ansond 27:b8aaf7dc7023 847
ansond 27:b8aaf7dc7023 848 // Get our ObjectInstanceManager
ansond 27:b8aaf7dc7023 849 ObjectInstanceManager *Endpoint::getObjectInstanceManager() {
ansond 27:b8aaf7dc7023 850 return this->m_oim;
ansond 27:b8aaf7dc7023 851 }
ansond 27:b8aaf7dc7023 852
ansond 0:1f1f55e73248 853 // our logger
ansond 0:1f1f55e73248 854 Logger *Endpoint::logger()
ansond 0:1f1f55e73248 855 {
ansond 0:1f1f55e73248 856 return this->m_logger;
ansond 0:1f1f55e73248 857 }
ansond 0:1f1f55e73248 858
ansond 0:1f1f55e73248 859 } // namespace Connector