

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






static void bootinfo_start(handle_def *h)
{
  port_set_busy(h->p,1);
  list_def *l=list_creat_int();
  int cmds[]={0x13};
  list_appends(l,cmds,1);
  l=list_temp(l);
  array_def *a=arr_temp(arr_creat());
  emit port_send_signal(h->p,port_get_addr(h->p),0x13,l,a,60,5);
}


static bootinfo_data *bootinfo_decode(array_def *data);
static void bootinfo_dolater(handle_def *h,uint8_t src,uint8_t cmd,array_def *data,char *err_str)
{
  if(port_get_busy(h->p)==0) return;
  port_set_busy(h->p,0);
  //DBG_LOG("slave:%d, bootinfo recv",h->p->addr);
  port_timer_stop(h->p);
  if(arr_get(data,0)==0)
  {
    DBG_LOG("slave:%d, success",port_get_addr(h->p));
    arr_remove(data,0,1);
    emit port_end_signal(h->p,h->p,tappend(bootinfo_decode(data),0),0,"ok");
  }
  else{
    emit port_end_signal(h->p,h->p,0,-1,"slave return err");
  }
}


static bootinfo_data *bootinfo_decode(array_def *data)
{
  param_check(arr_length(data)>=24);
  bootinfo_data *d=calloc(1,sizeof(bootinfo_data));
  param_check(d);
  d->app_state=arr_get(data,0);
  d->slaver_addr=arr_get(data,1);
  d->chip_tmper=(arr_get(data,2)|(arr_get(data,3)<<8));
  d->ad_2v5=(arr_get(data,4)|(arr_get(data,5)<<8));
  d->ad_1v25=(arr_get(data,6)|(arr_get(data,7)<<8));
  d->ad_wave=(arr_get(data,8)|(arr_get(data,9)<<8));
  d->ad_5v5_just=(arr_get(data,10)|(arr_get(data,11)<<8));
  d->ad_26v_just=(arr_get(data,12)|(arr_get(data,13)<<8));
  d->hard_version=(arr_get(data,14)|(arr_get(data,15)<<8));
  d->soft_version=(arr_get(data,16)|(arr_get(data,17)<<8));
  d->res_just=arr_get(data,18)|(arr_get(data,19)<<8);
  d->scheme_id=arr_get(data,20)|(arr_get(data,21)<<8)|(arr_get(data,22)<<16)|(arr_get(data,23)<<24);
  DBG_LOG("\r\n"
    " slave=%d,bootinfo:\r\n"
    "      chip_tmper=%d\r\n"
    "      ad_2v5=%d\r\n"
    "      ad_1v25=%d\r\n"
    "      ad_wave=%d\r\n"
    "      ad_5v5_just=%d\r\n"
    "      ad_26v_just=%d\r\n"
    "      hard_version=%d\r\n"
    "      soft_version=%d\r\n"
    "      res_just=%d\r\n"
    "      scheme_id=%d\r\n"
    ,
    d->slaver_addr,d->chip_tmper,d->ad_2v5,d->ad_1v25,d->ad_wave,d->ad_5v5_just,
    d->ad_26v_just,d->hard_version,d->soft_version,d->res_just,d->scheme_id
    );
  return d;
}

static void bootinfo_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");
}



handle_def *bootinfo_creat(void)
{
  handle_def *h=calloc(1,sizeof(handle_def));
  param_check(h);
  h->static_=0;// 动态内存这项设置为0
  h->start=bootinfo_start;
  h->dolater=bootinfo_dolater;
  h->del=(void (*)(handle_def *))free;
  h->timeout=bootinfo_timeout;
  h->interval=3000;
  h->name="bootinfo";
  return h;
}














typedef struct{
  handle_def h;
  const scheme_def *sch;
  rt_timer_t timer;// 用于重复发送获取数据的命令
}check_def;



static void check_start(handle_def *h)
{
  port_set_busy(h->p,1);
  list_def *l=list_creat_int();
  int arr[]={0x0c,0x16};
  list_appends(l,arr,2);
  emit port_send_signal(h->p,port_get_addr(h->p),0x0c,list_temp(l),arr_temp(arr_creat()),60,0);
}


static void check_dolater(handle_def *h,uint8_t src,uint8_t cmd,array_def *data,char *err_str)
{
  check_def *c=(check_def *)h;
  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){
      // 检测中则继续发送
      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==0x0c)
  {
    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 check_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");
}


static void check_del(handle_def *h)
{
  check_def *c=(check_def *)h;
  CHECK_DO(c->timer,rt_timer_delete);
  free(h);
}


handle_def *check_creat(const scheme_def *sch)
{
  check_def *c=calloc(1,sizeof(check_def));
  handle_def *h=(handle_def *)c;
  param_check(h);
  h->static_=0;// 动态内存这项设置为0
  h->start=check_start;
  h->dolater=check_dolater;
  h->del=check_del;
  h->timeout=check_timeout;
  h->interval=sch->timeout_m*1000;// 此项根据方案调整
  h->name="check";
  c->sch=sch;
  DBG_LOG("check created,timeout=%d",h->interval);
  return h;
}



















static int bootinfo(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)<2){
    cmd_print("param num too less.");
    return -1;
  }
  list_def *addrs=str_atod_list(list_get_str(argv,1),',');
  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,bootinfo_creat());
  }
  cmd_print("get bootinfo,addr=%s",str_temp(list_string(addrs)));
  return 0;
}


commend_export(bootinfo,bootinfo,"get slave bootinfo")




static int check(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)<2){
    cmd_print("param num too less.");
    return -1;
  }
  list_def *addrs=str_atod_list(list_get_str(argv,1),',');
  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,check_creat(check_scheme()));
    }
  }
  cmd_print("start check,addr=%s",str_temp(list_string(addrs)));
  return 0;
}


commend_export(check,check,"start electrical check")






static int usercmd(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),',');
  uint8_t cmd=str_atoi(list_get_str(argv,2));
  array_def *data=arr_temp(arr_creat());
  if(list_length(argv)>=4){
    list_def *dat=str_atod_list(list_get_str(argv,3),',');
    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,usercmd_creat(cmd,data));
  }
  cmd_print("start usercmd,addr=%s,cmd=%02x",str_temp(list_string(addrs)),cmd);
  if(arr_length(data)>0){
    cmd_print("data=%s",str_temp(arr_string(data)));
  }
  return 0;
}


commend_export(usercmd,usercmd,"send usercmd to slave|use:usercmd [addrs] [cmd] [datas]")



