/***************************************************************************** 
* 
* File Name : main.c
* 
* Description: main 
* 
* Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd. 
* All rights reserved. 
* 
* Author : dave
* 
* Date : 2014-6-14
*****************************************************************************/ 
#include <string.h>
#include "wm_include.h"
#include "wm_gpio_afsel.h"
#include "nano_shell.h"
#include "lwip/netif.h"
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "rtostimers.h"

tls_os_task_t nano_shell_task_handle = NULL;
extern s16 uart0_rx_callback(u16 len, void *user_data);
extern s16 uart1_rx_callback(u16 len, void *user_data);
extern int shell_printf(const char *format, ...);

#define NANO_SHELL_TASK_STK_SIZE 1024
#define STATUS_LED WM_IO_PB_18

void tls_netif_status_event_cb(u8 status)
{
	struct netif *netif = tls_get_netif();
	switch(status)
  	{
		case NETIF_WIFI_JOIN_SUCCESS:
			shell_printf("Evt : NETIF_WIFI_JOIN_SUCCESS\n");
	  		break;
		case NETIF_WIFI_JOIN_FAILED:
			shell_printf("Evt : NETIF_WIFI_JOIN_FAILED\n");
	  		break;
		case NETIF_WIFI_DISCONNECTED:
			shell_printf("Evt : NETIF_WIFI_DISCONNECTED\n");
			break;
		case NETIF_IP_NET_UP:
			shell_printf("Evt : NETIF_IP_NET_UP\nip addr : %v\nnetmask : %v\ngateway : %v\n", netif->ip_addr.addr,
			netif->netmask.addr,
			netif->gw.addr);
			break;
		case NETIF_WIFI_SOFTAP_SUCCESS:
			shell_printf("Evt : NETIF_WIFI_SOFTAP_SUCCESS\n");
			break;
		case NETIF_WIFI_SOFTAP_FAILED:
			shell_printf("Evt : NETIF_WIFI_SOFTAP_FAILED\n");
			break;
		case NETIF_WIFI_SOFTAP_CLOSED:
			shell_printf("Evt : NETIF_WIFI_SOFTAP_CLOSED\n");
			break;
		case NETIF_IP_NET2_UP:
			shell_printf("Evt : NETIF_IP_NET2_UP\nip addr : %v\nnetmask : %v\ngateway : %v\n", netif->next->ip_addr.addr,
			netif->next->netmask.addr,
			netif->next->gw.addr);
			break;
		case NETIF_IPV6_NET_UP:
			shell_printf("Evt : NETIF_IPV6_NET_UP\n");
			break;
		default:
			shell_printf("Evt : UNKNOWN\n");
			break;
  }
}

void touchsensor_cb(u32 status)
{
	shell_printf("Touch detected : status(%u)\n", status);
}

void tls_gpio_irq_cb(void *arg)
{
	tls_clr_gpio_irq_status(WM_IO_PB_07);
}

void user_main(void *param)
{
	//We initialize input/output used by the app
	tls_gpio_cfg(STATUS_LED, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING);
	wm_uart1_tx_config(WM_IO_PB_06);
	wm_uart1_rx_config(WM_IO_PB_07);
	tls_gpio_irq_enable(WM_IO_PB_07, WM_GPIO_IRQ_TRIG_DOUBLE_EDGE);
	tls_gpio_isr_register(WM_IO_PB_07, &(tls_gpio_irq_cb), NULL);
	//We set a a pin as touch sensor :
	wm_touch_sensor_config(WM_IO_PA_07);
	tls_touchsensor_threshold_config(1, 120);
	tls_touchsensor_init_config(1, 16, 16,1);
	tls_touchsensor_irq_enable(1);
	tls_touchsensor_irq_register(&(touchsensor_cb));

	//We init the uart 1
	tls_uart_port_init(TLS_UART_1, NULL, 0);


	//We create a task for the nano_shell process
	u8 *nano_shell_task_stack = NULL;

	tls_uart_rx_callback_register(TLS_UART_0, &(uart0_rx_callback), NULL);
	tls_uart_rx_callback_register(TLS_UART_1, &(uart1_rx_callback), NULL);

	nano_shell_task_stack = tls_mem_alloc(sizeof(u32) * NANO_SHELL_TASK_STK_SIZE);
	if(nano_shell_task_stack != NULL)
	{
		tls_os_task_create(
			&nano_shell_task_handle,
			"na_shell",
			&(nano_shell_loop),
			NULL,
			(void*) nano_shell_task_stack,
			NANO_SHELL_TASK_STK_SIZE * sizeof(u32),
			62,
			0
		);
	}

	shell_printf("Registering netif callback.\n");
	tls_netif_add_status_event(&(tls_netif_status_event_cb));

	//Socket test
	/*
	char buffer[100] = "", *found = NULL;
	struct sockaddr_in server = {
									.sin_family = AF_INET,
									.sin_addr.s_addr = INADDR_ANY,
									.sin_port = htons(80)
								};
	struct sockaddr_in client;
	socklen_t socklent = sizeof(client);
	int listening_socket = socket(AF_INET, SOCK_STREAM, 0), client_socket;
	bind(listening_socket, (struct sockaddr *)&server, sizeof(server));
	listen(listening_socket, 1);*/

	for(;;)
	{
		//shell_printf("Waiting for clients\n");
		tls_gpio_write(STATUS_LED, !tls_gpio_read(STATUS_LED));
		tls_os_time_delay(pdMS_TO_TICKS(1000));
		/*client_socket = accept(listening_socket, (struct sockaddr *)&client, &socklent);
		shell_printf("Client got accepted\n");
		while(recv(client_socket, buffer, 99, 0) > 0)
		{
			if((found = strchr(buffer, '\r')) != NULL)
				*found = '\0';
			if(buffer[0])
				shell_printf("Recv data : #%s#\n", buffer);
			memset(buffer, 0, sizeof buffer);
		}
		close(client_socket);*/
	}
}

