added max-age and content-format

Fork of mbedConnectorInterface by Doug Anson

api/Utils.cpp

Committer:
ansond
Date:
2015-02-02
Revision:
11:c1b2b4b6c341
Parent:
8:b518d1c01df1
Child:
14:5cfaeee144bc

File content as of revision 11:c1b2b4b6c341:

/**
 * @file    Utils.cpp
 * @brief   mbed CoAP Endpoint misc utils collection
 * @author  Doug Anson
 * @version 1.0
 * @see
 *
 * Copyright (c) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// mbed Endpoint includes
#include "ConnectorEndpoint.h"
#include "OptionsBuilder.h"

// External references (defined in main.cpp)
Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder);
extern Logger logger;

// ************************* NSDL Linkage - MDS CONFIGURATION (defaulted) *********************************

uint8_t NSP_address_bytes[NSP_IP_ADDRESS_LENGTH] = NSP_IP_ADDRESS;     // which MDS instance we want to bind to...
uint8_t endpoint_name[NODE_NAME_LENGTH] = NODE_NAME;                   // our NODE name
uint8_t domain_name[NSP_DOMAIN_LENGTH] = NSP_DOMAIN;                   // our MDS domain name
int nsp_port = NSP_COAP_UDP_PORT;                                      // our MDS UDP port
uint8_t ep_type[NSP_ENDPOINT_TYPE_LENGTH] = NSP_ENDPOINT_TYPE;         // our NODE type
uint8_t lifetime_ptr[NSP_LIFE_TIME_LENGTH] = { NSP_LIFE_TIME };        // NSDL lifetime
uint8_t app_MAC_address[NODE_MAC_ADDRESS_LENGTH] = NODE_MAC_ADDRESS;   // Node MAC address
uint32_t channel_list = NODE_CHANNEL_LIST;                             // Node RF Channel list

// ************************* NSDL Linkage - MDS CONFIGURATION (defaulted)  *********************************


// further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint...
void configure_endpoint()
{    
    // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions...
    logger.log("configure_endpoint: setting defaults...");
    Connector::OptionsBuilder config;
    config.setNSPAddress(NSP_address_bytes);
    config.setNSPPortNumber(NSP_COAP_UDP_PORT);
    config.setDomain(NSP_DOMAIN);
    config.setEndpointType(NSP_ENDPOINT_TYPE);
    config.setLifetime(NSP_LIFE_TIME);
    
    // Node default configuration - see mbedConnectorInterface.h for definitions...
    config.setRadioChannelList(NODE_CHANNEL_LIST);
    config.setReadUpdatePeriod(NSP_RD_UPDATE_PERIOD);
    config.setEndpointNodename(NODE_NAME); 
    config.setMACAddress(app_MAC_address);   // TODO: arm_ns_tasklet_create() should call Endpoint::plumbNetwork()... currently its called before this MAC address can be (re)set
    
    // main.cpp can override or change any of the above defaults...
    logger.log("configure_endpoint: enabling default configuration overrides...");
    Connector::Options *options = configure_endpoint(config);
        
    // with options, lets set the underlying NSDL globals...
    logger.log("configure_endpoint: updating external NSDL globals...");
    memcpy(NSP_address_bytes,options->getNSPAddress(),NSP_IP_ADDRESS_LENGTH);
    memcpy(endpoint_name,options->getEndpointNodename().c_str(),options->getEndpointNodename().size());
    memcpy(domain_name,options->getDomain().c_str(),options->getDomain().size());
    nsp_port = options->getNSPPortNumber();
    memcpy(ep_type,options->getEndpointType().c_str(),options->getEndpointType().size());
    memcpy(lifetime_ptr,options->getLifetime(),NSP_LIFE_TIME_LENGTH);
    memcpy(app_MAC_address,options->getMACAddress(),NODE_MAC_ADDRESS_LENGTH); // TODO: arm_ns_tasklet_create() should call Endpoint::plumbNetwork()... currently its called before this MAC address can be (re)set
    channel_list = options->getRadioChannelList();

    // alloc Endpoint
    logger.log("configure_endpoint: allocating endpoint...");
    Connector::Endpoint endpoint(&logger,options);

    // initialize Endpoint resources
    logger.log("configure_endpoint: binding endpoint resources...");
    endpoint.initialize();

    // DONE
    logger.log("configure_endpoint: endpoint setup complete.");
}