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