12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812 |
- /****************************************************************************
- * @source : VMSCThread.cpp
- * @description : VMS Client Socket Thread
- ****************************************************************************
- * DATE AUTHOR DESCRIPTION
- * --------------------------------------------------------------------------
- * 2012/03/09 CYM [100] First Cut
- *
- ****************************************************************************/
- //---------------------------------------------------------------------------
- #include "AppGlobalF.h"
- #include "VMSCThread.h"
- #include "FrmSysLogF.h"
- #include "FrmVmsInfoF.h"
- #include "FrmVmsLogF.h"
- #include <winsock.h>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- // Important: Methods and properties of objects in VCL can only be
- // used in a method called using Synchronize, for example:
- //
- // Synchronize(&UpdateCaption);
- //
- // where UpdateCaption could look like:
- //
- // void __fastcall TVMSCThread::UpdateCaption()
- // {
- // Form1->Caption = "Updated in a thread";
- // }
- //---------------------------------------------------------------------------
- #define VMSID FVmsObj->VmsId
- /*
- * 스레드 생성자
- * arguments
- * void
- * return
- * void
- */
- __fastcall TVMSCThread::TVMSCThread(bool CreateSuspended, int Tag, bool *ActiveIndicator)
- : TThread(CreateSuspended)
- {
- try
- {
- FVmsObj = (TVmsCtlr*)Tag;
- FLogFile = FVmsObj->FCLog;
- ThreadActiveIndicator = ActiveIndicator;
- *ThreadActiveIndicator = true;
- // 정보 초기화
- ClientInitInfo();
- TcpClient = new TTcpClient(NULL);
- TcpClient->RemoteHost = FVmsObj->IpAddress;
- TcpClient->RemotePort = FVmsObj->LocalNo;//g_AppCfg.comm.nClientPort;
- TcpClient->BlockMode = bmBlocking;
- TcpClient->OnConnect = TcpClientConnect;
- TcpClient->OnCreateHandle = TcpClientCreateHandle;
- TcpClient->OnDestroyHandle = TcpClientDestroyHandle;
- TcpClient->OnDisconnect = TcpClientDisconnect;
- TcpClient->OnError = TcpClientError;
- TcpClient->OnReceive = TcpClientReceive;
- TcpClient->OnSend = TcpClientSend;
- Priority = tpNormal;
- FreeOnTerminate = true;
- Resume();
- }
- catch(Exception &e)
- {
- SERROR("Thread Init Error");
- Terminate();
- }
- }
- //---------------------------------------------------------------------------
- /*
- * 스레드 소멸자
- * arguments
- * void
- * return
- * void
- */
- __fastcall TVMSCThread::~TVMSCThread()
- {
- try
- {
- FVmsObj->Client.Status = process_fail;
- SAFE_DELETE(TcpClient);
- }
- catch(Exception &e)
- {
- }
- *ThreadActiveIndicator = false;
- }
- //---------------------------------------------------------------------------
- /*
- * 연결 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientConnect(TObject *Sender)
- {
- ClientInitInfo();
- FVmsObj->Client.Comm = state_open;
- FVmsObj->Client.Status = process_run;
- if (!APP_EXIT) {
- POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_CLIENT_STTS, (int)FVmsObj);
- SINFO("Server Connect ok: %s %s", TcpClient->RemoteHost.c_str(), TcpClient->RemotePort.c_str());
- }
- }
- //---------------------------------------------------------------------------
- /*
- * 연결 해제 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientDisconnect(TObject *Sender)
- {
- FVmsObj->Client.Comm = state_close;
- FVmsObj->Client.Status = process_run;
- m_pState->Comm = vms_error;
- if (!APP_EXIT) {
- //POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_CLIENT_STTS, (int)FVmsObj);
- SINFO("Server Disconnect: %s %s", TcpClient->RemoteHost.c_str(), TcpClient->RemotePort.c_str());
- }
- }
- //---------------------------------------------------------------------------
- /*
- * 핸들 생성 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientCreateHandle(TObject *Sender)
- {
- //LDEBUG("Thread Create Handle");
- }
- //---------------------------------------------------------------------------
- /*
- * 핸들 소멸 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientDestroyHandle(TObject *Sender)
- {
- //LDEBUG("Thread Destroy Handle");
- }
- //---------------------------------------------------------------------------
- void __fastcall TVMSCThread::Close(void)
- {
- try
- {
- FVmsObj->Client.Status == process_fail;
- if (TcpClient)
- {
- //if (TcpClient->Connected)
- {
- TcpClient->Close();
- //Sleep(3000);
- }
- }
- }
- catch(Exception &e)
- {
- }
- }
- //---------------------------------------------------------------------------
- /*
- * 에러 처리 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientError(TObject *Sender, int SocketError)
- {
- LPVOID lpMsgBuf = NULL;
- try
- {
- try
- {
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- //GetLastError(),
- //MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- SocketError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- );
- char *pData = (char*)lpMsgBuf;
- if (strlen(pData) > 2)
- {
- pData[strlen(pData)-2] = 0x00;
- pData[strlen(pData)-1] = 0x00;
- }
- SERROR("SocketError: (%d: %s)", SocketError, pData);
- }
- __finally
- {
- if (lpMsgBuf) LocalFree(lpMsgBuf);
- lpMsgBuf = NULL;
- }
- }
- catch(Exception &e)
- {
- }
- Close();
- }
- //---------------------------------------------------------------------------
- /*
- * 데이터 수신 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientReceive(TObject *Sender, char *Buf, int &DataLen)
- {
- //LDEBUG("VMS Client Receive: %s, %d", VMSID.c_str(), DataLen);
- }
- //---------------------------------------------------------------------------
- /*
- * 데이터 송신 이벤트 핸들러
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::TcpClientSend(TObject *Sender, char *Buf, int &DataLen)
- {
- //LDEBUG("VMS Client Send: %s, %d", VMSID.c_str(), DataLen);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TVMSCThread::SocketActivity(bool &AError)
- {
- bool Error, ReadReady, WriteReady, ExcepFlag;
- ReadReady = false;
- AError = false;
- if (!TcpClient) return ReadReady;
- try
- {
- Error = TcpClient->Select(&ReadReady, &WriteReady, &ExcepFlag, 0);
- if ((Error == false))// || (ExcepFlag == true)) //exception flag == o.o.b(out of band data receive)
- {
- AError = true;
- ReadReady = false;
- }
- }
- catch(Exception &e)
- {
- AError = true;
- ReadReady = false;
- }
- return ReadReady;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TVMSCThread::SocketActivity()
- {
- bool bResult = false;
- bool Error, ReadReady, WriteReady, ExcepFlag;
- if (!TcpClient) return bResult;
- try
- {
- Error = TcpClient->Select(&ReadReady, &WriteReady, &ExcepFlag, 0);
- if ((Error == false) || (ReadReady == true) || (ExcepFlag == true))
- {
- bResult = true;
- }
- }
- catch(Exception &e)
- {
- }
- return bResult;
- }
- //---------------------------------------------------------------------------
- void __fastcall TVMSCThread::Execute()
- {
- BYTE buffer[DEFAULT_TCP_PACKET_MAX_SIZE];
- int nError;
- int nResult;
- bool bError;
- int nReadLen;
- unsigned long nRecvLen;
- try
- {
- NameThreadForDebugging(m_strName);
- FErrLine = 0;
- SINFO("Thread start");
- try
- {
- while (!Terminated)
- {
- FVmsObj->Client.Status = process_run;
- m_pState->Comm = vms_error;
- if (g_AppCfg.bThrExit)
- {
- nError = 0;
- SINFO("Thread exit catched....1");
- Terminate();
- }
- if (FVmsObj->Server.Status != process_run)
- {
- //VMS서버프로세스가 연결이 안된상태임
- //LDEBUG("Server not connected...");
- Sleep(1000);
- continue;
- }
- // VMS가 서버로 접속을 하였으니까 VMS로 연결을 시도하자
- SINFO("Server connect try: %s.%s", TcpClient->RemoteHost.c_str(), TcpClient->RemotePort.c_str());
- TcpClient->Open();
- bError = false;
- while (!Terminated && TcpClient->Connected)
- {
- m_pState->Comm = vms_normal;
- try
- {
- ClientStateMachine();
- bError = false;
- if (TcpClient->WaitForData(100) || SocketActivity())
- {
- bError = false;
- nRecvLen = 0;
- #if 0
- nResult = ioctlsocket(TcpClient->Handle, FIONREAD, &nRecvLen);
- if (nResult != 0)
- {
- Close();
- }
- if (nRecvLen > 0)
- #endif
- {
- memset(buffer, 0, sizeof(buffer));
- nReadLen = TcpClient->ReceiveBuf(buffer, sizeof(buffer));
- if (nReadLen > 0)
- {
- if ((nResult = RecvPacket(buffer, nReadLen)) != SYS_ERR_NONE)
- {
- ProcErrorState(TRUE, nResult);
- }
- }
- else
- {
- bError = true;
- }
- #if 0
- if ((int)nRecvLen != nReadLen)
- {
- SERROR("Receive data size false: %d, %d", nRecvLen, nReadLen);
- }
- #endif
- }
- }
- if (bError)
- {
- SERROR("Socket Error[Closed]....");
- nError = -3;
- Close();
- }
- else
- {
- //ClientStateMachine();
- }
- if (FVmsObj->Server.Status != process_run)
- {
- nError = -4;
- Close();
- }
- if (g_AppCfg.bThrExit)
- {
- nError = 0;
- SINFO("Thread exit catched....2");
- Close();
- }
- }
- catch(Exception &e)
- {
- SERROR("Thread execute exception: %s, Line: %d", AnsiString(e.Message).c_str(), FErrLine);
- nError = -2;
- Close();
- }
- }
- if (FVmsObj->Server.Status == process_run)
- {
- Sleep(3000);
- }
- }
- SERROR("Term: %d, Conn: %d, Err: %d, Line: %d", Terminated, TcpClient->Connected, nError, FErrLine);
- }
- __finally
- {
- TcpClient->Close();
- Terminate();
- }
- }
- catch(Exception &e)
- {
- SERROR("Thread execute error: %s", AnsiString(e.Message).c_str());
- Terminate();
- }
- m_pState->Comm = vms_error;
- ClientTerminate();
- }
- //---------------------------------------------------------------------------
- /*
- * 클라이언트 정보 초기화 루틴
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::ClientInitInfo(void)
- {
- m_RxLen = 0;
- m_rTimer = Now();
- m_WatchDogTimer = Now();
- FWatchDogTime = g_AppCfg.comm.nWatchDogTime;
- m_Client.IpAddress = FVmsObj->IpAddress;
- m_Client.Addr = FVmsObj->LocalNo;
- m_Client.VmsId = FVmsObj->VmsId;
- m_pState = &FVmsObj->RSTATE;
- m_Client.Port = g_AppCfg.comm.nClientPort;
- m_Client.TimeOut = g_AppCfg.comm.nCmdTimeOut;
- m_Client.CycleTime = g_AppCfg.comm.nPollingTime;
- m_Client.State = CT_LOGIN;//CT_IDLE;
- m_Client.sTimer = Now() - ITSCOMM_SECTIME(CLIENT_CONNECT_RETRY_TIME - CLIENT_CHANGE_TIME);
- m_Client.iRetry = VMS_MAX_RETRY_COUNT;
- m_Client.cTimer = Now();
- //m_strName.sprintf("VMSC%03d", m_Client.Addr);
- m_strName = "VMSC" + FVmsObj->VmsId;
- m_Client.LocalCenterId = "";
- m_Client.CenterId = "";
- m_Client.LocalDomainNm = g_AppCfg.comm.sLocalDomain;
- m_Client.DomainNm = m_Client.VmsId;
- m_Client.UserName = g_AppCfg.comm.sLoginUser;
- m_Client.UserPasswd = g_AppCfg.comm.sLoginPswd;
- m_Client.MaxHeartbeatTime = VMS_MAX_HEARTBEAT_TIME;
- m_Client.DatagramSize = VMS_MAX_DATAGRAM_SIZE;
- m_DataPacketNumber = 0;
- m_SubscribeSerialNbr = 0;
- m_SubRegistered.GeneralStatus = false;
- m_SubRegistered.PowerStatus = false;
- m_SubRegistered.ModuleStatus = false;
- m_SubRegistered.DotStatus = false;
- m_SubRegistered.ScreenStatus = false;
- m_SubRegistered.DisplayingData = false;
- m_Registered.GeneralStatus = false;
- m_Registered.ModuleStatus = false;
- m_Registered.PowerStatus = false;
- m_Registered.ScreenDataStatusMessage = false;
- m_Registered.RealTimeDisplayMessage = false;
- FGenState = FModState = FPowState = false;
- m_pState->Comm = vms_error;
- #if 0
- if (m_pState != NULL)
- {
- m_pState->Cpsw.Enable = bit_enable;
- m_pState->CollectDate = Now().FormatString("yyyymmddhhnnss");
- m_pState->Door = STATE_UNKNOWN;
- m_pState->Heater = STATE_UNKNOWN;
- m_pState->Fan = STATE_UNKNOWN;
- m_pState->ModuleState = STATE_UNKNOWN;
- m_pState->Power = STATE_UNKNOWN;
- m_pState->Temp = 0;
- m_pState->Bright = 0;
- m_pState->ModuleHorizontal = 0;
- m_pState->ModuleVertical = 0;
- m_pState->PowerCount = 0;
- memset(m_pState->ModuleStatus, STATE_UNKNOWN, sizeof(m_pState->ModuleStatus));
- memset(m_pState->PowerStatus, STATE_UNKNOWN, sizeof(m_pState->PowerStatus));
- }
- #endif
- LINFO("ClientInitInfo ok");
- }
- //---------------------------------------------------------------------------
- void __fastcall TVMSCThread::ClientTerminate(void)
- {
- try
- {
- FVmsObj->Client.Comm = state_close;
- FVmsObj->Client.Status = process_fail;
- FVmsObj->Client.pThread = NULL;
- FVmsObj->Client.ActiveIndicator = false;
- if (!APP_EXIT) {
- //POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_CLIENT_STTS, (int)FVmsObj);
- SINFO("Thread stop");
- }
- }
- catch(Exception &e)
- {
- }
- }
- //---------------------------------------------------------------------------
- /*
- *
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::LOG_WriteTime(Time_t *pTime)
- {
- FErrLine = 1;
- if (!pTime) return;
- if (pTime->time_Year_qty)
- {
- LDEBUG("time_Year_qty: %d", *pTime->time_Year_qty);
- }
- if (pTime->time_Month_qty)
- {
- LDEBUG("time_Month_qty: %d", *pTime->time_Month_qty);
- }
- if (pTime->time_Day_qty)
- {
- LDEBUG("time_Day_qty: %d", *pTime->time_Day_qty);
- }
- LDEBUG("time_Hour_qty : %d", pTime->time_Hour_qty);
- LDEBUG("time_Minute_qty: %d", pTime->time_Minute_qty);
- LDEBUG("time_Second_qty: %d", pTime->time_Second_qty);
- switch(pTime->time_SecondFractions_zz.present)
- {
- case time_SecondFractions_PR_deci_seconds:
- LDEBUG("deci_seconds: %d", pTime->time_SecondFractions_zz.choice.deci_seconds);
- break;
- case time_SecondFractions_PR_centi_seconds:
- LDEBUG("centi_seconds: %d", pTime->time_SecondFractions_zz.choice.centi_seconds);
- break;
- case time_SecondFractions_PR_milliseconds:
- LDEBUG("milliseconds: %d", pTime->time_SecondFractions_zz.choice.milliseconds);
- break;
- default:
- LDEBUG("Unknown time_SecondFractions %d", pTime->time_SecondFractions_zz.present);
- break;
- }
- if (pTime->time_Timezone)
- {
- LDEBUG("timezone_Hour_qty: %d", pTime->time_Timezone->timezone_Hour_qty);
- LDEBUG(" time_Minute_qty: %d", pTime->time_Timezone->time_Minute_qty);
- }
- }
- /*--------------------------------------------------------------------------*/
- /*
- *
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::SaveStateData(GeneralStatusMessage_t *pGS)
- {
- FErrLine = 2;
- int result;
- long lv;
- result = SYS_ERR_OTHER;
- if (m_pState == NULL) return result;
- // m_pState->Cpsw.Enable = bit_enable;
- // m_pState->CollectDate = Now().FormatString("yyyymmddhhnnss");
- asn_INTEGER2long(&pGS->door_StatusCode, &lv);
- switch(lv)
- {
- case door_StatusCode_closed: m_pState->DoorStatus = vms_close; break;
- case door_StatusCode_open : m_pState->DoorStatus = vms_open; break;
- default : m_pState->DoorStatus = vms_unknown; break;
- }
- LDEBUG(" door_StatusCode: %u, %d", lv, m_pState->DoorStatus);
- asn_INTEGER2long(&pGS->modulePower_StatusCode, &lv);
- m_pState->ModulePowerStatus = lv;
- switch(lv)
- {
- case modulePower_StatusCode_off: m_pState->ModulePowerStatus = vms_off; break;
- case modulePower_StatusCode_on : m_pState->ModulePowerStatus = vms_on; break;
- default : m_pState->ModulePowerStatus = vms_unknown; break;
- }
- LDEBUG("modulePower_StatusCode: %u, %d", lv, m_pState->ModulePowerStatus);
- asn_INTEGER2long(&pGS->body_TemperatureQty, &lv);
- m_pState->BodyTemp = lv;
- LDEBUG(" body_TemperatureQty: %u, %d", lv, m_pState->BodyTemp);
- asn_INTEGER2long(&pGS->luminance_StatusQty, &lv);
- m_pState->LuminanceStatus = lv;
- LDEBUG(" luminance_StatusQty: %u, %d", lv, m_pState->LuminanceStatus);
- asn_INTEGER2long(&pGS->fan_StatusCode, &lv);
- m_pState->FanStatus = lv;
- switch(lv)
- {
- case fan_StatusCode_off: m_pState->FanStatus = vms_off; break;
- case fan_StatusCode_on : m_pState->FanStatus = vms_on; break;
- default : m_pState->FanStatus = vms_unknown; break;
- }
- LDEBUG(" fan_StatusCode: %u, %d", lv, m_pState->FanStatus);
- asn_INTEGER2long(&pGS->heater_StatusCode, &lv);
- m_pState->HeaterStatus = lv;
- switch(lv)
- {
- case heater_StatusCode_off: m_pState->HeaterStatus = vms_off; break;
- case heater_StatusCode_on : m_pState->HeaterStatus = vms_on; break;
- default : m_pState->HeaterStatus = vms_unknown; break;
- }
- LDEBUG(" heater_StatusCode: %u, %d", lv, m_pState->HeaterStatus);
- result = SYS_ERR_NONE;
- {
- if (pGS->externalLight_StatusCode)
- {
- asn_INTEGER2long(pGS->externalLight_StatusCode, &lv);
- m_pState->ExternalLightStatus = lv;
- //LDEBUG("externalLight_StatusCode: %u", lv);
- }
- if (pGS->alarmLight_StatusCode)
- {
- asn_INTEGER2long(pGS->alarmLight_StatusCode, &lv);
- m_pState->AlarmLightStatus = lv;
- //LDEBUG("alarmLight_StatusCode: %u", lv);
- }
- if (pGS->speaker_StatusCode)
- {
- asn_INTEGER2long(pGS->speaker_StatusCode, &lv);
- m_pState->SpeakerStatus = lv;
- //LDEBUG("speaker_StatusCode: %u", lv);
- }
- if (pGS->controller_CurrentTime)
- {
- sprintf((char*)m_pState->ControllerCurrentTime, ".*s", pGS->controller_CurrentTime->size, pGS->controller_CurrentTime->buf);
- //LDEBUG("controller_CurrentTime: %.*s", pGS->controller_CurrentTime->size, pGS->controller_CurrentTime->buf);
- }
- asn_INTEGER2long(&pGS->scheduledmessage_OperatingTime, &lv);
- m_pState->ScheduledMessageOperatingTime = lv;
- LDEBUG("scheduledmessage_OperatingTime: %u", lv);
- asn_INTEGER2long(&pGS->module_OperatingTemperatureQty, &lv);
- m_pState->ModuleOperatingTemperature = lv;
- LDEBUG("module_OperatingTemperatureQty: %u", lv);
- asn_INTEGER2long(&pGS->fan_OperatingTemperatureQty, &lv);
- m_pState->FanOperatingTemperature = lv;
- LDEBUG(" fan_OperatingTemperatureQty: %u", lv);
- asn_INTEGER2long(&pGS->heater_OperatingTemperatureQty, &lv);
- m_pState->HeaterOperatingTemperature = lv;
- LDEBUG("heater_OperatingTemperatureQty: %u", lv);
- if (pGS->externalLight_OperatingLuminanceQty)
- {
- asn_INTEGER2long(pGS->externalLight_OperatingLuminanceQty, &lv);
- m_pState->ExternalLightOperatingLuminance = lv;
- //LDEBUG("externalLight_OperatingLuminanceQty: %u", lv);
- }
- //아래존재함
- if (pGS->module_BasicFailureRate)
- {
- asn_INTEGER2long(pGS->module_BasicFailureRate, &lv);
- m_pState->ModuleBasicFailureRate = lv;
- //LDEBUG(" module_BasicFailureRate: %u", lv);
- }
- if (pGS->maximum_RetryQty)
- {
- asn_INTEGER2long(pGS->maximum_RetryQty, &lv);
- m_pState->MaximumRetry = lv;
- //LDEBUG("maximum_RetryQty: %u", lv);
- }
- if (pGS->response_TimeOutQty)
- {
- asn_INTEGER2long(pGS->response_TimeOutQty, &lv);
- m_pState->ResponseTimeOut = lv;
- //LDEBUG("response_TimeOutQty: %u", lv);
- }
- if (pGS->blinking_CycleTime)
- {
- asn_INTEGER2long(pGS->blinking_CycleTime, &lv);
- m_pState->BlinkingCycleTime = lv;
- //LDEBUG("blinking_CycleTime: %u", lv);
- }
- }
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 패킷 수신
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::RecvPacket(void *Data, int Len)
- {
- FErrLine = 3;
- int result;
- ssize_t stat;
- ssize_t length;
- DatexDataPacket_t *pMsg = 0; /* Type to decode */
- asn_dec_rval_t rval; /* Decoder return value */
- long version;
- WORD crc;
- result = SYS_ERR_NONE;
- if (Len > 0)
- {
- LogData("RECV", (char*)Data, Len);
- }
- if (m_RxLen)
- {
- if ((m_RxLen + Len) > CLIENT_MAX_PACKET_SIZE) m_RxLen = 0;
- memcpy(m_RxBuff + m_RxLen, Data, Len);
- m_RxLen += Len;
- }
- else
- {
- memcpy(m_RxBuff, Data, Len);
- m_RxLen = Len;
- m_rTimer = Now();
- }
- while (m_RxLen >= VMS_MIN_PACKET_SIZE)
- {
- stat = ASN1C_BerReadLength(m_RxBuff, m_RxLen, &length);
- if ((stat != RC_OK) || (length > VMS_MAX_PACKET_SIZE))
- {
- result = SYS_ERR_INVALID_TAGANDLEN;
- break;
- }
- LINFO("Recv: state[%d], length[%d/%d]", stat, length, m_RxLen);
- if (length > m_RxLen)
- {
- LINFO("Recv packet remain: ReqLen(%d) recv(%d)", length, m_RxLen);
- break;
- }
- /* Decode */
- #if 0
- rval = ber_decode(0, &asn_DEF_DatexDataPacket, (void **)&pMsg, m_RxBuff, m_RxLen);
- #else
- rval = ber_decode(0, &asn_DEF_DatexDataPacket, (void **)&pMsg, m_RxBuff, length);
- #endif
- switch(rval.code)
- {
- case RC_OK:
- if ((rval.consumed == 0) || (pMsg == 0))
- {
- result = SYS_ERR_DATEXDATAPACKET_DECODE;
- break;
- }
- asn_INTEGER2long(&pMsg->datex_Version_number, &version);
- if (version != datex_Version_number_version1)
- {
- LERROR("SYS_ERR_INVALID_VERSION_NUMBER: %d/%d", version, datex_Version_number_version1);
- result = SYS_ERR_INVALID_VERSION_NUMBER;
- break;
- }
- #if 1
- crc = ASN1C_MakeCrc16((void *)pMsg->datex_Data.buf, pMsg->datex_Data.size);
- if (pMsg->datex_Crc_nbr.size != 2 ||
- pMsg->datex_Crc_nbr.buf[0] != HIBYTE(crc) ||
- pMsg->datex_Crc_nbr.buf[1] != LOBYTE(crc))
- {
- result = SYS_ERR_CRC;
- break;
- }
- #endif
- /* 패킷 처리 */
- result = ProcessRecvPacket((BYTE *)pMsg->datex_Data.buf, pMsg->datex_Data.size);
- if (result != SYS_ERR_NONE)
- {
- SERROR("Receive Packet Error : %d", result);
- }
- break;
- case RC_WMORE:
- case RC_FAIL:
- default:
- result = SYS_ERR_DATEXDATAPACKET_DECODE;
- break;
- }
- if (pMsg)
- {
- ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
- pMsg = 0;
- }
- if (result != SYS_ERR_NONE)
- {
- m_RxLen = 0;
- break;
- }
- m_RxLen -= length;
- if (m_RxLen < 0) m_RxLen = 0;
- if (m_RxLen > 0)
- {
- memcpy(m_RxBuff, m_RxBuff + length, m_RxLen);
- }
- } /* end while */
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 수신 패킷 처리
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::ProcessRecvPacket(BYTE *pBuffer, int Len)
- {
- FErrLine = 4;
- int result, iResult;
- C2CAuthenticatedMessage_t *pMsg = 0; /* Type to decode */
- asn_dec_rval_t rval; /* Decoder return value */
- BYTE cmd;
- BYTE pdu;
- CLIENT_ARGUMENT arg;
- long DataPacket_number;
- result = SYS_ERR_OTHER;
- /* Decode */
- rval = ber_decode(0, &asn_DEF_C2CAuthenticatedMessage, (void **)&pMsg, pBuffer, Len);
- switch(rval.code)
- {
- case RC_OK:
- {
- if ((rval.consumed == 0) || (pMsg == 0))
- {
- LERROR("SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE");
- result = SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE;
- break;
- }
- /* 공통부분 */
- if (pMsg->datex_AuthenticationInfo_text.size != 1 ||
- /* pMsg->datex_DataPacket_number < 0 || */
- /* pMsg->datex_DataPacket_number > 4294967295 || */
- /* pMsg->datex_DataPacketPriority_number < 0 || */
- pMsg->datex_DataPacketPriority_number > 10)
- {
- LERROR("SYS_ERR_C2CAUTHENTICATEDMESSAGE: %d/%d", pMsg->datex_AuthenticationInfo_text.size, pMsg->datex_DataPacketPriority_number);
- result = SYS_ERR_C2CAUTHENTICATEDMESSAGE;
- break;
- }
- asn_INTEGER2long(&pMsg->datex_DataPacket_number, &DataPacket_number);
- pdu = pMsg->pdu.present;
- cmd = pMsg->datex_AuthenticationInfo_text.buf[0];
- #if 0
- HeaderOptions_t *options;
- /* Header Options */
- options = &pMsg->options;
- if (options->datex_OriginAddress_location && options->datex_DestinationAddress_location)
- {
- LINFO("Recv OrginAddr: %s(%s), DestinationAddr: %s(%s)",
- options->datex_OriginAddress_location->buf,
- m_Client.CenterId.c_str(),
- options->datex_DestinationAddress_location->buf,
- m_Client.LocalCenterId.c_str());
- }
- #if 0
- if (options->datex_OriginAddress_location->size == 0 ||
- options->datex_DestinationAddress_location->size == 0)
- {
- result = SYS_ERR_HEADER_OPTIONS;
- break;
- }
- if (memcmp(options->datex_OriginAddress_location->buf,
- m_Client.CenterId.c_str(),
- options->datex_OriginAddress_location->size) ||
- memcmp(options->datex_DestinationAddress_location->buf,
- m_Client.LocalCenterId.c_str(),
- options->datex_DestinationAddress_location->size))
- {
- result = SYS_ERR_ADDRESS;
- break;
- }
- #endif
- #endif
- /* options->datex_Origin_text */
- /* options->datex_Sender_text */
- /* options->datex_SenderAddress_location */
- /* options->datex_Destination_text */
- /* options->datex_Cost->amount_Currency_code */
- /* options->datex_Cost->amount_Factor_quantity */
- /* options->datex_Cost->amount_Quantity_quantity */
- /* options->datex_DataPacket_time->time_Year_qty */
- /* options->datex_DataPacket_time->time_Month_qty */
- /* options->datex_DataPacket_time->time_Day_qty */
- /* options->datex_DataPacket_time->time_Hour_qty */
- /* options->datex_DataPacket_time->time_Minute_qty */
- /* options->datex_DataPacket_time->time_Second_qty */
- /* options->datex_DataPacket_time->time_SecondFractions->present */
- /* options->datex_DataPacket_time->time_SecondFractions->choice.milliseconds */
- /* options->datex_DataPacket_time->time_Timezone->timezone_Hour_qty */
- /* options->datex_DataPacket_time->time_Timezone->time_Minute_qty */
- if (cmd == AI_Reject)
- {
- //용인VMS제어기 인지를 확인하는 코드임....
- if (pMsg->pdu.present == PDUs_PR_accept)
- {
- if (m_Client.State == CT_LOGIN_WAIT)
- {
- //용인 VMS ==> 상태정보 요청하면 VMS제어기 프로그램이 다운됨
- //10초마다 상태정보를 자동으로 올림
- LINFO("OLD VMS Connect...");
- if (FVmsObj->FIsOldProtocol == false)
- {
- //여기서 설정하지 말고 환경설정으로 되어 있는것으로 하자..
- //FVmsObj->FIsOldProtocol = true;
- }
- }
- }
- }
- switch(pdu)
- {
- case PDUs_PR_NOTHING:
- LINFO("<<< PDUs_PR_NOTHING 수신xxxxx");
- result = SYS_ERR_NONE;
- break;
- case PDUs_PR_datex_Initiate_null: /* 초기 통신연결을 위한 개시 요청 데이터 패킷 */
- LINFO("<<< PDUs_PR_datex_Initiate 수신");
- m_Client.State = CT_LOGIN;
- result = SYS_ERR_NONE;
- break;
- case PDUs_PR_login:
- //여기는 들어오면 안됌(클아이언트 모듈에서 보내는 메시지임)
- LINFO("<<< PDUs_PR_login 수신xxxxx");
- result = SYS_ERR_NONE;
- break;
- case PDUs_PR_fred: /* 서버와 클라이언트의 연결을 유지하기 위한 확인 데이터 패킷 */
- {
- long fred;
- asn_INTEGER2long(&pMsg->pdu.choice.fred, &fred);
- result = SYS_ERR_NONE;
- LINFO("<<< PDUs_PR_fred 수신: %u", fred);
- }
- if (m_Client.State != CT_FRED_WAIT)
- {
- LWARN("m_Client.State SYS_ERR_MISMATCH: %d, %d", m_Client.State, CT_FRED_WAIT);
- result = SYS_ERR_MISMATCH;
- break;
- }
- SetClientState(SET_CLIENT_SUCC);
- break;
- case PDUs_PR_terminate: /* 연결을 종료하고자 할 때, 서버에서 클라이언트에 요청하는 데이터 패킷 */
- {
- try
- {
- long terminate;
- asn_INTEGER2long(&pMsg->pdu.choice.terminate, &terminate);
- result = SYS_ERR_NONE;
- LINFO("<<< PDUs_PR_terminate 수신 %u", terminate);
- } catch(Exception &e) {}
- }
- break;
- case PDUs_PR_logout: /* 접속을 종료하기 위한 클라이언트의 로그아웃 데이터 패킷 */
- //여기는 들어오면 안됌(클아이언트 모듈에서 보내는 메시지임)
- LINFO("<<< PDUs_PR_logout 수신xxxxx");
- result = SYS_ERR_NONE;
- #if 0
- {
- long logout;
- asn_INTEGER2long(&pMsg->pdu.choice.logout, &logout);
- result = SYS_ERR_NONE;
- LINFO("<<< PDUs_PR_logout 수신 %d", logout);
- }
- arg.FrED = m_FrEDConfirmPacketNumber++;
- if((iResult = ProcessCommand(AI_FrED, &arg)) != SYS_ERR_NONE)
- {
- LERROR("Logout FrED Command failed: %d", iResult);
- }
- #endif
- break;
- case PDUs_PR_subscripiton:
- //여기는 들어오면 안됌(클아이언트 모듈에서 보내는 메시지임)
- LINFO("<<< PDUs_PR_subscripiton 수신xxxxx");
- break;
- case PDUs_PR_publication:
- LINFO("<<< PDUs_PR_publication 수신");
- switch(cmd)
- {
- case AI_GeneralStatusMessage: LINFO(" *** AI_GeneralStatusMessage 수신"); break;
- case AI_PowerStatusMessage: LINFO(" *** AI_PowerStatusMessage 수신"); break;
- case AI_ModuleStatusMessage: LINFO(" *** AI_ModuleStatusMessage 수신"); break;
- case AI_DotStatusMessage: LINFO(" *** AI_DotStatusMessage 수신"); break;
- case AI_ScreenStatusMessage: LINFO(" *** AI_ScreenStatusMessage 수신"); break;
- case AI_DisplayingDataMessage:LINFO(" *** AI_DisplayingDataMessage 수신"); break;
- default: LINFO(" *** PDUs_PR_publication Unknown 수신: %d", cmd); break;
- }
- result = PacketPublication(&pMsg->pdu.choice.publication);
- if (result == SYS_ERR_NONE)
- {
- m_WatchDogTimer = Now();
- arg.Accept = DataPacket_number;
- if((iResult = ProcessCommand(AI_Accept, &arg)) != SYS_ERR_NONE)
- {
- SERROR("Publication Accept Command failed: %d", iResult);
- }
- }
- else
- {
- int ErrCode;
- switch(result)
- {
- case SYS_ERR_DATEXPUBLISH_FORMAT:
- ErrCode = datexReject_Publication_cd_invalidPublishFormat;
- break;
- case SYS_ERR_UNKNOWN_MESSAGE_ID:
- ErrCode = datexReject_Publication_cd_unknownPublicationMsgId;
- break;
- case SYS_ERR_DATEXPUBLISH_TYPE:
- ErrCode = datexReject_Publication_cd_invalidPublicationMsgContent;
- break;
- case SYS_ERR_MESSAGE_DECODE:
- ErrCode = datexReject_Publication_cd_invalidPublicationMsgContent;
- break;
- default:
- ErrCode = datexReject_Publication_cd_other;
- }
- /* datexReject_Publication_cd_unknownSubscription */
- /* datexReject_Publication_cd_unknownPublicationNbr */
- /* datexReject_Publication_cd_invalidPublicationMsgId */
- /* datexReject_Publication_cd_repeatedPublicationNbr */
- arg.Reject.nbr = DataPacket_number;
- arg.Reject.code = ErrCode;
- if ((iResult = ProcessCommand(AI_Reject, &arg)) != SYS_ERR_NONE)
- {
- SERROR("Publication Reject Command failed: %d", iResult);
- }
- }
- break;
- case PDUs_PR_transfer_done:
- LINFO("<<< PDUs_PR_transfer_done 수신");
- result = SYS_ERR_NONE;
- break;
- case PDUs_PR_accept: /* 클라이언트의 요청에 대한 수용 */
- LINFO("<<< PDUs_PR_accept 수신");
- result = PacketAccept(&pMsg->pdu.choice.accept);
- break;
- case PDUs_PR_reject: /* 클라이언트의 요청에 대한 거부 */
- LINFO("<<< PDUs_PR_reject 수신");
- result = PacketReject(&pMsg->pdu.choice.reject);
- break;
- default:
- LINFO("<<< Unknown PDUs 수신: pdu(%d), cmd(%d)", pdu, cmd);
- result = SYS_ERR_UNKNOWN_COMMAND;
- break;
- }/* end switch */
- }
- break;
- case RC_WMORE:
- case RC_FAIL:
- default:
- result = SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE;
- break;
- }/* end switch */
- if (pMsg)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- }
- if ((result == SYS_ERR_NONE) && (cmd != AI_Reject))
- {
- m_Client.iRetry = VMS_MAX_RETRY_COUNT;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 수신 패킷 처리
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::PacketAccept(Accept_t *pAccept)
- {
- FErrLine = 5;
- int result;
- long nbr;
- long sub;
- result = SYS_ERR_OTHER;
- asn_INTEGER2long(&pAccept->datexAccept_Packet_nbr, &nbr);
- switch(pAccept->datexAccept_Type.present)
- {
- case datexAccept_Type_PR_logIn:
- if (m_Client.State != CT_LOGIN_WAIT)
- {
- result = SYS_ERR_MISMATCH;
- break;
- }
- /* pAccept->datexAccept_Type.choice.logIn */
- SetClientState(SET_CLIENT_SUCC);
- result = SYS_ERR_NONE;
- LINFO(" *** Accept_Type_PR_logIn: %u", nbr);
- break;
- case datexAccept_Type_PR_single_subscription:
- if (m_Client.State != CT_LOCAL_WAIT)
- {
- result = SYS_ERR_MISMATCH;
- break;
- }
- if (nbr == (long)m_Ctl.DataPacketNumber)
- {
- if (m_Ctl.obj == OBJ_GeneralStatus)
- {
- m_Registered.GeneralStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_ModuleStatus)
- {
- m_Registered.ModuleStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_PowerStatus)
- {
- m_Registered.PowerStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_ScreenStatus)
- {
- m_Registered.ScreenDataStatusMessage = true;
- }
- else
- if (m_Ctl.obj == OBJ_DisplayingData)
- {
- m_Registered.RealTimeDisplayMessage = true;
- }
- }/* end if */
- SetClientState(SET_CLIENT_SUCC);
- result = SYS_ERR_NONE;
- LINFO(" *** Accept_Type_PR_single_subscription: %u", nbr);
- break;
- case datexAccept_Type_PR_registered_subscription:
- if (m_Client.State != CT_LOCAL_WAIT && m_Client.State != CT_REGIST_WAIT)
- {
- result = SYS_ERR_MISMATCH;
- break;
- }
- if (nbr == (long)m_Ctl.DataPacketNumber)
- {
- if (m_Ctl.obj == OBJ_GeneralStatus)
- {
- if (m_Client.State == CT_REGIST_WAIT)
- m_SubRegistered.GeneralStatus = true;
- else
- m_Registered.GeneralStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_PowerStatus)
- {
- if (m_Client.State == CT_REGIST_WAIT)
- m_SubRegistered.PowerStatus = true;
- else
- m_Registered.PowerStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_ModuleStatus)
- {
- if (m_Client.State == CT_REGIST_WAIT)
- m_SubRegistered.ModuleStatus = true;
- else
- m_Registered.ModuleStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_DotStatus)
- {
- if (m_Client.State == CT_REGIST_WAIT)
- m_SubRegistered.DotStatus = true;
- else
- m_Registered.DotStatus = true;
- }
- else
- if (m_Ctl.obj == OBJ_ScreenStatus)
- {
- if (m_Client.State == CT_REGIST_WAIT)
- m_SubRegistered.ScreenStatus = true;
- else
- m_Registered.ScreenDataStatusMessage = true;
- }
- else
- if (m_Ctl.obj == OBJ_DisplayingData)
- {
- if (m_Client.State == CT_REGIST_WAIT)
- m_SubRegistered.DisplayingData = true;
- else
- m_Registered.RealTimeDisplayMessage = true;
- }
- }/* end if */
- asn_INTEGER2long(&pAccept->datexAccept_Type.choice.registered_subscription, &sub);
- SetClientState(SET_CLIENT_SUCC);
- result = SYS_ERR_NONE;
- LINFO(" *** Accept_Type_PR_registered_subscription: %u, %u", nbr, sub);
- break;
- /*
- case datexAccept_Type_PR_publication:
- break;
- */
- default:
- SERROR(" *** Unknown Accept_Type: %u, %d", nbr, pAccept->datexAccept_Type.present);
- result = SYS_ERR_ACCEPT_TYPE;
- break;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 수신 패킷 처리
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::PacketReject(Reject_t *pReject)
- {
- FErrLine = 6;
- int result;
- long nbr;
- long ErrCode;
- result = SYS_ERR_OTHER;
- asn_INTEGER2long(&pReject->datexReject_Packet_nbr, &nbr);
- switch(pReject->datexReject_Type.present)
- {
- case RejectType_PR_datexReject_Login_cd:
- asn_INTEGER2long(&pReject->datexReject_Type.choice.datexReject_Login_cd, &ErrCode);
- if (m_Client.State != CT_LOGIN_WAIT)
- {
- result = SYS_ERR_MISMATCH;
- break;
- }
- SetClientState(SET_CLIENT_FAIL);
- result = SYS_ERR_NONE;
- break;
- case RejectType_PR_datexReject_Subscription_cd:
- asn_INTEGER2long(&pReject->datexReject_Type.choice.datexReject_Subscription_cd, &ErrCode);
- if (m_Client.State != CT_LOCAL_WAIT) {
- result = SYS_ERR_MISMATCH;
- break;
- }
- SetClientState(SET_CLIENT_FAIL);
- result = SYS_ERR_NONE;
- break;
- /*
- case RejectType_PR_datexReject_Publication_cd:
- asn_INTEGER2long(&pReject->datexReject_Type.choice.datexReject_Publication_cd, &ErrCode);
- break;
- */
- default:
- ErrCode = -1;
- break;
- }
- /* pReject->datexReject_AlternateRequest */
- LINFO("Reject 수신[%d %d %d]", nbr, pReject->datexReject_Type.present, ErrCode);
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 수신 패킷 처리
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::PacketPublication(Publication_t *pPublication)
- {
- FErrLine = 7;
- int result;
- PublicationData_t *pData;
- int i;
- result = SYS_ERR_OTHER;
- /* pPublication->datexPublish_Guaranteed_bool */
- switch(pPublication->datexPublish_Format.present)
- {
- case Publish_Format_PR_datexPublish_Data:
- for(i = 0; i < pPublication->datexPublish_Format.choice.datexPublish_Data.list.count; i++)
- {
- pData = (PublicationData_t *)pPublication->datexPublish_Format.choice.datexPublish_Data.list.array[i];
- if ((result = PacketPublicationData(pData)) != SYS_ERR_NONE)
- {
- break;
- }
- }
- break;
- case Publish_Format_PR_datexPublish_FileName_txt:
- result = SYS_ERR_NONE;
- break;
- default:
- result = SYS_ERR_DATEXPUBLISH_FORMAT;
- break;
- }/* end switch */
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 수신 패킷 처리
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::PacketPublicationData(PublicationData_t *pData)
- {
- FErrLine = 8;
- int result;
- int Message_id;
- int i;
- result = SYS_ERR_OTHER;
- /* pData->datexPublish_SubscribeSerial_nbr */
- /* pData->datexPublish_Serial_nbr */
- /* pData->datexPublish_LatePublicationFlag */
- #if 0
- {
- long lv;
- asn_INTEGER2long(&pData->datexPublish_SubscribeSerial_nbr, &lv);
- LDEBUG("datexPublish_SubscribeSerial_nbr: %u", lv);
- asn_INTEGER2long(&pData->datexPublish_Serial_nbr, &lv);
- LDEBUG(" datexPublish_Serial_nbr: %u", lv);
- LDEBUG("datexPublish_LatePublicationFlag: %d", pData->datexPublish_LatePublicationFlag);
- }
- #endif
- switch(pData->datexPublish_Type.present)
- {
- case PublicationType_PR_datexPublication_Management_cd:
- result = SYS_ERR_NONE;
- break;
- case PublicationType_PR_datexPublish_Data:
- #if 0
- {
- int arcs[10];
- int count, j;
- count = OBJECT_IDENTIFIER_get_arcs(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_id,
- arcs, sizeof(int), 10);
- LDEBUG("endApplication_Message_id: %d", count);
- for (j = 0; j < count && j < 10; j++)
- {
- LDEBUG("[%d]: %d", j, arcs[j]);
- }
- }
- #endif
- if ((Message_id = ASN1C_GetObjId(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_id)) == -1)
- {
- return SYS_ERR_UNKNOWN_MESSAGE_ID;
- }
- /* pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg */
- switch(Message_id)
- {
- case OBJ_GeneralStatus: /* 기본상태정보 */
- {
- GeneralStatusMessage_t *pGS;
- MessageBodyGeneralStatusMessage_t *pMsg = 0; /* Type to decode */
- /* Decode */
- if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
- &asn_DEF_MessageBodyGeneralStatusMessage,
- (void **)&pMsg) == ASN_ERR_NONE)
- {
- for(i = 0; i < pMsg->list.count; i++)
- {
- pGS = (GeneralStatusMessage_t *)pMsg->list.array[i];
- SaveStateData(pGS);
- }
- //LINFO("GeneralStatus Publication Recv %d OK", pMsg->list.count);
- result = VERR_NONE;
- }
- else
- {
- result = SYS_ERR_MESSAGE_DECODE;
- }
- if(pMsg)
- {
- ASN1C_Free(&asn_DEF_MessageBodyGeneralStatusMessage, pMsg);
- }
- FGenState = true;
- }
- break;
- case OBJ_PowerStatus: /* 전원상태정보 */
- {
- PowerStatusMessage_t *pMS;
- MessageBodyPowerStatusMessage_t *pMsg = 0; /* Type to decode */
- long PowerQty;
- int numbits, bit;
- /* Decode */
- if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
- &asn_DEF_MessageBodyPowerStatusMessage,
- (void **)&pMsg) == ASN_ERR_NONE)
- {
- for(i = 0; i < pMsg->list.count; i++)
- {
- pMS = (PowerStatusMessage_t *)pMsg->list.array[i];
- asn_INTEGER2long(&pMS->power_Qty, &PowerQty);
- numbits = DATEX_NUMBITS(pMS->power_Status);
- //LDEBUG("Power Module Count: %d, %d", PowerQty, numbits);
- if (PowerQty != numbits)
- {
- PowerQty = numbits;//양산거는 다르게 나타남
- }
- if (numbits >= INT_VMS_MAX_POWER)
- {
- numbits = INT_VMS_MAX_POWER;
- PowerQty = numbits;
- LERROR("Power size overflow: %d, %d", numbits, INT_VMS_MAX_POWER);
- }
- if (PowerQty == numbits)
- {
- memset((char*)m_pState->PowerStatus, 0x00, sizeof(m_pState->PowerStatus));
- for (bit = 0; bit < numbits; bit++)
- {
- int nOnOff = ASN1C_CheckBitString(&pMS->power_Status, bit);
- if (nOnOff == bit_enable)
- {
- VComm_SetBitString((char*)m_pState->PowerStatus, bit);
- }
- }
- m_pState->PowerCount = PowerQty;
- }
- if (FLogFile->FLogCfg.Debug)
- {
- LDEBUG(" module_PowerQty: %d", PowerQty);
- AnsiString sModule = "";
- for (int v = 0; v < PowerQty; v++)
- {
- int nM = VComm_GetBitString((char*)m_pState->PowerStatus, v);
- sModule += String(nM);
- }
- LDEBUG("%s", sModule.c_str());
- }
- }/* end for */
- result = VERR_NONE;
- }
- else
- {
- result = SYS_ERR_MESSAGE_DECODE;
- }/* end if */
- if(pMsg)
- {
- ASN1C_Free(&asn_DEF_MessageBodyPowerStatusMessage, pMsg);
- }
- FPowState = true;
- }
- break;
- case OBJ_ModuleStatus: /* 모듈상태정보 */
- {
- ModuleStatusMessage_t *pMS;
- MessageBodyModuleStatusMessage_t *pMsg = 0; /* Type to decode */
- long Horizontal, Vertical;
- int numbits, bit;
- BYTE ModuleState;
- /* Decode */
- if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
- &asn_DEF_MessageBodyModuleStatusMessage,
- (void **)&pMsg) == ASN_ERR_NONE)
- {
- for(i = 0; i < pMsg->list.count; i++)
- {
- pMS = (ModuleStatusMessage_t *)pMsg->list.array[i];
- asn_INTEGER2long(&pMS->module_HorizontalQty, &Horizontal);
- asn_INTEGER2long(&pMS->module_VerticalQty, &Vertical);
- numbits = DATEX_NUMBITS(pMS->module_Status);
- if (numbits >= INT_VMS_MAX_MODULE)
- {
- numbits = INT_VMS_MAX_MODULE;
- LERROR("Module size overflow: %d, %d", numbits, INT_VMS_MAX_MODULE);
- }
- memset((char*)m_pState->ModuleStatus, 0x00, sizeof(m_pState->ModuleStatus));
- if ((Horizontal * Vertical) == numbits)
- {
- ModuleState = vms_normal;
- for (bit = 0; bit < numbits; bit++)
- {
- int nOnOff = ASN1C_CheckBitString(&pMS->module_Status, bit);
- if (nOnOff == bit_disable)
- {
- ModuleState = vms_error;
- }
- else
- {
- VComm_SetBitString((char*)m_pState->ModuleStatus, bit);
- }
- }
- if ((m_pState != NULL) && (numbits < INT_VMS_MAX_MODULE))
- {
- m_pState->ModuleHorizontal = Horizontal;
- m_pState->ModuleVertical = Vertical;
- }
- }
- else
- {
- ModuleState = vms_unknown;
- m_pState->ModuleHorizontal = 0;
- m_pState->ModuleVertical = 0;
- }
- m_pState->ModuleState = ModuleState;
- if (FLogFile->FLogCfg.Debug)
- {
- LDEBUG(" module_VerticalQty x HorizontalQty(Cnt): %d x %d (%d)", Vertical, Horizontal, numbits);
- //LDEBUG(" module_HorizontalQty: %d", Horizontal);
- //LDEBUG("module_Status numbits: %d", numbits);
- for (int v = 0, bit=0; v < Vertical; v++)
- {
- AnsiString sModule = "";
- for(int h = 0; h < Horizontal; h++)
- {
- int nM = VComm_GetBitString((char*)m_pState->ModuleStatus, v*h);
- sModule += String(nM);
- }
- LDEBUG("%d, %s", v, sModule.c_str());
- }
- }
- }/* end for */
- result = SYS_ERR_NONE;
- }
- else
- {
- result = SYS_ERR_MESSAGE_DECODE;
- }/* end if */
- if(pMsg)
- {
- ASN1C_Free(&asn_DEF_MessageBodyModuleStatusMessage, pMsg);
- }
- FModState = true;
- }
- break;
- case OBJ_DotStatus: /* 도트상태정보 */
- LINFO(" *** OBJ_DotStatus");
- result = SYS_ERR_NONE;
- break;
- case OBJ_ScreenStatus: /* 표출상태정보 */
- {
- LINFO(" *** OBJ_ScreenStatus");
- ScreenStatusMessage_t *pMS;
- MessageBodyScreenStatusMessage_t *pMsg = 0; /* Type to decode */
- /* Decode */
- if (ANY_to_type(&pData->datexPublish_Type.choice.datexPublish_Data.endApplication_Message_msg,
- &asn_DEF_MessageBodyScreenStatusMessage,
- (void **)&pMsg) == ASN_ERR_NONE)
- {
- BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
- int eLen;
- for(i = 0; i < pMsg->list.count; i++)
- {
- pMS = (ScreenStatusMessage_t *)pMsg->list.array[i];
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pMS->screen_StatusData,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -14;
- break;
- }
- //pMS->capture_Time;
- //pMS->screen_StatusData;
- }/* end for */
- result = SYS_ERR_NONE;
- }
- else
- {
- result = SYS_ERR_MESSAGE_DECODE;
- }/* end if */
- if (pMsg)
- {
- ASN1C_Free(&asn_DEF_MessageBodyScreenStatusMessage, pMsg);
- }
- }
- //result = SYS_ERR_NONE;
- break;
- case OBJ_DisplayingData: /* 표출정보 */
- {
- LINFO(" *** OBJ_DisplayingData");
- }
- result = SYS_ERR_NONE;
- break;
- default:
- LINFO(" *** Unknown PublicationData: %u", Message_id);
- result = SYS_ERR_UNKNOWN_MESSAGE_ID;
- break;
- }/* end switch */
- if (FGenState && FModState && FPowState)
- {
- FGenState = FModState = FPowState = false;
- POST_MSG(MAINHANDLE, WM_VMS_THREAD, WM_VMS_STATE_STTS, (int)FVmsObj);
- }
- break;
- default:
- result = SYS_ERR_DATEXPUBLISH_TYPE;
- break;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 패킷 송신
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::SendPacket(void *Data, int Len, BYTE cmd)
- {
- FErrLine = 9;
- int result;
- DatexDataPacket_t *pMsg; /* Type to encode */
- asn_enc_rval_t er; /* Encoder return value */
- BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
- WORD crc;
- int eLen, wLen;
- result = SYS_ERR_OTHER;
- pMsg = (DatexDataPacket_t *)ASN1C_Alloc(sizeof(DatexDataPacket_t));
- if (pMsg == NULL)
- {
- return SYS_ERR_INTERNAL;
- }
- if (asn_long2INTEGER(&pMsg->datex_Version_number, datex_Version_number_version1) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
- return -1;
- }
- if (OCTET_STRING_fromBuf(&pMsg->datex_Data, (char *)Data, Len) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
- return -2;
- }
- crc = ASN1C_MakeCrc16(Data, Len);
- crc = CommUtil_WordSwap(crc);
- if (OCTET_STRING_fromBuf(&pMsg->datex_Crc_nbr, (char *)&crc, sizeof(crc)) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
- return -3;
- }
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_DatexDataPacket, pMsg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("SendPacket: Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_DATEXDATAPACKET_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- wLen = TcpClient->SendBuf(MsgBuf, eLen);
- LINFO("Send: %d/%d, cmd: %02X", eLen, wLen, cmd);
- if (eLen == wLen)
- {
- --m_Client.iRetry;
- m_Client.cTimer = Now();
- result = SYS_ERR_NONE;
- LogData("SEND", (char*)MsgBuf, eLen);
- }
- else
- {
- LERROR("TCP_Write error(%d %d)", eLen, wLen);
- result = SYS_ERR_WRITE_LENGTH;
- LogData("SEND Fail", (char*)MsgBuf, eLen);
- }
- }
- ASN1C_Free(&asn_DEF_DatexDataPacket, pMsg);
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 송신 패킷 처리
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::ProcessCommand(BYTE cmd, CLIENT_ARGUMENT *arg)
- {
- FErrLine = 10;
- C2CAuthenticatedMessage_t *pMsg; /* Type to encode */
- asn_enc_rval_t er; /* Encoder return value */
- BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
- int eLen;
- int result;
- Word year, month, day;
- Word hour, min, sec, msec;
- result = SYS_ERR_OTHER;
- Now().DecodeDate(&year, &month, &day);
- Now().DecodeTime(&hour, &min, &sec, &msec);
- pMsg = (C2CAuthenticatedMessage_t *)ASN1C_Alloc(sizeof(C2CAuthenticatedMessage_t));
- if(pMsg == NULL)
- {
- return SYS_ERR_INTERNAL;
- }
- /* 공통부분 */
- BYTE auth = cmd;
- if (FVmsObj->FIsOldProtocol)
- {
- if (auth == AI_Subscription) auth = 0x05;
- else
- if (auth == AI_Login) auth = 0x01;
- }
- if (OCTET_STRING_fromBuf(&pMsg->datex_AuthenticationInfo_text, (char *)&auth, sizeof(auth)) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -1;
- }
- if (asn_long2INTEGER(&pMsg->datex_DataPacket_number, m_DataPacketNumber++) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -2;
- }
- pMsg->datex_DataPacketPriority_number = 0;
- /* Header Options */
- /* pMsg->options.datex_Origin_text */
- pMsg->options.datex_OriginAddress_location = (OCTET_STRING_t *)ASN1C_Alloc(sizeof(OCTET_STRING_t));
- if (pMsg->options.datex_OriginAddress_location == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -3;
- }
- if (OCTET_STRING_fromString(pMsg->options.datex_OriginAddress_location, m_Client.LocalDomainNm.c_str()) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -4;
- }
- /* pMsg->options.datex_Sender_text */
- /* pMsg->options.datex_SenderAddress_location */
- /* pMsg->options.datex_Destination_text */
- #if 1
- pMsg->options.datex_SenderAddress_location = (OCTET_STRING_t *)ASN1C_Alloc(sizeof(OCTET_STRING_t));
- if (pMsg->options.datex_SenderAddress_location == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -5;
- }
- if (OCTET_STRING_fromString(pMsg->options.datex_SenderAddress_location, m_Client.LocalDomainNm.c_str()) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -6;
- }
- #endif
- pMsg->options.datex_DestinationAddress_location = (OCTET_STRING_t *)ASN1C_Alloc(sizeof(OCTET_STRING_t));
- if (pMsg->options.datex_DestinationAddress_location == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -5;
- }
- if (OCTET_STRING_fromString(pMsg->options.datex_DestinationAddress_location, m_Client.DomainNm.c_str()) != ASN_ERR_NONE)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -6;
- }
- /* pMsg->options.datex_Cost->amount_Currency_code */
- /* pMsg->options.datex_Cost->amount_Factor_quantity */
- /* pMsg->options.datex_Cost->amount_Quantity_quantity */
- pMsg->options.datex_DataPacket_time = (Time_t *)ASN1C_Alloc(sizeof(Time_t));
- if (pMsg->options.datex_DataPacket_time == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -7;
- }
- ////2016-11-17 13:08:25
- #if 0
- year = 2016;
- month = 11;
- day = 17;
- hour = 13;
- min = 8;
- sec = 25;
- #endif
- pMsg->options.datex_DataPacket_time->time_Year_qty = (long *)ASN1C_Alloc(sizeof(long));
- if (pMsg->options.datex_DataPacket_time->time_Year_qty == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -8;
- }
- *pMsg->options.datex_DataPacket_time->time_Year_qty = year;
- pMsg->options.datex_DataPacket_time->time_Month_qty = (long *)ASN1C_Alloc(sizeof(long));
- if (pMsg->options.datex_DataPacket_time->time_Month_qty == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -9;
- }
- *pMsg->options.datex_DataPacket_time->time_Month_qty = month;
- pMsg->options.datex_DataPacket_time->time_Day_qty = (long *)ASN1C_Alloc(sizeof(long));
- if (pMsg->options.datex_DataPacket_time->time_Day_qty == NULL)
- {
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return -10;
- }
- *pMsg->options.datex_DataPacket_time->time_Day_qty = day;
- pMsg->options.datex_DataPacket_time->time_Hour_qty = hour;
- pMsg->options.datex_DataPacket_time->time_Minute_qty = min;
- pMsg->options.datex_DataPacket_time->time_Second_qty = sec;
- pMsg->options.datex_DataPacket_time->time_SecondFractions_zz.present = time_SecondFractions_PR_milliseconds;
- pMsg->options.datex_DataPacket_time->time_SecondFractions_zz.choice.milliseconds = 0;
- #if 0
- pMsg->options.datex_DataPacket_time->time_Timezone = (Timezone_t*)ASN1C_Alloc(sizeof(Timezone_t));
- if (pMsg->options.datex_DataPacket_time->time_Timezone)
- {
- pMsg->options.datex_DataPacket_time->time_Timezone->timezone_Hour_qty = 9;
- pMsg->options.datex_DataPacket_time->time_Timezone->time_Minute_qty = 0;
- }
- #endif
- /* pMsg->options.datex_DataPacket_time->time_Timezone->timezone_Hour_qty */
- /* pMsg->options.datex_DataPacket_time->time_Timezone->time_Minute_qty */
- switch(cmd)
- {
- case AI_Login: /* 서버에 접속하기 위한 클라이언트의 로그인 데이터 패킷 */
- LINFO(">>> AI_Login 전송");
- pMsg->pdu.present = PDUs_PR_login;
- if ((result = CommandLogin(&pMsg->pdu.choice.login)) != SYS_ERR_NONE)
- {
- SERROR("CommandLogin result failed: %d", result);
- result = -12;
- }
- break;
- case AI_FrED: /* 서버와 클라이언트의 연결을 유지하기 위한 확인 데이터 패킷 */
- LINFO(">>> AI_FrED 전송");
- pMsg->pdu.present = PDUs_PR_fred;
- //TODO: arg==NULL인데 사용하고 있음.......
- #if 0
- if (asn_long2INTEGER(&pMsg->pdu.choice.fred, arg->FrED) != ASN_ERR_NONE)
- {
- result = -13;
- break;
- }
- #endif
- result = SYS_ERR_NONE;
- break;
- case AI_Logout: /* 접속을 종료하기 위한 클라이언트의 로그아웃 데이터 패킷 */
- LINFO(">>> AI_Logout 전송");
- pMsg->pdu.present = PDUs_PR_logout;
- #if 0
- if (asn_long2INTEGER(&pMsg->pdu.choice.logout, arg->Logout) != ASN_ERR_NONE)
- {
- result = -14;
- break;
- }
- #endif
- result = SYS_ERR_NONE;
- break;
- case AI_Subscription: /* 클라이언트가 서버에 정보를 요청할 경우 송신하는 데이터 패킷 */
- LINFO(">>> AI_Subscription 전송");
- pMsg->pdu.present = PDUs_PR_subscripiton;
- if ((result = CommandSubscription(&pMsg->pdu.choice.subscripiton, arg->Subscription)) != SYS_ERR_NONE)
- {
- SERROR("CommandSubscription result %d", result);
- result = -15;
- }
- break;
- case AI_Accept: /* 클라이언트의 요청에 대한 수용 */
- LINFO(">>> AI_Accept 전송");
- pMsg->pdu.present = PDUs_PR_accept;
- if (asn_long2INTEGER(&pMsg->pdu.choice.accept.datexAccept_Packet_nbr, arg->Accept) != ASN_ERR_NONE)
- {
- result = -16;
- break;
- }
- pMsg->pdu.choice.accept.datexAccept_Type.present = datexAccept_Type_PR_publication;
- result = SYS_ERR_NONE;
- break;
- case AI_Reject: /* 클라이언트의 요청에 대한 거부 */
- LINFO(">>> AI_Reject 전송");
- pMsg->pdu.present = PDUs_PR_reject;
- if (asn_long2INTEGER(&pMsg->pdu.choice.reject.datexReject_Packet_nbr, arg->Reject.nbr) != ASN_ERR_NONE)
- {
- result = -17;
- break;
- }
- pMsg->pdu.choice.reject.datexReject_Type.present = RejectType_PR_datexReject_Publication_cd;
- if (asn_long2INTEGER(&pMsg->pdu.choice.reject.datexReject_Type.choice.datexReject_Publication_cd,
- arg->Reject.code) != ASN_ERR_NONE)
- {
- result = -18;
- break;
- }
- /* pMsg->pdu.choice.reject.datexReject_AlternateRequest */
- result = SYS_ERR_NONE;
- break;
- default:
- LERROR(">>> Unknown command(ProcessCommand)");
- result = SYS_ERR_UNKNOWN_COMMAND;
- break;
- }
- if (result == SYS_ERR_NONE)
- {
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_C2CAuthenticatedMessage, pMsg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("ProcessCommand: Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_C2CAUTHENTICATEDMESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- result = SendPacket(MsgBuf, eLen, cmd);
- }
- }
- ASN1C_Free(&asn_DEF_C2CAuthenticatedMessage, pMsg);
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 송신 패킷 생성
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::CommandLogin(Login_t *pLogin)
- {
- FErrLine = 11;
- int result;
- OBJECT_IDENTIFIER_t *pOid;
- int oid[3] = {2, 1, 1};
- result = SYS_ERR_OTHER;
- //VMS2000001
- //LocalDomainName
- if (OCTET_STRING_fromString(&pLogin->datex_Sender_txt, m_Client.LocalDomainNm.c_str()) != ASN_ERR_NONE)
- {
- return -1;
- }
- //DomainName=VmsId
- //m_Client.DomainNm = "00000000";
- if (OCTET_STRING_fromString(&pLogin->datex_Destinatin_txt, m_Client.DomainNm.c_str()) != ASN_ERR_NONE)
- {
- return -2;
- }
- //UserName=Site Name
- if (OCTET_STRING_fromString(&pLogin->datexLogin_UserName_txt, m_Client.UserName.c_str()) != ASN_ERR_NONE)
- {
- return -3;
- }
- //UserPswd
- if (OCTET_STRING_fromString(&pLogin->datexLogin_Password_txt, m_Client.UserPasswd.c_str()) != ASN_ERR_NONE)
- {
- return -4;
- }
- pOid = (OBJECT_IDENTIFIER_t *)ASN1C_Alloc(sizeof(OBJECT_IDENTIFIER_t));
- if (pOid == NULL)
- {
- return -5;
- }
- if (ASN1C_SetObjectIdentifier(pOid, oid, sizeof(oid)) != ASN_ERR_NONE)
- {
- return -6;
- }
- if (ASN_SEQUENCE_ADD(&pLogin->datexLogin_EncodingRules_id_zz, pOid)!= ASN_ERR_NONE)
- {
- return -7;
- }
- pLogin->datexLogin_HearteatDurationMax_qty = m_Client.MaxHeartbeatTime;
- pLogin->datexLogin_ResponseTimeOut_qty = m_Client.TimeOut;
- if (asn_long2INTEGER(&pLogin->datexLogin_Initiator_cd, datexLogin_Initiator_cd_clientInitiated) != ASN_ERR_NONE)
- {
- return -8;
- }
- pLogin->datexLogin_DatagramSize_qty = m_Client.DatagramSize;
- result = SYS_ERR_NONE;
- #if 0
- {
- long lv;
- int arcs[10];
- int count, i, j;
- OBJECT_IDENTIFIER_t *pOid;
- LDEBUG("datex_Sender_txt : %s", pLogin->datex_Sender_txt.buf);
- LDEBUG("datex_Destinatin_txt : %s", pLogin->datex_Destinatin_txt.buf);
- LDEBUG("datexLogin_UserName_txt : %s", pLogin->datexLogin_UserName_txt.buf);
- LDEBUG("datexLogin_Password_txt : %s", pLogin->datexLogin_Password_txt.buf);
- LDEBUG("datexLogin_HearteatDurationMax_qty : %d", pLogin->datexLogin_HearteatDurationMax_qty);
- LDEBUG("datexLogin_ResponseTimeOut_qty : %d", pLogin->datexLogin_ResponseTimeOut_qty);
- asn_INTEGER2long(&pLogin->datexLogin_Initiator_cd, &lv);
- LDEBUG("datexLogin_Initiator_cd : %d", lv);
- LDEBUG("datexLogin_DatagramSize_qty : %d", pLogin->datexLogin_DatagramSize_qty);
- for (i = 0; i < pLogin->datexLogin_EncodingRules_id_zz.list.count; i++)
- {
- LDEBUG("datexLogin_EncodingRules_id: [%d]", i);
- pOid = (OBJECT_IDENTIFIER_t *)pLogin->datexLogin_EncodingRules_id_zz.list.array[i];
- count = OBJECT_IDENTIFIER_get_arcs(pOid, arcs, sizeof(int), 10);
- for (j = 0; j < count && j < 10; j++)
- {
- LDEBUG("[%d]: %d", j, arcs[j]);
- }
- }
- }
- #endif
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 송신 패킷 생성
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::CommandSubscription(Subscription_t *pSubscripiton, SUBSCRIPTION_ARGUMENT Arg)
- {
- FErrLine = 12;
- int result;
- SubscriptionData_t *pData;
- asn_enc_rval_t er; /* Encoder return value */
- BYTE MsgBuf[CLIENT_MSG_BUF_SIZE];
- int eLen;
- int i, count = 0;
- result = SYS_ERR_OTHER;
- if (asn_long2INTEGER(&pSubscripiton->datexSubscribe_Serial_nbr, Arg.nbr) != ASN_ERR_NONE)
- {
- return -1;
- }
- pSubscripiton->datexSubscribe_Type.present = SubscriptionType_PR_subscription;
- pData = &pSubscripiton->datexSubscribe_Type.choice.subscription;
- if (FVmsObj->FIsOldProtocol)
- pData->datexSubscribe_Persistent_bool = true;
- else
- pData->datexSubscribe_Persistent_bool = false;
- if (asn_long2INTEGER(&pData->datexSubscribe_Status_cd, datexSubscribe_Status_cd_new) != ASN_ERR_NONE)
- {
- return -3;
- }
- switch(Arg.mode)
- {
- case SubscriptionMode_PR_single:
- pData->datexSubscribe_Mode.present = SubscriptionMode_PR_single;
- break;
- case SubscriptionMode_PR_event_driven:
- pData->datexSubscribe_Mode.present = SubscriptionMode_PR_event_driven;
- /* Registered_PR_continuous 1 */
- /* Registered_PR_daily 2 */
- pData->datexSubscribe_Mode.choice.event_driven.present = Registered_PR_continuous;
- pData->datexSubscribe_Mode.choice.event_driven.choice.continuous.datexRegistered_UpdateDelay_qty = Arg.delay;
- /* pData->datexSubscribe_Mode.choice.event_driven.choice.continuous.datexRegistered_StartTime */
- /* pData->datexSubscribe_Mode.choice.event_driven.choice.continuous.datexRegistered_EndTime */
- break;
- case SubscriptionMode_PR_periodic:
- pData->datexSubscribe_Mode.present = SubscriptionMode_PR_periodic;
- /* Registered_PR_continuous 1 */
- /* Registered_PR_daily 2 */
- pData->datexSubscribe_Mode.choice.periodic.present = Registered_PR_continuous;
- pData->datexSubscribe_Mode.choice.periodic.choice.continuous.datexRegistered_UpdateDelay_qty = Arg.delay;
- /* pData->datexSubscribe_Mode.choice.periodic.choice.continuous.datexRegistered_StartTime */
- /* pData->datexSubscribe_Mode.choice.periodic.choice.continuous.datexRegistered_EndTime */
- break;
- default:
- pData->datexSubscribe_Mode.present = SubscriptionMode_PR_single;
- break;
- }
- if (asn_long2INTEGER(&pData->datexSubscribe_PublishFormat_cd, datexSubscribe_PublishFormat_cd_dataPacket) != ASN_ERR_NONE)
- {
- return -4;
- }
- pData->datexSubscription_Priority_nbr = 1;
- pData->datexSubscribe_Guarantee_bool = true;
- #if 0
- {
- long lv;
- asn_INTEGER2long(&pSubscripiton->datexSubscribe_Serial_nbr, &lv);
- LDEBUG(" SubscriptionMode: %d", Arg.mode);
- LDEBUG("datexSubscribe_Serial_nbr: %u", lv);
- LDEBUG(" datexSubscribe_Type: %d", pSubscripiton->datexSubscribe_Type.present);
- switch(pSubscripiton->datexSubscribe_Type.present)
- {
- case SubscriptionType_PR_subscription:
- asn_INTEGER2long(&pData->datexSubscribe_Status_cd, &lv);
- LDEBUG("datexSubscribe_Persistent_bool: %d", pData->datexSubscribe_Persistent_bool);
- LDEBUG(" datexSubscribe_Status_cd: %d", lv);
- LDEBUG(" datexSubscribe_Mode: %d", pData->datexSubscribe_Mode.present);
- switch(pData->datexSubscribe_Mode.present)
- {
- case SubscriptionMode_PR_single:
- break;
- case SubscriptionMode_PR_event_driven:
- case SubscriptionMode_PR_periodic:
- {
- Registered_t *pRegistered;
- int numbits, bit;
- if(pData->datexSubscribe_Mode.present == SubscriptionMode_PR_event_driven)
- {
- pRegistered = &pData->datexSubscribe_Mode.choice.event_driven;
- }
- else
- {
- pRegistered = &pData->datexSubscribe_Mode.choice.periodic;
- }
- LDEBUG("Registered: %d", pRegistered->present);
- switch(pRegistered->present)
- {
- case Registered_PR_continuous:
- LDEBUG("datexRegistered_UpdateDelay_qty: %u", pRegistered->choice.continuous.datexRegistered_UpdateDelay_qty);
- if (pRegistered->choice.continuous.datexRegistered_StartTime)
- {
- LOG_WriteTime(pRegistered->choice.continuous.datexRegistered_StartTime);
- }
- if (pRegistered->choice.continuous.datexRegistered_EndTime)
- {
- LOG_WriteTime(pRegistered->choice.continuous.datexRegistered_EndTime);
- }
- break;
- case Registered_PR_daily:
- LDEBUG("datexRegistered_UpdateDelay_qty: %u", pRegistered->choice.daily.datexRegistered_UpdateDelay_qty);
- numbits = DATEX_NUMBITS(pRegistered->choice.daily.datexRegistered_DaysOfWeek_cd);
- LDEBUG("datexRegistered_DaysOfWeek_cd numbits: %d", numbits);
- for (bit = 0; bit < numbits; bit++)
- {
- LDEBUG("[%d]: %d", bit, ASN1C_CheckBitString(&pRegistered->choice.daily.datexRegistered_DaysOfWeek_cd, bit));
- }
- if (pRegistered->choice.daily.datexRegistered_StartDate)
- {
- LOG_WriteTime(pRegistered->choice.daily.datexRegistered_StartDate);
- }
- if (pRegistered->choice.daily.datexRegistered_EndDate)
- {
- LOG_WriteTime(pRegistered->choice.daily.datexRegistered_EndDate);
- }
- if (pRegistered->choice.daily.datexRegistered_StartTime)
- {
- LOG_WriteTime(pRegistered->choice.daily.datexRegistered_StartTime);
- }
- if (pRegistered->choice.daily.datexRegistered_Duration_qty)
- {
- //LDEBUG("datexRegistered_Duration_qty: %u", *pRegistered->choice.daily.datexRegistered_Duration_qty);
- }
- break;
- default:
- LDEBUG("Unknown Registered %d", pRegistered->present);
- break;
- }/* end swtich */
- }
- break;
- default :
- LDEBUG("Unknown datexSubscribe_Mode %d", pData->datexSubscribe_Mode.present);
- break;
- }/* end switch */
- asn_INTEGER2long(&pData->datexSubscribe_PublishFormat_cd, &lv);
- LDEBUG("datexSubscribe_PublishFormat_cd: %d", lv);
- LDEBUG(" datexSubscription_Priority_nbr: %d", pData->datexSubscription_Priority_nbr);
- LDEBUG(" datexSubscribe_Guarantee_bool: %d", pData->datexSubscribe_Guarantee_bool);
- break;
- case SubscriptionType_PR_datexSubscribe_CancelReason_cd:
- LDEBUG("datexSubscribe_CancelReason_cd: %d", pSubscripiton->datexSubscribe_Type.choice.datexSubscribe_CancelReason_cd);
- break;
- default:
- LDEBUG("Unknown datexSubscribe_Type %d", pSubscripiton->datexSubscribe_Type.present);
- break;
- }/* end switch */
- }
- #endif
- if (FVmsObj->FIsOldProtocol)
- {
- int s_arcs[6];
- int *arcs = s_arcs;
- s_arcs[0] = 1;
- s_arcs[1] = 0;
- s_arcs[2] = 14827;
- s_arcs[3] = 2;
- s_arcs[4] = 0;
- s_arcs[5] = Arg.obj;
- int count = OBJECT_IDENTIFIER_set_arcs(&pData->datexSubscribe_Pdu.endApplication_Message_id, arcs, sizeof(*arcs), 6);
- }
- else
- {
- ASN1C_SetObjId(&pData->datexSubscribe_Pdu.endApplication_Message_id, Arg.obj);
- }
- switch(Arg.obj)
- {
- case OBJ_GeneralStatus: /* 기본상태정보 */
- {
- LINFO(" *** OBJ_GeneralStatus 전송");
- GeneralStatusMessage_t *pGS;
- MessageBodyGeneralStatusMessage_t Msg; /* Type to encode */
- memset((void *)&Msg, 0x00, sizeof(MessageBodyGeneralStatusMessage_t));
- for ( i = 0; i < count; i++)
- {
- if ((pGS = (GeneralStatusMessage_t *)ASN1C_Alloc(sizeof(GeneralStatusMessage_t))) == NULL)
- {
- result = -10;
- break;
- }
- if(ASN_SEQUENCE_ADD(&Msg, pGS)!= ASN_ERR_NONE)
- {
- result = -11;
- break;
- }
- }/* end for */
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_MessageBodyGeneralStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_MESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
- if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
- {
- result = -12;
- break;
- }
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -13;
- break;
- }
- result = SYS_ERR_NONE;
- }/* end if */
- ASN1C_FreeC(&asn_DEF_MessageBodyGeneralStatusMessage, &Msg);
- }
- break;
- #if 1
- case OBJ_PowerStatus: /* 전원상태정보 */
- {
- LINFO(" *** OBJ_PowerStatus 전송");
- PowerStatusMessage_t *pMS;
- MessageBodyPowerStatusMessage_t Msg;
- memset((void *)&Msg, 0x00, sizeof(MessageBodyPowerStatusMessage_t));
- for ( i = 0; i < count; i++)
- {
- if ((pMS = (PowerStatusMessage_t *)ASN1C_Alloc(sizeof(PowerStatusMessage_t))) == NULL)
- {
- result = -30;
- break;
- }
- if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
- {
- result = -31;
- break;
- }
- }/* end for */
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_MessageBodyPowerStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_MESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
- if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
- {
- result = -32;
- break;
- }
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -33;
- break;
- }
- result = SYS_ERR_NONE;
- }/* end if */
- ASN1C_FreeC(&asn_DEF_MessageBodyPowerStatusMessage, &Msg);
- }
- break;
- #endif
- case OBJ_ModuleStatus: /* 모듈상태정보 */
- {
- LINFO(" *** OBJ_ModuleStatus 전송");
- ModuleStatusMessage_t *pMS;
- MessageBodyModuleStatusMessage_t Msg; /* Type to encode */
- memset((void *)&Msg, 0x00, sizeof(MessageBodyModuleStatusMessage_t));
- for ( i = 0; i < count; i++)
- {
- if ((pMS = (ModuleStatusMessage_t *)ASN1C_Alloc(sizeof(ModuleStatusMessage_t))) == NULL)
- {
- result = -30;
- break;
- }
- if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
- {
- result = -31;
- break;
- }
- }/* end for */
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_MessageBodyModuleStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_MESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
- if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
- {
- result = -32;
- break;
- }
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -33;
- break;
- }
- result = SYS_ERR_NONE;
- }/* end if */
- ASN1C_FreeC(&asn_DEF_MessageBodyModuleStatusMessage, &Msg);
- }
- break;
- case OBJ_DotStatus: /* 도트상태정보 */
- {
- LINFO(" *** OBJ_DotStatus 전송");
- DotStatusMessage_t *pMS;
- MessageBodyDotStatusMessage_t Msg; /* Type to encode */
- memset((void *)&Msg, 0x00, sizeof(MessageBodyDotStatusMessage_t));
- for ( i = 0; i < count; i++)
- {
- if ((pMS = (DotStatusMessage_t *)ASN1C_Alloc(sizeof(DotStatusMessage_t))) == NULL)
- {
- result = -30;
- break;
- }
- if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
- {
- result = -31;
- break;
- }
- }/* end for */
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_MessageBodyDotStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_MESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
- if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
- {
- result = -32;
- break;
- }
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -33;
- break;
- }
- result = SYS_ERR_NONE;
- }/* end if */
- ASN1C_FreeC(&asn_DEF_MessageBodyDotStatusMessage, &Msg);
- }
- break;
- case OBJ_ScreenStatus: /* 표출상태정보 */
- {
- LINFO(" *** OBJ_ScreenStatus 전송");
- ScreenStatusMessage_t *pMS;
- MessageBodyScreenStatusMessage_t Msg; /* Type to encode */
- memset((void *)&Msg, 0x00, sizeof(MessageBodyScreenStatusMessage_t));
- for ( i = 0; i < count; i++)
- {
- if ((pMS = (ScreenStatusMessage_t *)ASN1C_Alloc(sizeof(ScreenStatusMessage_t))) == NULL)
- {
- result = -30;
- break;
- }
- if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
- {
- result = -31;
- break;
- }
- }/* end for */
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_MessageBodyScreenStatusMessage, &Msg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_MESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
- if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
- {
- result = -32;
- break;
- }
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -33;
- break;
- }
- result = SYS_ERR_NONE;
- }/* end if */
- ASN1C_FreeC(&asn_DEF_MessageBodyScreenStatusMessage, &Msg);
- }
- break;
- case OBJ_DisplayingData: /* 표출정보 */
- {
- LINFO(" *** OBJ_DisplayingData 전송");
- DisplayingDataMessage_t *pMS;
- MessageBodyDisplayingDataMessage_t Msg; /* Type to encode */
- memset((void *)&Msg, 0x00, sizeof(MessageBodyDisplayingDataMessage_t));
- for ( i = 0; i < count; i++)
- {
- if ((pMS = (DisplayingDataMessage_t *)ASN1C_Alloc(sizeof(DisplayingDataMessage_t))) == NULL)
- {
- result = -30;
- break;
- }
- if (ASN_SEQUENCE_ADD(&Msg, pMS)!= ASN_ERR_NONE)
- {
- result = -31;
- break;
- }
- }/* end for */
- /* Encode */
- memset(MsgBuf, 0x00, sizeof(MsgBuf));
- eLen = -1;
- er = der_encode_to_buffer(&asn_DEF_MessageBodyDisplayingDataMessage, &Msg, MsgBuf, sizeof(MsgBuf));
- if (er.encoded == -1)
- {
- /*
- * Failed to encode the data.
- */
- SERROR("Cannot encode %s", er.failed_type->name);
- result = SYS_ERR_MESSAGE_ENCODE;
- }
- else
- {
- /* Return the number of bytes */
- eLen = er.encoded;
- pData->datexSubscribe_Pdu.endApplication_Message_msg.buf = (uint8_t *)ASN1C_Alloc(eLen);
- if (pData->datexSubscribe_Pdu.endApplication_Message_msg.buf == NULL)
- {
- result = -32;
- break;
- }
- if (OCTET_STRING_fromBuf((OCTET_STRING_t *)&pData->datexSubscribe_Pdu.endApplication_Message_msg,
- (char *)MsgBuf, eLen) != ASN_ERR_NONE)
- {
- result = -33;
- break;
- }
- result = SYS_ERR_NONE;
- }/* end if */
- ASN1C_FreeC(&asn_DEF_MessageBodyDisplayingDataMessage, &Msg);
- }
- break;
- default:
- result = SYS_ERR_UNKNOWN_MESSAGE_ID;
- break;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- * 클라이언트 처리 루틴
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::ClientStateMachine(void)
- {
- FErrLine = 13;
- int result;
- bool bRequest;
- result = SYS_ERR_NONE;
- //timeout check
- if (m_RxLen && (CommUtil_TimeDiff(m_rTimer) > m_Client.TimeOut))
- {
- m_RxLen = 0;
- ProcErrorState(TRUE, SYS_ERR_RECEIVE_TIMEOUT);
- return;
- }
- #if 0
- {
- switch (m_Client.State)
- {
- case CT_IDLE: LDETAIL("IDLE"); break;
- case CT_LOGIN: LDETAIL("LOGIN"); break;
- case CT_SECTION: LDETAIL("SECTION"); break;
- case CT_CLOSE: LDETAIL("CLOSE"); break;
- case CT_LOCAL_COMMAND: LDETAIL("LOCAL_COMMAND"); break;
- case CT_CLIENT_COMMAND: LDETAIL("CLIENT_COMMAND"); break;
- case CT_LOGIN_WAIT: LDETAIL("LOGIN_WAIT"); break;
- case CT_FRED_WAIT: LDETAIL("FRED_WAIT"); break;
- case CT_LOCAL_WAIT: LDETAIL("LOCAL_WAIT"); break;
- case CT_CLIENT_WAIT: LDETAIL("CLIENT_WAIT"); break;
- default: LDETAIL("Unknown State[%d]", m_Client.State); break;
- }
- }
- #endif
- switch(m_Client.State)
- {
- case CT_IDLE:
- if (CommUtil_TimeDiff(m_Client.sTimer) > CLIENT_CONNECT_RETRY_TIME)
- {
- m_Client.sTimer = Now();
- if (m_Client.iRetry <= 0)
- {
- m_Client.State = CT_CLOSE;
- }
- else
- {
- m_Client.State = CT_LOGIN;
- }
- }
- break;
- case CT_LOGIN:
- #if 1
- //TODO
- //용인거는 로그인 요청을 수행하지 않도록 한다.
- if ((result = ProcessCommand(AI_Login, NULL)) == SYS_ERR_NONE)
- {
- m_Client.State = CT_LOGIN_WAIT;
- }
- else
- {
- if (--m_Client.iRetry <= 0)
- {
- m_Client.State = CT_CLOSE;
- }
- }
- #else
- m_Client.State = CT_LOCAL_COMMAND;
- #endif
- break;
- case CT_SECTION:
- if (CommUtil_TimeDiff(m_WatchDogTimer) > FWatchDogTime)
- {
- LERROR("WatchDog Time over");
- m_Client.State = CT_CLOSE;
- break;
- }
- if (CommUtil_TimeDiff(m_rTimer) > (m_Client.MaxHeartbeatTime/3))
- {
- if ((result = ProcessCommand(AI_FrED, NULL)) == SYS_ERR_NONE)
- {
- m_Client.State = CT_FRED_WAIT;
- }
- else
- {
- if (--m_Client.iRetry <= 0)
- {
- m_Client.State = CT_CLOSE;
- }
- }
- break;
- }
- if (CommUtil_TimeDiff(m_Client.sTimer) > m_Client.CycleTime)
- {
- m_Client.sTimer = Now();
- m_Client.State = CT_LOCAL_COMMAND;
- m_Registered.GeneralStatus = false;
- m_Registered.ModuleStatus = false;
- m_Registered.PowerStatus = false;
- }
- else
- {
- m_Client.State = CT_LOCAL_COMMAND;
- }
- break;
- case CT_CLOSE:
- Terminate();
- break;
- case CT_REGIST:
- if (CommUtil_TimeDiff(m_Client.sTimer) > m_Client.CycleTime)
- {
- m_Client.sTimer = Now();
- m_Client.State = CT_LOCAL_COMMAND;
- m_SubRegistered.GeneralStatus = false;
- m_SubRegistered.PowerStatus = false;
- m_SubRegistered.ModuleStatus = false;
- m_SubRegistered.DotStatus = false;
- m_SubRegistered.ScreenStatus = false;
- m_SubRegistered.DisplayingData = false;
- }
- else
- {
- m_Client.State = CT_REGIST_COMMAND;
- }
- break;
- case CT_REGIST_COMMAND:
- result = CheckRegistData(bRequest);
- if (bRequest)
- {
- if (result == SYS_ERR_NONE)
- {
- m_Client.State = CT_REGIST_WAIT;
- }
- else
- {
- if (--m_Client.iRetry <= 0)
- {
- m_Client.State = CT_CLOSE;
- }
- }
- }
- else
- {
- m_Client.State = CT_LOCAL_COMMAND;
- }
- break;
- case CT_REGIST_WAIT:
- if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
- {
- if (m_Client.iRetry > 0)
- {
- m_Client.State = CT_IDLE;
- }
- else
- {
- m_Client.State = CT_CLOSE;
- }
- }
- break;
- case CT_LOCAL_COMMAND:
- result = CheckLocalData(bRequest);
- if (bRequest)
- {
- if (result == SYS_ERR_NONE)
- {
- m_Client.State = CT_LOCAL_WAIT;
- }
- else
- {
- if (--m_Client.iRetry <= 0)
- {
- m_Client.State = CT_CLOSE;
- }
- }
- }
- else
- {
- m_Client.State = CT_CLIENT_COMMAND;
- }
- break;
- case CT_CLIENT_COMMAND:
- result = CheckClientData(bRequest);
- if (bRequest)
- {
- if (result == SYS_ERR_NONE)
- {
- m_Client.State = CT_CLIENT_WAIT;
- }
- else
- {
- if (--m_Client.iRetry <= 0)
- {
- m_Client.State = CT_CLOSE;
- }
- }
- }
- else
- {
- m_Client.State = CT_SECTION;
- }
- break;
- case CT_LOGIN_WAIT:
- if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
- {
- if (m_Client.iRetry > 0)
- {
- m_Client.State = CT_IDLE;
- }
- else
- {
- m_Client.State = CT_CLOSE;
- }
- }
- break;
- case CT_FRED_WAIT:
- if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
- {
- if (m_Client.iRetry > 0)
- {
- m_Client.State = CT_SECTION;
- }
- else
- {
- m_Client.State = CT_CLOSE;
- }
- }
- break;
- case CT_LOCAL_WAIT:
- if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
- {
- if (m_Client.iRetry > 0){
- m_Client.State = CT_LOCAL_COMMAND;
- }else{
- m_Client.State = CT_CLOSE;
- }
- }
- break;
- case CT_CLIENT_WAIT:
- if (CommUtil_TimeDiff(m_Client.cTimer) > m_Client.TimeOut)
- {
- if (m_Client.iRetry > 0)
- {
- m_Client.State = CT_CLIENT_COMMAND;
- }
- else
- {
- m_Client.State = CT_CLOSE;
- }
- }
- break;
- default:
- m_Client.State = CT_IDLE;
- break;
- }/* end switch */
- if (result)
- {
- ProcErrorState(FALSE, result);
- }
- }
- //---------------------------------------------------------------------------
- /*
- * 에러 처리
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::ProcErrorState(bool flag, int result)
- {
- FErrLine = 14;
- #if 1
- {
- AnsiString strLog;
- switch(result)
- {
- case SYS_ERR_NONE: strLog.sprintf("no error [%d]", result); break;
- case SYS_ERR_OTHER: strLog.sprintf("other [%d]", result); break;
- case SYS_ERR_INTERNAL: strLog.sprintf("internal system error(kernel..) [%d]", result); break;
- case SYS_ERR_MEMORY: strLog.sprintf("memory overflow [%d]", result); break;
- case SYS_ERR_DATABASE: strLog.sprintf("system database error [%d]", result); break;
- case SYS_ERR_RESET: strLog.sprintf("system reset [%d]", result); break;
- case SYS_ERR_START: strLog.sprintf("system start [%d]", result); break;
- case SYS_ERR_OFFLINE: strLog.sprintf("offline [%d]", result); break;
- case SYS_ERR_NO_RES: strLog.sprintf("no response [%d]", result); break;
- case SYS_ERR_ACCESS: strLog.sprintf("access denied [%d]", result); break;
- case SYS_ERR_INVALID: strLog.sprintf("invalid data type [%d]", result); break;
- case SYS_ERR_UNKNOWN: strLog.sprintf("unknown data type [%d]", result); break;
- case SYS_ERR_WRITE_LENGTH: strLog.sprintf("write length [%d]", result); break;
- case SYS_ERR_PACKET_SIZE_LARGE: strLog.sprintf("packet size large [%d]", result); break;
- case SYS_ERR_INVALID_TAG: strLog.sprintf("invalid tag [%d]", result); break;
- case SYS_ERR_CRC: strLog.sprintf("crc [%d]", result); break;
- case SYS_ERR_LENGTH: strLog.sprintf("length [%d]", result); break;
- case SYS_ERR_ADDRESS: strLog.sprintf("address [%d]", result); break;
- case SYS_ERR_HEADER_OPTIONS: strLog.sprintf("header options [%d]", result); break;
- case SYS_ERR_UNKNOWN_COMMAND: strLog.sprintf("unknown command [%d]", result); break;
- case SYS_ERR_RECEIVE_TIMEOUT: strLog.sprintf("receive timeout [%d]", result); break;
- case SYS_ERR_DATA_SIZE: strLog.sprintf("data size [%d]", result); break;
- case SYS_ERR_MISMATCH: strLog.sprintf("mismatch command [%d]", result); break;
- case SYS_ERR_INVALID_PARA: strLog.sprintf("invalid parameter [%d]", result); break;
- case SYS_ERR_MOVEMENT: strLog.sprintf("movement [%d]", result); break;
- case SYS_ERR_UNAUTHORIZED: strLog.sprintf("unauthorized [%d]", result); break;
- case SYS_ERR_DATEXDATAPACKET_DECODE: strLog.sprintf("DatexDataPacket decode [%d]", result); break;
- case SYS_ERR_INVALID_VERSION_NUMBER: strLog.sprintf("invalid version number [%d]", result); break;
- case SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE: strLog.sprintf("C2CAuthenticatedMessage decode [%d]", result); break;
- case SYS_ERR_C2CAUTHENTICATEDMESSAGE: strLog.sprintf("C2CAuthenticatedMessage [%d]", result); break;
- case SYS_ERR_NOT_LOGIN: strLog.sprintf("Not login [%d]", result); break;
- case SYS_ERR_ACCEPT_TYPE: strLog.sprintf("Accept Type [%d]", result); break;
- case SYS_ERR_DATEXPUBLISH_FORMAT: strLog.sprintf("datexPublish Format [%d]", result); break;
- case SYS_ERR_DATEXPUBLISH_TYPE: strLog.sprintf("datexPublish Type [%d]", result); break;
- case SYS_ERR_UNKNOWN_MESSAGE_ID: strLog.sprintf("unknown Message id [%d]", result); break;
- case SYS_ERR_MESSAGE_DECODE: strLog.sprintf("Message decode [%d]", result); break;
- case SYS_ERR_DATEXDATAPACKET_ENCODE: strLog.sprintf("DatexDataPacket encode [%d]", result); break;
- case SYS_ERR_C2CAUTHENTICATEDMESSAGE_ENCODE: strLog.sprintf("C2CAuthenticatedMessage encode [%d]", result); break;
- case SYS_ERR_MESSAGE_ENCODE: strLog.sprintf("Message encode [%d]", result); break;
- case SYS_ERR_PUBLICATIONDATA_ENCODE: strLog.sprintf("PublicationData encode [%d]", result); break;
- case SYS_ERR_MISMATCH_DESTINATION_TEXT: strLog.sprintf("mismatch Destination text [%d]", result); break;
- case SYS_ERR_DATEXLOGIN_VALUE: strLog.sprintf("Login Value [%d]", result); break;
- case SYS_ERR_SUBSCRIPTION_TYPE: strLog.sprintf("SubscriptionType Type [%d]", result); break;
- case SYS_ERR_SUBSCRIPTION_STATUS_VALUE: strLog.sprintf("SubscriptionType Status Value [%d]", result); break;
- case SYS_ERR_SUBSCRIPTION_MODE_VALUE: strLog.sprintf("SubscriptionType Mode Value [%d]", result); break;
- case SYS_ERR_SUBSCRIPTION_FORMAT_VALUE: strLog.sprintf("SubscriptionType Format Value [%d]", result); break;
- case SYS_ERR_HANGUL_CONVERSION: strLog.sprintf("Hangul Conversion [%d]", result); break;
- case SYS_ERR_INVALID_TAGANDLEN: strLog.sprintf("invalid Tag And Len [%d]", result); break;
- case SYS_ERR_PUBLICATIONDATA_DECODE: strLog.sprintf("PublicationData decode [%d]", result); break;
- default: strLog.sprintf("Unknown Error [%d]", result); break;
- }
- if (flag){
- LERROR("Recv Error %s", strLog.c_str());
- }else{
- LERROR("Send Error %s", strLog.c_str());
- }
- }
- #endif
- }
- //---------------------------------------------------------------------------
- /*
- * 상태 변경
- * arguments
- * void
- * return
- * void
- */
- void __fastcall TVMSCThread::SetClientState(int ACase)
- {
- FErrLine = 15;
- switch(m_Client.State)
- {
- case CT_LOGIN_WAIT:
- if (ACase == SET_CLIENT_SUCC)
- {
- if (FVmsObj->FIsOldProtocol)
- {
- //정보요청을 등록해야함(기존 VMS)
- m_Client.State = CT_SECTION;//CT_REGIST; //TODO
- m_Client.sTimer = Now() - ITSCOMM_SECTIME(m_Client.CycleTime);
- }
- else
- {
- m_Client.State = CT_SECTION;
- m_Client.sTimer = Now() - ITSCOMM_SECTIME(m_Client.CycleTime);
- }
- }
- else
- if (ACase == SET_CLIENT_FAIL)
- {
- m_Client.State = CT_IDLE;
- }
- break;
- case CT_FRED_WAIT:
- m_Client.State = CT_SECTION;
- break;
- case CT_LOCAL_WAIT:
- if (ACase == SET_CLIENT_SUCC)
- {
- m_Client.State = CT_LOCAL_COMMAND;//CT_CLIENT_COMMAND;
- }
- else
- if (ACase == SET_CLIENT_FAIL)
- {
- m_Client.State = CT_LOCAL_COMMAND;
- }
- break;
- case CT_CLIENT_WAIT:
- if (ACase == SET_CLIENT_SUCC)
- {
- m_Client.State = CT_SECTION;
- }
- else
- if (ACase == SET_CLIENT_FAIL)
- {
- m_Client.State = CT_CLIENT_COMMAND;
- }
- break;
- case CT_REGIST_WAIT:
- if (ACase == SET_CLIENT_SUCC)
- {
- m_Client.State = CT_REGIST_COMMAND;
- }
- else
- if (ACase == SET_CLIENT_FAIL)
- {
- m_Client.State = CT_CLIENT_COMMAND;
- }
- break;
- default:
- break;
- }
- }
- //---------------------------------------------------------------------------
- /*
- *
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::CheckLocalData(bool &ARequest)
- {
- FErrLine = 16;
- int result;
- CLIENT_ARGUMENT arg;
- result = SYS_ERR_NONE;
- ARequest = false;
- if (FVmsObj->FIsStatusAuto) return result;
- memset(&arg, 0x00, sizeof(arg));
- memset(&m_Ctl, 0x00, sizeof(m_Ctl));
- if (m_Registered.GeneralStatus == false)
- {
- LINFO("**** GeneralStatus 요청");
- arg.Subscription.nbr = NBR_GENERAL_STATUS;
- arg.Subscription.mode = SubscriptionMode_PR_single;
- arg.Subscription.delay = 60;
- arg.Subscription.obj = OBJ_GeneralStatus;
- m_Ctl.obj = OBJ_GeneralStatus;
- m_Ctl.DataPacketNumber = m_DataPacketNumber;
- if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
- {
- //
- }
- ARequest = true;
- return result;
- }
- if (m_Registered.ModuleStatus == false)
- {
- LINFO("**** ModuleStatus 요청");
- arg.Subscription.nbr = NBR_MODULE_STATUS;
- arg.Subscription.mode = SubscriptionMode_PR_single;
- arg.Subscription.delay = 60;
- arg.Subscription.obj = OBJ_ModuleStatus;
- m_Ctl.obj = OBJ_ModuleStatus;
- m_Ctl.DataPacketNumber = m_DataPacketNumber;
- if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
- {
- //
- }
- ARequest = true;
- return result;
- }
- if (m_Registered.PowerStatus == false)
- {
- LINFO("**** PowerStatus 요청");
- arg.Subscription.nbr = NBR_POWER_STATUS;
- arg.Subscription.mode = SubscriptionMode_PR_single;
- arg.Subscription.delay = 60;
- arg.Subscription.obj = OBJ_PowerStatus;
- m_Ctl.obj = OBJ_PowerStatus;
- m_Ctl.DataPacketNumber = m_DataPacketNumber;
- if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
- {
- //
- }
- ARequest = true;
- return result;
- }
- #if 0
- if (m_Registered.RealTimeDisplayMessage == false)
- {
- LINFO("**** RealTimeDisplayMessage 요청");
- arg.Subscription.nbr = NBR_REALTIME_DISPLAY;
- arg.Subscription.mode = SubscriptionMode_PR_single;
- arg.Subscription.delay = 60;
- arg.Subscription.obj = OBJ_DisplayingData;
- m_Ctl.obj = OBJ_DisplayingData;
- m_Ctl.DataPacketNumber = m_DataPacketNumber;
- if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
- {
- //
- }
- ARequest = true;
- return result;
- }
- if (m_Registered.ScreenDataStatusMessage == false)
- {
- LINFO("**** ScreenDataStatusMessage 요청");
- arg.Subscription.nbr = NBR_SCREEN_STATUS;
- arg.Subscription.mode = SubscriptionMode_PR_single;
- arg.Subscription.delay = 60;
- arg.Subscription.obj = OBJ_ScreenStatus;
- m_Ctl.obj = OBJ_ScreenStatus;
- m_Ctl.DataPacketNumber = m_DataPacketNumber;
- if ((result = ProcessCommand(AI_Subscription, &arg)) == SYS_ERR_NONE)
- {
- //
- }
- ARequest = true;
- return result;
- }
- #endif
- return result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TVMSCThread::CheckRegistData(bool &ARequest)
- {
- FErrLine = 16;
- int result;
- CLIENT_ARGUMENT arg;
- result = SYS_ERR_NONE;
- ARequest = false;
- memset(&arg, 0x00, sizeof(arg));
- memset(&m_Ctl, 0x00, sizeof(m_Ctl));
- //TODO
- return result;
- if (m_SubRegistered.GeneralStatus == false)
- {
- m_Ctl.obj = OBJ_GeneralStatus;
- m_Ctl.DataPacketNumber = 15;
- SendSubRegister(OBJ_GeneralStatus);
- ARequest = true;
- return result;
- }
- if (m_SubRegistered.PowerStatus == false)
- {
- m_Ctl.obj = OBJ_PowerStatus;
- m_Ctl.DataPacketNumber = 16;
- SendSubRegister(OBJ_PowerStatus);
- ARequest = true;
- return result;
- }
- if (m_SubRegistered.ModuleStatus == false)
- {
- m_Ctl.obj = OBJ_ModuleStatus;
- m_Ctl.DataPacketNumber = 17;
- SendSubRegister(OBJ_ModuleStatus);
- ARequest = true;
- return result;
- }
- if (m_SubRegistered.DotStatus == false)
- {
- m_Ctl.obj = OBJ_DotStatus;
- m_Ctl.DataPacketNumber = 18;
- SendSubRegister(OBJ_DotStatus);
- ARequest = true;
- return result;
- }
- if (m_SubRegistered.ScreenStatus == false)
- {
- m_Ctl.obj = OBJ_ScreenStatus;
- m_Ctl.DataPacketNumber = 19;
- SendSubRegister(OBJ_ScreenStatus);
- ARequest = true;
- return result;
- }
- if (m_SubRegistered.DisplayingData == false)
- {
- m_Ctl.obj = OBJ_DisplayingData;
- m_Ctl.DataPacketNumber = 20;
- SendSubRegister(OBJ_DisplayingData);
- ARequest = true;
- return result;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- /*
- *
- * arguments
- * void
- * return
- * void
- */
- int __fastcall TVMSCThread::CheckClientData(bool &ARequest)
- {
- int result;
- result = SYS_ERR_NONE;
- ARequest = false;
- return result;
- }
- //---------------------------------------------------------------------------
- int TVMSCThread::LogData(char *ASndRcv, BYTE *AData, int ALen)
- {
- int result = -1;
- int nStep = 0;
- if (!FLogFile) return result;
- if (!FLogFile->FLogCfg.Data) return result;
- try
- {
- FLogFile->LogData(ASndRcv, AData, ALen);
- if (FVmsObj->FDispLog)
- {
- IPC_LOG_MESSAGE *pLog = &FLogMsg;//new IPC_LOG_MESSAGE;
- if (pLog)
- {
- pLog->Kind = eLOG_DATA;
- pLog->Flag = ASndRcv[0] == 'S' ? 1 : 2;
- pLog->Len = ALen > MAX_LOG_BUFFER ? MAX_LOG_BUFFER-1 : ALen;
- pLog->Tm = Now();
- pLog->Type = 'C';
- memset(pLog->Msg, 0x00, sizeof(pLog->Msg));
- memcpy(pLog->Msg, (char*)AData, pLog->Len);
- pLog->Obj = (void*)FVmsObj;
- DWORD dwResult = 0;
- LRESULT lResult = SEND_LOGMSG(FrmVmsLog->Handle, WM_LOG_DISPLAY, (WPARAM)pLog, NULL, SMTO_NORMAL, 2000, dwResult);
- if (lResult == 0)
- {
- dwResult = GetLastError();
- if (dwResult == ERROR_TIMEOUT)
- {
- }
- //SAFE_DELETE(pLog);
- }
- }
- }
- }
- catch(Exception &e)
- {
- }
- return result;
- }
- //---------------------------------------------------------------------------
- int TVMSCThread::SysLogWrite(int ALogKind, char *AFmt, ...)
- {
- va_list ap;
- int cnt = 0;
- char szFmtData[MAX_LOG_BUFFER];
- AnsiString sLogKind;
- int nStep = 0;
- if (!FLogFile) return cnt;
- bool bLog = false;
- switch(ALogKind)
- {
- case eLOG_INFO : bLog = FLogFile->FLogCfg.Info; sLogKind = " [INF] "; break;
- case eLOG_DATA : bLog = FLogFile->FLogCfg.Data; sLogKind = " [DAT] "; break;
- case eLOG_ERROR : bLog = FLogFile->FLogCfg.Error; sLogKind = " [ERR] "; break;
- case eLOG_WARNING: bLog = FLogFile->FLogCfg.Warning; sLogKind = " [WAN] "; break;
- case eLOG_DEBUG : bLog = FLogFile->FLogCfg.Debug; sLogKind = " [DBG] "; break;
- case eLOG_DETAIL : bLog = FLogFile->FLogCfg.Detail; sLogKind = " [DET] "; break;
- }
- if (!bLog) return -1;
- try
- {
- va_start(ap, AFmt);
- cnt = vsprintf(szFmtData, AFmt, ap);
- va_end(ap);
- FLogFile->LogWrite(NULL, ALogKind, szFmtData);
- IPC_LOG_MESSAGE *pLog = &FLogMsg;//new IPC_LOG_MESSAGE;
- if (pLog)
- {
- pLog->Kind = ALogKind;
- pLog->Flag = 0;
- pLog->Len = strlen(szFmtData);
- pLog->Tm = Now();
- pLog->Type = 'C';
- memset(pLog->Msg, 0x00, sizeof(pLog->Msg));
- sprintf(pLog->Msg, "%s-CLI %s, %s", sLogKind.c_str(), VMSID.c_str(), szFmtData);
- pLog->Obj = (void*)FVmsObj;
- DWORD dwResult = 0;
- LRESULT lResult = SEND_LOGMSG(FrmSysLog->Handle, WM_LOG_DISPLAY, pLog, NULL, SMTO_NORMAL, 2000, dwResult);
- if (lResult == 0)
- {
- dwResult = GetLastError();
- if (dwResult == ERROR_TIMEOUT)
- {
- }
- //SAFE_DELETE(pLog);
- }
- }
- if (FVmsObj->FDispLog)
- {
- IPC_LOG_MESSAGE *pLog2 = &FLogMsg;//new IPC_LOG_MESSAGE;
- if (pLog2)
- {
- pLog2->Kind = ALogKind;
- pLog2->Flag = 0;
- pLog2->Len = strlen(szFmtData);
- pLog2->Tm = Now();
- pLog2->Type = 'C';
- memset(pLog2->Msg, 0x00, sizeof(pLog2->Msg));
- sprintf(pLog2->Msg, "%s-CLI %s, %s", sLogKind.c_str(), VMSID.c_str(), szFmtData);
- pLog2->Obj = (void*)FVmsObj;
- DWORD dwResult = 0;
- LRESULT lResult = SEND_LOGMSG(FrmVmsLog->Handle, WM_LOG_DISPLAY, pLog2, NULL, SMTO_NORMAL, 2000, dwResult);
- if (lResult == 0)
- {
- dwResult = GetLastError();
- if (dwResult == ERROR_TIMEOUT)
- {
- }
- //SAFE_DELETE(pLog2);
- }
- }
- }
- }
- catch(Exception &e)
- {
- }
- return cnt;
- }
- //---------------------------------------------------------------------------
- int TVMSCThread::LogWrite(int ALogKind, char *AFmt, ...)
- {
- va_list ap;
- int cnt = 0;
- char szFmtData[MAX_LOG_BUFFER];
- AnsiString sLogKind;
- int nStep = 0;
- if (!FLogFile) return cnt;
- bool bLog = false;
- switch(ALogKind)
- {
- case eLOG_INFO : bLog = FLogFile->FLogCfg.Info; sLogKind = " [INF] "; break;
- case eLOG_DATA : bLog = FLogFile->FLogCfg.Data; sLogKind = " [DAT] "; break;
- case eLOG_ERROR : bLog = FLogFile->FLogCfg.Error; sLogKind = " [ERR] "; break;
- case eLOG_WARNING: bLog = FLogFile->FLogCfg.Warning; sLogKind = " [WAN] "; break;
- case eLOG_DEBUG : bLog = FLogFile->FLogCfg.Debug; sLogKind = " [DBG] "; break;
- case eLOG_DETAIL : bLog = FLogFile->FLogCfg.Detail; sLogKind = " [DET] "; break;
- }
- if (!bLog) return -1;
- try
- {
- va_start(ap, AFmt);
- cnt = vsprintf(szFmtData, AFmt, ap);
- va_end(ap);
- FLogFile->LogWrite(NULL, ALogKind, szFmtData);
- if (FVmsObj->FDispLog)
- {
- IPC_LOG_MESSAGE *pLog = &FLogMsg;//new IPC_LOG_MESSAGE;
- if (pLog)
- {
- pLog->Kind = ALogKind;
- pLog->Flag = 0;
- pLog->Len = strlen(szFmtData);
- pLog->Tm = Now();
- pLog->Type = 'C';
- memset(pLog->Msg, 0x00, sizeof(pLog->Msg));
- sprintf(pLog->Msg, "%s-CLI %s, %s", sLogKind.c_str(), VMSID.c_str(), szFmtData);
- pLog->Obj = (void*)FVmsObj;
- DWORD dwResult = 0;
- LRESULT lResult = SEND_LOGMSG(FrmVmsLog->Handle, WM_LOG_DISPLAY, pLog, NULL, SMTO_NORMAL, 2000, dwResult);
- if (lResult == 0)
- {
- dwResult = GetLastError();
- if (dwResult == ERROR_TIMEOUT)
- {
- }
- //SAFE_DELETE(pLog);
- }
- }
- }
- }
- catch(Exception &e)
- {
- }
- return cnt;
- }
- //---------------------------------------------------------------------------
- int __fastcall TVMSCThread::SendSubRegister(int AObj)
- {
- TStringList *sl = NULL;
- String sMsg = "";
- String sGeneralStatus =
- "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
- "0F 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
- "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
- "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
- "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
- "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 06 A1 "
- "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
- "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
- "F3 6B 02 00 06 A1 02 30 00 82 02 9E 17 ";
- String sPowerStatus =
- "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
- "10 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
- "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
- "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
- "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
- "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 07 A1 "
- "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
- "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
- "F3 6B 02 00 07 A1 02 30 00 82 02 27 24 ";
- String sModuleStatus =
- "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
- "11 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
- "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
- "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
- "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
- "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 08 A1 "
- "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
- "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
- "F3 6B 02 00 08 A1 02 30 00 82 02 BD E6 ";
- String sDotStatus =
- "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
- "12 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
- "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
- "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
- "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
- "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 09 A1 "
- "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
- "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
- "F3 6B 02 00 09 A1 02 30 00 82 02 CD 4D ";
- String sScreenStatus =
- "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
- "13 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
- "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
- "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
- "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
- "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 0A A1 "
- "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
- "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
- "F3 6B 02 00 0A A1 02 30 00 82 02 9F 82 ";
- String sDisplayingData =
- "30 81 8A 80 01 01 81 81 80 30 7E 80 01 05 81 01 "
- "14 82 01 00 A3 42 81 08 30 30 30 30 30 30 30 30 "
- "83 08 30 30 30 30 30 30 30 30 85 0A 56 4D 53 31 "
- "30 30 30 30 31 33 A7 20 80 02 07 E0 81 01 0B 82 "
- "01 11 83 01 0D 84 01 08 85 01 19 A6 03 82 01 00 "
- "A7 06 80 01 09 81 01 00 A4 2F A5 2D 80 01 0B A1 "
- "28 A0 26 80 01 FF 81 01 00 A2 07 A1 05 A0 03 80 "
- "01 00 83 01 03 84 01 01 85 01 FF A6 0C 80 06 28 "
- "F3 6B 02 00 0B A1 02 30 00 82 02 2C 1B ";
- switch (AObj)
- {
- case OBJ_GeneralStatus: sMsg = sGeneralStatus; break;
- case OBJ_PowerStatus: sMsg = sPowerStatus; break;
- case OBJ_ModuleStatus: sMsg = sModuleStatus; break;
- case OBJ_DotStatus: sMsg = sDotStatus; break;
- case OBJ_ScreenStatus: sMsg = sScreenStatus; break;
- case OBJ_DisplayingData: sMsg = sDisplayingData; break;
- default: return SYS_ERR_OTHER;
- }
- try
- {
- sl = new TStringList;
- sl->Delimiter = ' ';
- sl->StrictDelimiter = true;
- sl->DelimitedText = sMsg;
- int ii;
- unsigned int hn, ln;
- BYTE chn, cln, val;
- char szData[2048];
- BYTE RxBuff[VMS_MAX_PACKET_SIZE];
- int RxLen;
- RxLen = 0;
- memset(RxBuff, 0x00, sizeof(RxBuff));
- for (ii = 0; ii < sl->Count; ii++)
- {
- AnsiString sTemp = sl->Strings[ii];
- if (sTemp.Trim() == "") continue;
- sTemp = StringReplace(sTemp, " ", "", TReplaceFlags() << rfReplaceAll);
- if (sTemp.Trim() == "\r\n") continue;
- memset(szData, 0x00, sizeof(szData));
- memcpy(szData, sTemp.c_str(), sTemp.Length());
- chn = (BYTE)szData[0];
- cln = (BYTE)szData[1];
- hn = chn > '9' ? chn - 'A' + 10 : chn - '0';
- ln = cln > '9' ? cln - 'A' + 10 : cln - '0';
- val = (BYTE)((hn << 4 ) | ln);
- RxBuff[RxLen] = val;
- RxLen++;
- }
- int nSends = TcpClient->SendBuf(RxBuff, RxLen);
- LINFO("SendSubRegister: %d/%d Bytes", RxLen, nSends);
- if (sl) delete sl;
- }
- catch(Exception &e)
- {
- //Application->ShowException(&e);
- }
- return SYS_ERR_NONE;
- }
- //---------------------------------------------------------------------------
|