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