use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

source/Utils.cpp

Committer:
ansond
Date:
2016-02-24
Revision:
8:f950fb1b78c0
Parent:
7:e94f0bd92545
Child:
10:3f79b5e67c22

File content as of revision 8:f950fb1b78c0:

/**
 * @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 "mbed-connector-interface/ConnectorEndpoint.h"
#include "mbed-connector-interface/OptionsBuilder.h"
#include "mbed-connector-interface/mbedEndpointNetwork.h"

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

// Our Endpoint
Connector::Endpoint *__endpoint = NULL;

// Our Endpoint configured Options
Connector::OptionsBuilder config;
Connector::Options *options = NULL;

// initialize the Connector::Endpoint instance
void *utils_init_endpoint(bool canActAsRouterNode) {
	// alloc Endpoint
    logger.log("Endpoint: allocating endpoint instance...");
	Connector::Endpoint *ep = new Connector::Endpoint(&logger,options);
	if (ep != NULL) {
		ep->asRouterNode(canActAsRouterNode);
    }
    return (void *)ep;
}

// further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint...
void utils_configure_endpoint(void *p)
{   
	// our Endpoint 
	Connector::Endpoint *ep = (Connector::Endpoint *)p;
	
    // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions... 
    logger.log("Endpoint: setting defaults...");
    config.setEndpointNodename(NODE_NAME);
    config.setEndpointType(NSP_ENDPOINT_TYPE);
    config.setRegUpdatePeriod(REG_UPDATE_PERIOD_MS);
    config.setLifetime(REG_LIFETIME_SEC);

    // WiFi defaults
    config.setWiFiSSID((char *)WIFI_DEFAULT_SSID);          			// default: changeme
    config.setWiFiAuthType(WIFI_WPA_PERSONAL);      					// default: WPA Personal
    config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY);   	// default: changeme
    
    // 802.15.4 defaults
    unsigned char psk[16];
    unsigned char psk_identity[2];
    memset(psk,0,16);
    memset(psk_identity,0,2);
    config.setPreSharedKey(psk);
    config.setPreSharedKeyIdentity(psk_identity);
                 
    // Establish default CoAP observation behavior
    config.setImmedateObservationEnabled(true);    

    // Establish default CoAP GET-based observation control behavior
    config.setEnableGETObservationControl(false);    

    // main.cpp can override or change any of the above defaults...
    logger.log("Endpoint: gathering configuration overrides...");
    options = configure_endpoint(config);
	ep->setOptions(options);
	
    // DONE
    logger.log("Endpoint: endpoint configuration completed.");
}

// build out  the endpoint and its resources
void utils_build_endpoint(void *p)
{
    if (p != NULL) {
    	// Build the Endpoint
    	logger.log("Endpoint: building endpoint and its resources...");
		Connector::Endpoint *ep = (Connector::Endpoint *)p;
	    ep->build_endpoint();
	}
}