mbed Connector Endpoint interface. This interface permits a mbed endpoint to easily setup MDS resources and emit those resources to an MDS server.

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
ansond
Date:
Mon Feb 02 06:36:58 2015 +0000
Revision:
12:c068a2d3e8fe
Parent:
9:d094cfc650c3
Child:
13:b0b2f1ae4c30
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 8:b518d1c01df1 1 /**
ansond 8:b518d1c01df1 2 * @file ConnectorEndpoint.cpp
ansond 8:b518d1c01df1 3 * @brief mbed CoAP Endpoint base class
ansond 8:b518d1c01df1 4 * @author Doug Anson/Chris Paola
ansond 8:b518d1c01df1 5 * @version 1.0
ansond 8:b518d1c01df1 6 * @see
ansond 8:b518d1c01df1 7 *
ansond 8:b518d1c01df1 8 * Copyright (c) 2014
ansond 8:b518d1c01df1 9 *
ansond 8:b518d1c01df1 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 8:b518d1c01df1 11 * you may not use this file except in compliance with the License.
ansond 8:b518d1c01df1 12 * You may obtain a copy of the License at
ansond 8:b518d1c01df1 13 *
ansond 8:b518d1c01df1 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 8:b518d1c01df1 15 *
ansond 8:b518d1c01df1 16 * Unless required by applicable law or agreed to in writing, software
ansond 8:b518d1c01df1 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 8:b518d1c01df1 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 8:b518d1c01df1 19 * See the License for the specific language governing permissions and
ansond 8:b518d1c01df1 20 * limitations under the License.
ansond 8:b518d1c01df1 21 */
ansond 8:b518d1c01df1 22
ansond 8:b518d1c01df1 23 #include "ConnectorEndpoint.h"
ansond 8:b518d1c01df1 24
ansond 9:d094cfc650c3 25 // lower level network stubs integration
ansond 9:d094cfc650c3 26 #include "mbedEndpointNetworkStubs.h"
ansond 8:b518d1c01df1 27
ansond 8:b518d1c01df1 28 // Connector namespace
ansond 8:b518d1c01df1 29 namespace Connector {
ansond 8:b518d1c01df1 30
ansond 8:b518d1c01df1 31 // Constructor
ansond 8:b518d1c01df1 32 Endpoint::Endpoint(const Logger *logger, const Options *options)
ansond 8:b518d1c01df1 33 {
ansond 8:b518d1c01df1 34 this->m_logger = (Logger *)logger;
ansond 8:b518d1c01df1 35 this->m_options = (Options *)options;
ansond 8:b518d1c01df1 36 }
ansond 8:b518d1c01df1 37
ansond 8:b518d1c01df1 38 // Copy Constructor
ansond 8:b518d1c01df1 39 Endpoint::Endpoint(const Endpoint &ep)
ansond 8:b518d1c01df1 40 {
ansond 8:b518d1c01df1 41 this->m_logger = ep.m_logger;
ansond 8:b518d1c01df1 42 this->m_options = ep.m_options;
ansond 8:b518d1c01df1 43 }
ansond 8:b518d1c01df1 44
ansond 8:b518d1c01df1 45 // Destructor
ansond 8:b518d1c01df1 46 Endpoint::~Endpoint()
ansond 8:b518d1c01df1 47 {
ansond 8:b518d1c01df1 48 }
ansond 8:b518d1c01df1 49
ansond 8:b518d1c01df1 50 // Plumb the network
ansond 8:b518d1c01df1 51 void Endpoint::plumbNetwork(bool canActAsRouterNode) {
ansond 9:d094cfc650c3 52 // call into our network stubs to plumb the network...
ansond 9:d094cfc650c3 53 net_stubs_plumb_network(canActAsRouterNode);
ansond 8:b518d1c01df1 54 }
ansond 8:b518d1c01df1 55
ansond 8:b518d1c01df1 56 // initialize the endpoint
ansond 8:b518d1c01df1 57 void Endpoint::initialize()
ansond 8:b518d1c01df1 58 {
ansond 12:c068a2d3e8fe 59 // initialize NSDL
ansond 12:c068a2d3e8fe 60 nsdl_init();
ansond 12:c068a2d3e8fe 61
ansond 8:b518d1c01df1 62 // Create the NSDL Resource Pointer...
ansond 8:b518d1c01df1 63 this->logger()->log("Endpoint::initialize(): initializing NSP resource pointer...");
ansond 8:b518d1c01df1 64 sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
ansond 8:b518d1c01df1 65 if(!resource_ptr) return;
ansond 8:b518d1c01df1 66 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
ansond 8:b518d1c01df1 67
ansond 8:b518d1c01df1 68 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
ansond 8:b518d1c01df1 69 if(!resource_ptr->resource_parameters_ptr) {
ansond 8:b518d1c01df1 70 nsdl_free(resource_ptr);
ansond 8:b518d1c01df1 71 return;
ansond 8:b518d1c01df1 72 }
ansond 8:b518d1c01df1 73 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
ansond 8:b518d1c01df1 74
ansond 8:b518d1c01df1 75 // Loop through Static Resources and bind each of them...
ansond 8:b518d1c01df1 76 this->logger()->log("Endpoint::initialize(): adding static resources...");
ansond 8:b518d1c01df1 77 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
ansond 8:b518d1c01df1 78 for(int i=0; i<static_resources->size(); ++i) {
ansond 8:b518d1c01df1 79 this->logger()->log("Endpoint::initialize(): binding static resource: [%s]...",static_resources->at(i)->getName().c_str());
ansond 8:b518d1c01df1 80 static_resources->at(i)->bind(resource_ptr);
ansond 8:b518d1c01df1 81 }
ansond 8:b518d1c01df1 82
ansond 8:b518d1c01df1 83 // Loop through Dynamic Resources and bind each of them...
ansond 8:b518d1c01df1 84 this->logger()->log("Endpoint::initialize(): adding dynamic resources...");
ansond 8:b518d1c01df1 85 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 8:b518d1c01df1 86 for(int i=0; i<dynamic_resources->size(); ++i) {
ansond 8:b518d1c01df1 87 this->logger()->log("Endpoint::initialize(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getName().c_str());
ansond 8:b518d1c01df1 88 dynamic_resources->at(i)->bind(resource_ptr);
ansond 8:b518d1c01df1 89 }
ansond 8:b518d1c01df1 90
ansond 12:c068a2d3e8fe 91 // register the endpoint
ansond 12:c068a2d3e8fe 92 net_stubs_register_endpoint();
ansond 8:b518d1c01df1 93
ansond 8:b518d1c01df1 94 // clean up
ansond 8:b518d1c01df1 95 nsdl_free(resource_ptr->resource_parameters_ptr);
ansond 8:b518d1c01df1 96 nsdl_free(resource_ptr);
ansond 8:b518d1c01df1 97 }
ansond 8:b518d1c01df1 98
ansond 8:b518d1c01df1 99 // Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
ansond 8:b518d1c01df1 100 void Endpoint::start()
ansond 8:b518d1c01df1 101 {
ansond 9:d094cfc650c3 102 // create the main loop plumbing for our lower network stack
ansond 9:d094cfc650c3 103 DBG("Endpoint::start(): creating main loop plumbing...\r\n");
ansond 9:d094cfc650c3 104 net_stubs_create_main_loop();
ansond 9:d094cfc650c3 105
ansond 9:d094cfc650c3 106 // call into the lower network stubs to start event processing
ansond 9:d094cfc650c3 107 DBG("Endpoint::start(): beginning main event loop...\r\n");
ansond 9:d094cfc650c3 108 net_stubs_begin_main_loop();
ansond 8:b518d1c01df1 109 }
ansond 8:b518d1c01df1 110
ansond 8:b518d1c01df1 111 // our logger
ansond 8:b518d1c01df1 112 Logger *Endpoint::logger()
ansond 8:b518d1c01df1 113 {
ansond 8:b518d1c01df1 114 return this->m_logger;
ansond 8:b518d1c01df1 115 }
ansond 8:b518d1c01df1 116
ansond 8:b518d1c01df1 117 } // namespace Connector