This is the sample program that can see the decode result of barcode data on Watson IoT.

Dependencies:   AsciiFont DisplayApp GR-PEACH_video LCD_shield_config LWIPBP3595Interface_STA_for_mbed-os USBDevice

mbedConnectorInterface/source/mbedEndpointNetwork.cpp

Committer:
Osamu Nakamura
Date:
2016-11-10
Revision:
1:67f8b5cfde75
Parent:
0:7d720671e6dc

File content as of revision 1:67f8b5cfde75:

/**
 * @file    mbedEndpointNetwork.cpp
 * @brief   mbed Connector Interface network low level functions and support (Ethernet, WiFi, Mesh (6LowPAN,Thread))
 * @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.
 */

// Connector Endpoint
#include "mbed-connector-interface/ConnectorEndpoint.h"

// OptionsBuilder
#include "mbed-connector-interface/OptionsBuilder.h"

// Forward declarations of public functions in mbedEndpointNetwork
#include "mbed-connector-interface/mbedEndpointNetworkImpl.h"

// Network Selection
#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
	#define NETWORK_TYPE			(char *)"WiFi"
#if 1 //bp3595
    #include "LWIPBP3595Interface.h"
    LWIPBP3595Interface network;
    DigitalOut usb1en(P3_8);
#else //#if 1   
	#include "ESP8266Interface.h"
	ESP8266Interface network(MBED_CONF_APP_WIFI_TX,MBED_CONF_APP_WIFI_RX);
#endif
#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
	#define NETWORK_TYPE			(char *)"Ethernet"
	#include "EthernetInterface.h"
	EthernetInterface network;
#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
	#define NETWORK_TYPE			(char *)"6LowPAN"
	#include "NanostackInterface.h"
	LoWPANNDInterface network;
#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
	#define NETWORK_TYPE			(char *)"Thread"
	#include "NanostackInterface.h"
	ThreadInterface network;
#endif

// Logger instance
extern Logger logger;

// endpoint instance 
static void *_endpoint_instance = NULL;

// LWIP network instance forward reference
extern NetworkInterface *__network_interface;

// main loop cycle period
static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;

// endpoint shutdown indicator
static volatile bool _shutdown_endpoint = false;			

extern "C" {

/*********************** START LOCAL FUNCTIONS **************************/

// start shutting downt the endpoint
void start_endpoint_shutdown(void) {
	if (_shutdown_endpoint == true) {
		Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
		if (ep != NULL && ep->isRegistered() == true) {
			logger.logging("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
			ep->de_register_endpoint();
		}
		
		// Clean up
		if (ep != NULL) {
			delete ep;
			_endpoint_instance = NULL;
		}
	}
	
	// ready to shutdown...
	logger.logging("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
}
	
// setup shutdown button
#if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
void configure_deregistration_button(void) {
	logger.logging("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); 
	shutdown_button.fall(&net_shutdown_endpoint);
}	
#endif

// setup shutdown button
void setup_deregistration_button(void) {
#if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
	configure_deregistration_button();
#endif
}

// configure main loop parameters
void configure_main_loop_params(Connector::Endpoint *endpoint) {
	// set the initial shutdown state
	_shutdown_endpoint = false;
}

// perform an actvity in the main loop
void peform_main_loop_activity(void) {
	// empty for now...
	;
}

// begin the main loop for processing endpoint events
void begin_main_loop(void) 
{
	// DEBUG
	logger.logging("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
	
	// enter our main loop (until the shutdown condition flags it...)
	while(_shutdown_endpoint == false) {
		Thread::wait(_main_loop_iteration_wait_ms);
		peform_main_loop_activity();
	}
	
	// main loop has exited... start the endpoint shutdown...
	logger.logging("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
	start_endpoint_shutdown();
}

/************************ END LOCAL FUNCTIONS ***************************/

/*********************** START PUBLIC FUNCTIONS *************************/

// get the network type
char *net_get_type() {
	return NETWORK_TYPE;
}

// shutdown the endpoint
void net_shutdown_endpoint() {
    _shutdown_endpoint = true;
}
	
// called after the endpoint is configured...
void net_plumb_network(void *p) 
{
    int connected = 0;
    Connector::Endpoint *ep = NULL;
    Connector::Options *options = NULL;
    
    // save 
    _endpoint_instance = p;
    
    // connected
    if (p != NULL) {
		ep = (Connector::Endpoint *)p;
		options = ep->getOptions();
	}
    
#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
#if 1 //BP3595
    usb1en = 1;
    Thread::wait(5);
    usb1en = 0;
    Thread::wait(5);
    connected = network.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
#else //#if 1
	// map security types
    nsapi_security_t security_opt = NSAPI_SECURITY_NONE;
    if  (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) {
    	security_opt = NSAPI_SECURITY_WPA;
    }
    if  (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) {
    	security_opt = NSAPI_SECURITY_WPA2;
    }
    if  (options->getWiFiAuthType() == WIFI_WEP) {
    	security_opt = NSAPI_SECURITY_WEP;
    }
    
	// Network Init (WIFI)...
    connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt);
#endif
#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
	// Set the IP Address type to IPV6
	((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6);
	
	// Network Init (Mesh)
	connected = network.connect();
#else
	// not used... just removes a compiler warning...
    options->getConnectorURL();
    
    // Network Init (Ethernet)
    connected = network.connect();
#endif

	// check the connection status..
	if (connected == 0) {
		// success
    	__network_interface = (NetworkInterface *)&network;
    	if (ep != NULL) {
    		ep->isConnected(true);
    	
    		// Debug
   			logger.logging("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address());
   		}
   	}
   	else {
   		__network_interface = NULL;
    	if (ep != NULL) {
    		ep->isConnected(false);
    	}
    	
    	// Debug
   		logger.logging("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
   	}
}

// finalize and run the endpoint main loop
void net_finalize_and_run_endpoint_main_loop(void *p) 
{
	// cast
	Connector::Endpoint *ep = (Connector::Endpoint *)p;
	
	// Initialize our main loop... 
    configure_main_loop_params(ep);
    
    // setup the shutdown button (if enabled for a given platform...)
    setup_deregistration_button();

	// register the endpoint
	logger.logging("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); 
	ep->register_endpoint(ep->getEndpointSecurity(),ep->getEndpointObjectList());
	       
    // Begin the endpoint's main loop
    begin_main_loop();
}

/************************ END PUBLIC FUNCTIONS **************************/

}