Doug Anson / mbedConnectorInterface

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
ansond
Date:
Wed Jan 28 14:43:46 2015 +0000
Revision:
5:a929d65eb385
Parent:
2:853f9ecc12df
updates should be close to final

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
ansond 5:a929d65eb385 28 // Tasklet ID
ansond 5:a929d65eb385 29 int main_tasklet_id = -1;
ansond 5:a929d65eb385 30
sam_grove 2:853f9ecc12df 31 // Connector namespace
sam_grove 2:853f9ecc12df 32 namespace Connector {
sam_grove 2:853f9ecc12df 33
sam_grove 2:853f9ecc12df 34 // Constructor
sam_grove 2:853f9ecc12df 35 Endpoint::Endpoint(const Logger *logger, const Options *options)
sam_grove 2:853f9ecc12df 36 {
sam_grove 2:853f9ecc12df 37 this->m_logger = (Logger *)logger;
sam_grove 2:853f9ecc12df 38 this->m_options = (Options *)options;
sam_grove 2:853f9ecc12df 39 }
sam_grove 2:853f9ecc12df 40
sam_grove 2:853f9ecc12df 41 // Copy Constructor
sam_grove 2:853f9ecc12df 42 Endpoint::Endpoint(const Endpoint &ep)
sam_grove 2:853f9ecc12df 43 {
sam_grove 2:853f9ecc12df 44 this->m_logger = ep.m_logger;
sam_grove 2:853f9ecc12df 45 this->m_options = ep.m_options;
sam_grove 2:853f9ecc12df 46 }
sam_grove 2:853f9ecc12df 47
sam_grove 2:853f9ecc12df 48 // Destructor
sam_grove 2:853f9ecc12df 49 Endpoint::~Endpoint()
sam_grove 2:853f9ecc12df 50 {
sam_grove 2:853f9ecc12df 51 }
sam_grove 2:853f9ecc12df 52
ansond 5:a929d65eb385 53 // Plumb the network
ansond 5:a929d65eb385 54 void Endpoint::plumbNetwork(bool canActAsRouterNode) {
ansond 5:a929d65eb385 55 // call into mbedEndpointLib directly for now... (TODO: wont be able to (re)set MAC address in OptionsBuilder as its already been plumbed here...)
sam_grove 2:853f9ecc12df 56 init_network(canActAsRouterNode);
sam_grove 2:853f9ecc12df 57 }
sam_grove 2:853f9ecc12df 58
sam_grove 2:853f9ecc12df 59 // initialize the endpoint
sam_grove 2:853f9ecc12df 60 void Endpoint::initialize()
sam_grove 2:853f9ecc12df 61 {
sam_grove 2:853f9ecc12df 62 // Create the NSDL Resource Pointer...
ansond 5:a929d65eb385 63 this->logger()->log("Endpoint::initialize(): initializing NSP resource pointer...");
sam_grove 2:853f9ecc12df 64 sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
sam_grove 2:853f9ecc12df 65 if(!resource_ptr) return;
sam_grove 2:853f9ecc12df 66 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
sam_grove 2:853f9ecc12df 67
sam_grove 2:853f9ecc12df 68 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
sam_grove 2:853f9ecc12df 69 if(!resource_ptr->resource_parameters_ptr) {
ansond 0:b438482ebbfc 70 nsdl_free(resource_ptr);
ansond 0:b438482ebbfc 71 return;
ansond 0:b438482ebbfc 72 }
sam_grove 2:853f9ecc12df 73 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
sam_grove 2:853f9ecc12df 74
sam_grove 2:853f9ecc12df 75 // Loop through Static Resources and bind each of them...
ansond 5:a929d65eb385 76 this->logger()->log("Endpoint::initialize(): adding static resources...");
sam_grove 2:853f9ecc12df 77 const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
sam_grove 2:853f9ecc12df 78 for(int i=0; i<static_resources->size(); ++i) {
ansond 5:a929d65eb385 79 this->logger()->log("Endpoint::initialize(): binding static resource: [%s]...",static_resources->at(i)->getName().c_str());
sam_grove 2:853f9ecc12df 80 static_resources->at(i)->bind(resource_ptr);
sam_grove 2:853f9ecc12df 81 }
sam_grove 2:853f9ecc12df 82
sam_grove 2:853f9ecc12df 83 // Loop through Dynamic Resources and bind each of them...
ansond 5:a929d65eb385 84 this->logger()->log("Endpoint::initialize(): adding dynamic resources...");
sam_grove 2:853f9ecc12df 85 const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
sam_grove 2:853f9ecc12df 86 for(int i=0; i<dynamic_resources->size(); ++i) {
ansond 5:a929d65eb385 87 this->logger()->log("Endpoint::initialize(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getName().c_str());
sam_grove 2:853f9ecc12df 88 dynamic_resources->at(i)->bind(resource_ptr);
sam_grove 2:853f9ecc12df 89 }
sam_grove 2:853f9ecc12df 90
sam_grove 2:853f9ecc12df 91 // initialize the Network
sam_grove 2:853f9ecc12df 92 this->initNetwork();
sam_grove 2:853f9ecc12df 93
sam_grove 2:853f9ecc12df 94 // clean up
sam_grove 2:853f9ecc12df 95 nsdl_free(resource_ptr->resource_parameters_ptr);
sam_grove 2:853f9ecc12df 96 nsdl_free(resource_ptr);
sam_grove 2:853f9ecc12df 97 }
sam_grove 2:853f9ecc12df 98
sam_grove 2:853f9ecc12df 99 // initialize the NSDL Network
sam_grove 2:853f9ecc12df 100 void Endpoint::initNetwork()
sam_grove 2:853f9ecc12df 101 {
sam_grove 2:853f9ecc12df 102 // register with NSP
ansond 5:a929d65eb385 103 this->logger()->log("Calling NSP_registration()...");
sam_grove 2:853f9ecc12df 104 NSP_registration();
ansond 5:a929d65eb385 105 this->logger()->log("NSP_registration() completed");
sam_grove 2:853f9ecc12df 106 }
sam_grove 2:853f9ecc12df 107
ansond 5:a929d65eb385 108 // Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
sam_grove 2:853f9ecc12df 109 void Endpoint::start()
ansond 5:a929d65eb385 110 {
sam_grove 2:853f9ecc12df 111 // mbedEndpointLib tasklet creation...
ansond 5:a929d65eb385 112 main_tasklet_id = arm_ns_tasklet_create(&tasklet_main);
sam_grove 2:853f9ecc12df 113 if(main_tasklet_id < 0) {
sam_grove 2:853f9ecc12df 114 //Tasklet cerate fail
ansond 5:a929d65eb385 115 std::printf("startTasklet: Tasklet creation failed...\r\n");
sam_grove 2:853f9ecc12df 116 return;
sam_grove 2:853f9ecc12df 117 }
sam_grove 2:853f9ecc12df 118
sam_grove 2:853f9ecc12df 119 // mbedEndpointLib event dispatching
ansond 5:a929d65eb385 120 std::printf("startTasklet: Beginning event dispatch...\r\n");
sam_grove 2:853f9ecc12df 121 event_dispatch();
sam_grove 2:853f9ecc12df 122 return;
sam_grove 2:853f9ecc12df 123 }
sam_grove 2:853f9ecc12df 124
ansond 5:a929d65eb385 125 // our logger
ansond 5:a929d65eb385 126 Logger *Endpoint::logger()
ansond 5:a929d65eb385 127 {
ansond 5:a929d65eb385 128 return this->m_logger;
ansond 5:a929d65eb385 129 }
ansond 5:a929d65eb385 130
sam_grove 2:853f9ecc12df 131 } // namespace Connector