
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/api.h"
#include "debug.h"
#include "lwip/tcp.h"
#include "board.h"
#include "dev_flash.h"
#include "tcp.h"


typedef struct{
  int inited;
  int run;
  struct netconn *conn;
  struct netbuf *buf;
  int if_connect;
  void (*irq_fun)(void *t,uint8_t d);
  void (*irq_fun_end)(void *t,uint32_t len);
  void *ptr;
  uint8_t *rx_buff;
  uint32_t rx_buff_size;
}self_def;


void *tappend(void *p,void *del);


static void tcp_thread(void *arg)
{
  self_def *s=arg;
  err_t err;
  LWIP_UNUSED_ARG(arg);
  ip_addr_t dst;
  const sys_param_def *par=sys_param();
  IP4_ADDR(&dst,par->host_ip[0],par->host_ip[1],par->host_ip[2],par->host_ip[3]);
  DBG_LOG("tcp_thread init.");
  while (s->run) {

    s->conn = netconn_new(NETCONN_TCP);
    s->conn->pcb.tcp->so_options |= SOF_KEEPALIVE;
    netconn_bind(s->conn, IP_ADDR_ANY, 1234);
    err=netconn_connect(s->conn,&dst,par->host_port);
    if (err == ERR_OK) {
      void *data;
      u16_t len;
      s->if_connect=1;
      emit tcp_connect_signal(s);
      while ((err = netconn_recv(s->conn, &s->buf)) == ERR_OK) {
        do {
             netbuf_data(s->buf, &data, &len);
             if(s->irq_fun){
               for(int i=0;i<len;i++)
                 s->irq_fun(s->ptr,((uint8_t *)data)[i]);
             }
             if(s->irq_fun_end){
               if(len>s->rx_buff_size) len=s->rx_buff_size;
               memcpy(s->rx_buff,data,len);
               s->irq_fun_end(s->ptr,len);
             }
             array_def *arr=arr_creat();
             arr_appends(arr,data,len);
             emit tcp_recv_signal(s,arr_temp(arr));
        } while (netbuf_next(s->buf) >= 0);
        netbuf_delete(s->buf);
      }
    }
    else
    {
      //DBG_LOG("tcp:err=%d",err);
    }
    s->if_connect=0;
    s->buf=0;
    netconn_close(s->conn);
    netconn_delete(s->conn);
    sys_msleep(1000);
  }
}



static self_def *mytcp_init(void)
{
  self_def *s=calloc(1,sizeof(self_def));
  s->run=1;
  sys_thread_new("tcp_thread", tcp_thread, s, 2048, 22);
  s->inited=1;
  return s;
}



static int init(uart_def *u,int bsp){
  self_def *s=mytcp_init();
  u->private_data=s;
  app_variable("tcp",s,0);
  return  0;
}
static int deinit(uart_def *u){
  return 0;
}
static int set_irq(uart_def *u,void (*irq)(void *t,uint8_t d),void *t){
  param_check(u);
  param_check(u->private_data);
  self_def *self=u->private_data;
  irq_disable();
  self->irq_fun=irq;
  self->ptr=t;
  irq_enable();
  return 0;
}
static int set_end_irq(uart_def *u,uint8_t *rx_buff,int rx_buff_size,
  void (*irq)(void *t,uint32_t len),void *t)
{
  param_check(u);
  param_check(u->private_data);
  self_def *self=u->private_data;
  irq_disable();
  self->irq_fun_end=irq;
  self->ptr=t;
  self->rx_buff=rx_buff;
  self->rx_buff_size=rx_buff_size;
  irq_enable();
  return 0;
}
static int read(uart_def *u,uint8_t *d,int len){
  return 0;
}
static int write(uart_def *u,const uint8_t *b,int len){
  param_check(u);
  param_check(u->private_data);
  self_def *s=u->private_data;
  err_t err;
  DBG_LOG("tcp:write");
  if(s->if_connect){
    err = netconn_write(s->conn, b, len, NETCONN_COPY);
  }
  else err=ERR_CONN;
  if(err==ERR_OK)
    return len;
  else
    return 0;
}

uart_init_export(utcp,init,deinit,set_irq,set_end_irq,read,write,0)







