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

📄 firmwareupdate.cpp

📁 UHF RFID Reader Program
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ../FirmwareUpdate.cpp : implementation file
//

#include "stdafx.h"
#include "RFRM.h"
#include "FirmwareUpdate.h"

#include "Util.h"
#include "intel_stack.h"
// CFirmwareUpdate

const char*     ROOT_TAG_NAME       = "blocks";
const char*     BLOCK_TAG_NAME      = "block";
const char*     ADDRESS_TAG_NAME    = "address";
const char*     FILE_TAG_NAME       = "file";


CFirmwareUpdate::CFirmwareUpdate(RFID_RADIO_HANDLE handle) 
: m_pBlocks(NULL)
, m_bSkipTestMode(FALSE)
, m_bTestModeOnly(TRUE)
, m_pRoot(NULL)
, m_bStopRequest(FALSE)
, m_pThread(NULL)
, m_strFileName(TEXT(""))
, m_rfidHandle(handle)
, m_pParent(NULL)
{
	m_rfidHandle = handle;
}

CFirmwareUpdate::CFirmwareUpdate(RFID_RADIO_HANDLE handle, BOOL skipTestMode, BOOL testModeOnly)
: m_pBlocks(NULL)
, m_pRoot(NULL)
, m_bStopRequest(FALSE)
, m_pThread(NULL)
, m_strFileName(TEXT(""))
, m_rfidHandle(handle)
, m_bSkipTestMode(skipTestMode)
, m_bTestModeOnly(testModeOnly)
, m_pParent(NULL)
{
// 	m_rfidHandle = handle;
// 	m_bSkipTestMode = skipTestMode;
// 	m_bTestModeOnly = testModeOnly;
}

CFirmwareUpdate::CFirmwareUpdate(RFID_RADIO_HANDLE handle, LPCTSTR filename, BOOL skipTestMode, BOOL testModeOnly)
: m_pBlocks(NULL)
, m_pRoot(NULL)
, m_bStopRequest(FALSE)
, m_pThread(NULL)
, m_strFileName(filename)
, m_rfidHandle(handle)
, m_bSkipTestMode(skipTestMode)
, m_bTestModeOnly(testModeOnly)
, m_pParent(NULL)
{

}

CFirmwareUpdate::CFirmwareUpdate(RFID_RADIO_HANDLE handle, LPCTSTR filename, BOOL skipTestMode, BOOL testModeOnly, CWnd* pParent)
: m_pBlocks(NULL)
, m_pRoot(NULL)
, m_bStopRequest(FALSE)
, m_pThread(NULL)
, m_strFileName(filename)
, m_rfidHandle(handle)
, m_bSkipTestMode(skipTestMode)
, m_bTestModeOnly(testModeOnly)
, m_pParent(pParent)
{

}

CFirmwareUpdate::CFirmwareUpdate(RFID_RADIO_HANDLE handle, LPCTSTR filename, RFID_RADIO_FIRMWARE_MODE mode, CWnd* pParent)
: m_pBlocks(NULL)
, m_pRoot(NULL)
, m_bStopRequest(FALSE)
, m_pThread(NULL)
, m_strFileName(filename)
, m_rfidHandle(handle)
, m_pParent(pParent)
{
	switch(mode)
	{
	case RFID_RADIO_FIRMWARE_UPDATE_ALL:
		m_bSkipTestMode = FALSE;
		m_bTestModeOnly = FALSE;
		break;
	case RFID_RADIO_FIRMWARE_TEST_ONLY:
		m_bSkipTestMode = FALSE;
		m_bTestModeOnly = TRUE;

		break;
	case RFID_RADIO_FIRMWARE_SKIP_TEST:
		m_bSkipTestMode = TRUE;
		m_bTestModeOnly = FALSE;
		break;
	default:
		m_bSkipTestMode = FALSE;
		m_bTestModeOnly = FALSE;
		break;
	}
}


CString RFIDStatusToString(
							   RFID_STATUS status
							   )
{
	CString pString;

	switch(status)
	{
	case RFID_STATUS_OK:
		pString.LoadString(IDS_STRING_RFID_STATUS_OK);
		break;

	case RFID_ERROR_ALREADY_OPEN:
		pString.LoadString(IDS_STRING_RFID_ERROR_ALREADY_OPEN);
		break;

	case RFID_ERROR_BUFFER_TOO_SMALL:
		pString.LoadString(IDS_STRING_RFID_ERROR_BUFFER_TOO_SMALL);
		break;

	case RFID_ERROR_CURRENTLY_NOT_ALLOWED:
		pString.LoadString(IDS_STRING_RFID_ERROR_CURRENTLY_NOT_ALLOWED);
		break;

	case RFID_ERROR_FAILURE:
		pString.LoadString(IDS_STRING_RFID_ERROR_FAILURE);
		break;

	case RFID_ERROR_DRIVER_LOAD:
		pString.LoadString(IDS_STRING_RFID_ERROR_DRIVER_LOAD);
		break;

	case RFID_ERROR_DRIVER_MISMATCH:
		pString.LoadString(IDS_STRING_RFID_ERROR_DRIVER_MISMATCH);
		break;

	case RFID_ERROR_EMULATION_MODE:
		pString.LoadString(IDS_STRING_RFID_ERROR_EMULATION_MODE);
		break;

	case RFID_ERROR_INVALID_ANTENNA:
		pString.LoadString(IDS_STRING_RFID_ERROR_INVALID_ANTENNA);
		break;

	case RFID_ERROR_INVALID_HANDLE:
		pString.LoadString(IDS_STRING_RFID_ERROR_INVALID_HANDLE);
		break;

	case RFID_ERROR_INVALID_PARAMETER:
		pString.LoadString(IDS_STRING_RFID_ERROR_INVALID_PARAMETER);
		break;
	case RFID_ERROR_NO_SUCH_RADIO:
		pString.LoadString(IDS_STRING_RFID_ERROR_NO_SUCH_RADIO);
		break;

	case RFID_ERROR_NOT_INITIALIZED:
		pString.LoadString(IDS_STRING_RFID_ERROR_NOT_INITIALIZED);
		break;

	case RFID_ERROR_NOT_SUPPORTED:
		pString.LoadString(IDS_STRING_RFID_ERROR_NOT_SUPPORTED);
		break;

	case RFID_ERROR_OPERATION_CANCELLED:
		pString.LoadString(IDS_STRING_RFID_ERROR_OPERATION_CANCELLED);
		break;

	case RFID_ERROR_OUT_OF_MEMORY:
		pString.LoadString(IDS_STRING_RFID_ERROR_OUT_OF_MEMORY);
		break;

	case RFID_ERROR_RADIO_BUSY:
		pString.LoadString(IDS_STRING_RFID_ERROR_RADIO_BUSY);
		break;

	case RFID_ERROR_RADIO_FAILURE:
		pString.LoadString(IDS_STRING_RFID_ERROR_RADIO_FAILURE);
		break;

	case RFID_ERROR_RADIO_NOT_PRESENT:
		pString.LoadString(IDS_STRING_RFID_ERROR_RADIO_NOT_PRESENT);
		break;

	case RFID_ERROR_RADIO_NOT_RESPONDING:
		pString.LoadString(IDS_STRING_RFID_ERROR_RADIO_NOT_RESPONDING);
		break;

	case RFID_ERROR_NONVOLATILE_INIT_FAILED:
		pString.LoadString(IDS_STRING_RFID_ERROR_NONVOLATILE_INIT_FAILED);
		break;

	case RFID_ERROR_NONVOLATILE_OUT_OF_BOUNDS:
		pString.LoadString(IDS_STRING_RFID_ERROR_NONVOLATILE_OUT_OF_BOUNDS);
		break;

	case RFID_ERROR_NONVOLATILE_WRITE_FAILED:
		pString.LoadString(IDS_STRING_RFID_ERROR_NONVOLATILE_WRITE_FAILED);
		break;

	default:
		pString.LoadString(IDS_STRING_RFID_ERROR_UNKNOWN);
		break;
	}

	return pString;
} /* RFIDStatusToString */


CFirmwareUpdate::~CFirmwareUpdate()
{
//	UnmapFileFromMemory();
}

XML_NODE* CFirmwareUpdate::CreateXmlTree( const char* pFileStream,
										  INT32U      length)
{
//	XML_NODE*       pRoot = NULL;
	STACK_HANDLE    stack = INVALID_STACK_HANDLE;

	/* Create a stack that is used during process of XML text                 */
	stack = StackCreate();
	if (INVALID_STACK_HANDLE == stack)
	{
		goto EXIT;
	}

	while (1)
	{
		/* Get rid of any whitespace and if we are at the end of the file data*/
		/* then we are done.                                                  */
		for ( ; length && isspace(*pFileStream); --length, ++pFileStream);
		if (!length)
		{
			break;
		}

		/* If we are at a tag, then we need to figure out the tag name        */
		if ('<' == *pFileStream)
		{
			/* Find the tag's closing >                                       */
			const char* pStartTag = ++pFileStream;
			for (--length;
				length && ('>' != *pFileStream);
				--length, ++pFileStream);
			if (pStartTag == pFileStream)
			{
				fprintf(stderr, "ERROR: Found an empty XML begin tag\n");
				goto DESTROY_XML_TREE;
			}

			/* If this is a begin tag, then we'll create a new tree node and  */
			/* push it onto the stack.                                        */
			if ('/' != *pStartTag)
			{
				/* Allocate and initialize a new node.                        */
				XML_NODE* pNewNode = (XML_NODE *) malloc(sizeof(XML_NODE));
				if (NULL == pNewNode)
				{
					fprintf(stderr, "ERROR: Failed to allocate memory\n");
					goto DESTROY_XML_TREE;
				}
				pNewNode->pNextSibling  = NULL;
				pNewNode->pFirstChild   = NULL;
				pNewNode->pLastChild    = NULL;
				pNewNode->pValue        = NULL;
				pNewNode->pName         = NULL;

				/* If the stack is empty, then this tag is the root           */
				if (StackEmpty(stack))
				{
					if (NULL != m_pRoot)
					{
						fprintf(
							stderr,
							"ERROR: Found a second root tag named %.*s\n",
							pFileStream - pStartTag,
							pStartTag);
						free(pNewNode);
						goto DESTROY_XML_TREE;
					}
					m_pRoot = pNewNode;
				}
				/* Otherwise, the tag is a child of the node that is on top   */
				/* of the stack.  Additionally, if the top node already has   */
				/* children, then this child is a sibling to that(those)      */
				/* node(s)                                                    */
				else
				{
					XML_NODE* pTop = (XML_NODE *) StackTop(stack);
					if (NULL == pTop->pFirstChild)
					{
						pTop->pFirstChild = pNewNode;
					}
					else
					{
						pTop->pLastChild->pNextSibling = pNewNode;
					}
					pTop->pLastChild = pNewNode;
				}

				if (StackPush(stack, pNewNode))
				{
					fprintf(stderr, "ERROR: Failed to push value on stack\n");
					free(pNewNode);
					goto DESTROY_XML_TREE;
				}

				/* Copy the tag's name                                        */
				pNewNode->pName     = 
					(char *) malloc(pFileStream - pStartTag + 1);
				if (NULL == pNewNode->pName)
				{
					fprintf(stderr, "ERROR: Failed to allocate memory\n");
					goto DESTROY_XML_TREE;
				}
				strncpy(pNewNode->pName, pStartTag, pFileStream - pStartTag);
				pNewNode->pName[pFileStream - pStartTag] = '\0';
			}
			/* Otherwise, since this is an end tag, then we need to ensure    */
			/* it matches the tag on top of the stack and then pop the stack. */
			else
			{
				/* Verify that the end tag is not empty                       */
				if (++pStartTag == pFileStream)
				{
					fprintf(stderr, "ERROR: Found an empty XML end tag\n");
					goto DESTROY_XML_TREE;
				}

				/* Verify that the end tag name matches the name of the tag  */
				/* that is on top of the stack.                              */
				if (StackEmpty(stack))
				{
					fprintf(
						stderr,
						"ERROR: Found an end tag, but no start tag\n");
					goto DESTROY_XML_TREE;
				}
				if (strncmp(
					((XML_NODE *) StackTop(stack))->pName,
					pStartTag,
					pFileStream - pStartTag))
				{
					fprintf(
						stderr,
						"ERROR: End tag </%.*s> doesn't match begin tag <%s>\n",
						pFileStream - pStartTag,
						pStartTag,
						((XML_NODE *) StackTop(stack))->pName);
					goto DESTROY_XML_TREE;
				}

				/* Since we have found the matching end tag, we can pop the   */
				/* tag off of the stack                                       */
				StackPop(stack);
			}

			/* Move past the tag closing >                                    */
			--length;
			++pFileStream;
		}
		/* Otherwise, we are at a data value                                  */
		else
		{
			const char* pStartValue = pFileStream;
			XML_NODE*   pNode;

			/* Ensure that there is actually a tag on top of the stack        */
			if (StackEmpty(stack))
			{
				fprintf(
					stderr,
					"ERROR: Found a tag value, but not inside of tag\n");
				goto DESTROY_XML_TREE;
			}

			/* Find the end of the value                                      */
			for ( ; length && ('<' != *pFileStream); --length, ++pFileStream);
			if (!length)
			{
				fprintf(
					stderr,
					"ERROR: Reached end of file in middle of tag value\n");
				goto DESTROY_XML_TREE;
			}

			/* Copy the value into the XML node                               */
			pNode           = (XML_NODE *) StackTop(stack);
			pNode->pValue   = (char *) malloc(pFileStream - pStartValue + 1);
			if (NULL == pNode->pValue)
			{
				fprintf(stderr, "ERROR: Failed to allocate memory\n");
				goto DESTROY_XML_TREE;
			}
			strncpy(pNode->pValue, pStartValue, pFileStream - pStartValue);
			pNode->pValue[pFileStream - pStartValue] = '\0';
		}
	}

	/* At this point, the stack should be empty - meaning that the file has   */
	/* been completely processed and an opening/closing tag should have been  */
	/* found.                                                                 */
	if (!StackEmpty(stack))
	{
		fprintf(stderr,
			"ERROR: Found additional non-whitespace after closing tag\n");
		goto DESTROY_XML_TREE;
	}

	goto DESTROY_STACK;

DESTROY_XML_TREE:
	DestroyXmlTree(m_pRoot);
	m_pRoot = NULL;

DESTROY_STACK:

⌨️ 快捷键说明

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