#include "board.h"
#include "bytearray.h"
#include "mystdlib.h"
#include "mystring.h"
#include "debug.h"
#include "stdlib.h"
#include "dev_flash.h"
#include "compiler_info.h"

#include "elec_judge.h"
#include "elec_det.h"
#include "base/delay.h"
#include "base/utility.h"
#include "hardware/adc_cfg.h"
#include "hardware/dac_cfg.h"
#include "hardware/gpio_cfg.h"
#include "hardware/jw3425iic.h"
#include "hardware/power.h"
#include "hardware/timer_cfg.h"
#include "driver/EWDriver.h"
#include "driver/JQDriver.h"
#include "driver/XTDriver.h"
#include "interface/BaseChecker.h"
#include "interface/JQChecker.h"
#include "interface/XTChecker.h"
#include "interface/EWChecker.h"
#include "interface/CheckerExt.h"



/*

此文件定义了电子模块与外界软件程序交互的接口

*/




#define PLAN_MAX_TASK 64

#define TASK_PAR_BACK_LEN 10


typedef struct{
  CheckerTask_Info_st task_par;
  int par_count;
  int ret_count;
  int rtv_index;// 返回值下标
  uint16_t params[10];
}task_par_back;


typedef struct{
  CheckerTask_Info_st* task_info_array[PLAN_MAX_TASK];//方案参数结构体指针
  const uint8_t* taskid_table;
  CheckerTask *tasks_fun_table[3];
  CheckerTask_Info_st *task_par;
  CheckerTask task_fun;
  uint8_t chip_type;
  int task_index;
  int task_num;
  int scheme_inited;
  array_def *data;
  task_par_back *task_back_table[TASK_PAR_BACK_LEN];
  int task_back_index;
}self_def;


static self_def g_self={
  .taskid_table=(uint8_t*)(APP_TEST_PLAN_ADDR+4),
  .tasks_fun_table={jqtaskArray,xttaskArray,ewtaskArray},
};


static int elec_check_scheme(void);
static void elec_load_scheme(void);
static int elec_parper_power(void);


// 初始化
int elec_init(void)
{
  void Ye_BoardInit(void);
  Ye_BoardInit();
  elec_bootinfo();
  DBG_LOG("local addr:%d.",elec_local_addr());
  return elec_scheme_init();
}


// 初始化方案
int elec_scheme_init(void)
{
  self_def *s=&g_self;
  int ret=0;
  if(elec_check_scheme()!=0){
    // 方案校验失败
    DBG_WARN("scheme check failed.");
    s->scheme_inited=0;
    return 1;
  }
  s->scheme_inited=1;
  elec_load_scheme();
  ret=elec_parper_power();
  return ret;
}


// 方案去初始化
int elec_scheme_deinit(void)
{
  self_def *s=&g_self;
  s->scheme_inited=0;
  board_st.plan_id = ~0;
  return 0;
}







// 找到第一个指定taskid的任务
static CheckerTask_Info_st *elec_find_task_by_taskid(int id)
{
  self_def *s=&g_self;
  CheckerTask_Info_st *task_par=0;
  for(int i=0;i<s->task_num;i++){
    task_par= s->task_info_array[i];
    if(task_par->taskid==id){
      return task_par;
    }
  }
  return 0;
}


// 调压
static int elec_parper_power(void)
{
  self_def *s=&g_self;
  CheckerTask_Info_st *task_par=0;
  int ret=0;
  task_par= elec_find_task_by_taskid(0);
  if(task_par==0){
    // 找不到调压任务，失败
    DBG_WARN("parper power failed,can not find task with id 0.");
    return 1;
  }
  // 装载参数
  checker_runcfg.power_prapare_exe=0;
  rt_memcpy(&checker_runcfg.task_info,task_par,sizeof(CheckerTask_Info_st));
  checker_runcfg.param_count = checker_runcfg.task_info.param_rtv_count & 0x0F;
  checker_runcfg.rtv_count = (checker_runcfg.task_info.param_rtv_count >> 4) & 0x0F;
  rt_memcpy(checker_runcfg.params,checker_runcfg.task_info.params,checker_runcfg.param_count*2);
  Checker_PowerPrapare();
  ret=checker_runcfg.excue_rtv;
  if(ret){
    DBG_WARN("parper power failed,ret=%d.",ret);
  }else{
    checker_runcfg.power_prapare_exe=1;
  }
	JQBUS_OFF;
	delay_ms(20);
	PowerCalibration_set(POWER_DEF_V,POWER_DEF_V-10);
  return ret;
}



// 获取本机地址
uint8_t elec_local_addr(void)
{
  static uint8_t addr=0;
  if(addr==0){
    addr=Gpio_GetDeivceAddr();
  }
  return addr;
}


// 设置led1状态
void elec_led1_power(int power)
{
  LED1_Out=power?0:1;
}

// 设置led2状态
void elec_led2_power(int power)
{
  LED2_Out=power?0:1;
}


// 获取小板硬件版本号
int elec_hard_version(void)
{
  return HARD_VERSION;
}






// 获取自检参数
array_def *elec_bootinfo(void)
{
  void Ye_BoardCheck(void);
  Ye_BoardCheck();
  board_st.hard_v=sys_param()->hard_version;
  board_st.resistor_diff=sys_param()->resistor_diff;
  //board_st.hard_v=rt_tick_get()/1000;
  board_st.hard_v=HARD_VERSION;
  board_st.soft_v=(int)(str_atof(SOFT_VERSION)*100+0.5);
  array_def *r=arr_creat();
  arr_append(r,0);
  arr_appends(r,&board_st,sizeof(BoartCheck_st));
  return arr_temp(r);
}



// 获取检测结果
array_def *elec_check_result(void)
{
  self_def *s=&g_self;
  array_def *r=0;
  if(s->data){
    r=arr_duplicate(s->data);
  }
  return arr_temp(r);
}


// 清除检测结果
void elec_check_result_clear(void)
{
  self_def *s=&g_self;
  CHECK_DO(s->data,arr_delete);
}



// 写硬件版本号
array_def *elec_write_hardversion(int version)
{
  sys_param_def *spar=0;
  spar=calloc(1,sizeof(sys_param_def));
  memcpy(spar,sys_param(),sizeof(sys_param_def));
  spar->hard_version=version;
  flash_save_param(spar);
  free(spar);
  board_st.hard_v=version;
  array_def *r=arr_creat();
  arr_append(r,0);
  return arr_temp(r);
}



// 写电阻校准值
array_def *elec_write_resistor_cbv(int cbv)
{
  sys_param_def *spar=0;
  spar=calloc(1,sizeof(sys_param_def));
  memcpy(spar,sys_param(),sizeof(sys_param_def));
  spar->resistor_diff=cbv;
  flash_save_param(spar);
  free(spar);
  board_st.resistor_diff=cbv;
  array_def *r=arr_creat();
  arr_append(r,0);
  return arr_temp(r);
}


// 写缓存
array_def *elec_write_fat_buff(array_def *d)
{
  int ret=0;
  int num=arr_length(d);
  if(num<CHECKER_FAC_BUF_LEN){
    memcpy(Checker_FacBuf,arr_data(d),num);
    DBG_LOG("facbuf:%s.",str_temp(arr_string(d)));
    ret=0;
  }else{
    ret=1;
  }
  array_def *r=arr_creat();
  arr_append(r,ret);
  return arr_temp(r);
}




// 测量桥丝阻值
array_def *elec_check_resistor(uint8_t index)
{
	/*
			1 通道1-4通，过桥丝
			2 通道2-3通，过桥丝
			3 通道1-3通，测阻抗
			4 通道2-4通，测阻抗
	*/
	uint16_t aus_sample[5];
	uint16_t us_resistor;
	uint8_t uc_index = 0;
	AD_SampleResistor(aus_sample);
	for(uc_index = 0; uc_index < 5; uc_index++)
	{
		aus_sample[uc_index] -= board_st.resistor_diff;
	}
  // 读取方案中测量电阻的模式
	switch(index)
	{
		case 0: us_resistor = aus_sample[0];break;
		case 1: us_resistor = aus_sample[1];break;
		case 2: us_resistor = aus_sample[2];break;
    case 5: us_resistor = board_st.resistor_diff;break;
		default : us_resistor = 0;break;
	}
  array_def *r=arr_creat();
  arr_append(r,0);
  arr_append(r,us_resistor&0xff);
  arr_append(r,(us_resistor >> 8) & 0xff);
  return arr_temp(r);
}





// 校验方案,返0成功
static int elec_check_scheme(void)
{
	uint32_t ul_crc32;
	
	ul_crc32 = Crc32Calu((uint32_t*)APP_TEST_PLAN_ADDR,(APP_TEST_PLANINFO_SIZE-4));
	if(ul_crc32 != (*(uint32_t*)(APP_TEST_PLAN_ADDR+APP_TEST_PLANINFO_SIZE-4)))
	{
    board_st.plan_id = ~0;
    LED1_Out_Off;
    return 1;
	}else{
    board_st.plan_id = *((uint32_t*)APP_TEST_PLAN_ADDR);
  }
  return 0;
}




// 加载方案
static void elec_load_scheme(void)
{
  self_def *s=&g_self;
	uint8_t uc_index = 0;
	uint32_t ul_len;
	CheckerTask_Info_st* pinfo_st = (CheckerTask_Info_st*)(APP_TEST_PLAN_ADDR+256);
	for(uc_index = 0; uc_index < PLAN_MAX_TASK; uc_index++)
	{
		s->task_info_array[uc_index] = 0;
	}
	uc_index = 0;
  s->task_num=0;
	while((pinfo_st->runindex != 0xFF) && (uc_index < PLAN_MAX_TASK))
	{
		s->task_info_array[uc_index++] = pinfo_st;
    s->task_num++;
		ul_len = pinfo_st->param_rtv_count & 0x0F;
		ul_len = (ul_len<<1)+5;
		pinfo_st = (CheckerTask_Info_st*)((uint8_t*)pinfo_st + ul_len);
	}

}


// 注码 参数校验 1,检测时注码；0,单独注码；-1,失败
static int elec_code_param_check(array_def *uid_psw)
{
  if((uid_psw==0)||(arr_length(uid_psw)==0)){
    return -1;
  }
  if(arr_length(uid_psw)!=(arr_get(uid_psw,0)+arr_get(uid_psw,1)+3)){
    return -1;
  }
  if((arr_get(uid_psw,0)>13)||(arr_get(uid_psw,1)>8)){
    return -1;
  }
  if(arr_get(uid_psw,2)!=0){
    return 1;
  }
  return 0;
}

// 注码 准备参数
static int elec_code_load_param(array_def *uid_psw)
{
  uint8_t pwd_len=0,uid_len=0;
	uid_len = arr_get(uid_psw,0);
	pwd_len = arr_get(uid_psw,1);
  checker_runcfg.uid_len=uid_len;
  checker_runcfg.pwd_len=pwd_len;
  memcpy(checker_runcfg.writeuid,arr_data(uid_psw)+3,uid_len);
  memcpy(checker_runcfg.writepwd,arr_data(uid_psw)+3+uid_len,pwd_len);
  return 0;
}

// 检测 准备参数
int elec_check_load_task_param(uint8_t task_index)
{
  self_def *s=&g_self;
  uint8_t taskid;
  CheckerTask_Info_st *task_par;
  taskid=s->taskid_table[task_index];
  s->task_fun=elec_get_task_fun(taskid);
  task_par= s->task_info_array[task_index];
  memset(&checker_runcfg.task_info,0,sizeof(CheckerTask_Info_st));
  memset(checker_runcfg.params,0,20);
  checker_runcfg.param_count = 0;//参数个数
  checker_runcfg.rtv_count = 0;//返回值个数
  checker_runcfg.excue_rtv = 0;
  checker_runcfg.task_info.retry_time = 0;
  if(taskid>=(CHECKER_MAXID_COUNT+CHECKER_EXTID_COUNT)){
    DBG_WARN("taskid=%d out of bound.",taskid);
    return 1;
  }
  if(task_par==0){
    DBG_WARN("can not find task params.");
    return 2;
  }
  if(s->task_fun==0){
    DBG_WARN("can not find task fun.");
    return 3;
  }
  
  // 装载参数
  s->task_par=task_par;
  memcpy(&checker_runcfg.task_info,task_par,sizeof(CheckerTask_Info_st));
  checker_runcfg.param_count = checker_runcfg.task_info.param_rtv_count & 0x0F;
  checker_runcfg.rtv_count = (checker_runcfg.task_info.param_rtv_count >> 4) & 0x0F;
  memcpy(checker_runcfg.params,checker_runcfg.task_info.params,checker_runcfg.param_count*2);
  return 0;
}

// 备份任务运行环境,0成功
int elec_task_env_back(void)
{
  self_def *s=&g_self;
  task_par_back *b=0;
  if(s->task_back_index>=TASK_PAR_BACK_LEN){
    return 1;
  }
  s->task_back_table[s->task_back_index]=calloc(1,sizeof(task_par_back));
  b=s->task_back_table[s->task_back_index];
  if(b==0){
    return 2;
  }
  b->par_count=checker_runcfg.param_count;
  b->ret_count=checker_runcfg.rtv_count;
  b->rtv_index=checker_runcfg.rtv_index;
  memcpy(&b->task_par, &checker_runcfg.task_info,sizeof(CheckerTask_Info_st));
  memcpy(b->params,checker_runcfg.params,checker_runcfg.param_count*2);
  s->task_back_index++;
  return 0;
}
// 恢复任务运行环境,0成功
int elec_task_env_restore(void)
{
  self_def *s=&g_self;
  task_par_back *b=0;
  if(s->task_back_index<=0){
    return 1;
  }
  b=s->task_back_table[s->task_back_index-1];
  checker_runcfg.param_count=b->par_count;
  checker_runcfg.rtv_count=b->ret_count;
  checker_runcfg.rtv_index=b->rtv_index;
  memcpy(&checker_runcfg.task_info,&b->task_par, sizeof(CheckerTask_Info_st));
  memcpy(checker_runcfg.params,b->params,checker_runcfg.param_count*2);
  free(b);
  s->task_back_index--;
  return 0;
}


// 根据任务id获取检测函数
CheckerTask elec_get_task_fun(uint8_t taskid)
{
  self_def *s=&g_self;
  CheckerTask task_fun=0;
  CheckerTask **tasks_fun_table=s->tasks_fun_table;
  uint8_t chip_type=s->chip_type;
  if(taskid<CHECKER_MAXID_COUNT){
    task_fun=tasks_fun_table[chip_type][taskid];
  }else if((taskid>=CHECKER_MAXID_COUNT)&&
    (taskid<(CHECKER_MAXID_COUNT+CHECKER_EXTID_COUNT)))
  {
    task_fun=exttaskArray[taskid-CHECKER_MAXID_COUNT];
  }
  s->task_fun=task_fun;
  return task_fun;
}

// 执行任务
void elec_exe_task(void)
{
  self_def *s=&g_self;
  if(s->task_fun==0){
    return;
  }
  DBG_LOG("start task_index:%d",s->task_index);
  for(int i=0;i<1+s->task_par->retry_time;i++)
  {
    checker_runcfg.excue_rtv=1;
    s->task_fun();
    Checker_Excueindex(s->task_index);
    checker_runcfg.rtv_index-=checker_runcfg.rtv_count;
    if(checker_runcfg.excue_rtv==0)
      break;
  }
  char *str=malloc(200);
  int str_len=0;
  for(int i=0;i<checker_runcfg.rtv_count;i++)
  {
    str_len+=sprintf(str+str_len,"%d,",checker_runcfg.Test_Rtv[checker_runcfg.rtv_index+i]);
  }
  checker_runcfg.rtv_index+=checker_runcfg.rtv_count;
  if(checker_runcfg.rtv_count>0){
    DBG_LOG("task_index:%d,taskid:%d,ret=%d,rtv_index=%d.",s->task_index,
      s->task_par->taskid,checker_runcfg.excue_rtv,checker_runcfg.rtv_index);
    DBG_LOG("rets:%s\n",str);
  }else{
    DBG_LOG("task_index:%d,taskid:%d,ret=%d,rtv_index=%d.\n",s->task_index,
      s->task_par->taskid,checker_runcfg.excue_rtv,checker_runcfg.rtv_index);
  }
  free(str);
}



// 进行检测
array_def *elec_check_with_scheme(array_def *uid_psw)
{
  self_def *s=&g_self;
  s->task_par=0;
  s->chip_type=0;
  uint8_t taskid=0;
  uint8_t res=0;
  s->task_index=0;
  array_def *r=arr_creat();
  if(s->scheme_inited==0){
    DBG_WARN("scheme not init.");
    arr_append(r,20);
    return arr_temp(r);
  }
  res=checker_runcfg.power_prapare_exe;
  memset(&checker_runcfg,0,sizeof(Checker_RunCfg_st));
  checker_runcfg.power_prapare_exe=res;
  s->chip_type=(board_st.plan_id >> 12 ) & 0x0F;
  if(elec_code_param_check(uid_psw)==1){
    elec_code_load_param(uid_psw);
  }
  
  if(s->chip_type>=LENGTH(s->tasks_fun_table)){
    DBG_WARN("unknown chip type.");
    arr_append(r,20);
    return arr_temp(r);
  }
  elec_check_result_clear();
  LED1_Out_Off;
  while(s->task_index<s->task_num){
    if(elec_check_load_task_param(s->task_index)){
      break;
    }
    elec_exe_task();
    
    // 找寻下一个任务
    s->task_index++;
    if(checker_runcfg.excue_rtv != 0){
      uint8_t err_to=checker_runcfg.task_info.error_jumpto;
      uint8_t temp;
      if(err_to==0xff) continue;
      if(err_to>s->task_num-1) err_to=s->task_num-1;
      for(;s->task_index<err_to;s->task_index++){
        Checker_MaskResult(1,s->task_index);
        temp=(s->task_info_array[s->task_index]->param_rtv_count >> 4) & 0x0F;
        checker_runcfg.rtv_index+=temp;
      }
    }
  }
  
  elec_judge_def *e=malloc(sizeof(elec_judge_def));
  elec_judge(e,s->task_num,checker_runcfg.Task_Result,
    checker_runcfg.Task_Excute,
    (uint8_t *)checker_runcfg.Test_Rtv,&res,0);
  free(e);
  arr_append(r,res);
  arr_appends(r,checker_runcfg.Task_Result,8);
  arr_appends(r,checker_runcfg.Task_Excute,8);
  arr_appends(r,checker_runcfg.Test_Rtv,checker_runcfg.rtv_index*2);
  s->data=arr_duplicate(r);
  return arr_temp(r);
}

// 单独注码
array_def *elec_code(array_def *uid_psw)
{
  self_def *s=&g_self;
  array_def *r=arr_creat();
  uint8_t ret=0;
  int check_ret=0;
  elec_check_result_clear();
  check_ret=elec_code_param_check(uid_psw);
  if(check_ret!=0){
    arr_append(r,1);
    return arr_temp(r);
  }
  elec_code_load_param(uid_psw);
	ret = XT_UID_PWD_Bind();
  arr_append(r,ret);
  s->data=arr_duplicate(r);
  return arr_temp(r);
}



// 执行任务插槽
array_def *elec_check_slot(void)
{
  self_def *s=&g_self;
  s->task_par=0;
  s->chip_type=0;
  uint8_t taskid=0;
  uint8_t res=0;
  s->task_index=0;
  array_def *r=arr_creat();
  res=checker_runcfg.power_prapare_exe;
  memset(&checker_runcfg,0,sizeof(Checker_RunCfg_st));
  checker_runcfg.power_prapare_exe=res;
  
  checker_runcfg.param_count=1;
  checker_runcfg.params[0]=1;
  CheckerExt_Slot();
  
  elec_judge_def *e=malloc(sizeof(elec_judge_def));
  elec_judge(e,s->task_num,checker_runcfg.Task_Result,
    checker_runcfg.Task_Excute,
    (uint8_t *)checker_runcfg.Test_Rtv,&res,0);
  free(e);
  arr_append(r,res);
  return arr_temp(r);
}




