/*Copyright (c) 2012, Sierra Wireless, Inc
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
•Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
•Redistributions in binary form must reproduce the above copyright notice, this
 list of conditions and the following disclaimer in the documentation and/or
 other materials provided with the distribution.
•Neither the name of Sierra Wireless Inc, Richmond, Canada nor the names of its
 contributors may be used to endorse or promote products derived from this
 software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABIL ITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* ************************************************************************* */
/*
   File:		ethernet.c
   Version:		v1.0 (20091211)
   Author:		SPT
*/
/* ************************************************************************* */

/* ************************************************************************* */
#include "adl_global.h"
#include "wip.h"
#include "wireless_internet_gateway.h"
/* ************************************************************************* */
wip_bearer_t gl_eth_br  = {0};
wip_eth_addr_t gl_eth_mac_addr = { 0x00, 0x02, 0x26, 0x87, 0x00, 0x00, 0x00 };
u8 pad1 = 0;
wip_in_addr_t gl_eth_ip_addr, gl_eth_net_mask, gl_eth_gateway_ip_addr = {0};

extern wip_in_addr_t gl_dns1_ip_addr;
extern wip_in_addr_t gl_dns2_ip_addr;

wip_netDhcpOption_t gl_dhcp_options [] = {
/* Router option is RFC2132 */             3, sizeof(wip_in_addr_t), (u8*) &gl_eth_gateway_ip_addr,
/* Domain Name Server option in RFC2132 */ 6, sizeof(wip_in_addr_t), (u8*) &gl_dns1_ip_addr,
	                                       255 /* End of options */
};

u8 gl_eth_state  = WIP_BEV_CLOSED;
bool gl_restart_eth = FALSE;
/* ************************************************************************* */
void fn_start_eth ( u8 lo_id, void* lo_ctx );
/* ************************************************************************* */
void
fn_eth_br_cb (
			   wip_bearer_t lo_br,
			   s8 lo_ev,
			   void* lo_ctx )
{
	switch ( lo_ev )
	{
		case WIP_BEV_IP_CONNECTED:
			gl_eth_state  = WIP_BEV_IP_CONNECTED;
			TRACE (( 1, "[ETH] WIP_BEV_IP_CONNECTED" ));
			
			fn_start_web_server ();

			break;
		case WIP_BEV_IP_DISCONNECTED:
			gl_eth_state  = WIP_BEV_IP_DISCONNECTED;
			TRACE (( 1, "[ETH] WIP_BEV_IP_DISCONNECTED" ));
			wip_bearerStop ( lo_br );
			break;
		case WIP_BEV_STOPPED:
			gl_eth_state  = WIP_BEV_STOPPED;
			TRACE (( 1, "[ETH] WIP_BEV_STOPPED" ));
			wip_bearerClose ( lo_br );
			gl_eth_state = WIP_BEV_CLOSED;

			if ( gl_restart_eth == TRUE )
			{
				gl_restart_eth = FALSE;
				adl_tmrSubscribe ( FALSE, 1, ADL_TMR_TYPE_100MS, fn_start_eth );
			}
			break;
		default:
			break;
	}
}
/* ************************************************************************* */
void
fn_close_eth ( void )
{
	if ( (gl_eth_state == WIP_BEV_IP_CONNECTED) || (gl_eth_state == WIP_BEV_IP_DISCONNECTED) )
		wip_bearerStop ( gl_eth_br );
	else if ( gl_eth_state == WIP_BEV_STOPPED )
		wip_bearerClose ( gl_eth_br );
}
/* ************************************************************************* */

void
fn_configure_dhcps ( wip_netDhcpOption_t* lo_dhcp_options )
{
	s32 lo_ret = 0;

	lo_ret =
	wip_netSetOpts ( 
				    WIP_NET_OPT_DHCPS_GLOB_OPT,  (wip_netDhcpOption_t*) lo_dhcp_options,
					WIP_NET_OPT_DHCPS, TRUE,
					WIP_NET_OPT_END );
	
	if ( lo_ret == OK )
		TRACE (( 1, "[ETH] DHCPS running .. " ));
	else
		TRACE (( 1, "[ETH] %d <- wip_bearerSetOpts(DHCPS)", lo_ret ));
}
/* ************************************************************************* */
void
fn_start_eth ( u8 lo_id, void* lo_ctx )
{
	wip_in_addr_t lo_local_ip = {0};
	s32 lo_ret = 0;

	TRACE (( 1, "[ETH] DRV SUBSCRIBED" ));

	lo_ret =
		     wip_bearerOpen ( &gl_eth_br, "ETHER", fn_eth_br_cb, NULL );
	TRACE (( 1, "[ETH] %d <- wip_bearerOpen()", lo_ret ));

	// FSU will be DHCPS & Gateway
	wip_inet_aton ( DHCP_SERVER_IP, &lo_local_ip );
	wip_inet_aton ( DHCP_SERVER_IP, &gl_eth_gateway_ip_addr );

	lo_ret =
	wip_bearerSetOpts ( gl_eth_br,
                 		WIP_BOPT_ETH_ADDR,  &gl_eth_mac_addr,
                        WIP_BOPT_IP_DHCP,   FALSE,
						WIP_BOPT_IP_ADDR,   lo_local_ip,
                       	WIP_BOPT_EXTNAT,    FALSE,						
						WIP_BOPT_END );

	TRACE (( 1, "[ETH] %d <- wip_bearerSetOpts()", lo_ret ));
	
	fn_configure_dhcps ( &gl_dhcp_options );

	lo_ret = wip_bearerStart( gl_eth_br );
	
	if ( lo_ret == OK )
		TRACE (( 1, "[ETH] BEARER START OK" ));
	else
		TRACE (( 1, "[ETH] %d <- wip_bearerStart()", lo_ret ));
}
/* ************************************************************************* */
void
fn_restart_ethernet ( void )
{
	s32 lo_ret = 0;

	// Restart Ethernet to set DNS received from GPRS.
	// At times, the DNS setting fails (random), Google Public DNS is therefore set as default DNS.
	gl_restart_eth = TRUE;	

	lo_ret =
	wip_netSetOpts ( 
				    WIP_NET_OPT_DHCPS, FALSE,
					WIP_NET_OPT_END );
	if ( lo_ret == OK )
		TRACE (( 1, "[ETH] DHCPS stopped .. " ));
	else
		TRACE (( 1, "[ETH] %d <- wip_bearerSetOpts(DHCPS)", lo_ret ));

	fn_close_eth ();
}
/* ************************************************************************* */
void
fn_init_ethernet ( void )
{
	fn_eth_drv_subscribe ( "ETHER" );
	gl_restart_eth = FALSE;
	adl_tmrSubscribe ( FALSE, 50, ADL_TMR_TYPE_100MS, fn_start_eth );
}
/* ************************************************************************* */
