#include "bsp_os.h"
#include "ptz_header_file.h"
//#include "device_rotate.h"
#include "service_areascan.h" 
#include "service_cmdpreprocess.h"
#include "enet_to_udp.h"
/********用于自动启动某些功能，进行指令预处理********/
PtzCmdBeforeData BeforeData[CMD_BEFORE_AMOUNT + 1];//数组最后一位用来存储所有数据的效验码
unsigned char cmd_before_count;


//计算每一条指令的CRC
unsigned int ptz_cmd_before_single_crc(PtzCmdBeforeData data)
{
  int i = 0;
  unsigned char *p;
  unsigned int crc = 0;
  p = (unsigned char *)&data;
  for(i = 0; i < (sizeof(data)- sizeof(unsigned int)); i++)
  {
    crc = crc + *p;
    p++;
  }
  return crc;
}

//计算所有指令的总效验
unsigned int ptz_cmd_before_all_crc()
{
  int i = 0;
  unsigned int crc = 0;
  for(i = 0; i < CMD_BEFORE_AMOUNT; i++)
  {
    crc = crc + BeforeData[i].crc;
  }
  return crc;
}


char ptz_cmd_before_data_save()
{
  char i;
  PtzCmdBeforeData BeforeData_a[CMD_BEFORE_AMOUNT + 1];
 
  for(i = 0; i < CMD_BEFORE_SAVE_FLASH_NUM; i++)
  {
    memset(BeforeData_a, 0, sizeof(BeforeData_a));
    write_many_data(sizeof(BeforeData),(unsigned char *)BeforeData, CMD_BEFORE_FLASH_ADD);
    OSTimeDlyHMSM(0u, 0u, 0u, 5u);
    Flash_Read((unsigned char *)BeforeData_a, CMD_BEFORE_FLASH_ADD, sizeof(BeforeData));
    if(memcmp(BeforeData, BeforeData_a, sizeof(BeforeData)) == 0)//判断写入数据是否正确
    {
      return 1;
    }
    OSTimeDlyHMSM(0u, 0u, 0u, 5u);
  }
  return 0;
}

char ptz_cmd_before_data_erase()
{
  char i;
  PtzCmdBeforeData BeforeData_a[CMD_BEFORE_AMOUNT + 1];
  
  memset(BeforeData, 0, sizeof(BeforeData));
  for(i = 0; i < CMD_BEFORE_SAVE_FLASH_NUM; i++)
  {
    memset(BeforeData_a, 0, sizeof(BeforeData_a));
    write_many_data(sizeof(BeforeData),(unsigned char *)BeforeData, CMD_BEFORE_FLASH_ADD);
    OSTimeDlyHMSM(0u, 0u, 0u, 5u);
    Flash_Read((unsigned char *)BeforeData_a, CMD_BEFORE_FLASH_ADD, sizeof(BeforeData));
    if(memcmp(BeforeData, BeforeData_a, sizeof(BeforeData)) == 0)//判断写入数据是否正确
    {
      return 1;
    }
    OSTimeDlyHMSM(0u, 0u, 0u, 5u);
  }
  return 0;
}

char ptz_cmd_before_data_read()
{
  memset(BeforeData, 0, sizeof(BeforeData));
  //读取数据
  Flash_Read((unsigned char *)BeforeData, CMD_BEFORE_FLASH_ADD, sizeof(BeforeData));
  //判断总效验码
  if(BeforeData[CMD_BEFORE_AMOUNT].crc == ptz_cmd_before_all_crc())
  {
    return 1;
  }
  return 0;
}


char ptz_cmd_before_mode(char dev, PTZ_DATA_PACK *pack)
{
  int i;
  switch(pack->command[1])
  {
    case CMD_BEFORE_SAVE_OFF://0关闭
      g_ptz.cmd_before_handle_swtich = CMD_BEFORE_SAVE_OFF;
      return 1;
      break;
      
    case CMD_BEFORE_SAVE_ON://1进入指令保存操作状态
      g_ptz.cmd_before_handle_swtich = CMD_BEFORE_SAVE_ON;
      return 1;
      break;
      
    case CMD_BEFORE_SAVE_SAVE:///2保存所有指令
      if(g_ptz.cmd_before_handle_swtich == CMD_BEFORE_SAVE_ON)
      {
        
        for(i = 0; i < CMD_BEFORE_AMOUNT; i++)
        {//计算每个指令对应的效验码
          BeforeData[i].crc = ptz_cmd_before_single_crc(BeforeData[i]);
        }
        //计算总效验码
        BeforeData[CMD_BEFORE_AMOUNT].crc = ptz_cmd_before_all_crc();
        return ptz_cmd_before_data_save();
      }
      break;
      
    case CMD_BEFORE_SAVE_ERASE://2擦除所有保存指令
      if(g_ptz.cmd_before_handle_swtich == CMD_BEFORE_SAVE_ON)
      {
        cmd_before_count = 0;
        memset(BeforeData, 0, sizeof(BeforeData));
        return ptz_cmd_before_data_erase();
      }
      break;
  }
  return 0;
}


void ptz_cmd_before_start()
{
  int i;
  if(ptz_cmd_before_data_read() == 1)
  {
    for(i = 0; i < CMD_BEFORE_AMOUNT; i++)
    {
      if(BeforeData[i].swtich != CMD_BEFORE_ON)
      {//指令无效
        break;
      }
      if(BeforeData[i].crc != ptz_cmd_before_single_crc(BeforeData[i]))
      {//单个效验码不正确
        break;
      }
      //处理指令
      if(BeforeData[i].cmd_dev == PTZ_UDP)
      {
        memcpy(&ptz_from, &BeforeData[i].cmd_from, ptz_fromlen);
        ptz_fromlen = BeforeData[i].cmd_fromlen;
      }
      ptz_data_pack_process(BeforeData[i].cmd_dev, (PTZ_DATA_PACK *)BeforeData[i].cmd_data);
      cmd_before_count ++;
      OSTimeDlyHMSM(0u, 0u, 0u, 5u);
    }
  }
}

