/*
 * MBIC_Bootloader.c
 *
 *  Created on: 2020. 5. 18.
 *      Author: parkyj
 */

#include <stdint.h>
#include <stdbool.h>

#include "main.h"
#include "MBIC_Bootloader.h"
#include "CRC16.h"
#include "uart.h"


extern uint8_t Bank_Flash_write(uint8_t* data,uint32_t StartBankAddress);





BootLoaderDataCheck_st MBIC_FileDataCheck(uint8_t* data){
  BootLoaderDataCheck_st ret = {0,0,false};
  int8_t MBIC_Mark[9] = "JT-NRDAS ";
  int8_t MBIC_FileName1[] = "jhfs-mbic-nrdas-v";
  int8_t MBIC_FileName2[] = "v00.00.01.mbc";
  uint8_t MBIC_type = 0x20;
  uint8_t MBIC_FileType = 0x00;
  int i = 0;
  /***
  MARK Check
  ***/
  for(i = MARK_START_POSITION; i < TYPE_START_POSITION; i++){
    if(MBIC_Mark[i] != data[i]){
      printf("Data Diff \r\n");
      return ret;
    }else{
      printf("MARK Data Success \r\n");
    }
  }
    /***
  TYPE Check
  ***/
  for(i = TYPE_START_POSITION; i < FILE_TYPE_START_POSITION; i++){
    if(MBIC_type != data[i]){
      printf("Data Diff \r\n");
      return ret;
    }
    else
    printf("Type Data Success \r\n");
  }  
    /***
  File Type Check
  ***/
  for(i = FILE_TYPE_START_POSITION; i < VERSION_START_POSITION; i++){
    if(MBIC_FileType != data[i]){
      printf("Data Diff \r\n");
      return ret;
    }
    else
      printf("File Type Data Success \r\n");
  }  
    /***
  Version Check 
  ***/
  for(i = VERSION_START_POSITION; i < FILENAME_START_POSITION; i++){
    printf("Version Data Success Version %x\r\n",data[i]);    
  }
  /***
  File Name Check
  ***/
  for(i = FILENAME_START_POSITION; i < 30; i++){
    if(MBIC_FileName1[i-14] != data[i]){
      printf("Data Diff \r\n");
      printf("MBIC_FileName1[%d] : %x, data[%d] :  %x\r\n",i - 14,MBIC_FileName1[i - 14],i,data[i]);
      return ret;
    }
    else
      printf("File Name Data Success %c\r\n",data[i]);
  }  
  for(i = i; i < 43; i++){
    if(MBIC_FileName2[i-30] != data[i]){
//      printf("Data Diff %c\r\n",data[i]);
//      printf("MBIC_FileName2[%d] : %x, data[%d] :  %x\r\n",i - 30,MBIC_FileName2[i - 30],i,data[i]);
   //   return ret;
    }
    else
      printf("File Name Data Success %c\r\n",data[i]);
  }   

  for(i = i; i < 49; i++){
      printf("Creation Success %x\r\n",data[i]);
  }
  for(i = i; i < 55; i++){
    printf("Creation Success data[%d] : %x\r\n",i,data[i]);
  }
  printf(" %d",data[i++]);
  printf(" -%02d",data[i++]);
  printf(" -%02d",data[i++]);
  printf(" -%02d",data[i++]);
  printf(" -%02d",data[i++]);
  printf(" -%02d\r\n",data[i++]);
  
  
  
  ret.Length = ((data[i++] << 8) & 0xFF00);
  ret.Length += (data[i++]);
  printf("data[%d] : %d\r\n",i - 1,ret.Length);
  printf("data[%d] : %d\r\n",i - 1,ret.Length);

  ret.Crcchk = ((data[i++] << 8) & 0xFF00);
  ret.Crcchk += (data[i++]);
  /*ONLY DATA CRC*/  
  printf("CRC_H[%d] : %x\r\n",i,ret.Crcchk);
  printf("CRC_L[%d] : %x\r\n",i,ret.Crcchk);

  ret.FileChk = true;
  return ret;
}

/*
  MBIC Basic Header merge function 
  Data : Response Data
  Length : Response Data Length
  CRCINDEX : CRC INDEX Number

*/
uint8_t* MBIC_HeaderMergeFunction(uint8_t* data,uint16_t Length )
{
    uint8_t ret[Length + 22 + 3];/*Data Length + Header Length + Tail Length*/
    uint16_t CRCData = CRC16_Generate(data,Length);
    /*CRC Create*/
    ret[MBIC_PAYLOADSTART + Length + 0] = ((CRCData & 0xFF00) >> 8);
    ret[MBIC_PAYLOADSTART + Length + 1] = ((CRCData & 0x00FF));
    ret[MBIC_PAYLOADSTART + Length + 2] = 0x03;
    /*Data Mark Create*/
    ret[MBIC_PREAMBLE_0] = MBIC_PREAMBLE0;
    ret[MBIC_PREAMBLE_1] = MBIC_PREAMBLE1;
    ret[MBIC_PREAMBLE_2] = MBIC_PREAMBLE2;
    ret[MBIC_PREAMBLE_3] = MBIC_PREAMBLE3;
    /*Data Subid Create*/
    ret[MBIC_SUBUID_0] = MBIC_SUBUID0;
    ret[MBIC_SUBUID_1] = MBIC_SUBUID1;

    ret[MBIC_RCODE_0] = data[MBIC_RCODE_0];

    ret[MBIC_TRID_0] = data[MBIC_TRID_0];
    ret[MBIC_TRID_1] = data[MBIC_TRID_1];

    ret[MBIC_SEQSUM_0] = data[MBIC_SEQSUM_0];

    ret[MBIC_TTL_0] = data[MBIC_TTL_0];

    ret[MBIC_TIME_0] = data[MBIC_TIME_0];
    ret[MBIC_TIME_1] = data[MBIC_TIME_1];
    ret[MBIC_TIME_2] = data[MBIC_TIME_2];
    ret[MBIC_TIME_3] = data[MBIC_TIME_3];
    ret[MBIC_TIME_4] = data[MBIC_TIME_4];
    ret[MBIC_TIME_5] = data[MBIC_TIME_5];
    ret[MBIC_ERRRESPONSE_0] = MBIC_ERRRESPONSE;



        

    ret[MBIC_LENGTH_0] = (Length & 0xFF00) << 8;
    ret[MBIC_LENGTH_1] = Length & 0x00FF;

    ret[MBIC_HEADERCHECKSUM_0] = Chksum_Create(ret);
    /*Data Move*/
//    for(int i = 0; i < Length; i++){
//        data[MBIC_PAYLOADSTART + i] = data[i];
//    }
    /*
        MBIC Header Data input
    */
    for(int i = 0; i < MBIC_HEADER_SIZE; i++){
        if(i == MBIC_CMD_0) /*cmd exception*/
            continue;
        data[i] = ret[i];
    }
      /*
          MBIC Tail Data input
      */
    
      for(int i = MBIC_HEADER_SIZE + Length; i < MBIC_HEADER_SIZE + MBIC_TAIL_SIZE + Length; i++){
        data[i] = ret[i];
    }
//    for(uint16_t i = 0; i < Length; i ++)
//        ret[MBIC_PAYLOADSTART + i] = data[i];
//    for(int i = 0; i < Length; i++)
//        printf("MBIC : %x \r\n",data[i]);

    return data;
}

void MBIC_Bootloader_FirmwareUpdate(uint8_t* data){
    uint8_t MBIC_DownLoadData[0xFFFF];
    uint8_t cmd = data[MBIC_CMD_0];
    static uint8_t Download_Option = 0;
    uint16_t index = 0;
    static uint32_t Curr_Download_DataIndex = 0;
    static uint32_t Prev_Download_DataIndex = 0;    
    uint32_t TotalFrame = 0;
    uint32_t CurrFrame = 0;
    
    uint32_t i = 0 ;
#if 0 // PYJ.2020.06.04_BEGIN -- 
    uint8_t dataTest[1024] = {
      0x4A,0x54,0x2D,0x4E,0x52,0x44,0x41,0x53,0x20,0x20,0x00,0x00,
        0x00,0x01,0x6A,0x68,0x66,0x73,0x2D,0x6D,0x62,0x69,0x63,0x2D,
        0x6E,0x72,0x64,0x61,0x73,0x2D,0x76,0x30,0x30,0x2E,0x30,0x30,0x2E,
        0x30,0x31,0x2E,0x6D,0x62,0x63,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
        0x20,0x20,0x20,0x20,0x20,0x14,0x06,0x03,0x10,0x1F,0xC4,0x3C,0x49,
        0x42,0x98,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    };
    
#endif // PYJ.2020.06.04_END -- 
    

//    printf("RX");
//    for(int i = 0; i < 128; i++)
//        printf("%c",*data++);
    switch(cmd){
        case MBIC_Notice_REQ:
            cmd = MBIC_Notice_RSP;
            /*TOTAL FRAME NUMBER*/
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];        
            /*DOWNLOAD OPTION*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
            Download_Option = data[MBIC_PAYLOADSTART + 4];
            /*DOWNLOAD DELAY REQUEST*/
            data[MBIC_PAYLOADSTART + index++] = 3;
            /*DOWNLOAD Reserve*/
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;      
//            printf("ccc\r\n");
        break;
        case MBIC_Download_DATA_REQ:
            TotalFrame = data[MBIC_PAYLOADSTART + 0] << 24
                        |data[MBIC_PAYLOADSTART + 1] << 16
                        |data[MBIC_PAYLOADSTART + 2] << 8
                        |data[MBIC_PAYLOADSTART + 3] << 0;
//            MBIC_FileDataCheck(data);
            cmd = MBIC_Download_DATA_RSP;
            /*TOTAL FRAME NUMBER*/
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];        
            /*Current Download Frame Number*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 5];        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 6];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 7];        
            Curr_Download_DataIndex = data[MBIC_PAYLOADSTART  + 4] << 24;
            Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
            Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;            
            Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];                        
            /*DOWNLOAD Reserve*/
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            for(i = 0; i < Curr_Download_DataIndex - Prev_Download_DataIndex; i++){
                    MBIC_DownLoadData[i] = data[MBIC_PAYLOADSTART + 12 +i];
                    printf("%02x ",MBIC_DownLoadData[i]);
            }
//            printf("Data End\r\n");
//            Bank_Flash_write(data,FLASH_USER_TEMPBANK_START_ADDR);
//            HAL_Delay(100);
            Prev_Download_DataIndex  = Curr_Download_DataIndex;
            break;
        case MBIC_Download_Confirm_REQ:
            cmd = MBIC_Download_Confirm_RSP;
            /*TOTAL FRAME NUMBER*/
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];        
            /*DOWNLOAD OPTION*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
            Download_Option = data[MBIC_PAYLOADSTART + 4];
            /*DOWNLOAD DELAY REQUEST*/
            data[MBIC_PAYLOADSTART + index++] = 3;
            /*DOWNLOAD Reserve*/
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;  
        break;
        case MBIC_Complete_Notice_REQ_REQ:
            cmd = MBIC_Complete_Notice_RSP;
            /*TOTAL FRAME NUMBER*/
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];        
            /*DOWNLOAD OPTION*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
            Download_Option = data[MBIC_PAYLOADSTART + 4];
            /*DOWNLOAD DELAY REQUEST*/
            data[MBIC_PAYLOADSTART + index++] = 3;
            /*DOWNLOAD Reserve*/
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;  
            break;
        case MBIC_Reboot_Notice_REQ:
            cmd = MBIC_Reboot_Notice_RSP;
            /*TOTAL FRAME NUMBER*/
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];        
            /*DOWNLOAD OPTION*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
            Download_Option = data[MBIC_PAYLOADSTART + 4];
            /*DOWNLOAD DELAY REQUEST*/
            data[MBIC_PAYLOADSTART + index++] = 3;
            /*DOWNLOAD Reserve*/
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;          
            break;
        default:
            return;
    }
    data[MBIC_CMD_0] = cmd;
    
    data = MBIC_HeaderMergeFunction(data,index); // reponse
//	HAL_UART_Transmit_DMA(&huart1, data,22 + 3 + index);

    
    Uart1_Data_Send(data ,22 + 3 + index);
}
