#include "tcpser.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "espconn.h"
#include "gpio.h"
#include "uart.h"
/******************************************************************************
*此区域为编辑区,请需要添加设计的请到此区域编辑!
*******************************************************************************/



//TCP服务器“server1”程序起始
LOCAL struct espconn server1;
/***透传模块放置区域******/


/*******************************************************************************/
//串口接收程序起始
os_timer_t  datatime;
char dataline[2048];
int datacout;
void ICACHE_FLASH_ATTR
at_recvTask(void)//此程序只能创建一次
{
  uint8_t temp;

  temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
 //uart_tx_one_char(temp); //display back
os_timer_disarm (&datatime) ;  
if(datacout<2048)
  { dataline[datacout]=temp;
  datacout++;}
  os_timer_arm(&datatime,20,0);   
}

void ICACHE_FLASH_ATTR
comexe(void){
os_timer_disarm (&datatime) ;


if(dataline){
/*************
*此区域处理串口发送的数据 dataline
****************/


 //  uart0_sendStr(dataline);//串口输出
espconn_sent(&server1, dataline, strlen(dataline));//网络发送

  /*********************/  
 }
 memset(dataline,0,sizeof(dataline));
  datacout=0;  
  os_timer_arm(&datatime,20,0); 

  }



  void ICACHE_FLASH_ATTR
  com1_init(void){   //调用透传接口
   os_timer_setfn(&datatime, (os_timer_func_t *)comexe, NULL);
os_timer_arm(&datatime,20,0); 
    
    }
  
  
//串口接收程序结尾

/*******************************************************************************/



/***透传模块放置区域******/
LOCAL void ICACHE_FLASH_ATTR
server1_recv(void *arg, char *pdata, unsigned short len)//pdata为接收的数据
{
  struct espconn *pespconn = (struct espconn *)arg;

  uart0_sendStr(pdata);
}



LOCAL void ICACHE_FLASH_ATTR
server1_discon_cb(void *arg)//断开信息
{
  struct espconn *pespconn = (struct espconn *) arg;
 os_printf("S conect C: %p\r\n", arg);
}


LOCAL void ICACHE_FLASH_ATTR
server1_recon_cb(void *arg, sint8 errType)//重连回调
{
  struct espconn *pespconn = (struct espconn *)arg;
 os_printf("S conect C: %p\r\n", arg);
}

static void ICACHE_FLASH_ATTR
server1_sent_cb(void *arg)//发送成功后回调
{struct espconn *pespconn = (struct espconn *)arg;

}


LOCAL void ICACHE_FLASH_ATTR
server1_listen(void *arg)  //pTcpServer侦听
{struct espconn *pespconn = (struct espconn *)arg;
espconn_regist_recvcb(pespconn,server1_recv);
espconn_regist_reconcb(pespconn, server1_recon_cb);
espconn_regist_disconcb(pespconn, server1_discon_cb);
espconn_regist_sentcb(pespconn, server1_sent_cb); 
}
void ICACHE_FLASH_ATTR
server1_init(void) //TCP服务器调用函数
{

server1.type = ESPCONN_TCP;
server1.state = ESPCONN_NONE;
server1.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
server1.proto.tcp->local_port = 5000;
espconn_regist_connectcb(&server1, server1_listen);
espconn_accept(&server1);
espconn_regist_time(&server1,0,0); 

}
//TCP服务器“server1”程序结尾





/******************************************************************************
*编辑区结尾,下面是唯一个子程序,也叫接口程序,请不要动其位置!
*******************************************************************************/
void ICACHE_FLASH_ATTR
tcpser_init(void)
{
com1_init();

server1_init();


}
