/*
 * MBIC_Bootloader.c
 *
 *  Created on: 2020. 5. 18.
 *      Author: parkyj
 */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include "main.h"
#include "MBIC_Bootloader.h"
#include "flash.h"
#include "CRC.h"
#include "uart.h"
#include "eeprom.h"

extern bool EraseInit;

extern uint8_t Bank_Flash_write(uint8_t* data,uint32_t StartBankAddress);
extern void MBIC_DataErase_Func(uint8_t bank_num);
extern uint8_t MBIC_APIFLASH_Func(uint8_t* data,uint32_t size,uint8_t bank_num);
extern void MBIC_DataBankWrite(uint8_t BankNum);
extern void DLI_FRBT_ONOFF_Set(bool);
extern uint8_t DataWrite[sizeof(BLUESTATUS_st)];




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.04.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_CMD_0] = data[MBIC_CMD_0];



        

    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;
}
extern uint32_t MBIC_Address;
uint8_t MBIC_Resp[2048];
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;
    static uint32_t prevFrame = 0xFF;
     uint8_t* tmpstr;
    uint16_t DataLength = 0;
    uint16_t Crcret = 0;
    uint16_t RecvCrc = 0;
    uint32_t i = 0 ;
    int32_t FirmFile_Length  = 0;
    uint32_t  CurrApiAddress = 0,Bank1Address=0,Bank2Address = 0;
    static uint8_t BankNum = 0;
    static bool CRCERR = false;
    CurrApiAddress = FLASH_MBICUSER_START_ADDR;
    Bank1Address = FLASH_USER_BANK1_START_ADDR;
    Bank2Address = FLASH_USER_BANK2_START_ADDR;

    uint8_t* Currdata = (uint8_t*)CurrApiAddress;
    uint8_t* Bank1data = (uint8_t*)Bank1Address;
    uint8_t* Bank2data = (uint8_t*)Bank2Address;    
    uint8_t* pdata;
//    printf("RX");
//    for(int i = 0; i < 128; i++)
//        printf("%c",*data++);
    switch(cmd){
        case MBIC_Notice_REQ:
            EraseInit = false;
            CRCERR = false;
            Download_Option = 0;
            Curr_Download_DataIndex = 0;
            Prev_Download_DataIndex = 0;
            prevFrame = 0xFF;
            printf("MBIC DOWNLOAD REQUEST \r\n");
            cmd = MBIC_Notice_RSP;
            CRCERR = false;
            bluecell_Currdatastatus.MBIC_Download_Auto_Restart_Set 
                = Download_Option 
                = data[MBIC_PAYLOADSTART + 4];
            printf("bluecell_Currdatastatus.MBIC_Download_Auto_Restart_Set  : %d \r\n",bluecell_Currdatastatus.MBIC_Download_Auto_Restart_Set );
            
            /*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++] = 1;
//            data[MBIC_PAYLOADSTART + index++] = 0;//data[MBIC_PAYLOADSTART + 4];
//            for(int i =0; i < 33; i++)
//                printf("%x ",data[i]);
//            printf("\r\n");
            

            
            /*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;
              if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1){
                BankNum = HFR_BANK2;
                pdata = & bluecell_Currdatastatus.CPU_Bank2_Image_Version1;
                for(int i = 0; i < 41; i++)
                    pdata[i] = 0xFF;  

              }else{
                BankNum = HFR_BANK1;
                pdata = & bluecell_Currdatastatus.CPU_Bank1_Image_Version1;
                for(int i = 0; i < 41; i++)
                    pdata[i] = 0xFF;  
              }           
            if(bluecell_Currdatastatus.MBIC_Download_Auto_Restart_Set != false){
//                printf("MBIC_Download_Auto_Restart_Set == true\r\n");
                if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1_SEL){
                    bluecell_Currdatastatus.CPU_Bank_Select = HFR_BANK2_SEL;
//                    printf("Download Bank sel = Bank 2 \r\n");
                }else{
                    bluecell_Currdatastatus.CPU_Bank_Select = HFR_BANK1_SEL;
//                    printf("Download Bank sel = Bank 1 \r\n");            
                }
            }              
//            MBIC_DataBankWrite(1);
            //if()
//            MBIC_DataErase_Func(bluecell_Currdatastatus.CPU_Bank_Select);
//            HAL_Delay(10);
//            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;
            CurrFrame = data[MBIC_PAYLOADSTART + 4] << 24
                        |data[MBIC_PAYLOADSTART + 5] << 16
                        |data[MBIC_PAYLOADSTART + 6] << 8
                        |data[MBIC_PAYLOADSTART + 7] << 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;
#if 0 // PYJ.2020.06.24_BEGIN -- 
            for(i = 0; i < Curr_Download_DataIndex - Prev_Download_DataIndex; i++){
                    MBIC_DownLoadData[i] = ;
//                    printf("%02x ",MBIC_DownLoadData[i]);
            }
#endif // PYJ.2020.06.24_END -- 
//            printf("Data End\r\n");
//            Bank_Flash_write(data,FLASH_USER_BANK1_START_ADDR);

              if(prevFrame != CurrFrame){
                MBIC_APIFLASH_Func(&data[MBIC_PAYLOADSTART + 12],1024,BankNum);
              }else{
                if(MBIC_Address < FLASH_USER_BANK2_START_ADDR)
                    MBIC_Address = FLASH_USER_BANK2_START_ADDR + (CurrFrame * 1024);
                else
                    MBIC_Address = FLASH_USER_BANK1_START_ADDR + (CurrFrame * 1024);
                
                printf("Frame Retry \r\n");
              }
#if 0 // PYJ.2020.06.29_BEGIN -- 
              if(BankNum == HFR_BANK1){
                  printf("Bank1data[MBIC_BOOT_MARK] : %d \r\n",Bank1data[MBIC_BOOT_MARK]);
              }else{

                  printf("Bank1data[MBIC_BOOT_MARK] : %d \r\n",Bank2data[MBIC_BOOT_MARK]);
              }
              
#endif // PYJ.2020.06.29_END -- 
//              if(BankNum == HFR_BANK1)
//                  Crcret = CRC16_Generate(&Bank1data[MBIC_BOOT_MARK], TotalFrame);
//              else
//                Crcret = CRC16_Generate(&Bank2data[MBIC_BOOT_MARK], TotalFrame);
//                printf("CurrFrame : %d ,FirmFile_Length : %d \r\n",CurrFrame,TotalFrame - 1);
//                printf("FILE CRC RET : %x \r\n",Crcret);              

//            HAL_Delay(100);
            Prev_Download_DataIndex  = Curr_Download_DataIndex + 1;
            prevFrame = CurrFrame;

            break;
        case MBIC_Download_Confirm_REQ:
            
//            printf("MBIC_Download_Confirm_REQ \r\n");
            cmd = MBIC_Download_Confirm_RSP;
            /*TOTAL FRAME NUMBER*/
             
            if(BankNum == HFR_BANK1){
                FirmFile_Length = (Bank1data[MBIC_BOOT_LENGTH + 0] << 24)
                                | Bank1data[MBIC_BOOT_LENGTH + 1] << 16
                                | Bank1data[MBIC_BOOT_LENGTH + 2] << 8
                                | Bank1data[MBIC_BOOT_LENGTH + 3];
                Crcret = CRC16_Generate(&Bank1data[MBIC_BOOT_MARK], FirmFile_Length + 128);
//                printf("Bank 1 CRC Calc\r\n");                
            }
            else {
                 FirmFile_Length = (Bank2data[MBIC_BOOT_LENGTH + 0] << 24)
                                | Bank2data[MBIC_BOOT_LENGTH + 1] << 16
                                | Bank2data[MBIC_BOOT_LENGTH + 2] << 8
                                | Bank2data[MBIC_BOOT_LENGTH + 3];   
                Crcret = CRC16_Generate(&Bank2data[MBIC_BOOT_MARK], FirmFile_Length + 128);
//                printf("Bank 2 CRC Calc\r\n");
            }
            RecvCrc = data[MBIC_PAYLOADSTART + 4] << 8 | data[MBIC_PAYLOADSTART + 5];
            
//            printf("FirmFile_Length : %d Byte \r\n",((FirmFile_Length  * 1024) + 128));
//            printf("FILE CRC RET : %x  : Recv CRC : %x \r\n",Crcret,RecvCrc);
            if(RecvCrc != Crcret){
                CRCERR = true; // CRC ERR Occur 
            }
            data[MBIC_PAYLOADSTART + index++] = ((Crcret & 0xFF00) >> 8);
            data[MBIC_PAYLOADSTART + index++] = ((Crcret & 0x00FF));
#if 0 // PYJ.2020.06.29_BEGIN -- 
            if(Crcret != RecvCrc){
                CRCERR = true;
                printf("CRC ERROR : MBIC : %x / MDOC : %x \r\n",Crcret,RecvCrc);
                data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2] = 0;
            }else{
                CRCERR = false;
                printf("CRC Success : MBIC : %x / MDOC : %x \r\n",Crcret,RecvCrc);
                data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2] = 1;
            }
#endif // PYJ.2020.06.29_END -- 
            
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3] = 1;        
            /*DOWNLOAD OPTION*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
            data[MBIC_PAYLOADSTART + index++] = 0;
//         if(BankNum == HFR_BANK1){
//            bluecell_Currdatastatus.CPUVERSION1 = Bank1data[MBIC_BOOT_VERSION];
//            bluecell_Currdatastatus.CPUVERSION2 = Bank1data[MBIC_BOOT_VERSION + 1];
//            bluecell_Currdatastatus.CPUVERSION3 = Bank1data[MBIC_BOOT_VERSION + 2];          
//              printf("Bank1data[MBIC_BOOT_VERSION] : %d \r\n",Bank1data[MBIC_BOOT_VERSION + 2]);
//          }else{
//              bluecell_Currdatastatus.CPUVERSION1 = Bank2data[MBIC_BOOT_VERSION];
//              bluecell_Currdatastatus.CPUVERSION2 = Bank2data[MBIC_BOOT_VERSION + 1];
//              bluecell_Currdatastatus.CPUVERSION3 = Bank2data[MBIC_BOOT_VERSION + 2]; 
//              printf("Bank2data[MBIC_BOOT_VERSION] : %d \r\n",Bank2data[MBIC_BOOT_VERSION + 2]);
//          }   
            bluecell_Currdatastatus.CPU_Bank1_Image_Version1 = Bank1data[MBIC_BOOT_VERSION];
            bluecell_Currdatastatus.CPU_Bank1_Image_Version2 = Bank1data[MBIC_BOOT_VERSION + 1];
            bluecell_Currdatastatus.CPU_Bank1_Image_Version3 = Bank1data[MBIC_BOOT_VERSION + 2];

            bluecell_Currdatastatus.CPU_Bank2_Image_Version1 = Bank2data[MBIC_BOOT_VERSION];
            bluecell_Currdatastatus.CPU_Bank2_Image_Version2 = Bank2data[MBIC_BOOT_VERSION + 1];
            bluecell_Currdatastatus.CPU_Bank2_Image_Version3 = Bank2data[MBIC_BOOT_VERSION + 2];

            bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime1 = Bank1data[MBIC_BOOT_CREATION_TIME];
            bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime2 = Bank1data[MBIC_BOOT_CREATION_TIME + 1];
            bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime3 = Bank1data[MBIC_BOOT_CREATION_TIME + 2];         
            bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime4 = Bank1data[MBIC_BOOT_CREATION_TIME + 3];         
            bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime5 = Bank1data[MBIC_BOOT_CREATION_TIME + 4];         
            bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime6 = Bank1data[MBIC_BOOT_CREATION_TIME + 5];   


            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime1 = Bank2data[MBIC_BOOT_CREATION_TIME];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime2 = Bank2data[MBIC_BOOT_CREATION_TIME + 1];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime3 = Bank2data[MBIC_BOOT_CREATION_TIME + 2];         
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime4 = Bank2data[MBIC_BOOT_CREATION_TIME + 3];         
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime5 = Bank2data[MBIC_BOOT_CREATION_TIME + 4];         
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime6 = Bank2data[MBIC_BOOT_CREATION_TIME + 5];   
            tmpstr = &bluecell_Currdatastatus.CPU_Bank1_Image_Name;
            for(int i = 0 ; i< 32; i++){
                tmpstr[i] = Bank1data[MBIC_BOOT_FILENAME + i];
            }  
            tmpstr = &bluecell_Currdatastatus.CPU_Bank2_Image_Name;
            for(int i = 0 ; i< 32; i++){
                tmpstr[i] = Bank2data[MBIC_BOOT_FILENAME + i];
//                printf("%x",tmpstr[i]);
            }               
//            printf("Curr Version : %02x.%02x.%02x \r\n",bluecell_Currdatastatus.CPUVERSION1,bluecell_Currdatastatus.CPUVERSION2,bluecell_Currdatastatus.CPUVERSION3);
//            printf("Bank1data Version : %02x.%02x.%02x \r\n", Bank1data[MBIC_BOOT_VERSION + 0], Bank1data[MBIC_BOOT_VERSION + 1], Bank1data[MBIC_BOOT_VERSION + 2]);
//            printf("Bank2data Version : %02x.%02x.%02x \r\n", Bank2data[MBIC_BOOT_VERSION + 0], Bank2data[MBIC_BOOT_VERSION + 1], Bank2data[MBIC_BOOT_VERSION + 2]);            
        
        break;
        case MBIC_Complete_Notice_REQ_REQ:
//            printf("MBIC_Complete_Notice_REQ_REQ \r\n");
            cmd = MBIC_Complete_Notice_RSP;
            if(CRCERR == false){
                data[MBIC_PAYLOADSTART + index++] = 1;  
                printf("Crc Success \r\n");
            }else{
                data[MBIC_PAYLOADSTART + index++] = 0;  
            }
            data[MBIC_PAYLOADSTART + index++] = 1;    
            
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime1 = Bank2data[MBIC_BOOT_CREATION_TIME + 0];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime2 = Bank2data[MBIC_BOOT_CREATION_TIME + 1];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime3 = Bank2data[MBIC_BOOT_CREATION_TIME + 2];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime4 = Bank2data[MBIC_BOOT_CREATION_TIME + 3];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime5 = Bank2data[MBIC_BOOT_CREATION_TIME + 4];
            bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime6 = Bank2data[MBIC_BOOT_CREATION_TIME + 5];
            bluecell_Currdatastatus.CPU_Bank2_Image_Version1 = Bank2data[MBIC_BOOT_VERSION + 0];
            bluecell_Currdatastatus.CPU_Bank2_Image_Version2 = Bank2data[MBIC_BOOT_VERSION + 1];
            bluecell_Currdatastatus.CPU_Bank2_Image_Version3 = Bank2data[MBIC_BOOT_VERSION + 2];



//          if(BankNum == HFR_BANK1){
//            bluecell_Currdatastatus.CPUVERSION1 = Bank1data[MBIC_BOOT_VERSION];
//            bluecell_Currdatastatus.CPUVERSION2 = Bank1data[MBIC_BOOT_VERSION + 1];
//            bluecell_Currdatastatus.CPUVERSION3 = Bank1data[MBIC_BOOT_VERSION + 2];          
//              printf("Bank1data[MBIC_BOOT_VERSION] : %d \r\n",Bank1data[MBIC_BOOT_VERSION]);
//          }else{
//              bluecell_Currdatastatus.CPUVERSION1 = Bank2data[MBIC_BOOT_VERSION];
//              bluecell_Currdatastatus.CPUVERSION2 = Bank2data[MBIC_BOOT_VERSION + 1];
//              bluecell_Currdatastatus.CPUVERSION3 = Bank2data[MBIC_BOOT_VERSION + 2]; 
//
//              printf("Bank1data[MBIC_BOOT_VERSION] : %d \r\n",Bank2data[MBIC_BOOT_VERSION]);
//          }   
//            printf("Curr Version : %02x.%02x.%02x \r\n",bluecell_Currdatastatus.CPUVERSION1,bluecell_Currdatastatus.CPUVERSION2,bluecell_Currdatastatus.CPUVERSION3);
//            printf("Bank1data Version : %02x.%02x.%02x \r\n", Bank1data[MBIC_BOOT_VERSION + 0], Bank1data[MBIC_BOOT_VERSION + 1], Bank1data[MBIC_BOOT_VERSION + 2]);
//            printf("Bank2data Version : %02x.%02x.%02x \r\n", Bank2data[MBIC_BOOT_VERSION + 0], Bank2data[MBIC_BOOT_VERSION + 1], Bank2data[MBIC_BOOT_VERSION + 2]);
            break;
#if 0 // PYJ.2020.06.30_BEGIN -- 
        case MBIC_Reboot_Notice_REQ:
            printf("MBIC_Reboot_Notice_REQ\r\n");
            cmd = MBIC_Reboot_Notice_RSP;
            /*TOTAL FRAME NUMBER*/
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
            data[MBIC_PAYLOADSTART + index++] = 0;        
            /*DOWNLOAD OPTION*/        
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];            
            data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
            break;
#endif // PYJ.2020.06.30_END -- 
        default:
            return;
    }
    data[MBIC_CMD_0] = cmd;
    
    data = MBIC_HeaderMergeFunction(data,index); // reponse
    for(int i =0; i < 22 + 3 + index; i++){
        MBIC_Resp[i] = data[i];
    }
    DataLength = 22 + 3 + index;
    Crcret =CRC16_Generate(&data[MBIC_PAYLOADSTART], DataLength - 25);
    MBIC_Resp[(DataLength - 1) - 2] = (Crcret & 0xFF00) >> 8;  
    MBIC_Resp[(DataLength - 1) - 1] = (Crcret & 0x00FF);       
    
    Uart1_Data_Send(MBIC_Resp ,22 + 3 + index);
    if( cmd == MBIC_Complete_Notice_RSP
        &&bluecell_Currdatastatus.MBIC_Download_Auto_Restart_Set != false)
    {
        printf("Reset Start \r\n");
        HAL_Delay(100);
        NVIC_SystemReset();
    }
    MBIC_DataErase_Func(BankNum);
}
