mbedConnectorInterface back port from mbedOS v3 using mbed-client C++ call interface
source/ConnectorEndpoint.cpp@10:3f79b5e67c22, 2016-03-04 (annotated)
- Committer:
- ansond
- Date:
- Fri Mar 04 19:16:57 2016 +0000
- Revision:
- 10:3f79b5e67c22
- Parent:
- 8:f950fb1b78c0
- Child:
- 13:9edad7677211
updated to allow for specification of UDP or TCP under CoAP and IPv4 or IPv6 addressing types
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 | 0:1f1f55e73248 | 32 | // DEBUG |
ansond | 0:1f1f55e73248 | 33 | #ifndef NDEBUG |
ansond | 0:1f1f55e73248 | 34 | #define DEBUG_OUT(...) { printf(__VA_ARGS__); } |
ansond | 0:1f1f55e73248 | 35 | #else |
ansond | 0:1f1f55e73248 | 36 | #define DEBUG_OUT(...) /* nothing */ |
ansond | 0:1f1f55e73248 | 37 | #endif |
ansond | 0:1f1f55e73248 | 38 | |
ansond | 1:16f0fb5b8d97 | 39 | // our endpoint instance |
ansond | 1:16f0fb5b8d97 | 40 | static Connector::Endpoint *__endpoint = NULL; |
ansond | 0:1f1f55e73248 | 41 | |
ansond | 0:1f1f55e73248 | 42 | // Connector namespace |
ansond | 0:1f1f55e73248 | 43 | namespace Connector { |
ansond | 0:1f1f55e73248 | 44 | |
ansond | 0:1f1f55e73248 | 45 | // STATIC: Plumb the network |
ansond | 0:1f1f55e73248 | 46 | void Endpoint::plumbNetwork(bool canActAsRouterNode) { |
ansond | 1:16f0fb5b8d97 | 47 | if (__endpoint == NULL) { |
ansond | 1:16f0fb5b8d97 | 48 | // initialize our endpoint instance |
ansond | 1:16f0fb5b8d97 | 49 | DEBUG_OUT("Endpoint::plumbNetwork: initializing endpoint instance...\r\n"); |
ansond | 1:16f0fb5b8d97 | 50 | __endpoint = (Connector::Endpoint *)utils_init_endpoint(canActAsRouterNode); |
ansond | 1:16f0fb5b8d97 | 51 | } |
ansond | 8:f950fb1b78c0 | 52 | |
ansond | 0:1f1f55e73248 | 53 | // configure the endpoint now... |
ansond | 0:1f1f55e73248 | 54 | DEBUG_OUT("Endpoint::plumbNetwork: configuring endpoint...\r\n"); |
ansond | 1:16f0fb5b8d97 | 55 | utils_configure_endpoint((void *)__endpoint); |
ansond | 2:1a7a292555d1 | 56 | |
ansond | 2:1a7a292555d1 | 57 | // plumb network |
ansond | 2:1a7a292555d1 | 58 | DEBUG_OUT("Endpoint::plumbNetwork: plumbing network...\r\n"); |
ansond | 8:f950fb1b78c0 | 59 | net_plumb_network((void *)__endpoint); |
ansond | 0:1f1f55e73248 | 60 | } |
ansond | 0:1f1f55e73248 | 61 | |
ansond | 0:1f1f55e73248 | 62 | // STATIC: Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger) |
ansond | 8:f950fb1b78c0 | 63 | void Endpoint::start() |
ansond | 8:f950fb1b78c0 | 64 | { |
ansond | 8:f950fb1b78c0 | 65 | // complete setup of our endpoint... |
ansond | 8:f950fb1b78c0 | 66 | DEBUG_OUT("Endpoint::start: building out endpoint...\r\n"); |
ansond | 8:f950fb1b78c0 | 67 | utils_build_endpoint((void *)__endpoint); |
ansond | 0:1f1f55e73248 | 68 | |
ansond | 8:f950fb1b78c0 | 69 | // register the endpoint |
ansond | 8:f950fb1b78c0 | 70 | DEBUG_OUT("Endpoint::start: completing endpoint build out..\r\n"); |
ansond | 8:f950fb1b78c0 | 71 | net_perform_endpoint_registration(__endpoint); |
ansond | 0:1f1f55e73248 | 72 | } |
ansond | 0:1f1f55e73248 | 73 | |
ansond | 0:1f1f55e73248 | 74 | // Constructor |
ansond | 0:1f1f55e73248 | 75 | Endpoint::Endpoint(const Logger *logger, const Options *options) : M2MInterfaceObserver() |
ansond | 0:1f1f55e73248 | 76 | { |
ansond | 0:1f1f55e73248 | 77 | this->m_logger = (Logger *)logger; |
ansond | 0:1f1f55e73248 | 78 | this->m_options = (Options *)options; |
ansond | 0:1f1f55e73248 | 79 | } |
ansond | 0:1f1f55e73248 | 80 | |
ansond | 0:1f1f55e73248 | 81 | // Copy Constructor |
ansond | 0:1f1f55e73248 | 82 | Endpoint::Endpoint(const Endpoint &ep) |
ansond | 0:1f1f55e73248 | 83 | { |
ansond | 0:1f1f55e73248 | 84 | this->m_logger = ep.m_logger; |
ansond | 0:1f1f55e73248 | 85 | this->m_options = ep.m_options; |
ansond | 0:1f1f55e73248 | 86 | this->m_interface = ep.m_interface; |
ansond | 0:1f1f55e73248 | 87 | this->m_server_instance = ep.m_server_instance; |
ansond | 0:1f1f55e73248 | 88 | this->m_object_list = ep.m_object_list; |
ansond | 0:1f1f55e73248 | 89 | this->m_device_object = ep.m_device_object; |
ansond | 0:1f1f55e73248 | 90 | } |
ansond | 0:1f1f55e73248 | 91 | |
ansond | 0:1f1f55e73248 | 92 | // Destructor |
ansond | 0:1f1f55e73248 | 93 | Endpoint::~Endpoint() { |
ansond | 0:1f1f55e73248 | 94 | if (this->m_interface != NULL) |
ansond | 0:1f1f55e73248 | 95 | delete this->m_interface; |
ansond | 0:1f1f55e73248 | 96 | if (this->m_server_instance != NULL) |
ansond | 0:1f1f55e73248 | 97 | delete this->m_server_instance; |
ansond | 0:1f1f55e73248 | 98 | } |
ansond | 0:1f1f55e73248 | 99 | |
ansond | 1:16f0fb5b8d97 | 100 | // router node behavior setting |
ansond | 1:16f0fb5b8d97 | 101 | void Endpoint::asRouterNode(bool canActAsRouterNode) { |
ansond | 1:16f0fb5b8d97 | 102 | this->m_canActAsRouterNode = canActAsRouterNode; |
ansond | 1:16f0fb5b8d97 | 103 | } |
ansond | 1:16f0fb5b8d97 | 104 | |
ansond | 1:16f0fb5b8d97 | 105 | // set our Options |
ansond | 1:16f0fb5b8d97 | 106 | void Endpoint::setOptions(Options *options) { |
ansond | 1:16f0fb5b8d97 | 107 | this->m_options = options; |
ansond | 1:16f0fb5b8d97 | 108 | } |
ansond | 1:16f0fb5b8d97 | 109 | |
ansond | 0:1f1f55e73248 | 110 | // get our Options |
ansond | 0:1f1f55e73248 | 111 | Options *Endpoint::getOptions() { |
ansond | 0:1f1f55e73248 | 112 | return this->m_options; |
ansond | 0:1f1f55e73248 | 113 | } |
ansond | 0:1f1f55e73248 | 114 | |
ansond | 0:1f1f55e73248 | 115 | // get our Server |
ansond | 0:1f1f55e73248 | 116 | M2MSecurity *Endpoint::getServer() { |
ansond | 0:1f1f55e73248 | 117 | return this->m_server_instance; |
ansond | 0:1f1f55e73248 | 118 | } |
ansond | 0:1f1f55e73248 | 119 | |
ansond | 0:1f1f55e73248 | 120 | // get our ObjectList |
ansond | 0:1f1f55e73248 | 121 | M2MObjectList Endpoint::getObjectList() { |
ansond | 0:1f1f55e73248 | 122 | return this->m_object_list; |
ansond | 0:1f1f55e73248 | 123 | } |
ansond | 0:1f1f55e73248 | 124 | |
ansond | 0:1f1f55e73248 | 125 | // mbed-client: create our interface |
ansond | 0:1f1f55e73248 | 126 | void Endpoint::create_interface() { |
ansond | 10:3f79b5e67c22 | 127 | // get the CoAP listening port |
ansond | 10:3f79b5e67c22 | 128 | uint16_t listening_port = (uint16_t)this->m_options->getConnectorPort(); |
ansond | 10:3f79b5e67c22 | 129 | |
ansond | 10:3f79b5e67c22 | 130 | // randomize the port if we are using certificates... |
ansond | 10:3f79b5e67c22 | 131 | if (this->m_options->getServerCertificateSize() > 0) { |
ansond | 0:1f1f55e73248 | 132 | // Randomizing listening port for Certificate mode connectivity |
ansond | 0:1f1f55e73248 | 133 | srand(time(NULL)); |
ansond | 10:3f79b5e67c22 | 134 | listening_port = rand() % 65535 + 12345; |
ansond | 10:3f79b5e67c22 | 135 | } |
ansond | 10:3f79b5e67c22 | 136 | |
ansond | 10:3f79b5e67c22 | 137 | // Binding Mode - TCP or UDP |
ansond | 10:3f79b5e67c22 | 138 | M2MInterface::BindingMode network_protocol = M2MInterface::UDP; |
ansond | 10:3f79b5e67c22 | 139 | if (this->m_options->getCoAPConnectionType() == COAP_TCP) network_protocol = M2MInterface::TCP; |
ansond | 10:3f79b5e67c22 | 140 | |
ansond | 10:3f79b5e67c22 | 141 | // Network Type IPv4 or IPv6 |
ansond | 10:3f79b5e67c22 | 142 | M2MInterface::NetworkStack ip_address_type = M2MInterface::LwIP_IPv4; |
ansond | 10:3f79b5e67c22 | 143 | if (this->m_options->getIPAddressType() == IP_ADDRESS_TYPE_IPV6) ip_address_type = M2MInterface::LwIP_IPv6; |
ansond | 10:3f79b5e67c22 | 144 | |
ansond | 10:3f79b5e67c22 | 145 | // DEBUG |
ansond | 10:3f79b5e67c22 | 146 | if (network_protocol == M2MInterface::TCP) this->logger()->log("Endpoint: Underlying Protocol: TCP"); |
ansond | 10:3f79b5e67c22 | 147 | if (network_protocol == M2MInterface::UDP) this->logger()->log("Endpoint: Underlying Protocol: UDP"); |
ansond | 10:3f79b5e67c22 | 148 | if (ip_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Endpoint: IP Address Type: IPv4"); |
ansond | 10:3f79b5e67c22 | 149 | if (ip_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Endpoint: IP Address Type: IPv6"); |
ansond | 10:3f79b5e67c22 | 150 | |
ansond | 10:3f79b5e67c22 | 151 | // Create the M2M Interface instance |
ansond | 10:3f79b5e67c22 | 152 | this->m_interface = M2MInterfaceFactory::create_interface(*this, |
ansond | 10:3f79b5e67c22 | 153 | (char *)this->m_options->getEndpointNodename().c_str(), |
ansond | 10:3f79b5e67c22 | 154 | (char *)this->m_options->getEndpointType().c_str(), |
ansond | 10:3f79b5e67c22 | 155 | (int32_t)this->m_options->getLifetime(), |
ansond | 10:3f79b5e67c22 | 156 | listening_port, // listening port |
ansond | 10:3f79b5e67c22 | 157 | (char *)this->m_options->getDomain().c_str(), |
ansond | 10:3f79b5e67c22 | 158 | network_protocol, // CoAP over UDP or TCP... |
ansond | 10:3f79b5e67c22 | 159 | ip_address_type); // IPv4 addressing or IPv6 addressing |
ansond | 0:1f1f55e73248 | 160 | } |
ansond | 0:1f1f55e73248 | 161 | |
ansond | 0:1f1f55e73248 | 162 | // mbed-client: create_server_instance() |
ansond | 0:1f1f55e73248 | 163 | M2MSecurity *Endpoint::create_server_instance() { |
ansond | 0:1f1f55e73248 | 164 | // Creates register server object with mbed device server address and other parameters |
ansond | 0:1f1f55e73248 | 165 | M2MSecurity *server = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); |
ansond | 0:1f1f55e73248 | 166 | if (server != NULL) { |
ansond | 0:1f1f55e73248 | 167 | const String url = this->m_options->getConnectorURL(); |
ansond | 0:1f1f55e73248 | 168 | server->set_resource_value(M2MSecurity::M2MServerUri, url); |
ansond | 8:f950fb1b78c0 | 169 | server->set_resource_value(M2MSecurity::BootstrapServer, NULL); |
ansond | 0:1f1f55e73248 | 170 | server->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); |
ansond | 0:1f1f55e73248 | 171 | server->set_resource_value(M2MSecurity::ServerPublicKey,this->m_options->getServerCertificate(),this->m_options->getServerCertificateSize()); |
ansond | 0:1f1f55e73248 | 172 | server->set_resource_value(M2MSecurity::PublicKey,this->m_options->getClientCertificate(),this->m_options->getClientCertificateSize()); |
ansond | 0:1f1f55e73248 | 173 | server->set_resource_value(M2MSecurity::Secretkey,this->m_options->getClientKey(),this->m_options->getClientKeySize()); |
ansond | 0:1f1f55e73248 | 174 | } |
ansond | 0:1f1f55e73248 | 175 | return server; |
ansond | 0:1f1f55e73248 | 176 | } |
ansond | 0:1f1f55e73248 | 177 | |
ansond | 0:1f1f55e73248 | 178 | // mbed-client: Callback from mbed client stack if any error is encountered |
ansond | 0:1f1f55e73248 | 179 | void Endpoint::error(M2MInterface::Error error) { |
ansond | 0:1f1f55e73248 | 180 | switch(error){ |
ansond | 0:1f1f55e73248 | 181 | case M2MInterface::AlreadyExists: |
ansond | 0:1f1f55e73248 | 182 | DEBUG_OUT("[ERROR:] M2MInterface::AlreadyExists"); |
ansond | 0:1f1f55e73248 | 183 | break; |
ansond | 0:1f1f55e73248 | 184 | case M2MInterface::BootstrapFailed: |
ansond | 0:1f1f55e73248 | 185 | DEBUG_OUT("[ERROR:] M2MInterface::BootstrapFailed"); |
ansond | 0:1f1f55e73248 | 186 | break; |
ansond | 0:1f1f55e73248 | 187 | case M2MInterface::InvalidParameters: |
ansond | 0:1f1f55e73248 | 188 | DEBUG_OUT("[ERROR:] M2MInterface::InvalidParameters"); |
ansond | 0:1f1f55e73248 | 189 | break; |
ansond | 0:1f1f55e73248 | 190 | case M2MInterface::NotRegistered: |
ansond | 0:1f1f55e73248 | 191 | DEBUG_OUT("[ERROR:] M2MInterface::NotRegistered"); |
ansond | 0:1f1f55e73248 | 192 | break; |
ansond | 0:1f1f55e73248 | 193 | case M2MInterface::Timeout: |
ansond | 0:1f1f55e73248 | 194 | DEBUG_OUT("[ERROR:] M2MInterface::Timeout"); |
ansond | 0:1f1f55e73248 | 195 | break; |
ansond | 0:1f1f55e73248 | 196 | case M2MInterface::NetworkError: |
ansond | 0:1f1f55e73248 | 197 | DEBUG_OUT("[ERROR:] M2MInterface::NetworkError"); |
ansond | 0:1f1f55e73248 | 198 | break; |
ansond | 0:1f1f55e73248 | 199 | case M2MInterface::ResponseParseFailed: |
ansond | 0:1f1f55e73248 | 200 | DEBUG_OUT("[ERROR:] M2MInterface::ResponseParseFailed"); |
ansond | 0:1f1f55e73248 | 201 | break; |
ansond | 0:1f1f55e73248 | 202 | case M2MInterface::UnknownError: |
ansond | 0:1f1f55e73248 | 203 | DEBUG_OUT("[ERROR:] M2MInterface::UnknownError"); |
ansond | 0:1f1f55e73248 | 204 | break; |
ansond | 0:1f1f55e73248 | 205 | case M2MInterface::MemoryFail: |
ansond | 0:1f1f55e73248 | 206 | DEBUG_OUT("[ERROR:] M2MInterface::MemoryFail"); |
ansond | 0:1f1f55e73248 | 207 | break; |
ansond | 0:1f1f55e73248 | 208 | case M2MInterface::NotAllowed: |
ansond | 0:1f1f55e73248 | 209 | DEBUG_OUT("[ERROR:] M2MInterface::NotAllowed"); |
ansond | 0:1f1f55e73248 | 210 | break; |
ansond | 0:1f1f55e73248 | 211 | default: |
ansond | 0:1f1f55e73248 | 212 | break; |
ansond | 0:1f1f55e73248 | 213 | } |
ansond | 0:1f1f55e73248 | 214 | } |
ansond | 0:1f1f55e73248 | 215 | |
ansond | 8:f950fb1b78c0 | 216 | // register the endpoint |
ansond | 8:f950fb1b78c0 | 217 | void Endpoint::register_endpoint(M2MSecurity *server_instance, M2MObjectList resources) { |
ansond | 8:f950fb1b78c0 | 218 | if (this->m_interface != NULL && server_instance != NULL && resources.size() > 0) { |
ansond | 8:f950fb1b78c0 | 219 | // register endpoint |
ansond | 8:f950fb1b78c0 | 220 | this->logger()->log("Registering endpoint..."); |
ansond | 8:f950fb1b78c0 | 221 | this->m_interface->register_object(server_instance, resources); |
ansond | 8:f950fb1b78c0 | 222 | } |
ansond | 0:1f1f55e73248 | 223 | } |
ansond | 0:1f1f55e73248 | 224 | |
ansond | 8:f950fb1b78c0 | 225 | // re-register the endpoint |
ansond | 8:f950fb1b78c0 | 226 | void Endpoint::re_register_endpoint() { |
ansond | 8:f950fb1b78c0 | 227 | if (this->m_interface != NULL) { |
ansond | 8:f950fb1b78c0 | 228 | this->m_interface->update_registration(this->m_server_instance, this->m_options->getLifetime()); |
ansond | 8:f950fb1b78c0 | 229 | } |
ansond | 8:f950fb1b78c0 | 230 | } |
ansond | 8:f950fb1b78c0 | 231 | |
ansond | 8:f950fb1b78c0 | 232 | // de-register endpoint |
ansond | 8:f950fb1b78c0 | 233 | void Endpoint::de_register_endpoint(void) { |
ansond | 0:1f1f55e73248 | 234 | if (this->m_interface != NULL) { |
ansond | 0:1f1f55e73248 | 235 | // de-register endpoint |
ansond | 0:1f1f55e73248 | 236 | this->logger()->log("De-registering endpoint..."); |
ansond | 0:1f1f55e73248 | 237 | this->m_interface->unregister_object(NULL); |
ansond | 0:1f1f55e73248 | 238 | } |
ansond | 0:1f1f55e73248 | 239 | } |
ansond | 0:1f1f55e73248 | 240 | |
ansond | 8:f950fb1b78c0 | 241 | // object registered |
ansond | 8:f950fb1b78c0 | 242 | void Endpoint::object_registered(M2MSecurity */*security_object */, const M2MServer &/*server_object*/) { |
ansond | 8:f950fb1b78c0 | 243 | this->logger()->log("Endpoint registered"); |
ansond | 8:f950fb1b78c0 | 244 | } |
ansond | 8:f950fb1b78c0 | 245 | |
ansond | 8:f950fb1b78c0 | 246 | // registration updated |
ansond | 8:f950fb1b78c0 | 247 | void Endpoint::registration_updated(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/) { |
ansond | 8:f950fb1b78c0 | 248 | this->logger()->log("Endpoint re-registered."); |
ansond | 8:f950fb1b78c0 | 249 | } |
ansond | 8:f950fb1b78c0 | 250 | |
ansond | 8:f950fb1b78c0 | 251 | // object unregistered |
ansond | 8:f950fb1b78c0 | 252 | void Endpoint::object_unregistered(M2MSecurity */*server_object*/) { |
ansond | 8:f950fb1b78c0 | 253 | // ready to exit |
ansond | 8:f950fb1b78c0 | 254 | this->logger()->log("Endpoint de-registered... Ready to exit..."); |
ansond | 8:f950fb1b78c0 | 255 | |
ansond | 8:f950fb1b78c0 | 256 | // stop |
ansond | 8:f950fb1b78c0 | 257 | exit(0); |
ansond | 0:1f1f55e73248 | 258 | } |
ansond | 0:1f1f55e73248 | 259 | |
ansond | 0:1f1f55e73248 | 260 | // bootstrap done |
ansond | 0:1f1f55e73248 | 261 | void Endpoint::bootstrap_done(M2MSecurity * /*server_object */) { |
ansond | 0:1f1f55e73248 | 262 | this->logger()->log("Bootstrapped"); |
ansond | 0:1f1f55e73248 | 263 | } |
ansond | 0:1f1f55e73248 | 264 | |
ansond | 0:1f1f55e73248 | 265 | // resource value updated |
ansond | 0:1f1f55e73248 | 266 | void Endpoint::value_updated(M2MBase *base, M2MBase::BaseType type) { |
ansond | 0:1f1f55e73248 | 267 | this->logger()->log("Value Updated"); |
ansond | 0:1f1f55e73248 | 268 | DynamicResource *target_res = this->lookupDynamicResource(base); |
ansond | 0:1f1f55e73248 | 269 | target_res->process(base->operation(),type); |
ansond | 0:1f1f55e73248 | 270 | } |
ansond | 0:1f1f55e73248 | 271 | |
ansond | 0:1f1f55e73248 | 272 | // lookup which DynamicResource cooresponds to a given M2MBase instance... |
ansond | 0:1f1f55e73248 | 273 | DynamicResource *Endpoint::lookupDynamicResource(M2MBase *base) { |
ansond | 0:1f1f55e73248 | 274 | DynamicResource *res = NULL; |
ansond | 0:1f1f55e73248 | 275 | bool found = false; |
ansond | 0:1f1f55e73248 | 276 | const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList(); |
ansond | 0:1f1f55e73248 | 277 | for(int i=0; i<(int)dynamic_resources->size() && found == false; ++i) { |
ansond | 0:1f1f55e73248 | 278 | M2MBase *t = dynamic_resources->at(i)->getResource(); |
ansond | 0:1f1f55e73248 | 279 | if (t == base) { |
ansond | 0:1f1f55e73248 | 280 | res = dynamic_resources->at(i); |
ansond | 0:1f1f55e73248 | 281 | found = true; |
ansond | 0:1f1f55e73248 | 282 | } |
ansond | 0:1f1f55e73248 | 283 | } |
ansond | 0:1f1f55e73248 | 284 | |
ansond | 0:1f1f55e73248 | 285 | return res; |
ansond | 0:1f1f55e73248 | 286 | } |
ansond | 0:1f1f55e73248 | 287 | |
ansond | 8:f950fb1b78c0 | 288 | // build out the endpoint |
ansond | 8:f950fb1b78c0 | 289 | void Endpoint::build_endpoint() |
ansond | 0:1f1f55e73248 | 290 | { |
ansond | 0:1f1f55e73248 | 291 | // initialize as an mbed-client |
ansond | 0:1f1f55e73248 | 292 | this->create_interface(); |
ansond | 0:1f1f55e73248 | 293 | |
ansond | 0:1f1f55e73248 | 294 | // Create our server instance |
ansond | 0:1f1f55e73248 | 295 | this->m_server_instance = this->create_server_instance(); |
ansond | 0:1f1f55e73248 | 296 | |
ansond | 0:1f1f55e73248 | 297 | // Loop through Static Resources and bind each of them... |
ansond | 8:f950fb1b78c0 | 298 | this->logger()->log("Endpoint::build(): adding device resources..."); |
ansond | 0:1f1f55e73248 | 299 | const DeviceResourcesList *device_resources = this->m_options->getDeviceResourceList(); |
ansond | 0:1f1f55e73248 | 300 | for(int i=0; i<(int)device_resources->size(); ++i) { |
ansond | 8:f950fb1b78c0 | 301 | this->logger()->log("Endpoint::build(): binding device resource: [%s]...",device_resources->at(i)->getFullName().c_str()); |
ansond | 0:1f1f55e73248 | 302 | this->m_object_list.push_back(device_resources->at(i)->bind(this)); |
ansond | 0:1f1f55e73248 | 303 | } |
ansond | 0:1f1f55e73248 | 304 | |
ansond | 0:1f1f55e73248 | 305 | // Loop through Static Resources and bind each of them... |
ansond | 8:f950fb1b78c0 | 306 | this->logger()->log("Endpoint::build(): adding static resources..."); |
ansond | 0:1f1f55e73248 | 307 | const StaticResourcesList *static_resources = this->m_options->getStaticResourceList(); |
ansond | 0:1f1f55e73248 | 308 | for(int i=0; i<(int)static_resources->size(); ++i) { |
ansond | 8:f950fb1b78c0 | 309 | this->logger()->log("Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str()); |
ansond | 0:1f1f55e73248 | 310 | this->m_object_list.push_back(static_resources->at(i)->bind(this)); |
ansond | 0:1f1f55e73248 | 311 | } |
ansond | 0:1f1f55e73248 | 312 | |
ansond | 0:1f1f55e73248 | 313 | // Loop through Dynamic Resources and bind each of them... |
ansond | 8:f950fb1b78c0 | 314 | this->logger()->log("Endpoint::build(): adding dynamic resources..."); |
ansond | 0:1f1f55e73248 | 315 | const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList(); |
ansond | 0:1f1f55e73248 | 316 | for(int i=0; i<(int)dynamic_resources->size(); ++i) { |
ansond | 8:f950fb1b78c0 | 317 | this->logger()->log("Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str()); |
ansond | 0:1f1f55e73248 | 318 | this->m_object_list.push_back(dynamic_resources->at(i)->bind(this)); |
ansond | 0:1f1f55e73248 | 319 | } |
ansond | 0:1f1f55e73248 | 320 | } |
ansond | 0:1f1f55e73248 | 321 | |
ansond | 0:1f1f55e73248 | 322 | // our logger |
ansond | 0:1f1f55e73248 | 323 | Logger *Endpoint::logger() |
ansond | 0:1f1f55e73248 | 324 | { |
ansond | 0:1f1f55e73248 | 325 | return this->m_logger; |
ansond | 0:1f1f55e73248 | 326 | } |
ansond | 0:1f1f55e73248 | 327 | |
ansond | 0:1f1f55e73248 | 328 | } // namespace Connector |