Doug Anson / mbedConnectorInterface

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
ansond
Date:
Tue Jan 27 22:23:51 2015 +0000
Revision:
0:b438482ebbfc
Child:
1:cabdd0350707
initial check in

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
ansond 0:b438482ebbfc 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 */
ansond 0:b438482ebbfc 22
ansond 0:b438482ebbfc 23 #include "Endpoint.h"
ansond 0:b438482ebbfc 24
ansond 0:b438482ebbfc 25 // support for temporary mbedEndpointLib calls...
ansond 0:b438482ebbfc 26 #include "mbedEndpointLib.h"
ansond 0:b438482ebbfc 27
ansond 0:b438482ebbfc 28 // Connector namespace
ansond 0:b438482ebbfc 29 namespace Connector {
ansond 0:b438482ebbfc 30 // Constructor
ansond 0:b438482ebbfc 31 Endpoint::Endpoint(const Logger *logger, const Options *options) {
ansond 0:b438482ebbfc 32 this->m_logger = (Logger *)logger;
ansond 0:b438482ebbfc 33 this->m_options = (Options *)options;
ansond 0:b438482ebbfc 34 }
ansond 0:b438482ebbfc 35
ansond 0:b438482ebbfc 36 // Copy Constructor
ansond 0:b438482ebbfc 37 Endpoint::Endpoint(const Endpoint &ep) {
ansond 0:b438482ebbfc 38 this->m_logger = ep.m_logger;
ansond 0:b438482ebbfc 39 this->m_options = ep.m_options;
ansond 0:b438482ebbfc 40 }
ansond 0:b438482ebbfc 41
ansond 0:b438482ebbfc 42 // Destructor
ansond 0:b438482ebbfc 43 Endpoint::~Endpoint() {
ansond 0:b438482ebbfc 44 }
ansond 0:b438482ebbfc 45
ansond 0:b438482ebbfc 46 // plumb network
ansond 0:b438482ebbfc 47 void Endpoint::plumbNetwork(bool canActAsRouterNode) {
ansond 0:b438482ebbfc 48 // call into mbedEndpointLib for now...
ansond 0:b438482ebbfc 49 init_network(canActAsRouterNode);
ansond 0:b438482ebbfc 50 }
ansond 0:b438482ebbfc 51
ansond 0:b438482ebbfc 52 // initialize the endpoint
ansond 0:b438482ebbfc 53 void Endpoint::initialize() {
ansond 0:b438482ebbfc 54 // Create the NSDL Resource Pointer...
ansond 0:b438482ebbfc 55 std::printf("Endpoint::initialize(): initializing NSP resource pointer...\r\n");
ansond 0:b438482ebbfc 56 sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
ansond 0:b438482ebbfc 57 if(!resource_ptr) return;
ansond 0:b438482ebbfc 58 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
ansond 0:b438482ebbfc 59
ansond 0:b438482ebbfc 60 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
ansond 0:b438482ebbfc 61 if(!resource_ptr->resource_parameters_ptr) {
ansond 0:b438482ebbfc 62 nsdl_free(resource_ptr);
ansond 0:b438482ebbfc 63 return;
ansond 0:b438482ebbfc 64 }
ansond 0:b438482ebbfc 65 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
ansond 0:b438482ebbfc 66
ansond 0:b438482ebbfc 67 // Loop through Static Resources and bind each of them...
ansond 0:b438482ebbfc 68 std::printf("Endpoint::initialize(): adding static resources...\r\n");
ansond 0:b438482ebbfc 69 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
ansond 0:b438482ebbfc 70 for(int i=0;i<static_resources->size();++i) {
ansond 0:b438482ebbfc 71 std::printf("Endpoint::initialize(): binding static resource: [%s]...\r\n",static_resources->at(i)->getName().c_str());
ansond 0:b438482ebbfc 72 static_resources->at(i)->bind(resource_ptr);
ansond 0:b438482ebbfc 73 }
ansond 0:b438482ebbfc 74
ansond 0:b438482ebbfc 75 // Loop through Dynamic Resources and bind each of them...
ansond 0:b438482ebbfc 76 std::printf("Endpoint::initialize(): adding dynamic resources...\r\n");
ansond 0:b438482ebbfc 77 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
ansond 0:b438482ebbfc 78 for(int i=0;i<dynamic_resources->size();++i) {
ansond 0:b438482ebbfc 79 std::printf("Endpoint::initialize(): binding dynamic resource: [%s]...\r\n",dynamic_resources->at(i)->getName().c_str());
ansond 0:b438482ebbfc 80 dynamic_resources->at(i)->bind(resource_ptr);
ansond 0:b438482ebbfc 81 }
ansond 0:b438482ebbfc 82
ansond 0:b438482ebbfc 83 // initialize the Network
ansond 0:b438482ebbfc 84 this->initNetwork();
ansond 0:b438482ebbfc 85
ansond 0:b438482ebbfc 86 // clean up
ansond 0:b438482ebbfc 87 nsdl_free(resource_ptr->resource_parameters_ptr);
ansond 0:b438482ebbfc 88 nsdl_free(resource_ptr);
ansond 0:b438482ebbfc 89 }
ansond 0:b438482ebbfc 90
ansond 0:b438482ebbfc 91 // initialize the NSDL Network
ansond 0:b438482ebbfc 92 void Endpoint::initNetwork() {
ansond 0:b438482ebbfc 93 // register with NSP
ansond 0:b438482ebbfc 94 DBG("Calling NSP_registration()...\r\n");
ansond 0:b438482ebbfc 95 NSP_registration();
ansond 0:b438482ebbfc 96 DBG("NSP_registration() completed\r\n");
ansond 0:b438482ebbfc 97 }
ansond 0:b438482ebbfc 98
ansond 0:b438482ebbfc 99 // start the endpoint's main loop
ansond 0:b438482ebbfc 100 void Endpoint::startTasklet() {
ansond 0:b438482ebbfc 101 // mbedEndpointLib tasklet creation...
ansond 0:b438482ebbfc 102 int main_tasklet_id = arm_ns_tasklet_create(&tasklet_main);
ansond 0:b438482ebbfc 103 if(main_tasklet_id < 0) {
ansond 0:b438482ebbfc 104 //Tasklet cerate fail
ansond 0:b438482ebbfc 105 DBG("startTasklet: Tasklet creation failed...\r\n");
ansond 0:b438482ebbfc 106 return;
ansond 0:b438482ebbfc 107 }
ansond 0:b438482ebbfc 108
ansond 0:b438482ebbfc 109 // mbedEndpointLib event dispatching
ansond 0:b438482ebbfc 110 DBG("startTasklet: Beginning event dispatch...\r\n");
ansond 0:b438482ebbfc 111 event_dispatch();
ansond 0:b438482ebbfc 112 return;
ansond 0:b438482ebbfc 113 }
ansond 0:b438482ebbfc 114 }