chenluhua1980
2026-01-09 570e812442a64360239f558ae4b55c0d204a5523
SourceCode/Bond/Servo/HsmsPassive.cpp
@@ -12,6 +12,9 @@
#include <set>
#include <regex>
// ControlState values (keep in sync with Model::ControlState / VariableList.txt)
static constexpr uint8_t kControlStateOnlineRemote = 5;
const char ACK[2] = {0, 1};
const char* ACK0 = &ACK[0];
@@ -110,23 +113,35 @@
   ASSERT(pParent);
   ASSERT(pVariable);
   std::string svNote("SV");
   {
      SERVO::CVariable* pDef = getVariable((int)pVariable->getVarialbleId());
      if (pDef == nullptr) {
         pDef = pVariable;
      }
      auto& name = pDef->getName();
      if (!name.empty()) {
         svNote += " -> ";
         svNote += name;
      }
   }
   ISECS2Item* pItemList;
   SERVO::SVFromat format = pVariable->getFormat();
   switch (format)
   {
   case SERVO::SVFromat::U1:
      pParent->addU1Item((unsigned char)pVariable->getIntValue(), "SV");
      pParent->addU1Item((unsigned char)pVariable->getIntValue(), svNote.c_str());
      break;
   case SERVO::SVFromat::U2:
      pParent->addU2Item((unsigned char)pVariable->getIntValue(), "SV");
      pParent->addU2Item((unsigned char)pVariable->getIntValue(), svNote.c_str());
      break;
   case SERVO::SVFromat::I2:
      pParent->addI2Item((unsigned char)pVariable->getIntValue(), "SV");
      pParent->addI2Item((unsigned char)pVariable->getIntValue(), svNote.c_str());
      break;
   case SERVO::SVFromat::A20:
   case SERVO::SVFromat::A50:
      pParent->addItem(pVariable->getValue().c_str(), "SV");
      pParent->addItem(pVariable->getValue().c_str(), svNote.c_str());
      break;
   case SERVO::SVFromat::L:
      pItemList = pParent->addItem();
@@ -432,6 +447,21 @@
   return nullptr;
}
int CHsmsPassive::getCurrentControlState()
{
   auto v = getVariable("CurrentControlState");
   if (v != nullptr) {
      return static_cast<int>(v->getIntValue());
   }
   return 0;
}
bool CHsmsPassive::isHostCommandAllowed()
{
   // Only allow host control commands in OnlineRemote.
   return getCurrentControlState() == kControlStateOnlineRemote;
}
void CHsmsPassive::clearAllVariabel()
{
   for (auto item : m_variabels) {
@@ -495,26 +525,37 @@
void CHsmsPassive::setVariableValue(const char* pszName, __int64 value)
{
   auto v = getVariable(pszName);
   if (v != nullptr) {
   // Protect variable list updates; multiple threads may set SVs.
   Lock();
   if (auto v = getVariable(pszName)) {
      v->setValue(value);
   }
   Unlock();
}
void CHsmsPassive::setVariableValue(const char* pszName, const char* value)
{
   auto v = getVariable(pszName);
   if (v != nullptr) {
   Lock();
   if (auto v = getVariable(pszName)) {
      v->setValue(value);
   }
   Unlock();
}
void CHsmsPassive::setVariableValue(const char* pszName, std::vector<SERVO::CVariable>& vars)
{
   auto v = getVariable(pszName);
   if (v != nullptr) {
   Lock();
   if (auto v = getVariable(pszName)) {
      v->setValue(vars);
   }
   Unlock();
}
void CHsmsPassive::withVariableLock(const std::function<void()>& fn)
{
   Lock();
   if (fn) fn();
   Unlock();
}
static bool isValidFormat(const std::string& fmt)
@@ -1590,7 +1631,8 @@
            ASSERT(pMessage);
            m_pPassive->sendMessage(pMessage);
            LOGI("<HSMS> [SEND] SysByte=%u sessionId:%d", pMessage->getHeader()->systemBytes, pMessage->getHeader()->sessionId);
            LOGI("<HSMS>[SEND]SysByte=%u sessionId:%d", pMessage->getHeader()->systemBytes, pMessage->getHeader()->sessionId);
            LOGI("<HSMS>[SEND]%s", pMessage->toString());
            int nRet = WaitForSingleObject(pAction->getEvent(), pAction->getTimeout() * 1000);
            if (nRet == WAIT_TIMEOUT) {
@@ -1615,7 +1657,8 @@
            ASSERT(pMessage);
            m_pPassive->sendMessage(pMessage);
            LOGI("<HSMS> [SEND] SysByte=%u sessionId:%d", pMessage->getHeader()->systemBytes, pMessage->getHeader()->sessionId);
            LOGI("<HSMS>[SEND]SysByte=%u sessionId:%d", pMessage->getHeader()->systemBytes, pMessage->getHeader()->sessionId);
            LOGI("<HSMS>[SEND]%s", pMessage->toString());
         }
      }
@@ -1770,8 +1813,15 @@
      goto MYREPLY;
   }
   if (!pBody->getSubItemU2(0, SVID)) {
      pMessage->getBody()->addU1Item(SVU1, "SV");
      goto MYREPLY;
      // also accept I2 or U4 to be tolerant with host implementations
      if (!pBody->getSubItemI2(0, (short&)SVID)) {
         unsigned int svidU4 = 0;
         if (!pBody->getSubItemU4(0, svidU4)) {
            pMessage->getBody()->addU1Item(SVU1, "SV");
            goto MYREPLY;
         }
         SVID = static_cast<unsigned short>(svidU4);
      }
   }
   SERVO::CVariable* pVariable = getVariable((int)SVID);
@@ -2200,6 +2250,13 @@
      goto MYREPLY;
   }
   if (!isHostCommandAllowed()) {
      CAACK = CAACK_5;
      ERRCODE = CAACK_5;
      strError = "rejected - ControlState not OnlineRemote";
      goto MYREPLY;
   }
   ISECS2Item* pBody = pRecv->getBody();
   if (pBody == nullptr || pBody->getType() != SITYPE::L) ER_PARAM_ERROR;
@@ -2403,6 +2460,13 @@
   ISECS2Item* pReplyItemAck = pReplyItemAcks->addU1Item(0, "OBJACK");
   ISECS2Item* pReplyItemErrs = pReplyItemAcks->addItem();
   if (!isHostCommandAllowed()) {
      ISECS2Item* pItemError = pReplyItemErrs->addItem();
      pItemError->addU4Item(2001, "ERRCODE");
      pItemError->addItem("rejected - ControlState not OnlineRemote", "ERRTEXT");
      goto MYREPLY;
   }
   // 当前只处理类各为ControlJob
   if (_strcmpi(pszObjType, "ControlJob") == 0) {
@@ -2517,6 +2581,25 @@
   ISECS2Item* pBody = pRecv->getBody();
   if (pBody == nullptr || pBody->getType() != SITYPE::L) ER_PARAM_ERROR;
   if (!isHostCommandAllowed()) {
      IMessage* pMessage = NULL;
      HSMS_Create1Message(pMessage, m_nSessionId, 16, 16, ++m_nSystemByte);
      ASSERT(pMessage);
      ISECS2Item* pItemPrjobIds = pMessage->getBody()->addItem();
      ISECS2Item* pItemErrors = pMessage->getBody()->addItem();
      pItemErrors->addBoolItem(false, "ACKA");
      ISECS2Item* pItemErrors2 = pItemErrors->addItem();
      auto err = pItemErrors2->addItem();
      err->addU4Item(2001, "ERRCODE");
      err->addItem("rejected - ControlState not OnlineRemote", "ERRTEXT");
      m_pPassive->sendMessage(pMessage);
      LOGI("<HSMS>[SEND]sessionId:%d, sType:%d systemBytes:%d",
         pMessage->getHeader()->sessionId, pMessage->getHeader()->sType, pMessage->getHeader()->systemBytes);
      LOGI("<HSMS>[SEND]%s", pMessage->toString());
      HSMS_Destroy1Message(pMessage);
      return 0;
   }
   // 解释数据,得到CProcessJob
   ISECS2Item* pItemPjs, * pItemPj,* pItemCarriers, * pItemCarrier, *pItemSlots, *pItemRecipes;
@@ -2569,9 +2652,6 @@
      pjs.push_back(pj);
   }
   ASSERT(m_listener.onPRJobMultiCreate != nullptr);
   int nRet = m_listener.onPRJobMultiCreate(this, pjs);
   // 回复报文
   IMessage* pMessage = NULL;
@@ -2604,6 +2684,10 @@
      pMessage->getHeader()->sessionId, pMessage->getHeader()->sType, pMessage->getHeader()->systemBytes);
   LOGI("<HSMS>[SEND]%s", pMessage->toString());
   HSMS_Destroy1Message(pMessage);
   ASSERT(m_listener.onPRJobMultiCreate != nullptr);
   int nRet = m_listener.onPRJobMultiCreate(this, pjs);
   // 释放有问题(未添加到master)的内存
@@ -2670,7 +2754,15 @@
   ISECS2Item* pItem = pMessage->getBody();
   // pItem->addU2Item(++DATAID, "DATAID");      // 根据别的日志显示DATAID恒为0,所以我们先照使用0
   pItem->addU2Item(0, "DATAID");
   pItem->addU4Item(CEID, "CEID");
   std::string ceidNote("CEID");
   if (pEvent != nullptr) {
      auto& name = pEvent->getName();
      if (!name.empty()) {
         ceidNote += " -> ";
         ceidNote += name;
      }
   }
   pItem->addU4Item(CEID, ceidNote.c_str());
   ISECS2Item* pItemList1 = pItem->addItem(); // L[n] reports
   if (pReport != nullptr) {
      ISECS2Item* pItemList2 = pItemList1->addItem();