VMSCThread.cpp 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812
  1. /****************************************************************************
  2. * @source : VMSCThread.cpp
  3. * @description : VMS Client Socket Thread
  4. ****************************************************************************
  5. * DATE AUTHOR DESCRIPTION
  6. * --------------------------------------------------------------------------
  7. * 2012/03/09 CYM [100] First Cut
  8. *
  9. ****************************************************************************/
  10. //---------------------------------------------------------------------------
  11. #include "AppGlobalF.h"
  12. #include "VMSCThread.h"
  13. #include "FrmSysLogF.h"
  14. #include "FrmVmsInfoF.h"
  15. #include "FrmVmsLogF.h"
  16. #include <winsock.h>
  17. //---------------------------------------------------------------------------
  18. #pragma package(smart_init)
  19. //---------------------------------------------------------------------------
  20. // Important: Methods and properties of objects in VCL can only be
  21. // used in a method called using Synchronize, for example:
  22. //
  23. // Synchronize(&UpdateCaption);
  24. //
  25. // where UpdateCaption could look like:
  26. //
  27. // void __fastcall TVMSCThread::UpdateCaption()
  28. // {
  29. // Form1->Caption = "Updated in a thread";
  30. // }
  31. //---------------------------------------------------------------------------
  32. #define VMSID FVmsObj->VmsId
  33. /*
  34. * 스레드 생성자
  35. * arguments
  36. * void
  37. * return
  38. * void
  39. */
  40. __fastcall TVMSCThread::TVMSCThread(bool CreateSuspended, int Tag, bool *ActiveIndicator)
  41. : TThread(CreateSuspended)
  42. {
  43. try
  44. {
  45. FVmsObj = (TVmsCtlr*)Tag;
  46. FLogFile = FVmsObj->FCLog;
  47. ThreadActiveIndicator = ActiveIndicator;
  48. *ThreadActiveIndicator = true;
  49. // 정보 초기화
  50. ClientInitInfo();
  51. TcpClient = new TTcpClient(NULL);
  52. TcpClient->RemoteHost = FVmsObj->IpAddress;
  53. TcpClient->RemotePort = FVmsObj->LocalNo;//g_AppCfg.comm.nClientPort;
  54. TcpClient->BlockMode = bmBlocking;
  55. TcpClient->OnConnect = TcpClientConnect;
  56. TcpClient->OnCreateHandle = TcpClientCreateHandle;
  57. TcpClient->OnDestroyHandle = TcpClientDestroyHandle;
  58. TcpClient->OnDisconnect = TcpClientDisconnect;
  59. TcpClient->OnError = TcpClientError;
  60. TcpClient->OnReceive = TcpClientReceive;
  61. TcpClient->OnSend = TcpClientSend;
  62. Priority = tpNormal;
  63. FreeOnTerminate = true;
  64. Resume();
  65. }
  66. catch(Exception &e)
  67. {
  68. SERROR("Thread Init Error");
  69. Terminate();
  70. }
  71. }
  72. //---------------------------------------------------------------------------
  73. /*
  74. * 스레드 소멸자
  75. * arguments
  76. * void
  77. * return
  78. * void
  79. */
  80. __fastcall TVMSCThread::~TVMSCThread()
  81. {
  82. try
  83. {
  84. FVmsObj->Client.Status = process_fail;
  85. SAFE_DELETE(TcpClient);
  86. }
  87. catch(Exception &e)
  88. {
  89. }
  90. *ThreadActiveIndicator = false;
  91. }
  92. //---------------------------------------------------------------------------
  93. /*
  94. * 연결 이벤트 핸들러
  95. * arguments
  96. * void
  97. * return
  98. * void
  99. */
  100. void __fastcall TVMSCThread::TcpClientConnect(TObject *Sender)
  101. {
  102. ClientInitInfo();
  103. FVmsObj->Client.Comm = state_open;
  104. FVmsObj->Client.Status = process_run;
  105. if (!APP_EXIT) {
  106. POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_CLIENT_STTS, (int)FVmsObj);
  107. SINFO("Server Connect ok: %s %s", TcpClient->RemoteHost.c_str(), TcpClient->RemotePort.c_str());
  108. }
  109. }
  110. //---------------------------------------------------------------------------
  111. /*
  112. * 연결 해제 이벤트 핸들러
  113. * arguments
  114. * void
  115. * return
  116. * void
  117. */
  118. void __fastcall TVMSCThread::TcpClientDisconnect(TObject *Sender)
  119. {
  120. FVmsObj->Client.Comm = state_close;
  121. FVmsObj->Client.Status = process_run;
  122. m_pState->Comm = vms_error;
  123. if (!APP_EXIT) {
  124. //POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_CLIENT_STTS, (int)FVmsObj);
  125. SINFO("Server Disconnect: %s %s", TcpClient->RemoteHost.c_str(), TcpClient->RemotePort.c_str());
  126. }
  127. }
  128. //---------------------------------------------------------------------------
  129. /*
  130. * 핸들 생성 이벤트 핸들러
  131. * arguments
  132. * void
  133. * return
  134. * void
  135. */
  136. void __fastcall TVMSCThread::TcpClientCreateHandle(TObject *Sender)
  137. {
  138. //LDEBUG("Thread Create Handle");
  139. }
  140. //---------------------------------------------------------------------------
  141. /*
  142. * 핸들 소멸 이벤트 핸들러
  143. * arguments
  144. * void
  145. * return
  146. * void
  147. */
  148. void __fastcall TVMSCThread::TcpClientDestroyHandle(TObject *Sender)
  149. {
  150. //LDEBUG("Thread Destroy Handle");
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TVMSCThread::Close(void)
  154. {
  155. try
  156. {
  157. FVmsObj->Client.Status == process_fail;
  158. if (TcpClient)
  159. {
  160. //if (TcpClient->Connected)
  161. {
  162. TcpClient->Close();
  163. //Sleep(3000);
  164. }
  165. }
  166. }
  167. catch(Exception &e)
  168. {
  169. }
  170. }
  171. //---------------------------------------------------------------------------
  172. /*
  173. * 에러 처리 이벤트 핸들러
  174. * arguments
  175. * void
  176. * return
  177. * void
  178. */
  179. void __fastcall TVMSCThread::TcpClientError(TObject *Sender, int SocketError)
  180. {
  181. LPVOID lpMsgBuf = NULL;
  182. try
  183. {
  184. try
  185. {
  186. FormatMessage(
  187. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  188. NULL,
  189. //GetLastError(),
  190. //MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  191. SocketError,
  192. MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
  193. (LPTSTR) &lpMsgBuf,
  194. 0,
  195. NULL
  196. );
  197. char *pData = (char*)lpMsgBuf;
  198. if (strlen(pData) > 2)
  199. {
  200. pData[strlen(pData)-2] = 0x00;
  201. pData[strlen(pData)-1] = 0x00;
  202. }
  203. SERROR("SocketError: (%d: %s)", SocketError, pData);
  204. }
  205. __finally
  206. {
  207. if (lpMsgBuf) LocalFree(lpMsgBuf);
  208. lpMsgBuf = NULL;
  209. }
  210. }
  211. catch(Exception &e)
  212. {
  213. }
  214. Close();
  215. }
  216. //---------------------------------------------------------------------------
  217. /*
  218. * 데이터 수신 이벤트 핸들러
  219. * arguments
  220. * void
  221. * return
  222. * void
  223. */
  224. void __fastcall TVMSCThread::TcpClientReceive(TObject *Sender, char *Buf, int &DataLen)
  225. {
  226. //LDEBUG("VMS Client Receive: %s, %d", VMSID.c_str(), DataLen);
  227. }
  228. //---------------------------------------------------------------------------
  229. /*
  230. * 데이터 송신 이벤트 핸들러
  231. * arguments
  232. * void
  233. * return
  234. * void
  235. */
  236. void __fastcall TVMSCThread::TcpClientSend(TObject *Sender, char *Buf, int &DataLen)
  237. {
  238. //LDEBUG("VMS Client Send: %s, %d", VMSID.c_str(), DataLen);
  239. }
  240. //---------------------------------------------------------------------------
  241. bool __fastcall TVMSCThread::SocketActivity(bool &AError)
  242. {
  243. bool Error, ReadReady, WriteReady, ExcepFlag;
  244. ReadReady = false;
  245. AError = false;
  246. if (!TcpClient) return ReadReady;
  247. try
  248. {
  249. Error = TcpClient->Select(&ReadReady, &WriteReady, &ExcepFlag, 0);
  250. if ((Error == false))// || (ExcepFlag == true)) //exception flag == o.o.b(out of band data receive)
  251. {
  252. AError = true;
  253. ReadReady = false;
  254. }
  255. }
  256. catch(Exception &e)
  257. {
  258. AError = true;
  259. ReadReady = false;
  260. }
  261. return ReadReady;
  262. }
  263. //---------------------------------------------------------------------------
  264. bool __fastcall TVMSCThread::SocketActivity()
  265. {
  266. bool bResult = false;
  267. bool Error, ReadReady, WriteReady, ExcepFlag;
  268. if (!TcpClient) return bResult;
  269. try
  270. {
  271. Error = TcpClient->Select(&ReadReady, &WriteReady, &ExcepFlag, 0);
  272. if ((Error == false) || (ReadReady == true) || (ExcepFlag == true))
  273. {
  274. bResult = true;
  275. }
  276. }
  277. catch(Exception &e)
  278. {
  279. }
  280. return bResult;
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall TVMSCThread::Execute()
  284. {
  285. BYTE buffer[DEFAULT_TCP_PACKET_MAX_SIZE];
  286. int nError;
  287. int nResult;
  288. bool bError;
  289. int nReadLen;
  290. unsigned long nRecvLen;
  291. try
  292. {
  293. NameThreadForDebugging(m_strName);
  294. FErrLine = 0;
  295. SINFO("Thread start");
  296. try
  297. {
  298. while (!Terminated)
  299. {
  300. FVmsObj->Client.Status = process_run;
  301. m_pState->Comm = vms_error;
  302. if (g_AppCfg.bThrExit)
  303. {
  304. nError = 0;
  305. SINFO("Thread exit catched....1");
  306. Terminate();
  307. }
  308. if (FVmsObj->Server.Status != process_run)
  309. {
  310. //VMS서버프로세스가 연결이 안된상태임
  311. //LDEBUG("Server not connected...");
  312. Sleep(1000);
  313. continue;
  314. }
  315. // VMS가 서버로 접속을 하였으니까 VMS로 연결을 시도하자
  316. SINFO("Server connect try: %s.%s", TcpClient->RemoteHost.c_str(), TcpClient->RemotePort.c_str());
  317. TcpClient->Open();
  318. bError = false;
  319. while (!Terminated && TcpClient->Connected)
  320. {
  321. m_pState->Comm = vms_normal;
  322. try
  323. {
  324. ClientStateMachine();
  325. bError = false;
  326. if (TcpClient->WaitForData(100) || SocketActivity())
  327. {
  328. bError = false;
  329. nRecvLen = 0;
  330. #if 0
  331. nResult = ioctlsocket(TcpClient->Handle, FIONREAD, &nRecvLen);
  332. if (nResult != 0)
  333. {
  334. Close();
  335. }
  336. if (nRecvLen > 0)
  337. #endif
  338. {
  339. memset(buffer, 0, sizeof(buffer));
  340. nReadLen = TcpClient->ReceiveBuf(buffer, sizeof(buffer));
  341. if (nReadLen > 0)
  342. {
  343. if ((nResult = RecvPacket(buffer, nReadLen)) != SYS_ERR_NONE)
  344. {
  345. ProcErrorState(TRUE, nResult);
  346. }
  347. }
  348. else
  349. {
  350. bError = true;
  351. }
  352. #if 0
  353. if ((int)nRecvLen != nReadLen)
  354. {
  355. SERROR("Receive data size false: %d, %d", nRecvLen, nReadLen);
  356. }
  357. #endif
  358. }
  359. }
  360. if (bError)
  361. {
  362. SERROR("Socket Error[Closed]....");
  363. nError = -3;
  364. Close();
  365. }
  366. else
  367. {
  368. //ClientStateMachine();
  369. }
  370. if (FVmsObj->Server.Status != process_run)
  371. {
  372. nError = -4;
  373. Close();
  374. }
  375. if (g_AppCfg.bThrExit)
  376. {
  377. nError = 0;
  378. SINFO("Thread exit catched....2");
  379. Close();
  380. }
  381. }
  382. catch(Exception &e)
  383. {
  384. SERROR("Thread execute exception: %s, Line: %d", AnsiString(e.Message).c_str(), FErrLine);
  385. nError = -2;
  386. Close();
  387. }
  388. }
  389. if (FVmsObj->Server.Status == process_run)
  390. {
  391. Sleep(3000);
  392. }
  393. }
  394. SERROR("Term: %d, Conn: %d, Err: %d, Line: %d", Terminated, TcpClient->Connected, nError, FErrLine);
  395. }
  396. __finally
  397. {
  398. TcpClient->Close();
  399. Terminate();
  400. }
  401. }
  402. catch(Exception &e)
  403. {
  404. SERROR("Thread execute error: %s", AnsiString(e.Message).c_str());
  405. Terminate();
  406. }
  407. m_pState->Comm = vms_error;
  408. ClientTerminate();
  409. }
  410. //---------------------------------------------------------------------------
  411. /*
  412. * 클라이언트 정보 초기화 루틴
  413. * arguments
  414. * void
  415. * return
  416. * void
  417. */
  418. void __fastcall TVMSCThread::ClientInitInfo(void)
  419. {
  420. m_RxLen = 0;
  421. m_rTimer = Now();
  422. m_WatchDogTimer = Now();
  423. FWatchDogTime = g_AppCfg.comm.nWatchDogTime;
  424. m_Client.IpAddress = FVmsObj->IpAddress;
  425. m_Client.Addr = FVmsObj->LocalNo;
  426. m_Client.VmsId = FVmsObj->VmsId;
  427. m_pState = &FVmsObj->RSTATE;
  428. m_Client.Port = g_AppCfg.comm.nClientPort;
  429. m_Client.TimeOut = g_AppCfg.comm.nCmdTimeOut;
  430. m_Client.CycleTime = g_AppCfg.comm.nPollingTime;
  431. m_Client.State = CT_LOGIN;//CT_IDLE;
  432. m_Client.sTimer = Now() - ITSCOMM_SECTIME(CLIENT_CONNECT_RETRY_TIME - CLIENT_CHANGE_TIME);
  433. m_Client.iRetry = VMS_MAX_RETRY_COUNT;
  434. m_Client.cTimer = Now();
  435. //m_strName.sprintf("VMSC%03d", m_Client.Addr);
  436. m_strName = "VMSC" + FVmsObj->VmsId;
  437. m_Client.LocalCenterId = "";
  438. m_Client.CenterId = "";
  439. m_Client.LocalDomainNm = g_AppCfg.comm.sLocalDomain;
  440. m_Client.DomainNm = m_Client.VmsId;
  441. m_Client.UserName = g_AppCfg.comm.sLoginUser;
  442. m_Client.UserPasswd = g_AppCfg.comm.sLoginPswd;
  443. m_Client.MaxHeartbeatTime = VMS_MAX_HEARTBEAT_TIME;
  444. m_Client.DatagramSize = VMS_MAX_DATAGRAM_SIZE;
  445. m_DataPacketNumber = 0;
  446. m_SubscribeSerialNbr = 0;
  447. m_SubRegistered.GeneralStatus = false;
  448. m_SubRegistered.PowerStatus = false;
  449. m_SubRegistered.ModuleStatus = false;
  450. m_SubRegistered.DotStatus = false;
  451. m_SubRegistered.ScreenStatus = false;
  452. m_SubRegistered.DisplayingData = false;
  453. m_Registered.GeneralStatus = false;
  454. m_Registered.ModuleStatus = false;
  455. m_Registered.PowerStatus = false;
  456. m_Registered.ScreenDataStatusMessage = false;
  457. m_Registered.RealTimeDisplayMessage = false;
  458. FGenState = FModState = FPowState = false;
  459. m_pState->Comm = vms_error;
  460. #if 0
  461. if (m_pState != NULL)
  462. {
  463. m_pState->Cpsw.Enable = bit_enable;
  464. m_pState->CollectDate = Now().FormatString("yyyymmddhhnnss");
  465. m_pState->Door = STATE_UNKNOWN;
  466. m_pState->Heater = STATE_UNKNOWN;
  467. m_pState->Fan = STATE_UNKNOWN;
  468. m_pState->ModuleState = STATE_UNKNOWN;
  469. m_pState->Power = STATE_UNKNOWN;
  470. m_pState->Temp = 0;
  471. m_pState->Bright = 0;
  472. m_pState->ModuleHorizontal = 0;
  473. m_pState->ModuleVertical = 0;
  474. m_pState->PowerCount = 0;
  475. memset(m_pState->ModuleStatus, STATE_UNKNOWN, sizeof(m_pState->ModuleStatus));
  476. memset(m_pState->PowerStatus, STATE_UNKNOWN, sizeof(m_pState->PowerStatus));
  477. }
  478. #endif
  479. LINFO("ClientInitInfo ok");
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TVMSCThread::ClientTerminate(void)
  483. {
  484. try
  485. {
  486. FVmsObj->Client.Comm = state_close;
  487. FVmsObj->Client.Status = process_fail;
  488. FVmsObj->Client.pThread = NULL;
  489. FVmsObj->Client.ActiveIndicator = false;
  490. if (!APP_EXIT) {
  491. //POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_CLIENT_STTS, (int)FVmsObj);
  492. SINFO("Thread stop");
  493. }
  494. }
  495. catch(Exception &e)
  496. {
  497. }
  498. }
  499. //---------------------------------------------------------------------------
  500. /*
  501. *
  502. * arguments
  503. * void
  504. * return
  505. * void
  506. */
  507. void __fastcall TVMSCThread::LOG_WriteTime(Time_t *pTime)
  508. {
  509. FErrLine = 1;
  510. if (!pTime) return;
  511. if (pTime->time_Year_qty)
  512. {
  513. LDEBUG("time_Year_qty: %d", *pTime->time_Year_qty);
  514. }
  515. if (pTime->time_Month_qty)
  516. {
  517. LDEBUG("time_Month_qty: %d", *pTime->time_Month_qty);
  518. }
  519. if (pTime->time_Day_qty)
  520. {
  521. LDEBUG("time_Day_qty: %d", *pTime->time_Day_qty);
  522. }
  523. LDEBUG("time_Hour_qty : %d", pTime->time_Hour_qty);
  524. LDEBUG("time_Minute_qty: %d", pTime->time_Minute_qty);
  525. LDEBUG("time_Second_qty: %d", pTime->time_Second_qty);
  526. switch(pTime->time_SecondFractions_zz.present)
  527. {
  528. case time_SecondFractions_PR_deci_seconds:
  529. LDEBUG("deci_seconds: %d", pTime->time_SecondFractions_zz.choice.deci_seconds);
  530. break;
  531. case time_SecondFractions_PR_centi_seconds:
  532. LDEBUG("centi_seconds: %d", pTime->time_SecondFractions_zz.choice.centi_seconds);
  533. break;
  534. case time_SecondFractions_PR_milliseconds:
  535. LDEBUG("milliseconds: %d", pTime->time_SecondFractions_zz.choice.milliseconds);
  536. break;
  537. default:
  538. LDEBUG("Unknown time_SecondFractions %d", pTime->time_SecondFractions_zz.present);
  539. break;
  540. }
  541. if (pTime->time_Timezone)
  542. {
  543. LDEBUG("timezone_Hour_qty: %d", pTime->time_Timezone->timezone_Hour_qty);
  544. LDEBUG(" time_Minute_qty: %d", pTime->time_Timezone->time_Minute_qty);
  545. }
  546. }
  547. /*--------------------------------------------------------------------------*/
  548. /*
  549. *
  550. * arguments
  551. * void
  552. * return
  553. * void
  554. */
  555. int __fastcall TVMSCThread::SaveStateData(GeneralStatusMessage_t *pGS)
  556. {
  557. FErrLine = 2;
  558. int result;
  559. long lv;
  560. result = SYS_ERR_OTHER;
  561. if (m_pState == NULL) return result;
  562. // m_pState->Cpsw.Enable = bit_enable;
  563. // m_pState->CollectDate = Now().FormatString("yyyymmddhhnnss");
  564. asn_INTEGER2long(&pGS->door_StatusCode, &lv);
  565. switch(lv)
  566. {
  567. case door_StatusCode_closed: m_pState->DoorStatus = vms_close; break;
  568. case door_StatusCode_open : m_pState->DoorStatus = vms_open; break;
  569. default : m_pState->DoorStatus = vms_unknown; break;
  570. }
  571. LDEBUG(" door_StatusCode: %u, %d", lv, m_pState->DoorStatus);
  572. asn_INTEGER2long(&pGS->modulePower_StatusCode, &lv);
  573. m_pState->ModulePowerStatus = lv;
  574. switch(lv)
  575. {
  576. case modulePower_StatusCode_off: m_pState->ModulePowerStatus = vms_off; break;
  577. case modulePower_StatusCode_on : m_pState->ModulePowerStatus = vms_on; break;
  578. default : m_pState->ModulePowerStatus = vms_unknown; break;
  579. }
  580. LDEBUG("modulePower_StatusCode: %u, %d", lv, m_pState->ModulePowerStatus);
  581. asn_INTEGER2long(&pGS->body_TemperatureQty, &lv);
  582. m_pState->BodyTemp = lv;
  583. LDEBUG(" body_TemperatureQty: %u, %d", lv, m_pState->BodyTemp);
  584. asn_INTEGER2long(&pGS->luminance_StatusQty, &lv);
  585. m_pState->LuminanceStatus = lv;
  586. LDEBUG(" luminance_StatusQty: %u, %d", lv, m_pState->LuminanceStatus);
  587. asn_INTEGER2long(&pGS->fan_StatusCode, &lv);
  588. m_pState->FanStatus = lv;
  589. switch(lv)
  590. {
  591. case fan_StatusCode_off: m_pState->FanStatus = vms_off; break;
  592. case fan_StatusCode_on : m_pState->FanStatus = vms_on; break;
  593. default : m_pState->FanStatus = vms_unknown; break;
  594. }
  595. LDEBUG(" fan_StatusCode: %u, %d", lv, m_pState->FanStatus);
  596. asn_INTEGER2long(&pGS->heater_StatusCode, &lv);
  597. m_pState->HeaterStatus = lv;
  598. switch(lv)
  599. {
  600. case heater_StatusCode_off: m_pState->HeaterStatus = vms_off; break;
  601. case heater_StatusCode_on : m_pState->HeaterStatus = vms_on; break;
  602. default : m_pState->HeaterStatus = vms_unknown; break;
  603. }
  604. LDEBUG(" heater_StatusCode: %u, %d", lv, m_pState->HeaterStatus);
  605. result = SYS_ERR_NONE;
  606. {
  607. if (pGS->externalLight_StatusCode)
  608. {
  609. asn_INTEGER2long(pGS->externalLight_StatusCode, &lv);
  610. m_pState->ExternalLightStatus = lv;
  611. //LDEBUG("externalLight_StatusCode: %u", lv);
  612. }
  613. if (pGS->alarmLight_StatusCode)
  614. {
  615. asn_INTEGER2long(pGS->alarmLight_StatusCode, &lv);
  616. m_pState->AlarmLightStatus = lv;
  617. //LDEBUG("alarmLight_StatusCode: %u", lv);
  618. }
  619. if (pGS->speaker_StatusCode)
  620. {
  621. asn_INTEGER2long(pGS->speaker_StatusCode, &lv);
  622. m_pState->SpeakerStatus = lv;
  623. //LDEBUG("speaker_StatusCode: %u", lv);
  624. }
  625. if (pGS->controller_CurrentTime)
  626. {
  627. sprintf((char*)m_pState->ControllerCurrentTime, ".*s", pGS->controller_CurrentTime->size, pGS->controller_CurrentTime->buf);
  628. //LDEBUG("controller_CurrentTime: %.*s", pGS->controller_CurrentTime->size, pGS->controller_CurrentTime->buf);
  629. }
  630. asn_INTEGER2long(&pGS->scheduledmessage_OperatingTime, &lv);
  631. m_pState->ScheduledMessageOperatingTime = lv;
  632. LDEBUG("scheduledmessage_OperatingTime: %u", lv);
  633. asn_INTEGER2long(&pGS->module_OperatingTemperatureQty, &lv);
  634. m_pState->ModuleOperatingTemperature = lv;
  635. LDEBUG("module_OperatingTemperatureQty: %u", lv);
  636. asn_INTEGER2long(&pGS->fan_OperatingTemperatureQty, &lv);
  637. m_pState->FanOperatingTemperature = lv;
  638. LDEBUG(" fan_OperatingTemperatureQty: %u", lv);
  639. asn_INTEGER2long(&pGS->heater_OperatingTemperatureQty, &lv);
  640. m_pState->HeaterOperatingTemperature = lv;
  641. LDEBUG("heater_OperatingTemperatureQty: %u", lv);
  642. if (pGS->externalLight_OperatingLuminanceQty)
  643. {
  644. asn_INTEGER2long(pGS->externalLight_OperatingLuminanceQty, &lv);
  645. m_pState->ExternalLightOperatingLuminance = lv;
  646. //LDEBUG("externalLight_OperatingLuminanceQty: %u", lv);
  647. }
  648. //아래존재함
  649. if (pGS->module_BasicFailureRate)
  650. {
  651. asn_INTEGER2long(pGS->module_BasicFailureRate, &lv);
  652. m_pState->ModuleBasicFailureRate = lv;
  653. //LDEBUG(" module_BasicFailureRate: %u", lv);
  654. }
  655. if (pGS->maximum_RetryQty)
  656. {
  657. asn_INTEGER2long(pGS->maximum_RetryQty, &lv);
  658. m_pState->MaximumRetry = lv;
  659. //LDEBUG("maximum_RetryQty: %u", lv);
  660. }
  661. if (pGS->response_TimeOutQty)
  662. {
  663. asn_INTEGER2long(pGS->response_TimeOutQty, &lv);
  664. m_pState->ResponseTimeOut = lv;
  665. //LDEBUG("response_TimeOutQty: %u", lv);
  666. }
  667. if (pGS->blinking_CycleTime)
  668. {
  669. asn_INTEGER2long(pGS->blinking_CycleTime, &lv);
  670. m_pState->BlinkingCycleTime = lv;
  671. //LDEBUG("blinking_CycleTime: %u", lv);
  672. }
  673. }
  674. return result;
  675. }
  676. //---------------------------------------------------------------------------
  677. /*
  678. * 패킷 수신
  679. * arguments
  680. * void
  681. * return
  682. * void
  683. */
  684. int __fastcall TVMSCThread::RecvPacket(void *Data, int Len)
  685. {
  686. FErrLine = 3;
  687. int result;
  688. ssize_t stat;
  689. ssize_t length;
  690. DatexDataPacket_t *pMsg = 0; /* Type to decode */
  691. asn_dec_rval_t rval; /* Decoder return value */
  692. long version;
  693. WORD crc;
  694. result = SYS_ERR_NONE;
  695. if (Len > 0)
  696. {
  697. LogData("RECV", (char*)Data, Len);
  698. }
  699. if (m_RxLen)
  700. {
  701. if ((m_RxLen + Len) > CLIENT_MAX_PACKET_SIZE) m_RxLen = 0;
  702. memcpy(m_RxBuff + m_RxLen, Data, Len);
  703. m_RxLen += Len;
  704. }
  705. else
  706. {
  707. memcpy(m_RxBuff, Data, Len);
  708. m_RxLen = Len;
  709. m_rTimer = Now();
  710. }
  711. while (m_RxLen >= VMS_MIN_PACKET_SIZE)
  712. {
  713. stat = ASN1C_BerReadLength(m_RxBuff, m_RxLen, &length);
  714. if ((stat != RC_OK) || (length > VMS_MAX_PACKET_SIZE))
  715. {
  716. result = SYS_ERR_INVALID_TAGANDLEN;
  717. break;
  718. }
  719. LINFO("Recv: state[%d], length[%d/%d]", stat, length, m_RxLen);
  720. if (length > m_RxLen)
  721. {
  722. LINFO("Recv packet remain: ReqLen(%d) recv(%d)", length, m_RxLen);
  723. break;
  724. }
  725. /* Decode */
  726. #if 0
  727. rval = ber_decode(0, &asn_DEF_DatexDataPacket, (void **)&pMsg, m_RxBuff, m_RxLen);
  728. #else
  729. rval = ber_decode(0, &asn_DEF_DatexDataPacket, (void **)&pMsg, m_RxBuff, length);
  730. #endif
  731. switch(rval.code)
  732. {
  733. case RC_OK:
  734. if ((rval.consumed == 0) || (pMsg == 0))
  735. {
  736. result = SYS_ERR_DATEXDATAPACKET_DECODE;
  737. break;
  738. }
  739. asn_INTEGER2long(&pMsg->datex_Version_number, &version);
  740. if (version != datex_Version_number_version1)
  741. {
  742. LERROR("SYS_ERR_INVALID_VERSION_NUMBER: %d/%d", version, datex_Version_number_version1);
  743. result = SYS_ERR_INVALID_VERSION_NUMBER;
  744. break;
  745. }
  746. #if 1
  747. crc = ASN1C_MakeCrc16((void *)pMsg->datex_Data.buf, pMsg->datex_Data.size);
  748. if (pMsg->datex_Crc_nbr.size != 2 ||
  749. pMsg->datex_Crc_nbr.buf[0] != HIBYTE(crc) ||
  750. pMsg->datex_Crc_nbr.buf[1] != LOBYTE(crc))
  751. {
  752. result = SYS_ERR_CRC;
  753. break;
  754. }
  755. #endif
  756. /* 패킷 처리 */
  757. result = ProcessRecvPacket((BYTE *)pMsg->datex_Data.buf, pMsg->datex_Data.size);
  758. if (result != SYS_ERR_NONE)
  759. {
  760. SERROR("Receive Packet Error : %d", result);
  761. }
  762. break;
  763. case RC_WMORE:
  764. case RC_FAIL:
  765. default:
  766. result = SYS_ERR_DATEXDATAPACKET_DECODE;
  767. break;
  768. }
  769. if (pMsg)
  770. {
  771. ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
  772. pMsg = 0;
  773. }
  774. if (result != SYS_ERR_NONE)
  775. {
  776. m_RxLen = 0;
  777. break;
  778. }
  779. m_RxLen -= length;
  780. if (m_RxLen < 0) m_RxLen = 0;
  781. if (m_RxLen > 0)
  782. {
  783. memcpy(m_RxBuff, m_RxBuff + length, m_RxLen);
  784. }
  785. } /* end while */
  786. return result;
  787. }
  788. //---------------------------------------------------------------------------
  789. /*
  790. * 수신 패킷 처리
  791. * arguments
  792. * void
  793. * return
  794. * void
  795. */
  796. int __fastcall TVMSCThread::ProcessRecvPacket(BYTE *pBuffer, int Len)
  797. {
  798. FErrLine = 4;
  799. int result, iResult;
  800. C2CAuthenticatedMessage_t *pMsg = 0; /* Type to decode */
  801. asn_dec_rval_t rval; /* Decoder return value */
  802. BYTE cmd;
  803. BYTE pdu;
  804. CLIENT_ARGUMENT arg;
  805. long DataPacket_number;
  806. result = SYS_ERR_OTHER;
  807. /* Decode */
  808. rval = ber_decode(0, &asn_DEF_C2CAuthenticatedMessage, (void **)&pMsg, pBuffer, Len);
  809. switch(rval.code)
  810. {
  811. case RC_OK:
  812. {
  813. if ((rval.consumed == 0) || (pMsg == 0))
  814. {
  815. LERROR("SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE");
  816. result = SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE;
  817. break;
  818. }
  819. /* 공통부분 */
  820. if (pMsg->datex_AuthenticationInfo_text.size != 1 ||
  821. /* pMsg->datex_DataPacket_number < 0 || */
  822. /* pMsg->datex_DataPacket_number > 4294967295 || */
  823. /* pMsg->datex_DataPacketPriority_number < 0 || */
  824. pMsg->datex_DataPacketPriority_number > 10)
  825. {
  826. LERROR("SYS_ERR_C2CAUTHENTICATEDMESSAGE: %d/%d", pMsg->datex_AuthenticationInfo_text.size, pMsg->datex_DataPacketPriority_number);
  827. result = SYS_ERR_C2CAUTHENTICATEDMESSAGE;
  828. break;
  829. }
  830. asn_INTEGER2long(&pMsg->datex_DataPacket_number, &DataPacket_number);
  831. pdu = pMsg->pdu.present;
  832. cmd = pMsg->datex_AuthenticationInfo_text.buf[0];
  833. #if 0
  834. HeaderOptions_t *options;
  835. /* Header Options */
  836. options = &pMsg->options;
  837. if (options->datex_OriginAddress_location && options->datex_DestinationAddress_location)
  838. {
  839. LINFO("Recv OrginAddr: %s(%s), DestinationAddr: %s(%s)",
  840. options->datex_OriginAddress_location->buf,
  841. m_Client.CenterId.c_str(),
  842. options->datex_DestinationAddress_location->buf,
  843. m_Client.LocalCenterId.c_str());
  844. }
  845. #if 0
  846. if (options->datex_OriginAddress_location->size == 0 ||
  847. options->datex_DestinationAddress_location->size == 0)
  848. {
  849. result = SYS_ERR_HEADER_OPTIONS;
  850. break;
  851. }
  852. if (memcmp(options->datex_OriginAddress_location->buf,
  853. m_Client.CenterId.c_str(),
  854. options->datex_OriginAddress_location->size) ||
  855. memcmp(options->datex_DestinationAddress_location->buf,
  856. m_Client.LocalCenterId.c_str(),
  857. options->datex_DestinationAddress_location->size))
  858. {
  859. result = SYS_ERR_ADDRESS;
  860. break;
  861. }
  862. #endif
  863. #endif
  864. /* options->datex_Origin_text */
  865. /* options->datex_Sender_text */
  866. /* options->datex_SenderAddress_location */
  867. /* options->datex_Destination_text */
  868. /* options->datex_Cost->amount_Currency_code */
  869. /* options->datex_Cost->amount_Factor_quantity */
  870. /* options->datex_Cost->amount_Quantity_quantity */
  871. /* options->datex_DataPacket_time->time_Year_qty */
  872. /* options->datex_DataPacket_time->time_Month_qty */
  873. /* options->datex_DataPacket_time->time_Day_qty */
  874. /* options->datex_DataPacket_time->time_Hour_qty */
  875. /* options->datex_DataPacket_time->time_Minute_qty */
  876. /* options->datex_DataPacket_time->time_Second_qty */
  877. /* options->datex_DataPacket_time->time_SecondFractions->present */
  878. /* options->datex_DataPacket_time->time_SecondFractions->choice.milliseconds */
  879. /* options->datex_DataPacket_time->time_Timezone->timezone_Hour_qty */
  880. /* options->datex_DataPacket_time->time_Timezone->time_Minute_qty */
  881. if (cmd == AI_Reject)
  882. {
  883. //용인VMS제어기 인지를 확인하는 코드임....
  884. if (pMsg->pdu.present == PDUs_PR_accept)
  885. {
  886. if (m_Client.State == CT_LOGIN_WAIT)
  887. {
  888. //용인 VMS ==> 상태정보 요청하면 VMS제어기 프로그램이 다운됨
  889. //10초마다 상태정보를 자동으로 올림
  890. LINFO("OLD VMS Connect...");
  891. if (FVmsObj->FIsOldProtocol == false)
  892. {
  893. //여기서 설정하지 말고 환경설정으로 되어 있는것으로 하자..
  894. //FVmsObj->FIsOldProtocol = true;
  895. }
  896. }
  897. }
  898. }
  899. switch(pdu)
  900. {
  901. case PDUs_PR_NOTHING:
  902. LINFO("<<< PDUs_PR_NOTHING 수신xxxxx");
  903. result = SYS_ERR_NONE;
  904. break;
  905. case PDUs_PR_datex_Initiate_null: /* 초기 통신연결을 위한 개시 요청 데이터 패킷 */
  906. LINFO("<<< PDUs_PR_datex_Initiate 수신");
  907. m_Client.State = CT_LOGIN;
  908. result = SYS_ERR_NONE;
  909. break;
  910. case PDUs_PR_login:
  911. //여기는 들어오면 안됌(클아이언트 모듈에서 보내는 메시지임)
  912. LINFO("<<< PDUs_PR_login 수신xxxxx");
  913. result = SYS_ERR_NONE;
  914. break;
  915. case PDUs_PR_fred: /* 서버와 클라이언트의 연결을 유지하기 위한 확인 데이터 패킷 */
  916. {
  917. long fred;
  918. asn_INTEGER2long(&pMsg->pdu.choice.fred, &fred);
  919. result = SYS_ERR_NONE;
  920. LINFO("<<< PDUs_PR_fred 수신: %u", fred);
  921. }
  922. if (m_Client.State != CT_FRED_WAIT)
  923. {
  924. LWARN("m_Client.State SYS_ERR_MISMATCH: %d, %d", m_Client.State, CT_FRED_WAIT);
  925. result = SYS_ERR_MISMATCH;
  926. break;
  927. }
  928. SetClientState(SET_CLIENT_SUCC);
  929. break;
  930. case PDUs_PR_terminate: /* 연결을 종료하고자 할 때, 서버에서 클라이언트에 요청하는 데이터 패킷 */
  931. {
  932. try
  933. {
  934. long terminate;
  935. asn_INTEGER2long(&pMsg->pdu.choice.terminate, &terminate);
  936. result = SYS_ERR_NONE;
  937. LINFO("<<< PDUs_PR_terminate 수신 %u", terminate);
  938. } catch(Exception &e) {}
  939. }
  940. break;
  941. case PDUs_PR_logout: /* 접속을 종료하기 위한 클라이언트의 로그아웃 데이터 패킷 */
  942. //여기는 들어오면 안됌(클아이언트 모듈에서 보내는 메시지임)
  943. LINFO("<<< PDUs_PR_logout 수신xxxxx");
  944. result = SYS_ERR_NONE;
  945. #if 0
  946. {
  947. long logout;
  948. asn_INTEGER2long(&pMsg->pdu.choice.logout, &logout);
  949. result = SYS_ERR_NONE;
  950. LINFO("<<< PDUs_PR_logout 수신 %d", logout);
  951. }
  952. arg.FrED = m_FrEDConfirmPacketNumber++;
  953. if((iResult = ProcessCommand(AI_FrED, &arg)) != SYS_ERR_NONE)
  954. {
  955. LERROR("Logout FrED Command failed: %d", iResult);
  956. }
  957. #endif
  958. break;
  959. case PDUs_PR_subscripiton:
  960. //여기는 들어오면 안됌(클아이언트 모듈에서 보내는 메시지임)
  961. LINFO("<<< PDUs_PR_subscripiton 수신xxxxx");
  962. break;
  963. case PDUs_PR_publication:
  964. LINFO("<<< PDUs_PR_publication 수신");
  965. switch(cmd)
  966. {
  967. case AI_GeneralStatusMessage: LINFO(" *** AI_GeneralStatusMessage 수신"); break;
  968. case AI_PowerStatusMessage: LINFO(" *** AI_PowerStatusMessage 수신"); break;
  969. case AI_ModuleStatusMessage: LINFO(" *** AI_ModuleStatusMessage 수신"); break;
  970. case AI_DotStatusMessage: LINFO(" *** AI_DotStatusMessage 수신"); break;
  971. case AI_ScreenStatusMessage: LINFO(" *** AI_ScreenStatusMessage 수신"); break;
  972. case AI_DisplayingDataMessage:LINFO(" *** AI_DisplayingDataMessage 수신"); break;
  973. default: LINFO(" *** PDUs_PR_publication Unknown 수신: %d", cmd); break;
  974. }
  975. result = PacketPublication(&pMsg->pdu.choice.publication);
  976. if (result == SYS_ERR_NONE)
  977. {
  978. m_WatchDogTimer = Now();
  979. arg.Accept = DataPacket_number;
  980. if((iResult = ProcessCommand(AI_Accept, &arg)) != SYS_ERR_NONE)
  981. {
  982. SERROR("Publication Accept Command failed: %d", iResult);
  983. }
  984. }
  985. else
  986. {
  987. int ErrCode;
  988. switch(result)
  989. {
  990. case SYS_ERR_DATEXPUBLISH_FORMAT:
  991. ErrCode = datexReject_Publication_cd_invalidPublishFormat;
  992. break;
  993. case SYS_ERR_UNKNOWN_MESSAGE_ID:
  994. ErrCode = datexReject_Publication_cd_unknownPublicationMsgId;
  995. break;
  996. case SYS_ERR_DATEXPUBLISH_TYPE:
  997. ErrCode = datexReject_Publication_cd_invalidPublicationMsgContent;
  998. break;
  999. case SYS_ERR_MESSAGE_DECODE:
  1000. ErrCode = datexReject_Publication_cd_invalidPublicationMsgContent;
  1001. break;
  1002. default:
  1003. ErrCode = datexReject_Publication_cd_other;
  1004. }
  1005. /* datexReject_Publication_cd_unknownSubscription */
  1006. /* datexReject_Publication_cd_unknownPublicationNbr */
  1007. /* datexReject_Publication_cd_invalidPublicationMsgId */
  1008. /* datexReject_Publication_cd_repeatedPublicationNbr */
  1009. arg.Reject.nbr = DataPacket_number;
  1010. arg.Reject.code = ErrCode;
  1011. if ((iResult = ProcessCommand(AI_Reject, &arg)) != SYS_ERR_NONE)
  1012. {
  1013. SERROR("Publication Reject Command failed: %d", iResult);
  1014. }
  1015. }
  1016. break;
  1017. case PDUs_PR_transfer_done:
  1018. LINFO("<<< PDUs_PR_transfer_done 수신");
  1019. result = SYS_ERR_NONE;
  1020. break;
  1021. case PDUs_PR_accept: /* 클라이언트의 요청에 대한 수용 */
  1022. LINFO("<<< PDUs_PR_accept 수신");
  1023. result = PacketAccept(&pMsg->pdu.choice.accept);
  1024. break;
  1025. case PDUs_PR_reject: /* 클라이언트의 요청에 대한 거부 */
  1026. LINFO("<<< PDUs_PR_reject 수신");
  1027. result = PacketReject(&pMsg->pdu.choice.reject);
  1028. break;
  1029. default:
  1030. LINFO("<<< Unknown PDUs 수신: pdu(%d), cmd(%d)", pdu, cmd);
  1031. result = SYS_ERR_UNKNOWN_COMMAND;
  1032. break;
  1033. }/* end switch */
  1034. }
  1035. break;
  1036. case RC_WMORE:
  1037. case RC_FAIL:
  1038. default:
  1039. result = SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE;
  1040. break;
  1041. }/* end switch */
  1042. if (pMsg)
  1043. {
  1044. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1045. }
  1046. if ((result == SYS_ERR_NONE) && (cmd != AI_Reject))
  1047. {
  1048. m_Client.iRetry = VMS_MAX_RETRY_COUNT;
  1049. }
  1050. return result;
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. /*
  1054. * 수신 패킷 처리
  1055. * arguments
  1056. * void
  1057. * return
  1058. * void
  1059. */
  1060. int __fastcall TVMSCThread::PacketAccept(Accept_t *pAccept)
  1061. {
  1062. FErrLine = 5;
  1063. int result;
  1064. long nbr;
  1065. long sub;
  1066. result = SYS_ERR_OTHER;
  1067. asn_INTEGER2long(&pAccept->datexAccept_Packet_nbr, &nbr);
  1068. switch(pAccept->datexAccept_Type.present)
  1069. {
  1070. case datexAccept_Type_PR_logIn:
  1071. if (m_Client.State != CT_LOGIN_WAIT)
  1072. {
  1073. result = SYS_ERR_MISMATCH;
  1074. break;
  1075. }
  1076. /* pAccept->datexAccept_Type.choice.logIn */
  1077. SetClientState(SET_CLIENT_SUCC);
  1078. result = SYS_ERR_NONE;
  1079. LINFO(" *** Accept_Type_PR_logIn: %u", nbr);
  1080. break;
  1081. case datexAccept_Type_PR_single_subscription:
  1082. if (m_Client.State != CT_LOCAL_WAIT)
  1083. {
  1084. result = SYS_ERR_MISMATCH;
  1085. break;
  1086. }
  1087. if (nbr == (long)m_Ctl.DataPacketNumber)
  1088. {
  1089. if (m_Ctl.obj == OBJ_GeneralStatus)
  1090. {
  1091. m_Registered.GeneralStatus = true;
  1092. }
  1093. else
  1094. if (m_Ctl.obj == OBJ_ModuleStatus)
  1095. {
  1096. m_Registered.ModuleStatus = true;
  1097. }
  1098. else
  1099. if (m_Ctl.obj == OBJ_PowerStatus)
  1100. {
  1101. m_Registered.PowerStatus = true;
  1102. }
  1103. else
  1104. if (m_Ctl.obj == OBJ_ScreenStatus)
  1105. {
  1106. m_Registered.ScreenDataStatusMessage = true;
  1107. }
  1108. else
  1109. if (m_Ctl.obj == OBJ_DisplayingData)
  1110. {
  1111. m_Registered.RealTimeDisplayMessage = true;
  1112. }
  1113. }/* end if */
  1114. SetClientState(SET_CLIENT_SUCC);
  1115. result = SYS_ERR_NONE;
  1116. LINFO(" *** Accept_Type_PR_single_subscription: %u", nbr);
  1117. break;
  1118. case datexAccept_Type_PR_registered_subscription:
  1119. if (m_Client.State != CT_LOCAL_WAIT && m_Client.State != CT_REGIST_WAIT)
  1120. {
  1121. result = SYS_ERR_MISMATCH;
  1122. break;
  1123. }
  1124. if (nbr == (long)m_Ctl.DataPacketNumber)
  1125. {
  1126. if (m_Ctl.obj == OBJ_GeneralStatus)
  1127. {
  1128. if (m_Client.State == CT_REGIST_WAIT)
  1129. m_SubRegistered.GeneralStatus = true;
  1130. else
  1131. m_Registered.GeneralStatus = true;
  1132. }
  1133. else
  1134. if (m_Ctl.obj == OBJ_PowerStatus)
  1135. {
  1136. if (m_Client.State == CT_REGIST_WAIT)
  1137. m_SubRegistered.PowerStatus = true;
  1138. else
  1139. m_Registered.PowerStatus = true;
  1140. }
  1141. else
  1142. if (m_Ctl.obj == OBJ_ModuleStatus)
  1143. {
  1144. if (m_Client.State == CT_REGIST_WAIT)
  1145. m_SubRegistered.ModuleStatus = true;
  1146. else
  1147. m_Registered.ModuleStatus = true;
  1148. }
  1149. else
  1150. if (m_Ctl.obj == OBJ_DotStatus)
  1151. {
  1152. if (m_Client.State == CT_REGIST_WAIT)
  1153. m_SubRegistered.DotStatus = true;
  1154. else
  1155. m_Registered.DotStatus = true;
  1156. }
  1157. else
  1158. if (m_Ctl.obj == OBJ_ScreenStatus)
  1159. {
  1160. if (m_Client.State == CT_REGIST_WAIT)
  1161. m_SubRegistered.ScreenStatus = true;
  1162. else
  1163. m_Registered.ScreenDataStatusMessage = true;
  1164. }
  1165. else
  1166. if (m_Ctl.obj == OBJ_DisplayingData)
  1167. {
  1168. if (m_Client.State == CT_REGIST_WAIT)
  1169. m_SubRegistered.DisplayingData = true;
  1170. else
  1171. m_Registered.RealTimeDisplayMessage = true;
  1172. }
  1173. }/* end if */
  1174. asn_INTEGER2long(&pAccept->datexAccept_Type.choice.registered_subscription, &sub);
  1175. SetClientState(SET_CLIENT_SUCC);
  1176. result = SYS_ERR_NONE;
  1177. LINFO(" *** Accept_Type_PR_registered_subscription: %u, %u", nbr, sub);
  1178. break;
  1179. /*
  1180. case datexAccept_Type_PR_publication:
  1181. break;
  1182. */
  1183. default:
  1184. SERROR(" *** Unknown Accept_Type: %u, %d", nbr, pAccept->datexAccept_Type.present);
  1185. result = SYS_ERR_ACCEPT_TYPE;
  1186. break;
  1187. }
  1188. return result;
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. /*
  1192. * 수신 패킷 처리
  1193. * arguments
  1194. * void
  1195. * return
  1196. * void
  1197. */
  1198. int __fastcall TVMSCThread::PacketReject(Reject_t *pReject)
  1199. {
  1200. FErrLine = 6;
  1201. int result;
  1202. long nbr;
  1203. long ErrCode;
  1204. result = SYS_ERR_OTHER;
  1205. asn_INTEGER2long(&pReject->datexReject_Packet_nbr, &nbr);
  1206. switch(pReject->datexReject_Type.present)
  1207. {
  1208. case RejectType_PR_datexReject_Login_cd:
  1209. asn_INTEGER2long(&pReject->datexReject_Type.choice.datexReject_Login_cd, &ErrCode);
  1210. if (m_Client.State != CT_LOGIN_WAIT)
  1211. {
  1212. result = SYS_ERR_MISMATCH;
  1213. break;
  1214. }
  1215. SetClientState(SET_CLIENT_FAIL);
  1216. result = SYS_ERR_NONE;
  1217. break;
  1218. case RejectType_PR_datexReject_Subscription_cd:
  1219. asn_INTEGER2long(&pReject->datexReject_Type.choice.datexReject_Subscription_cd, &ErrCode);
  1220. if (m_Client.State != CT_LOCAL_WAIT) {
  1221. result = SYS_ERR_MISMATCH;
  1222. break;
  1223. }
  1224. SetClientState(SET_CLIENT_FAIL);
  1225. result = SYS_ERR_NONE;
  1226. break;
  1227. /*
  1228. case RejectType_PR_datexReject_Publication_cd:
  1229. asn_INTEGER2long(&pReject->datexReject_Type.choice.datexReject_Publication_cd, &ErrCode);
  1230. break;
  1231. */
  1232. default:
  1233. ErrCode = -1;
  1234. break;
  1235. }
  1236. /* pReject->datexReject_AlternateRequest */
  1237. LINFO("Reject 수신[%d %d %d]", nbr, pReject->datexReject_Type.present, ErrCode);
  1238. return result;
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. /*
  1242. * 수신 패킷 처리
  1243. * arguments
  1244. * void
  1245. * return
  1246. * void
  1247. */
  1248. int __fastcall TVMSCThread::PacketPublication(Publication_t *pPublication)
  1249. {
  1250. FErrLine = 7;
  1251. int result;
  1252. PublicationData_t *pData;
  1253. int i;
  1254. result = SYS_ERR_OTHER;
  1255. /* pPublication->datexPublish_Guaranteed_bool */
  1256. switch(pPublication->datexPublish_Format.present)
  1257. {
  1258. case Publish_Format_PR_datexPublish_Data:
  1259. for(i = 0; i < pPublication->datexPublish_Format.choice.datexPublish_Data.list.count; i++)
  1260. {
  1261. pData = (PublicationData_t *)pPublication->datexPublish_Format.choice.datexPublish_Data.list.array[i];
  1262. if ((result = PacketPublicationData(pData)) != SYS_ERR_NONE)
  1263. {
  1264. break;
  1265. }
  1266. }
  1267. break;
  1268. case Publish_Format_PR_datexPublish_FileName_txt:
  1269. result = SYS_ERR_NONE;
  1270. break;
  1271. default:
  1272. result = SYS_ERR_DATEXPUBLISH_FORMAT;
  1273. break;
  1274. }/* end switch */
  1275. return result;
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. /*
  1279. * 수신 패킷 처리
  1280. * arguments
  1281. * void
  1282. * return
  1283. * void
  1284. */
  1285. int __fastcall TVMSCThread::PacketPublicationData(PublicationData_t *pData)
  1286. {
  1287. FErrLine = 8;
  1288. int result;
  1289. int Message_id;
  1290. int i;
  1291. result = SYS_ERR_OTHER;
  1292. /* pData->datexPublish_SubscribeSerial_nbr */
  1293. /* pData->datexPublish_Serial_nbr */
  1294. /* pData->datexPublish_LatePublicationFlag */
  1295. #if 0
  1296. {
  1297. long lv;
  1298. asn_INTEGER2long(&pData->datexPublish_SubscribeSerial_nbr, &lv);
  1299. LDEBUG("datexPublish_SubscribeSerial_nbr: %u", lv);
  1300. asn_INTEGER2long(&pData->datexPublish_Serial_nbr, &lv);
  1301. LDEBUG(" datexPublish_Serial_nbr: %u", lv);
  1302. LDEBUG("datexPublish_LatePublicationFlag: %d", pData->datexPublish_LatePublicationFlag);
  1303. }
  1304. #endif
  1305. switch(pData->datexPublish_Type.present)
  1306. {
  1307. case PublicationType_PR_datexPublication_Management_cd:
  1308. result = SYS_ERR_NONE;
  1309. break;
  1310. case PublicationType_PR_datexPublish_Data:
  1311. #if 0
  1312. {
  1313. int arcs[10];
  1314. int count, j;
  1315. count = OBJECT_IDENTIFIER_get_arcs(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_id,
  1316. arcs, sizeof(int), 10);
  1317. LDEBUG("endApplication_Message_id: %d", count);
  1318. for (j = 0; j < count && j < 10; j++)
  1319. {
  1320. LDEBUG("[%d]: %d", j, arcs[j]);
  1321. }
  1322. }
  1323. #endif
  1324. if ((Message_id = ASN1C_GetObjId(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_id)) == -1)
  1325. {
  1326. return SYS_ERR_UNKNOWN_MESSAGE_ID;
  1327. }
  1328. /* pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg */
  1329. switch(Message_id)
  1330. {
  1331. case OBJ_GeneralStatus: /* 기본상태정보 */
  1332. {
  1333. GeneralStatusMessage_t *pGS;
  1334. MessageBodyGeneralStatusMessage_t *pMsg = 0; /* Type to decode */
  1335. /* Decode */
  1336. if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
  1337. &asn_DEF_MessageBodyGeneralStatusMessage,
  1338. (void **)&pMsg) == ASN_ERR_NONE)
  1339. {
  1340. for(i = 0; i < pMsg->list.count; i++)
  1341. {
  1342. pGS = (GeneralStatusMessage_t *)pMsg->list.array[i];
  1343. SaveStateData(pGS);
  1344. }
  1345. //LINFO("GeneralStatus Publication Recv %d OK", pMsg->list.count);
  1346. result = VERR_NONE;
  1347. }
  1348. else
  1349. {
  1350. result = SYS_ERR_MESSAGE_DECODE;
  1351. }
  1352. if(pMsg)
  1353. {
  1354. ASN1C_Free(&asn_DEF_MessageBodyGeneralStatusMessage, pMsg);
  1355. }
  1356. FGenState = true;
  1357. }
  1358. break;
  1359. case OBJ_PowerStatus: /* 전원상태정보 */
  1360. {
  1361. PowerStatusMessage_t *pMS;
  1362. MessageBodyPowerStatusMessage_t *pMsg = 0; /* Type to decode */
  1363. long PowerQty;
  1364. int numbits, bit;
  1365. /* Decode */
  1366. if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
  1367. &asn_DEF_MessageBodyPowerStatusMessage,
  1368. (void **)&pMsg) == ASN_ERR_NONE)
  1369. {
  1370. for(i = 0; i < pMsg->list.count; i++)
  1371. {
  1372. pMS = (PowerStatusMessage_t *)pMsg->list.array[i];
  1373. asn_INTEGER2long(&pMS->power_Qty, &PowerQty);
  1374. numbits = DATEX_NUMBITS(pMS->power_Status);
  1375. //LDEBUG("Power Module Count: %d, %d", PowerQty, numbits);
  1376. if (PowerQty != numbits)
  1377. {
  1378. PowerQty = numbits;//양산거는 다르게 나타남
  1379. }
  1380. if (numbits >= INT_VMS_MAX_POWER)
  1381. {
  1382. numbits = INT_VMS_MAX_POWER;
  1383. PowerQty = numbits;
  1384. LERROR("Power size overflow: %d, %d", numbits, INT_VMS_MAX_POWER);
  1385. }
  1386. if (PowerQty == numbits)
  1387. {
  1388. memset((char*)m_pState->PowerStatus, 0x00, sizeof(m_pState->PowerStatus));
  1389. for (bit = 0; bit < numbits; bit++)
  1390. {
  1391. int nOnOff = ASN1C_CheckBitString(&pMS->power_Status, bit);
  1392. if (nOnOff == bit_enable)
  1393. {
  1394. VComm_SetBitString((char*)m_pState->PowerStatus, bit);
  1395. }
  1396. }
  1397. m_pState->PowerCount = PowerQty;
  1398. }
  1399. if (FLogFile->FLogCfg.Debug)
  1400. {
  1401. LDEBUG(" module_PowerQty: %d", PowerQty);
  1402. AnsiString sModule = "";
  1403. for (int v = 0; v < PowerQty; v++)
  1404. {
  1405. int nM = VComm_GetBitString((char*)m_pState->PowerStatus, v);
  1406. sModule += String(nM);
  1407. }
  1408. LDEBUG("%s", sModule.c_str());
  1409. }
  1410. }/* end for */
  1411. result = VERR_NONE;
  1412. }
  1413. else
  1414. {
  1415. result = SYS_ERR_MESSAGE_DECODE;
  1416. }/* end if */
  1417. if(pMsg)
  1418. {
  1419. ASN1C_Free(&asn_DEF_MessageBodyPowerStatusMessage, pMsg);
  1420. }
  1421. FPowState = true;
  1422. }
  1423. break;
  1424. case OBJ_ModuleStatus: /* 모듈상태정보 */
  1425. {
  1426. ModuleStatusMessage_t *pMS;
  1427. MessageBodyModuleStatusMessage_t *pMsg = 0; /* Type to decode */
  1428. long Horizontal, Vertical;
  1429. int numbits, bit;
  1430. BYTE ModuleState;
  1431. /* Decode */
  1432. if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
  1433. &asn_DEF_MessageBodyModuleStatusMessage,
  1434. (void **)&pMsg) == ASN_ERR_NONE)
  1435. {
  1436. for(i = 0; i < pMsg->list.count; i++)
  1437. {
  1438. pMS = (ModuleStatusMessage_t *)pMsg->list.array[i];
  1439. asn_INTEGER2long(&pMS->module_HorizontalQty, &Horizontal);
  1440. asn_INTEGER2long(&pMS->module_VerticalQty, &Vertical);
  1441. numbits = DATEX_NUMBITS(pMS->module_Status);
  1442. if (numbits >= INT_VMS_MAX_MODULE)
  1443. {
  1444. numbits = INT_VMS_MAX_MODULE;
  1445. LERROR("Module size overflow: %d, %d", numbits, INT_VMS_MAX_MODULE);
  1446. }
  1447. memset((char*)m_pState->ModuleStatus, 0x00, sizeof(m_pState->ModuleStatus));
  1448. if ((Horizontal * Vertical) == numbits)
  1449. {
  1450. ModuleState = vms_normal;
  1451. for (bit = 0; bit < numbits; bit++)
  1452. {
  1453. int nOnOff = ASN1C_CheckBitString(&pMS->module_Status, bit);
  1454. if (nOnOff == bit_disable)
  1455. {
  1456. ModuleState = vms_error;
  1457. }
  1458. else
  1459. {
  1460. VComm_SetBitString((char*)m_pState->ModuleStatus, bit);
  1461. }
  1462. }
  1463. if ((m_pState != NULL) && (numbits < INT_VMS_MAX_MODULE))
  1464. {
  1465. m_pState->ModuleHorizontal = Horizontal;
  1466. m_pState->ModuleVertical = Vertical;
  1467. }
  1468. }
  1469. else
  1470. {
  1471. ModuleState = vms_unknown;
  1472. m_pState->ModuleHorizontal = 0;
  1473. m_pState->ModuleVertical = 0;
  1474. }
  1475. m_pState->ModuleState = ModuleState;
  1476. if (FLogFile->FLogCfg.Debug)
  1477. {
  1478. LDEBUG(" module_VerticalQty x HorizontalQty(Cnt): %d x %d (%d)", Vertical, Horizontal, numbits);
  1479. //LDEBUG(" module_HorizontalQty: %d", Horizontal);
  1480. //LDEBUG("module_Status numbits: %d", numbits);
  1481. for (int v = 0, bit=0; v < Vertical; v++)
  1482. {
  1483. AnsiString sModule = "";
  1484. for(int h = 0; h < Horizontal; h++)
  1485. {
  1486. int nM = VComm_GetBitString((char*)m_pState->ModuleStatus, v*h);
  1487. sModule += String(nM);
  1488. }
  1489. LDEBUG("%d, %s", v, sModule.c_str());
  1490. }
  1491. }
  1492. }/* end for */
  1493. result = SYS_ERR_NONE;
  1494. }
  1495. else
  1496. {
  1497. result = SYS_ERR_MESSAGE_DECODE;
  1498. }/* end if */
  1499. if(pMsg)
  1500. {
  1501. ASN1C_Free(&asn_DEF_MessageBodyModuleStatusMessage, pMsg);
  1502. }
  1503. FModState = true;
  1504. }
  1505. break;
  1506. case OBJ_DotStatus: /* 도트상태정보 */
  1507. LINFO(" *** OBJ_DotStatus");
  1508. result = SYS_ERR_NONE;
  1509. break;
  1510. case OBJ_ScreenStatus: /* 표출상태정보 */
  1511. {
  1512. LINFO(" *** OBJ_ScreenStatus");
  1513. ScreenStatusMessage_t *pMS;
  1514. MessageBodyScreenStatusMessage_t *pMsg = 0; /* Type to decode */
  1515. /* Decode */
  1516. if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
  1517. &asn_DEF_MessageBodyScreenStatusMessage,
  1518. (void **)&pMsg) == ASN_ERR_NONE)
  1519. {
  1520. BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
  1521. int eLen;
  1522. for(i = 0; i < pMsg->list.count; i++)
  1523. {
  1524. pMS = (ScreenStatusMessage_t *)pMsg->list.array[i];
  1525. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  1526. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pMS->screen_StatusData,
  1527. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  1528. {
  1529. result = -14;
  1530. break;
  1531. }
  1532. //pMS->capture_Time;
  1533. //pMS->screen_StatusData;
  1534. }/* end for */
  1535. result = SYS_ERR_NONE;
  1536. }
  1537. else
  1538. {
  1539. result = SYS_ERR_MESSAGE_DECODE;
  1540. }/* end if */
  1541. if (pMsg)
  1542. {
  1543. ASN1C_Free(&asn_DEF_MessageBodyScreenStatusMessage, pMsg);
  1544. }
  1545. }
  1546. //result = SYS_ERR_NONE;
  1547. break;
  1548. case OBJ_DisplayingData: /* 표출정보 */
  1549. {
  1550. LINFO(" *** OBJ_DisplayingData");
  1551. }
  1552. result = SYS_ERR_NONE;
  1553. break;
  1554. default:
  1555. LINFO(" *** Unknown PublicationData: %u", Message_id);
  1556. result = SYS_ERR_UNKNOWN_MESSAGE_ID;
  1557. break;
  1558. }/* end switch */
  1559. if (FGenState && FModState && FPowState)
  1560. {
  1561. FGenState = FModState = FPowState = false;
  1562. POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_STATE_STTS, (int)FVmsObj);
  1563. }
  1564. break;
  1565. default:
  1566. result = SYS_ERR_DATEXPUBLISH_TYPE;
  1567. break;
  1568. }
  1569. return result;
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. /*
  1573. * 패킷 송신
  1574. * arguments
  1575. * void
  1576. * return
  1577. * void
  1578. */
  1579. int __fastcall TVMSCThread::SendPacket(void *Data, int Len, BYTE cmd)
  1580. {
  1581. FErrLine = 9;
  1582. int result;
  1583. DatexDataPacket_t *pMsg; /* Type to encode */
  1584. asn_enc_rval_t er; /* Encoder return value */
  1585. BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
  1586. WORD crc;
  1587. int eLen, wLen;
  1588. result = SYS_ERR_OTHER;
  1589. pMsg = (DatexDataPacket_t *)ASN1C_Alloc(sizeof(DatexDataPacket_t));
  1590. if (pMsg == NULL)
  1591. {
  1592. return SYS_ERR_INTERNAL;
  1593. }
  1594. if (asn_long2INTEGER(&pMsg->datex_Version_number, datex_Version_number_version1) != ASN_ERR_NONE)
  1595. {
  1596. ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
  1597. return -1;
  1598. }
  1599. if (OCTET_STRING_fromBuf(&pMsg->datex_Data, (char *)Data, Len) != ASN_ERR_NONE)
  1600. {
  1601. ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
  1602. return -2;
  1603. }
  1604. crc = ASN1C_MakeCrc16(Data, Len);
  1605. crc = CommUtil_WordSwap(crc);
  1606. if (OCTET_STRING_fromBuf(&pMsg->datex_Crc_nbr, (char *)&crc, sizeof(crc)) != ASN_ERR_NONE)
  1607. {
  1608. ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
  1609. return -3;
  1610. }
  1611. /* Encode */
  1612. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  1613. eLen = -1;
  1614. er = der_encode_to_buffer(&asn_DEF_DatexDataPacket, pMsg, MsgBuf, sizeof(MsgBuf));
  1615. if (er.encoded == -1)
  1616. {
  1617. /*
  1618. * Failed to encode the data.
  1619. */
  1620. SERROR("SendPacket: Cannot encode %s", er.failed_type->name);
  1621. result = SYS_ERR_DATEXDATAPACKET_ENCODE;
  1622. }
  1623. else
  1624. {
  1625. /* Return the number of bytes */
  1626. eLen = er.encoded;
  1627. wLen = TcpClient->SendBuf(MsgBuf, eLen);
  1628. LINFO("Send: %d/%d, cmd: %02X", eLen, wLen, cmd);
  1629. if (eLen == wLen)
  1630. {
  1631. --m_Client.iRetry;
  1632. m_Client.cTimer = Now();
  1633. result = SYS_ERR_NONE;
  1634. LogData("SEND", (char*)MsgBuf, eLen);
  1635. }
  1636. else
  1637. {
  1638. LERROR("TCP_Write error(%d %d)", eLen, wLen);
  1639. result = SYS_ERR_WRITE_LENGTH;
  1640. LogData("SEND Fail", (char*)MsgBuf, eLen);
  1641. }
  1642. }
  1643. ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
  1644. return result;
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. /*
  1648. * 송신 패킷 처리
  1649. * arguments
  1650. * void
  1651. * return
  1652. * void
  1653. */
  1654. int __fastcall TVMSCThread::ProcessCommand(BYTE cmd, CLIENT_ARGUMENT *arg)
  1655. {
  1656. FErrLine = 10;
  1657. C2CAuthenticatedMessage_t *pMsg; /* Type to encode */
  1658. asn_enc_rval_t er; /* Encoder return value */
  1659. BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
  1660. int eLen;
  1661. int result;
  1662. Word year, month, day;
  1663. Word hour, min, sec, msec;
  1664. result = SYS_ERR_OTHER;
  1665. Now().DecodeDate(&year, &month, &day);
  1666. Now().DecodeTime(&hour, &min, &sec, &msec);
  1667. pMsg = (C2CAuthenticatedMessage_t *)ASN1C_Alloc(sizeof(C2CAuthenticatedMessage_t));
  1668. if(pMsg == NULL)
  1669. {
  1670. return SYS_ERR_INTERNAL;
  1671. }
  1672. /* 공통부분 */
  1673. BYTE auth = cmd;
  1674. if (FVmsObj->FIsOldProtocol)
  1675. {
  1676. if (auth == AI_Subscription) auth = 0x05;
  1677. else
  1678. if (auth == AI_Login) auth = 0x01;
  1679. }
  1680. if (OCTET_STRING_fromBuf(&pMsg->datex_AuthenticationInfo_text, (char *)&auth, sizeof(auth)) != ASN_ERR_NONE)
  1681. {
  1682. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1683. return -1;
  1684. }
  1685. if (asn_long2INTEGER(&pMsg->datex_DataPacket_number, m_DataPacketNumber++) != ASN_ERR_NONE)
  1686. {
  1687. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1688. return -2;
  1689. }
  1690. pMsg->datex_DataPacketPriority_number = 0;
  1691. /* Header Options */
  1692. /* pMsg->options.datex_Origin_text */
  1693. pMsg->options.datex_OriginAddress_location = (OCTET_STRING_t *)ASN1C_Alloc(sizeof(OCTET_STRING_t));
  1694. if (pMsg->options.datex_OriginAddress_location == NULL)
  1695. {
  1696. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1697. return -3;
  1698. }
  1699. if (OCTET_STRING_fromString(pMsg->options.datex_OriginAddress_location, m_Client.LocalDomainNm.c_str()) != ASN_ERR_NONE)
  1700. {
  1701. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1702. return -4;
  1703. }
  1704. /* pMsg->options.datex_Sender_text */
  1705. /* pMsg->options.datex_SenderAddress_location */
  1706. /* pMsg->options.datex_Destination_text */
  1707. #if 1
  1708. pMsg->options.datex_SenderAddress_location = (OCTET_STRING_t *)ASN1C_Alloc(sizeof(OCTET_STRING_t));
  1709. if (pMsg->options.datex_SenderAddress_location == NULL)
  1710. {
  1711. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1712. return -5;
  1713. }
  1714. if (OCTET_STRING_fromString(pMsg->options.datex_SenderAddress_location, m_Client.LocalDomainNm.c_str()) != ASN_ERR_NONE)
  1715. {
  1716. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1717. return -6;
  1718. }
  1719. #endif
  1720. pMsg->options.datex_DestinationAddress_location = (OCTET_STRING_t *)ASN1C_Alloc(sizeof(OCTET_STRING_t));
  1721. if (pMsg->options.datex_DestinationAddress_location == NULL)
  1722. {
  1723. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1724. return -5;
  1725. }
  1726. if (OCTET_STRING_fromString(pMsg->options.datex_DestinationAddress_location, m_Client.DomainNm.c_str()) != ASN_ERR_NONE)
  1727. {
  1728. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1729. return -6;
  1730. }
  1731. /* pMsg->options.datex_Cost->amount_Currency_code */
  1732. /* pMsg->options.datex_Cost->amount_Factor_quantity */
  1733. /* pMsg->options.datex_Cost->amount_Quantity_quantity */
  1734. pMsg->options.datex_DataPacket_time = (Time_t *)ASN1C_Alloc(sizeof(Time_t));
  1735. if (pMsg->options.datex_DataPacket_time == NULL)
  1736. {
  1737. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1738. return -7;
  1739. }
  1740. ////2016-11-17 13:08:25
  1741. #if 0
  1742. year = 2016;
  1743. month = 11;
  1744. day = 17;
  1745. hour = 13;
  1746. min = 8;
  1747. sec = 25;
  1748. #endif
  1749. pMsg->options.datex_DataPacket_time->time_Year_qty = (long *)ASN1C_Alloc(sizeof(long));
  1750. if (pMsg->options.datex_DataPacket_time->time_Year_qty == NULL)
  1751. {
  1752. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1753. return -8;
  1754. }
  1755. *pMsg->options.datex_DataPacket_time->time_Year_qty = year;
  1756. pMsg->options.datex_DataPacket_time->time_Month_qty = (long *)ASN1C_Alloc(sizeof(long));
  1757. if (pMsg->options.datex_DataPacket_time->time_Month_qty == NULL)
  1758. {
  1759. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1760. return -9;
  1761. }
  1762. *pMsg->options.datex_DataPacket_time->time_Month_qty = month;
  1763. pMsg->options.datex_DataPacket_time->time_Day_qty = (long *)ASN1C_Alloc(sizeof(long));
  1764. if (pMsg->options.datex_DataPacket_time->time_Day_qty == NULL)
  1765. {
  1766. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1767. return -10;
  1768. }
  1769. *pMsg->options.datex_DataPacket_time->time_Day_qty = day;
  1770. pMsg->options.datex_DataPacket_time->time_Hour_qty = hour;
  1771. pMsg->options.datex_DataPacket_time->time_Minute_qty = min;
  1772. pMsg->options.datex_DataPacket_time->time_Second_qty = sec;
  1773. pMsg->options.datex_DataPacket_time->time_SecondFractions_zz.present = time_SecondFractions_PR_milliseconds;
  1774. pMsg->options.datex_DataPacket_time->time_SecondFractions_zz.choice.milliseconds = 0;
  1775. #if 0
  1776. pMsg->options.datex_DataPacket_time->time_Timezone = (Timezone_t*)ASN1C_Alloc(sizeof(Timezone_t));
  1777. if (pMsg->options.datex_DataPacket_time->time_Timezone)
  1778. {
  1779. pMsg->options.datex_DataPacket_time->time_Timezone->timezone_Hour_qty = 9;
  1780. pMsg->options.datex_DataPacket_time->time_Timezone->time_Minute_qty = 0;
  1781. }
  1782. #endif
  1783. /* pMsg->options.datex_DataPacket_time->time_Timezone->timezone_Hour_qty */
  1784. /* pMsg->options.datex_DataPacket_time->time_Timezone->time_Minute_qty */
  1785. switch(cmd)
  1786. {
  1787. case AI_Login: /* 서버에 접속하기 위한 클라이언트의 로그인 데이터 패킷 */
  1788. LINFO(">>> AI_Login 전송");
  1789. pMsg->pdu.present = PDUs_PR_login;
  1790. if ((result = CommandLogin(&pMsg->pdu.choice.login)) != SYS_ERR_NONE)
  1791. {
  1792. SERROR("CommandLogin result failed: %d", result);
  1793. result = -12;
  1794. }
  1795. break;
  1796. case AI_FrED: /* 서버와 클라이언트의 연결을 유지하기 위한 확인 데이터 패킷 */
  1797. LINFO(">>> AI_FrED 전송");
  1798. pMsg->pdu.present = PDUs_PR_fred;
  1799. //TODO: arg==NULL인데 사용하고 있음.......
  1800. #if 0
  1801. if (asn_long2INTEGER(&pMsg->pdu.choice.fred, arg->FrED) != ASN_ERR_NONE)
  1802. {
  1803. result = -13;
  1804. break;
  1805. }
  1806. #endif
  1807. result = SYS_ERR_NONE;
  1808. break;
  1809. case AI_Logout: /* 접속을 종료하기 위한 클라이언트의 로그아웃 데이터 패킷 */
  1810. LINFO(">>> AI_Logout 전송");
  1811. pMsg->pdu.present = PDUs_PR_logout;
  1812. #if 0
  1813. if (asn_long2INTEGER(&pMsg->pdu.choice.logout, arg->Logout) != ASN_ERR_NONE)
  1814. {
  1815. result = -14;
  1816. break;
  1817. }
  1818. #endif
  1819. result = SYS_ERR_NONE;
  1820. break;
  1821. case AI_Subscription: /* 클라이언트가 서버에 정보를 요청할 경우 송신하는 데이터 패킷 */
  1822. LINFO(">>> AI_Subscription 전송");
  1823. pMsg->pdu.present = PDUs_PR_subscripiton;
  1824. if ((result = CommandSubscription(&pMsg->pdu.choice.subscripiton, arg->Subscription)) != SYS_ERR_NONE)
  1825. {
  1826. SERROR("CommandSubscription result %d", result);
  1827. result = -15;
  1828. }
  1829. break;
  1830. case AI_Accept: /* 클라이언트의 요청에 대한 수용 */
  1831. LINFO(">>> AI_Accept 전송");
  1832. pMsg->pdu.present = PDUs_PR_accept;
  1833. if (asn_long2INTEGER(&pMsg->pdu.choice.accept.datexAccept_Packet_nbr, arg->Accept) != ASN_ERR_NONE)
  1834. {
  1835. result = -16;
  1836. break;
  1837. }
  1838. pMsg->pdu.choice.accept.datexAccept_Type.present = datexAccept_Type_PR_publication;
  1839. result = SYS_ERR_NONE;
  1840. break;
  1841. case AI_Reject: /* 클라이언트의 요청에 대한 거부 */
  1842. LINFO(">>> AI_Reject 전송");
  1843. pMsg->pdu.present = PDUs_PR_reject;
  1844. if (asn_long2INTEGER(&pMsg->pdu.choice.reject.datexReject_Packet_nbr, arg->Reject.nbr) != ASN_ERR_NONE)
  1845. {
  1846. result = -17;
  1847. break;
  1848. }
  1849. pMsg->pdu.choice.reject.datexReject_Type.present = RejectType_PR_datexReject_Publication_cd;
  1850. if (asn_long2INTEGER(&pMsg->pdu.choice.reject.datexReject_Type.choice.datexReject_Publication_cd,
  1851. arg->Reject.code) != ASN_ERR_NONE)
  1852. {
  1853. result = -18;
  1854. break;
  1855. }
  1856. /* pMsg->pdu.choice.reject.datexReject_AlternateRequest */
  1857. result = SYS_ERR_NONE;
  1858. break;
  1859. default:
  1860. LERROR(">>> Unknown command(ProcessCommand)");
  1861. result = SYS_ERR_UNKNOWN_COMMAND;
  1862. break;
  1863. }
  1864. if (result == SYS_ERR_NONE)
  1865. {
  1866. /* Encode */
  1867. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  1868. eLen = -1;
  1869. er = der_encode_to_buffer(&asn_DEF_C2CAuthenticatedMessage, pMsg, MsgBuf, sizeof(MsgBuf));
  1870. if (er.encoded == -1)
  1871. {
  1872. /*
  1873. * Failed to encode the data.
  1874. */
  1875. SERROR("ProcessCommand: Cannot encode %s", er.failed_type->name);
  1876. result = SYS_ERR_C2CAUTHENTICATEDMESSAGE_ENCODE;
  1877. }
  1878. else
  1879. {
  1880. /* Return the number of bytes */
  1881. eLen = er.encoded;
  1882. result = SendPacket(MsgBuf, eLen, cmd);
  1883. }
  1884. }
  1885. ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
  1886. return result;
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. /*
  1890. * 송신 패킷 생성
  1891. * arguments
  1892. * void
  1893. * return
  1894. * void
  1895. */
  1896. int __fastcall TVMSCThread::CommandLogin(Login_t *pLogin)
  1897. {
  1898. FErrLine = 11;
  1899. int result;
  1900. OBJECT_IDENTIFIER_t *pOid;
  1901. int oid[3] = {2, 1, 1};
  1902. result = SYS_ERR_OTHER;
  1903. //VMS2000001
  1904. //LocalDomainName
  1905. if (OCTET_STRING_fromString(&pLogin->datex_Sender_txt, m_Client.LocalDomainNm.c_str()) != ASN_ERR_NONE)
  1906. {
  1907. return -1;
  1908. }
  1909. //DomainName=VmsId
  1910. //m_Client.DomainNm = "00000000";
  1911. if (OCTET_STRING_fromString(&pLogin->datex_Destinatin_txt, m_Client.DomainNm.c_str()) != ASN_ERR_NONE)
  1912. {
  1913. return -2;
  1914. }
  1915. //UserName=Site Name
  1916. if (OCTET_STRING_fromString(&pLogin->datexLogin_UserName_txt, m_Client.UserName.c_str()) != ASN_ERR_NONE)
  1917. {
  1918. return -3;
  1919. }
  1920. //UserPswd
  1921. if (OCTET_STRING_fromString(&pLogin->datexLogin_Password_txt, m_Client.UserPasswd.c_str()) != ASN_ERR_NONE)
  1922. {
  1923. return -4;
  1924. }
  1925. pOid = (OBJECT_IDENTIFIER_t *)ASN1C_Alloc(sizeof(OBJECT_IDENTIFIER_t));
  1926. if (pOid == NULL)
  1927. {
  1928. return -5;
  1929. }
  1930. if (ASN1C_SetObjectIdentifier(pOid, oid, sizeof(oid)) != ASN_ERR_NONE)
  1931. {
  1932. return -6;
  1933. }
  1934. if (ASN_SEQUENCE_ADD(&pLogin->datexLogin_EncodingRules_id_zz, pOid)!= ASN_ERR_NONE)
  1935. {
  1936. return -7;
  1937. }
  1938. pLogin->datexLogin_HearteatDurationMax_qty = m_Client.MaxHeartbeatTime;
  1939. pLogin->datexLogin_ResponseTimeOut_qty = m_Client.TimeOut;
  1940. if (asn_long2INTEGER(&pLogin->datexLogin_Initiator_cd, datexLogin_Initiator_cd_clientInitiated) != ASN_ERR_NONE)
  1941. {
  1942. return -8;
  1943. }
  1944. pLogin->datexLogin_DatagramSize_qty = m_Client.DatagramSize;
  1945. result = SYS_ERR_NONE;
  1946. #if 0
  1947. {
  1948. long lv;
  1949. int arcs[10];
  1950. int count, i, j;
  1951. OBJECT_IDENTIFIER_t *pOid;
  1952. LDEBUG("datex_Sender_txt : %s", pLogin->datex_Sender_txt.buf);
  1953. LDEBUG("datex_Destinatin_txt : %s", pLogin->datex_Destinatin_txt.buf);
  1954. LDEBUG("datexLogin_UserName_txt : %s", pLogin->datexLogin_UserName_txt.buf);
  1955. LDEBUG("datexLogin_Password_txt : %s", pLogin->datexLogin_Password_txt.buf);
  1956. LDEBUG("datexLogin_HearteatDurationMax_qty : %d", pLogin->datexLogin_HearteatDurationMax_qty);
  1957. LDEBUG("datexLogin_ResponseTimeOut_qty : %d", pLogin->datexLogin_ResponseTimeOut_qty);
  1958. asn_INTEGER2long(&pLogin->datexLogin_Initiator_cd, &lv);
  1959. LDEBUG("datexLogin_Initiator_cd : %d", lv);
  1960. LDEBUG("datexLogin_DatagramSize_qty : %d", pLogin->datexLogin_DatagramSize_qty);
  1961. for (i = 0; i < pLogin->datexLogin_EncodingRules_id_zz.list.count; i++)
  1962. {
  1963. LDEBUG("datexLogin_EncodingRules_id: [%d]", i);
  1964. pOid = (OBJECT_IDENTIFIER_t *)pLogin->datexLogin_EncodingRules_id_zz.list.array[i];
  1965. count = OBJECT_IDENTIFIER_get_arcs(pOid, arcs, sizeof(int), 10);
  1966. for (j = 0; j < count && j < 10; j++)
  1967. {
  1968. LDEBUG("[%d]: %d", j, arcs[j]);
  1969. }
  1970. }
  1971. }
  1972. #endif
  1973. return result;
  1974. }
  1975. //---------------------------------------------------------------------------
  1976. /*
  1977. * 송신 패킷 생성
  1978. * arguments
  1979. * void
  1980. * return
  1981. * void
  1982. */
  1983. int __fastcall TVMSCThread::CommandSubscription(Subscription_t *pSubscripiton, SUBSCRIPTION_ARGUMENT Arg)
  1984. {
  1985. FErrLine = 12;
  1986. int result;
  1987. SubscriptionData_t *pData;
  1988. asn_enc_rval_t er; /* Encoder return value */
  1989. BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
  1990. int eLen;
  1991. int i, count = 0;
  1992. result = SYS_ERR_OTHER;
  1993. if (asn_long2INTEGER(&pSubscripiton->datexSubscribe_Serial_nbr, Arg.nbr) != ASN_ERR_NONE)
  1994. {
  1995. return -1;
  1996. }
  1997. pSubscripiton->datexSubscribe_Type.present = SubscriptionType_PR_subscription;
  1998. pData = &pSubscripiton->datexSubscribe_Type.choice.subscription;
  1999. if (FVmsObj->FIsOldProtocol)
  2000. pData->datexSubscribe_Persistent_bool = true;
  2001. else
  2002. pData->datexSubscribe_Persistent_bool = false;
  2003. if (asn_long2INTEGER(&pData->datexSubscribe_Status_cd, datexSubscribe_Status_cd_new) != ASN_ERR_NONE)
  2004. {
  2005. return -3;
  2006. }
  2007. switch(Arg.mode)
  2008. {
  2009. case SubscriptionMode_PR_single:
  2010. pData->datexSubscribe_Mode.present = SubscriptionMode_PR_single;
  2011. break;
  2012. case SubscriptionMode_PR_event_driven:
  2013. pData->datexSubscribe_Mode.present = SubscriptionMode_PR_event_driven;
  2014. /* Registered_PR_continuous 1 */
  2015. /* Registered_PR_daily 2 */
  2016. pData->datexSubscribe_Mode.choice.event_driven.present = Registered_PR_continuous;
  2017. pData->datexSubscribe_Mode.choice.event_driven.choice.continuous.datexRegistered_UpdateDelay_qty = Arg.delay;
  2018. /* pData->datexSubscribe_Mode.choice.event_driven.choice.continuous.datexRegistered_StartTime */
  2019. /* pData->datexSubscribe_Mode.choice.event_driven.choice.continuous.datexRegistered_EndTime */
  2020. break;
  2021. case SubscriptionMode_PR_periodic:
  2022. pData->datexSubscribe_Mode.present = SubscriptionMode_PR_periodic;
  2023. /* Registered_PR_continuous 1 */
  2024. /* Registered_PR_daily 2 */
  2025. pData->datexSubscribe_Mode.choice.periodic.present = Registered_PR_continuous;
  2026. pData->datexSubscribe_Mode.choice.periodic.choice.continuous.datexRegistered_UpdateDelay_qty = Arg.delay;
  2027. /* pData->datexSubscribe_Mode.choice.periodic.choice.continuous.datexRegistered_StartTime */
  2028. /* pData->datexSubscribe_Mode.choice.periodic.choice.continuous.datexRegistered_EndTime */
  2029. break;
  2030. default:
  2031. pData->datexSubscribe_Mode.present = SubscriptionMode_PR_single;
  2032. break;
  2033. }
  2034. if (asn_long2INTEGER(&pData->datexSubscribe_PublishFormat_cd, datexSubscribe_PublishFormat_cd_dataPacket) != ASN_ERR_NONE)
  2035. {
  2036. return -4;
  2037. }
  2038. pData->datexSubscription_Priority_nbr = 1;
  2039. pData->datexSubscribe_Guarantee_bool = true;
  2040. #if 0
  2041. {
  2042. long lv;
  2043. asn_INTEGER2long(&pSubscripiton->datexSubscribe_Serial_nbr, &lv);
  2044. LDEBUG(" SubscriptionMode: %d", Arg.mode);
  2045. LDEBUG("datexSubscribe_Serial_nbr: %u", lv);
  2046. LDEBUG(" datexSubscribe_Type: %d", pSubscripiton->datexSubscribe_Type.present);
  2047. switch(pSubscripiton->datexSubscribe_Type.present)
  2048. {
  2049. case SubscriptionType_PR_subscription:
  2050. asn_INTEGER2long(&pData->datexSubscribe_Status_cd, &lv);
  2051. LDEBUG("datexSubscribe_Persistent_bool: %d", pData->datexSubscribe_Persistent_bool);
  2052. LDEBUG(" datexSubscribe_Status_cd: %d", lv);
  2053. LDEBUG(" datexSubscribe_Mode: %d", pData->datexSubscribe_Mode.present);
  2054. switch(pData->datexSubscribe_Mode.present)
  2055. {
  2056. case SubscriptionMode_PR_single:
  2057. break;
  2058. case SubscriptionMode_PR_event_driven:
  2059. case SubscriptionMode_PR_periodic:
  2060. {
  2061. Registered_t *pRegistered;
  2062. int numbits, bit;
  2063. if(pData->datexSubscribe_Mode.present == SubscriptionMode_PR_event_driven)
  2064. {
  2065. pRegistered = &pData->datexSubscribe_Mode.choice.event_driven;
  2066. }
  2067. else
  2068. {
  2069. pRegistered = &pData->datexSubscribe_Mode.choice.periodic;
  2070. }
  2071. LDEBUG("Registered: %d", pRegistered->present);
  2072. switch(pRegistered->present)
  2073. {
  2074. case Registered_PR_continuous:
  2075. LDEBUG("datexRegistered_UpdateDelay_qty: %u", pRegistered->choice.continuous.datexRegistered_UpdateDelay_qty);
  2076. if (pRegistered->choice.continuous.datexRegistered_StartTime)
  2077. {
  2078. LOG_WriteTime(pRegistered->choice.continuous.datexRegistered_StartTime);
  2079. }
  2080. if (pRegistered->choice.continuous.datexRegistered_EndTime)
  2081. {
  2082. LOG_WriteTime(pRegistered->choice.continuous.datexRegistered_EndTime);
  2083. }
  2084. break;
  2085. case Registered_PR_daily:
  2086. LDEBUG("datexRegistered_UpdateDelay_qty: %u", pRegistered->choice.daily.datexRegistered_UpdateDelay_qty);
  2087. numbits = DATEX_NUMBITS(pRegistered->choice.daily.datexRegistered_DaysOfWeek_cd);
  2088. LDEBUG("datexRegistered_DaysOfWeek_cd numbits: %d", numbits);
  2089. for (bit = 0; bit < numbits; bit++)
  2090. {
  2091. LDEBUG("[%d]: %d", bit, ASN1C_CheckBitString(&pRegistered->choice.daily.datexRegistered_DaysOfWeek_cd, bit));
  2092. }
  2093. if (pRegistered->choice.daily.datexRegistered_StartDate)
  2094. {
  2095. LOG_WriteTime(pRegistered->choice.daily.datexRegistered_StartDate);
  2096. }
  2097. if (pRegistered->choice.daily.datexRegistered_EndDate)
  2098. {
  2099. LOG_WriteTime(pRegistered->choice.daily.datexRegistered_EndDate);
  2100. }
  2101. if (pRegistered->choice.daily.datexRegistered_StartTime)
  2102. {
  2103. LOG_WriteTime(pRegistered->choice.daily.datexRegistered_StartTime);
  2104. }
  2105. if (pRegistered->choice.daily.datexRegistered_Duration_qty)
  2106. {
  2107. //LDEBUG("datexRegistered_Duration_qty: %u", *pRegistered->choice.daily.datexRegistered_Duration_qty);
  2108. }
  2109. break;
  2110. default:
  2111. LDEBUG("Unknown Registered %d", pRegistered->present);
  2112. break;
  2113. }/* end swtich */
  2114. }
  2115. break;
  2116. default :
  2117. LDEBUG("Unknown datexSubscribe_Mode %d", pData->datexSubscribe_Mode.present);
  2118. break;
  2119. }/* end switch */
  2120. asn_INTEGER2long(&pData->datexSubscribe_PublishFormat_cd, &lv);
  2121. LDEBUG("datexSubscribe_PublishFormat_cd: %d", lv);
  2122. LDEBUG(" datexSubscription_Priority_nbr: %d", pData->datexSubscription_Priority_nbr);
  2123. LDEBUG(" datexSubscribe_Guarantee_bool: %d", pData->datexSubscribe_Guarantee_bool);
  2124. break;
  2125. case SubscriptionType_PR_datexSubscribe_CancelReason_cd:
  2126. LDEBUG("datexSubscribe_CancelReason_cd: %d", pSubscripiton->datexSubscribe_Type.choice.datexSubscribe_CancelReason_cd);
  2127. break;
  2128. default:
  2129. LDEBUG("Unknown datexSubscribe_Type %d", pSubscripiton->datexSubscribe_Type.present);
  2130. break;
  2131. }/* end switch */
  2132. }
  2133. #endif
  2134. if (FVmsObj->FIsOldProtocol)
  2135. {
  2136. int s_arcs[6];
  2137. int *arcs = s_arcs;
  2138. s_arcs[0] = 1;
  2139. s_arcs[1] = 0;
  2140. s_arcs[2] = 14827;
  2141. s_arcs[3] = 2;
  2142. s_arcs[4] = 0;
  2143. s_arcs[5] = Arg.obj;
  2144. int count = OBJECT_IDENTIFIER_set_arcs(&pData->datexSubscribe_Pdu.endApplication_Message_id, arcs, sizeof(*arcs), 6);
  2145. }
  2146. else
  2147. {
  2148. ASN1C_SetObjId(&pData->datexSubscribe_Pdu.endApplication_Message_id, Arg.obj);
  2149. }
  2150. switch(Arg.obj)
  2151. {
  2152. case OBJ_GeneralStatus: /* 기본상태정보 */
  2153. {
  2154. LINFO(" *** OBJ_GeneralStatus 전송");
  2155. GeneralStatusMessage_t *pGS;
  2156. MessageBodyGeneralStatusMessage_t Msg; /* Type to encode */
  2157. memset((void *)&Msg, 0x00, sizeof(MessageBodyGeneralStatusMessage_t));
  2158. for ( i = 0; i < count; i++)
  2159. {
  2160. if ((pGS = (GeneralStatusMessage_t *)ASN1C_Alloc(sizeof(GeneralStatusMessage_t))) == NULL)
  2161. {
  2162. result = -10;
  2163. break;
  2164. }
  2165. if(ASN_SEQUENCE_ADD(&Msg, pGS)!= ASN_ERR_NONE)
  2166. {
  2167. result = -11;
  2168. break;
  2169. }
  2170. }/* end for */
  2171. /* Encode */
  2172. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  2173. eLen = -1;
  2174. er = der_encode_to_buffer(&asn_DEF_MessageBodyGeneralStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
  2175. if (er.encoded == -1)
  2176. {
  2177. /*
  2178. * Failed to encode the data.
  2179. */
  2180. SERROR("Cannot encode %s", er.failed_type->name);
  2181. result = SYS_ERR_MESSAGE_ENCODE;
  2182. }
  2183. else
  2184. {
  2185. /* Return the number of bytes */
  2186. eLen = er.encoded;
  2187. pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
  2188. if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
  2189. {
  2190. result = -12;
  2191. break;
  2192. }
  2193. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
  2194. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  2195. {
  2196. result = -13;
  2197. break;
  2198. }
  2199. result = SYS_ERR_NONE;
  2200. }/* end if */
  2201. ASN1C_FreeC(&asn_DEF_MessageBodyGeneralStatusMessage, &Msg);
  2202. }
  2203. break;
  2204. #if 1
  2205. case OBJ_PowerStatus: /* 전원상태정보 */
  2206. {
  2207. LINFO(" *** OBJ_PowerStatus 전송");
  2208. PowerStatusMessage_t *pMS;
  2209. MessageBodyPowerStatusMessage_t Msg;
  2210. memset((void *)&Msg, 0x00, sizeof(MessageBodyPowerStatusMessage_t));
  2211. for ( i = 0; i < count; i++)
  2212. {
  2213. if ((pMS = (PowerStatusMessage_t *)ASN1C_Alloc(sizeof(PowerStatusMessage_t))) == NULL)
  2214. {
  2215. result = -30;
  2216. break;
  2217. }
  2218. if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
  2219. {
  2220. result = -31;
  2221. break;
  2222. }
  2223. }/* end for */
  2224. /* Encode */
  2225. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  2226. eLen = -1;
  2227. er = der_encode_to_buffer(&asn_DEF_MessageBodyPowerStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
  2228. if (er.encoded == -1)
  2229. {
  2230. /*
  2231. * Failed to encode the data.
  2232. */
  2233. SERROR("Cannot encode %s", er.failed_type->name);
  2234. result = SYS_ERR_MESSAGE_ENCODE;
  2235. }
  2236. else
  2237. {
  2238. /* Return the number of bytes */
  2239. eLen = er.encoded;
  2240. pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
  2241. if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
  2242. {
  2243. result = -32;
  2244. break;
  2245. }
  2246. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
  2247. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  2248. {
  2249. result = -33;
  2250. break;
  2251. }
  2252. result = SYS_ERR_NONE;
  2253. }/* end if */
  2254. ASN1C_FreeC(&asn_DEF_MessageBodyPowerStatusMessage, &Msg);
  2255. }
  2256. break;
  2257. #endif
  2258. case OBJ_ModuleStatus: /* 모듈상태정보 */
  2259. {
  2260. LINFO(" *** OBJ_ModuleStatus 전송");
  2261. ModuleStatusMessage_t *pMS;
  2262. MessageBodyModuleStatusMessage_t Msg; /* Type to encode */
  2263. memset((void *)&Msg, 0x00, sizeof(MessageBodyModuleStatusMessage_t));
  2264. for ( i = 0; i < count; i++)
  2265. {
  2266. if ((pMS = (ModuleStatusMessage_t *)ASN1C_Alloc(sizeof(ModuleStatusMessage_t))) == NULL)
  2267. {
  2268. result = -30;
  2269. break;
  2270. }
  2271. if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
  2272. {
  2273. result = -31;
  2274. break;
  2275. }
  2276. }/* end for */
  2277. /* Encode */
  2278. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  2279. eLen = -1;
  2280. er = der_encode_to_buffer(&asn_DEF_MessageBodyModuleStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
  2281. if (er.encoded == -1)
  2282. {
  2283. /*
  2284. * Failed to encode the data.
  2285. */
  2286. SERROR("Cannot encode %s", er.failed_type->name);
  2287. result = SYS_ERR_MESSAGE_ENCODE;
  2288. }
  2289. else
  2290. {
  2291. /* Return the number of bytes */
  2292. eLen = er.encoded;
  2293. pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
  2294. if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
  2295. {
  2296. result = -32;
  2297. break;
  2298. }
  2299. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
  2300. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  2301. {
  2302. result = -33;
  2303. break;
  2304. }
  2305. result = SYS_ERR_NONE;
  2306. }/* end if */
  2307. ASN1C_FreeC(&asn_DEF_MessageBodyModuleStatusMessage, &Msg);
  2308. }
  2309. break;
  2310. case OBJ_DotStatus: /* 도트상태정보 */
  2311. {
  2312. LINFO(" *** OBJ_DotStatus 전송");
  2313. DotStatusMessage_t *pMS;
  2314. MessageBodyDotStatusMessage_t Msg; /* Type to encode */
  2315. memset((void *)&Msg, 0x00, sizeof(MessageBodyDotStatusMessage_t));
  2316. for ( i = 0; i < count; i++)
  2317. {
  2318. if ((pMS = (DotStatusMessage_t *)ASN1C_Alloc(sizeof(DotStatusMessage_t))) == NULL)
  2319. {
  2320. result = -30;
  2321. break;
  2322. }
  2323. if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
  2324. {
  2325. result = -31;
  2326. break;
  2327. }
  2328. }/* end for */
  2329. /* Encode */
  2330. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  2331. eLen = -1;
  2332. er = der_encode_to_buffer(&asn_DEF_MessageBodyDotStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
  2333. if (er.encoded == -1)
  2334. {
  2335. /*
  2336. * Failed to encode the data.
  2337. */
  2338. SERROR("Cannot encode %s", er.failed_type->name);
  2339. result = SYS_ERR_MESSAGE_ENCODE;
  2340. }
  2341. else
  2342. {
  2343. /* Return the number of bytes */
  2344. eLen = er.encoded;
  2345. pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
  2346. if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
  2347. {
  2348. result = -32;
  2349. break;
  2350. }
  2351. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
  2352. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  2353. {
  2354. result = -33;
  2355. break;
  2356. }
  2357. result = SYS_ERR_NONE;
  2358. }/* end if */
  2359. ASN1C_FreeC(&asn_DEF_MessageBodyDotStatusMessage, &Msg);
  2360. }
  2361. break;
  2362. case OBJ_ScreenStatus: /* 표출상태정보 */
  2363. {
  2364. LINFO(" *** OBJ_ScreenStatus 전송");
  2365. ScreenStatusMessage_t *pMS;
  2366. MessageBodyScreenStatusMessage_t Msg; /* Type to encode */
  2367. memset((void *)&Msg, 0x00, sizeof(MessageBodyScreenStatusMessage_t));
  2368. for ( i = 0; i < count; i++)
  2369. {
  2370. if ((pMS = (ScreenStatusMessage_t *)ASN1C_Alloc(sizeof(ScreenStatusMessage_t))) == NULL)
  2371. {
  2372. result = -30;
  2373. break;
  2374. }
  2375. if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
  2376. {
  2377. result = -31;
  2378. break;
  2379. }
  2380. }/* end for */
  2381. /* Encode */
  2382. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  2383. eLen = -1;
  2384. er = der_encode_to_buffer(&asn_DEF_MessageBodyScreenStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
  2385. if (er.encoded == -1)
  2386. {
  2387. /*
  2388. * Failed to encode the data.
  2389. */
  2390. SERROR("Cannot encode %s", er.failed_type->name);
  2391. result = SYS_ERR_MESSAGE_ENCODE;
  2392. }
  2393. else
  2394. {
  2395. /* Return the number of bytes */
  2396. eLen = er.encoded;
  2397. pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
  2398. if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
  2399. {
  2400. result = -32;
  2401. break;
  2402. }
  2403. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
  2404. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  2405. {
  2406. result = -33;
  2407. break;
  2408. }
  2409. result = SYS_ERR_NONE;
  2410. }/* end if */
  2411. ASN1C_FreeC(&asn_DEF_MessageBodyScreenStatusMessage, &Msg);
  2412. }
  2413. break;
  2414. case OBJ_DisplayingData: /* 표출정보 */
  2415. {
  2416. LINFO(" *** OBJ_DisplayingData 전송");
  2417. DisplayingDataMessage_t *pMS;
  2418. MessageBodyDisplayingDataMessage_t Msg; /* Type to encode */
  2419. memset((void *)&Msg, 0x00, sizeof(MessageBodyDisplayingDataMessage_t));
  2420. for ( i = 0; i < count; i++)
  2421. {
  2422. if ((pMS = (DisplayingDataMessage_t *)ASN1C_Alloc(sizeof(DisplayingDataMessage_t))) == NULL)
  2423. {
  2424. result = -30;
  2425. break;
  2426. }
  2427. if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
  2428. {
  2429. result = -31;
  2430. break;
  2431. }
  2432. }/* end for */
  2433. /* Encode */
  2434. memset(MsgBuf, 0x00, sizeof(MsgBuf));
  2435. eLen = -1;
  2436. er = der_encode_to_buffer(&asn_DEF_MessageBodyDisplayingDataMessage, &Msg, MsgBuf, sizeof(MsgBuf));
  2437. if (er.encoded == -1)
  2438. {
  2439. /*
  2440. * Failed to encode the data.
  2441. */
  2442. SERROR("Cannot encode %s", er.failed_type->name);
  2443. result = SYS_ERR_MESSAGE_ENCODE;
  2444. }
  2445. else
  2446. {
  2447. /* Return the number of bytes */
  2448. eLen = er.encoded;
  2449. pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
  2450. if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
  2451. {
  2452. result = -32;
  2453. break;
  2454. }
  2455. if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
  2456. (char *)MsgBuf, eLen) != ASN_ERR_NONE)
  2457. {
  2458. result = -33;
  2459. break;
  2460. }
  2461. result = SYS_ERR_NONE;
  2462. }/* end if */
  2463. ASN1C_FreeC(&asn_DEF_MessageBodyDisplayingDataMessage, &Msg);
  2464. }
  2465. break;
  2466. default:
  2467. result = SYS_ERR_UNKNOWN_MESSAGE_ID;
  2468. break;
  2469. }
  2470. return result;
  2471. }
  2472. //---------------------------------------------------------------------------
  2473. /*
  2474. * 클라이언트 처리 루틴
  2475. * arguments
  2476. * void
  2477. * return
  2478. * void
  2479. */
  2480. void __fastcall TVMSCThread::ClientStateMachine(void)
  2481. {
  2482. FErrLine = 13;
  2483. int result;
  2484. bool bRequest;
  2485. result = SYS_ERR_NONE;
  2486. //timeout check
  2487. if (m_RxLen && (CommUtil_TimeDiff(m_rTimer) > m_Client.TimeOut))
  2488. {
  2489. m_RxLen = 0;
  2490. ProcErrorState(TRUE, SYS_ERR_RECEIVE_TIMEOUT);
  2491. return;
  2492. }
  2493. #if 0
  2494. {
  2495. switch (m_Client.State)
  2496. {
  2497. case CT_IDLE: LDETAIL("IDLE"); break;
  2498. case CT_LOGIN: LDETAIL("LOGIN"); break;
  2499. case CT_SECTION: LDETAIL("SECTION"); break;
  2500. case CT_CLOSE: LDETAIL("CLOSE"); break;
  2501. case CT_LOCAL_COMMAND: LDETAIL("LOCAL_COMMAND"); break;
  2502. case CT_CLIENT_COMMAND: LDETAIL("CLIENT_COMMAND"); break;
  2503. case CT_LOGIN_WAIT: LDETAIL("LOGIN_WAIT"); break;
  2504. case CT_FRED_WAIT: LDETAIL("FRED_WAIT"); break;
  2505. case CT_LOCAL_WAIT: LDETAIL("LOCAL_WAIT"); break;
  2506. case CT_CLIENT_WAIT: LDETAIL("CLIENT_WAIT"); break;
  2507. default: LDETAIL("Unknown State[%d]", m_Client.State); break;
  2508. }
  2509. }
  2510. #endif
  2511. switch(m_Client.State)
  2512. {
  2513. case CT_IDLE:
  2514. if (CommUtil_TimeDiff(m_Client.sTimer) > CLIENT_CONNECT_RETRY_TIME)
  2515. {
  2516. m_Client.sTimer = Now();
  2517. if (m_Client.iRetry <= 0)
  2518. {
  2519. m_Client.State = CT_CLOSE;
  2520. }
  2521. else
  2522. {
  2523. m_Client.State = CT_LOGIN;
  2524. }
  2525. }
  2526. break;
  2527. case CT_LOGIN:
  2528. #if 1
  2529. //TODO
  2530. //용인거는 로그인 요청을 수행하지 않도록 한다.
  2531. if ((result = ProcessCommand(AI_Login, NULL)) == SYS_ERR_NONE)
  2532. {
  2533. m_Client.State = CT_LOGIN_WAIT;
  2534. }
  2535. else
  2536. {
  2537. if (--m_Client.iRetry <= 0)
  2538. {
  2539. m_Client.State = CT_CLOSE;
  2540. }
  2541. }
  2542. #else
  2543. m_Client.State = CT_LOCAL_COMMAND;
  2544. #endif
  2545. break;
  2546. case CT_SECTION:
  2547. if (CommUtil_TimeDiff(m_WatchDogTimer) > FWatchDogTime)
  2548. {
  2549. LERROR("WatchDog Time over");
  2550. m_Client.State = CT_CLOSE;
  2551. break;
  2552. }
  2553. if (CommUtil_TimeDiff(m_rTimer) > (m_Client.MaxHeartbeatTime/3))
  2554. {
  2555. if ((result = ProcessCommand(AI_FrED, NULL)) == SYS_ERR_NONE)
  2556. {
  2557. m_Client.State = CT_FRED_WAIT;
  2558. }
  2559. else
  2560. {
  2561. if (--m_Client.iRetry <= 0)
  2562. {
  2563. m_Client.State = CT_CLOSE;
  2564. }
  2565. }
  2566. break;
  2567. }
  2568. if (CommUtil_TimeDiff(m_Client.sTimer) > m_Client.CycleTime)
  2569. {
  2570. m_Client.sTimer = Now();
  2571. m_Client.State = CT_LOCAL_COMMAND;
  2572. m_Registered.GeneralStatus = false;
  2573. m_Registered.ModuleStatus = false;
  2574. m_Registered.PowerStatus = false;
  2575. }
  2576. else
  2577. {
  2578. m_Client.State = CT_LOCAL_COMMAND;
  2579. }
  2580. break;
  2581. case CT_CLOSE:
  2582. Terminate();
  2583. break;
  2584. case CT_REGIST:
  2585. if (CommUtil_TimeDiff(m_Client.sTimer) > m_Client.CycleTime)
  2586. {
  2587. m_Client.sTimer = Now();
  2588. m_Client.State = CT_LOCAL_COMMAND;
  2589. m_SubRegistered.GeneralStatus = false;
  2590. m_SubRegistered.PowerStatus = false;
  2591. m_SubRegistered.ModuleStatus = false;
  2592. m_SubRegistered.DotStatus = false;
  2593. m_SubRegistered.ScreenStatus = false;
  2594. m_SubRegistered.DisplayingData = false;
  2595. }
  2596. else
  2597. {
  2598. m_Client.State = CT_REGIST_COMMAND;
  2599. }
  2600. break;
  2601. case CT_REGIST_COMMAND:
  2602. result = CheckRegistData(bRequest);
  2603. if (bRequest)
  2604. {
  2605. if (result == SYS_ERR_NONE)
  2606. {
  2607. m_Client.State = CT_REGIST_WAIT;
  2608. }
  2609. else
  2610. {
  2611. if (--m_Client.iRetry <= 0)
  2612. {
  2613. m_Client.State = CT_CLOSE;
  2614. }
  2615. }
  2616. }
  2617. else
  2618. {
  2619. m_Client.State = CT_LOCAL_COMMAND;
  2620. }
  2621. break;
  2622. case CT_REGIST_WAIT:
  2623. if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
  2624. {
  2625. if (m_Client.iRetry > 0)
  2626. {
  2627. m_Client.State = CT_IDLE;
  2628. }
  2629. else
  2630. {
  2631. m_Client.State = CT_CLOSE;
  2632. }
  2633. }
  2634. break;
  2635. case CT_LOCAL_COMMAND:
  2636. result = CheckLocalData(bRequest);
  2637. if (bRequest)
  2638. {
  2639. if (result == SYS_ERR_NONE)
  2640. {
  2641. m_Client.State = CT_LOCAL_WAIT;
  2642. }
  2643. else
  2644. {
  2645. if (--m_Client.iRetry <= 0)
  2646. {
  2647. m_Client.State = CT_CLOSE;
  2648. }
  2649. }
  2650. }
  2651. else
  2652. {
  2653. m_Client.State = CT_CLIENT_COMMAND;
  2654. }
  2655. break;
  2656. case CT_CLIENT_COMMAND:
  2657. result = CheckClientData(bRequest);
  2658. if (bRequest)
  2659. {
  2660. if (result == SYS_ERR_NONE)
  2661. {
  2662. m_Client.State = CT_CLIENT_WAIT;
  2663. }
  2664. else
  2665. {
  2666. if (--m_Client.iRetry <= 0)
  2667. {
  2668. m_Client.State = CT_CLOSE;
  2669. }
  2670. }
  2671. }
  2672. else
  2673. {
  2674. m_Client.State = CT_SECTION;
  2675. }
  2676. break;
  2677. case CT_LOGIN_WAIT:
  2678. if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
  2679. {
  2680. if (m_Client.iRetry > 0)
  2681. {
  2682. m_Client.State = CT_IDLE;
  2683. }
  2684. else
  2685. {
  2686. m_Client.State = CT_CLOSE;
  2687. }
  2688. }
  2689. break;
  2690. case CT_FRED_WAIT:
  2691. if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
  2692. {
  2693. if (m_Client.iRetry > 0)
  2694. {
  2695. m_Client.State = CT_SECTION;
  2696. }
  2697. else
  2698. {
  2699. m_Client.State = CT_CLOSE;
  2700. }
  2701. }
  2702. break;
  2703. case CT_LOCAL_WAIT:
  2704. if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
  2705. {
  2706. if (m_Client.iRetry > 0){
  2707. m_Client.State = CT_LOCAL_COMMAND;
  2708. }else{
  2709. m_Client.State = CT_CLOSE;
  2710. }
  2711. }
  2712. break;
  2713. case CT_CLIENT_WAIT:
  2714. if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
  2715. {
  2716. if (m_Client.iRetry > 0)
  2717. {
  2718. m_Client.State = CT_CLIENT_COMMAND;
  2719. }
  2720. else
  2721. {
  2722. m_Client.State = CT_CLOSE;
  2723. }
  2724. }
  2725. break;
  2726. default:
  2727. m_Client.State = CT_IDLE;
  2728. break;
  2729. }/* end switch */
  2730. if (result)
  2731. {
  2732. ProcErrorState(FALSE, result);
  2733. }
  2734. }
  2735. //---------------------------------------------------------------------------
  2736. /*
  2737. * 에러 처리
  2738. * arguments
  2739. * void
  2740. * return
  2741. * void
  2742. */
  2743. void __fastcall TVMSCThread::ProcErrorState(bool flag, int result)
  2744. {
  2745. FErrLine = 14;
  2746. #if 1
  2747. {
  2748. AnsiString strLog;
  2749. switch(result)
  2750. {
  2751. case SYS_ERR_NONE: strLog.sprintf("no error [%d]", result); break;
  2752. case SYS_ERR_OTHER: strLog.sprintf("other [%d]", result); break;
  2753. case SYS_ERR_INTERNAL: strLog.sprintf("internal system error(kernel..) [%d]", result); break;
  2754. case SYS_ERR_MEMORY: strLog.sprintf("memory overflow [%d]", result); break;
  2755. case SYS_ERR_DATABASE: strLog.sprintf("system database error [%d]", result); break;
  2756. case SYS_ERR_RESET: strLog.sprintf("system reset [%d]", result); break;
  2757. case SYS_ERR_START: strLog.sprintf("system start [%d]", result); break;
  2758. case SYS_ERR_OFFLINE: strLog.sprintf("offline [%d]", result); break;
  2759. case SYS_ERR_NO_RES: strLog.sprintf("no response [%d]", result); break;
  2760. case SYS_ERR_ACCESS: strLog.sprintf("access denied [%d]", result); break;
  2761. case SYS_ERR_INVALID: strLog.sprintf("invalid data type [%d]", result); break;
  2762. case SYS_ERR_UNKNOWN: strLog.sprintf("unknown data type [%d]", result); break;
  2763. case SYS_ERR_WRITE_LENGTH: strLog.sprintf("write length [%d]", result); break;
  2764. case SYS_ERR_PACKET_SIZE_LARGE: strLog.sprintf("packet size large [%d]", result); break;
  2765. case SYS_ERR_INVALID_TAG: strLog.sprintf("invalid tag [%d]", result); break;
  2766. case SYS_ERR_CRC: strLog.sprintf("crc [%d]", result); break;
  2767. case SYS_ERR_LENGTH: strLog.sprintf("length [%d]", result); break;
  2768. case SYS_ERR_ADDRESS: strLog.sprintf("address [%d]", result); break;
  2769. case SYS_ERR_HEADER_OPTIONS: strLog.sprintf("header options [%d]", result); break;
  2770. case SYS_ERR_UNKNOWN_COMMAND: strLog.sprintf("unknown command [%d]", result); break;
  2771. case SYS_ERR_RECEIVE_TIMEOUT: strLog.sprintf("receive timeout [%d]", result); break;
  2772. case SYS_ERR_DATA_SIZE: strLog.sprintf("data size [%d]", result); break;
  2773. case SYS_ERR_MISMATCH: strLog.sprintf("mismatch command [%d]", result); break;
  2774. case SYS_ERR_INVALID_PARA: strLog.sprintf("invalid parameter [%d]", result); break;
  2775. case SYS_ERR_MOVEMENT: strLog.sprintf("movement [%d]", result); break;
  2776. case SYS_ERR_UNAUTHORIZED: strLog.sprintf("unauthorized [%d]", result); break;
  2777. case SYS_ERR_DATEXDATAPACKET_DECODE: strLog.sprintf("DatexDataPacket decode [%d]", result); break;
  2778. case SYS_ERR_INVALID_VERSION_NUMBER: strLog.sprintf("invalid version number [%d]", result); break;
  2779. case SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE: strLog.sprintf("C2CAuthenticatedMessage decode [%d]", result); break;
  2780. case SYS_ERR_C2CAUTHENTICATEDMESSAGE: strLog.sprintf("C2CAuthenticatedMessage [%d]", result); break;
  2781. case SYS_ERR_NOT_LOGIN: strLog.sprintf("Not login [%d]", result); break;
  2782. case SYS_ERR_ACCEPT_TYPE: strLog.sprintf("Accept Type [%d]", result); break;
  2783. case SYS_ERR_DATEXPUBLISH_FORMAT: strLog.sprintf("datexPublish Format [%d]", result); break;
  2784. case SYS_ERR_DATEXPUBLISH_TYPE: strLog.sprintf("datexPublish Type [%d]", result); break;
  2785. case SYS_ERR_UNKNOWN_MESSAGE_ID: strLog.sprintf("unknown Message id [%d]", result); break;
  2786. case SYS_ERR_MESSAGE_DECODE: strLog.sprintf("Message decode [%d]", result); break;
  2787. case SYS_ERR_DATEXDATAPACKET_ENCODE: strLog.sprintf("DatexDataPacket encode [%d]", result); break;
  2788. case SYS_ERR_C2CAUTHENTICATEDMESSAGE_ENCODE: strLog.sprintf("C2CAuthenticatedMessage encode [%d]", result); break;
  2789. case SYS_ERR_MESSAGE_ENCODE: strLog.sprintf("Message encode [%d]", result); break;
  2790. case SYS_ERR_PUBLICATIONDATA_ENCODE: strLog.sprintf("PublicationData encode [%d]", result); break;
  2791. case SYS_ERR_MISMATCH_DESTINATION_TEXT: strLog.sprintf("mismatch Destination text [%d]", result); break;
  2792. case SYS_ERR_DATEXLOGIN_VALUE: strLog.sprintf("Login Value [%d]", result); break;
  2793. case SYS_ERR_SUBSCRIPTION_TYPE: strLog.sprintf("SubscriptionType Type [%d]", result); break;
  2794. case SYS_ERR_SUBSCRIPTION_STATUS_VALUE: strLog.sprintf("SubscriptionType Status Value [%d]", result); break;
  2795. case SYS_ERR_SUBSCRIPTION_MODE_VALUE: strLog.sprintf("SubscriptionType Mode Value [%d]", result); break;
  2796. case SYS_ERR_SUBSCRIPTION_FORMAT_VALUE: strLog.sprintf("SubscriptionType Format Value [%d]", result); break;
  2797. case SYS_ERR_HANGUL_CONVERSION: strLog.sprintf("Hangul Conversion [%d]", result); break;
  2798. case SYS_ERR_INVALID_TAGANDLEN: strLog.sprintf("invalid Tag And Len [%d]", result); break;
  2799. case SYS_ERR_PUBLICATIONDATA_DECODE: strLog.sprintf("PublicationData decode [%d]", result); break;
  2800. default: strLog.sprintf("Unknown Error [%d]", result); break;
  2801. }
  2802. if (flag){
  2803. LERROR("Recv Error %s", strLog.c_str());
  2804. }else{
  2805. LERROR("Send Error %s", strLog.c_str());
  2806. }
  2807. }
  2808. #endif
  2809. }
  2810. //---------------------------------------------------------------------------
  2811. /*
  2812. * 상태 변경
  2813. * arguments
  2814. * void
  2815. * return
  2816. * void
  2817. */
  2818. void __fastcall TVMSCThread::SetClientState(int ACase)
  2819. {
  2820. FErrLine = 15;
  2821. switch(m_Client.State)
  2822. {
  2823. case CT_LOGIN_WAIT:
  2824. if (ACase == SET_CLIENT_SUCC)
  2825. {
  2826. if (FVmsObj->FIsOldProtocol)
  2827. {
  2828. //정보요청을 등록해야함(기존 VMS)
  2829. m_Client.State = CT_SECTION;//CT_REGIST; //TODO
  2830. m_Client.sTimer = Now() - ITSCOMM_SECTIME(m_Client.CycleTime);
  2831. }
  2832. else
  2833. {
  2834. m_Client.State = CT_SECTION;
  2835. m_Client.sTimer = Now() - ITSCOMM_SECTIME(m_Client.CycleTime);
  2836. }
  2837. }
  2838. else
  2839. if (ACase == SET_CLIENT_FAIL)
  2840. {
  2841. m_Client.State = CT_IDLE;
  2842. }
  2843. break;
  2844. case CT_FRED_WAIT:
  2845. m_Client.State = CT_SECTION;
  2846. break;
  2847. case CT_LOCAL_WAIT:
  2848. if (ACase == SET_CLIENT_SUCC)
  2849. {
  2850. m_Client.State = CT_LOCAL_COMMAND;//CT_CLIENT_COMMAND;
  2851. }
  2852. else
  2853. if (ACase == SET_CLIENT_FAIL)
  2854. {
  2855. m_Client.State = CT_LOCAL_COMMAND;
  2856. }
  2857. break;
  2858. case CT_CLIENT_WAIT:
  2859. if (ACase == SET_CLIENT_SUCC)
  2860. {
  2861. m_Client.State = CT_SECTION;
  2862. }
  2863. else
  2864. if (ACase == SET_CLIENT_FAIL)
  2865. {
  2866. m_Client.State = CT_CLIENT_COMMAND;
  2867. }
  2868. break;
  2869. case CT_REGIST_WAIT:
  2870. if (ACase == SET_CLIENT_SUCC)
  2871. {
  2872. m_Client.State = CT_REGIST_COMMAND;
  2873. }
  2874. else
  2875. if (ACase == SET_CLIENT_FAIL)
  2876. {
  2877. m_Client.State = CT_CLIENT_COMMAND;
  2878. }
  2879. break;
  2880. default:
  2881. break;
  2882. }
  2883. }
  2884. //---------------------------------------------------------------------------
  2885. /*
  2886. *
  2887. * arguments
  2888. * void
  2889. * return
  2890. * void
  2891. */
  2892. int __fastcall TVMSCThread::CheckLocalData(bool &ARequest)
  2893. {
  2894. FErrLine = 16;
  2895. int result;
  2896. CLIENT_ARGUMENT arg;
  2897. result = SYS_ERR_NONE;
  2898. ARequest = false;
  2899. if (FVmsObj->FIsStatusAuto) return result;
  2900. memset(&arg, 0x00, sizeof(arg));
  2901. memset(&m_Ctl, 0x00, sizeof(m_Ctl));
  2902. if (m_Registered.GeneralStatus == false)
  2903. {
  2904. LINFO("**** GeneralStatus 요청");
  2905. arg.Subscription.nbr = NBR_GENERAL_STATUS;
  2906. arg.Subscription.mode = SubscriptionMode_PR_single;
  2907. arg.Subscription.delay = 60;
  2908. arg.Subscription.obj = OBJ_GeneralStatus;
  2909. m_Ctl.obj = OBJ_GeneralStatus;
  2910. m_Ctl.DataPacketNumber = m_DataPacketNumber;
  2911. if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
  2912. {
  2913. //
  2914. }
  2915. ARequest = true;
  2916. return result;
  2917. }
  2918. if (m_Registered.ModuleStatus == false)
  2919. {
  2920. LINFO("**** ModuleStatus 요청");
  2921. arg.Subscription.nbr = NBR_MODULE_STATUS;
  2922. arg.Subscription.mode = SubscriptionMode_PR_single;
  2923. arg.Subscription.delay = 60;
  2924. arg.Subscription.obj = OBJ_ModuleStatus;
  2925. m_Ctl.obj = OBJ_ModuleStatus;
  2926. m_Ctl.DataPacketNumber = m_DataPacketNumber;
  2927. if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
  2928. {
  2929. //
  2930. }
  2931. ARequest = true;
  2932. return result;
  2933. }
  2934. if (m_Registered.PowerStatus == false)
  2935. {
  2936. LINFO("**** PowerStatus 요청");
  2937. arg.Subscription.nbr = NBR_POWER_STATUS;
  2938. arg.Subscription.mode = SubscriptionMode_PR_single;
  2939. arg.Subscription.delay = 60;
  2940. arg.Subscription.obj = OBJ_PowerStatus;
  2941. m_Ctl.obj = OBJ_PowerStatus;
  2942. m_Ctl.DataPacketNumber = m_DataPacketNumber;
  2943. if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
  2944. {
  2945. //
  2946. }
  2947. ARequest = true;
  2948. return result;
  2949. }
  2950. #if 0
  2951. if (m_Registered.RealTimeDisplayMessage == false)
  2952. {
  2953. LINFO("**** RealTimeDisplayMessage 요청");
  2954. arg.Subscription.nbr = NBR_REALTIME_DISPLAY;
  2955. arg.Subscription.mode = SubscriptionMode_PR_single;
  2956. arg.Subscription.delay = 60;
  2957. arg.Subscription.obj = OBJ_DisplayingData;
  2958. m_Ctl.obj = OBJ_DisplayingData;
  2959. m_Ctl.DataPacketNumber = m_DataPacketNumber;
  2960. if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
  2961. {
  2962. //
  2963. }
  2964. ARequest = true;
  2965. return result;
  2966. }
  2967. if (m_Registered.ScreenDataStatusMessage == false)
  2968. {
  2969. LINFO("**** ScreenDataStatusMessage 요청");
  2970. arg.Subscription.nbr = NBR_SCREEN_STATUS;
  2971. arg.Subscription.mode = SubscriptionMode_PR_single;
  2972. arg.Subscription.delay = 60;
  2973. arg.Subscription.obj = OBJ_ScreenStatus;
  2974. m_Ctl.obj = OBJ_ScreenStatus;
  2975. m_Ctl.DataPacketNumber = m_DataPacketNumber;
  2976. if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
  2977. {
  2978. //
  2979. }
  2980. ARequest = true;
  2981. return result;
  2982. }
  2983. #endif
  2984. return result;
  2985. }
  2986. //---------------------------------------------------------------------------
  2987. int __fastcall TVMSCThread::CheckRegistData(bool &ARequest)
  2988. {
  2989. FErrLine = 16;
  2990. int result;
  2991. CLIENT_ARGUMENT arg;
  2992. result = SYS_ERR_NONE;
  2993. ARequest = false;
  2994. memset(&arg, 0x00, sizeof(arg));
  2995. memset(&m_Ctl, 0x00, sizeof(m_Ctl));
  2996. //TODO
  2997. return result;
  2998. if (m_SubRegistered.GeneralStatus == false)
  2999. {
  3000. m_Ctl.obj = OBJ_GeneralStatus;
  3001. m_Ctl.DataPacketNumber = 15;
  3002. SendSubRegister(OBJ_GeneralStatus);
  3003. ARequest = true;
  3004. return result;
  3005. }
  3006. if (m_SubRegistered.PowerStatus == false)
  3007. {
  3008. m_Ctl.obj = OBJ_PowerStatus;
  3009. m_Ctl.DataPacketNumber = 16;
  3010. SendSubRegister(OBJ_PowerStatus);
  3011. ARequest = true;
  3012. return result;
  3013. }
  3014. if (m_SubRegistered.ModuleStatus == false)
  3015. {
  3016. m_Ctl.obj = OBJ_ModuleStatus;
  3017. m_Ctl.DataPacketNumber = 17;
  3018. SendSubRegister(OBJ_ModuleStatus);
  3019. ARequest = true;
  3020. return result;
  3021. }
  3022. if (m_SubRegistered.DotStatus == false)
  3023. {
  3024. m_Ctl.obj = OBJ_DotStatus;
  3025. m_Ctl.DataPacketNumber = 18;
  3026. SendSubRegister(OBJ_DotStatus);
  3027. ARequest = true;
  3028. return result;
  3029. }
  3030. if (m_SubRegistered.ScreenStatus == false)
  3031. {
  3032. m_Ctl.obj = OBJ_ScreenStatus;
  3033. m_Ctl.DataPacketNumber = 19;
  3034. SendSubRegister(OBJ_ScreenStatus);
  3035. ARequest = true;
  3036. return result;
  3037. }
  3038. if (m_SubRegistered.DisplayingData == false)
  3039. {
  3040. m_Ctl.obj = OBJ_DisplayingData;
  3041. m_Ctl.DataPacketNumber = 20;
  3042. SendSubRegister(OBJ_DisplayingData);
  3043. ARequest = true;
  3044. return result;
  3045. }
  3046. return result;
  3047. }
  3048. //---------------------------------------------------------------------------
  3049. /*
  3050. *
  3051. * arguments
  3052. * void
  3053. * return
  3054. * void
  3055. */
  3056. int __fastcall TVMSCThread::CheckClientData(bool &ARequest)
  3057. {
  3058. int result;
  3059. result = SYS_ERR_NONE;
  3060. ARequest = false;
  3061. return result;
  3062. }
  3063. //---------------------------------------------------------------------------
  3064. int TVMSCThread::LogData(char *ASndRcv, BYTE *AData, int ALen)
  3065. {
  3066. int result = -1;
  3067. int nStep = 0;
  3068. if (!FLogFile) return result;
  3069. if (!FLogFile->FLogCfg.Data) return result;
  3070. try
  3071. {
  3072. FLogFile->LogData(ASndRcv, AData, ALen);
  3073. if (FVmsObj->FDispLog)
  3074. {
  3075. IPC_LOG_MESSAGE *pLog = &FLogMsg;//new IPC_LOG_MESSAGE;
  3076. if (pLog)
  3077. {
  3078. pLog->Kind = eLOG_DATA;
  3079. pLog->Flag = ASndRcv[0] == 'S' ? 1 : 2;
  3080. pLog->Len = ALen > MAX_LOG_BUFFER ? MAX_LOG_BUFFER-1 : ALen;
  3081. pLog->Tm = Now();
  3082. pLog->Type = 'C';
  3083. memset(pLog->Msg, 0x00, sizeof(pLog->Msg));
  3084. memcpy(pLog->Msg, (char*)AData, pLog->Len);
  3085. pLog->Obj = (void*)FVmsObj;
  3086. DWORD dwResult = 0;
  3087. LRESULT lResult = SEND_LOGMSG(FrmVmsLog->Handle, WM_LOG_DISPLAY, (WPARAM)pLog, NULL, SMTO_NORMAL, 2000, dwResult);
  3088. if (lResult == 0)
  3089. {
  3090. dwResult = GetLastError();
  3091. if (dwResult == ERROR_TIMEOUT)
  3092. {
  3093. }
  3094. //SAFE_DELETE(pLog);
  3095. }
  3096. }
  3097. }
  3098. }
  3099. catch(Exception &e)
  3100. {
  3101. }
  3102. return result;
  3103. }
  3104. //---------------------------------------------------------------------------
  3105. int TVMSCThread::SysLogWrite(int ALogKind, char *AFmt, ...)
  3106. {
  3107. va_list ap;
  3108. int cnt = 0;
  3109. char szFmtData[MAX_LOG_BUFFER];
  3110. AnsiString sLogKind;
  3111. int nStep = 0;
  3112. if (!FLogFile) return cnt;
  3113. bool bLog = false;
  3114. switch(ALogKind)
  3115. {
  3116. case eLOG_INFO : bLog = FLogFile->FLogCfg.Info; sLogKind = " [INF] "; break;
  3117. case eLOG_DATA : bLog = FLogFile->FLogCfg.Data; sLogKind = " [DAT] "; break;
  3118. case eLOG_ERROR : bLog = FLogFile->FLogCfg.Error; sLogKind = " [ERR] "; break;
  3119. case eLOG_WARNING: bLog = FLogFile->FLogCfg.Warning; sLogKind = " [WAN] "; break;
  3120. case eLOG_DEBUG : bLog = FLogFile->FLogCfg.Debug; sLogKind = " [DBG] "; break;
  3121. case eLOG_DETAIL : bLog = FLogFile->FLogCfg.Detail; sLogKind = " [DET] "; break;
  3122. }
  3123. if (!bLog) return -1;
  3124. try
  3125. {
  3126. va_start(ap, AFmt);
  3127. cnt = vsprintf(szFmtData, AFmt, ap);
  3128. va_end(ap);
  3129. FLogFile->LogWrite(NULL, ALogKind, szFmtData);
  3130. IPC_LOG_MESSAGE *pLog = &FLogMsg;//new IPC_LOG_MESSAGE;
  3131. if (pLog)
  3132. {
  3133. pLog->Kind = ALogKind;
  3134. pLog->Flag = 0;
  3135. pLog->Len = strlen(szFmtData);
  3136. pLog->Tm = Now();
  3137. pLog->Type = 'C';
  3138. memset(pLog->Msg, 0x00, sizeof(pLog->Msg));
  3139. sprintf(pLog->Msg, "%s-CLI %s, %s", sLogKind.c_str(), VMSID.c_str(), szFmtData);
  3140. pLog->Obj = (void*)FVmsObj;
  3141. DWORD dwResult = 0;
  3142. LRESULT lResult = SEND_LOGMSG(FrmSysLog->Handle, WM_LOG_DISPLAY, pLog, NULL, SMTO_NORMAL, 2000, dwResult);
  3143. if (lResult == 0)
  3144. {
  3145. dwResult = GetLastError();
  3146. if (dwResult == ERROR_TIMEOUT)
  3147. {
  3148. }
  3149. //SAFE_DELETE(pLog);
  3150. }
  3151. }
  3152. if (FVmsObj->FDispLog)
  3153. {
  3154. IPC_LOG_MESSAGE *pLog2 = &FLogMsg;//new IPC_LOG_MESSAGE;
  3155. if (pLog2)
  3156. {
  3157. pLog2->Kind = ALogKind;
  3158. pLog2->Flag = 0;
  3159. pLog2->Len = strlen(szFmtData);
  3160. pLog2->Tm = Now();
  3161. pLog2->Type = 'C';
  3162. memset(pLog2->Msg, 0x00, sizeof(pLog2->Msg));
  3163. sprintf(pLog2->Msg, "%s-CLI %s, %s", sLogKind.c_str(), VMSID.c_str(), szFmtData);
  3164. pLog2->Obj = (void*)FVmsObj;
  3165. DWORD dwResult = 0;
  3166. LRESULT lResult = SEND_LOGMSG(FrmVmsLog->Handle, WM_LOG_DISPLAY, pLog2, NULL, SMTO_NORMAL, 2000, dwResult);
  3167. if (lResult == 0)
  3168. {
  3169. dwResult = GetLastError();
  3170. if (dwResult == ERROR_TIMEOUT)
  3171. {
  3172. }
  3173. //SAFE_DELETE(pLog2);
  3174. }
  3175. }
  3176. }
  3177. }
  3178. catch(Exception &e)
  3179. {
  3180. }
  3181. return cnt;
  3182. }
  3183. //---------------------------------------------------------------------------
  3184. int TVMSCThread::LogWrite(int ALogKind, char *AFmt, ...)
  3185. {
  3186. va_list ap;
  3187. int cnt = 0;
  3188. char szFmtData[MAX_LOG_BUFFER];
  3189. AnsiString sLogKind;
  3190. int nStep = 0;
  3191. if (!FLogFile) return cnt;
  3192. bool bLog = false;
  3193. switch(ALogKind)
  3194. {
  3195. case eLOG_INFO : bLog = FLogFile->FLogCfg.Info; sLogKind = " [INF] "; break;
  3196. case eLOG_DATA : bLog = FLogFile->FLogCfg.Data; sLogKind = " [DAT] "; break;
  3197. case eLOG_ERROR : bLog = FLogFile->FLogCfg.Error; sLogKind = " [ERR] "; break;
  3198. case eLOG_WARNING: bLog = FLogFile->FLogCfg.Warning; sLogKind = " [WAN] "; break;
  3199. case eLOG_DEBUG : bLog = FLogFile->FLogCfg.Debug; sLogKind = " [DBG] "; break;
  3200. case eLOG_DETAIL : bLog = FLogFile->FLogCfg.Detail; sLogKind = " [DET] "; break;
  3201. }
  3202. if (!bLog) return -1;
  3203. try
  3204. {
  3205. va_start(ap, AFmt);
  3206. cnt = vsprintf(szFmtData, AFmt, ap);
  3207. va_end(ap);
  3208. FLogFile->LogWrite(NULL, ALogKind, szFmtData);
  3209. if (FVmsObj->FDispLog)
  3210. {
  3211. IPC_LOG_MESSAGE *pLog = &FLogMsg;//new IPC_LOG_MESSAGE;
  3212. if (pLog)
  3213. {
  3214. pLog->Kind = ALogKind;
  3215. pLog->Flag = 0;
  3216. pLog->Len = strlen(szFmtData);
  3217. pLog->Tm = Now();
  3218. pLog->Type = 'C';
  3219. memset(pLog->Msg, 0x00, sizeof(pLog->Msg));
  3220. sprintf(pLog->Msg, "%s-CLI %s, %s", sLogKind.c_str(), VMSID.c_str(), szFmtData);
  3221. pLog->Obj = (void*)FVmsObj;
  3222. DWORD dwResult = 0;
  3223. LRESULT lResult = SEND_LOGMSG(FrmVmsLog->Handle, WM_LOG_DISPLAY, pLog, NULL, SMTO_NORMAL, 2000, dwResult);
  3224. if (lResult == 0)
  3225. {
  3226. dwResult = GetLastError();
  3227. if (dwResult == ERROR_TIMEOUT)
  3228. {
  3229. }
  3230. //SAFE_DELETE(pLog);
  3231. }
  3232. }
  3233. }
  3234. }
  3235. catch(Exception &e)
  3236. {
  3237. }
  3238. return cnt;
  3239. }
  3240. //---------------------------------------------------------------------------
  3241. int __fastcall TVMSCThread::SendSubRegister(int AObj)
  3242. {
  3243. TStringList *sl = NULL;
  3244. String sMsg = "";
  3245. String sGeneralStatus =
  3246. "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
  3247. "0F 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
  3248. "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
  3249. "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
  3250. "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
  3251. "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 06 A1 "
  3252. "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
  3253. "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
  3254. "F3 6B 02 00 06 A1 02 30 00 82 02 9E 17 ";
  3255. String sPowerStatus =
  3256. "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
  3257. "10 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
  3258. "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
  3259. "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
  3260. "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
  3261. "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 07 A1 "
  3262. "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
  3263. "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
  3264. "F3 6B 02 00 07 A1 02 30 00 82 02 27 24 ";
  3265. String sModuleStatus =
  3266. "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
  3267. "11 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
  3268. "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
  3269. "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
  3270. "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
  3271. "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 08 A1 "
  3272. "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
  3273. "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
  3274. "F3 6B 02 00 08 A1 02 30 00 82 02 BD E6 ";
  3275. String sDotStatus =
  3276. "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
  3277. "12 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
  3278. "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
  3279. "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
  3280. "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
  3281. "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 09 A1 "
  3282. "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
  3283. "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
  3284. "F3 6B 02 00 09 A1 02 30 00 82 02 CD 4D ";
  3285. String sScreenStatus =
  3286. "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
  3287. "13 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
  3288. "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
  3289. "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
  3290. "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
  3291. "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 0A A1 "
  3292. "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
  3293. "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
  3294. "F3 6B 02 00 0A A1 02 30 00 82 02 9F 82 ";
  3295. String sDisplayingData =
  3296. "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
  3297. "14 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
  3298. "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
  3299. "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
  3300. "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
  3301. "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 0B A1 "
  3302. "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
  3303. "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
  3304. "F3 6B 02 00 0B A1 02 30 00 82 02 2C 1B ";
  3305. switch (AObj)
  3306. {
  3307. case OBJ_GeneralStatus: sMsg = sGeneralStatus; break;
  3308. case OBJ_PowerStatus: sMsg = sPowerStatus; break;
  3309. case OBJ_ModuleStatus: sMsg = sModuleStatus; break;
  3310. case OBJ_DotStatus: sMsg = sDotStatus; break;
  3311. case OBJ_ScreenStatus: sMsg = sScreenStatus; break;
  3312. case OBJ_DisplayingData: sMsg = sDisplayingData; break;
  3313. default: return SYS_ERR_OTHER;
  3314. }
  3315. try
  3316. {
  3317. sl = new TStringList;
  3318. sl->Delimiter = ' ';
  3319. sl->StrictDelimiter = true;
  3320. sl->DelimitedText = sMsg;
  3321. int ii;
  3322. unsigned int hn, ln;
  3323. BYTE chn, cln, val;
  3324. char szData[2048];
  3325. BYTE RxBuff[VMS_MAX_PACKET_SIZE];
  3326. int RxLen;
  3327. RxLen = 0;
  3328. memset(RxBuff, 0x00, sizeof(RxBuff));
  3329. for (ii = 0; ii < sl->Count; ii++)
  3330. {
  3331. AnsiString sTemp = sl->Strings[ii];
  3332. if (sTemp.Trim() == "") continue;
  3333. sTemp = StringReplace(sTemp, " ", "", TReplaceFlags() << rfReplaceAll);
  3334. if (sTemp.Trim() == "\r\n") continue;
  3335. memset(szData, 0x00, sizeof(szData));
  3336. memcpy(szData, sTemp.c_str(), sTemp.Length());
  3337. chn = (BYTE)szData[0];
  3338. cln = (BYTE)szData[1];
  3339. hn = chn > '9' ? chn - 'A' + 10 : chn - '0';
  3340. ln = cln > '9' ? cln - 'A' + 10 : cln - '0';
  3341. val = (BYTE)((hn << 4 ) | ln);
  3342. RxBuff[RxLen] = val;
  3343. RxLen++;
  3344. }
  3345. int nSends = TcpClient->SendBuf(RxBuff, RxLen);
  3346. LINFO("SendSubRegister: %d/%d Bytes", RxLen, nSends);
  3347. if (sl) delete sl;
  3348. }
  3349. catch(Exception &e)
  3350. {
  3351. //Application->ShowException(&e);
  3352. }
  3353. return SYS_ERR_NONE;
  3354. }
  3355. //---------------------------------------------------------------------------