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