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