From ff688ae6ea488901a79c017463832bd8b554b717 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 06 一月 2025 14:32:38 +0800
Subject: [PATCH] 1.引入Hsms库,封装发送接收类,模拟测试Are you there;

---
 SourceCode/Bond/HSMSSDK/Include/HSMSSDK.h    |   71 +++++
 SourceCode/Bond/Servo/Servo.vcxproj          |    6 
 SourceCode/Bond/Servo/HsmsPassive.h          |   51 +++
 SourceCode/Bond/Servo/Context.cpp            |   12 
 SourceCode/Bond/Servo/HsmsPassive.cpp        |  293 ++++++++++++++++++++++
 SourceCode/Bond/Servo/Servo.cpp              |    4 
 SourceCode/Bond/Servo/stdafx.h               |    2 
 SourceCode/Bond/Servo/Model.cpp              |    4 
 SourceCode/Bond/HSMSSDK/Include/ISECS2Item.h |   35 ++
 SourceCode/Bond/Servo/Servo.vcxproj.filters  |   18 +
 SourceCode/Bond/Servo/HsmsAction.h           |   40 +++
 SourceCode/Bond/HSMSSDK/使用说明.txt             |   24 +
 SourceCode/Bond/Servo/Model.h                |    2 
 SourceCode/Bond/HSMSSDK/Include/IPassive.h   |   40 +++
 SourceCode/Bond/Servo/Servo.rc               |    0 
 SourceCode/Bond/HSMSSDK/Include/IMessage.h   |   32 ++
 SourceCode/Bond/Servo/Context.h              |    8 
 SourceCode/Bond/Servo/HsmsAction.cpp         |  109 ++++++++
 SourceCode/Bond/Servo/Common.h               |    2 
 19 files changed, 751 insertions(+), 2 deletions(-)

diff --git a/SourceCode/Bond/HSMSSDK/Include/HSMSSDK.h b/SourceCode/Bond/HSMSSDK/Include/HSMSSDK.h
new file mode 100644
index 0000000..4231f09
--- /dev/null
+++ b/SourceCode/Bond/HSMSSDK/Include/HSMSSDK.h
@@ -0,0 +1,71 @@
+#pragma once
+#include <functional>
+#include "IPassive.h"
+#include "IMessage.h"
+
+
+#ifdef _COMPILE_AS_LIB
+#warning "compiling as lib!"
+#else
+#ifdef _DEBUG
+#ifndef _WIN64
+#pragma comment(lib, "../HSMSSDK/lib/Win32/Debug/HSMS.lib")
+#else 
+#pragma comment(lib, "../HSMSSDK/lib/x64/Debug/HSMS.lib")
+#endif
+#else 
+#ifndef _WIN64
+#pragma comment(lib, "../HSMSSDK/lib/Win32/Release/HSMS.lib")
+#else 
+#pragma comment(lib, "../HSMSSDK/lib/x64/Release/HSMS.lib")
+#endif
+#endif
+#endif // !BUILD_AS_LIB
+
+
+
+///////////////////////////////////////////////////////////////////////////////////
+//// Define C linkage if using a C++ compiler
+///////////////////////////////////////////////////////////////////////////////////
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	///////////////////////////////////////////////////////////////////////////////////
+	//// Determine callling convention baesd on compiler
+	///////////////////////////////////////////////////////////////////////////////////
+#ifdef __BORLANDC__
+
+#ifdef __WIN32__
+#define EXPORTED __declspec(dllexport) __stdcall
+#else
+#define EXPORTED FAR PASCAL __export
+#endif // WIN32
+
+#else // __BORLANDC__
+
+
+#ifdef WIN32
+#define EXPORTED __declspec(dllexport)
+#else
+#define EXPORTED FAR PASCAL __export
+#endif // WIN32
+
+#endif //__BORLANDC__
+
+
+
+	/////////////////////////////////////////////////////////////////////////////////////
+	//// 导出函数
+	////////////////////////////////////////////////////////////////////////////////////
+	int				EXPORTED	HSMS_Initialize();
+	int				EXPORTED	HSMS_Term();
+	int				EXPORTED	HSMS_CreatePassive(OUT IPassive*& pPassive, const char* pName, const unsigned int nPort);
+	int				EXPORTED	HSMS_DestroyPassive(IPassive* pPassive);
+	int				EXPORTED	HSMS_Create1Message(OUT IMessage*& pMessage, unsigned short sessionId, BYTE stream, BYTE function, unsigned int nSystemBytes);
+	int				EXPORTED	HSMS_Destroy1Message(IMessage* pMessage);
+
+#ifdef __cplusplus
+};
+
+#endif //__cplusplus
diff --git a/SourceCode/Bond/HSMSSDK/Include/IMessage.h b/SourceCode/Bond/HSMSSDK/Include/IMessage.h
new file mode 100644
index 0000000..41d1767
--- /dev/null
+++ b/SourceCode/Bond/HSMSSDK/Include/IMessage.h
@@ -0,0 +1,32 @@
+#pragma once
+#include "ISECS2Item.h"
+
+#define MSG_DATA			0
+#define MSG_SELECT_REQ		1
+#define MSG_SELECT_RSP		2
+#define MSG_DESELECT_REQ	3
+#define MSG_DESELECT_RSP	4
+#define MSG_LINKTEST_REQ	5
+#define MSG_LINKTEST_RSP	6
+#define MSG_REJECT_REQ		7
+#define MSG_SEPARATE_REQ	9
+
+#define REPLY				0x80
+typedef struct _HEADER
+{
+	unsigned short sessionId;
+	BYTE stream;
+	BYTE function;
+	BYTE pType;
+	BYTE sType;
+	unsigned int systemBytes;
+}HEADER;
+
+class IMessage
+{
+public:
+	virtual int getLength() = 0;
+	virtual HEADER* getHeader() = 0;
+	virtual ISECS2Item* getBody() = 0;
+	virtual const char* toString() = 0;
+};
diff --git a/SourceCode/Bond/HSMSSDK/Include/IPassive.h b/SourceCode/Bond/HSMSSDK/Include/IPassive.h
new file mode 100644
index 0000000..2a53a9d
--- /dev/null
+++ b/SourceCode/Bond/HSMSSDK/Include/IPassive.h
@@ -0,0 +1,40 @@
+#pragma once
+#include <functional>
+#include "IMessage.h"
+
+
+enum STATE
+{
+	NOT_CONNECTED = 0,
+	NOT_SELECTED = 1,
+	SELECTED = 2
+};
+
+typedef std::function<void(void* pFrom, STATE state)> ONSTATECHANGED;
+typedef std::function<void(void* pFrom, const char* pszData, int size)> ONRECVRAWDATA;
+typedef std::function<void(void* pFrom, IMessage* pMessage)> ONRECVDATAMSG;
+typedef std::function<void(void* pFrom, IMessage* pMessage)> ONRECVSYSMSG;
+typedef std::function<void(void* pFrom, int error)> ONDATAERROR;
+typedef struct _PassiveListener
+{
+	ONSTATECHANGED		funStateChanged;
+	ONRECVRAWDATA		funRecvRawData;
+	ONRECVDATAMSG		funRecvDataMessage;
+	ONRECVSYSMSG		funRecvSysMessage;
+	ONDATAERROR			funError;
+} PassiveListener;
+
+
+class IPassive
+{
+public:
+	virtual void setListener(PassiveListener listener) = 0;
+	virtual void setTimeout3(int timeout) = 0;
+	virtual void setTimeout5(int timeout) = 0;
+	virtual void setTimeout6(int timeout) = 0;
+	virtual void setTimeout7(int timeout) = 0;
+	virtual void setTimeout8(int timeout) = 0;
+	virtual int sendMessage(IMessage* pMessage) = 0;
+	virtual STATE getState() = 0;
+};
+
diff --git a/SourceCode/Bond/HSMSSDK/Include/ISECS2Item.h b/SourceCode/Bond/HSMSSDK/Include/ISECS2Item.h
new file mode 100644
index 0000000..e2d256c
--- /dev/null
+++ b/SourceCode/Bond/HSMSSDK/Include/ISECS2Item.h
@@ -0,0 +1,35 @@
+#pragma once
+
+enum SITYPE
+{
+	L = 000,
+	B = 010,
+	Bool = 011,
+	A = 020,
+	J = 021,
+	SLASH = 022,
+	I8 = 030,
+	I1 = 031,
+	I2 = 032,
+	I4 = 034,
+	F8 = 040,
+	F4 = 044,
+	U8 = 050,
+	U1 = 051,
+	U2 = 052,
+	U4 = 054
+};
+
+class ISECS2Item
+{
+public:
+	virtual SITYPE getType() = 0;
+	virtual const char* toString() = 0;
+	virtual bool getString(char*& pszText) = 0;
+	virtual int getSubItemSize() = 0;
+	virtual ISECS2Item* getSubItem(int index) = 0;
+	virtual bool getSubItemString(int index, char*& pszText) = 0;
+	virtual void reset() = 0;
+	virtual ISECS2Item* addItem(const char* pszText, const char* pszNote) = 0;
+	virtual ISECS2Item* addItem() = 0;
+};
diff --git "a/SourceCode/Bond/HSMSSDK/\344\275\277\347\224\250\350\257\264\346\230\216.txt" "b/SourceCode/Bond/HSMSSDK/\344\275\277\347\224\250\350\257\264\346\230\216.txt"
new file mode 100644
index 0000000..5264505
--- /dev/null
+++ "b/SourceCode/Bond/HSMSSDK/\344\275\277\347\224\250\350\257\264\346\230\216.txt"
@@ -0,0 +1,24 @@
+浣跨敤璇存槑锛�
+1.鍦ㄧ▼搴忓惎鍔ㄦ椂鍒濆鍖�
+HSMS_Initialize
+
+2.鍒涘缓鏈嶅姟绔�
+
+IPassive* m_pPassive;
+HSMS_CreatePassive(m_pPassive, "APP", 7000);
+
+璋冪敤IPassive鐨剆etListener鐩戝惉杩炴帴鐘舵��
+
+3.鏋勯�犳秷鎭拰鍙戦�佹秷鎭�
+
+IMessage* pMessage = NULL;
+HSMS_Create1Message(pMessage, m_nSessionId, 1| REPLY, 1, getSystemByte());
+	ISECS2Item* pItem = pMessage->getBody();
+	pItem->addItem((LPTSTR)(LPCTSTR)m_strEquipmentModelType, "MDLN");
+	pItem->addItem((LPTSTR)(LPCTSTR)m_strSoftRev, "SOFTREV");
+
+m_pPassive->sendMessage(pMessage);
+HSMS_Destroy1Message(pMessage);
+
+4.绋嬪簭缁撴潫鏃惰皟鐢�
+HSMS_Term()
\ No newline at end of file
diff --git a/SourceCode/Bond/Servo/Common.h b/SourceCode/Bond/Servo/Common.h
index 394b577..3761f38 100644
--- a/SourceCode/Bond/Servo/Common.h
+++ b/SourceCode/Bond/Servo/Common.h
@@ -4,6 +4,8 @@
 /* Rx Code */
 #define RX_CODE_TEST					0
 #define RX_CODE_LOG						1000
+#define RX_CODE_PASSIVE_STATUS_CHANGED	1001
+#define RX_CODE_MES_MESSAGE				1002
 
 
 /* Channel Name */
diff --git a/SourceCode/Bond/Servo/Context.cpp b/SourceCode/Bond/Servo/Context.cpp
new file mode 100644
index 0000000..7fb9d06
--- /dev/null
+++ b/SourceCode/Bond/Servo/Context.cpp
@@ -0,0 +1,12 @@
+#include "stdafx.h"
+#include "Context.h"
+
+
+CContext::CContext()
+{
+}
+
+
+CContext::~CContext()
+{
+}
diff --git a/SourceCode/Bond/Servo/Context.h b/SourceCode/Bond/Servo/Context.h
new file mode 100644
index 0000000..c23adcb
--- /dev/null
+++ b/SourceCode/Bond/Servo/Context.h
@@ -0,0 +1,8 @@
+#pragma once
+class CContext
+{
+public:
+	CContext();
+	~CContext();
+};
+
diff --git a/SourceCode/Bond/Servo/HsmsAction.cpp b/SourceCode/Bond/Servo/HsmsAction.cpp
new file mode 100644
index 0000000..fb85224
--- /dev/null
+++ b/SourceCode/Bond/Servo/HsmsAction.cpp
@@ -0,0 +1,109 @@
+#include "stdafx.h"
+#include "HsmsAction.h"
+
+
+CHsmsAction::CHsmsAction()
+{
+	m_nAction = ACTION_IDLE;
+	m_bNeedWaitReply = FALSE;
+	m_nTimeout = 45;
+	m_nResponseTime = 0;
+	m_pContext = NULL;
+	m_hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+}
+
+CHsmsAction::CHsmsAction(int nAction, BOOL bNeedReply, unsigned int nTimeout)
+{
+	m_nAction = nAction;
+	m_bNeedWaitReply = bNeedReply;
+	m_nTimeout = nTimeout;
+	m_nResponseTime = 0;
+	m_pContext = NULL;
+	m_hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+}
+
+CHsmsAction::~CHsmsAction()
+{
+	if (m_hEvent != NULL) {
+		CloseHandle(m_hEvent);
+		m_hEvent = NULL;
+	}
+
+	if (m_pSendMessage != NULL) {
+		HSMS_Destroy1Message(m_pSendMessage);
+		m_pSendMessage = NULL;
+	}
+}
+
+void CHsmsAction::reset()
+{
+	m_nAction = ACTION_IDLE;
+	m_bNeedWaitReply = FALSE;
+	m_nTimeout = 45;
+	m_nResponseTime = 0;
+	::ResetEvent(m_hEvent);
+}
+
+void CHsmsAction::resetResponseTime()
+{
+	m_nResponseTime = 0;
+}
+
+void CHsmsAction::setAction(int nAction, BOOL bNeedReply/* = TRUE*/, unsigned int nTimeout/* = 45*/)
+{
+	m_nAction = nAction;
+	m_bNeedWaitReply = bNeedReply;
+	m_nTimeout = nTimeout;
+}
+
+int CHsmsAction::getAction()
+{
+	return m_nAction;
+}
+
+void CHsmsAction::setContext(CContext* pContext)
+{
+	m_pContext = pContext;
+}
+
+CContext* CHsmsAction::getContext()
+{
+	return m_pContext;
+}
+
+void CHsmsAction::setSendMessage(IMessage* pMessage)
+{
+	m_pSendMessage = pMessage;
+}
+
+IMessage* CHsmsAction::getSendMessage()
+{
+	return m_pSendMessage;
+}
+
+unsigned int CHsmsAction::getTimeout()
+{
+	return m_nTimeout;
+}
+
+BOOL CHsmsAction::isNeedWaitReply()
+{
+	return m_bNeedWaitReply;
+}
+
+HANDLE CHsmsAction::getEvent()
+{
+	return m_hEvent;
+}
+
+int CHsmsAction::responseTimeIncrement()
+{
+	return InterlockedIncrement(&m_nResponseTime);
+}
+
+BOOL CHsmsAction::incrementAndCheckTimeout()
+{
+	unsigned int nResponseTime = InterlockedIncrement(&m_nResponseTime);
+	return nResponseTime >= m_nTimeout;
+}
+
diff --git a/SourceCode/Bond/Servo/HsmsAction.h b/SourceCode/Bond/Servo/HsmsAction.h
new file mode 100644
index 0000000..6defe08
--- /dev/null
+++ b/SourceCode/Bond/Servo/HsmsAction.h
@@ -0,0 +1,40 @@
+#pragma once
+#include "Context.h"
+
+
+#define ACTION_IDLE								0
+#define ACTION_HELLO							1		/* S1F1 */
+
+
+class CHsmsAction
+{
+public:
+	CHsmsAction();
+	CHsmsAction(int nAction, BOOL bNeedReply, unsigned int nTimeout);
+	~CHsmsAction();
+
+public:
+	void reset();
+	void resetResponseTime();
+	void setAction(int nAction, BOOL bNeedReply = TRUE, unsigned int nTimeout = 45);
+	int getAction();
+	void setContext(CContext* pContext);
+	CContext* getContext();
+	void setSendMessage(IMessage* pMessage);
+	IMessage* getSendMessage();
+	unsigned int getTimeout();
+	BOOL isNeedWaitReply();
+	HANDLE getEvent();
+	int responseTimeIncrement();
+	BOOL incrementAndCheckTimeout();
+
+private:
+	int m_nAction;
+	unsigned int m_nTimeout;				// 超时(秒)
+	unsigned int m_nResponseTime;
+	CContext* m_pContext;
+	HANDLE m_hEvent;
+	BOOL m_bNeedWaitReply;
+	IMessage* m_pSendMessage;
+};
+
diff --git a/SourceCode/Bond/Servo/HsmsPassive.cpp b/SourceCode/Bond/Servo/HsmsPassive.cpp
new file mode 100644
index 0000000..22d340d
--- /dev/null
+++ b/SourceCode/Bond/Servo/HsmsPassive.cpp
@@ -0,0 +1,293 @@
+#include "stdafx.h"
+#include "HsmsPassive.h"
+#include "Log.h"
+#include "Model.h"
+#include "Common.h"
+
+
+unsigned __stdcall CimWorkThreadFunction(LPVOID lpParam)
+{
+	CHsmsPassive* pPassive = (CHsmsPassive*)lpParam;
+	return pPassive->OnCimWork();
+}
+
+CHsmsPassive* g_pPassive = NULL;
+void CALLBACK HsmsPassiveTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
+{
+	if (g_pPassive != NULL) {
+		g_pPassive->OnTimer(nTimerid);
+	}
+}
+
+CHsmsPassive::CHsmsPassive()
+{
+	m_pPassive = nullptr;
+	m_bAreYouThereRequest = FALSE;
+	m_pModel = nullptr;
+	m_nActionTimeout = 6;
+	m_nSystemByte = 0;
+	m_strSoftRev = _T("1.0.1");
+	m_hCimWorkThreadHandle = NULL;
+	m_nCimWorkThrdaddr = 0;
+	m_bCimWorking = FALSE;
+	m_hCimWorkEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+	InitializeCriticalSection(&m_criticalSection);
+}
+
+CHsmsPassive::~CHsmsPassive()
+{
+	Lock();
+	for (auto item : m_listAction) {
+		delete item;
+	}
+	m_listAction.clear();
+	for (auto item : m_listActionSent) {
+		delete item;
+	}
+	m_listActionSent.clear();
+	Unlock();
+
+	if (m_hCimWorkEvent != NULL) {
+		CloseHandle(m_hCimWorkEvent);
+		m_hCimWorkEvent = NULL;
+	}
+	DeleteCriticalSection(&m_criticalSection);
+}
+
+void CHsmsPassive::setActionTimeout(int nSecond)
+{
+	m_nActionTimeout = max(3, nSecond);
+}
+
+void CHsmsPassive::OnTimer(UINT nTimerid)
+{
+	// 所有已发送的Action自加1
+	Lock();
+	for (auto iter = m_listActionSent.begin(); iter != m_listActionSent.end();) {
+		if ((*iter)->incrementAndCheckTimeout()) {
+			TRACE("CHsmsPassive::超时\n");
+			delete (*iter);
+			m_listActionSent.erase(iter++);
+		}
+		else {
+			++iter;
+		}
+	}
+	Unlock();
+}
+
+int CHsmsPassive::onRecvMsg(IMessage* pMessage)
+{
+	Lock();
+	CHsmsAction* pAction = nullptr;
+	for (auto iter = m_listActionSent.begin(); iter != m_listActionSent.end(); iter++) {
+		if ((*iter)->getSendMessage()->getHeader()->systemBytes == pMessage->getHeader()->systemBytes) {
+			LOGI("CHsmsPassive::找到");
+			pAction = (*iter);
+			m_listActionSent.erase(iter);
+			break;
+		}
+	}
+	Unlock();
+
+	if (pAction != nullptr) {
+		LOGI("onRecvMsg::相应处理");
+		delete pAction;
+	}
+
+
+	return 0;
+}
+
+int CHsmsPassive::init(CModel* pModel, const char* pszName, unsigned int port)
+{
+	m_pModel = pModel;
+	HSMS_CreatePassive(m_pPassive, pszName, port);
+	if (m_pPassive == NULL) {
+		return -1;
+	}
+	auto onStatusChanged = [&](void* pFrom, STATE state) -> void {
+		m_pModel->notifyInt(RX_CODE_PASSIVE_STATUS_CHANGED, (int)state);
+
+		// 连上之后发S1F1, 
+		if (STATE::SELECTED == state) {
+			m_bAreYouThereRequest = FALSE;
+			if (!m_bAreYouThereRequest) {
+				m_bAreYouThereRequest = TRUE;
+				secsAreYouThereRequest();
+			}
+		}
+	};
+	auto onRecvSysMessage = [&](void* pFrom, IMessage* pMessage) -> void {
+		LOGI("<HSMS>onRecvSysMessage:sessionId:%d, sType:%d systemBytes:%d",
+			pMessage->getHeader()->sessionId, pMessage->getHeader()->sType, pMessage->getHeader()->systemBytes);
+		onRecvMsg(pMessage);
+		if (MSG_LINKTEST_REQ == pMessage->getHeader()->sType) {
+			Sleep(10);
+			if (!m_bAreYouThereRequest) {
+				m_bAreYouThereRequest = TRUE;
+				secsAreYouThereRequest();
+			}
+		}
+	};
+
+	auto onError = [&](void* pFrom, int error) -> void {
+		LOGI("<HSMS>onError:%d", error);
+		// secsIllegalData();
+	};
+
+	auto onRecvRawData = [&](void* pFrom, const char* pszData, int size) {
+		LOGI("<HSMS>onRecvRawData:<0x%x, %d>", pszData, size);
+	};
+
+	auto onRecvDataMessage = [&](void* pFrom, IMessage* pMessage) -> void {
+		HEADER* pHeader = pMessage->getHeader();
+		int nStream = (pHeader->stream & 0x7F);
+
+		LOGI("<HSMS>收到消息 S%dF%d", nStream, pHeader->function);
+		if (nStream == 1 && pHeader->function == 1) {
+			// S1F1
+			secsAreYouThereReply(pHeader->sessionId, pHeader->systemBytes);
+		}
+	};
+
+	PassiveListener listener;
+	listener.funStateChanged = onStatusChanged;
+	listener.funRecvRawData = onRecvRawData;
+	listener.funRecvDataMessage = onRecvDataMessage;
+	listener.funRecvSysMessage = onRecvSysMessage;
+	listener.funError = onError;
+	m_pPassive->setListener(listener);
+
+	// 启动工作线程
+	m_bCimWorking = TRUE;
+	m_hCimWorkThreadHandle = (HANDLE)_beginthreadex(NULL, 0, ::CimWorkThreadFunction, this,
+		0, &m_nCimWorkThrdaddr);
+
+	g_pPassive = this;
+	SetTimer(NULL, 1, 1000, (TIMERPROC)HsmsPassiveTimerProc);
+
+	return 0;
+}
+
+int CHsmsPassive::term()
+{
+	// 结束线程
+	m_bCimWorking = FALSE;
+	SetEvent(m_hCimWorkEvent);
+	if (m_hCimWorkThreadHandle != NULL) {
+		WaitForSingleObject(m_hCimWorkThreadHandle, INFINITE);
+		CloseHandle(m_hCimWorkThreadHandle);
+		m_hCimWorkThreadHandle = NULL;
+	}
+
+	if (m_pPassive != NULL) {
+		HSMS_DestroyPassive(m_pPassive);
+		m_pPassive = NULL;
+	}
+
+	return 0;
+}
+
+unsigned CHsmsPassive::OnCimWork()
+{
+	while (m_bCimWorking) {
+
+		// 待退出信号或时间到
+		int nRet = WaitForSingleObject(m_hCimWorkEvent, INFINITE);
+		ResetEvent(m_hCimWorkEvent);
+		if (!m_bCimWorking) break;
+
+		// 取出请求列表并进行处理
+		std::list<CHsmsAction*> list;
+		Lock();
+		list.splice(list.end(), m_listAction);
+		Unlock();
+
+		while (!list.empty()) {
+			Lock();
+			CHsmsAction* pAction = list.front();
+			Unlock();
+			list.pop_front();
+			TRACE("OnCimWork 004.\n");
+
+			IMessage* pMessage = pAction->getSendMessage();
+			ASSERT(pMessage);
+			m_pPassive->sendMessage(pMessage);
+			LOGI("<HSMS> [SEND] SysByte=%u sessionId:%d", pMessage->getHeader()->systemBytes, pMessage->getHeader()->sessionId);
+
+			if (pAction->isNeedWaitReply()) {
+				// 如果需要等待回复
+				int nRet = WaitForSingleObject(pAction->getEvent(), pAction->getTimeout() * 1000);
+				if (nRet == WAIT_TIMEOUT) {
+					TRACE("Timeout...\n");
+					CContext* pContext = pAction->getContext();
+					if (pContext != NULL) {
+						//pContext->setRetCode(CRC_TIMEOUT);
+						//pContext->setRetMsg("超时");
+						//pContext->setEvent();
+					}
+				}
+
+				delete pAction;
+				pAction = NULL;
+				TRACE("delete m_pCurrentAction, next...\n");
+			}
+			else {
+				Lock();
+				m_listActionSent.push_back(pAction);
+				Unlock();
+			}
+
+
+		}
+
+		TRACE("OnCimWork \n");
+	}
+
+	// _endthreadex(0);
+	TRACE("OnCimWork exit\n");
+	return 0;
+}
+
+// S1F1
+int CHsmsPassive::secsAreYouThereRequest()
+{
+	if (m_pPassive == NULL || STATE::SELECTED != m_pPassive->getState()) {
+		return ER_NOTSELECT;
+	}
+
+	Lock();
+	CHsmsAction* pAction = new CHsmsAction(ACTION_HELLO, FALSE, m_nActionTimeout);
+	m_listAction.push_back(pAction);
+	IMessage* pMessage = NULL;
+	HSMS_Create1Message(pMessage, 0xffff, 1 | REPLY, 1, ++m_nSystemByte);
+	ASSERT(pMessage);
+	pAction->setSendMessage(pMessage);
+
+	SetEvent(m_hCimWorkEvent);
+	Unlock();
+
+	return ER_NOERROR;
+}
+
+// S1F2
+int CHsmsPassive::secsAreYouThereReply(unsigned int sessionId, unsigned int systemBytes)
+{
+	if (m_pPassive == NULL || STATE::SELECTED != m_pPassive->getState()) {
+		return ER_NOTSELECT;
+	}
+
+	IMessage* pMessage = NULL;
+	HSMS_Create1Message(pMessage, sessionId, 1, 2, systemBytes);
+	ASSERT(pMessage);
+
+	ISECS2Item* pItem = pMessage->getBody();
+	pItem->addItem(m_strEquipmentModelType.c_str(), "MDLN");
+	pItem->addItem(m_strSoftRev.c_str(), "SOFTREV");
+	m_pPassive->sendMessage(pMessage);
+	LOGI("<HSMS>[SECS Msg SEND]S1F2 (SysByte=%u)", pMessage->getHeader()->systemBytes);
+	HSMS_Destroy1Message(pMessage);
+
+	return 0;
+}
diff --git a/SourceCode/Bond/Servo/HsmsPassive.h b/SourceCode/Bond/Servo/HsmsPassive.h
new file mode 100644
index 0000000..ce0c46e
--- /dev/null
+++ b/SourceCode/Bond/Servo/HsmsPassive.h
@@ -0,0 +1,51 @@
+#pragma once
+#include <string>
+#include <list>
+#include "HsmsAction.h"
+
+
+#define ER_NOERROR			0
+#define ER_NOTSELECT		-1
+#define ER_BUSY				-2
+
+
+class CModel;
+class CHsmsPassive
+{
+public:
+	CHsmsPassive();
+	~CHsmsPassive();
+
+public:
+	unsigned OnCimWork();
+	void OnTimer(UINT nTimerid);
+	void setActionTimeout(int nSecond);
+	int init(CModel* pModel, const char* pszName, unsigned int port);
+	int term();
+	int secsAreYouThereRequest();
+	int secsAreYouThereReply(unsigned int sessionId, unsigned int systemBytes);
+
+private:
+	inline void Lock() { EnterCriticalSection(&m_criticalSection); }
+	inline void Unlock() { LeaveCriticalSection(&m_criticalSection); }
+	int onRecvMsg(IMessage* pMessage);
+
+private:
+	CModel* m_pModel;
+	IPassive* m_pPassive;
+	int m_nActionTimeout;
+	unsigned int m_nSystemByte;
+	BOOL m_bAreYouThereRequest;
+	std::string m_strEquipmentModelType;
+	std::string m_strSoftRev;
+	CRITICAL_SECTION m_criticalSection;
+	std::list<CHsmsAction*> m_listAction;
+	std::list<CHsmsAction*> m_listActionSent;
+
+private:
+	BOOL m_bCimWorking;
+	HANDLE m_hCimWorkEvent;
+	HANDLE m_hCimWorkThreadHandle;
+	unsigned m_nCimWorkThrdaddr;
+};
+
diff --git a/SourceCode/Bond/Servo/Model.cpp b/SourceCode/Bond/Servo/Model.cpp
index 4526155..548e170 100644
--- a/SourceCode/Bond/Servo/Model.cpp
+++ b/SourceCode/Bond/Servo/Model.cpp
@@ -52,11 +52,15 @@
 	});
 
 
+	m_hsmsPassive.init(this, "APP", 7000);
+
+
 	return 0;
 }
 
 int CModel::term()
 {
+	m_hsmsPassive.term();
 	CLog::GetLog()->SetOnLogCallback(nullptr);
 	return 0;
 }
diff --git a/SourceCode/Bond/Servo/Model.h b/SourceCode/Bond/Servo/Model.h
index b6b954f..00733d2 100644
--- a/SourceCode/Bond/Servo/Model.h
+++ b/SourceCode/Bond/Servo/Model.h
@@ -1,5 +1,6 @@
 #pragma once
 #include "Configuration.h"
+#include "HsmsPassive.h"
 
 class CModel
 {
@@ -29,6 +30,7 @@
 
 public:
 	CConfiguration m_configuration;
+	CHsmsPassive m_hsmsPassive;
 
 private:
 	IObservable* m_pObservable;
diff --git a/SourceCode/Bond/Servo/Servo.cpp b/SourceCode/Bond/Servo/Servo.cpp
index 9f7e3fe..b422966 100644
--- a/SourceCode/Bond/Servo/Servo.cpp
+++ b/SourceCode/Bond/Servo/Servo.cpp
@@ -88,8 +88,9 @@
 	CServoGraph::RegisterWndClass();
 
 
-	// 初始化BEQ库
+	// 初始化Rx库
 	RX_Init();
+	HSMS_Initialize();
 
 
 	CServoDlg dlg;
@@ -125,6 +126,7 @@
 int CServoApp::ExitInstance()
 {
 	m_model.term();
+	HSMS_Term();
 	RX_Term();
 
 	return CWinApp::ExitInstance();
diff --git a/SourceCode/Bond/Servo/Servo.rc b/SourceCode/Bond/Servo/Servo.rc
index 3fdbd84..57cf59a 100644
--- a/SourceCode/Bond/Servo/Servo.rc
+++ b/SourceCode/Bond/Servo/Servo.rc
Binary files differ
diff --git a/SourceCode/Bond/Servo/Servo.vcxproj b/SourceCode/Bond/Servo/Servo.vcxproj
index 6fc02ae..8b45150 100644
--- a/SourceCode/Bond/Servo/Servo.vcxproj
+++ b/SourceCode/Bond/Servo/Servo.vcxproj
@@ -193,6 +193,9 @@
     <ClInclude Include="BlButton.h" />
     <ClInclude Include="Common.h" />
     <ClInclude Include="Configuration.h" />
+    <ClInclude Include="Context.h" />
+    <ClInclude Include="HsmsAction.h" />
+    <ClInclude Include="HsmsPassive.h" />
     <ClInclude Include="Log.h" />
     <ClInclude Include="LogDlg.h" />
     <ClInclude Include="LogEdit.h" />
@@ -207,6 +210,9 @@
   <ItemGroup>
     <ClCompile Include="BlButton.cpp" />
     <ClCompile Include="Configuration.cpp" />
+    <ClCompile Include="Context.cpp" />
+    <ClCompile Include="HsmsAction.cpp" />
+    <ClCompile Include="HsmsPassive.cpp" />
     <ClCompile Include="Log.cpp" />
     <ClCompile Include="LogDlg.cpp" />
     <ClCompile Include="LogEdit.cpp" />
diff --git a/SourceCode/Bond/Servo/Servo.vcxproj.filters b/SourceCode/Bond/Servo/Servo.vcxproj.filters
index da9d2bb..4023d01 100644
--- a/SourceCode/Bond/Servo/Servo.vcxproj.filters
+++ b/SourceCode/Bond/Servo/Servo.vcxproj.filters
@@ -57,6 +57,15 @@
     <ClInclude Include="Log.h">
       <Filter>澶存枃浠�</Filter>
     </ClInclude>
+    <ClInclude Include="HsmsPassive.h">
+      <Filter>澶存枃浠�</Filter>
+    </ClInclude>
+    <ClInclude Include="HsmsAction.h">
+      <Filter>澶存枃浠�</Filter>
+    </ClInclude>
+    <ClInclude Include="Context.h">
+      <Filter>澶存枃浠�</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Servo.cpp">
@@ -89,6 +98,15 @@
     <ClCompile Include="Log.cpp">
       <Filter>婧愭枃浠�</Filter>
     </ClCompile>
+    <ClCompile Include="HsmsPassive.cpp">
+      <Filter>婧愭枃浠�</Filter>
+    </ClCompile>
+    <ClCompile Include="HsmsAction.cpp">
+      <Filter>婧愭枃浠�</Filter>
+    </ClCompile>
+    <ClCompile Include="Context.cpp">
+      <Filter>婧愭枃浠�</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="Servo.rc">
diff --git a/SourceCode/Bond/Servo/stdafx.h b/SourceCode/Bond/Servo/stdafx.h
index ee67f29..ea7a70f 100644
--- a/SourceCode/Bond/Servo/stdafx.h
+++ b/SourceCode/Bond/Servo/stdafx.h
@@ -36,7 +36,7 @@
 
 
 #include "..\RxWindows1.0\include\RxWindowsLib.h"
-
+#include "..\HSMSSDK\Include\HSMSSDK.h"
 
 
 

--
Gitblit v1.9.3