⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firmwareupdate.cpp

📁 UHF RFID Reader Program
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	while (!StackEmpty(stack))
	{
		StackPop(stack);
	}
	StackDestroy(stack);

EXIT:
	return m_pRoot;
} /* CreateXmlTree */


void CFirmwareUpdate::DestroyXmlTree(
					XML_NODE*   pRoot
					)
{
	if (pRoot)
	{
		DestroyXmlTree(pRoot->pFirstChild);
		DestroyXmlTree(pRoot->pNextSibling);
		if (pRoot->pName)
		{
			free(pRoot->pName);
		}
		if (pRoot->pValue)
		{
			free(pRoot->pValue);
		}
		free(pRoot);
	}
} /* DestroyXmlTree */


INT32S CFirmwareUpdate::ProcessXmlTree(
					  const XML_NODE*                 pRoot,
					  RFID_NONVOLATILE_MEMORY_BLOCK** ppBlocks,
					  MEMMAP_HANDLE**                 ppHandles,
					  INT32U*                         pNumberBlocks
					  )
{
	INT32S                          result      = -1;
	XML_NODE*                       pNode;
	RFID_NONVOLATILE_MEMORY_BLOCK*  pBlocks     = NULL;
	MEMMAP_HANDLE*                  pHandles    = NULL;
	INT32U                          numberBlocks;
	INT32U                          blockNumber;

	*ppBlocks       = NULL;
	*ppHandles      = NULL;
	*pNumberBlocks  = 0;

	/* Verify that the root of the tree is named properly.                    */
	if (strcmp(pRoot->pName, ROOT_TAG_NAME))
	{
		TRACE2("ERROR: XML root tag is named <%s> instead of <%s>\n", pRoot->pName, ROOT_TAG_NAME);
		goto EXIT;
	}

	/* Verify that there are update blocks                                    */
	if (NULL == pRoot->pFirstChild)
	{
		fprintf(
			stderr,
			"ERROR: Update file must contain at least one update block\n");
		goto EXIT;
	}

	/* Process each block to create the update block array                    */
	for (pNode = pRoot->pFirstChild, numberBlocks = 0;
		pNode != NULL;
		pNode = pNode->pNextSibling, ++numberBlocks)
	{
		RFID_NONVOLATILE_MEMORY_BLOCK*  pNewBlocks;
		MEMMAP_HANDLE*                  pNewHandles;
		XML_NODE*                       pChild;
		INT32U                          addressSet = 0;

		if (strcmp(pNode->pName, BLOCK_TAG_NAME))
		{
			fprintf(
				stderr,
				"ERROR: Found XML tag named <%s>, but expected <%s>\n",
				pNode->pName,
				BLOCK_TAG_NAME);
			goto CLEANUP;
		}

		/* Expand the array of nonvolatile memory blocks and file mapping     */
		/* handles.                                                           */
		pNewBlocks = 
			(RFID_NONVOLATILE_MEMORY_BLOCK *) realloc(
			pBlocks,
			(numberBlocks + 1) * sizeof(RFID_NONVOLATILE_MEMORY_BLOCK));
		if (NULL == pNewBlocks)
		{
			fprintf(stderr, "ERROR: Failed to allocate memory\n");
			goto CLEANUP;
		}
		pBlocks = pNewBlocks;
		pBlocks[numberBlocks].pData = NULL;
		pBlocks[numberBlocks].flags = 0;

		pNewHandles = 
			(MEMMAP_HANDLE *) realloc(
			pHandles,
			(numberBlocks + 1) * sizeof(MEMMAP_HANDLE));
		if (NULL == pNewHandles)
		{
			fprintf(stderr, "ERROR: Failed to allocate memory\n");
			goto CLEANUP;
		}
		pHandles = pNewHandles;
		pHandles[numberBlocks] = INVALID_MEMMAP_HANDLE;

		/* Process the children to get the address and the update file        */
		for (pChild = pNode->pFirstChild;
			NULL != pChild;
			pChild = pChild->pNextSibling)
		{
			/* Convert the address string to the update address               */
			if (!strcmp(pChild->pName, ADDRESS_TAG_NAME))
			{
				char* endString;

				if (addressSet)
				{
					fprintf(
						stderr,
						"ERROR: Tag <address> appears more than once in "
						"<block>\n");
					goto CLEANUP;
				}

				if (NULL == pChild->pValue)
				{
					fprintf(
						stderr, "ERROR: Tag <address> does not have a value\n");
					goto CLEANUP;
				}

				pBlocks[numberBlocks].address =
					strtoul(pChild->pValue, &endString, 0);
				if (*endString)
				{
					fprintf(
						stderr,
						"ERROR: \"%s\" is an invalid address\n",
						pChild->pValue);
					goto CLEANUP;
				}

				addressSet = 1;
			}
			/* Map the update file onto memory so we can use it               */
			else if (!strcmp(pChild->pName, FILE_TAG_NAME))
			{
				if (NULL != pBlocks[numberBlocks].pData)
				{
					fprintf(
						stderr,
						"ERROR: Tag <file> appears more than once in "
						"<block>\n");
					goto CLEANUP;
				}

				if (NULL == pChild->pValue)
				{
					fprintf(
						stderr, "ERROR: Tag <file> does not have a value\n");
					goto CLEANUP;
				}

				pHandles[numberBlocks] = 
					MapFileOntoMemory(pChild->pValue);
				if (INVALID_MEMMAP_HANDLE == pHandles[numberBlocks])
				{
					fprintf(
						stderr,
						"ERROR: Failed to open update file \"%s\"\n",
						pChild->pValue);
					goto CLEANUP;
				}

				pBlocks[numberBlocks].pData     =(INT8U*)
					GetFileMappingStartAddress(pHandles[numberBlocks]);
				pBlocks[numberBlocks].length    =
					GetFileMappingSize(pHandles[numberBlocks]);
			}
			else
			{
				fprintf(
					stderr,
					"ERROR: Encountered unexpected tag <%s>\n",
					pChild->pName);
				goto CLEANUP;
			}
		}
	}

	result          = 0;
	*ppBlocks       = pBlocks;
	*ppHandles      = pHandles;
	*pNumberBlocks  = numberBlocks;

	goto EXIT;

CLEANUP:
	for (blockNumber = 0; blockNumber < numberBlocks; ++blockNumber)
	{
		if (INVALID_MEMMAP_HANDLE != pHandles[blockNumber])
		{
			UnmapFileFromMemory(pHandles[blockNumber]);
		}
	}

	free(pHandles);
	free(pBlocks);

EXIT:
	return result;
} /* ProcessXmlTree */

BOOL CFirmwareUpdate::FirmwareUpate()
{
	m_bStopRequest = FALSE;

	CUtil							util;
	CString							strResult;
	MEMMAP_HANDLE                   memmap;
	char*                           pFileStart;
	INT32U                          fileSize;
	XML_NODE*						pRoot;
	MEMMAP_HANDLE*                  pUpdateHandles = NULL;

	m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("File Loading"), (LPARAM)TEXT("Initialize"));

	/* Map the configuration file onto memory...this will make life a little  */
	/* simpler                                                                */
	memmap = ::MapFileOntoMemory(util.WCharToChar(m_strFileName));
	if (INVALID_MEMMAP_HANDLE == memmap)
	{
		///////////////////////// Send Message ///////////////////////////////
		strResult.Format(TEXT("Unable to open file \"%s\"\n"), m_strFileName);

#ifdef _DEBUG
		TRACE(strResult); 
#endif
		m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("File Loading"), (LPARAM)(LPCTSTR)strResult);

	}
	else
	{
		///////////////////////// Send Message ///////////////////////////////
		strResult.Format(TEXT("Success to open file \"%s\"\n"), m_strFileName);

#ifdef _DEBUG
		TRACE(strResult); 
#endif
		m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("File Loading"), (LPARAM)(LPCTSTR)strResult);
	}

	pFileStart  = (char *) ::GetFileMappingStartAddress(memmap);

	///////////////////////// Send Message ///////////////////////////////
	strResult.Format(TEXT("%s"), pFileStart);
	m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Start File"), (LPARAM)(LPCTSTR)strResult);

	fileSize    = ::GetFileMappingSize(memmap);

	///////////////////////// Send Message ///////////////////////////////
	strResult.Format(TEXT("%u"), fileSize);
	m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("File Size"), (LPARAM)(LPCTSTR)strResult);

	///////////////////////// Send Message ///////////////////////////////
	m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("XML Parsing"), (LPARAM)TEXT("Initialize"));
	/* Create the XML tree that represents the file.                          */
	if (NULL == 
		(pRoot = CreateXmlTree(pFileStart, fileSize)))
	{
		///////////////////////// Send Message ///////////////////////////////
		m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("XML Parsing"), (LPARAM)TEXT("File to parsing XML"));

		::UnmapFileFromMemory(memmap);
	}
	else
	{
		///////////////////////// Send Message ///////////////////////////////
		m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("XML Parsing"), (LPARAM)TEXT("Success to parsing XML"));
	}


	///////////////////////// Send Message ///////////////////////////////
	m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("XML Parsing"), (LPARAM)TEXT("Memory Allocation"));
	/* Process the XML tree to create the nonvolatile memory blocks and map   */
	/* the update files into memory                                           */
	if (ProcessXmlTree(pRoot, &m_pBlocks, &pUpdateHandles, &m_numberBlocks))
	{
		///////////////////////// Send Message ///////////////////////////////
		strResult.Format(_T("ERROR: Contents of configuration file were invalid\n"));
		m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("XML Parsing"), (LPARAM)(LPCTSTR) strResult);
#ifdef _DEBUG		
		TRACE(strResult);
#endif
		DestroyXmlTree(pRoot);
	}
	else
	{
		m_pParent->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("XML Parsing"), (LPARAM)TEXT("Success to create XML Tree"));
	}

	m_pThread = ::AfxBeginThread((AFX_THREADPROC)FirmwareUpdateThreadProc, (LPVOID)this, 0, 0,  CREATE_SUSPENDED, 0);

	if( !m_pThread )
	{
		TRACE( TEXT("Cannot start Firmware Update thread!\n") );
		return FALSE;
	}

//	m_pThread->m_bAutoDelete = FALSE; 
	m_pThread->ResumeThread();


	return TRUE;
}

DWORD WINAPI CFirmwareUpdate::FirmwareUpdateThreadProc(LPVOID pParam)
{
	CString strResult;
	CFirmwareUpdate* pFirmware = static_cast<CFirmwareUpdate *>(pParam);

	pFirmware->GetParent()->SendMessage(WM_FIRMWARE_UPDATE_START);

	if(pFirmware->GetHandle())
	{
		RFID_STATUS   status;
		pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Updating"), (LPARAM)TEXT("Initialize"));

		if (!pFirmware->GetSkipTestMode())
		{
			pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)TEXT("Initialize"));
#ifdef _DEBUG
			TRACE(TEXT("\nTest update for radio\n"));
#endif
			
			RFID_NONVOLATILE_MEMORY_BLOCK*  pBlocks = pFirmware->GetBlocks();

			for (unsigned __int32 blockNumber = 0; blockNumber < pFirmware->GetNumberofBlocks(); ++blockNumber)
			{
				pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)TEXT("Initialize"));

				strResult.Format(TEXT("Block %d:\nStart address:\t0x%.8x\nNumber of bytes:\t%u\n"), 
					blockNumber + 1, 
					pBlocks[blockNumber].address,
					pBlocks[blockNumber].length);
				pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)(LPCTSTR)strResult);

#ifdef _DEBUG
				TRACE(TEXT("  Block %d:\n"), blockNumber + 1);
				TRACE(TEXT("    Start address: 0x%.8x\n"), pBlocks[blockNumber].address);
				TRACE(TEXT("    Number of bytes: %u\n"), pBlocks[blockNumber].length);
#endif
				if(RFID_STATUS_OK != 
					(status =
					RFID_MacUpdateNonvolatileMemory(
					pFirmware->GetHandle(),
					1,
					pBlocks + blockNumber,
					RFID_FLAG_NVMEM_UPDATE_TEST)))
				{
					strResult.Format(TEXT("Test Error : %s\n"), RFIDStatusToString(status)); 
					pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)(LPCTSTR)strResult);

#ifdef _DEBUG
					TRACE(strResult);
#endif
//					return 0L;
				}
			}

			pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)TEXT("Update Test Success"));
#ifdef _DEBUG
			TRACE(TEXT("Update Test Success"));
#endif
		}

		/* If "test mode only" was not specified, then do the real update.    */
		if (!pFirmware->GetTestOnlyMode())
		{
			strResult.Format(TEXT("WARNING - DO NOT UNPLUG RADIO DURING UPDATE !!\n"));
			pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)(LPCTSTR)strResult);

#ifdef _DEBUG
			TRACE(strResult);
#endif

			if (RFID_STATUS_OK !=
				(status = RFID_MacUpdateNonvolatileMemory( pFirmware->GetHandle(),
				pFirmware->GetNumberofBlocks(),
				pFirmware->GetBlocks(),
				0)))
			{
				strResult.Format(TEXT("Test Error : %s\n"), RFIDStatusToString(status)); 
				pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)(LPCTSTR)strResult);
#ifdef _DEBUG
				TRACE(strResult);
#endif
//				return 0L;
			}

			pFirmware->GetParent()->PostMessage(WM_FIRMWARE_MSG, (WPARAM)TEXT("Firmware Test Update for Radio"), (LPARAM)TEXT("\nNonvolatile memory update on radio succeeded\n"));

			TRACE(TEXT("\nNonvolatile memory update on radio succeeded\n"));

			pFirmware->GetParent()->PostMessage(WM_FIRMWARE_UPDATE_END);
			AfxGetMainWnd()->PostMessage(WM_RADIO_RESET);
		}
	}
//	::AfxEndThread(0, FALSE); 

	return 0L;
}

// CFirmwareUpdate member functions

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -