#include "handle.h"
#include "mystdlib.h"
#include "board.h"
#include "debug.h"
#include "transmit.h"
#include "commend.h"
#include "mystring.h"


// 这个文件实现与赋码仪相关的操作


typedef struct{
  handle_def h;
  uint8_t uid_size;
  uint8_t pw_size;
  uint8_t data[24];
}code_def;


static void code_start(handle_def *h)
{
  code_def *c=(code_def *)h;
  port_set_busy(h->p,1);
  array_def *a=arr_creat();
  arr_append(a,c->uid_size);
  arr_append(a,c->pw_size);
  arr_append(a,0);
  arr_appends(a,c->data,c->uid_size+c->pw_size);
  
  list_def *l=list_creat_int();
  int cmds[]={0x17};
  list_appends(l,cmds,1);
  l=list_temp(l);
  emit port_send_signal(h->p,port_get_addr(h->p),0x17,l,arr_temp(a),60,5);
}


static void code_dolater(handle_def *h,uint8_t src,uint8_t cmd,array_def *data,char *err_str)
{
  if(port_get_busy(h->p)==0) return;
  if(cmd==0x17)
  {
    if(arr_get(data,0)==0)
    {
      DBG_LOG("slave:%d, code start",port_get_addr(h->p));
      list_def *l=list_creat_int();
      int cmds[]={0x18};
      list_appends(l,cmds,1);
      l=list_temp(l);
      emit port_send_signal(h->p,port_get_addr(h->p),0x18,l,arr_temp(arr_creat()),60,h->interval/60);
    }
    else
    {
      port_set_busy(h->p,0);
      port_timer_stop(h->p);
      DBG_LOG("slave:%d, start failed",port_get_addr(h->p));
      emit port_end_signal(h->p,h->p,0,-1,"start failed");
    }
  }else if(cmd==0x18)
  {
    port_set_busy(h->p,0);
    port_timer_stop(h->p);
    // 注码比较特殊，0x55表示成功
    if(arr_get(data,0)==0x55)
    {
      DBG_LOG("slave:%d, success",port_get_addr(h->p));
      emit port_end_signal(h->p,h->p,0,0,"ok");
    }
    else{
      DBG_LOG("slave:%d, code failed.%s",port_get_addr(h->p),str_temp(arr_string(data)));
      emit port_end_signal(h->p,h->p,0,arr_get(data,0),"code failed");
    }
  }
}



static void code_timeout(handle_def *h)
{
  // 从机没有指定数目时从这里返回
  port_timer_stop(h->p);
  port_set_busy(h->p,0);
  DBG_WARN("slave:%d,%s timeout.",port_get_addr(h->p), h->name);
  emit port_end_signal(h->p,h->p,0,-1,"timeout");
}



// uid_size+pw_size不应大于24
handle_def *code_creat(int uid_size,int pw_size,uint8_t *data)
{
  code_def *c=calloc(1,sizeof(code_def));
  param_check(c);
  if(uid_size+pw_size>24)
  {
    free(c);
    return 0;
  }
  c->uid_size=uid_size;
  c->pw_size=pw_size;
  memcpy(c->data,data,c->uid_size+c->pw_size);
  
  handle_def *h=(handle_def *)c;
  h->static_=0;// 动态内存这项设置为0
  h->start=code_start;
  h->dolater=code_dolater;
  h->del=(void (*)(handle_def *))free;
  h->timeout=code_timeout;
  h->interval=10000;
  h->name="code";
  return h;
}







static int code(list_def *argv)
{
  tran_def *tran=app_variable("tran",0,0);
  if(tran==0){
    DBG_WARN("can not fond variable \"tran\"");
    return -1;
  }
  if(list_length(argv)<3){
    cmd_print("param num too less.");
    return -1;
  }
  list_def *addrs=str_atod_list(list_get_str(argv,1),',');
  array_def *data=arr_temp(arr_creat());
  list_def *dat=str_atod_list(list_get_str(argv,2),',');
  for(int i=0;i<list_length(dat);i++){
    arr_append(data,list_get_int(dat,i));
  }
  for(int i=0;i<list_length(addrs);i++)
  {
    int addr=list_get_int(addrs,i);
    port_mcu *mcu=tran_get_portm(tran,addr-1);
    if(mcu){
      port_start(mcu,code_creat(8,4,arr_data(data)));
    }
  }
  cmd_print("start write uid,addr=%s",str_temp(list_string(addrs)));
  if(arr_length(data)>0){
    cmd_print("data=%s",str_temp(arr_string(data)));
  }
  return 0;
}


commend_export(code,code,"write uidcode to slave,uid_len=8,pw_len=4")






// 检测赋码


typedef struct{
  handle_def h;
  uint8_t uid_size;
  uint8_t pw_size;
  uint8_t data[24];
}code2_def;


static void code2_start(handle_def *h)
{
  code2_def *c=(code2_def *)h;
  port_set_busy(h->p,1);
  array_def *a=arr_creat();
  arr_append(a,c->uid_size);
  arr_append(a,c->pw_size);
  arr_append(a,1);// 此项为1，检测中赋码
  arr_appends(a,c->data,c->uid_size+c->pw_size);
  
  list_def *l=list_creat_int();
  int cmds[]={0x0b};
  list_appends(l,cmds,1);
  l=list_temp(l);
  emit port_send_signal(h->p,port_get_addr(h->p),0x0b,l,arr_temp(a),60,5);
}


static void code2_dolater(handle_def *h,uint8_t src,uint8_t cmd,array_def *data,char *err_str)
{
  if(port_get_busy(h->p)==0) return;
  if(cmd==0x02)
  {
    if(arr_get(data,0)==0)
    {
      port_set_busy(h->p,0);
      port_timer_stop(h->p);
      DBG_LOG("slave:%d, recv check data",src);
      arr_remove(data,0,1);
      DBG_LOG("slave:%d, recv:%s",src,str_temp(arr_string(data)));
      //emit port_end_signal(h->p,h->p,tappend(scheme_check_returns(c->sch,data),0),0,"ok");
      // 直接返回原始数据
      emit port_end_signal(h->p,h->p,data,0,"ok");
//    }else if(arr_get(data,0)==2){
    }else{
      // 检测中则继续发送
      list_def *l=list_creat_int();
      int arr[]={0x02};
      list_appends(l,arr,1);
      emit port_send_signal(h->p,port_get_addr(h->p),0x02,list_temp(l),arr_temp(arr_creat()),60,130);
    }
//    else{
//      port_set_busy(h->p,0);
//      port_timer_stop(h->p);
//      DBG_WARN("slave:%d,get check_result err,%s",src,str_temp(arr_string(data)));
//      emit port_end_signal(h->p,h->p,0,arr_get(data,0),"get check_result err");
//    }
  }
  else if(cmd==0x0b)
  {
    if(arr_get(data,0)==0)
    {
      int timeout =h->interval;
      DBG_LOG("slave:%d, check first ack,timeout=%d",src,timeout);
      list_def *l=list_creat_int();
      int arr[]={0x02};
      list_appends(l,arr,1);
      emit port_send_signal(h->p,port_get_addr(h->p),0x02,list_temp(l),arr_temp(arr_creat()),60,timeout/60);
    }
    else{
      port_set_busy(h->p,0);
      port_timer_stop(h->p);
      DBG_WARN("slave:%d,get start_check err,%s",src,str_temp(arr_string(data)));
      emit port_end_signal(h->p,h->p,0,-1,"get start_check err");
    }
  }
}



static void code2_timeout(handle_def *h)
{
  // 从机没有指定数目时从这里返回
  port_timer_stop(h->p);
  port_set_busy(h->p,0);
  DBG_WARN("slave:%d,%s timeout.",port_get_addr(h->p), h->name);
  emit port_end_signal(h->p,h->p,0,-1,"timeout");
}



// uid_size+pw_size不应大于24
handle_def *code2_creat(int uid_size,int pw_size,uint8_t *data)
{
  code2_def *c=calloc(1,sizeof(code2_def));
  param_check(c);
  if(uid_size+pw_size>24)
  {
    free(c);
    return 0;
  }
  c->uid_size=uid_size;
  c->pw_size=pw_size;
  memcpy(c->data,data,c->uid_size+c->pw_size);
  
  handle_def *h=(handle_def *)c;
  h->static_=0;// 动态内存这项设置为0
  h->start=code2_start;
  h->dolater=code2_dolater;
  h->del=(void (*)(handle_def *))free;
  h->timeout=code2_timeout;
  h->interval=10000;
  h->name="code2";
  return h;
}





