Module_EvComm.c 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446
  1. /*
  2. * Module_EvComm.c
  3. *
  4. * Created on: 2020年9月14日
  5. * Author: Wendell
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/can.h>
  19. #include <linux/can/raw.h>
  20. #include <linux/wireless.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> /*標準輸入輸出定義*/
  26. #include <stdlib.h> /*標準函數庫定義*/
  27. #include <unistd.h> /*Unix 標準函數定義*/
  28. #include <fcntl.h> /*檔控制定義*/
  29. #include <termios.h> /*PPSIX 終端控制定義*/
  30. #include <errno.h> /*錯誤號定義*/
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <ifaddrs.h>
  36. #include <signal.h>
  37. #include <net/if_arp.h>
  38. #include "../../define.h"
  39. #include "Module_EvComm.h"
  40. #include "Config.h"
  41. #include "Common.h"
  42. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  43. ChargerInfoData *ShmChargerInfo;
  44. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  45. struct PsuData *ShmPsuData;
  46. ChargingCapabilityResponseInfo ConnectorCapability[GENERAL_GUN_QUANTITY];
  47. RefundResponse ConnectorRefund[GENERAL_GUN_QUANTITY];
  48. PrepaymentResponseInfo ConnectorPrepayment[GENERAL_GUN_QUANTITY];
  49. PaymentFailResponse ConnectorPaymentFailReason[GENERAL_GUN_QUANTITY];
  50. char _ConnectorQRCode[GENERAL_GUN_QUANTITY][128];
  51. StationInfoData _stationInfo[GENERAL_GUN_QUANTITY];
  52. DeductResultInfoData _deductResult[GENERAL_GUN_QUANTITY];
  53. void ShowSocketData(struct PACKET_STRUCTURE *packet)
  54. {
  55. printf("se: %02X, id: %02X, op: %d, len: %3d, reg: %02X",
  56. packet->Header.se, packet->Header.id, packet->Header.op, packet->Header.len, packet->Payload.reg);
  57. if(packet->Header.len > 1)
  58. {
  59. printf(", Data:");
  60. for(int i = 0; i < packet->Header.len - 1; i++)
  61. {
  62. printf(" %02X", packet->Payload.data[i]);
  63. }
  64. }
  65. printf("\n");
  66. }
  67. //==========================================
  68. // Init all share memory
  69. //==========================================
  70. int InitShareMemory()
  71. {
  72. int result = PASS;
  73. int MeterSMId;
  74. //creat ShmSysConfigAndInfo
  75. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  76. {
  77. #ifdef SystemLogMessage
  78. LOG_ERROR("shmget ShmSysConfigAndInfo NG");
  79. #endif
  80. result = FAIL;
  81. }
  82. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  83. {
  84. #ifdef SystemLogMessage
  85. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  86. #endif
  87. result = FAIL;
  88. }
  89. else
  90. {
  91. }
  92. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  93. {
  94. #ifdef SystemLogMessage
  95. LOG_ERROR("shmat ChargerInfoData NG");
  96. #endif
  97. result = FAIL;
  98. }
  99. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  100. {
  101. #ifdef SystemLogMessage
  102. LOG_ERROR("shmat ChargerInfoData NG");
  103. #endif
  104. result = FAIL;
  105. }
  106. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  107. {
  108. #ifdef SystemLogMessage
  109. LOG_ERROR("shmget ShmPsuData NG");
  110. #endif
  111. result = FAIL;
  112. }
  113. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  114. {
  115. #ifdef SystemLogMessage
  116. LOG_ERROR("shmat ShmPsuData NG");
  117. #endif
  118. result = FAIL;
  119. }
  120. return result;
  121. }
  122. //-------------------------------------------Socket Client Start-------------------------------------------
  123. unsigned char seqNum = 0;
  124. void sendCmdDispenserModelName(int socket, unsigned char se)
  125. {
  126. struct PACKET_STRUCTURE sendBuffer;
  127. char *modelName = "DSYE601J0EW2PH";
  128. //char *modelName = "DSYE601J00W2PH";
  129. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  130. sendBuffer.Header.se = se;
  131. sendBuffer.Header.id = 0xFF;
  132. sendBuffer.Header.op = _Header_Write;
  133. sendBuffer.Header.len = strlen(modelName) + 2;
  134. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  135. memcpy(&sendBuffer.Payload.data[0], modelName, strlen(modelName));
  136. sendBuffer.Payload.data[strlen(modelName)] = 1;
  137. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  138. }
  139. void sendCmdDispenserConnectorIDRequest(int socket, unsigned char se)
  140. {
  141. struct PACKET_STRUCTURE sendBuffer;
  142. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  143. sendBuffer.Header.se = se;
  144. sendBuffer.Header.id = 0xFF;
  145. sendBuffer.Header.op = _Header_Read;
  146. sendBuffer.Header.len = 1;
  147. sendBuffer.Payload.reg = _Reg_Connector_ID;
  148. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  149. }
  150. void sendCmdPowerCabinetStatusRequest(int socket, unsigned char connector, unsigned char se)
  151. {
  152. struct PACKET_STRUCTURE sendBuffer;
  153. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  154. sendBuffer.Header.se = se;
  155. sendBuffer.Header.id = connector;
  156. sendBuffer.Header.op = _Header_Read;
  157. sendBuffer.Header.len = 1;
  158. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  159. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  160. }
  161. void sendCmdWriteDispenserStatus(int socket, unsigned char connector, unsigned char se)
  162. {
  163. struct PACKET_STRUCTURE sendBuffer;
  164. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  165. sendBuffer.Header.se = se;
  166. sendBuffer.Header.id = connector;
  167. sendBuffer.Header.op = _Header_Write;
  168. sendBuffer.Header.len = 1;
  169. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  170. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  171. }
  172. void sendCmdReadChargingCapability(int socket, unsigned char connector, unsigned char se)
  173. {
  174. struct PACKET_STRUCTURE sendBuffer;
  175. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  176. sendBuffer.Header.se = se;
  177. sendBuffer.Header.id = connector;
  178. sendBuffer.Header.op = _Header_Read;
  179. sendBuffer.Header.len = 1;
  180. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  181. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  182. }
  183. void sendCmdWriteChargingTarget(int socket, unsigned char connector, unsigned char se, unsigned short voltage, unsigned short current)
  184. {
  185. struct PACKET_STRUCTURE sendBuffer;
  186. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  187. sendBuffer.Header.se = se;
  188. sendBuffer.Header.id = connector;
  189. sendBuffer.Header.op = _Header_Write;
  190. sendBuffer.Header.len = 5;
  191. sendBuffer.Payload.reg = _Reg_Charging_Target;
  192. sendBuffer.Payload.data[0] = (voltage >> 8) & 0xFF;
  193. sendBuffer.Payload.data[1] = voltage & 0xFF;
  194. sendBuffer.Payload.data[2] = (current >> 8) & 0xFF;
  195. sendBuffer.Payload.data[3] = current & 0xFF;
  196. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  197. }
  198. void sendCmdWritePlugInStatus(int socket, unsigned char connector, unsigned char se)
  199. {
  200. struct PACKET_STRUCTURE sendBuffer;
  201. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  202. sendBuffer.Header.se = se;
  203. sendBuffer.Header.id = connector;
  204. sendBuffer.Header.op = _Header_Write;
  205. sendBuffer.Header.len = 2;
  206. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  207. sendBuffer.Payload.data[0] = se % 2 == 0 ?_PIS_UnPlugged : _PIS_PluggedIn;
  208. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  209. }
  210. void sendCmdWriteConnectorState(int socket, unsigned char connector, unsigned char se)
  211. {
  212. struct PACKET_STRUCTURE sendBuffer;
  213. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  214. sendBuffer.Header.se = se;
  215. sendBuffer.Header.id = connector;
  216. sendBuffer.Header.op = _Header_Write;
  217. sendBuffer.Header.len = 2;
  218. sendBuffer.Payload.reg = _Reg_Connector_State;
  219. sendBuffer.Payload.data[0] = _CRS_Idle;
  220. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  221. }
  222. void sendCmdWriteUserID(int socket, unsigned char connector, unsigned char se)
  223. {
  224. struct PACKET_STRUCTURE sendBuffer;
  225. char *userID = "010203040506";
  226. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  227. sendBuffer.Header.se = se;
  228. sendBuffer.Header.id = connector;
  229. sendBuffer.Header.op = _Header_Write;
  230. sendBuffer.Header.len = strlen(userID) + 1;
  231. sendBuffer.Payload.reg = _Reg_User_ID;
  232. memcpy(sendBuffer.Payload.data, userID, strlen(userID));
  233. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  234. }
  235. void sendCmdReadChargingPermission(int socket, unsigned char connector, unsigned char se)
  236. {
  237. struct PACKET_STRUCTURE sendBuffer;
  238. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  239. sendBuffer.Header.se = se;
  240. sendBuffer.Header.id = connector;
  241. sendBuffer.Header.op = _Header_Read;
  242. sendBuffer.Header.len = 1;
  243. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  244. send(socket, &sendBuffer, sendBuffer.Header.len + 4, 0);
  245. }
  246. int DispenserModelNameCheckIn(int socket, unsigned char se)
  247. {
  248. int rxLen = 0, timeout = 0;
  249. struct PACKET_STRUCTURE receiveBuffer;
  250. int result = FAIL;
  251. sendCmdDispenserModelName(socket, se);
  252. while(1)
  253. {
  254. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  255. {
  256. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  257. {
  258. ShowSocketData(&receiveBuffer);
  259. if(receiveBuffer.Header.len == 2)
  260. {
  261. if(receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name && receiveBuffer.Payload.data[0] == _R_OK)
  262. {
  263. LOG_INFO("Write Model Name OK");
  264. result = PASS;
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. timeout += SOCKET_RECEIVE_INTERVAL;
  271. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  272. if(timeout >= DISPENSER_MODEL_NAME_RESEND)
  273. {
  274. //timeout
  275. break;
  276. }
  277. }
  278. return result;
  279. }
  280. int DispenserConnectorRequest(int socket, unsigned char se)
  281. {
  282. int rxLen = 0, timeout = 0;
  283. struct PACKET_STRUCTURE receiveBuffer;
  284. int result = FAIL;
  285. sendCmdDispenserConnectorIDRequest(socket, se);
  286. while(1)
  287. {
  288. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  289. {
  290. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  291. {
  292. ShowSocketData(&receiveBuffer);
  293. if(receiveBuffer.Header.len == 4)
  294. {
  295. if(receiveBuffer.Payload.reg == _Reg_Connector_ID && receiveBuffer.Payload.data[0] == _R_OK)
  296. {
  297. LOG_INFO("Get Connector ID: %02X, %02X", receiveBuffer.Payload.data[1], receiveBuffer.Payload.data[2]);
  298. result = PASS;
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. timeout += SOCKET_RECEIVE_INTERVAL;
  305. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  306. if(timeout >= DISPENSER_CONNECTOR_RESEND)
  307. {
  308. //timeout
  309. break;
  310. }
  311. }
  312. return result;
  313. }
  314. int DispenserReadPowerCabinetStatus(int socket, unsigned char connector, unsigned char se)
  315. {
  316. int rxLen = 0, timeout = 0;
  317. struct PACKET_STRUCTURE receiveBuffer;
  318. int result = FAIL;
  319. sendCmdPowerCabinetStatusRequest(socket, connector, se);
  320. while(1)
  321. {
  322. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  323. {
  324. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  325. {
  326. ShowSocketData(&receiveBuffer);
  327. if(receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status && receiveBuffer.Payload.data[0] == _R_OK)
  328. {
  329. LOG_INFO("Get Cabinet Status Code, Len: %d", (receiveBuffer.Header.len - 2) / 6);
  330. result = PASS;
  331. }
  332. }
  333. }
  334. timeout += SOCKET_RECEIVE_INTERVAL;
  335. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  336. if(timeout >= CABINET_STATUS_REQUEST_RESEND)
  337. {
  338. //timeout
  339. break;
  340. }
  341. }
  342. return result;
  343. }
  344. int DispenserWriteStatusCode(int socket, unsigned char connector, unsigned char se)
  345. {
  346. int rxLen = 0, timeout = 0;
  347. struct PACKET_STRUCTURE receiveBuffer;
  348. int result = FAIL;
  349. sendCmdWriteDispenserStatus(socket, connector, se);
  350. while(1)
  351. {
  352. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  353. {
  354. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  355. {
  356. ShowSocketData(&receiveBuffer);
  357. if(receiveBuffer.Header.len == 2)
  358. {
  359. if(receiveBuffer.Payload.reg == _Reg_Dispenser_Status && receiveBuffer.Payload.data[0] == _R_OK)
  360. {
  361. LOG_INFO("Write Dispenser Status Code OK");
  362. result = PASS;
  363. }
  364. }
  365. }
  366. }
  367. timeout += SOCKET_RECEIVE_INTERVAL;
  368. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  369. if(timeout >= CABINET_STATUS_REQUEST_RESEND)
  370. {
  371. //timeout
  372. break;
  373. }
  374. }
  375. return result;
  376. }
  377. int DispenserReadChargingCapability(int socket, unsigned char connector, unsigned char se)
  378. {
  379. int rxLen = 0, timeout = 0;
  380. struct PACKET_STRUCTURE receiveBuffer;
  381. int result = FAIL;
  382. unsigned short voltage = 0, current = 0, power = 0;
  383. sendCmdReadChargingCapability(socket, connector, se);
  384. while(1)
  385. {
  386. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  387. {
  388. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  389. {
  390. ShowSocketData(&receiveBuffer);
  391. if(receiveBuffer.Header.len == 8)
  392. {
  393. if(receiveBuffer.Payload.reg == _Reg_Charging_Capability && receiveBuffer.Payload.data[0] == _R_OK)
  394. {
  395. voltage = (receiveBuffer.Payload.data[1] << 8) + receiveBuffer.Payload.data[2];
  396. current = (receiveBuffer.Payload.data[3] << 8) + receiveBuffer.Payload.data[4];
  397. power = (receiveBuffer.Payload.data[5] << 8) + receiveBuffer.Payload.data[6];
  398. LOG_INFO("Capability Voltage: %d.%dV, Current: %d.%dA, Power: %d.%dkW",
  399. voltage / 10, voltage % 10, current / 10, current % 10, power / 10, power % 10);
  400. result = PASS;
  401. }
  402. }
  403. }
  404. }
  405. timeout += SOCKET_RECEIVE_INTERVAL;
  406. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  407. if(timeout >= CHARGING_CAPABILITY_RESEND)
  408. {
  409. //timeout
  410. break;
  411. }
  412. }
  413. return result;
  414. }
  415. int DispenserWriteChargingTarget(int socket, unsigned char connector, unsigned char se)
  416. {
  417. int rxLen = 0, timeout = 0;
  418. struct PACKET_STRUCTURE receiveBuffer;
  419. int result = FAIL;
  420. sendCmdWriteChargingTarget(socket, connector, se, 7505, 406);
  421. while(1)
  422. {
  423. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  424. {
  425. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  426. {
  427. ShowSocketData(&receiveBuffer);
  428. if(receiveBuffer.Header.len == 2)
  429. {
  430. if(receiveBuffer.Payload.reg == _Reg_Charging_Target && receiveBuffer.Payload.data[0] == _R_OK)
  431. {
  432. LOG_INFO("Write Charging Target OK");
  433. result = PASS;
  434. }
  435. }
  436. }
  437. }
  438. timeout += SOCKET_RECEIVE_INTERVAL;
  439. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  440. if(timeout >= CHARGING_CAPABILITY_RESEND)
  441. {
  442. //timeout
  443. break;
  444. }
  445. }
  446. return result;
  447. }
  448. int DispenserWritePlugInStatus(int socket, unsigned char connector, unsigned char se)
  449. {
  450. int rxLen = 0, timeout = 0;
  451. struct PACKET_STRUCTURE receiveBuffer;
  452. int result = FAIL;
  453. sendCmdWritePlugInStatus(socket, connector, se);
  454. while(1)
  455. {
  456. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  457. {
  458. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  459. {
  460. ShowSocketData(&receiveBuffer);
  461. if(receiveBuffer.Header.len == 2)
  462. {
  463. if(receiveBuffer.Payload.reg == _Reg_Plug_In_Status && receiveBuffer.Payload.data[0] == _R_OK)
  464. {
  465. LOG_INFO("Write Plug In Status OK");
  466. result = PASS;
  467. }
  468. }
  469. }
  470. }
  471. timeout += SOCKET_RECEIVE_INTERVAL;
  472. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  473. if(timeout >= CHARGING_CAPABILITY_RESEND)
  474. {
  475. //timeout
  476. break;
  477. }
  478. }
  479. return result;
  480. }
  481. int DispenserWriteConnectorState(int socket, unsigned char connector, unsigned char se)
  482. {
  483. int rxLen = 0, timeout = 0;
  484. struct PACKET_STRUCTURE receiveBuffer;
  485. int result = FAIL;
  486. sendCmdWriteConnectorState(socket, connector, se);
  487. while(1)
  488. {
  489. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  490. {
  491. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  492. {
  493. ShowSocketData(&receiveBuffer);
  494. if(receiveBuffer.Header.len == 2)
  495. {
  496. if(receiveBuffer.Payload.reg == _Reg_Connector_State && receiveBuffer.Payload.data[0] == _R_OK)
  497. {
  498. LOG_INFO("Write Connector State OK");
  499. result = PASS;
  500. }
  501. }
  502. }
  503. }
  504. timeout += SOCKET_RECEIVE_INTERVAL;
  505. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  506. if(timeout >= CHARGING_CAPABILITY_RESEND)
  507. {
  508. //timeout
  509. break;
  510. }
  511. }
  512. return result;
  513. }
  514. int DispenserWriteUserID(int socket, unsigned char connector, unsigned char se)
  515. {
  516. int rxLen = 0, timeout = 0;
  517. struct PACKET_STRUCTURE receiveBuffer;
  518. int result = FAIL;
  519. sendCmdWriteUserID(socket, connector, se);
  520. while(1)
  521. {
  522. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  523. {
  524. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  525. {
  526. ShowSocketData(&receiveBuffer);
  527. if(receiveBuffer.Header.len == 2)
  528. {
  529. if(receiveBuffer.Payload.reg == _Reg_User_ID && receiveBuffer.Payload.data[0] == _R_OK)
  530. {
  531. LOG_INFO("Write User ID OK");
  532. result = PASS;
  533. }
  534. }
  535. }
  536. }
  537. timeout += SOCKET_RECEIVE_INTERVAL;
  538. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  539. if(timeout >= CHARGING_CAPABILITY_RESEND)
  540. {
  541. //timeout
  542. break;
  543. }
  544. }
  545. return result;
  546. }
  547. int DispenserReadChargingPermission(int socket, unsigned char connector, unsigned char se)
  548. {
  549. int rxLen = 0, timeout = 0;
  550. struct PACKET_STRUCTURE receiveBuffer;
  551. int result = FAIL;
  552. sendCmdReadChargingPermission(socket, connector, se);
  553. while(1)
  554. {
  555. if((rxLen = recv(socket, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  556. {
  557. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  558. {
  559. ShowSocketData(&receiveBuffer);
  560. if(receiveBuffer.Header.len == 3)
  561. {
  562. if(receiveBuffer.Payload.reg == _Reg_Charging_Permission && receiveBuffer.Payload.data[0] == _R_OK)
  563. {
  564. LOG_INFO("Charging Permission: %s", receiveBuffer.Payload.data[1] == _PS_Permitted ? "Permitted" : "Not Permitted");
  565. result = PASS;
  566. }
  567. }
  568. }
  569. }
  570. timeout += SOCKET_RECEIVE_INTERVAL;
  571. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  572. if(timeout >= CHARGING_CAPABILITY_RESEND)
  573. {
  574. //timeout
  575. break;
  576. }
  577. }
  578. return result;
  579. }
  580. void tcpSocketClientStart(void)
  581. {
  582. struct sockaddr_in info;
  583. struct timeval tv;
  584. uint8_t socketEnable;
  585. int sockfd = 0;
  586. unsigned char clientStatus = 0;
  587. unsigned char se = 0;
  588. //start dhcp client
  589. //udhcpc -i eth1 --script /root/dhcp_script/eth1.script
  590. //udhcpc -i eth1 -s /root/dhcp_script/eth1.script &
  591. //udhcpc -i eth0 -x hostname CSU3_DSYE601J0EW2PH-SERIALFORRD -s /root/dhcp_script/eth0.script
  592. for(;;)
  593. {
  594. bzero(&info,sizeof(info));
  595. info.sin_family = PF_INET;
  596. info.sin_addr.s_addr = inet_addr(SOCKET_SERVER_IP);
  597. info.sin_port = htons(TCP_LISTEN_PORT);
  598. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  599. if (sockfd == -1)
  600. {
  601. LOG_INFO("Fail to create a socket.");
  602. return;
  603. }
  604. LOG_INFO("TCP Client Start");
  605. if(connect(sockfd, (struct sockaddr *)&info, sizeof(info)) == -1)
  606. {
  607. LOG_INFO("Connection error");
  608. }
  609. else
  610. {
  611. LOG_INFO("Connect to %s success", inet_ntoa(info.sin_addr));
  612. tv.tv_sec = 0;
  613. tv.tv_usec = 500000;
  614. setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  615. socketEnable = ON;
  616. }
  617. while(socketEnable)
  618. {
  619. switch(clientStatus)
  620. {
  621. case 0:
  622. //5.1 Dispenser model name
  623. if(DispenserModelNameCheckIn(sockfd, se++) == PASS)
  624. {
  625. clientStatus = 1;
  626. }
  627. break;
  628. case 1:
  629. if(DispenserConnectorRequest(sockfd, se++) == PASS)
  630. {
  631. clientStatus = 2;
  632. }
  633. break;
  634. case 2:
  635. if(DispenserReadPowerCabinetStatus(sockfd, 0x01, se++) == PASS)
  636. {
  637. if(DispenserWriteStatusCode(sockfd, 0x01, se++) == PASS)
  638. {
  639. clientStatus = 3;
  640. }
  641. }
  642. break;
  643. case 3:
  644. if(DispenserWriteUserID(sockfd, 0x01, se++) == PASS)
  645. {
  646. clientStatus = 4;
  647. }
  648. break;
  649. case 4:
  650. if(DispenserReadChargingCapability(sockfd, 0x01, se++) == PASS)
  651. {
  652. }
  653. if(DispenserWriteChargingTarget(sockfd, 0x01, se++) == PASS)
  654. {
  655. }
  656. if(DispenserWritePlugInStatus(sockfd, 0x01, se++) == PASS)
  657. {
  658. }
  659. if(DispenserWriteConnectorState(sockfd, 0x01, se++) == PASS)
  660. {
  661. }
  662. if(DispenserReadChargingPermission(sockfd, 0x01, se++) == PASS)
  663. {
  664. }
  665. break;
  666. default:
  667. break;
  668. }
  669. }
  670. close(sockfd);
  671. sleep(5);
  672. return;
  673. }
  674. }
  675. //--------------------------------------------Socket Client End--------------------------------------------
  676. BOOL IsAvalibleGunType(char name, unsigned char *type)
  677. {
  678. // 20210817 remove type "R"
  679. char modelList[10] = {'J', 'U', 'V', 'E', 'F', 'G', 'T', 'D', 'K', 'P'};
  680. unsigned char typeList[10] = { 0, 1, 1, 1, 1, 2, 1, 1, 0, 1}; // 0 : Chademo, 1: CCS, 2: GB
  681. for(int i = 0; i < 11; i++)
  682. {
  683. if(name == modelList[i])
  684. {
  685. *type = typeList[i];
  686. return true;
  687. }
  688. }
  689. return false;
  690. }
  691. unsigned char ConnectorQuantityTypeParsing(unsigned char *modelName, unsigned char *type, unsigned char *physical)
  692. {
  693. unsigned char quantity = 0;
  694. for(int i = 0; i < 3; i++)
  695. {
  696. if(IsAvalibleGunType(modelName[7 + i], &type[quantity]))
  697. {
  698. physical[quantity] = modelName[7 + i];
  699. if(quantity < 2)
  700. {
  701. quantity++;
  702. }
  703. else
  704. {
  705. quantity = 0;
  706. break;
  707. }
  708. }
  709. }
  710. return quantity;
  711. }
  712. void SendPacket(int socket, struct PACKET_STRUCTURE *packet)
  713. {
  714. //ShowSocketData(packet);
  715. send(socket, packet, packet->Header.len + 4, 0);
  716. }
  717. void ModelNameResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  718. {
  719. struct PACKET_STRUCTURE sendBuffer;
  720. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  721. sendBuffer.Header.se = packet->Header.se;
  722. sendBuffer.Header.id = 0xFF;
  723. sendBuffer.Header.op = _Header_Response;
  724. sendBuffer.Header.len = 2;
  725. sendBuffer.Payload.reg = _Reg_Dispenser_Model_Name;
  726. sendBuffer.Payload.data[0] = result;
  727. SendPacket(socket, &sendBuffer);
  728. }
  729. void ConnectorIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char dispenserIndex)
  730. {
  731. struct PACKET_STRUCTURE sendBuffer;
  732. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  733. sendBuffer.Header.se = packet->Header.se;
  734. sendBuffer.Header.id = 0xFF;
  735. sendBuffer.Header.op = _Header_Response;
  736. sendBuffer.Header.len = 4;
  737. sendBuffer.Payload.reg = _Reg_Connector_ID;
  738. sendBuffer.Payload.data[0] = result;
  739. sendBuffer.Payload.data[1] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0];
  740. sendBuffer.Payload.data[2] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1];
  741. SendPacket(socket, &sendBuffer);
  742. }
  743. // more message for debug
  744. struct PACKET_STRUCTURE CabinetStatusToDispenser[4];
  745. void PowerCabinetStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  746. {
  747. struct PACKET_STRUCTURE sendBuffer;
  748. unsigned char MiscEventCode[7];
  749. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  750. sendBuffer.Header.se = packet->Header.se;
  751. sendBuffer.Header.id = packet->Header.id;
  752. sendBuffer.Header.op = _Header_Response;
  753. sendBuffer.Header.len = 2;
  754. sendBuffer.Payload.reg = _Reg_Power_Cabinet_Status;
  755. sendBuffer.Payload.data[0] = _R_OK;
  756. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0)
  757. {
  758. sendBuffer.Header.len += (ShmSysConfigAndInfo->SysWarningInfo.WarningCount * 6);
  759. for(int i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++)
  760. {
  761. memcpy(&sendBuffer.Payload.data[1 + (i * 6)], &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 6);
  762. if(ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] == '1')
  763. {
  764. sendBuffer.Payload.data[1 + (i * 6) + 1] = '4';
  765. }
  766. }
  767. }
  768. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.MiscNeedAnnouncement)
  769. {
  770. sendBuffer.Header.len += 6;
  771. memset(MiscEventCode, 0, sizeof(MiscEventCode));
  772. memcpy(MiscEventCode, "B40001", sizeof(MiscEventCode) - 1);
  773. memcpy(&sendBuffer.Payload.data[1 + (ShmSysConfigAndInfo->SysWarningInfo.WarningCount * 6)], &MiscEventCode[0], 6);
  774. }
  775. bool need_copy = false;
  776. if(sendBuffer.Header.len != CabinetStatusToDispenser[dispenserIndex].Header.len ||
  777. sendBuffer.Header.op != CabinetStatusToDispenser[dispenserIndex].Header.op ||
  778. sendBuffer.Payload.reg != CabinetStatusToDispenser[dispenserIndex].Payload.reg ||
  779. sendBuffer.Payload.data[0] != CabinetStatusToDispenser[dispenserIndex].Payload.data[0])
  780. {
  781. need_copy = true;
  782. }
  783. else
  784. {
  785. for(int i = 0; i < sendBuffer.Header.len - 2; i++)
  786. {
  787. if(sendBuffer.Payload.data[1 + i] != CabinetStatusToDispenser[dispenserIndex].Payload.data[1 + i])
  788. {
  789. need_copy = true;
  790. }
  791. }
  792. }
  793. if(need_copy)
  794. {
  795. char str[256];
  796. memcpy(&CabinetStatusToDispenser[dispenserIndex], &sendBuffer, sizeof(struct PACKET_STRUCTURE));
  797. if(CabinetStatusToDispenser[dispenserIndex].Header.len > 2)
  798. {
  799. sprintf(str, "Cabinet >> Dispenser %d Status Code:", dispenserIndex + 1);
  800. for(int i = 0; i < CabinetStatusToDispenser[dispenserIndex].Header.len - 2; i += 6)
  801. {
  802. char status[7];
  803. char temp[8];
  804. memset(status, 0x00, sizeof(status));
  805. memcpy(status, (char *)&CabinetStatusToDispenser[dispenserIndex].Payload.data[1 + i], 6);
  806. sprintf(temp, " %s", status);
  807. strcat(str, temp);
  808. }
  809. LOG_INFO("%s", str);
  810. }
  811. else
  812. {
  813. LOG_INFO("Cabinet >> Dispenser %d Status Code Clear", dispenserIndex + 1);
  814. }
  815. }
  816. SendPacket(socket, &sendBuffer);
  817. }
  818. void DispenserStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  819. {
  820. struct PACKET_STRUCTURE sendBuffer;
  821. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  822. sendBuffer.Header.se = packet->Header.se;
  823. sendBuffer.Header.id = packet->Header.id;
  824. sendBuffer.Header.op = _Header_Response;
  825. sendBuffer.Header.len = 2;
  826. sendBuffer.Payload.reg = _Reg_Dispenser_Status;
  827. sendBuffer.Payload.data[0] = result;
  828. SendPacket(socket, &sendBuffer);
  829. }
  830. void GetPhysicalLimitVoltageAndCurrent(byte index, unsigned short *voltage, unsigned short *currrent)
  831. {
  832. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage != 0 &&
  833. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage <= *voltage)
  834. {
  835. *voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalVoltage;
  836. }
  837. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent != 0 &&
  838. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent <= *currrent)
  839. {
  840. *currrent = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemoteMaxPhysicalCurrent;
  841. }
  842. }
  843. void GetModelNameLimitPower(byte index, unsigned short *power)
  844. {
  845. unsigned short limitPower = 0;
  846. unsigned char inUsingCnt = 0;
  847. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  848. {
  849. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  850. {
  851. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  852. }
  853. }
  854. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  855. ShmChargerInfo->Control.ChargerRatingPower != 0)
  856. {
  857. limitPower = ShmChargerInfo->Control.ChargerRatingPower;
  858. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  859. {
  860. limitPower += ShmChargerInfo->ParallelCabinet.TotalParallelPower;
  861. }
  862. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  863. }
  864. if(limitPower != 0 && limitPower <= *power)
  865. {
  866. *power = limitPower;
  867. }
  868. }
  869. void GetConfigLimitPowerAndCurrent(byte index, unsigned short *power, unsigned short *currrent)
  870. {
  871. unsigned short limitPower = 0, limitCurrent = 0;
  872. unsigned char inUsingCnt = 0;
  873. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  874. {
  875. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  876. {
  877. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  878. }
  879. }
  880. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  881. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  882. {
  883. limitPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  884. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  885. }
  886. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].MaxTotalChargingPower = limitPower;
  887. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  888. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  889. {
  890. limitCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  891. limitCurrent = (limitCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  892. }
  893. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].MaxTotalChargingCurrent = limitCurrent;
  894. if(limitPower != 0 && limitPower <= *power)
  895. {
  896. *power = limitPower;
  897. }
  898. if(limitCurrent != 0 && limitCurrent <= *currrent)
  899. {
  900. *currrent = limitCurrent;
  901. }
  902. }
  903. void GetChargingProfileLimit(byte index, unsigned short *power, unsigned short *currrent)
  904. {
  905. if((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  906. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  907. {
  908. if(chargingInfo[index]->ChargingProfilePower >= 0 && *power > ((int)chargingInfo[index]->ChargingProfilePower / 100))
  909. {
  910. *power = (int)chargingInfo[index]->ChargingProfilePower / 100;
  911. }
  912. if(chargingInfo[index]->ChargingProfileCurrent >= 0 && *currrent > (int)chargingInfo[index]->ChargingProfileCurrent)
  913. {
  914. *currrent = (int)chargingInfo[index]->ChargingProfileCurrent;
  915. }
  916. }
  917. }
  918. void GetMaxChargingProfileLimit(byte index, unsigned short *power, unsigned short *currrent)
  919. {
  920. unsigned short limitPower = 0, limitCurrent = 0;
  921. unsigned char inUsingCnt = 0;
  922. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  923. {
  924. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  925. {
  926. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  927. }
  928. }
  929. // max charging profile
  930. if(ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  931. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  932. {
  933. limitPower = (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 100;
  934. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[index].GunPsuQuantity) / inUsingCnt;
  935. if(*power > limitPower)
  936. {
  937. *power = limitPower;
  938. }
  939. if (chargingInfo[index]->EvBatterytargetVoltage > 0 &&
  940. (int)chargingInfo[index]->PresentChargingVoltage > 0)
  941. {
  942. limitCurrent = (limitPower * 100 / chargingInfo[index]->PresentChargingVoltage) * 10;
  943. if(*currrent > limitCurrent)
  944. {
  945. *currrent = limitCurrent;
  946. }
  947. }
  948. }
  949. }
  950. void ChargingCapabilityResponse(int socket, struct PACKET_STRUCTURE *packet)
  951. {
  952. struct PACKET_STRUCTURE sendBuffer;
  953. unsigned short voltage = 0, current = 0, power = 0;
  954. unsigned char currency = 0;
  955. float fPrice = 0, fCost = 0, fBalance = 0, fDiscount = 0;
  956. unsigned int lPrice = 0, lCost = 0, lBalance = 0, lDiscount = 0;
  957. int transaction = 0;
  958. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  959. voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.MaximumChargingVoltage;
  960. current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingCurrent;
  961. power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.AvailableChargingPower;
  962. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  963. GetModelNameLimitPower(packet->Header.id - 1, &power);
  964. GetConfigLimitPowerAndCurrent(packet->Header.id - 1, &power, &current);
  965. GetChargingProfileLimit(packet->Header.id - 1, &power, &current);
  966. GetMaxChargingProfileLimit(packet->Header.id - 1, &power, &current);
  967. currency = ShmChargerInfo->CabinetMiscValue.Currency;
  968. fPrice = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].UserPrice;
  969. fCost = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].TotalCost;
  970. fBalance = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance;
  971. transaction = ShmChargerInfo->UserTransaction[packet->Header.id - 1].TransactionId;
  972. fDiscount = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CostDiscount;
  973. lPrice = (unsigned int)(fPrice * 100);
  974. lCost = (unsigned int)(fCost * 100);
  975. lBalance = (int)(fBalance * 100);
  976. lDiscount = (int)(fDiscount * 100);
  977. if((ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage / 10) != (voltage / 10) ||
  978. (ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent / 10) != (current / 10) ||
  979. (ConnectorCapability[packet->Header.id - 1].MaxOuputPower / 10) != (power / 10))
  980. {
  981. LOG_INFO("Connector %d Capability Voltage: %4d.%d V, Current: %3d.%d A, Power: %d kW", packet->Header.id,
  982. (voltage / 10), (voltage % 10), (current / 10), (current % 10), (power / 10));
  983. }
  984. if(ConnectorCapability[packet->Header.id - 1].Currency != currency)
  985. {
  986. memset(ConnectorCapability[packet->Header.id - 1].CurrencyString, 0x00, sizeof(ConnectorCapability[packet->Header.id - 1].CurrencyString));
  987. if(currency < 54)
  988. {
  989. memcpy(ConnectorCapability[packet->Header.id - 1].CurrencyString, (char *)&Currency[currency][0], 3);
  990. }
  991. else
  992. {
  993. LOG_INFO("Connector %d unknown currency: %d", packet->Header.id, (int)(currency));
  994. memcpy(ConnectorCapability[packet->Header.id - 1].CurrencyString, "???", 3);
  995. }
  996. }
  997. if(ConnectorCapability[packet->Header.id - 1].Currency != currency ||
  998. ConnectorCapability[packet->Header.id - 1].UserPrice != lPrice ||
  999. ConnectorCapability[packet->Header.id - 1].TotalCost != lCost ||
  1000. ConnectorCapability[packet->Header.id - 1].TransactionId != transaction)
  1001. {
  1002. LOG_INFO("Connector %d TransactionId: %d UserPrice: %6.2f, Cost: %.2f [%s]",
  1003. packet->Header.id, transaction, fPrice, fCost, ConnectorCapability[packet->Header.id - 1].CurrencyString);
  1004. }
  1005. if(ConnectorCapability[packet->Header.id - 1].AccountBalance != lBalance ||
  1006. ConnectorCapability[packet->Header.id - 1].CostDiscount != lDiscount)
  1007. {
  1008. LOG_INFO("Connector %d TransactionId: %d Account: %8.2f, Discount: %.2f [%s]",
  1009. packet->Header.id, transaction, fBalance, fDiscount, ConnectorCapability[packet->Header.id - 1].CurrencyString);
  1010. }
  1011. ConnectorCapability[packet->Header.id - 1].MaxOuputVoltage = voltage;
  1012. ConnectorCapability[packet->Header.id - 1].MaxOuputCurrent = current;
  1013. ConnectorCapability[packet->Header.id - 1].MaxOuputPower = power;
  1014. ConnectorCapability[packet->Header.id - 1].Currency = currency;
  1015. ConnectorCapability[packet->Header.id - 1].UserPrice = lPrice;
  1016. ConnectorCapability[packet->Header.id - 1].TotalCost = lCost;
  1017. ConnectorCapability[packet->Header.id - 1].AccountBalance = lBalance;
  1018. ConnectorCapability[packet->Header.id - 1].CostDiscount = lDiscount;
  1019. ConnectorCapability[packet->Header.id - 1].TransactionId = transaction;
  1020. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CapabilityVoltage = voltage;
  1021. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CapabilityCurrent = current;
  1022. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].CapabilityPower = power;
  1023. sendBuffer.Header.se = packet->Header.se;
  1024. sendBuffer.Header.id = packet->Header.id;
  1025. sendBuffer.Header.op = _Header_Response;
  1026. sendBuffer.Header.len = 29;
  1027. sendBuffer.Payload.reg = _Reg_Charging_Capability;
  1028. sendBuffer.Payload.data[0] = _R_OK;
  1029. sendBuffer.Payload.data[1] = ((voltage >> 8) & 0xFF);
  1030. sendBuffer.Payload.data[2] = (voltage & 0xFF);
  1031. sendBuffer.Payload.data[3] = ((current >> 8) & 0xFF);
  1032. sendBuffer.Payload.data[4] = (current & 0xFF);
  1033. sendBuffer.Payload.data[5] = ((power >> 8) & 0xFF);
  1034. sendBuffer.Payload.data[6] = (power & 0xFF);
  1035. sendBuffer.Payload.data[7] = currency;
  1036. sendBuffer.Payload.data[8] = ((lPrice >> 24) & 0xFF);
  1037. sendBuffer.Payload.data[9] = ((lPrice >> 16) & 0xFF);
  1038. sendBuffer.Payload.data[10] = ((lPrice >> 8) & 0xFF);
  1039. sendBuffer.Payload.data[11] = (lPrice & 0xFF);
  1040. sendBuffer.Payload.data[12] = ((lCost >> 24) & 0xFF);
  1041. sendBuffer.Payload.data[13] = ((lCost >> 16) & 0xFF);
  1042. sendBuffer.Payload.data[14] = ((lCost >> 8) & 0xFF);
  1043. sendBuffer.Payload.data[15] = (lCost & 0xFF);
  1044. sendBuffer.Payload.data[16] = ((lBalance >> 24) & 0xFF);
  1045. sendBuffer.Payload.data[17] = ((lBalance >> 16) & 0xFF);
  1046. sendBuffer.Payload.data[18] = ((lBalance >> 8) & 0xFF);
  1047. sendBuffer.Payload.data[19] = (lBalance & 0xFF);
  1048. sendBuffer.Payload.data[20] = ((lDiscount >> 24) & 0xFF);
  1049. sendBuffer.Payload.data[21] = ((lDiscount >> 16) & 0xFF);
  1050. sendBuffer.Payload.data[22] = ((lDiscount >> 8) & 0xFF);
  1051. sendBuffer.Payload.data[23] = (lDiscount & 0xFF);
  1052. sendBuffer.Payload.data[24] = ((transaction >> 24) & 0xFF);
  1053. sendBuffer.Payload.data[25] = ((transaction >> 16) & 0xFF);
  1054. sendBuffer.Payload.data[26] = ((transaction >> 8) & 0xFF);
  1055. sendBuffer.Payload.data[27] = (transaction & 0xFF);
  1056. SendPacket(socket, &sendBuffer);
  1057. }
  1058. void ChargingTargetResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1059. {
  1060. struct PACKET_STRUCTURE sendBuffer;
  1061. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1062. sendBuffer.Header.se = packet->Header.se;
  1063. sendBuffer.Header.id = packet->Header.id;
  1064. sendBuffer.Header.op = _Header_Response;
  1065. sendBuffer.Header.len = 2;
  1066. sendBuffer.Payload.reg = _Reg_Charging_Target;
  1067. sendBuffer.Payload.data[0] = result;
  1068. SendPacket(socket, &sendBuffer);
  1069. }
  1070. void FirmwareUpgradeResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1071. {
  1072. struct PACKET_STRUCTURE sendBuffer;
  1073. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1074. sendBuffer.Header.se = packet->Header.se;
  1075. sendBuffer.Header.id = packet->Header.id;
  1076. sendBuffer.Header.op = _Header_Response;
  1077. sendBuffer.Header.len = 3;
  1078. sendBuffer.Payload.reg = _Reg_Software_Update;
  1079. sendBuffer.Payload.data[0] = _R_OK;
  1080. sendBuffer.Payload.data[1] = result;
  1081. if(result == _R_NeedUpgrade)
  1082. {
  1083. int length = strlen(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FwFileName);
  1084. memcpy(&sendBuffer.Payload.data[2], ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FwFileName, length);
  1085. sendBuffer.Header.len += length;
  1086. }
  1087. SendPacket(socket, &sendBuffer);
  1088. }
  1089. void PlugInStatusResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1090. {
  1091. struct PACKET_STRUCTURE sendBuffer;
  1092. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1093. sendBuffer.Header.se = packet->Header.se;
  1094. sendBuffer.Header.id = packet->Header.id;
  1095. sendBuffer.Header.op = _Header_Response;
  1096. sendBuffer.Header.len = 2;
  1097. sendBuffer.Payload.reg = _Reg_Plug_In_Status;
  1098. sendBuffer.Payload.data[0] = result;
  1099. SendPacket(socket, &sendBuffer);
  1100. }
  1101. void ConnectorStateResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1102. {
  1103. struct PACKET_STRUCTURE sendBuffer;
  1104. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1105. sendBuffer.Header.se = packet->Header.se;
  1106. sendBuffer.Header.id = packet->Header.id;
  1107. sendBuffer.Header.op = _Header_Response;
  1108. sendBuffer.Header.len = 2;
  1109. sendBuffer.Payload.reg = _Reg_Connector_State;
  1110. sendBuffer.Payload.data[0] = result;
  1111. SendPacket(socket, &sendBuffer);
  1112. }
  1113. void UserIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1114. {
  1115. struct PACKET_STRUCTURE sendBuffer;
  1116. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1117. sendBuffer.Header.se = packet->Header.se;
  1118. sendBuffer.Header.id = packet->Header.id;
  1119. sendBuffer.Header.op = _Header_Response;
  1120. sendBuffer.Header.len = 3;
  1121. sendBuffer.Payload.reg = _Reg_User_ID;
  1122. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  1123. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  1124. SendPacket(socket, &sendBuffer);
  1125. }
  1126. void ChargingPermissionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1127. {
  1128. struct PACKET_STRUCTURE sendBuffer;
  1129. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1130. sendBuffer.Header.se = packet->Header.se;
  1131. sendBuffer.Header.id = packet->Header.id;
  1132. sendBuffer.Header.op = _Header_Response;
  1133. sendBuffer.Header.len = 3;
  1134. sendBuffer.Payload.reg = _Reg_Charging_Permission;
  1135. sendBuffer.Payload.data[0] = result == _DAS_Wait ? _R_NG : _R_OK;
  1136. sendBuffer.Payload.data[1] = result == _DAS_Allowed ? _PS_Permitted : _PS_NotPermitted;
  1137. SendPacket(socket, &sendBuffer);
  1138. }
  1139. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1140. {
  1141. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1142. }
  1143. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1144. {
  1145. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1146. }
  1147. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1148. {
  1149. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1150. }
  1151. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1152. {
  1153. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1154. }
  1155. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1156. {
  1157. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1158. }
  1159. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1160. {
  1161. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1162. }
  1163. void AddMiscCommand(struct PACKET_STRUCTURE *packet, struct MISC_COMMAND *misc)
  1164. {
  1165. packet->Payload.data[packet->Header.len - 1 + 0] = ((misc->Command >> 8) & 0xFF);
  1166. packet->Payload.data[packet->Header.len - 1 + 1] = ((misc->Command >> 0) & 0xFF);
  1167. packet->Payload.data[packet->Header.len - 1 + 2] = ((misc->Value >> 24) & 0xFF);
  1168. packet->Payload.data[packet->Header.len - 1 + 3] = ((misc->Value >> 16) & 0xFF);
  1169. packet->Payload.data[packet->Header.len - 1 + 4] = ((misc->Value >> 8) & 0xFF);
  1170. packet->Payload.data[packet->Header.len - 1 + 5] = ((misc->Value >> 0) & 0xFF);
  1171. packet->Header.len += 6;
  1172. }
  1173. bool Is_LcmPageChange_Req(int gun_index, int lcm_req)
  1174. {
  1175. if((ShmChargerInfo->ConnectorPageReq[gun_index].CtrlValue & lcm_req) > 0)
  1176. {
  1177. return true;
  1178. }
  1179. return false;
  1180. }
  1181. void Clean_LcmPage_Req(int gun_index, int lcm_req)
  1182. {
  1183. ShmChargerInfo->ConnectorPageReq[gun_index].CtrlValue &= ~lcm_req;
  1184. }
  1185. // LOG_INFO("Cabinet >> Connector %d Availability [SelfTestFail]", packet->Header.id);
  1186. // LOG_INFO("Cabinet >> Connector %d Availability [Disable]", packet->Header.id);
  1187. // LOG_INFO("Cabinet >> Connector %d Availability [Inoperative]", packet->Header.id);
  1188. // LOG_INFO("Cabinet >> Connector %d Availability [Enable]", packet->Header.id);
  1189. // LOG_INFO("Cabinet >> Connector %d RemoteStart [%d]", packet->Header.id, misc.Value);
  1190. // LOG_INFO("Cabinet >> Connector %d Balance [%d]", packet->Header.id, (misc.Value));
  1191. // LOG_INFO("Cabinet >> Connector %d Reservation [Enable]", packet->Header.id);
  1192. // LOG_INFO("Cabinet >> Connector %d QR Code [Enable]", packet->Header.id);
  1193. // LOG_INFO("Cabinet >> Connector %d Final Cost [Enable]", packet->Header.id);
  1194. // LOG_INFO("Cabinet >> Dispenser %d PlugTimeout [%d]", dispenserIndex + 1, misc.Value);
  1195. // LOG_INFO("Cabinet >> Dispenser %d DefaultPrice [%d]", dispenserIndex + 1, misc.Value);
  1196. // LOG_INFO("Cabinet >> Dispenser %d Currency [%d]", dispenserIndex + 1, misc.Value);
  1197. // LOG_INFO("Cabinet >> Dispenser %d BackendConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1198. // LOG_INFO("Cabinet >> Dispenser %d BackendConn [%d]", dispenserIndex + 1, (misc.Value));
  1199. // LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1200. // LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%d]", dispenserIndex + 1, (misc.Value));
  1201. // LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1202. // LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%d]", dispenserIndex + 1, (misc.Value));
  1203. // LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1204. // LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%d]", dispenserIndex + 1, (misc.Value));
  1205. // LOG_INFO("Cabinet >> Dispenser %d BillingInfo [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1206. // LOG_INFO("Cabinet >> Dispenser %d StopButton [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1207. // LOG_INFO("Cabinet >> Dispenser %d HardReboot [%d]", dispenserIndex + 1, misc.Value);
  1208. // LOG_INFO("Cabinet >> Dispenser %d SoftReset [%d]", dispenserIndex + 1, misc.Value);
  1209. // LOG_INFO("Cabinet >> Dispenser %d AuthMode [%s]", dispenserIndex + 1, misc.Value ? "Disable" : "Enable");
  1210. // LOG_INFO("Cabinet >> Dispenser %d EVCCID [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1211. // LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%s]", dispenserIndex + 1, strIntensity[misc.Value]);
  1212. // LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%d]", dispenserIndex + 1, misc.Value);
  1213. // LOG_INFO("Cabinet >> Dispenser %d Station Info [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1214. // LOG_INFO("Cabinet >> Dispenser %d Time Offset [%s%02d:%02d]", dispenserIndex + 1, misc.Value >= 0 ? "+" : "-", (abs(misc.Value) / 60), (abs(misc.Value) % 60));
  1215. // LOG_INFO("Cabinet >> Dispenser %d Time Offset [+%d]", dispenserIndex + 1, misc.Value);
  1216. // LOG_INFO("Cabinet >> Dispenser %d Time Offset [%d]", dispenserIndex + 1, misc.Value);
  1217. void MiscCmdRes(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1218. {
  1219. char *strConnection[] = {STR_CONN_DISABLE, STR_CONN_CONNECTED, STR_CONN_DISCONNECTED};
  1220. char *strIntensity[] = {STR_DARKEST, STR_MEDIUM, STR_BRIGHTEST};
  1221. char *strLcmPage[] = {STR_PAGE_NONE, STR_REMOTE_NO_ID};
  1222. struct PACKET_STRUCTURE sendBuffer;
  1223. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1224. sendBuffer.Header.se = packet->Header.se;
  1225. sendBuffer.Header.id = packet->Header.id;
  1226. sendBuffer.Header.op = _Header_Response;
  1227. sendBuffer.Header.len = 2;
  1228. sendBuffer.Payload.reg = _Reg_Misc_Control;
  1229. sendBuffer.Payload.data[0] = result;
  1230. if(result == _R_OK)
  1231. {
  1232. struct MISC_COMMAND misc;
  1233. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_AVAILABILITY))
  1234. {
  1235. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_AVAILABILITY);
  1236. misc.Command = _MiscCmd_ChangeOperative;
  1237. unsigned char IsAvailable = true;
  1238. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1239. {
  1240. IsAvailable = false;
  1241. LOG_INFO("Cabinet >> Connector %d Availability [SelfTestFail]", packet->Header.id);
  1242. }
  1243. else if(!ShmChargerInfo->Control.GunAvailable[packet->Header.id - 1])
  1244. {
  1245. IsAvailable = false;
  1246. LOG_INFO("Cabinet >> Connector %d Availability [Disable]", packet->Header.id);
  1247. }
  1248. else if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable)
  1249. {
  1250. IsAvailable = false;
  1251. LOG_INFO("Cabinet >> Connector %d Availability [Inoperative]", packet->Header.id);
  1252. }
  1253. else
  1254. {
  1255. LOG_INFO("Cabinet >> Connector %d Availability [Enable]", packet->Header.id);
  1256. }
  1257. misc.Value = IsAvailable;
  1258. AddMiscCommand(&sendBuffer, &misc);
  1259. }
  1260. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_START))
  1261. {
  1262. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_START);
  1263. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStartConfirm = true;
  1264. misc.Command = _MiscCmd_RemoteStart;
  1265. misc.Value = true;
  1266. LOG_INFO("Cabinet >> Connector %d RemoteStart [%d]", packet->Header.id, misc.Value);
  1267. AddMiscCommand(&sendBuffer, &misc);
  1268. }
  1269. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_STOP))
  1270. {
  1271. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_REMOTE_STOP);
  1272. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.RemoteStopConfirm = true;
  1273. misc.Command = _MiscCmd_RemoteStop;
  1274. misc.Value = true;
  1275. LOG_INFO("Cabinet >> Connector %d Remote Stop: %d", packet->Header.id, misc.Value);
  1276. AddMiscCommand(&sendBuffer, &misc);
  1277. }
  1278. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_UNLOCK))
  1279. {
  1280. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_UNLOCK);
  1281. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.UnlockStopConfirm = true;
  1282. misc.Command = _MiscCmd_UnlockStop;
  1283. misc.Value = true;
  1284. LOG_INFO("Cabinet >> Connector %d Unlock Stop: %d", packet->Header.id, misc.Value);
  1285. AddMiscCommand(&sendBuffer, &misc);
  1286. }
  1287. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_ACCOUNT_BALANCE))
  1288. {
  1289. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_ACCOUNT_BALANCE);
  1290. misc.Command = _MiscCmd_AccountBalance;
  1291. misc.Value = (int)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AccountBalance * 100);
  1292. LOG_INFO("Cabinet >> Connector %d Balance [%d]", packet->Header.id, (misc.Value));
  1293. AddMiscCommand(&sendBuffer, &misc);
  1294. }
  1295. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_RESERVATION))
  1296. {
  1297. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_RESERVATION);
  1298. misc.Command = _MiscCmd_Reservation;
  1299. misc.Value = true;
  1300. LOG_INFO("Cabinet >> Connector %d Reservation [Enable]", packet->Header.id);
  1301. AddMiscCommand(&sendBuffer, &misc);
  1302. }
  1303. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_QRCODE_INFO))
  1304. {
  1305. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_QRCODE_INFO);
  1306. misc.Command = _MiscCmd_QRCodeRequest;
  1307. misc.Value = true;
  1308. LOG_INFO("Cabinet >> Connector %d QR Code [Enable]", packet->Header.id);
  1309. AddMiscCommand(&sendBuffer, &misc);
  1310. }
  1311. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_FINAL_COST))
  1312. {
  1313. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_FINAL_COST);
  1314. misc.Command = _MiscCmd_FinalCost;
  1315. misc.Value = true;
  1316. LOG_INFO("Cabinet >> Connector %d Final Cost [Enable]", packet->Header.id);
  1317. AddMiscCommand(&sendBuffer, &misc);
  1318. }
  1319. if(Is_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_LINE_STATUS))
  1320. {
  1321. Clean_Connector_MiscCommand(packet->Header.id - 1, MISC_CONN_LINE_STATUS);
  1322. misc.Command = _MiscCmd_LineStatus;
  1323. misc.Value = ShmChargerInfo->LineApi.StatusCode[packet->Header.id - 1];
  1324. LOG_INFO("Cabinet >> Connector %d Line Status [%d]", packet->Header.id, misc.Value);
  1325. AddMiscCommand(&sendBuffer, &misc);
  1326. }
  1327. // announce misc command to first connector of the dispenser
  1328. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  1329. {
  1330. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CONNECTION_TIMEOUT))
  1331. {
  1332. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CONNECTION_TIMEOUT);
  1333. misc.Command = _MiscCmd_ConnectorTimeout;
  1334. misc.Value = ShmChargerInfo->CabinetMiscValue.ConnectionTimeout;
  1335. LOG_INFO("Cabinet >> Dispenser %d PlugTimeout [%d]", dispenserIndex + 1, misc.Value);
  1336. AddMiscCommand(&sendBuffer, &misc);
  1337. }
  1338. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_DEFAULT_PRICE))
  1339. {
  1340. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_DEFAULT_PRICE);
  1341. misc.Command = _MiscCmd_DefaultPrice;
  1342. misc.Value = (unsigned int)(ShmChargerInfo->CabinetMiscValue.DefaultPrice * 100);
  1343. LOG_INFO("Cabinet >> Dispenser %d DefaultPrice [%d.%02d]", dispenserIndex + 1, (misc.Value / 100), (misc.Value % 100));
  1344. AddMiscCommand(&sendBuffer, &misc);
  1345. }
  1346. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CURRENCY))
  1347. {
  1348. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CURRENCY);
  1349. misc.Command = _MiscCmd_Currency;
  1350. misc.Value = ShmChargerInfo->CabinetMiscValue.Currency;
  1351. LOG_INFO("Cabinet >> Dispenser %d Currency [%d]", dispenserIndex + 1, misc.Value);
  1352. AddMiscCommand(&sendBuffer, &misc);
  1353. }
  1354. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BACKEND_STATUS))
  1355. {
  1356. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BACKEND_STATUS);
  1357. misc.Command = _MiscCmd_BackendStatus;
  1358. misc.Value = ShmChargerInfo->CabinetMiscValue.BackendStatus;
  1359. if(misc.Value <= _Connnection_Disconnected)
  1360. {
  1361. LOG_INFO("Cabinet >> Dispenser %d BackendConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1362. }
  1363. else
  1364. {
  1365. LOG_INFO("Cabinet >> Dispenser %d BackendConn [%d]", dispenserIndex + 1, (misc.Value));
  1366. }
  1367. AddMiscCommand(&sendBuffer, &misc);
  1368. }
  1369. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_ETHERNET_STATUS))
  1370. {
  1371. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_ETHERNET_STATUS);
  1372. misc.Command = _MiscCmd_EthernetStatus;
  1373. misc.Value = ShmChargerInfo->CabinetMiscValue.EthernetStatus;
  1374. if(misc.Value <= _Connnection_Disconnected)
  1375. {
  1376. LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1377. }
  1378. else
  1379. {
  1380. LOG_INFO("Cabinet >> Dispenser %d EthernetConn [%d]", dispenserIndex + 1, (misc.Value));
  1381. }
  1382. AddMiscCommand(&sendBuffer, &misc);
  1383. }
  1384. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_WIFI_STATUS))
  1385. {
  1386. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_WIFI_STATUS);
  1387. misc.Command = _MiscCmd_WiFiStatus;
  1388. misc.Value = ShmChargerInfo->CabinetMiscValue.WiFiStatus;
  1389. if(misc.Value <= _Connnection_Disconnected)
  1390. {
  1391. LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1392. }
  1393. else
  1394. {
  1395. LOG_INFO("Cabinet >> Dispenser %d WiFiConn [%d]", dispenserIndex + 1, (misc.Value));
  1396. }
  1397. AddMiscCommand(&sendBuffer, &misc);
  1398. }
  1399. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TELCOM_MODEM_STATUS))
  1400. {
  1401. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TELCOM_MODEM_STATUS);
  1402. misc.Command = _MiscCmd_4GStatus;
  1403. misc.Value = ShmChargerInfo->CabinetMiscValue.TelcomModemStatus;
  1404. if(misc.Value <= _Connnection_Disconnected)
  1405. {
  1406. LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%s]", dispenserIndex + 1, strConnection[misc.Value]);
  1407. }
  1408. else
  1409. {
  1410. LOG_INFO("Cabinet >> Dispenser %d TelModemConn [%d]", dispenserIndex + 1, (misc.Value));
  1411. }
  1412. AddMiscCommand(&sendBuffer, &misc);
  1413. }
  1414. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BILLING_STATUS))
  1415. {
  1416. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BILLING_STATUS);
  1417. misc.Command = _MiscCmd_Billing;
  1418. misc.Value = ShmChargerInfo->CabinetMiscValue.BillingStatus;
  1419. LOG_INFO("Cabinet >> Dispenser %d BillingInfo [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1420. AddMiscCommand(&sendBuffer, &misc);
  1421. }
  1422. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STOP_BUTTON_STATUS))
  1423. {
  1424. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STOP_BUTTON_STATUS);
  1425. misc.Command = _MiscCmd_StopButton;
  1426. misc.Value = ShmChargerInfo->CabinetMiscValue.StopChargingButton;
  1427. LOG_INFO("Cabinet >> Dispenser %d StopButton [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1428. AddMiscCommand(&sendBuffer, &misc);
  1429. }
  1430. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_HARDWARE_REBOOT))
  1431. {
  1432. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_HARDWARE_REBOOT);
  1433. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.HardwareRebootConfirm = true;
  1434. misc.Command = _MiscCmd_HardwareReboot;
  1435. misc.Value = true;
  1436. LOG_INFO("Cabinet >> Dispenser %d HardReboot [%d]", dispenserIndex + 1, misc.Value);
  1437. AddMiscCommand(&sendBuffer, &misc);
  1438. }
  1439. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_SOFTWARE_RESET))
  1440. {
  1441. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_SOFTWARE_RESET);
  1442. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.SoftwareResetConfirm = true;
  1443. misc.Command = _MiscCmd_SoftwareRestart;
  1444. misc.Value = true;
  1445. LOG_INFO("Cabinet >> Dispenser %d SoftReset [%d]", dispenserIndex + 1, misc.Value);
  1446. AddMiscCommand(&sendBuffer, &misc);
  1447. }
  1448. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_AUTH_MODE_CONFIG))
  1449. {
  1450. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_AUTH_MODE_CONFIG);
  1451. misc.Command = _MiscCmd_AuthDisable;
  1452. misc.Value = ShmChargerInfo->CabinetMiscValue.AuthModeConfig;
  1453. LOG_INFO("Cabinet >> Dispenser %d AuthMode [%s]", dispenserIndex + 1, misc.Value ? "Disable" : "Enable");
  1454. AddMiscCommand(&sendBuffer, &misc);
  1455. }
  1456. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_EVCCID_CONFIG))
  1457. {
  1458. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_EVCCID_CONFIG);
  1459. misc.Command = _MiscCmd_EVCCIDEnable;
  1460. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName[0] == 'D' &&
  1461. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName[0] == 'X')
  1462. {
  1463. misc.Value = ShmChargerInfo->CabinetMiscValue.AuthModeConfig > 0 ? 0 : 1;
  1464. }
  1465. else
  1466. {
  1467. misc.Value = ShmChargerInfo->CabinetMiscValue.EVCCIDConfig;
  1468. }
  1469. LOG_INFO("Cabinet >> Dispenser %d EVCCID [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1470. AddMiscCommand(&sendBuffer, &misc);
  1471. }
  1472. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_LED_INTENSITY))
  1473. {
  1474. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_LED_INTENSITY);
  1475. misc.Command = _MiscCmd_LEDIntensity;
  1476. misc.Value = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1477. if(misc.Value <= _LED_INTENSITY_BRIGHTEST)
  1478. {
  1479. LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%s]", dispenserIndex + 1, strIntensity[misc.Value]);
  1480. }
  1481. else
  1482. {
  1483. LOG_INFO("Cabinet >> Dispenser %d LEDIntensity [%d]", dispenserIndex + 1, misc.Value);
  1484. }
  1485. AddMiscCommand(&sendBuffer, &misc);
  1486. }
  1487. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STATION_INFO))
  1488. {
  1489. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STATION_INFO);
  1490. misc.Command = _MiscCmd_StationInfo;
  1491. misc.Value = true;
  1492. LOG_INFO("Cabinet >> Dispenser %d Station Info [%s]", dispenserIndex + 1, misc.Value ? "Enable" : "Disable");
  1493. AddMiscCommand(&sendBuffer, &misc);
  1494. }
  1495. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TIME_OFFSET))
  1496. {
  1497. Clean_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TIME_OFFSET);
  1498. misc.Command = _MiscCmd_TimeOffset;
  1499. misc.Value = ShmChargerInfo->CabinetMiscValue.TimeOffset;
  1500. LOG_INFO("Cabinet >> Dispenser %d Time Offset [%s%02d:%02d]",
  1501. dispenserIndex + 1, misc.Value >= 0 ? "+" : "-", (abs(misc.Value) / 60), (abs(misc.Value) % 60));
  1502. AddMiscCommand(&sendBuffer, &misc);
  1503. }
  1504. }
  1505. if(Is_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CHANGE_LCM_INFO))
  1506. {
  1507. int gun = packet->Header.id - 1;
  1508. for(int i = 0; i < _LCM_Page_RemoteStartNoID; i++)
  1509. {
  1510. unsigned int pageReq = 1 << i;
  1511. if(Is_LcmPageChange_Req(gun, pageReq))
  1512. {
  1513. Clean_LcmPage_Req(gun, pageReq);
  1514. misc.Command = _MiscCmd_ChangeLcmPage;
  1515. misc.Value = i + 1;
  1516. LOG_INFO("Cabinet >> Connector %d LCM_Page [%s]", packet->Header.id, strLcmPage[misc.Value]);
  1517. AddMiscCommand(&sendBuffer, &misc);
  1518. }
  1519. }
  1520. }
  1521. }
  1522. SendPacket(socket, &sendBuffer);
  1523. }
  1524. void WriteCsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1525. {
  1526. struct PACKET_STRUCTURE sendBuffer;
  1527. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1528. sendBuffer.Header.se = packet->Header.se;
  1529. sendBuffer.Header.id = packet->Header.id;
  1530. sendBuffer.Header.op = _Header_Response;
  1531. sendBuffer.Header.len = 2;
  1532. sendBuffer.Payload.reg = _Reg_Report_Csu_Version;
  1533. sendBuffer.Payload.data[0] = result;
  1534. SendPacket(socket, &sendBuffer);
  1535. }
  1536. void WriteOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1537. {
  1538. struct PACKET_STRUCTURE sendBuffer;
  1539. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1540. sendBuffer.Header.se = packet->Header.se;
  1541. sendBuffer.Header.id = packet->Header.id;
  1542. sendBuffer.Header.op = _Header_Response;
  1543. sendBuffer.Header.len = 2;
  1544. sendBuffer.Payload.reg = _Reg_Report_Other_Version;
  1545. sendBuffer.Payload.data[0] = result;
  1546. SendPacket(socket, &sendBuffer);
  1547. }
  1548. void WriteChargingInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1549. {
  1550. struct PACKET_STRUCTURE sendBuffer;
  1551. unsigned short voltage = 0, current = 0;
  1552. unsigned char gun = packet->Header.id - 1;
  1553. #if 0
  1554. // for safety test
  1555. voltage = (unsigned short)(chargingInfo[gun]->PresentChargingVoltage * 10);
  1556. current = (unsigned short)(chargingInfo[gun]->PresentChargingCurrent * 10);
  1557. voltage -= ((current * 8 * 10) / 10000);
  1558. #endif
  1559. if(chargingInfo[gun]->PantographFlag == YES)
  1560. {
  1561. voltage = (unsigned short)(chargingInfo[gun]->PresentChargingVoltage * 10);
  1562. current = (unsigned short)(chargingInfo[gun]->PresentChargingCurrent * 10);
  1563. }
  1564. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1565. sendBuffer.Header.se = packet->Header.se;
  1566. sendBuffer.Header.id = packet->Header.id;
  1567. sendBuffer.Header.op = _Header_Response;
  1568. sendBuffer.Header.len = 6;
  1569. sendBuffer.Payload.reg = _Reg_Charging_Info;
  1570. sendBuffer.Payload.data[0] = result;
  1571. sendBuffer.Payload.data[1] = (voltage >> 8) & 0xFF;
  1572. sendBuffer.Payload.data[2] = (voltage >> 0) & 0xFF;
  1573. sendBuffer.Payload.data[3] = (current >> 8) & 0xFF;
  1574. sendBuffer.Payload.data[4] = (current >> 0) & 0xFF;
  1575. SendPacket(socket, &sendBuffer);
  1576. }
  1577. int GetChargerSystemId(char *id)
  1578. {
  1579. int len = 0;
  1580. strcpy((char *)id, "");
  1581. if(ShmChargerInfo->Control.CustomerCode != _CUSTOMER_CODE_Audi &&
  1582. ShmChargerInfo->Control.CustomerCode != _CUSTOMER_CODE_TCC)
  1583. {
  1584. ShmChargerInfo->AuthInfo.QRCodeMode = ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode;
  1585. }
  1586. switch(ShmChargerInfo->AuthInfo.QRCodeMode)
  1587. {
  1588. case _QR_MODE_Customized:
  1589. strcpy((char *)id, (char *)ShmSysConfigAndInfo->SysConfig.QRCodeContent);
  1590. break;
  1591. case _QR_MODE_ChargeBoxId:
  1592. strcpy((char *)id, (char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1593. break;
  1594. case _QR_MODE_Default:
  1595. default:
  1596. strcat((char *)id, (char *)ShmSysConfigAndInfo->SysConfig.SystemId);
  1597. break;
  1598. }
  1599. len = strlen((char *)id);
  1600. return len;
  1601. }
  1602. void ChargerSystemIdResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1603. {
  1604. struct PACKET_STRUCTURE sendBuffer;
  1605. char system_id[128];
  1606. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1607. sendBuffer.Header.se = packet->Header.se;
  1608. sendBuffer.Header.id = packet->Header.id;
  1609. sendBuffer.Header.op = _Header_Response;
  1610. sendBuffer.Header.len = 2;
  1611. sendBuffer.Payload.reg = _Reg_Charger_System_Id;
  1612. sendBuffer.Payload.data[0] = result;
  1613. if(result == _R_OK)
  1614. {
  1615. int strLen = 0;
  1616. strLen = GetChargerSystemId(system_id);
  1617. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], (char *)system_id, strLen);
  1618. sendBuffer.Header.len += strLen + 1;
  1619. char TimeSync[32];
  1620. struct timeb SeqEndTime;
  1621. struct tm *tm;
  1622. ftime(&SeqEndTime);
  1623. SeqEndTime.time = time(NULL);
  1624. tm=localtime(&SeqEndTime.time);
  1625. memset(TimeSync, 0x00, 32);
  1626. sprintf(TimeSync, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  1627. strLen = strlen(TimeSync);
  1628. memcpy(&sendBuffer.Payload.data[sendBuffer.Header.len - 1], TimeSync, strLen);
  1629. sendBuffer.Header.len += strLen + 1;
  1630. //LOG_INFO("Dispenser %d Read System Id: %s", dispenserIndex + 1, ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1631. }
  1632. SendPacket(socket, &sendBuffer);
  1633. }
  1634. void WriteWaitPlugInResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1635. {
  1636. struct PACKET_STRUCTURE sendBuffer;
  1637. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1638. sendBuffer.Header.se = packet->Header.se;
  1639. sendBuffer.Header.id = packet->Header.id;
  1640. sendBuffer.Header.op = _Header_Response;
  1641. sendBuffer.Header.len = 2;
  1642. sendBuffer.Payload.reg = _Reg_WaitPlugIn;
  1643. sendBuffer.Payload.data[0] = result;
  1644. SendPacket(socket, &sendBuffer);
  1645. }
  1646. void GroundFaultDetectionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1647. {
  1648. struct PACKET_STRUCTURE sendBuffer;
  1649. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1650. sendBuffer.Header.se = packet->Header.se;
  1651. sendBuffer.Header.id = packet->Header.id;
  1652. sendBuffer.Header.op = _Header_Response;
  1653. sendBuffer.Header.len = 3;
  1654. sendBuffer.Payload.reg = _Reg_GroundFaultDetection;
  1655. sendBuffer.Payload.data[0] = result;
  1656. sendBuffer.Payload.data[1] = result == _R_OK ? chargingInfo[packet->Header.id - 1]->GroundFaultStatus : GFD_WAIT;
  1657. SendPacket(socket, &sendBuffer);
  1658. }
  1659. void CabinetCSUVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1660. {
  1661. struct PACKET_STRUCTURE sendBuffer;
  1662. char version[64];
  1663. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1664. sendBuffer.Header.se = packet->Header.se;
  1665. sendBuffer.Header.id = packet->Header.id;
  1666. sendBuffer.Header.op = _Header_Response;
  1667. sendBuffer.Header.len = 194;
  1668. sendBuffer.Payload.reg = _Reg_Get_Cabinet_CSU_Version;
  1669. sendBuffer.Payload.data[0] = result;
  1670. memset(version, 0x00, sizeof(version));
  1671. strcpy(version, (char *)&ShmSysConfigAndInfo->SysConfig.ModelName[0]);
  1672. memcpy(&sendBuffer.Payload.data[1], version, 32);
  1673. memset(version, 0x00, sizeof(version));
  1674. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev[0]);
  1675. memcpy(&sendBuffer.Payload.data[33], version, 32);
  1676. memset(version, 0x00, sizeof(version));
  1677. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev[0]);
  1678. memcpy(&sendBuffer.Payload.data[65], version, 32);
  1679. memset(version, 0x00, sizeof(version));
  1680. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[0]);
  1681. memcpy(&sendBuffer.Payload.data[97], version, 32);
  1682. memset(version, 0x00, sizeof(version));
  1683. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[0]);
  1684. memcpy(&sendBuffer.Payload.data[129], version, 32);
  1685. memset(version, 0x00, sizeof(version));
  1686. strcpy(version, (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress[0]);
  1687. memcpy(&sendBuffer.Payload.data[161], version, 32);
  1688. SendPacket(socket, &sendBuffer);
  1689. }
  1690. void CabinetOtherVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1691. {
  1692. struct PACKET_STRUCTURE sendBuffer;
  1693. char version[64];
  1694. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1695. sendBuffer.Header.se = packet->Header.se;
  1696. sendBuffer.Header.id = packet->Header.id;
  1697. sendBuffer.Header.op = _Header_Response;
  1698. sendBuffer.Header.len = 98;
  1699. sendBuffer.Payload.reg = _Reg_Get_Cabinet_Other_Version;
  1700. sendBuffer.Payload.data[0] = result;
  1701. memset(version, 0x00, sizeof(version));
  1702. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0]);
  1703. memcpy(&sendBuffer.Payload.data[1], version, 32);
  1704. memset(version, 0x00, sizeof(version));
  1705. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev[0]);
  1706. memcpy(&sendBuffer.Payload.data[33], version, 32);
  1707. memset(version, 0x00, sizeof(version));
  1708. strcpy(version, (char *)&ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0]);
  1709. memcpy(&sendBuffer.Payload.data[65], version, 32);
  1710. SendPacket(socket, &sendBuffer);
  1711. }
  1712. void PsuQuantityResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1713. {
  1714. struct PACKET_STRUCTURE sendBuffer;
  1715. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1716. sendBuffer.Header.se = packet->Header.se;
  1717. sendBuffer.Header.id = packet->Header.id;
  1718. sendBuffer.Header.op = _Header_Response;
  1719. sendBuffer.Header.len = 3;
  1720. sendBuffer.Payload.reg = _Reg_Get_Psu_Quantity;
  1721. sendBuffer.Payload.data[0] = result;
  1722. sendBuffer.Payload.data[1] = ShmPsuData->SystemPresentPsuQuantity;
  1723. SendPacket(socket, &sendBuffer);
  1724. }
  1725. void PsuVersionResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, int psuIndex)
  1726. {
  1727. struct PACKET_STRUCTURE sendBuffer;
  1728. char version[64];
  1729. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1730. sendBuffer.Header.se = packet->Header.se;
  1731. sendBuffer.Header.id = packet->Header.id;
  1732. sendBuffer.Header.op = _Header_Response;
  1733. sendBuffer.Header.len = 3 + 64;
  1734. sendBuffer.Payload.reg = _Reg_Get_Psu_Version;
  1735. sendBuffer.Payload.data[0] = result;
  1736. sendBuffer.Payload.data[1] = psuIndex;
  1737. memset(version, 0x00, sizeof(version));
  1738. strcpy(version, (char *)&ShmPsuData->PsuVersion[psuIndex].FwPrimaryVersion[0]);
  1739. memcpy(&sendBuffer.Payload.data[2], version, 32);
  1740. memset(version, 0x00, sizeof(version));
  1741. strcpy(version, (char *)&ShmPsuData->PsuVersion[psuIndex].FwSecondVersion[0]);
  1742. memcpy(&sendBuffer.Payload.data[34], version, 32);
  1743. SendPacket(socket, &sendBuffer);
  1744. }
  1745. void ReservationResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char _reservation)
  1746. {
  1747. struct PACKET_STRUCTURE sendBuffer;
  1748. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1749. sendBuffer.Header.se = packet->Header.se;
  1750. sendBuffer.Header.id = packet->Header.id;
  1751. sendBuffer.Header.op = _Header_Response;
  1752. sendBuffer.Header.len = 3;
  1753. sendBuffer.Payload.reg = _Reg_Get_Reservation;
  1754. sendBuffer.Payload.data[0] = result;
  1755. sendBuffer.Payload.data[1] = _reservation;
  1756. if(_reservation)
  1757. {
  1758. strcpy((char *)&sendBuffer.Payload.data[2], &ShmChargerInfo->Control.ResevedIdTag[packet->Header.id - 1][0]);
  1759. sendBuffer.Header.len += strlen(&ShmChargerInfo->Control.ResevedIdTag[packet->Header.id - 1][0]);
  1760. }
  1761. // '\0'
  1762. sendBuffer.Header.len += 1;
  1763. SendPacket(socket, &sendBuffer);
  1764. }
  1765. void DispenserReqHandlerResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1766. {
  1767. struct PACKET_STRUCTURE sendBuffer;
  1768. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1769. sendBuffer.Header.se = packet->Header.se;
  1770. sendBuffer.Header.id = packet->Header.id;
  1771. sendBuffer.Header.op = _Header_Response;
  1772. sendBuffer.Header.len = 2;
  1773. sendBuffer.Payload.reg = _Reg_Dispenser_Request;
  1774. sendBuffer.Payload.data[0] = result;
  1775. SendPacket(socket, &sendBuffer);
  1776. }
  1777. void RemoteStarNoIDResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result, unsigned char state)
  1778. {
  1779. struct PACKET_STRUCTURE sendBuffer;
  1780. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1781. sendBuffer.Header.se = packet->Header.se;
  1782. sendBuffer.Header.id = packet->Header.id;
  1783. sendBuffer.Header.op = _Header_Response;
  1784. sendBuffer.Header.len = 3;
  1785. sendBuffer.Payload.reg = _Reg_RemoteStartNoIDState;
  1786. sendBuffer.Payload.data[0] = result;
  1787. sendBuffer.Payload.data[1] = state;
  1788. SendPacket(socket, &sendBuffer);
  1789. }
  1790. void RefundAmountResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1791. {
  1792. struct PACKET_STRUCTURE sendBuffer;
  1793. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1794. sendBuffer.Header.se = packet->Header.se;
  1795. sendBuffer.Header.id = packet->Header.id;
  1796. sendBuffer.Header.op = _Header_Response;
  1797. sendBuffer.Header.len = 2 + 128;
  1798. sendBuffer.Payload.reg = _Reg_RefundAmount;
  1799. sendBuffer.Payload.data[0] = result;
  1800. memcpy(&sendBuffer.Payload.data[1], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1801. memcpy(&sendBuffer.Payload.data[33], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1802. memcpy(&sendBuffer.Payload.data[65], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1803. memcpy(&sendBuffer.Payload.data[97], &ConnectorRefund[packet->Header.id - 1].ChargingDate, 32);
  1804. SendPacket(socket, &sendBuffer);
  1805. }
  1806. void PrepaymentResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1807. {
  1808. struct PACKET_STRUCTURE sendBuffer;
  1809. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1810. sendBuffer.Header.se = packet->Header.se;
  1811. sendBuffer.Header.id = packet->Header.id;
  1812. sendBuffer.Header.op = _Header_Response;
  1813. sendBuffer.Header.len = 3 + 128;
  1814. sendBuffer.Payload.reg = _Reg_PrepaymentInfo;
  1815. sendBuffer.Payload.data[0] = result;
  1816. sendBuffer.Payload.data[1] = ConnectorPrepayment[packet->Header.id - 1].PaymentType;
  1817. memcpy(&sendBuffer.Payload.data[2], &ConnectorPrepayment[packet->Header.id - 1].TopUp, 32);
  1818. memcpy(&sendBuffer.Payload.data[34], &ConnectorPrepayment[packet->Header.id - 1].AccountAmount, 32);
  1819. memcpy(&sendBuffer.Payload.data[66], &ConnectorPrepayment[packet->Header.id - 1].Prepayment, 32);
  1820. memcpy(&sendBuffer.Payload.data[98], &ConnectorPrepayment[packet->Header.id - 1].Balance, 32);
  1821. SendPacket(socket, &sendBuffer);
  1822. }
  1823. void PaymentFailReasonResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1824. {
  1825. struct PACKET_STRUCTURE sendBuffer;
  1826. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1827. sendBuffer.Header.se = packet->Header.se;
  1828. sendBuffer.Header.id = packet->Header.id;
  1829. sendBuffer.Header.op = _Header_Response;
  1830. sendBuffer.Header.len = 3;
  1831. sendBuffer.Payload.reg = _Reg_PaymentFailReason;
  1832. sendBuffer.Payload.data[0] = result;
  1833. sendBuffer.Payload.data[1] = ConnectorPaymentFailReason[packet->Header.id - 1].ReasonIndex;
  1834. if(ConnectorPaymentFailReason[packet->Header.id - 1].ReasonIndex == 0)
  1835. {
  1836. int len = strlen(ConnectorPaymentFailReason[packet->Header.id - 1].Reason) + 1;
  1837. memcpy(&sendBuffer.Payload.data[2], &ConnectorPaymentFailReason[packet->Header.id - 1].Reason, len);
  1838. sendBuffer.Header.len += len;
  1839. }
  1840. SendPacket(socket, &sendBuffer);
  1841. }
  1842. void QRCodeResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1843. {
  1844. struct PACKET_STRUCTURE sendBuffer;
  1845. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1846. sendBuffer.Header.se = packet->Header.se;
  1847. sendBuffer.Header.id = packet->Header.id;
  1848. sendBuffer.Header.op = _Header_Response;
  1849. sendBuffer.Header.len = 2;
  1850. sendBuffer.Payload.reg = _Reg_ConnectorQRCode;
  1851. sendBuffer.Payload.data[0] = result;
  1852. if(result == _R_OK)
  1853. {
  1854. char qrCode[128];
  1855. int qrCodeLen = 0;
  1856. memset(qrCode, 0x00, sizeof(qrCode));
  1857. qrCodeLen = strlen(ShmChargerInfo->StationInfo.QRCode[sendBuffer.Header.id - 1]);
  1858. if(qrCodeLen > 0)
  1859. {
  1860. memcpy(qrCode, ShmChargerInfo->StationInfo.QRCode[sendBuffer.Header.id - 1], qrCodeLen);
  1861. }
  1862. else
  1863. {
  1864. qrCodeLen = GetChargerSystemId(qrCode);
  1865. sprintf(&qrCode[qrCodeLen], "_%d", packet->Header.id - 1);
  1866. qrCodeLen = strlen(qrCode);
  1867. }
  1868. if(strcmp((char *)&_ConnectorQRCode[packet->Header.id - 1][0], qrCode) != 0)
  1869. {
  1870. strcpy((char *)&_ConnectorQRCode[packet->Header.id - 1][0], qrCode);
  1871. LOG_INFO("Gun %d QR Code: [%s]", packet->Header.id, &_ConnectorQRCode[packet->Header.id - 1][0]);
  1872. }
  1873. memcpy(&sendBuffer.Payload.data[1], (char *)qrCode, qrCodeLen);
  1874. sendBuffer.Header.len += qrCodeLen;
  1875. }
  1876. // '\0'
  1877. sendBuffer.Header.len += 1;
  1878. SendPacket(socket, &sendBuffer);
  1879. }
  1880. void StationInfoResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char result)
  1881. {
  1882. char strWeather[16];
  1883. char *str_weather_type[] = {STR_WEATHER_NONE, STR_WEATHER_SUNNY, STR_WEATHER_CLOUDY, STR_WEATHER_RAIN, STR_WEATHER_THUNDERSTORM, STR_WEATHER_SNOW, STR_WEATHER_MIST};
  1884. struct PACKET_STRUCTURE sendBuffer;
  1885. memset(strWeather, 0x00, sizeof(strWeather));
  1886. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1887. sendBuffer.Header.se = packet->Header.se;
  1888. sendBuffer.Header.id = packet->Header.id;
  1889. sendBuffer.Header.op = _Header_Response;
  1890. sendBuffer.Header.len = 78;
  1891. sendBuffer.Payload.reg = _Reg_StationInfo;
  1892. sendBuffer.Payload.data[0] = result;
  1893. sendBuffer.Payload.data[1] = ((ShmChargerInfo->StationInfo.StationID >> 24) & 0xFF);
  1894. sendBuffer.Payload.data[2] = ((ShmChargerInfo->StationInfo.StationID >> 16) & 0xFF);
  1895. sendBuffer.Payload.data[3] = ((ShmChargerInfo->StationInfo.StationID >> 8) & 0xFF);
  1896. sendBuffer.Payload.data[4] = (ShmChargerInfo->StationInfo.StationID & 0xFF);
  1897. memcpy(&sendBuffer.Payload.data[5], ShmChargerInfo->StationInfo.StationName, 64);
  1898. sendBuffer.Payload.data[69] = (ShmChargerInfo->StationInfo.WeatherID >> 24) & 0xFF;
  1899. sendBuffer.Payload.data[70] = (ShmChargerInfo->StationInfo.WeatherID >> 16) & 0xFF;
  1900. sendBuffer.Payload.data[71] = (ShmChargerInfo->StationInfo.WeatherID >> 8) & 0xFF;
  1901. sendBuffer.Payload.data[72] = ShmChargerInfo->StationInfo.WeatherID & 0xFF;
  1902. char *ptr = (char *)&ShmChargerInfo->StationInfo.Temperature;
  1903. sendBuffer.Payload.data[73] = *(ptr + 3);
  1904. sendBuffer.Payload.data[74] = *(ptr + 2);
  1905. sendBuffer.Payload.data[75] = *(ptr + 1);
  1906. sendBuffer.Payload.data[76] = *ptr;
  1907. if(strcmp(_stationInfo[dispenserIndex].StationName, ShmChargerInfo->StationInfo.StationName) != 0 ||
  1908. _stationInfo[dispenserIndex].StationID != ShmChargerInfo->StationInfo.StationID ||
  1909. _stationInfo[dispenserIndex].WeatherID != ShmChargerInfo->StationInfo.WeatherID ||
  1910. _stationInfo[dispenserIndex].Temperature != ShmChargerInfo->StationInfo.Temperature)
  1911. {
  1912. memcpy((char *)&_stationInfo[dispenserIndex], (char *)&ShmChargerInfo->StationInfo, sizeof(StationInfoData));
  1913. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  1914. {
  1915. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  1916. }
  1917. else
  1918. {
  1919. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  1920. }
  1921. LOG_INFO("Dispenser %d StationID: %d, Name: %s, WeatherID: %s, Temperature: %4.1f",
  1922. dispenserIndex + 1,
  1923. ShmChargerInfo->StationInfo.StationID,
  1924. ShmChargerInfo->StationInfo.StationName,
  1925. strWeather,
  1926. ShmChargerInfo->StationInfo.Temperature);
  1927. }
  1928. SendPacket(socket, &sendBuffer);
  1929. }
  1930. void DeductResultResponse(int socket, struct PACKET_STRUCTURE *packet, unsigned char result)
  1931. {
  1932. struct PACKET_STRUCTURE sendBuffer;
  1933. memset(&sendBuffer, 0x00, sizeof(sendBuffer));
  1934. sendBuffer.Header.se = packet->Header.se;
  1935. sendBuffer.Header.id = packet->Header.id;
  1936. sendBuffer.Header.op = _Header_Response;
  1937. sendBuffer.Header.len = 2;
  1938. sendBuffer.Payload.reg = _Reg_DeductResult;
  1939. sendBuffer.Payload.data[0] = result;
  1940. SendPacket(socket, &sendBuffer);
  1941. }
  1942. BOOL FindConnectorID(unsigned char dispenserIndex, unsigned char id)
  1943. {
  1944. BOOL find = false;
  1945. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  1946. {
  1947. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] == id)
  1948. {
  1949. find = true;
  1950. break;
  1951. }
  1952. }
  1953. return find;
  1954. }
  1955. void ConnectorPhysicalLimitBindingHandler(unsigned char connectorIndex, unsigned char physical)
  1956. {
  1957. // 20210817 remove type "R"
  1958. //char modelList[11] = {'J', 'U', 'V', 'E', 'F', 'G', 'T', 'D', 'K', 'P'};
  1959. //unsigned char typeList[11] = { 0, 1, 1, 1, 1, 2, 1, 1, 0, 1}; // 0 : Chademo, 1: CCS, 2: GB
  1960. switch(physical)
  1961. {
  1962. case 'J':
  1963. case 'K':
  1964. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CHA_MAX_PHYSICAL_VOLTAGE;
  1965. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CHA_NATURAL_MAX_CURRENT;
  1966. break;
  1967. case 'U':
  1968. case 'E':
  1969. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1970. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_NATURAL_MAX_CURRENT;
  1971. break;
  1972. case 'G':
  1973. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = GBT_MAX_PHYSICAL_VOLTAGE;
  1974. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = GBT_NATURAL_MAX_CURRENT;
  1975. break;
  1976. case 'V':
  1977. case 'F':
  1978. case 'P':
  1979. //case 'R': // 20210817 remove type "R"
  1980. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1981. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_LIQUID_MAX_CURRENT;
  1982. break;
  1983. case 'T':
  1984. case 'D':
  1985. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = CCS_MAX_PHYSICAL_VOLTAGE;
  1986. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = CCS_NATURAL_REMA_MAX_CURRENT;
  1987. break;
  1988. default:
  1989. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalVoltage = 0;
  1990. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connectorIndex].RemoteMaxPhysicalCurrent = 0;
  1991. break;
  1992. }
  1993. }
  1994. void ConnectorPantographBindingHandler(unsigned char connectorIndex, unsigned char physical)
  1995. {
  1996. switch(physical)
  1997. {
  1998. case 'P':
  1999. chargingInfo[connectorIndex]->PantographFlag = YES;
  2000. break;
  2001. default:
  2002. chargingInfo[connectorIndex]->PantographFlag = NO;
  2003. break;
  2004. }
  2005. }
  2006. void ConnectorTypeBindingHandler(unsigned char dispenserIndex, unsigned char *type, unsigned char *physicalType)
  2007. {
  2008. char *str_gun_type[] = {STR_GUN_TYPE_CHADEMO, STR_GUN_TYPE_CCS, STR_GUN_TYPE_GBT};
  2009. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  2010. {
  2011. unsigned char gunIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  2012. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex = dispenserIndex;
  2013. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Index = gunIndex;
  2014. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.Type = type[i];
  2015. ConnectorPhysicalLimitBindingHandler(gunIndex, physicalType[i]);
  2016. ConnectorPantographBindingHandler(gunIndex, physicalType[i]);
  2017. LOG_INFO("Dispenser %d Connector %d type: %s, MaxVol: %4d, MaxCur: %4d, %s",
  2018. dispenserIndex + 1, gunIndex + 1, str_gun_type[type[i]],
  2019. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage,
  2020. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent,
  2021. chargingInfo[gunIndex]->PantographFlag == YES ? "Pantograph" : "Normal");
  2022. }
  2023. }
  2024. BOOL IsAvailableDispenserIndexSequence(unsigned char index)
  2025. {
  2026. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  2027. {
  2028. if(i == index)
  2029. {
  2030. return true;
  2031. }
  2032. if((ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status & (1 << i)) == 0)
  2033. {
  2034. // i is not check in yet before index
  2035. return false;
  2036. }
  2037. }
  2038. return false;
  2039. }
  2040. void DispenserCheckInInfoUpdate(void)
  2041. {
  2042. unsigned char dispenser = 0, connector = 0;
  2043. BOOL change = false;
  2044. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  2045. {
  2046. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  2047. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  2048. {
  2049. dispenser++;
  2050. connector += ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity;
  2051. }
  2052. }
  2053. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity != dispenser ||
  2054. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity != connector)
  2055. {
  2056. change = true;
  2057. }
  2058. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity = dispenser;
  2059. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity = connector;
  2060. if(change)
  2061. {
  2062. LOG_INFO("Total Dispenser: %d, Total Connector: %d\n",
  2063. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity,
  2064. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity);
  2065. }
  2066. }
  2067. void SetConnectorID(unsigned char dispenserIndex, unsigned char quantity)
  2068. {
  2069. unsigned char currentQuantity = 0;
  2070. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] = 0x00;
  2071. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[1] = 0x00;
  2072. for(int i = 0; i < dispenserIndex; i++)
  2073. {
  2074. currentQuantity += ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i];
  2075. }
  2076. for(int i = 0; i < quantity; i++)
  2077. {
  2078. currentQuantity++;
  2079. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] = currentQuantity;
  2080. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].Enable = true;
  2081. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[currentQuantity - 1].GeneralChargingData.Index = currentQuantity - 1;
  2082. }
  2083. }
  2084. BOOL CheckNewDispenserSequence(unsigned char index, unsigned char quantity)
  2085. {
  2086. if(index == ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence)
  2087. {
  2088. LOG_INFO("Get New Dispenser %d, Connector Quantity: %d\n", index + 1, quantity);
  2089. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << index);
  2090. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[index] = quantity;
  2091. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence = index + 1;
  2092. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity += quantity;
  2093. ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[index] = quantity;
  2094. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.FlashConfigChanged = true;
  2095. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence;
  2096. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity;
  2097. return true;
  2098. }
  2099. return false;
  2100. }
  2101. BOOL DispenserIdentificationHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex, unsigned char *modelName)
  2102. {
  2103. unsigned char quantity = 0;
  2104. unsigned char connectorType[2] = {0, 0};
  2105. unsigned char physicalType[2] = {0, 0};
  2106. quantity = ConnectorQuantityTypeParsing(modelName, connectorType, physicalType);
  2107. if(quantity > 0)
  2108. {
  2109. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex] != 0 &&
  2110. quantity != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[dispenserIndex])
  2111. {
  2112. LOG_INFO("The same dispenser id (%d) but connector quantity not match", dispenserIndex + 1);
  2113. return false;
  2114. }
  2115. if(dispenserIndex == ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence &&
  2116. (quantity + ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity) > ShmChargerInfo->Control.MaxConnector)
  2117. {
  2118. LOG_INFO("Dispenser %d connector quantity %d over range, total quantity: %d",
  2119. dispenserIndex + 1, quantity, ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity);
  2120. return false;
  2121. }
  2122. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_None ||
  2123. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus == _DS_Timeout)
  2124. {
  2125. CheckNewDispenserSequence(dispenserIndex, quantity);
  2126. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Identification;
  2127. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity = quantity;
  2128. SetConnectorID(dispenserIndex, quantity);
  2129. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, packet->Payload.data, (packet->Header.len - 2));
  2130. ConnectorTypeBindingHandler(dispenserIndex, connectorType, physicalType);
  2131. LOG_INFO("Dispenser id %d identified, Connector quantity: %d\n", dispenserIndex + 1, quantity);
  2132. DispenserCheckInInfoUpdate();
  2133. return true;
  2134. }
  2135. else
  2136. {
  2137. LOG_INFO("Conflict dispenser ID: %d", dispenserIndex + 1);
  2138. }
  2139. }
  2140. else
  2141. {
  2142. LOG_INFO("Connector quantity fail, model name: %s", modelName);
  2143. }
  2144. return false;
  2145. }
  2146. BOOL DispenserStatusCodeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2147. {
  2148. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2149. if(find)
  2150. {
  2151. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = (packet->Header.len - 1) / 6;
  2152. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 10)
  2153. {
  2154. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount = 10;
  2155. }
  2156. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  2157. {
  2158. memcpy(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  2159. &packet->Payload.data[i * 6], 6);
  2160. }
  2161. /*
  2162. BOOL needUpdate = FALSE;
  2163. // check is there new status code
  2164. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  2165. {
  2166. if(strcmp(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i][0],
  2167. &LastWarningInfo[packet->Header.id - 1].WarningCode[i][0]) == 0)
  2168. {
  2169. needUpdate = TRUE;
  2170. break;
  2171. }
  2172. }
  2173. if(needUpdate)
  2174. {
  2175. memcpy(&LastWarningInfo[packet->Header.id - 1], &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo, sizeof(struct WARNING_CODE_INFO));
  2176. }
  2177. */
  2178. char strDispenserStatus[128];
  2179. sprintf(strDispenserStatus, "Dispenser id %d, Status Code Len: %d", packet->Header.id, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount);
  2180. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount > 0)
  2181. {
  2182. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCount; i++)
  2183. {
  2184. char strTemp[16];
  2185. sprintf(strTemp, ", %s", ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].WarningInfo.WarningCode[i]);
  2186. strcat(strDispenserStatus, strTemp);
  2187. }
  2188. }
  2189. LOG_INFO("%s", strDispenserStatus);
  2190. }
  2191. else
  2192. {
  2193. }
  2194. return find;
  2195. }
  2196. BOOL ConnectorChargingCapabilityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2197. {
  2198. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2199. BOOL done = false;
  2200. if(find)
  2201. {
  2202. done = true;
  2203. }
  2204. return done;
  2205. }
  2206. BOOL ConnectorChargingTargetHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2207. {
  2208. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2209. BOOL done = false;
  2210. unsigned short voltage = 0, current = 0, power = 0;
  2211. if(find)
  2212. {
  2213. voltage = ((packet->Payload.data[0] << 8) + packet->Payload.data[1]);
  2214. current = ((packet->Payload.data[2] << 8) + packet->Payload.data[3]);
  2215. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  2216. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  2217. {
  2218. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage = voltage;
  2219. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent = current;
  2220. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  2221. {
  2222. LOG_INFO("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id, voltage, current);
  2223. }
  2224. }
  2225. GetPhysicalLimitVoltageAndCurrent(packet->Header.id - 1, &voltage, &current);
  2226. GetModelNameLimitPower(packet->Header.id - 1, &power);
  2227. GetConfigLimitPowerAndCurrent(packet->Header.id - 1, &power, &current);
  2228. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage != voltage ||
  2229. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent != current)
  2230. {
  2231. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable)
  2232. {
  2233. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = true;
  2234. LOG_INFO("Connector %d Remote Voltage: %d, Remote Current: %d", packet->Header.id,
  2235. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetVoltage,
  2236. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteTargetCurrent);
  2237. LOG_INFO("Connector %d Limit Voltage: %d, Limit Current: %d", packet->Header.id, voltage, current);
  2238. }
  2239. }
  2240. else
  2241. {
  2242. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.OutputLimitEnable = false;
  2243. }
  2244. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage = voltage;
  2245. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent = current;
  2246. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_PREPARING_FOR_EVSE ||
  2247. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_CHARGING)
  2248. {
  2249. float targetVol = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitVoltage / 10);
  2250. float targetCur = (float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteLimitCurrent / 10);
  2251. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != targetVol ||
  2252. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != targetCur)
  2253. {
  2254. LOG_INFO("Connector %d Target Voltage: %d V, Current: %d A",
  2255. packet->Header.id, (int)(targetVol), (int)(targetCur));
  2256. }
  2257. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = targetVol;
  2258. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = targetCur;
  2259. done = true;
  2260. }
  2261. else
  2262. {
  2263. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage != 0 ||
  2264. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent != 0)
  2265. {
  2266. LOG_INFO("Connector %d Not Charging Status, Set Voltage & Current Fail", packet->Header.id);
  2267. }
  2268. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetVoltage = 0;
  2269. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.EvBatterytargetCurrent = 0;
  2270. }
  2271. }
  2272. else
  2273. {
  2274. }
  2275. return done;
  2276. }
  2277. unsigned char FirmwareUpgradeHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2278. {
  2279. unsigned char UpgradeResponse = _R_NoUpgrade;
  2280. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0] == packet->Header.id)
  2281. {
  2282. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest)
  2283. {
  2284. if(packet->Payload.data[0] == 0)
  2285. {
  2286. UpgradeResponse = _R_NeedUpgrade;
  2287. }
  2288. else
  2289. {
  2290. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  2291. {
  2292. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm = true;
  2293. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateRequest = false;
  2294. LOG_INFO("Dispenser %d Updating...", dispenserIndex + 1);
  2295. }
  2296. }
  2297. }
  2298. else
  2299. {
  2300. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateConfirm)
  2301. {
  2302. if(packet->Payload.data[0] == 0)
  2303. {
  2304. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted)
  2305. {
  2306. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.FirmwareUpdateCompleted = true;
  2307. LOG_INFO("Dispenser %d Upgrade Completed", dispenserIndex + 1);
  2308. }
  2309. }
  2310. }
  2311. }
  2312. }
  2313. return UpgradeResponse;
  2314. }
  2315. BOOL ConnectorPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2316. {
  2317. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2318. if(find)
  2319. {
  2320. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != packet->Payload.data[0])
  2321. {
  2322. LOG_INFO("Connector %d %s", packet->Header.id, packet->Payload.data[0] == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  2323. }
  2324. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = packet->Payload.data[0];
  2325. }
  2326. else
  2327. {
  2328. }
  2329. return find;
  2330. }
  2331. BOOL ConnectorStateHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2332. {
  2333. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2334. unsigned char ConnectionState;
  2335. unsigned char *AlarmCode;
  2336. unsigned char GunTemp = 0;
  2337. unsigned char ChillerTemp = 0;
  2338. unsigned char plugin = 0;
  2339. unsigned char noAlarm[6];
  2340. memset(noAlarm, 0x00, 6);
  2341. if(find)
  2342. {
  2343. ConnectionState = packet->Payload.data[0];
  2344. AlarmCode = packet->Header.len >= 8 ? &packet->Payload.data[1] : &noAlarm[0];
  2345. GunTemp = packet->Header.len >= 10 ? packet->Payload.data[7] : GunTemp;
  2346. ChillerTemp = packet->Header.len >= 10 ? packet->Payload.data[8] : ChillerTemp;
  2347. chargingInfo[packet->Header.id - 1]->ConnectorTemp = GunTemp;
  2348. chargingInfo[packet->Header.id - 1]->ChillerTemp = ChillerTemp;
  2349. plugin = packet->Header.len >= 11 ? packet->Payload.data[9] : plugin;
  2350. if(packet->Header.len >= 11)
  2351. {
  2352. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn != plugin)
  2353. {
  2354. LOG_INFO("Gun %d %s", packet->Header.id, plugin == _PIS_PluggedIn ? "Pluged In" : "Un Plugged");
  2355. }
  2356. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn = plugin;
  2357. }
  2358. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus != ConnectionState)
  2359. {
  2360. LOG_INFO("Connector %d Remote Status: %d", packet->Header.id, ConnectionState);
  2361. switch(ConnectionState)
  2362. {
  2363. case _CRS_Idle:
  2364. case _CRS_Terminating:
  2365. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  2366. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  2367. {
  2368. if(chargingInfo[packet->Header.id - 1]->SystemStatus != S_IDLE)
  2369. {
  2370. LOG_INFO("*********** Connector id %d Set Normal Stop Flag ***********\n", packet->Header.id);
  2371. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NormalStopRequest = true;
  2372. }
  2373. }
  2374. break;
  2375. case _CRS_Alarm:
  2376. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemotenAlarmCode, AlarmCode, 6);
  2377. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle)
  2378. {
  2379. LOG_INFO("*********** Connector id %d Set Fault Status [%s] ***********", packet->Header.id,
  2380. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemotenAlarmCode);
  2381. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.FaultStatusRequest = true;
  2382. }
  2383. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  2384. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  2385. {
  2386. LOG_INFO("*********** Connector id %d Set Alarm Stop Flag [%s] ***********", packet->Header.id,
  2387. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemotenAlarmCode);
  2388. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.AlarmStopRequest = true;
  2389. }
  2390. break;
  2391. default:
  2392. break;
  2393. }
  2394. }
  2395. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus = ConnectionState;
  2396. }
  2397. else
  2398. {
  2399. }
  2400. return true;
  2401. }
  2402. bool IsEnableForceCharging(byte dispenserIndex, byte connectorId)
  2403. {
  2404. if(connectorId != AUTO_GUN_SELECTION)
  2405. {
  2406. return ShmChargerInfo->Control.FCharging[connectorId - 1].FCtrl.bits.EnableForceCharging ? true : false;
  2407. }
  2408. else
  2409. {
  2410. int gun = 0;
  2411. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  2412. {
  2413. gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  2414. if(ShmChargerInfo->Control.FCharging[gun].FCtrl.bits.EnableForceCharging)
  2415. {
  2416. return true;
  2417. }
  2418. }
  2419. }
  2420. return false;
  2421. }
  2422. AuthorizingInfoData preGunAuthInfo[GENERAL_GUN_QUANTITY];
  2423. AuthorizingInfoData preDispenserAuthInfo[GENERAL_GUN_QUANTITY];
  2424. unsigned char UserIDHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2425. {
  2426. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2427. DispenserAck_Status authorize = _DAS_Wait;
  2428. unsigned char CardNumber[32];
  2429. //unsigned char *result;
  2430. AuthorizingInfoData *AuthInfo;
  2431. AuthorizingInfoData *preAuthInfo;
  2432. int length = 0;
  2433. if(find || packet->Header.id == AUTO_GUN_SELECTION)
  2434. {
  2435. if(IsEnableForceCharging(dispenserIndex, packet->Header.id))
  2436. {
  2437. return _DAS_NotAllowed;
  2438. }
  2439. length = packet->Header.len - 1;
  2440. memset(CardNumber, 0x00, sizeof(CardNumber));
  2441. memcpy(CardNumber, packet->Payload.data, length);
  2442. if(packet->Header.id != AUTO_GUN_SELECTION)
  2443. {
  2444. //result = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthorizingResult;
  2445. AuthInfo = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].AuthInfo;
  2446. preAuthInfo = &preGunAuthInfo[packet->Header.id - 1];
  2447. }
  2448. else
  2449. {
  2450. //result = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthResult;
  2451. AuthInfo = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].AuthInfo;
  2452. preAuthInfo = &preDispenserAuthInfo[dispenserIndex];
  2453. }
  2454. //if(*result == _AuthResult_Valid)
  2455. if(AuthInfo->AuthResult == _AuthResult_Valid)
  2456. {
  2457. if(preAuthInfo->AuthResult != AuthInfo->AuthResult)
  2458. {
  2459. LOG_INFO("Dispenser %d Target %d user id %s authorizing ok", dispenserIndex + 1, packet->Header.id, CardNumber);
  2460. }
  2461. authorize = _DAS_Allowed;
  2462. }
  2463. //else if(*result == _AuthResult_Invalid)
  2464. else if(AuthInfo->AuthResult == _AuthResult_Invalid)
  2465. {
  2466. if(preAuthInfo->AuthResult != AuthInfo->AuthResult)
  2467. {
  2468. LOG_INFO("Dispenser %d Target %d user id %s authorizing fail", dispenserIndex + 1, packet->Header.id, CardNumber);
  2469. }
  2470. authorize = _DAS_NotAllowed;
  2471. }
  2472. else
  2473. {
  2474. if(AuthInfo->AuthStatus == _AuthorizeStatus_Idle && AuthInfo->AuthRequest == NO)
  2475. {
  2476. memcpy(AuthInfo->AuthId, CardNumber, sizeof(CardNumber));
  2477. AuthInfo->AuthTarget = packet->Header.id;
  2478. AuthInfo->AuthRequest = YES;
  2479. if(AuthInfo->AuthTarget != AUTO_GUN_SELECTION)
  2480. {
  2481. LOG_INFO("Gun %d user id [%s] need authorize", packet->Header.id, CardNumber);
  2482. }
  2483. else
  2484. {
  2485. LOG_INFO("Dispenser %d user id [%s] need authorize", dispenserIndex + 1, CardNumber);
  2486. }
  2487. authorize = _DAS_Wait;
  2488. }
  2489. else
  2490. {
  2491. authorize = _DAS_Wait;
  2492. }
  2493. }
  2494. preAuthInfo->AuthResult = AuthInfo->AuthResult;
  2495. }
  2496. else
  2497. {
  2498. authorize = _DAS_NotAllowed;
  2499. }
  2500. return authorize;
  2501. }
  2502. unsigned char ChargingPermissionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2503. {
  2504. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2505. DispenserAck_Status permission = _DAS_Wait;
  2506. if(find)
  2507. {
  2508. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus)
  2509. {
  2510. case S_IDLE:
  2511. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Idle ||
  2512. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing)
  2513. {
  2514. permission = _DAS_Wait;
  2515. }
  2516. else
  2517. {
  2518. permission = _DAS_NotAllowed;
  2519. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2520. {
  2521. LOG_INFO("Cabinet status idle, not allow Dispenser %d Connector %d Charging",
  2522. dispenserIndex + 1, packet->Header.id);
  2523. }
  2524. }
  2525. break;
  2526. case S_AUTHORIZING:
  2527. case S_REASSIGN_CHECK:
  2528. case S_REASSIGN:
  2529. case S_PREPARNING:
  2530. permission = _DAS_Wait;
  2531. break;
  2532. case S_PREPARING_FOR_EV:
  2533. if(chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.RemoteStop ||
  2534. chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.UnlockStop)
  2535. {
  2536. permission = _DAS_NotAllowed;
  2537. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2538. {
  2539. LOG_INFO("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  2540. }
  2541. }
  2542. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus >= _CRS_Preparing &&
  2543. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus <= _CRS_Charging)
  2544. {
  2545. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest)
  2546. {
  2547. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.PermissionRequest = true;
  2548. LOG_INFO("Allow Dispenser %d Connector %d start preparing to charge", dispenserIndex + 1, packet->Header.id);
  2549. }
  2550. permission = _DAS_Allowed;
  2551. }
  2552. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Terminating)
  2553. {
  2554. permission = _DAS_NotAllowed;
  2555. LOG_INFO("Dispenser Terminating, Not allow dispenser index %d connector id %d preparing to charge", dispenserIndex, packet->Header.id);
  2556. }
  2557. else
  2558. {
  2559. // keep wait
  2560. }
  2561. break;
  2562. case S_PREPARING_FOR_EVSE:
  2563. permission = _DAS_Allowed;
  2564. break;
  2565. case S_CHARGING:
  2566. if(chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.RemoteStop ||
  2567. chargingInfo[packet->Header.id - 1]->ChargingStopFlag.bits.UnlockStop)
  2568. {
  2569. permission = _DAS_NotAllowed;
  2570. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2571. {
  2572. LOG_INFO("RemoteStop or UnlockStop, not allow dispenser index %d connector id %d charging", dispenserIndex, packet->Header.id);
  2573. }
  2574. }
  2575. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Preparing ||
  2576. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus == _CRS_Charging)
  2577. {
  2578. permission = _DAS_Allowed;
  2579. }
  2580. else
  2581. {
  2582. permission = _DAS_NotAllowed;
  2583. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2584. {
  2585. LOG_INFO("Dispenser index %d connector id %d remote status not in charging mode", dispenserIndex, packet->Header.id);
  2586. }
  2587. }
  2588. break;
  2589. case S_TERMINATING:
  2590. case S_COMPLETE:
  2591. case S_ALARM:
  2592. case S_FAULT:
  2593. case S_RESERVATION:
  2594. case S_MAINTAIN:
  2595. permission = _DAS_NotAllowed;
  2596. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission != permission)
  2597. {
  2598. LOG_INFO("Cabinet status(%d) not match, not allow dispenser index %d connector id %d charging",
  2599. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  2600. dispenserIndex, packet->Header.id);
  2601. }
  2602. break;
  2603. default:
  2604. break;
  2605. }
  2606. if(IsEnableForceCharging(dispenserIndex, packet->Header.id))
  2607. {
  2608. // not allow to charging when EnableForceCharging is true
  2609. permission = _DAS_NotAllowed;
  2610. }
  2611. }
  2612. else
  2613. {
  2614. LOG_INFO("Dispenser index %d Connector id %d not found", dispenserIndex, packet->Header.id);
  2615. permission = _DAS_NotAllowed;
  2616. }
  2617. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.ChargingPermission = permission;
  2618. // LOG_INFO("Dispenser %d Permission %s, Gun Status: %d, Connector Status: %d, Wait Plug: %d, PlugIn: %d, Available: %d, Order: %d", dispenserIndex, permission == true ? "OK" : "NG",
  2619. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus,
  2620. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].RemoteStatus,
  2621. // ShmSysConfigAndInfo->SysInfo.WaitForPlugit,
  2622. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.ConnectorPlugIn,
  2623. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.IsAvailable,
  2624. // ShmSysConfigAndInfo->SysInfo.OrderCharging);
  2625. return permission;
  2626. }
  2627. BOOL MiscControlHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2628. {
  2629. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2630. BOOL done = false;
  2631. if(find)
  2632. {
  2633. done = true;
  2634. }
  2635. return done;
  2636. }
  2637. unsigned char DispenserWriteCsuVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2638. {
  2639. char version[32];
  2640. BOOL writeVersion = FALSE, showVersion = FALSE;;
  2641. memcpy(version, &packet->Payload.data[0], 32);
  2642. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version) != 0)
  2643. {
  2644. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev, version, 32);
  2645. showVersion = true;
  2646. }
  2647. memcpy(version, &packet->Payload.data[32], 32);
  2648. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version) != 0)
  2649. {
  2650. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev, version, 32);
  2651. showVersion = true;
  2652. }
  2653. memcpy(version, &packet->Payload.data[64], 32);
  2654. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version) != 0)
  2655. {
  2656. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev, version, 32);
  2657. showVersion = true;
  2658. }
  2659. memcpy(version, &packet->Payload.data[96], 32);
  2660. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version) != 0)
  2661. {
  2662. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev, version, 32);
  2663. showVersion = true;
  2664. }
  2665. if(showVersion)
  2666. {
  2667. LOG_INFO("********** Dispenser id %d: Csu Version information **********", dispenserIndex + 1);
  2668. LOG_INFO("********** CsuBootLoadRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuBootLoadFwRev);
  2669. LOG_INFO("********** CsuKernelFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuKernelFwRev);
  2670. LOG_INFO("********** CsuRootFsFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuRootFsFwRev);
  2671. LOG_INFO("********** CsuPrimFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].CsuPrimFwRev);
  2672. }
  2673. writeVersion = TRUE;
  2674. return writeVersion;
  2675. }
  2676. unsigned char DispenserWriteOtherVersionInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2677. {
  2678. char version[32];
  2679. BOOL writeVersion = FALSE, showVersion = FALSE;;
  2680. memcpy(version, &packet->Payload.data[0], 32);
  2681. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version) != 0)
  2682. {
  2683. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev, version, 32);
  2684. showVersion = true;
  2685. }
  2686. memcpy(version, &packet->Payload.data[32], 32);
  2687. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version) != 0)
  2688. {
  2689. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev, version, 32);
  2690. showVersion = true;
  2691. }
  2692. memcpy(version, &packet->Payload.data[64], 32);
  2693. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version) != 0)
  2694. {
  2695. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev, version, 32);
  2696. showVersion = true;
  2697. }
  2698. memcpy(version, &packet->Payload.data[96], 32);
  2699. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version) != 0)
  2700. {
  2701. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev, version, 32);
  2702. showVersion = true;
  2703. }
  2704. memcpy(version, &packet->Payload.data[128], 32);
  2705. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version) != 0)
  2706. {
  2707. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev, version, 32);
  2708. showVersion = true;
  2709. }
  2710. if(showVersion)
  2711. {
  2712. LOG_INFO("********** Dispenser id %d: Other Version information **********", dispenserIndex + 1);
  2713. LOG_INFO("********** FanModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].FanModuleFwRev);
  2714. LOG_INFO("********** RelayModuleRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].RelayModuleFwRev);
  2715. LOG_INFO("********** Connector1Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector1FwRev);
  2716. LOG_INFO("********** Connector2Rev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Connector2FwRev);
  2717. LOG_INFO("********** LedModuleFwRev: %32s ***", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LedModuleFwRev);
  2718. }
  2719. writeVersion = TRUE;
  2720. return writeVersion;
  2721. }
  2722. unsigned char DispenserWriteChargingInfoHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2723. {
  2724. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2725. unsigned char gun = 0;
  2726. if(find)
  2727. {
  2728. gun = packet->Header.id - 1;
  2729. unsigned short diffVoltage = 0, diffCurrent = 0;
  2730. unsigned short voltage = (packet->Payload.data[0] << 8) + (packet->Payload.data[1]);
  2731. unsigned short current = (packet->Payload.data[2] << 8) + (packet->Payload.data[3]);
  2732. //unsigned int time = (packet->Payload.data[4] << 24) + (packet->Payload.data[5] << 16) + (packet->Payload.data[6] << 8) + (packet->Payload.data[7]);
  2733. unsigned char soc = (packet->Payload.data[8]);
  2734. diffVoltage = voltage >= ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage ?
  2735. voltage - ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage :
  2736. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage - voltage;
  2737. diffCurrent = current >= ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent ?
  2738. current - ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent :
  2739. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent - current;
  2740. if(diffVoltage > 10 || diffCurrent > 10 || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteSoc != soc)
  2741. {
  2742. LOG_INFO("Connector %d Charging Info: Voltage: %4d.%d V, Current: %3d.%d A, Soc: %3d, Temp: %3d, Chiller: %3d",
  2743. packet->Header.id, (voltage / 10), (voltage % 10), (current / 10), (current % 10), soc,
  2744. chargingInfo[gun]->ConnectorTemp - 60, chargingInfo[gun]->ChillerTemp - 60);
  2745. }
  2746. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingVoltage = voltage;
  2747. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteChargingCurrent = current;
  2748. //ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteRemainChargingDuration = time;
  2749. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].RemoteSoc = soc;
  2750. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].GeneralChargingData.EvBatterySoc = soc;
  2751. }
  2752. else
  2753. {
  2754. }
  2755. return find;
  2756. }
  2757. unsigned char ReadChargerSystemIdHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2758. {
  2759. BOOL find = TRUE;
  2760. return find;
  2761. }
  2762. unsigned char WriteWaitPlugInHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2763. {
  2764. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2765. if(find)
  2766. {
  2767. unsigned char StartWait = packet->Payload.data[0];
  2768. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus == S_AUTHORIZING)
  2769. {
  2770. if(StartWait)
  2771. {
  2772. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  2773. {
  2774. LOG_INFO("Connector %d Start Wait Plug In", packet->Header.id);
  2775. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.SwipeRfidConfirm = true;
  2776. }
  2777. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = true;
  2778. }
  2779. else
  2780. {
  2781. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug)
  2782. {
  2783. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].GeneralChargingData.SystemStatus != S_IDLE)
  2784. {
  2785. LOG_INFO("Connector %d Stop Wait Plug In", packet->Header.id);
  2786. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.NeedCleanAuthorizeInfo = true;
  2787. }
  2788. }
  2789. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  2790. }
  2791. }
  2792. else
  2793. {
  2794. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[packet->Header.id - 1].Parameter.bits.StartWaitPlug = false;
  2795. }
  2796. }
  2797. else
  2798. {
  2799. }
  2800. return find;
  2801. }
  2802. unsigned char GroundFaultDetectionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2803. {
  2804. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2805. unsigned char gun = 0;
  2806. if(find)
  2807. {
  2808. gun = packet->Header.id - 1;
  2809. unsigned char enable = packet->Payload.data[0];
  2810. if(chargingInfo[gun]->PantographFlag == YES)
  2811. {
  2812. if(enable == _GFD_Enable &&
  2813. (chargingInfo[gun]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun]->SystemStatus <= S_CHARGING))
  2814. {
  2815. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection != _GFD_Enable)
  2816. {
  2817. LOG_INFO("Connector %d Gfd Detection Start", gun + 1);
  2818. }
  2819. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection = _GFD_Enable;
  2820. }
  2821. else
  2822. {
  2823. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection != _GFD_Disable)
  2824. {
  2825. LOG_INFO("Connector %d Gfd Detection Stop", gun + 1);
  2826. }
  2827. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].Parameter.bits.GfdDetection = _GFD_Disable;
  2828. }
  2829. }
  2830. }
  2831. else
  2832. {
  2833. }
  2834. return find;
  2835. }
  2836. unsigned char GetCabinetCSUVersionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2837. {
  2838. return true;
  2839. }
  2840. unsigned char GetCabinetOtherVersionHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2841. {
  2842. return true;
  2843. }
  2844. unsigned char GetPsuQuantityHandler(struct PACKET_STRUCTURE *packet, unsigned char dispenserIndex)
  2845. {
  2846. return true;
  2847. }
  2848. unsigned char GetPsuVersionHandler(struct PACKET_STRUCTURE *packet, int *psuIndex)
  2849. {
  2850. *psuIndex = 0;
  2851. if(packet->Payload.data[0] <= ShmPsuData->SystemPresentPsuQuantity && packet->Payload.data[0] > 0)
  2852. {
  2853. *psuIndex = packet->Payload.data[0] - 1;
  2854. }
  2855. return true;
  2856. }
  2857. unsigned char GetReservationHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex, unsigned char *reservation)
  2858. {
  2859. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2860. unsigned char gun = 0;
  2861. if(find)
  2862. {
  2863. gun = packet->Header.id - 1;
  2864. *reservation = chargingInfo[gun]->SystemStatus == S_RESERVATION ? YES : NO;
  2865. }
  2866. else
  2867. {
  2868. }
  2869. return find;
  2870. }
  2871. unsigned char DispenserReqHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2872. {
  2873. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2874. unsigned char gun = 0;
  2875. if(find)
  2876. {
  2877. gun = packet->Header.id - 1;
  2878. int count = 0;
  2879. struct MISC_COMMAND connector_req;
  2880. while((packet->Header.len - 1) > (count * 6))
  2881. {
  2882. if(((packet->Header.len - 1) - (count * 6)) % 6 == 0)
  2883. {
  2884. memset(&connector_req, 0x00, sizeof(struct MISC_COMMAND));
  2885. connector_req.Command = (packet->Payload.data[0 + (count * 6)] << 8) | packet->Payload.data[1 + (count * 6)];
  2886. connector_req.Value = (packet->Payload.data[2 + (count * 6)] << 24) |
  2887. (packet->Payload.data[3 + (count * 6)] << 16) |
  2888. (packet->Payload.data[4 + (count * 6)] << 8) |
  2889. (packet->Payload.data[5 + (count * 6)]);
  2890. count++;
  2891. switch(connector_req.Command)
  2892. {
  2893. case _DisReq_ChargingCancel:
  2894. if(connector_req.Value == YES)
  2895. {
  2896. if(!ShmChargerInfo->ConnectorActReq[gun].Flag.bits.ChargingCancel)
  2897. {
  2898. LOG_INFO("Gun %d ChargingCancel Request", gun + 1);
  2899. }
  2900. ShmChargerInfo->ConnectorActReq[gun].Flag.bits.ChargingCancel = true;
  2901. }
  2902. break;
  2903. default:
  2904. break;
  2905. }
  2906. }
  2907. else
  2908. {
  2909. break;
  2910. }
  2911. }
  2912. }
  2913. else
  2914. {
  2915. }
  2916. return find;
  2917. }
  2918. unsigned char GetRemoteStartNoIDHandler(struct PACKET_STRUCTURE *packet, unsigned char *state)
  2919. {
  2920. *state = ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId ? YES : NO;
  2921. return true;
  2922. }
  2923. unsigned char GetRefundAmountHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2924. {
  2925. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2926. if(find)
  2927. {
  2928. }
  2929. else
  2930. {
  2931. }
  2932. return find;
  2933. }
  2934. unsigned char GetPrepaymentHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2935. {
  2936. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2937. if(find)
  2938. {
  2939. }
  2940. else
  2941. {
  2942. }
  2943. return find;
  2944. }
  2945. unsigned char GetPaymentFailReasonHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2946. {
  2947. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2948. if(find)
  2949. {
  2950. }
  2951. else
  2952. {
  2953. }
  2954. return find;
  2955. }
  2956. unsigned char GetQRCodeRequestHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2957. {
  2958. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2959. if(find)
  2960. {
  2961. }
  2962. else
  2963. {
  2964. }
  2965. return find;
  2966. }
  2967. unsigned char GetStationInfoHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2968. {
  2969. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2970. if(find)
  2971. {
  2972. }
  2973. else
  2974. {
  2975. if(packet->Header.id == 0xFF)
  2976. {
  2977. find = true;
  2978. }
  2979. }
  2980. return find;
  2981. }
  2982. unsigned char WriteDeductResultHandler(struct PACKET_STRUCTURE *packet, int dispenserIndex)
  2983. {
  2984. BOOL find = FindConnectorID(dispenserIndex, packet->Header.id);
  2985. unsigned char gun = 0;
  2986. DeductResultInfoData deduct;
  2987. bool done = false;
  2988. char appNo[10];
  2989. char vemData[65];
  2990. char cardNo[21];
  2991. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  2992. memset(appNo, 0x00, sizeof(appNo));
  2993. memset(vemData, 0x00, sizeof(vemData));
  2994. memset(cardNo, 0x00, sizeof(cardNo));
  2995. if(find || packet->Header.id == 0xFF)
  2996. {
  2997. memset(&deduct, 0x00, sizeof(DeductResultInfoData));
  2998. deduct.ConnectorId = packet->Header.id != 0xFF ?
  2999. packet->Header.id : ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[0];
  3000. gun = packet->Header.id - 1;
  3001. deduct.DeductResult = packet->Payload.data[0];
  3002. deduct.IsDonateInvoice = packet->Payload.data[1] == YES ? YES : NO;
  3003. deduct.TransactionId = (packet->Payload.data[2] << 24) +
  3004. (packet->Payload.data[3] << 16) +
  3005. (packet->Payload.data[4] << 8) +
  3006. packet->Payload.data[5];
  3007. deduct.DeductAmount = (packet->Payload.data[6] << 24) +
  3008. (packet->Payload.data[7] << 16) +
  3009. (packet->Payload.data[8] << 8) +
  3010. packet->Payload.data[9];
  3011. memcpy(deduct.ApprovalNumber, (char *)&packet->Payload.data[10], sizeof(deduct.ApprovalNumber));
  3012. memcpy(appNo, deduct.ApprovalNumber, sizeof(deduct.ApprovalNumber));
  3013. if(packet->Header.len >= 84)
  3014. {
  3015. memcpy(deduct.VemData, (char *)&packet->Payload.data[19], sizeof(deduct.VemData));
  3016. memcpy(vemData, deduct.VemData, sizeof(deduct.VemData));
  3017. }
  3018. if(packet->Header.len >= 104)
  3019. {
  3020. memcpy(deduct.creditNo, (char *)&packet->Payload.data[83], sizeof(deduct.creditNo));
  3021. memcpy(cardNo, deduct.creditNo, sizeof(deduct.creditNo));
  3022. }
  3023. if(deduct.DeductResult == _Deduct_Fail || deduct.DeductResult == _Deduct_Pass)
  3024. {
  3025. if(packet->Header.id != 0xFF)
  3026. {
  3027. if(deduct.TransactionId == ShmChargerInfo->DeductResultReq[gun].TransactionId)
  3028. {
  3029. if(!ShmChargerInfo->DeductResultReq[gun].DeductReq)
  3030. {
  3031. memcpy(&ShmChargerInfo->DeductResultReq[gun], &deduct, sizeof(DeductResultInfoData));
  3032. ShmChargerInfo->DeductResultReq[gun].DeductReq = YES;
  3033. done = true;
  3034. }
  3035. LOG_INFO("Gun %d [%s] %s", gun + 1, str_ReaderStatus[deduct.DeductResult], done ? "Done" : "Wait");
  3036. LOG_INFO("Gun %d TxId: [%d], Card: [%s], [%s], Amount: [%d.%02d], Donate: [%s], ApprovalNo: [%s], VemData: [%s]",
  3037. gun + 1,
  3038. deduct.TransactionId,
  3039. cardNo,
  3040. str_ReaderStatus[deduct.DeductResult],
  3041. (deduct.DeductAmount / 100), (deduct.DeductAmount % 100),
  3042. deduct.IsDonateInvoice ? "Yes" : "No",
  3043. appNo,
  3044. vemData);
  3045. return done;
  3046. }
  3047. }
  3048. if(!done)
  3049. {
  3050. if(!ShmChargerInfo->ReDeductReq.DeductReq)
  3051. {
  3052. memcpy(&ShmChargerInfo->ReDeductReq, &deduct, sizeof(DeductResultInfoData));
  3053. ShmChargerInfo->ReDeductReq.DeductReq = YES;
  3054. done = true;
  3055. }
  3056. LOG_INFO("Gun %d%s ReDeduct %s",
  3057. gun + 1, packet->Header.id != 0xFF ? "" : "(Default)", done ? "Done" : "Wait");
  3058. LOG_INFO("Gun %d ReDeduct TxId: [%d], Card: [%s], [%s], Amount: [%d.%02d], Donate: [%s], ApprovalNo: [%s], VemData: [%s]",
  3059. gun + 1,
  3060. deduct.TransactionId,
  3061. cardNo,
  3062. str_ReaderStatus[deduct.DeductResult],
  3063. (deduct.DeductAmount / 100), (deduct.DeductAmount % 100),
  3064. deduct.IsDonateInvoice ? "Yes" : "No",
  3065. appNo,
  3066. vemData);
  3067. return done;
  3068. }
  3069. }
  3070. else if(deduct.DeductResult == _Deduct_Cancel || deduct.DeductResult == _Deduct_PreAuth)
  3071. {
  3072. if(packet->Header.id != 0xFF)
  3073. {
  3074. if(!ShmChargerInfo->ReaderStatus[gun].ReaderStatusReq)
  3075. {
  3076. memcpy(&ShmChargerInfo->ReaderStatus[gun], &deduct, sizeof(DeductResultInfoData));
  3077. ShmChargerInfo->ReaderStatus[gun].ReaderStatusReq = YES;
  3078. done = true;
  3079. }
  3080. LOG_INFO("Gun %d [%s] %s", gun + 1, str_ReaderStatus[deduct.DeductResult], done ? "Done" : "Wait");
  3081. LOG_INFO("Gun %d %s, Card: [%s], ApprovalNo: [%s], VemData: [%s]",
  3082. gun + 1,
  3083. str_ReaderStatus[deduct.DeductResult],
  3084. cardNo,
  3085. appNo,
  3086. vemData);
  3087. return done;
  3088. }
  3089. else
  3090. {
  3091. find = true;
  3092. }
  3093. }
  3094. else
  3095. {
  3096. find = true;
  3097. }
  3098. }
  3099. return find;
  3100. }
  3101. void DisableConnector(unsigned char dispenserIndex)
  3102. {
  3103. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  3104. {
  3105. unsigned char connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  3106. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Enable = false;
  3107. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.TimeoutStopRequest = true;
  3108. }
  3109. }
  3110. void DispenserSocketProcess(int socketFd, struct sockaddr_in clientInfo, unsigned char index)
  3111. {
  3112. int rxLen = 0, timeout = 0;
  3113. unsigned char dispenserID = 0, dispenserIndex = 0;
  3114. struct PACKET_STRUCTURE receiveBuffer;
  3115. unsigned char modelName[64];
  3116. unsigned char ackResult = _R_NG;
  3117. DispenserAck_Status ack = _DAS_Wait;
  3118. LOG_INFO("IP %s connection(%d) is established, start dispenser process, PID = %d", (inet_ntoa(clientInfo.sin_addr)), index, getpid());
  3119. while(1)
  3120. {
  3121. if((rxLen = recv(socketFd, &receiveBuffer, sizeof(Packet_Structure), MSG_DONTWAIT )) > 0)
  3122. {
  3123. if(rxLen == (receiveBuffer.Header.len + PACKET_HEADER_LENGTH))
  3124. {
  3125. ackResult = _R_NG;
  3126. //ShowSocketData(&receiveBuffer);
  3127. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_WaitModelName)
  3128. {
  3129. // Reg: 0x01, Dispenser model name
  3130. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  3131. {
  3132. memset(modelName, 0x00, 64);
  3133. dispenserID = receiveBuffer.Payload.data[receiveBuffer.Header.len - 2];
  3134. if(dispenserID > 0 && dispenserID <= GENERAL_GUN_QUANTITY &&
  3135. dispenserID <= ShmChargerInfo->Control.MaxDispenser)
  3136. {
  3137. dispenserIndex = dispenserID - 1;
  3138. memcpy(modelName, receiveBuffer.Payload.data, receiveBuffer.Header.len - 2);
  3139. if(IsAvailableDispenserIndexSequence(dispenserIndex))
  3140. {
  3141. if(DispenserIdentificationHandler(&receiveBuffer, dispenserIndex, modelName) == true)
  3142. {
  3143. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].DispenserIndex = dispenserIndex;
  3144. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status = _CNS_DispenserMatched;
  3145. LOG_INFO("********** Dispenser id %d check in, model name: %s **********\n", dispenserID, modelName);
  3146. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ModelName, modelName, 64);
  3147. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectionChannel = index;
  3148. ackResult = _R_OK;
  3149. }
  3150. else
  3151. {
  3152. // response NG: gun quantity fail
  3153. LOG_INFO("Dispenser ID %d Identification Fail", dispenserID);
  3154. }
  3155. }
  3156. else
  3157. {
  3158. // response NG: dispenser Index fail
  3159. LOG_INFO("Dispenser ID %d Fail, Need Wait Another Dispenser", dispenserID);
  3160. }
  3161. }
  3162. else
  3163. {
  3164. LOG_INFO("Dispenser ID: %d Over Range", dispenserID);
  3165. }
  3166. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  3167. }
  3168. else
  3169. {
  3170. // do nothing
  3171. }
  3172. }
  3173. else if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  3174. {
  3175. // Reg: 0x01, Dispenser model name
  3176. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Model_Name)
  3177. {
  3178. ackResult = _R_OK;
  3179. ModelNameResponse(socketFd, &receiveBuffer, ackResult);
  3180. }
  3181. // Reg: 0x02, Connector ID
  3182. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Connector_ID)
  3183. {
  3184. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity > 0)
  3185. {
  3186. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Idle;
  3187. ackResult = _R_OK;
  3188. }
  3189. LOG_INFO("Dispenser ID %d Get Connector ID %s", dispenserID, ackResult == _R_OK ? "OK" : "NG");
  3190. ConnectorIDResponse(socketFd, &receiveBuffer, ackResult, dispenserIndex);
  3191. if(ackResult == _R_OK)
  3192. {
  3193. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync)
  3194. {
  3195. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DispenserConfigSync = true;
  3196. LOG_INFO("Dispenser ID %d Configuration Need To Synchronize", dispenserID);
  3197. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CONNECTION_TIMEOUT);
  3198. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_DEFAULT_PRICE);
  3199. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_CURRENCY);
  3200. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BACKEND_STATUS);
  3201. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_ETHERNET_STATUS);
  3202. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_WIFI_STATUS);
  3203. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TELCOM_MODEM_STATUS);
  3204. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_BILLING_STATUS);
  3205. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STOP_BUTTON_STATUS);
  3206. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_AUTH_MODE_CONFIG);
  3207. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_EVCCID_CONFIG);
  3208. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_TIME_OFFSET);
  3209. if(strlen(ShmChargerInfo->StationInfo.StationName) > 0 ||
  3210. ShmChargerInfo->StationInfo.StationID != 0 ||
  3211. ShmChargerInfo->StationInfo.WeatherID != 0 ||
  3212. ShmChargerInfo->StationInfo.Temperature != 0)
  3213. {
  3214. Set_Dispenser_MiscCommand(dispenserIndex, MISC_DISP_STATION_INFO);
  3215. }
  3216. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  3217. {
  3218. unsigned char gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  3219. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  3220. Set_Connector_MiscCommand(gun, MISC_CONN_QRCODE_INFO);
  3221. }
  3222. }
  3223. }
  3224. }
  3225. // Reg: 0x03, Power cabinet status
  3226. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Power_Cabinet_Status)
  3227. {
  3228. PowerCabinetStatusResponse(socketFd, &receiveBuffer, dispenserIndex);
  3229. }
  3230. // Reg: 0x04, Dispenser status
  3231. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Status)
  3232. {
  3233. if(DispenserStatusCodeHandler(&receiveBuffer, dispenserIndex))
  3234. {
  3235. ackResult = _R_OK;
  3236. }
  3237. DispenserStatusResponse(socketFd, &receiveBuffer, ackResult);
  3238. }
  3239. // Reg: 0x05, Charging capability
  3240. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Capability)
  3241. {
  3242. if(ConnectorChargingCapabilityHandler(&receiveBuffer, dispenserIndex))
  3243. {
  3244. ChargingCapabilityResponse(socketFd, &receiveBuffer);
  3245. }
  3246. }
  3247. // Reg: 0x06, Charging target
  3248. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Target)
  3249. {
  3250. if(receiveBuffer.Header.len == 5)
  3251. {
  3252. if(ConnectorChargingTargetHandler(&receiveBuffer, dispenserIndex))
  3253. {
  3254. ackResult = _R_OK;
  3255. }
  3256. }
  3257. ChargingTargetResponse(socketFd, &receiveBuffer, ackResult);
  3258. }
  3259. // Reg: 0x07, Software update
  3260. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Software_Update)
  3261. {
  3262. unsigned char NeedUpgrade = FirmwareUpgradeHandler(&receiveBuffer, dispenserIndex);
  3263. FirmwareUpgradeResponse(socketFd, &receiveBuffer, dispenserIndex, NeedUpgrade);
  3264. }
  3265. // Reg: 0x08, Plug-in status
  3266. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Plug_In_Status)
  3267. {
  3268. if(receiveBuffer.Header.len == 2)
  3269. {
  3270. if(ConnectorPlugInHandler(&receiveBuffer, dispenserIndex))
  3271. {
  3272. ackResult = _R_OK;
  3273. }
  3274. }
  3275. PlugInStatusResponse(socketFd, &receiveBuffer, ackResult);
  3276. }
  3277. // Reg: 0x09, Connector state
  3278. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Connector_State)
  3279. {
  3280. if(ConnectorStateHandler(&receiveBuffer, dispenserIndex))
  3281. {
  3282. ackResult = _R_OK;
  3283. }
  3284. ConnectorStateResponse(socketFd, &receiveBuffer, ackResult);
  3285. }
  3286. // Reg: 0x0A, User ID
  3287. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_User_ID)
  3288. {
  3289. ack = UserIDHandler(&receiveBuffer, dispenserIndex);
  3290. UserIDResponse(socketFd, &receiveBuffer, ack);
  3291. }
  3292. // Reg: 0x0B, Charging permission
  3293. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charging_Permission)
  3294. {
  3295. ack = ChargingPermissionHandler(&receiveBuffer, dispenserIndex);
  3296. ChargingPermissionResponse(socketFd, &receiveBuffer, ack);
  3297. }
  3298. // Reg: 0x0C, Misc Control
  3299. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Misc_Control)
  3300. {
  3301. if(MiscControlHandler(&receiveBuffer, dispenserIndex))
  3302. {
  3303. ackResult = _R_OK;
  3304. }
  3305. MiscCmdRes(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  3306. }
  3307. // Reg: 0x0D, Report csu version
  3308. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Csu_Version)
  3309. {
  3310. if(DispenserWriteCsuVersionInfoHandler(&receiveBuffer, dispenserIndex))
  3311. {
  3312. ackResult = _R_OK;
  3313. }
  3314. WriteCsuVersionResponse(socketFd, &receiveBuffer, ackResult);
  3315. }
  3316. // Reg: 0x0E, Report other version
  3317. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Report_Other_Version)
  3318. {
  3319. if(DispenserWriteOtherVersionInfoHandler(&receiveBuffer, dispenserIndex))
  3320. {
  3321. ackResult = _R_OK;
  3322. }
  3323. WriteOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  3324. }
  3325. // Reg: 0x0F, Charging info
  3326. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Charging_Info)
  3327. {
  3328. if(DispenserWriteChargingInfoHandler(&receiveBuffer, dispenserIndex))
  3329. {
  3330. ackResult = _R_OK;
  3331. }
  3332. WriteChargingInfoResponse(socketFd, &receiveBuffer, ackResult);
  3333. }
  3334. // Reg: 0x10, Charger system id
  3335. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Charger_System_Id)
  3336. {
  3337. if(ReadChargerSystemIdHandler(&receiveBuffer, dispenserIndex))
  3338. {
  3339. ackResult = _R_OK;
  3340. }
  3341. ChargerSystemIdResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  3342. }
  3343. // Reg: 0x11, Wait plug in
  3344. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_WaitPlugIn)
  3345. {
  3346. if(WriteWaitPlugInHandler(&receiveBuffer, dispenserIndex))
  3347. {
  3348. ackResult = _R_OK;
  3349. }
  3350. WriteWaitPlugInResponse(socketFd, &receiveBuffer, ackResult);
  3351. }
  3352. // Reg: 0x12, Ground Fault Detection
  3353. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_GroundFaultDetection)
  3354. {
  3355. if(GroundFaultDetectionHandler(&receiveBuffer, dispenserIndex))
  3356. {
  3357. ackResult = _R_OK;
  3358. }
  3359. GroundFaultDetectionResponse(socketFd, &receiveBuffer, ackResult);
  3360. }
  3361. // Reg: 0x13, Get Cabinet CSU Version
  3362. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Cabinet_CSU_Version)
  3363. {
  3364. if(GetCabinetCSUVersionHandler(&receiveBuffer, dispenserIndex))
  3365. {
  3366. ackResult = _R_OK;
  3367. }
  3368. CabinetCSUVersionResponse(socketFd, &receiveBuffer, ackResult);
  3369. }
  3370. // Reg: 0x14, Get Cabinet Other Version
  3371. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Cabinet_Other_Version)
  3372. {
  3373. if(GetCabinetOtherVersionHandler(&receiveBuffer, dispenserIndex))
  3374. {
  3375. ackResult = _R_OK;
  3376. }
  3377. CabinetOtherVersionResponse(socketFd, &receiveBuffer, ackResult);
  3378. }
  3379. // Reg: 0x15, Get Psu Quantity
  3380. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Psu_Quantity)
  3381. {
  3382. if(GetPsuQuantityHandler(&receiveBuffer, dispenserIndex))
  3383. {
  3384. ackResult = _R_OK;
  3385. }
  3386. PsuQuantityResponse(socketFd, &receiveBuffer, ackResult);
  3387. }
  3388. // Reg: 0x16, Get Psu Version
  3389. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Psu_Version)
  3390. {
  3391. int _PsuIndex = 0;
  3392. if(GetPsuVersionHandler(&receiveBuffer, &_PsuIndex))
  3393. {
  3394. ackResult = _R_OK;
  3395. }
  3396. PsuVersionResponse(socketFd, &receiveBuffer, ackResult, _PsuIndex);
  3397. }
  3398. // Reg: 0x17, Get Reservation
  3399. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_Get_Reservation)
  3400. {
  3401. unsigned char _reservation = NO;
  3402. if(GetReservationHandler(&receiveBuffer, dispenserIndex, &_reservation))
  3403. {
  3404. ackResult = _R_OK;
  3405. }
  3406. ReservationResponse(socketFd, &receiveBuffer, ackResult, _reservation);
  3407. }
  3408. // Reg: 0x18, Dispenser Request
  3409. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_Dispenser_Request)
  3410. {
  3411. if(DispenserReqHandler(&receiveBuffer, dispenserIndex))
  3412. {
  3413. ackResult = _R_OK;
  3414. }
  3415. DispenserReqHandlerResponse(socketFd, &receiveBuffer, ackResult);
  3416. }
  3417. // Reg: 0x19, State Of Remote Start Without Connector ID
  3418. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_RemoteStartNoIDState)
  3419. {
  3420. unsigned char _NoConnectorIDRemoteStart = 0;
  3421. if(GetRemoteStartNoIDHandler(&receiveBuffer, &_NoConnectorIDRemoteStart))
  3422. {
  3423. ackResult = _R_OK;
  3424. }
  3425. RemoteStarNoIDResponse(socketFd, &receiveBuffer, ackResult, _NoConnectorIDRemoteStart);
  3426. }
  3427. // Reg: 0x1A, Refund Amount
  3428. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_RefundAmount)
  3429. {
  3430. if(GetRefundAmountHandler(&receiveBuffer, dispenserIndex))
  3431. {
  3432. ackResult = _R_OK;
  3433. }
  3434. RefundAmountResponse(socketFd, &receiveBuffer, ackResult);
  3435. }
  3436. // Reg: 0x1B, Prepayment Info
  3437. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_PrepaymentInfo)
  3438. {
  3439. if(GetPrepaymentHandler(&receiveBuffer, dispenserIndex))
  3440. {
  3441. ackResult = _R_OK;
  3442. }
  3443. PrepaymentResponse(socketFd, &receiveBuffer, ackResult);
  3444. }
  3445. // Reg: 0x1C, Payment Fail Reason
  3446. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_PaymentFailReason)
  3447. {
  3448. if(GetPaymentFailReasonHandler(&receiveBuffer, dispenserIndex))
  3449. {
  3450. ackResult = _R_OK;
  3451. }
  3452. PaymentFailReasonResponse(socketFd, &receiveBuffer, ackResult);
  3453. }
  3454. // Reg: 0x1D, QR Code Request
  3455. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_ConnectorQRCode)
  3456. {
  3457. if(GetQRCodeRequestHandler(&receiveBuffer, dispenserIndex))
  3458. {
  3459. ackResult = _R_OK;
  3460. }
  3461. QRCodeResponse(socketFd, &receiveBuffer, ackResult);
  3462. }
  3463. // Reg: 0x1E, Station Info
  3464. if(receiveBuffer.Header.op == _Header_Read && receiveBuffer.Payload.reg == _Reg_StationInfo)
  3465. {
  3466. if(GetStationInfoHandler(&receiveBuffer, dispenserIndex))
  3467. {
  3468. ackResult = _R_OK;
  3469. }
  3470. StationInfoResponse(socketFd, &receiveBuffer, dispenserIndex, ackResult);
  3471. }
  3472. // Reg: 0x1F, Deduct Result
  3473. if(receiveBuffer.Header.op == _Header_Write && receiveBuffer.Payload.reg == _Reg_DeductResult)
  3474. {
  3475. if(WriteDeductResultHandler(&receiveBuffer, dispenserIndex))
  3476. {
  3477. ackResult = _R_OK;
  3478. }
  3479. DeductResultResponse(socketFd, &receiveBuffer, ackResult);
  3480. }
  3481. }
  3482. // clean timeout
  3483. timeout = 0;
  3484. }
  3485. }
  3486. else
  3487. {
  3488. timeout += SOCKET_RECEIVE_INTERVAL;
  3489. usleep((SOCKET_RECEIVE_INTERVAL * 1000));
  3490. }
  3491. if(timeout >= DISPENSER_SOCKET_TIMEOUT || ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DuplicateIp)
  3492. {
  3493. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].Setting.bits.DuplicateIp)
  3494. {
  3495. LOG_INFO("Dispenser %d has been kick", dispenserIndex + 1);
  3496. }
  3497. else
  3498. {
  3499. //timeout
  3500. LOG_INFO("IP: %s, Socket %d connection timeout", (inet_ntoa(clientInfo.sin_addr)), index);
  3501. }
  3502. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[index].Status == _CNS_DispenserMatched)
  3503. {
  3504. DisableConnector(dispenserIndex);
  3505. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex], 0x00, sizeof(struct DispenserModule));
  3506. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].LocalStatus = _DS_Timeout;
  3507. DispenserCheckInInfoUpdate();
  3508. }
  3509. break;
  3510. }
  3511. }
  3512. }
  3513. int GetMacAddress(int sockFd, unsigned char *mac)
  3514. {
  3515. struct arpreq arpReq;
  3516. struct sockaddr_in clientInfo;
  3517. socklen_t len = sizeof(struct sockaddr_in);
  3518. memset(mac, 0x00, 6);
  3519. if(getpeername(sockFd, (struct sockaddr*)&clientInfo, &len) < 0)
  3520. {
  3521. return 0;
  3522. }
  3523. else
  3524. {
  3525. memcpy(&arpReq.arp_pa, &clientInfo, sizeof(struct sockaddr_in));
  3526. strcpy(arpReq.arp_dev, "eth1");
  3527. arpReq.arp_pa.sa_family = AF_INET;
  3528. arpReq.arp_ha.sa_family = AF_UNSPEC;
  3529. if(ioctl(sockFd, SIOCGARP, &arpReq) < 0)
  3530. {
  3531. return 0;
  3532. }
  3533. else
  3534. {
  3535. memcpy(mac, arpReq.arp_ha.sa_data, 6);
  3536. }
  3537. }
  3538. return 1;
  3539. }
  3540. int FindFreeChannel(void)
  3541. {
  3542. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  3543. {
  3544. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_FREE)
  3545. {
  3546. return i;
  3547. }
  3548. }
  3549. return FAIL;
  3550. }
  3551. BOOL IsConflictIp(uint32_t ipAddress)
  3552. {
  3553. for(int i = 0; i < MAXIMUM_CONNECT_QUANTITY; i++)
  3554. {
  3555. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress == ipAddress)
  3556. {
  3557. int dispenser = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  3558. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.DuplicateIp == false)
  3559. {
  3560. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].Setting.bits.DuplicateIp = true;
  3561. }
  3562. return true;
  3563. }
  3564. }
  3565. return false;
  3566. }
  3567. void InitDispenserInfo(void)
  3568. {
  3569. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3570. {
  3571. bool disconnect = false;
  3572. //memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i], 0x00, sizeof(struct ConnectorInfoData));
  3573. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteStatus = 0;
  3574. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Enable = 0;
  3575. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ReadyToCharge = 0;
  3576. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex = 0;
  3577. disconnect = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection;
  3578. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.Value = 0;
  3579. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection = disconnect;
  3580. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].WarningInfo, 0x00, sizeof(struct WARNING_CODE_INFO));
  3581. //memset(&LastWarningInfo[i], 0x00, sizeof(struct WARNING_CODE_INFO));
  3582. }
  3583. //memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo, 0x00, sizeof(struct DispenserInfoData));
  3584. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence;
  3585. ShmSysConfigAndInfo->SysInfo.DispenserInfo.TotalConnectorQuantity = ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity;
  3586. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentDispenserQuantity = 0;
  3587. ShmSysConfigAndInfo->SysInfo.DispenserInfo.PresentConnectorQuantity = 0;
  3588. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3589. {
  3590. unsigned char localStatus = 0;
  3591. localStatus = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus;
  3592. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i], 0x00, sizeof(struct DispenserModule));
  3593. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus = localStatus;
  3594. }
  3595. // initial CheckInLog from WiringInfo.DispenserSequence
  3596. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status = 0;
  3597. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  3598. {
  3599. ShmSysConfigAndInfo->SysInfo.DispenserInfo.CheckInLog.Status |= (1 << i);
  3600. }
  3601. // initial ConnectorLog from WiringInfo.WiringSetting
  3602. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog, 0x00, sizeof(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog));
  3603. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  3604. {
  3605. if(i < GENERAL_GUN_QUANTITY)
  3606. {
  3607. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorLog[i] = ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i];
  3608. }
  3609. }
  3610. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo, 0x00, sizeof(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo));
  3611. ShmChargerInfo->CabinetMiscValue.Currency = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  3612. }
  3613. struct timespec _IpConflicted_time[CONNECTOR_QUANTITY];
  3614. void DispenserIpConflictedCheck(void)
  3615. {
  3616. while(1)
  3617. {
  3618. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3619. {
  3620. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIp)
  3621. {
  3622. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIpConfirm == NO)
  3623. {
  3624. GetClockTime(&_IpConflicted_time[i]);
  3625. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIpConfirm = true;
  3626. LOG_INFO("Dispenser %d IP Conflicted Confirm", i + 1);
  3627. }
  3628. else
  3629. {
  3630. if((GetTimeoutValue(_IpConflicted_time[i]) / mSEC_VAL) >= IP_CONFLICTED_TIME)
  3631. {
  3632. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  3633. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  3634. {
  3635. unsigned char channel = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectionChannel;
  3636. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[channel].Status != _CNS_FREE)
  3637. {
  3638. LOG_INFO("Dispenser %d IP Conflicted Stop", i + 1);
  3639. DisableConnector(i);
  3640. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i], 0x00, sizeof(struct DispenserModule));
  3641. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus = _DS_Timeout;
  3642. DispenserCheckInInfoUpdate();
  3643. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[channel], 0x00, sizeof(struct ConnectionInfoData));
  3644. }
  3645. }
  3646. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIp = false;
  3647. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DuplicateIpConfirm = false;
  3648. LOG_INFO("Dispenser %d IP Conflicted Clean", i + 1);
  3649. }
  3650. }
  3651. }
  3652. }
  3653. usleep(500000);
  3654. }
  3655. }
  3656. void ForkIpConflictedCheck(void)
  3657. {
  3658. pid_t forkId;
  3659. forkId = fork();
  3660. if(forkId == 0)
  3661. {
  3662. DispenserIpConflictedCheck();
  3663. return;
  3664. }
  3665. else if(forkId == -1)
  3666. {
  3667. LOG_INFO("fork fail");
  3668. }
  3669. }
  3670. int tcpSocketServerStart(void)
  3671. {
  3672. int sockFd = 0;
  3673. int clientSockFd = 0;
  3674. int connectIndex = 0;
  3675. int bindStatus = 0;
  3676. int reuseaddr = 1;
  3677. pid_t forkId;
  3678. struct sockaddr_in serverInfo, clientInfo;
  3679. socklen_t addrlen = sizeof(clientInfo);
  3680. //struct timeval timeout = {3, 0};
  3681. sockFd = socket(AF_INET, SOCK_STREAM, 0);
  3682. if(sockFd == -1)
  3683. {
  3684. LOG_INFO("InitSocketServer NG\n");
  3685. sleep(5);
  3686. return FAIL;
  3687. }
  3688. setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
  3689. InitDispenserInfo();
  3690. bzero(&serverInfo,sizeof(serverInfo));
  3691. serverInfo.sin_family = PF_INET;
  3692. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  3693. serverInfo.sin_port = htons(TCP_LISTEN_PORT);
  3694. bindStatus = bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  3695. if(bindStatus < 0)
  3696. {
  3697. LOG_INFO("socket bind fail");
  3698. return FAIL;
  3699. }
  3700. listen(sockFd, CONNECTION_LIMIT);
  3701. LOG_INFO("Dispenser TCP Server Start");
  3702. signal(SIGCHLD, SIG_IGN);
  3703. ForkIpConflictedCheck();
  3704. // Main loop
  3705. while(1)
  3706. {
  3707. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  3708. /*
  3709. if((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == ENONET) ||
  3710. (errno == EPROTO) || (errno == ENOPROTOOPT) || (errno == EOPNOTSUPP) ||
  3711. (errno == ENETDOWN) || (errno == ENETUNREACH) || (errno == EHOSTDOWN) ||
  3712. (errno == EHOSTUNREACH) || (errno == ECONNABORTED))
  3713. {
  3714. printf("accept error: %s\n", strerror(errno));
  3715. //exit(EXIT_FAILURE);
  3716. //break;
  3717. }
  3718. else if ( errno == EINTR)
  3719. {
  3720. printf("accept EINTRn");
  3721. continue;
  3722. }
  3723. else
  3724. {
  3725. printf("AccepCb: accept error: %s\n", strerror(errno));
  3726. //break;
  3727. //exit(EXIT_FAILURE);
  3728. }*/
  3729. LOG_INFO("Client ip: %s is accepted at port: %d", inet_ntoa(clientInfo.sin_addr), clientInfo.sin_port);
  3730. connectIndex = FindFreeChannel();
  3731. if(connectIndex >= 0)
  3732. {
  3733. if(IsConflictIp(clientInfo.sin_addr.s_addr) == false)
  3734. {
  3735. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].Status = _CNS_WaitModelName;
  3736. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex].IpAddress = clientInfo.sin_addr.s_addr;
  3737. forkId = fork();
  3738. if(forkId == 0)
  3739. {
  3740. // child process
  3741. DispenserSocketProcess(clientSockFd, clientInfo, connectIndex);
  3742. close(clientSockFd);
  3743. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  3744. return 0;
  3745. }
  3746. else if(forkId == -1)
  3747. {
  3748. LOG_INFO("fork fail");
  3749. }
  3750. }
  3751. else
  3752. {
  3753. // conflict ip address
  3754. LOG_INFO("Conflict IP address, close socket");
  3755. close(clientSockFd);
  3756. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[connectIndex], 0x00, sizeof(struct ConnectionInfoData));
  3757. }
  3758. }
  3759. else
  3760. {
  3761. // no free channel
  3762. LOG_INFO("No free channel, close socket");
  3763. close(clientSockFd);
  3764. }
  3765. usleep(10000);
  3766. }
  3767. return 0;
  3768. }
  3769. void InitialConnector(void)
  3770. {
  3771. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3772. {
  3773. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  3774. }
  3775. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  3776. {
  3777. memset(&ConnectorCapability[i], 0x00, sizeof(ChargingCapabilityResponseInfo));
  3778. memcpy(ConnectorCapability[i].CurrencyString, Currency[0], 3);
  3779. memset(&ConnectorRefund[i], 0x00, sizeof(RefundResponse));
  3780. memset(&ConnectorPrepayment[i], 0x00, sizeof(PrepaymentResponseInfo));
  3781. memset(&ConnectorPaymentFailReason[i], 0x00, sizeof(PaymentFailResponse));
  3782. memset(&_ConnectorQRCode[i][0], 0x00, 128);
  3783. memset(&_stationInfo[i], 0x00, sizeof(StationInfoData));
  3784. memset(&_deductResult[i], 0x00, sizeof(DeductResultInfoData));
  3785. }
  3786. }
  3787. int main(void)
  3788. {
  3789. if(InitShareMemory() == FAIL)
  3790. {
  3791. #ifdef SystemLogMessage
  3792. LOG_ERROR("InitShareMemory NG");
  3793. #endif
  3794. sleep(5);
  3795. return 0;
  3796. }
  3797. InitialConnector();
  3798. // wait for self test completed
  3799. while(ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING ||
  3800. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  3801. {
  3802. sleep(1);
  3803. }
  3804. tcpSocketServerStart();
  3805. //tcpSocketClientStart();
  3806. return 0;
  3807. }