//===========================Back Ground==========================================
private void MonitorUpdate(object sender, DoWorkEventArgs e)
{
MacBootLoaderVersion Version = new MacBootLoaderVersion();
string strFilePath = textFilePath.Text;
int iRealReadCount = 0;
Result result = Result.FAILURE;
Updatedata strcData;
#if _TRACE_OUT_PUT
m_clsInterface.PrintMagToTxt("--MonitorUpdate---");
#endif
ControlItem(ENUM_ITEM_TYPE.TEXT_VER, "");
//Clear buffer
rfid.clsPacket.TRANS_API_ClearBuffer();
//Get Version
while (true)
{
if (m_clsBackThread.CancellationPending == true)
{
e.Cancel = true;
return;
}
ControlItem(ENUM_ITEM_TYPE.MENU_SET_COM_PORT, false);
//Check whether connected or not.
if (Result.OK == GetVersion(AskMode()))
{
break;
}
System.Threading.Thread.Sleep(10);
}
//start to enter update mode
switch (m_Mode)
{
case DEV_MODE.AP:
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, "In the AP mode now, please press \"Update\" button again.");
MessageBox.Show("In the AP mode now, please press \"Update\" button again.",
"Update firmware unsuccessfully.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
break;
case DEV_MODE.BL:
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, "Enter to update mode.");
result = m_clsInterface.API_UpdataEnterUpdateMode();
if (Result.OK != result)
{
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, string.Format("Error:{0}. Can't enter update mode.", result.ToString()));
m_clsInterface.API_UpdataComplete();
MessageBox.Show(string.Format("Error:{0}. Can't enter update mode.", result.ToString()),
"Update firmware unsuccessfully.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, "In update mode, start to update.");
break;
case DEV_MODE.BL_ENTER_UPDATE:
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, "In update mode, start to update.");
break;
case DEV_MODE.NO:
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, "ERROR: Can't connect the device.");
return;
break;
}
//Write FW File to the device
strcData.bRealRunFlag = (bool)e.Argument;
strcData.btData = new Byte[DEFINE_UPDATE_DATA_SIZE];
Array.Clear(strcData.btData, 0, strcData.btData.Length);
//initial Progress Bar
ControlItem(ENUM_ITEM_TYPE.PROG_SET_MAX, (int)m_lFileSize);
ControlItem(ENUM_ITEM_TYPE.PROG_CLEAR, null);
ControlItem(ENUM_ITEM_TYPE.LABEL_PERCENT_TEXT, string.Format("{0}%", 0));
//Start to update
while (m_lFileSize > 0)
{
if (m_clsBackThread.CancellationPending == true)
{
e.Cancel = true;
return;
}
strcData.uiOffset = (uint)m_fileStream.Position + DEFINE_UPDATE_DATA_ADDRESS;
strcData.uiCRC = 0;
Array.Clear(strcData.btData, 0, strcData.btData.Length);
//Read data from file
iRealReadCount = m_fileStream.Read(strcData.btData, 0, DEFINE_UPDATE_DATA_SIZE);
//check file whether is end or not.
if (0 >= iRealReadCount)
{
break;
}
strcData.usSize = (ushort)((iRealReadCount / 4) + (iRealReadCount % 4 > 0 ? 1 : 0) + 3);
result = m_clsInterface.API_UpdataSendUpdateData(strcData);
if (Result.OK != result)
{
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, string.Format("Error:{0}. Write firmware file unsuccessfully.", result.ToString()));
MessageBox.Show(string.Format("Error:{0}. Write firmware file unsuccessfully.", result.ToString()),
"Update firmware unsuccessfully.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
m_lFileSize -= (uint)iRealReadCount;
//Increase progress bar
m_clsBackThread.ReportProgress(iRealReadCount);
}
#if _TRACE_OUT_PUT
m_clsInterface.PrintMagToTxt("--Write Data Complete---");
#endif
//exit update mode and turn to AP mode.
result = m_clsInterface.API_UpdataComplete();
do
{
if (Result.OK == result)
{
break;
}
//In the test mode, update the file and send complete command, then no error occur.
//It will return Result.FWUPD_EXIT_NOWRITES. Means doesn't actually write update data to
//the device
if (m_bTestMode == true &&
Result.FWUPD_EXIT_NOWRITES == result)
{
break;
}
//Show error message
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, string.Format("Error:{0}. Exit BL mode unsuccessfully. Please check file.", result.ToString()));
m_clsInterface.API_UpdataReset();
MessageBox.Show(string.Format("Error:{0}. Exit BL mode unsuccessfully. Please check file.", result.ToString()),
"Update firmware unsuccessfully.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}while(false);
ControlItem(ENUM_ITEM_TYPE.TEXT_STATUS, "Update progress finished. Try to return to AP Mode.");
//Get Version
while (true)
{
if (m_clsBackThread.CancellationPending == true)
{
e.Cancel = true;
return;
}
//Check whether connected or not.
if (Result.OK == GetVersion(AskMode()))
{
break;
}
System.Threading.Thread.Sleep(10);
}
//If return to AP mode, means update progress successfully
if (m_Mode == DEV_MODE.AP)
{
MessageBox.Show("Update firmware successfully.");
ControlItem(ENUM_ITEM_TYPE.PROG_CLEAR, null);
ControlItem(ENUM_ITEM_TYPE.LABEL_PERCENT_TEXT, string.Format("{0}%", 0));
}
}