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


#include "main.h"
#include <stdbool.h>
#include "MBIC_Bootloader.h"

#define MARK_START_POSITION						0
#define TYPE_START_POSITION						9	
#define FILE_TYPE_START_POSITION				10
#define VERSION_START_POSITION					11
#define FILENAME_START_POSITION					14
#define CREATION_TIME_START_POSITION			55
#define LENGTH_START_POSITION					61
#define CRC_START_POSITION						65
#define RESERVED_START_POSITION					69
typedef struct{
    uint16_t Length;
    uint16_t Crcchk;
    bool FileChk;
}BootLoaderDataCheck_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.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++]);
  
  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;
}



void MBIC_Bootloader_FirmwareUpdate(uint8_t* data){
    uint8_t datatype = data[MBIC_PAYLOADSTART];

#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
    };
    MBIC_FileDataCheck(dataTest);
    
#endif // PYJ.2020.06.04_END -- 


//    printf("RX");
//    for(int i = 0; i < 128; i++)
//        printf("%c",*data++);
    switch(datatype){
        case MBIC_Notice_REQ:
            
        break;
        case MBIC_Download_DATA_REQ:

        break;
        case MBIC_Download_Confirm_REQ:

        break;
        case MBIC_Complete_Notice_REQ_REQ:

        break;
        case MBIC_Reboot_Notice_REQ:

        break;
    }

}
