VMSCThreadRecvF.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. #include "VMSCThread.h"
  4. #include "VMSCThreadRecvF.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. /*
  8. * 패킷 수신
  9. * arguments
  10. * void
  11. * return
  12. * void
  13. */
  14. int __fastcall TVMSCThread::RecvPacket(BYTE *ABuffer, int ALen)
  15. {
  16. FErrLine = 3;
  17. int result = SYS_ERR_NONE;
  18. BYTE data;
  19. PKT_HEAD *pHd = (PKT_HEAD*)m_RxBuff;
  20. if (ALen > 0)
  21. {
  22. LogData("RECV", (char*)ABuffer, ALen);
  23. }
  24. m_rcvPacketTimer = Now();
  25. for (int ii = 0; ii < ALen; ii++)
  26. {
  27. data = ABuffer[ii];
  28. switch(m_RxState)
  29. {
  30. case RX_STX:
  31. if (data == VMS_STX)
  32. {
  33. m_RxBuff[m_RxLen++] = data;
  34. m_RxState = RX_HEAD;
  35. }
  36. else
  37. {
  38. LERROR("Receive packet format sequence error[STX]: %02X, %d", data, m_RxLen);
  39. InitRx();
  40. }
  41. break;
  42. case RX_HEAD:
  43. m_RxBuff[m_RxLen++] = data;
  44. if (m_RxLen == (int)sizeof(PKT_HEAD))
  45. {
  46. m_RxDataLen = htonl(pHd->Size);
  47. if (m_RxDataLen > (CLIENT_RCV_BUF_SIZE-10) || m_RxDataLen == 0)
  48. {
  49. LERROR("Receive packet length error: %d", m_RxDataLen);
  50. InitRx();
  51. }
  52. else
  53. {
  54. m_RxPktLen = 1 + 1 + m_RxDataLen; // stx+etx+data(id~crc)
  55. m_RxState = RX_DATA;
  56. }
  57. }
  58. break;
  59. case RX_DATA:
  60. // 나머지 데이터에 대한 유효성 처리는 하지 않는다.(ETX 체크안함)
  61. m_RxBuff[m_RxLen++] = data;
  62. if (m_RxLen == m_RxPktLen)
  63. {
  64. //패킷완성
  65. result = ProcessRecvPacket(m_RxBuff, m_RxPktLen);
  66. InitRx();
  67. m_WatchDogTimer = Now();
  68. }
  69. break;
  70. }
  71. }
  72. return result;
  73. }
  74. //---------------------------------------------------------------------------
  75. /*
  76. * 수신 패킷 처리
  77. * arguments
  78. * void
  79. * return
  80. * void
  81. */
  82. int __fastcall TVMSCThread::ProcessRecvPacket(BYTE *ABuffer, int ARcvLen)
  83. {
  84. FErrLine = 4;
  85. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  86. int result = SYS_ERR_NONE;
  87. InfoPacket(ABuffer, ARcvLen, false);
  88. switch(Pkt->hd.OpCode)
  89. {
  90. case RES_VMS_STATUS:// 0x50 // Status 응답, VMS->센터
  91. result = ResVmsStatus(ABuffer, ARcvLen);
  92. break;
  93. case RES_VMS_MSG_DOWNLOAD:// 0x51 // 문안 전송 응답, VMS->센터
  94. result = ResVmsMsgDownload(ABuffer, ARcvLen);
  95. break;
  96. case RES_VMS_LIB_DOWNLOAD:// 0x52 // Library download 응답, VMS->센터
  97. result = ResVmsLibDownload(ABuffer, ARcvLen);
  98. break;
  99. case RES_VMS_PARAM_DOWNLOAD:// 0x53 // Parameter download 응답, VMS->센터
  100. break;
  101. case RES_VMS_BLANK_DISP:// 0x54 // Blank 응답, VMS->센터
  102. break;
  103. case RES_VMS_DEF_MSG_DISP:// 0x55 // Default message display 응답, VMS->센터
  104. break;
  105. case RES_VMS_MODULE_STATUS:// 0x56 // Module status upload 응답, VMS->센터
  106. result = ResVmsModuleStatus(ABuffer, ARcvLen);
  107. break;
  108. case RES_VMS_INITIALIZE:// 0x57 // Initialize 응답, VMS->센터
  109. result = ResVmsInitialize(ABuffer, ARcvLen);
  110. break;
  111. case RES_VMS_LUMINANCE_LEVEL_SET:// 0x58 // Luminance level set 응답, VMS->센터
  112. break;
  113. case RES_SIGNBOARD_POWER_CONTROL:// 0x59 // Signboard power control 응답, VMS->센터
  114. result = ResVmsSignboardPowerControl(ABuffer, ARcvLen);
  115. break;
  116. default:
  117. LERROR("<<< Unknown ProcessRecvPacket: 0x%02X", Pkt->hd.OpCode);
  118. return SYS_ERR_UNKNOWN_COMMAND;
  119. }
  120. if (result == SYS_ERR_NONE)
  121. {
  122. FClient.sendRetry = VMS_MAX_RETRY_COUNT;
  123. }
  124. return result;
  125. }
  126. //---------------------------------------------------------------------------
  127. // Initialize 요청 응답
  128. int __fastcall TVMSCThread::ResVmsInitialize(BYTE *ABuffer, int ARcvLen)
  129. {
  130. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  131. int result = SYS_ERR_NONE;
  132. return result;
  133. }
  134. //---------------------------------------------------------------------------
  135. // Signboard powwer control 요청 응답
  136. int __fastcall TVMSCThread::ResVmsSignboardPowerControl(BYTE *ABuffer, int ARcvLen)
  137. {
  138. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  139. int result = SYS_ERR_NONE;
  140. return result;
  141. }
  142. //---------------------------------------------------------------------------
  143. // VMS 상태정보 요청 응답
  144. int __fastcall TVMSCThread::ResVmsStatus(BYTE *ABuffer, int ARcvLen)
  145. {
  146. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  147. int result = SYS_ERR_NONE;
  148. ACK_NAK res;
  149. if (FClient.State == CT_CLIENT_WAIT)
  150. {
  151. //TODO: 클라이언트로 결과전송
  152. }
  153. if (FLocalCmd.obj == OBJ_GeneralStatus)
  154. {
  155. FLocalCmd.GeneralStatus = true;
  156. }
  157. if (FClient.State == CT_CLIENT_WAIT)
  158. FClient.State = CT_CLIENT_COMMAND;
  159. else
  160. if (FClient.State == CT_LOCAL_WAIT)
  161. FClient.State = CT_LOCAL_COMMAND;
  162. SaveStatusData(ABuffer);
  163. return result;
  164. }
  165. //---------------------------------------------------------------------------
  166. /*
  167. *
  168. * arguments
  169. * void
  170. * return
  171. * void
  172. */
  173. int __fastcall TVMSCThread::SaveStatusData(BYTE *pGS)
  174. {
  175. int result = SYS_ERR_OTHER;
  176. VMS_STATUS1 status1;
  177. VMS_STATUS2 status2;
  178. short temp;
  179. if (m_pState == NULL) return result;
  180. #if 0
  181. typedef struct tagVmsStatus1
  182. {
  183. BYTE localModeActive : 1;
  184. BYTE defaultParameter : 1;
  185. BYTE resetOccured : 1;
  186. BYTE longPowerFail : 1;
  187. BYTE shortPowerFail : 1;
  188. BYTE doorOpen : 1;
  189. BYTE moduleErrRateOver: 1;
  190. BYTE ramRomError : 1;
  191. } VMS_STATUS1;
  192. typedef struct tagVmsStatus2
  193. {
  194. BYTE libChecksumErr: 1;
  195. BYTE fan : 1;
  196. BYTE heater : 1;
  197. BYTE boardPower : 1;
  198. BYTE boardPowerFail: 1;
  199. BYTE blankDisp : 1;
  200. BYTE sirenFail : 1;
  201. BYTE reserved : 1;
  202. } VMS_STATUS2;
  203. #endif
  204. memcpy((char*)&status2, (char*)&pGS[11], sizeof(BYTE));
  205. memcpy((char*)&status1, (char*)&pGS[12], sizeof(BYTE));
  206. temp = (short)pGS[13];
  207. m_pState->DoorStatus = status1.doorOpen == 0x01 ? vms_open : vms_close;
  208. m_pState->ModulePowerStatus = status2.boardPower == 0x01 ? vms_on : vms_off;
  209. m_pState->FanStatus = status2.fan == 0x01 ? vms_on : vms_off;
  210. m_pState->HeaterStatus = status2.heater == 0x01 ? vms_on : vms_off;
  211. m_pState->BodyTemp = temp;
  212. m_pState->LuminanceStatus = 0; //PASIG 없음
  213. m_pState->ModuleState = status1.moduleErrRateOver == 0x01 ? vms_error : vms_normal;
  214. LDEBUG(" module_StatusCode: %2d, %s", status1.moduleErrRateOver, m_pState->ModuleState == vms_normal ? "Normal" : "Error");
  215. LDEBUG("modulePower_StatusCode: %2d, %s", status2.boardPower, m_pState->ModulePowerStatus == vms_on ? "On" : "Off");
  216. LDEBUG(" door_StatusCode: %2d, %s", status1.doorOpen, m_pState->DoorStatus == vms_open ? "Open" : "Close");
  217. LDEBUG(" fan_StatusCode: %2d, %s", status2.fan, m_pState->FanStatus == vms_on ? "On" : "Off");
  218. LDEBUG(" heater_StatusCode: %2d, %s", status2.heater, m_pState->HeaterStatus == vms_on ? "On" : "Off");
  219. LDEBUG(" body_TemperatureQty: %2d, %d", temp, m_pState->BodyTemp );
  220. //LDEBUG(" luminance_StatusQty: %2d, %d", m_pState->LuminanceStatus, m_pState->LuminanceStatus );
  221. result = SYS_ERR_NONE;
  222. //if (FGenState && FModState && FPowState)
  223. {
  224. //FGenState = FModState = FPowState = false;
  225. POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_STATE_STTS, (int)FVmsObj);
  226. }
  227. return result;
  228. }
  229. //---------------------------------------------------------------------------
  230. // VMS 상태정보 요청 응답
  231. int __fastcall TVMSCThread::ResVmsModuleStatus(BYTE *ABuffer, int ARcvLen)
  232. {
  233. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  234. int result = SYS_ERR_NONE;
  235. if (FClient.State == CT_CLIENT_WAIT)
  236. {
  237. //TODO: 클라이언트로 결과전송
  238. }
  239. if (FLocalCmd.obj == OBJ_ModuleStatus)
  240. {
  241. FLocalCmd.ModuleStatus = true;
  242. }
  243. if (FClient.State == CT_CLIENT_WAIT)
  244. FClient.State = CT_CLIENT_COMMAND;
  245. else
  246. if (FClient.State == CT_LOCAL_WAIT)
  247. FClient.State = CT_LOCAL_COMMAND;
  248. BYTE ModuleState;
  249. memset((char*)m_pState->ModuleStatus, 0x00, sizeof(m_pState->ModuleStatus));
  250. //INT_VMS_MAX_MODULE_BIT
  251. int nBytes = (int)CommUtil_swapl(Pkt->hd.Size);
  252. nBytes -= 12;
  253. if (FLogFile->FLogCfg.Debug)
  254. {
  255. LDEBUG("ModuleInfo Bytes: %d, %d", nBytes, nBytes*8);
  256. for (int ii = 0; ii < nBytes; ii++)
  257. {
  258. LDEBUG(" : %d, %02X", ii, Pkt->data[ii]);
  259. }
  260. }
  261. memcpy((char*)m_pState->ModuleStatus, (char*)Pkt->data, nBytes);
  262. return result;
  263. }
  264. //---------------------------------------------------------------------------
  265. // VMS MSG Download 요청 응답
  266. int __fastcall TVMSCThread::ResVmsMsgDownload(BYTE *ABuffer, int ARcvLen)
  267. {
  268. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  269. int result = SYS_ERR_NONE;
  270. try
  271. {
  272. m_pVmsFormList->Lock();
  273. int nFormCnt = m_pVmsFormList->Count();
  274. for (int ii = 0; ii < nFormCnt; ii++)
  275. {
  276. TVmsForm *pVmsForm = m_pVmsFormList->GetItem(ii);
  277. if (!pVmsForm) continue;
  278. pVmsForm->SvcRes = true;
  279. }
  280. }
  281. __finally
  282. {
  283. m_pVmsFormList->UnLock();
  284. }
  285. if (FLocalCmd.obj == OBJ_RealTimeDisplay)
  286. {
  287. SAVE_STRUCT ProvideSave;
  288. memset(&ProvideSave, 0x00, sizeof(ProvideSave));
  289. ProvideSave.Type = save_provide;
  290. ProvideSave.Count = 1;
  291. ProvideSave.pObj[0] = (void*)FVmsObj;
  292. FVmsObj->CTLMODE->Enable = object_disable;
  293. FVmsObj->CTLMODE->SvcDate = Now().FormatString("yyyymmddhhnnss");
  294. FVmsObj->CTLMODE->SaveFlag = true;
  295. FVmsObj->CTLMODE->Result = true;
  296. if ((result = SendDbThreadDataMessage(WM_PROVIDE_SAVE, &ProvideSave, sizeof(ProvideSave))) != VERR_NONE)
  297. {
  298. SERROR("SendDbThreadDataMessage Error %d", result);
  299. }
  300. }
  301. return result;
  302. }
  303. //---------------------------------------------------------------------------
  304. // VMS LIB Download 요청 응답
  305. int __fastcall TVMSCThread::ResVmsLibDownload(BYTE *ABuffer, int ARcvLen)
  306. {
  307. VMS_PACKET *Pkt = (VMS_PACKET*)ABuffer;
  308. int result = SYS_ERR_NONE;
  309. try
  310. {
  311. m_pVmsFormList->Lock();
  312. int nFormCnt = m_pVmsFormList->Count();
  313. for (int ii = 0; ii < nFormCnt; ii++)
  314. {
  315. TVmsForm *pVmsForm = m_pVmsFormList->GetItem(ii);
  316. if (!pVmsForm) continue;
  317. pVmsForm->SvcRes = true;
  318. }
  319. }
  320. __finally
  321. {
  322. m_pVmsFormList->UnLock();
  323. }
  324. if (FLocalCmd.obj == OBJ_RealTimeDisplay)
  325. {
  326. SAVE_STRUCT ProvideSave;
  327. memset(&ProvideSave, 0x00, sizeof(ProvideSave));
  328. ProvideSave.Type = save_provide;
  329. ProvideSave.Count = 1;
  330. ProvideSave.pObj[0] = (void*)FVmsObj;
  331. FVmsObj->CTLMODE->Enable = object_disable;
  332. FVmsObj->CTLMODE->SvcDate = Now().FormatString("yyyymmddhhnnss");
  333. FVmsObj->CTLMODE->SaveFlag = true;
  334. FVmsObj->CTLMODE->Result = true;
  335. if ((result = SendDbThreadDataMessage(WM_PROVIDE_SAVE, &ProvideSave, sizeof(ProvideSave))) != VERR_NONE)
  336. {
  337. SERROR("SendDbThreadDataMessage Error %d", result);
  338. }
  339. }
  340. return result;
  341. }
  342. //---------------------------------------------------------------------------