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 mbedConnectorInterfaceWithDM by
ConnectorEndpoint.cpp
00001 /** 00002 * @file ConnectorEndpoint.cpp 00003 * @brief mbed CoAP Endpoint base class 00004 * @author Doug Anson/Chris Paola 00005 * @version 1.0 00006 * @see 00007 * 00008 * Copyright (c) 2014 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); 00011 * you may not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, 00018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 */ 00022 00023 // Lower level Network 00024 #include "mbed-connector-interface/mbedEndpointNetwork.h" 00025 00026 // ConnectorEndpoint 00027 #include "mbed-connector-interface/ConnectorEndpoint.h" 00028 00029 // Utils support 00030 #include "mbed-connector-interface/Utils.h" 00031 00032 // Device Manager support 00033 #include "mbed-connector-interface/DeviceManager.h" 00034 00035 // our endpoint instance 00036 static Connector::Endpoint *__endpoint = NULL; 00037 00038 // LWIP Network interface instance 00039 NetworkInterface *__network_interface = NULL; 00040 00041 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00042 // Static mbed endpoint 00043 static MbedCloudClient __mbed_cloud_client; 00044 #endif 00045 00046 // Connector namespace 00047 namespace Connector { 00048 00049 // STATIC: Plumb the network 00050 void Endpoint::plumbNetwork(void *device_manager,bool canActAsRouterNode) { 00051 // create our endpoint instance... 00052 if (__endpoint == NULL) { 00053 // initialize our endpoint instance 00054 printf("Connector::Endpoint::plumbNetwork: initializing endpoint instance...\r\n"); 00055 __endpoint = (Connector::Endpoint *)utils_init_endpoint(canActAsRouterNode); 00056 } 00057 00058 // set the device manager 00059 if (device_manager != NULL) { 00060 // device manager has been supplied 00061 printf("Connector::Endpoint::plumbNetwork: setting a device manager...\r\n"); 00062 __endpoint->setDeviceManager(device_manager); 00063 } 00064 else { 00065 // no device manager supplied 00066 printf("Connector::Endpoint::plumbNetwork: no device manager supplied (OK)\r\n"); 00067 } 00068 00069 // configure the endpoint... 00070 printf("Connector::Endpoint::plumbNetwork: configuring endpoint...\r\n"); 00071 utils_configure_endpoint((void *)__endpoint); 00072 00073 // plumb the endpoint's network... 00074 printf("Connector::Endpoint::plumbNetwork: plumbing network...\r\n"); 00075 net_plumb_network((void *)__endpoint); 00076 } 00077 00078 // STATIC: Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger) 00079 void Endpoint::start() 00080 { 00081 // build out the endpoint with its configuration... 00082 printf("Connector::Endpoint::start: building out endpoint...\r\n"); 00083 utils_build_endpoint((void *)__endpoint); 00084 00085 // finalize the endpoint and start its main loop 00086 printf("Endpoint::start: finalize and run the endpoint main loop..\r\n"); 00087 net_finalize_and_run_endpoint_main_loop((void *)__endpoint); 00088 } 00089 00090 // STATIC: Set the ConnectionStatusInterface Implementation instance 00091 void Endpoint::setConnectionStatusInterface(ConnectionStatusInterface *csi) { 00092 if (__endpoint != NULL) { 00093 __endpoint->setConnectionStatusInterfaceImpl(csi); 00094 } 00095 } 00096 00097 // Constructor 00098 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00099 Endpoint::Endpoint(const Logger *logger, const Options *options) : MbedCloudClientCallback(), M2MInterfaceObserver() 00100 #else 00101 Endpoint::Endpoint(const Logger *logger, const Options *options) : M2MInterfaceObserver() 00102 #endif 00103 { 00104 this->m_logger = (Logger *)logger; 00105 this->m_options = (Options *)options; 00106 this->m_device_manager = NULL; 00107 this->m_connected = false; 00108 this->m_registered = false; 00109 this->m_csi = NULL; 00110 this->m_oim = NULL; 00111 this->m_endpoint_security = NULL; 00112 this->m_endpoint_interface = NULL; 00113 } 00114 00115 // Copy Constructor 00116 Endpoint::Endpoint(const Endpoint &ep) 00117 { 00118 this->m_logger = ep.m_logger; 00119 this->m_options = ep.m_options; 00120 this->m_endpoint_interface = ep.m_endpoint_interface; 00121 this->m_endpoint_security = ep.m_endpoint_security; 00122 this->m_endpoint_object_list = ep.m_endpoint_object_list; 00123 this->m_device_manager = ep.m_device_manager; 00124 this->m_connected = ep.m_connected; 00125 this->m_registered = ep.m_registered; 00126 this->m_csi = ep.m_csi; 00127 this->m_oim = ep.m_oim; 00128 } 00129 00130 // Destructor 00131 Endpoint::~Endpoint() { 00132 #ifndef ENABLE_MBED_CLOUD_SUPPORT 00133 if (this->m_endpoint_interface != NULL) 00134 delete this->m_endpoint_interface; 00135 00136 if (this->m_endpoint_security != NULL) 00137 delete this->m_endpoint_security; 00138 #endif 00139 } 00140 00141 // set the device manager 00142 void Endpoint::setDeviceManager(void *device_manager) { 00143 this->m_device_manager = device_manager; 00144 } 00145 00146 // get the device manager 00147 void *Endpoint::getDeviceManager(void) { 00148 return this->m_device_manager; 00149 } 00150 00151 // router node behavior setting 00152 void Endpoint::asRouterNode(bool canActAsRouterNode) { 00153 this->m_canActAsRouterNode = canActAsRouterNode; 00154 } 00155 00156 // set our Options 00157 void Endpoint::setOptions(Options *options) { 00158 this->m_options = options; 00159 } 00160 00161 // get our Options 00162 Options *Endpoint::getOptions() { 00163 return this->m_options; 00164 } 00165 00166 // get our endpoint security instance 00167 M2MSecurity *Endpoint::getSecurityInstance() { 00168 return this->m_endpoint_security; 00169 } 00170 00171 // set our endpoint security instance 00172 void Endpoint::setSecurityInstance(M2MSecurity *security) { 00173 if (security != NULL) { 00174 this->m_endpoint_security = security; 00175 } 00176 } 00177 00178 // get our ObjectList 00179 M2MObjectList Endpoint::getEndpointObjectList() { 00180 return this->m_endpoint_object_list; 00181 } 00182 00183 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00184 // get our endpoint interface 00185 MbedCloudClient *Endpoint::getEndpointInterface() { 00186 return this->m_endpoint_interface; 00187 } 00188 #else 00189 // get our endpoint interface 00190 M2MInterface *Endpoint::getEndpointInterface() { 00191 return this->m_endpoint_interface; 00192 } 00193 #endif 00194 00195 // Connector::Endpoint: create our interface 00196 void Endpoint::createEndpointInterface() { 00197 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00198 this->createCloudEndpointInterface(); 00199 #else 00200 this->createConnectorEndpointInterface(); 00201 #endif 00202 } 00203 00204 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00205 // mbedCloudClient: create our interface 00206 void Endpoint::createCloudEndpointInterface() { 00207 if (this->m_endpoint_interface == NULL) { 00208 this->m_endpoint_interface = &__mbed_cloud_client; 00209 } 00210 00211 // bind LWIP network interface pointer... 00212 if (__network_interface != NULL && this->m_endpoint_interface != NULL) { 00213 this->logger()->log("Connector::Endpoint: binding LWIP network instance (Cloud)..."); 00214 this->m_endpoint_interface->on_registered(&Connector::Endpoint::on_registered); 00215 this->m_endpoint_interface->on_unregistered(&Connector::Endpoint::on_unregistered); 00216 this->m_endpoint_interface->on_error(&Connector::Endpoint::on_error); 00217 this->m_endpoint_interface->set_update_callback(this); 00218 this->m_endpoint_interface->setup(__network_interface); 00219 } 00220 } 00221 #else 00222 // mbed-client: create our interface 00223 void Endpoint::createConnectorEndpointInterface() { 00224 // get the CoAP listening port 00225 uint16_t listening_port = (uint16_t)this->m_options->getConnectorPort(); 00226 00227 // randomize the port if we are using certificates... 00228 if (this->m_options->getServerCertificateSize() > 0) { 00229 // Randomizing listening port for Certificate mode connectivity 00230 srand(time(NULL)); 00231 listening_port = rand() % 65535 + 12345; 00232 } 00233 00234 // DEBUG 00235 //this->logger()->log("Connector::Endpoint: listening port: %d",listening_port); 00236 00237 // Socket protocol type: TCP or UDP 00238 M2MInterface::BindingMode socket_protocol_type = M2MInterface::TCP; 00239 if (this->m_options->getCoAPConnectionType() == COAP_TCP) socket_protocol_type = M2MInterface::TCP; 00240 00241 // Socket address type: IPv4 or IPv6 00242 M2MInterface::NetworkStack socket_address_type = M2MInterface::LwIP_IPv4; 00243 if (this->m_options->getIPAddressType() == IP_ADDRESS_TYPE_IPV6) { 00244 // IPv6 mode for the socket addressing type... 00245 socket_address_type = M2MInterface::LwIP_IPv6; 00246 00247 #if defined (IPV4_OVERRIDE) 00248 // OVERRIDE (until patched...) 00249 this->logger()->log("Connector::Endpoint: Socket Address Type: IPv4 (IPv6 OVERRIDE)"); 00250 socket_address_type = M2MInterface::LwIP_IPv4; 00251 #endif 00252 } 00253 00254 // DEBUG 00255 if (socket_protocol_type == M2MInterface::TCP) this->logger()->log("Connector::Endpoint: Socket Protocol: TCP"); 00256 if (socket_protocol_type == M2MInterface::UDP) this->logger()->log("Connector::Endpoint: Socket Protocol: UDP"); 00257 if (socket_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Connector::Endpoint: Socket Address Type: IPv4"); 00258 if (socket_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Connector::Endpoint: Socket Address Type: IPv6"); 00259 00260 // Create the endpoint M2MInterface instance 00261 this->m_endpoint_interface = M2MInterfaceFactory::create_interface(*this, 00262 (char *)this->m_options->getEndpointNodename().c_str(), // endpoint name 00263 (char *)this->m_options->getEndpointType().c_str(), // endpoint type 00264 (int32_t)this->m_options->getLifetime(), // registration lifetime (in seconds) 00265 listening_port, // listening port (ephemeral...) 00266 (char *)this->m_options->getDomain().c_str(), // endpoint domain 00267 socket_protocol_type, // Socket protocol type: UDP or TCP... 00268 socket_address_type, // Socket addressing type: IPv4 or IPv6 00269 CONTEXT_ADDRESS_STRING // context address string (mbedConnectorInterface.h) 00270 ); 00271 00272 // bind LWIP network interface pointer... 00273 if (__network_interface != NULL && this->m_endpoint_interface != NULL) { 00274 this->logger()->log("Connector::Endpoint: binding LWIP network instance (Connector)..."); 00275 this->m_endpoint_interface->set_platform_network_handler((void *)__network_interface); 00276 } 00277 } 00278 #endif 00279 00280 // mbed-client: createEndpointSecurityInstance() 00281 M2MSecurity *Endpoint::createEndpointSecurityInstance() { 00282 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00283 // internalized... not used. 00284 return NULL; 00285 #else 00286 // Creates register server object with mbed device server address and other parameters 00287 M2MSecurity *server = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); 00288 if (server != NULL) { 00289 const String url = this->m_options->getConnectorURL(); 00290 server->set_resource_value(M2MSecurity::M2MServerUri, url); 00291 server->set_resource_value(M2MSecurity::BootstrapServer, false); 00292 server->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); 00293 server->set_resource_value(M2MSecurity::ServerPublicKey,this->m_options->getServerCertificate(),this->m_options->getServerCertificateSize()); 00294 server->set_resource_value(M2MSecurity::PublicKey,this->m_options->getClientCertificate(),this->m_options->getClientCertificateSize()); 00295 server->set_resource_value(M2MSecurity::Secretkey,this->m_options->getClientKey(),this->m_options->getClientKeySize()); 00296 } 00297 return server; 00298 #endif 00299 } 00300 00301 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00302 // mbed-cloud-client: Callback from mbed client stack if any error is encountered 00303 void Endpoint::on_error(int error_code) { 00304 char *error = (char *)"No Error"; 00305 switch(error_code) { 00306 case 0x01: 00307 error = (char *)"MbedCloudClient::IdentityError"; 00308 break; 00309 case 0x02: 00310 error = (char *)"MbedCloudClient::IdentityInvalidParameter"; 00311 break; 00312 case 0x03: 00313 error = (char *)"MbedCloudClient::IdentityOutofMemory"; 00314 break; 00315 case 0x04: 00316 error = (char *)"MbedCloudClient::IdentityProvisioningError"; 00317 break; 00318 case 0x05: 00319 error = (char *)"MbedCloudClient::IdentityInvalidSessionID"; 00320 break; 00321 case 0x06: 00322 error = (char *)"MbedCloudClient::IdentityNetworkError"; 00323 break; 00324 case 0x07: 00325 error = (char *)"MbedCloudClient::IdentityInvalidMessageType"; 00326 break; 00327 case 0x08: 00328 error = (char *)"MbedCloudClient::IdentityInvalidMessageSize"; 00329 break; 00330 case 0x09: 00331 error = (char *)"MbedCloudClient::IdentityCertOrKeyNotFound"; 00332 break; 00333 case 0x0A: 00334 error = (char *)"MbedCloudClient::IdentityRetransmissionError"; 00335 break; 00336 case 0x30: 00337 error = (char *)"MbedCloudClient::ConnectErrorNone"; 00338 break; 00339 case 0x31: 00340 error = (char *)"MbedCloudClient::ConnectAlreadyExists"; 00341 break; 00342 case 0x32: 00343 error = (char *)"MbedCloudClient::ConnectBootstrapFailed"; 00344 break; 00345 case 0x33: 00346 error = (char *)"MbedCloudClient::ConnectInvalidParameters"; 00347 break; 00348 case 0x34: 00349 error = (char *)"MbedCloudClient::ConnectNotRegistered"; 00350 break; 00351 case 0x35: 00352 error = (char *)"MbedCloudClient::ConnectTimeout"; 00353 break; 00354 case 0x36: 00355 error = (char *)"MbedCloudClient::ConnectNetworkError"; 00356 break; 00357 case 0x37: 00358 error = (char *)"MbedCloudClient::ConnectResponseParseFailed"; 00359 break; 00360 case 0x38: 00361 error = (char *)"MbedCloudClient::ConnectUnknownError"; 00362 break; 00363 case 0x39: 00364 error = (char *)"MbedCloudClient::ConnectMemoryFail"; 00365 break; 00366 case 0x3A: 00367 error = (char *)"MbedCloudClient::ConnectNotAllowed"; 00368 break; 00369 case 0x3B: 00370 error = (char *)"MbedCloudClient::ConnectSecureConnectionFailed"; 00371 break; 00372 case 0x3C: 00373 error = (char *)"MbedCloudClient::ConnectDnsResolvingFailed"; 00374 break; 00375 default: 00376 error = (char *)"UNKNOWN"; 00377 } 00378 printf("Connector::Endpoint(Cloud) Error(%x): %s\r\n",error_code,error); 00379 } 00380 #endif 00381 00382 // mbed-client: Callback from mbed client stack if any error is encountered 00383 void Endpoint::error(M2MInterface::Error error) { 00384 switch(error){ 00385 case M2MInterface::AlreadyExists: 00386 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::AlreadyExists"); 00387 break; 00388 case M2MInterface::BootstrapFailed: 00389 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::BootstrapFailed"); 00390 break; 00391 case M2MInterface::InvalidParameters: 00392 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::InvalidParameters"); 00393 break; 00394 case M2MInterface::NotRegistered: 00395 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotRegistered"); 00396 break; 00397 case M2MInterface::Timeout: 00398 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::Timeout"); 00399 break; 00400 case M2MInterface::NetworkError: 00401 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NetworkError"); 00402 break; 00403 case M2MInterface::ResponseParseFailed: 00404 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::ResponseParseFailed"); 00405 break; 00406 case M2MInterface::UnknownError: 00407 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::UnknownError"); 00408 break; 00409 case M2MInterface::MemoryFail: 00410 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::MemoryFail"); 00411 break; 00412 case M2MInterface::NotAllowed: 00413 this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotAllowed"); 00414 break; 00415 default: 00416 break; 00417 } 00418 } 00419 00420 // re-register the endpoint 00421 void Endpoint::re_register_endpoint() { 00422 if (this->m_endpoint_interface != NULL) { 00423 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00424 // DEBUG 00425 this->logger()->log("Connector::Endpoint(Cloud): re-register endpoint..."); 00426 #else 00427 this->m_endpoint_interface->update_registration(this->m_endpoint_security,this->m_options->getLifetime()); 00428 #endif 00429 } 00430 } 00431 00432 // de-register endpoint 00433 void Endpoint::de_register_endpoint(void) { 00434 if (this->m_endpoint_interface != NULL) { 00435 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00436 // DEBUG 00437 this->logger()->log("Connector::Endpoint(Cloud): de-registering endpoint..."); 00438 this->m_endpoint_interface->close(); 00439 #else 00440 // de-register endpoint 00441 this->logger()->log("Connector::Endpoint: de-registering endpoint..."); 00442 if (this->m_csi != NULL) { 00443 this->m_csi->begin_object_unregistering((void *)this); 00444 } 00445 else { 00446 this->m_endpoint_interface->unregister_object(NULL); 00447 } 00448 #endif 00449 } 00450 } 00451 00452 // register the endpoint 00453 void Endpoint::register_endpoint(M2MSecurity *endpoint_security, M2MObjectList endpoint_objects) { 00454 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00455 if (this->m_endpoint_interface != NULL) { 00456 this->logger()->log("Connector::Endpoint(Cloud): registering endpoint..."); 00457 this->m_endpoint_interface->add_objects(endpoint_objects); 00458 } 00459 #else 00460 if (this->m_endpoint_interface != NULL && endpoint_security != NULL && endpoint_objects.size() > 0) { 00461 // register endpoint 00462 this->logger()->log("Connector::Endpoint: registering endpoint..."); 00463 this->m_endpoint_interface->register_object(endpoint_security, endpoint_objects); 00464 } 00465 #endif 00466 } 00467 00468 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00469 // object registered 00470 void Endpoint::on_registered() { 00471 if (__endpoint != NULL) { 00472 printf("Connector::Endpoint(Cloud): on_registered()\r\n"); 00473 __endpoint->object_registered(); 00474 } 00475 } 00476 00477 // registration updated 00478 void Endpoint::on_registration_updated() { 00479 if (__endpoint != NULL) { 00480 printf("Connector::Endpoint(Cloud): on_registration_updated()\r\n"); 00481 __endpoint->registration_updated(); 00482 } 00483 } 00484 00485 // object unregistered 00486 void Endpoint::on_unregistered() { 00487 if (__endpoint != NULL) { 00488 printf("Connector::Endpoint(Cloud): on_unregistered()\r\n"); 00489 __endpoint->object_unregistered(__endpoint->getSecurityInstance()); 00490 } 00491 } 00492 #endif 00493 00494 // object registered 00495 void Endpoint::object_registered(M2MSecurity *security, const M2MServer &server) { 00496 this->object_registered((void *)security,(void *)&server); 00497 } 00498 00499 // registration updated 00500 void Endpoint::registration_updated(M2MSecurity *security, const M2MServer &server) { 00501 this->registration_updated((void *)security,(void *)&server); 00502 } 00503 00504 // object unregistered 00505 void Endpoint::object_unregistered(M2MSecurity *security) { 00506 // DEBUG 00507 this->logger()->log("Connector::Endpoint: endpoint de-registered."); 00508 00509 // no longer connected/registered 00510 this->m_registered = false; 00511 this->m_connected = false; 00512 00513 // stop all observers... 00514 this->stopObservations(); 00515 00516 // invoke ConnectionHandler if we have one... 00517 if (this->m_csi != NULL) { 00518 this->m_csi->object_unregistered((void *)this,(void *)security); 00519 } 00520 00521 // halt the main event loop... we are done. 00522 net_shutdown_endpoint(); 00523 } 00524 00525 // bootstrap done 00526 void Endpoint::bootstrap_done(M2MSecurity *security) { 00527 this->logger()->log("Connector::Endpoint: endpoint bootstrapped."); 00528 if (this->m_csi != NULL) { 00529 this->m_csi->bootstrapped((void *)this,(void *)security); 00530 } 00531 } 00532 00533 // object registered 00534 void Endpoint::object_registered(void *security,void *server) { 00535 this->logger()->log("Connector::Endpoint: endpoint registered."); 00536 this->m_connected = true; 00537 this->m_registered = true; 00538 if (this->m_csi != NULL) { 00539 this->m_csi->object_registered((void *)this,security,server); 00540 } 00541 } 00542 00543 // registration updated 00544 void Endpoint::registration_updated(void *security,void *server) { 00545 this->logger()->log("Connector::Endpoint: endpoint re-registered."); 00546 this->m_connected = true; 00547 this->m_registered = true; 00548 if (this->m_csi != NULL) { 00549 this->m_csi->registration_updated((void *)this,security,server); 00550 } 00551 } 00552 00553 // resource value updated 00554 void Endpoint::value_updated(M2MBase *base, M2MBase::BaseType type) { 00555 // Lookup the resource and invoke process() on it... 00556 DynamicResource *target_res = this->lookupDynamicResource(base); 00557 if (target_res != NULL) { 00558 // DEBUG 00559 //this->logger()->log("Value Updated (Custom Resource)"); 00560 00561 // its a custom resource... 00562 target_res->process(base->operation(),type); 00563 } 00564 else { 00565 // DEBUG 00566 //this->logger()->log("Value Updated (Device Manager)"); 00567 00568 // let DeviceManager handle it 00569 ((DeviceManager *)this->m_device_manager)->process(base,type); 00570 } 00571 00572 // CSI 00573 if (this->m_csi != NULL) { 00574 this->m_csi->value_updated((void *)this,(void *)base,(int)type); 00575 } 00576 } 00577 00578 // lookup which DynamicResource cooresponds to a given M2MBase instance... 00579 DynamicResource *Endpoint::lookupDynamicResource(M2MBase *base) { 00580 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList(); 00581 for(int i=0; i<(int)dynamic_resources->size(); ++i) { 00582 M2MBase *t = (M2MBase *)dynamic_resources->at(i)->getResource(); 00583 if (t == base) { 00584 return dynamic_resources->at(i); 00585 } 00586 } 00587 return NULL; 00588 } 00589 00590 // build out the endpoint 00591 void Endpoint::buildEndpoint() 00592 { 00593 // initialize as an mbed-client 00594 this->createEndpointInterface(); 00595 00596 // Create our server instance 00597 this->setSecurityInstance(this->createEndpointSecurityInstance()); 00598 00599 // We now have to bind our device resources 00600 if (this->m_device_manager != NULL) { 00601 // DEBUG 00602 this->logger()->log("Connector::Endpoint::build(): plumbing the device management objects and resources..."); 00603 00604 // bind the device manager 00605 ((DeviceManager *)this->m_device_manager)->bind(); 00606 00607 // push back the Device Resources Object 00608 if (this->m_options->getDeviceResourcesObject() != NULL) { 00609 // DEBUG 00610 this->logger()->log("Connector::Endpoint::build(): plumbing device resources object..."); 00611 00612 // push back the device resources object 00613 this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getDeviceResourcesObject()); 00614 } 00615 else { 00616 // unable to plumb device manager 00617 this->logger()->log("Connector::Endpoint::build(): Unable to plumb device resources. Not installing device resource object..."); 00618 } 00619 00620 // push back the Firmware Resources Object 00621 if (this->m_options->getFirmwareResourcesObject() != NULL) { 00622 // DEBUG 00623 this->logger()->log("Connector::Endpoint::build(): plumbing firmware resources object..."); 00624 00625 // push back the firmware resources object 00626 this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getFirmwareResourcesObject()); 00627 } 00628 else { 00629 // unable to plumb firmware manager 00630 this->logger()->log("Connector::Endpoint::build(): Unable to plumb firmware resources. Not installing firmware resource object..."); 00631 } 00632 } 00633 else { 00634 // no device manager installed 00635 this->logger()->log("Connector::Endpoint::build(): No device manager installed."); 00636 } 00637 00638 // Loop through Static Resources and bind each of them... 00639 this->logger()->log("Connector::Endpoint::build(): adding static resources..."); 00640 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList(); 00641 for(int i=0; i<(int)static_resources->size(); ++i) { 00642 this->logger()->log("Connector::Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str()); 00643 static_resources->at(i)->bind(this); 00644 } 00645 00646 // Loop through Dynamic Resources and bind each of them... 00647 this->logger()->log("Connector::Endpoint::build(): adding dynamic resources..."); 00648 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList(); 00649 for(int i=0; i<(int)dynamic_resources->size(); ++i) { 00650 this->logger()->log("Connector::Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str()); 00651 dynamic_resources->at(i)->bind(this); 00652 } 00653 00654 // Get the ObjectList from the ObjectInstanceManager... 00655 NamedPointerList list = this->getObjectInstanceManager()->getObjectList(); 00656 00657 // DEBUG 00658 //this->logger()->log("Endpoint::build(): All Resources bound. Number of Objects in list: %d",list.size()); 00659 00660 // add all of the object instances we have created... 00661 for(int i=0;i<(int)list.size();++i) { 00662 // DEBUG 00663 //this->logger()->log("Endpoint::build(): adding Object Instance with ObjID: %s...",list.at(i).name().c_str()); 00664 00665 // push back the object instance... 00666 this->m_endpoint_object_list.push_back((M2MObject *)(list.at(i).ptr())); 00667 } 00668 } 00669 00670 // stop underlying observation mechanisms 00671 void Endpoint::stopObservations() { 00672 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList(); 00673 for(int i=0; i<(int)dynamic_resources->size(); ++i) { 00674 if (dynamic_resources->at(i)->isObservable() == true) { 00675 ResourceObserver *observer = (ResourceObserver *)dynamic_resources->at(i)->getObserver(); 00676 if (observer != NULL) { 00677 this->logger()->log("Connector::Endpoint::stopObservations(): stopping resource observer for: [%s]...",dynamic_resources->at(i)->getFullName().c_str()); 00678 observer->halt(); 00679 } 00680 } 00681 } 00682 } 00683 00684 // underlying network is connected (SET) 00685 void Endpoint::isConnected(bool connected) { 00686 this->m_connected = connected; 00687 } 00688 00689 // underlying network is connected (GET) 00690 bool Endpoint::isConnected() { 00691 return this->m_connected; 00692 } 00693 00694 // Registered with mDC/mDS 00695 bool Endpoint::isRegistered() { 00696 return this->m_registered; 00697 } 00698 00699 // Set the ConnectionStatusInterface 00700 void Endpoint::setConnectionStatusInterfaceImpl(ConnectionStatusInterface *csi) { 00701 this->m_csi = csi; 00702 } 00703 00704 // Set our ObjectInstanceManager 00705 void Endpoint::setObjectInstanceManager(ObjectInstanceManager *oim) { 00706 this->m_oim = oim; 00707 } 00708 00709 // Get our ObjectInstanceManager 00710 ObjectInstanceManager *Endpoint::getObjectInstanceManager() { 00711 return this->m_oim; 00712 } 00713 00714 // our logger 00715 Logger *Endpoint::logger() 00716 { 00717 return this->m_logger; 00718 } 00719 00720 } // namespace Connector
Generated on Wed Jul 13 2022 01:24:02 by
