Doug Anson / mbedConnectorInterface

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
sam_grove
Date:
Tue Jan 27 23:41:34 2015 +0000
Revision:
2:853f9ecc12df
Parent:
1:cabdd0350707
Child:
5:a929d65eb385
Use auto-format on code and add markup to render class documentation

Who changed what in which revision?

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