#include "tran_for_coder.h"
#include "debug.h"
#include "mymisc.h"
#include "mystdlib.h"
#include "board.h"
#include "dev_flash.h"
#include "tcp.h"
#include "prot_uc.h"
#include "handle_for_coder.h"
#include "handle_for_checker.h"


// 这个文件解析跟赋码仪相关的命令



typedef struct{
  ucport_def u;
  uint16_t addrs;
  uint8_t uid_password[240];
  uint8_t ack[10];
  uint8_t ack_num;
  void (*doexert)(ucport_def *u);
}write_uid_def;



// 注码完成槽函数
static void write_uid_end(ucport_def *u,port_mcu *src,void *data,int ack,char *err_str)
{
  write_uid_def *w=(write_uid_def *)u;
  uint8_t addr=port_get_addr(src);
  if(addr<=0||addr>10){
    DBG_WARN("addr err:%d",addr);
    return;
  }
  w->ack[addr-1]=ack;
  w->ack_num++;
  if(w->ack_num>=10)
  {
    array_def *a=arr_creat();
    arr_append(a,0);
    arr_append(a,w->addrs&0xff);
    arr_append(a,w->addrs>>8);
    arr_appends(a,w->ack,10);
    emit tran_send_signal(w->u.p,0x81,arr_temp(a));
    tran_set_busy(w->u.p,0);
  }
}





static void write_uid_return_ok(void *p)
{
  write_uid_def *u=p;
  array_def *a=arr_creat();
  arr_append(a,0);
  arr_append(a,u->addrs&0xff);
  arr_append(a,u->addrs>>8);
  arr_append_num(a,10,0);
  emit tran_send_signal(u->u.p,0x81,arr_temp(a));
  tran_set_busy(u->u.p,0);
}


static ucport_def *write_uid(tran_def *t, uint8_t cmd,array_def *data)
{
  if(arr_length(data)<242){
    DBG_WARN("cmd format err.");
    return 0;
  }
  write_uid_def *u=calloc(1,sizeof(write_uid_def));
  u->u.p=t;
  u->u.doend=write_uid_end;
  u->addrs=arr_get(data,0)|(arr_get(data,1)<<8);
  // 数据固定240字节，如果不检测某通道，则该通道填充占位符
  memcpy(u->uid_password,arr_data(data)+2,240);
  // 默认失败
  memset(u->ack,1,10);
  tran_set_busy(t,1);
  for(int i=0;i<10;i++)
  {
    if(u->addrs&(1<<i)){
      port_mcu *mcu=tran_get_portm(u->u.p,i);
      // 这里打开赋码操作
      if(mcu){
        port_start(mcu,code_creat(15,8,&u->uid_password[i*24]));
      }
    }
  }
  
  array_def *a=arr_creat();
  arr_append(a,0);
  arr_append(a,u->addrs&0xff);
  arr_append(a,u->addrs>>8);
  emit tran_reply_signal(u->u.p,arr_temp(a));

  // test:稍后返回成功
  //later_execute(write_uid_return_ok,u,500);
  
  return (ucport_def *)u;
}


transmit_export(ym_coder,0x01,write_uid)










typedef struct{
  ucport_def u;
  uint16_t addrs;
  uint8_t data[5];
  uint8_t ack[(4+3)*10];
  uint8_t ack_num;
  void (*doexert)(ucport_def *u);
}check_def;



// 检测完成槽函数
static void check_end(ucport_def *u,port_mcu *src,void *data,int ack,char *err_str)
{
  check_def *w=(check_def *)u;
  uint8_t addr=port_get_addr(src);
  if(addr<=0||addr>10){
    DBG_WARN("addr err:%d",addr);
    return;
  }
  uint8_t *ack_d=&w->ack[(addr-1)*7];
  ack_d[2]=ack;
  // TODO: 还有4字节数据
  w->ack_num++;
  if(w->ack_num>=10)
  {
    array_def *a=arr_creat();
    arr_append(a,0);
    arr_append(a,w->addrs&0xff);
    arr_append(a,w->addrs>>8);
    arr_appends(a,w->ack,(4+3)*10);
    emit tran_send_signal(w->u.p,0x81,arr_temp(a));
    tran_set_busy(w->u.p,0);
  }
}



static void check_return_ok(void *p)
{
  check_def *u=p;
  array_def *a=arr_creat();
  arr_append(a,0);
  arr_append(a,u->addrs&0xff);
  arr_append(a,u->addrs>>8);
  // 固定返回7*10个字节，如果不检测某通道，则该通道填充占位符
  for(int i=0;i<10;i++){
    arr_append(a,i+1);
    arr_append(a,u->data[0]);
    arr_append(a,0);
    arr_append_num(a,4,0);
  }
  emit tran_send_signal(u->u.p,0x85,arr_temp(a));
  tran_set_busy(u->u.p,0);
}


static ucport_def *check(tran_def *t, uint8_t cmd,array_def *data)
{
  if(arr_length(data)<7){
    DBG_WARN("cmd format err.");
    return 0;
  }
  check_def *u=calloc(1,sizeof(check_def));
  u->u.p=t;
  u->u.doend=check_end;
  u->addrs=arr_get(data,0)|(arr_get(data,1)<<8);
  memcpy(u->data,arr_data(data)+2,5);
  // 默认失败
  for(int i=0;i<10;i++)
  {
    u->ack[i*7+0]=i+1;
    u->ack[i*7+1]=u->data[0];
    u->ack[i*7+2]=1;
  }
  tran_set_busy(t,1);
  for(int i=0;i<10;i++)
  {
    if(u->addrs&(1<<i)){
      port_mcu *mcu=tran_get_portm(u->u.p,i);
      // 这里打开检测
      if(mcu){
        port_start(mcu,check_creat(check_scheme()));
      }
    }
  }
  
  array_def *a=arr_creat();
  arr_append(a,0);
  arr_append(a,u->addrs&0xff);
  arr_append(a,u->addrs>>8);
  arr_append(a,u->data[0]);
  emit tran_reply_signal(u->u.p,arr_temp(a));

  // test:稍后返回成功
  later_execute(check_return_ok,u,500);
  
  return (ucport_def *)u;
}


transmit_export(ym_coder,0x05,check)








typedef struct{
  rt_timer_t timer;
}live_keeper_def;


static void live_connect(live_keeper_def *t)
{
  rt_tick_t tick=0;
  tick=rt_tick_from_millisecond(1000);
  rt_timer_control(t->timer,RT_TIMER_CTRL_SET_TIME,&tick);
  rt_timer_start(t->timer);
}
static void live_recv(live_keeper_def *t)
{
  rt_tick_t tick=0;
  tick=rt_tick_from_millisecond(5000);
  rt_timer_control(t->timer,RT_TIMER_CTRL_SET_TIME,&tick);
  rt_timer_start(t->timer);
}


// cmd=9x8a
static void live_send(void *p)
{
  const sys_param_def *par=sys_param();
  array_def *d=arr_creat();
  arr_append(d,0);
  arr_append(d,0xff);
  arr_append(d,0x03);
  arr_append(d,par->local_id&0xff);
  arr_append(d,0xff);// 在线的小板
  arr_append(d,0x03);
  emit coder_live_send_signal(p,0x8a,arr_temp(d));
}


static void init_for_tcp(void *t)
{
  void *tcp=app_variable("tcp",0,0);
  void *protu=app_variable("protu",0,0);
  if(tcp&&protu){
    live_keeper_def *live=calloc(1,sizeof(live_keeper_def));
    live->timer=rt_timer_create("live_t",live_send,live,
      rt_tick_from_millisecond(1000),
      RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER);
    protu_codec_set(protu,protu_find_codec("ym_coder"));
    connect(tcp,tcp_connect_signal,0,live,live_connect);
    connect(tcp,tcp_recv_signal,0,live,live_recv);
    connect(live,coder_live_send_signal,0,protu,protu_send_call);
  }else{
    DBG_WARN("can not fond variable \"tcp\" or/and \"protu\"");
  }
}




// 如果本机为赋码仪并且连接类型为tcp
// 初始化心跳
static int init_live_keeper(void)
{
  const sys_param_def *par=sys_param();
  if((strcmp(par->device_type,"coder")==0)&&(strcmp(par->host_if,"utcp")==0))
  {
    app_valid_call("protu",init_for_tcp,0);
  }
  return 0;
}
app_init_export(init_live_keeper);


