added max-age and content-format

Fork of mbedConnectorInterface by Doug Anson

Committer:
ansond
Date:
Sun Feb 01 14:54:18 2015 +0000
Revision:
8:b518d1c01df1
Child:
9:d094cfc650c3
renamed endpoint files due to name collisions in mbed network stack

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 8:b518d1c01df1 25 // support for temporary mbedEndpointLib calls...
ansond 8:b518d1c01df1 26 #include "mbedEndpointLib.h"
ansond 8:b518d1c01df1 27
ansond 8:b518d1c01df1 28 // Tasklet ID
ansond 8:b518d1c01df1 29 int main_tasklet_id = -1;
ansond 8:b518d1c01df1 30
ansond 8:b518d1c01df1 31 // Connector namespace
ansond 8:b518d1c01df1 32 namespace Connector {
ansond 8:b518d1c01df1 33
ansond 8:b518d1c01df1 34 // Constructor
ansond 8:b518d1c01df1 35 Endpoint::Endpoint(const Logger *logger, const Options *options)
ansond 8:b518d1c01df1 36 {
ansond 8:b518d1c01df1 37 this->m_logger = (Logger *)logger;
ansond 8:b518d1c01df1 38 this->m_options = (Options *)options;
ansond 8:b518d1c01df1 39 }
ansond 8:b518d1c01df1 40
ansond 8:b518d1c01df1 41 // Copy Constructor
ansond 8:b518d1c01df1 42 Endpoint::Endpoint(const Endpoint &ep)
ansond 8:b518d1c01df1 43 {
ansond 8:b518d1c01df1 44 this->m_logger = ep.m_logger;
ansond 8:b518d1c01df1 45 this->m_options = ep.m_options;
ansond 8:b518d1c01df1 46 }
ansond 8:b518d1c01df1 47
ansond 8:b518d1c01df1 48 // Destructor
ansond 8:b518d1c01df1 49 Endpoint::~Endpoint()
ansond 8:b518d1c01df1 50 {
ansond 8:b518d1c01df1 51 }
ansond 8:b518d1c01df1 52
ansond 8:b518d1c01df1 53 // Plumb the network
ansond 8:b518d1c01df1 54 void Endpoint::plumbNetwork(bool canActAsRouterNode) {
ansond 8:b518d1c01df1 55 // call into mbedEndpointLib directly for now... (TODO: wont be able to (re)set MAC address in OptionsBuilder as its already been plumbed here...)
ansond 8:b518d1c01df1 56 init_network(canActAsRouterNode);
ansond 8:b518d1c01df1 57 }
ansond 8:b518d1c01df1 58
ansond 8:b518d1c01df1 59 // initialize the endpoint
ansond 8:b518d1c01df1 60 void Endpoint::initialize()
ansond 8:b518d1c01df1 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 8:b518d1c01df1 91 // initialize the Network
ansond 8:b518d1c01df1 92 this->initNetwork();
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 // initialize the NSDL Network
ansond 8:b518d1c01df1 100 void Endpoint::initNetwork()
ansond 8:b518d1c01df1 101 {
ansond 8:b518d1c01df1 102 // register with NSP
ansond 8:b518d1c01df1 103 this->logger()->log("Calling NSP_registration()...");
ansond 8:b518d1c01df1 104 NSP_registration();
ansond 8:b518d1c01df1 105 this->logger()->log("NSP_registration() completed");
ansond 8:b518d1c01df1 106 }
ansond 8:b518d1c01df1 107
ansond 8:b518d1c01df1 108 // Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
ansond 8:b518d1c01df1 109 void Endpoint::start()
ansond 8:b518d1c01df1 110 {
ansond 8:b518d1c01df1 111 // mbedEndpointLib tasklet creation...
ansond 8:b518d1c01df1 112 main_tasklet_id = arm_ns_tasklet_create(&tasklet_main);
ansond 8:b518d1c01df1 113 if(main_tasklet_id < 0) {
ansond 8:b518d1c01df1 114 //Tasklet cerate fail
ansond 8:b518d1c01df1 115 std::printf("startTasklet: Tasklet creation failed...\r\n");
ansond 8:b518d1c01df1 116 return;
ansond 8:b518d1c01df1 117 }
ansond 8:b518d1c01df1 118
ansond 8:b518d1c01df1 119 // mbedEndpointLib event dispatching
ansond 8:b518d1c01df1 120 std::printf("startTasklet: Beginning event dispatch...\r\n");
ansond 8:b518d1c01df1 121 event_dispatch();
ansond 8:b518d1c01df1 122 return;
ansond 8:b518d1c01df1 123 }
ansond 8:b518d1c01df1 124
ansond 8:b518d1c01df1 125 // our logger
ansond 8:b518d1c01df1 126 Logger *Endpoint::logger()
ansond 8:b518d1c01df1 127 {
ansond 8:b518d1c01df1 128 return this->m_logger;
ansond 8:b518d1c01df1 129 }
ansond 8:b518d1c01df1 130
ansond 8:b518d1c01df1 131 } // namespace Connector