/*
   *
   * OTA Bootloader Program row Command
   * */
  public void OTAProgramRowCmd(
      long rowMSB, long rowLSB, int arrayID, byte[] data, String checkSumType) {

    int COMMAND_DATA_SIZE = 3;
    int totalSize = BootLoaderCommands.BASE_CMD_SIZE + COMMAND_DATA_SIZE + data.length;
    int checksum;
    int i;
    byte[] commandBytes = new byte[totalSize];
    int startCommand = 0x01;

    commandBytes[0] = (byte) startCommand;
    commandBytes[1] = (byte) BootLoaderCommands.PROGRAM_ROW;
    commandBytes[2] = (byte) (data.length + COMMAND_DATA_SIZE);
    commandBytes[3] = (byte) ((int) ((data.length + COMMAND_DATA_SIZE) >> 8));
    commandBytes[4] = (byte) arrayID;
    commandBytes[5] = (byte) rowMSB;
    commandBytes[6] = (byte) rowLSB;
    for (i = 0; i < data.length; i++) commandBytes[i + 7] = data[i];
    checksum =
        BootLoaderUtils.calculateCheckSum2(
            Integer.parseInt(checkSumType, 16), data.length + 7, commandBytes);
    commandBytes[totalSize - 3] = (byte) checksum;
    commandBytes[totalSize - 2] = (byte) (checksum >> 8);
    commandBytes[totalSize - 1] = (byte) BootLoaderCommands.PACKET_END;
    Logger.e("OTAProgramRowCmd send size--->" + commandBytes.length);
    BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes);
  }
 /** OTA Bootloader Get Flash Size Command */
 public void OTAGetFlashSizeCmd(byte[] data, String checkSumType, int dataLength) {
   byte[] commandBytes = new byte[BootLoaderCommands.BASE_CMD_SIZE + dataLength];
   int startCommand = 0x01;
   int dataLength1 = 0x00;
   commandBytes[0] = (byte) startCommand;
   commandBytes[1] = (byte) BootLoaderCommands.GET_FLASH_SIZE;
   commandBytes[2] = (byte) dataLength;
   commandBytes[3] = (byte) dataLength1;
   int dataByteLocationStart = 4;
   int datByteLocationEnd;
   for (int count = 0; count < dataLength; count++) {
     commandBytes[dataByteLocationStart] = data[count];
     dataByteLocationStart++;
   }
   datByteLocationEnd = dataByteLocationStart;
   String checkSum =
       Integer.toHexString(
           BootLoaderUtils.calculateCheckSum2(
               Integer.parseInt(checkSumType, 16), 4, commandBytes));
   long checksum = Long.parseLong(checkSum, 16);
   commandBytes[datByteLocationEnd] = (byte) checksum;
   commandBytes[datByteLocationEnd + 1] = (byte) (checksum >> 8);
   commandBytes[datByteLocationEnd + 2] = (byte) BootLoaderCommands.PACKET_END;
   Logger.e("OTAGetFlashSizeCmd");
   BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes);
 }
  /*
   *
   * OTA Verify CheckSum Command
   * */
  public void OTAVerifyCheckSumCmd(String checkSumType) {

    int checksum;
    byte[] commandBytes = new byte[BootLoaderCommands.BASE_CMD_SIZE];
    int startCommand = 0x01;

    commandBytes[0] = (byte) startCommand;
    commandBytes[1] = (byte) BootLoaderCommands.VERIFY_CHECK_SUM;
    commandBytes[2] = (byte) (0);
    commandBytes[3] = (byte) (0);
    checksum =
        BootLoaderUtils.calculateCheckSum2(
            Integer.parseInt(checkSumType, 16), BootLoaderCommands.BASE_CMD_SIZE - 3, commandBytes);
    commandBytes[4] = (byte) checksum;
    commandBytes[5] = (byte) (checksum >> 8);
    commandBytes[6] = (byte) BootLoaderCommands.PACKET_END;
    Logger.e("OTAVerifyCheckSumCmd");
    BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes);
  }
  /**
   * OTA Bootloader enter command method
   *
   * @param checkSumType
   */
  public void OTAEnterBootLoaderCmd(String checkSumType) {
    int startCommand = 0x01;
    int dataLength0 = 0x00;
    int dataLength1 = 0x00;

    byte[] commandBytes = new byte[7];
    commandBytes[0] = (byte) startCommand;
    commandBytes[1] = (byte) BootLoaderCommands.ENTER_BOOTLOADER;
    commandBytes[2] = (byte) dataLength0;
    commandBytes[3] = (byte) dataLength1;
    String checkSum =
        Integer.toHexString(
            BootLoaderUtils.calculateCheckSum2(
                Integer.parseInt(checkSumType, 16), 4, commandBytes));
    long checksum = Long.parseLong(checkSum, 16);
    commandBytes[4] = (byte) checksum;
    commandBytes[5] = (byte) (checksum >> 8);
    commandBytes[6] = (byte) BootLoaderCommands.PACKET_END;
    Logger.e("OTAEnterBootLoaderCmd");
    BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes);
  }
  /*
   *
   * Exit BootloaderCommand
   *
   * */
  public void OTAExitBootloaderCmd(String checkSumType) {

    int COMMAND_DATA_SIZE = 0x00;
    int COMMAND_SIZE = BootLoaderCommands.BASE_CMD_SIZE + COMMAND_DATA_SIZE;
    int checksum;
    byte[] commandBytes = new byte[BootLoaderCommands.BASE_CMD_SIZE];
    int startCommand = 0x01;

    commandBytes[0] = (byte) startCommand;
    commandBytes[1] = (byte) BootLoaderCommands.EXIT_BOOTLOADER;
    commandBytes[2] = (byte) (COMMAND_DATA_SIZE);
    commandBytes[3] = (byte) (COMMAND_DATA_SIZE >> 8);
    checksum =
        BootLoaderUtils.calculateCheckSum2(
            Integer.parseInt(checkSumType, 16), COMMAND_SIZE - 3, commandBytes);
    commandBytes[4] = (byte) checksum;
    commandBytes[5] = (byte) (checksum >> 8);
    commandBytes[6] = (byte) BootLoaderCommands.PACKET_END;
    Logger.e("OTAExitBootloaderCmd");
    BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes, true);
  }
  public void OTAProgramRowSendDataCmd(byte[] data, String checksumType) {
    int totalSize = BootLoaderCommands.BASE_CMD_SIZE + data.length;
    int checksum;
    int i;
    byte[] commandBytes = new byte[totalSize];
    int startCommand = 0x01;

    commandBytes[0] = (byte) startCommand;
    commandBytes[1] = (byte) BootLoaderCommands.SEND_DATA;
    commandBytes[2] = (byte) (data.length);
    commandBytes[3] = (byte) ((int) ((data.length) >> 8));
    for (i = 0; i < data.length; i++) commandBytes[i + 4] = data[i];
    checksum =
        BootLoaderUtils.calculateCheckSum2(
            Integer.parseInt(checksumType, 16), data.length + 4, commandBytes);
    commandBytes[totalSize - 3] = (byte) checksum;
    commandBytes[totalSize - 2] = (byte) (checksum >> 8);
    commandBytes[totalSize - 1] = (byte) BootLoaderCommands.PACKET_END;
    Logger.e("OTAProgramRowSendDataCmd Send size--->" + commandBytes.length);
    BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes);
  }
  /*
   *
   * OTA Bootloader Verify row Command
   * */
  public void OTAVerifyRowCmd(
      long rowMSB, long rowLSB, OTAFlashRowModel model, String checkSumType) {
    int COMMAND_DATA_SIZE = 3;
    int COMMAND_SIZE = BootLoaderCommands.BASE_CMD_SIZE + COMMAND_DATA_SIZE;
    int checksum;
    byte[] commandBytes = new byte[COMMAND_SIZE];
    int startCommand = 0x01;

    commandBytes[0] = (byte) startCommand;
    commandBytes[1] = (byte) BootLoaderCommands.VERIFY_ROW;
    commandBytes[2] = (byte) (COMMAND_DATA_SIZE);
    commandBytes[3] = (byte) (COMMAND_DATA_SIZE >> 8);
    commandBytes[4] = (byte) model.mArrayId;
    commandBytes[5] = (byte) rowMSB;
    commandBytes[6] = (byte) rowLSB;
    checksum =
        BootLoaderUtils.calculateCheckSum2(
            Integer.parseInt(checkSumType, 16), COMMAND_SIZE - 3, commandBytes);
    commandBytes[7] = (byte) checksum;
    commandBytes[8] = (byte) (checksum >> 8);
    commandBytes[9] = (byte) BootLoaderCommands.PACKET_END;
    Logger.e("OTAVerifyRowCmd");
    BluetoothLeService.writeOTABootLoaderCommand(mOTACharacteristic, commandBytes);
  }