main.c 171 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "Config.h"
  31. #include <stdbool.h>
  32. #include <dirent.h>
  33. #include "timeout.h"
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define BUFFER_SIZE 128
  38. #define YES 1
  39. #define NO 0
  40. #define NORMAL 0
  41. #define ABNORMAL 1
  42. #define EQUAL 0
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define MtdBlockSize 0x600000
  47. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  48. #define UPGRADE_FAN 0x02
  49. #define UPGRADE_RB 0x03
  50. #define UPGRADE_PRI 0x04
  51. #define UPGRADE_AC 0x05
  52. #define SYSTEM_MIN_VOL 150
  53. #define NO_DEFINE 255
  54. #define DEFAULT_AC_INDEX 2
  55. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  56. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  57. int whileLoopTime = 10000; // 10 ms
  58. int wtdFd = -1;
  59. bool IsAuthorizingMode();
  60. void ClearAuthorizedFlag();
  61. bool isDetectPlugin();
  62. void ClearDetectPluginFlag();
  63. int mystrcmp(unsigned char *p1, unsigned char *p2);
  64. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  65. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  66. void ChargingTerminalProcess(byte gunIndex);
  67. void ChkPrimaryStatus();
  68. void StartSystemTimeoutDet(unsigned char flag);
  69. void StopSystemTimeoutDet();
  70. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  71. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  72. int StoreLogMsg_1(const char *fmt, ...);
  73. unsigned long GetTimeoutValue(struct timeval _sour_time);
  74. void gpio_set_value(unsigned int gpio, unsigned int value);
  75. void PRINTF_FUNC(char *string, ...);
  76. void ChangeGunSelectByIndex(byte sel);
  77. void RecordAlarmCode(byte gunIndex, char *code);
  78. void RecordWarningCode(byte gunIndex, char *code);
  79. void ReleaseWarningCodeByString(byte gunIndex, char *code);
  80. void ReleaseAlarmCode(byte gunIndex);
  81. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  82. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  83. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  84. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  85. struct StatusCodeData *ShmStatusCodeData;
  86. struct PsuData *ShmPsuData;
  87. struct CHAdeMOData *ShmCHAdeMOData;
  88. struct GBTData *ShmGBTData;
  89. struct CcsData *ShmCcsData;
  90. struct PrimaryMcuData *ShmPrimaryMcuData;
  91. struct FanModuleData *ShmFanModuleData;
  92. struct RelayModuleData *ShmRelayModuleData;
  93. struct OCPP16Data *ShmOCPP16Data;
  94. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  95. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  96. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  97. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  98. // for initial index to check EV board type is correct
  99. byte _gunIndex = 0;
  100. byte _acgunIndex = 0;
  101. byte _chademoIndex = 0;
  102. byte _ccsIndex = 0;
  103. byte _gb_Index = 0;
  104. byte _ac_Index = 0;
  105. byte bd0_1_status = 0;
  106. byte bd0_2_status = 0;
  107. byte bd1_1_status = 0;
  108. byte bd1_2_status = 0;
  109. bool isCardScan = false;
  110. bool isModelNameMatch = true;
  111. int rfidFd = -1;
  112. char* rfidPortName = "/dev/ttyS2";
  113. char* fwVersion = "D0.13.00.0000.00";
  114. //================================================
  115. // initial can-bus
  116. //================================================
  117. int InitCanBus()
  118. {
  119. int s0,nbytes;
  120. struct timeval tv;
  121. struct ifreq ifr0;
  122. struct sockaddr_can addr0;
  123. system("/sbin/ip link set can0 down");
  124. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  125. system("/sbin/ip link set can0 up");
  126. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  127. tv.tv_sec = 0;
  128. tv.tv_usec = 10000;
  129. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  130. {
  131. #ifdef SystemLogMessage
  132. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  133. #endif
  134. }
  135. nbytes=40960;
  136. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  137. {
  138. #ifdef SystemLogMessage
  139. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  140. #endif
  141. }
  142. nbytes=40960;
  143. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  144. {
  145. #ifdef SystemLogMessage
  146. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  147. #endif
  148. }
  149. strcpy(ifr0.ifr_name, "can0" );
  150. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  151. addr0.can_family = AF_CAN;
  152. addr0.can_ifindex = ifr0.ifr_ifindex;
  153. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  154. return s0;
  155. }
  156. //================================================
  157. // initial uart port
  158. //================================================
  159. char *_priPortName = "/dev/ttyS1";
  160. char *_485PortName = "/dev/ttyS5";
  161. int InitComPort(byte target)
  162. {
  163. int fd;
  164. struct termios tios;
  165. if(target == UPGRADE_PRI)
  166. fd = open(_priPortName, O_RDWR);
  167. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC)
  168. fd = open(_485PortName, O_RDWR);
  169. if(fd<=0)
  170. {
  171. #ifdef SystemLogMessage
  172. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  173. #endif
  174. return -1;
  175. }
  176. ioctl (fd, TCGETS, &tios);
  177. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  178. tios.c_lflag = 0;
  179. tios.c_iflag = 0;
  180. tios.c_oflag = 0;
  181. tios.c_cc[VMIN]=0;
  182. tios.c_cc[VTIME]=(unsigned char)1;
  183. tios.c_lflag=0;
  184. tcflush(fd, TCIFLUSH);
  185. ioctl (fd, TCSETS, &tios);
  186. return fd;
  187. }
  188. //=================================
  189. // Common routine
  190. //=================================
  191. int InitWatchDog()
  192. {
  193. int fd;
  194. system("/usr/bin/fuser -k /dev/watchdog");
  195. sleep(1);
  196. system("echo V > /dev/watchdog");
  197. sleep(1);
  198. fd=open("/dev/watchdog", O_RDWR);
  199. if(fd<=0)
  200. {
  201. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  202. }
  203. return fd;
  204. }
  205. int StoreLogMsg_1(const char *fmt, ...)
  206. {
  207. char Buf[4096+256];
  208. char buffer[4096];
  209. time_t CurrentTime;
  210. struct tm *tm;
  211. va_list args;
  212. va_start(args, fmt);
  213. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  214. va_end(args);
  215. memset(Buf,0,sizeof(Buf));
  216. CurrentTime = time(NULL);
  217. tm=localtime(&CurrentTime);
  218. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  219. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  220. buffer,
  221. tm->tm_year+1900,tm->tm_mon+1);
  222. system(Buf);
  223. return rc;
  224. }
  225. unsigned long GetTimeoutValue(struct timeval _sour_time)
  226. {
  227. struct timeval _end_time;
  228. gettimeofday(&_end_time, NULL);
  229. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  230. }
  231. int mystrcmp(unsigned char *p1, unsigned char *p2)
  232. {
  233. while(*p1==*p2)
  234. {
  235. if(*p1=='\0' || *p2=='\0')
  236. break;
  237. p1++;
  238. p2++;
  239. }
  240. if(*p1=='\0' && *p2=='\0')
  241. return(PASS);
  242. else
  243. return(FAIL);
  244. }
  245. int DiffTimeb(struct timeb ST, struct timeb ET)
  246. {
  247. //return milli-second
  248. unsigned int StartTime,StopTime;
  249. StartTime=(unsigned int)ST.time;
  250. StopTime=(unsigned int)ET.time;
  251. return (StopTime-StartTime);
  252. }
  253. bool CheckTimeOut(struct timeb ST)
  254. {
  255. struct timeb ET;
  256. unsigned int StartTime, StopTime;
  257. ftime(&ET);
  258. StartTime = (unsigned int) ST.time;
  259. StopTime = (unsigned int) ET.time;
  260. return (StopTime > StartTime)? YES : NO;
  261. }
  262. void setChargerMode(byte gun_index, byte mode)
  263. {
  264. chargingInfo[gun_index]->SystemStatus = mode;
  265. }
  266. void PRINTF_FUNC(char *string, ...)
  267. {
  268. va_list args;
  269. char buffer[4096];
  270. va_start(args, string);
  271. vsnprintf(buffer, sizeof(buffer), string, args);
  272. va_end(args);
  273. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  274. printf("%s \n", buffer);
  275. else
  276. DEBUG_INFO_MSG("%s \n", buffer);
  277. }
  278. //==========================================
  279. // Check interface status
  280. //==========================================
  281. int isInterfaceUp(const char *interface)
  282. {
  283. int result = FAIL;
  284. FILE *fp;
  285. char cmd[256];
  286. char buf[512];
  287. strcpy(cmd, "ifconfig");
  288. fp = popen(cmd, "r");
  289. if(fp != NULL)
  290. {
  291. while(fgets(buf, sizeof(buf), fp) != NULL)
  292. {
  293. if(strstr(buf, interface) > 0)
  294. {
  295. result = PASS;
  296. }
  297. }
  298. }
  299. pclose(fp);
  300. return result;
  301. }
  302. //=================================
  303. // Create all share memory
  304. //=================================
  305. int CreateShareMemory()
  306. {
  307. int MeterSMId;
  308. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  309. {
  310. #ifdef SystemLogMessage
  311. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  312. #endif
  313. return 0;
  314. }
  315. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  316. {
  317. #ifdef SystemLogMessage
  318. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  319. #endif
  320. return 0;
  321. }
  322. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  323. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  324. {
  325. #ifdef SystemLogMessage
  326. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  327. #endif
  328. return 0;
  329. }
  330. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  331. {
  332. #ifdef SystemLogMessage
  333. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  334. #endif
  335. return 0;
  336. }
  337. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  338. //creat ShmPsuData
  339. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  340. {
  341. #ifdef SystemLogMessage
  342. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  343. #endif
  344. return 0;
  345. }
  346. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  347. {
  348. #ifdef SystemLogMessage
  349. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  350. #endif
  351. return 0;
  352. }
  353. memset(ShmPsuData, 0, sizeof(struct PsuData));
  354. if(CHAdeMO_QUANTITY > 0)
  355. {
  356. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  357. {
  358. #ifdef SystemLogMessage
  359. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  360. #endif
  361. return 0;
  362. }
  363. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  364. {
  365. #ifdef SystemLogMessage
  366. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  367. #endif
  368. return 0;
  369. }
  370. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  371. }
  372. if(GB_QUANTITY > 0)
  373. {
  374. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  375. {
  376. #ifdef SystemLogMessage
  377. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  378. #endif
  379. return 0;
  380. }
  381. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  382. {
  383. #ifdef SystemLogMessage
  384. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  385. #endif
  386. return 0;
  387. }
  388. memset(ShmGBTData, 0, sizeof(struct GBTData));
  389. }
  390. //creat ShmCcsData
  391. if(CCS_QUANTITY > 0)
  392. {
  393. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  394. {
  395. #ifdef SystemLogMessage
  396. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  397. #endif
  398. return 0;
  399. }
  400. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  401. {
  402. #ifdef SystemLogMessage
  403. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  404. #endif
  405. return 0;
  406. }
  407. memset(ShmCcsData, 0, sizeof(struct CcsData));
  408. }
  409. //creat ShmPrimaryMcuData
  410. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  411. {
  412. #ifdef SystemLogMessage
  413. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  414. #endif
  415. return 0;
  416. }
  417. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  418. {
  419. #ifdef SystemLogMessage
  420. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  421. #endif
  422. return 0;
  423. }
  424. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  425. //creat ShmFanModuleData
  426. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  427. {
  428. #ifdef SystemLogMessage
  429. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  430. #endif
  431. return 0;
  432. }
  433. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  434. {
  435. #ifdef SystemLogMessage
  436. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  437. #endif
  438. return 0;
  439. }
  440. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  441. //creat ShmRelayModuleData
  442. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  443. {
  444. #ifdef SystemLogMessage
  445. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  446. #endif
  447. return 0;
  448. }
  449. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  450. {
  451. #ifdef SystemLogMessage
  452. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  453. #endif
  454. return 0;
  455. }
  456. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  457. //creat ShmOCPP16Data
  458. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  459. {
  460. #ifdef SystemLogMessage
  461. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  462. #endif
  463. return 0;
  464. }
  465. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  466. {
  467. #ifdef SystemLogMessage
  468. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  469. #endif
  470. return 0;
  471. }
  472. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  473. return 1;
  474. }
  475. //=================================
  476. // LCM Page
  477. //=================================
  478. void ChangeLcmByIndex(byte page_index)
  479. {
  480. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  481. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  482. {
  483. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  484. }
  485. }
  486. //======================================================
  487. // Peripheral initial
  488. //======================================================
  489. void InitGPIO()
  490. {
  491. /*****************0~3, 4 bank, bank x 32+ num*********************/
  492. /***************************************************************/
  493. /*************** GPIO 0 ***************************************/
  494. /***************************************************************/
  495. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  496. system("echo 22 > /sys/class/gpio/export");
  497. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  498. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  499. system("echo 23 > /sys/class/gpio/export");
  500. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  501. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  502. system("echo 26 > /sys/class/gpio/export");
  503. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  504. system("echo 1 > /sys/class/gpio/gpio26/value");
  505. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  506. system("echo 27 > /sys/class/gpio/export");
  507. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  508. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  509. system("echo 29 > /sys/class/gpio/export");
  510. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  511. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  512. system("echo 19 > /sys/class/gpio/export");
  513. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  514. system("echo 1 > /sys/class/gpio/gpio19/value");
  515. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  516. system("echo 20 > /sys/class/gpio/export");
  517. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  518. /***************************************************************/
  519. /*************** GPIO 1 ***************************************/
  520. /***************************************************************/
  521. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  522. system("echo 44 > /sys/class/gpio/export");
  523. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  524. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  525. system("echo 45 > /sys/class/gpio/export");
  526. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  527. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  528. system("echo 46 > /sys/class/gpio/export");
  529. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  530. system("echo 0 > /sys/class/gpio/gpio46/value");
  531. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  532. system("echo 47 > /sys/class/gpio/export");
  533. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  534. /***************************************************************/
  535. /*************** GPIO 2 ***************************************/
  536. /***************************************************************/
  537. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  538. system("echo 89 > /sys/class/gpio/export");
  539. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  540. system("echo 1 > /sys/class/gpio/gpio89/value");
  541. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  542. system("echo 87 > /sys/class/gpio/export");
  543. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  544. system("echo 0 > /sys/class/gpio/gpio87/value");
  545. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  546. system("echo 88 > /sys/class/gpio/export");
  547. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  548. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  549. system("echo 86 > /sys/class/gpio/export");
  550. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  551. /***************************************************************/
  552. /*************** GPIO 3 ***************************************/
  553. /***************************************************************/
  554. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  555. system("echo 111 > /sys/class/gpio/export");
  556. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  557. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  558. system("echo 114 > /sys/class/gpio/export");
  559. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  560. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  561. system("echo 113 > /sys/class/gpio/export");
  562. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  563. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  564. system("echo 110 > /sys/class/gpio/export");
  565. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  566. system("echo 0 > /sys/class/gpio/gpio110/value");
  567. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  568. system("echo 115 > /sys/class/gpio/export");
  569. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  570. system("echo 0 > /sys/class/gpio/gpio115/value");
  571. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  572. system("echo 112 > /sys/class/gpio/export");
  573. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  574. system("echo 1 > /sys/class/gpio/gpio112/value");
  575. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  576. system("echo 116 > /sys/class/gpio/export");
  577. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  578. system("echo 0 > /sys/class/gpio/gpio116/value");
  579. #ifdef SystemLogMessage
  580. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  581. #endif
  582. }
  583. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  584. {
  585. int fd,wrd;
  586. unsigned char *buf;
  587. unsigned int ChkSum,ChkSumOrg;
  588. if((buf=malloc(MtdBlockSize))==NULL)
  589. {
  590. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  591. if(ShmStatusCodeData!=NULL)
  592. {
  593. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  594. }
  595. sleep(5);
  596. system("reboot -f");
  597. sleep(5);
  598. system("reboot -f");
  599. }
  600. memset(buf, 0, MtdBlockSize);
  601. //================================================
  602. // Load configuration from mtdblock10
  603. //================================================
  604. fd = open("/dev/mtdblock10", O_RDWR);
  605. if (fd < 0)
  606. {
  607. free(buf);
  608. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  609. if(ShmStatusCodeData!=NULL)
  610. {
  611. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  612. }
  613. sleep(5);
  614. system("reboot -f");
  615. sleep(5);
  616. system("reboot -f");
  617. }
  618. wrd=read(fd, buf, MtdBlockSize);
  619. close(fd);
  620. if(wrd<MtdBlockSize)
  621. {
  622. free(buf);
  623. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  624. if(ShmStatusCodeData!=NULL)
  625. {
  626. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  627. }
  628. sleep(5);
  629. system("reboot -f");
  630. sleep(5);
  631. system("reboot -f");
  632. }
  633. ChkSum=0;
  634. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  635. {
  636. ChkSum+=buf[wrd];
  637. }
  638. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  639. //================================================
  640. // Load configuration from mtdblock11
  641. //================================================
  642. if(ChkSum!=ChkSumOrg)
  643. {
  644. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  645. fd = open("/dev/mtdblock11", O_RDWR);
  646. if (fd < 0)
  647. {
  648. free(buf);
  649. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  650. if(ShmStatusCodeData!=NULL)
  651. {
  652. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  653. }
  654. sleep(5);
  655. system("reboot -f");
  656. sleep(5);
  657. system("reboot -f");
  658. }
  659. memset(buf, 0, MtdBlockSize);
  660. wrd=read(fd, buf,MtdBlockSize);
  661. close(fd);
  662. if(wrd<MtdBlockSize)
  663. {
  664. free(buf);
  665. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  666. if(ShmStatusCodeData!=NULL)
  667. {
  668. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  669. }
  670. sleep(5);
  671. system("reboot -f");
  672. sleep(5);
  673. system("reboot -f");
  674. }
  675. ChkSum=0;
  676. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  677. {
  678. ChkSum+=buf[wrd];
  679. }
  680. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  681. //================================================
  682. // Load configuration from mtdblock12 (Factory default)
  683. //================================================
  684. if(ChkSum!=ChkSumOrg)
  685. {
  686. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  687. fd = open("/dev/mtdblock12", O_RDWR);
  688. if (fd < 0)
  689. {
  690. free(buf);
  691. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  692. if(ShmStatusCodeData!=NULL)
  693. {
  694. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  695. }
  696. sleep(5);
  697. system("reboot -f");
  698. sleep(5);
  699. system("reboot -f");
  700. }
  701. memset(buf, 0, MtdBlockSize);
  702. wrd=read(fd, buf,MtdBlockSize);
  703. close(fd);
  704. if(wrd<MtdBlockSize)
  705. {
  706. free(buf);
  707. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  708. if(ShmStatusCodeData!=NULL)
  709. {
  710. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  711. }
  712. sleep(5);
  713. system("reboot -f");
  714. sleep(5);
  715. system("reboot -f");
  716. }
  717. ChkSum=0;
  718. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  719. {
  720. ChkSum+=buf[wrd];
  721. }
  722. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  723. if(ChkSum!=ChkSumOrg)
  724. {
  725. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  726. free(buf);
  727. system("cd /root;./FactoryConfig -m");
  728. system("sync");
  729. sleep(5);
  730. system("reboot -f");
  731. sleep(5);
  732. system("reboot -f");
  733. return FAIL;
  734. }
  735. }
  736. }
  737. //load OK
  738. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  739. free(buf);
  740. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  741. return PASS;
  742. }
  743. int isReachableInternet()
  744. {
  745. int result = FAIL;
  746. FILE *fp;
  747. char cmd[256];
  748. char buf[512];
  749. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  750. {
  751. strcpy(cmd, "ping -c 1 -w 3 ");
  752. strcat(cmd, valid_Internet[idx]);
  753. fp = popen(cmd, "r");
  754. if(fp != NULL)
  755. {
  756. while(fgets(buf, sizeof(buf), fp) != NULL)
  757. {
  758. if(strstr(buf, "transmitted") > 0)
  759. {
  760. if(strstr(buf,"100%") != NULL)
  761. {
  762. //NULL
  763. }
  764. else
  765. {
  766. result = PASS;
  767. }
  768. }
  769. }
  770. }
  771. pclose(fp);
  772. }
  773. return result;
  774. }
  775. void InitEthernet()
  776. {
  777. char tmpbuf[256];
  778. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  779. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  780. sleep(2);
  781. //Init Eth0 for internet
  782. memset(tmpbuf,0,256);
  783. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  784. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  785. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  786. system(tmpbuf);
  787. memset(tmpbuf,0,256);
  788. sprintf(tmpbuf,"route add default gw %s eth0 ",
  789. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  790. system(tmpbuf);
  791. //Init Eth1 for administrator tool
  792. memset(tmpbuf,0,256);
  793. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  794. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  795. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  796. system(tmpbuf);
  797. //Run DHCP client if enabled
  798. system("killall udhcpc");
  799. system("rm -rf /etc/resolv.conf");
  800. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  801. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  802. if(ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient == 0)
  803. {
  804. memset(tmpbuf,0,256);
  805. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  806. system(tmpbuf);
  807. }
  808. //Upgrade system id to /etc/hostname
  809. memset(tmpbuf,0,256);
  810. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  811. system(tmpbuf);
  812. pid_t pid = fork();
  813. uint8_t cnt_pingDNS_Fail;
  814. if(pid == 0)
  815. {
  816. for(;;)
  817. {
  818. if(isReachableInternet() == PASS)
  819. {
  820. ShmSysConfigAndInfo->SysInfo.InternetConn = YES;
  821. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= NO;
  822. cnt_pingDNS_Fail = 0;
  823. }
  824. else
  825. {
  826. if(++cnt_pingDNS_Fail > 3)
  827. {
  828. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  829. //ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= YES;
  830. }
  831. }
  832. sleep(5);
  833. }
  834. }
  835. #ifdef SystemLogMessage
  836. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK");
  837. #endif
  838. }
  839. int InitialRfidPort()
  840. {
  841. int uartO2 = open(rfidPortName, O_RDWR);
  842. struct termios tios;
  843. if (uartO2 != FAIL)
  844. {
  845. ioctl (uartO2, TCGETS, &tios);
  846. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  847. tios.c_lflag = 0;
  848. tios.c_iflag = 0;
  849. tios.c_oflag = 0;
  850. tios.c_cc[VMIN] = 0;
  851. tios.c_cc[VTIME] = (unsigned char) 1;
  852. tios.c_lflag = 0;
  853. tcflush(uartO2, TCIFLUSH);
  854. ioctl(uartO2, TCSETS, &tios);
  855. }
  856. if (uartO2 < 0)
  857. {
  858. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  859. }
  860. return uartO2;
  861. }
  862. void GetMacAddress()
  863. {
  864. for (byte index = 0; index < 2; index++)
  865. {
  866. int fd;
  867. struct ifreq ifr;
  868. char tarEth[5];
  869. char Mac[18];
  870. sprintf(tarEth,"eth%d",index);
  871. fd = socket(AF_INET, SOCK_DGRAM, 0);
  872. ifr.ifr_addr.sa_family = AF_INET;
  873. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  874. ioctl(fd, SIOCGIFHWADDR, &ifr);
  875. close(fd);
  876. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  877. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  878. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  879. if (index == 0)
  880. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  881. else
  882. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  883. }
  884. }
  885. void GetFirmwareVersion()
  886. {
  887. // Get CSU root file system version
  888. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  889. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  890. for(uint8_t idx=0;idx<3;idx++)
  891. {
  892. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  893. {
  894. chademo++;
  895. count++;
  896. }
  897. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  898. {
  899. gb++;
  900. count++;
  901. }
  902. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  903. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  904. {
  905. ccs++;
  906. count++;
  907. }
  908. }
  909. if (count == 1)
  910. {
  911. if (chademo > 0)
  912. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  913. else if (ccs > 0)
  914. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  915. else if (gb > 0)
  916. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  917. }
  918. else
  919. {
  920. if (chademo > 0 && ccs > 0)
  921. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  922. else if (chademo > 0 && gb > 0)
  923. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  924. else if (ccs > 0 && gb > 0)
  925. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  926. }
  927. // Get network option from model name
  928. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  929. {
  930. case 'B':
  931. case 'U':
  932. //Blue tooth
  933. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  934. break;
  935. case 'W':
  936. // WIFI
  937. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  938. break;
  939. case 'T':
  940. // 3G/4G
  941. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  942. break;
  943. default:
  944. // LAN
  945. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  946. break;
  947. }
  948. // Get rating power from model name
  949. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  950. // Get IEC or UL
  951. char _buf[3] = {0};
  952. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  953. if (strcmp(_buf, "YE") == EQUAL ||
  954. strcmp(_buf, "YC") == EQUAL)
  955. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  956. else if (strcmp(_buf, "WU") == EQUAL)
  957. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  958. }
  959. void InitialShareMemoryInfo()
  960. {
  961. FILE *fp;
  962. char cmd[512];
  963. char buf[512];
  964. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  965. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  966. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  967. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  968. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  969. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  970. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  971. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  972. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  973. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  974. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  975. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  976. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  977. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  978. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  979. sprintf(cmd, "/bin/uname -r");
  980. fp = popen(cmd, "r");
  981. if(fp == NULL)
  982. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  983. else
  984. {
  985. while(fgets(buf, sizeof(buf), fp) != NULL)
  986. {
  987. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  988. }
  989. }
  990. // 雙槍 CCS + Chademo
  991. GetFirmwareVersion();
  992. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  993. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  994. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  995. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  996. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  997. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  998. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  999. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1000. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1001. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1002. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1003. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1004. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1005. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1006. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1007. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1008. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1009. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1010. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1011. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  1012. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1013. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1014. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1015. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1016. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1017. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1018. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1019. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1020. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1021. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1022. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1023. ShmRelayModuleData->SelfTest_Comp = NO;
  1024. ShmFanModuleData->SelfTest_Comp = NO;
  1025. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1026. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1027. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1028. ShmFanModuleData->TestFanSpeed = 0;
  1029. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1030. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1031. char EvsePower[2];
  1032. EvsePower[2] = '\0';
  1033. unsigned short buf_pow = 0;
  1034. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  1035. {
  1036. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  1037. if (strcmp(EvsePower, "15") == EQUAL)
  1038. buf_pow = 150;
  1039. else if (strcmp(EvsePower, "30") == EQUAL)
  1040. buf_pow = 30;
  1041. else if (strcmp(EvsePower, "60") == EQUAL)
  1042. buf_pow = 60;
  1043. else if (strcmp(EvsePower, "18") == EQUAL)
  1044. buf_pow = 180;
  1045. else if (strcmp(EvsePower, "36") == EQUAL)
  1046. buf_pow = 360;
  1047. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (buf_pow / 30) * 100;
  1048. if(ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1049. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow)
  1050. {
  1051. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1052. }
  1053. }
  1054. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1055. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1056. }
  1057. int Initialization()
  1058. {
  1059. // 初始化卡號驗證的 Flag
  1060. ClearAuthorizedFlag();
  1061. // 初始化插槍驗證的 Flag
  1062. ClearDetectPluginFlag();
  1063. // UART 2 for Rfid
  1064. rfidFd = InitialRfidPort();
  1065. int pinOut[2] = { 115, 116 };
  1066. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1067. {
  1068. if (chargingInfo[count]->Type == _Type_Chademo)
  1069. {
  1070. gpio_set_value(pinOut[count], 0x01);
  1071. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1072. }
  1073. else if (chargingInfo[count]->Type == _Type_GB)
  1074. {
  1075. gpio_set_value(pinOut[count], 0x01);
  1076. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1077. }
  1078. else if (chargingInfo[count]->Type == _Type_CCS_2)
  1079. {
  1080. if (ShmCcsData->CommProtocol == 0x01)
  1081. {
  1082. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1083. gpio_set_value(pinOut[1], 0x00);
  1084. else
  1085. gpio_set_value(pinOut[count], 0x00);
  1086. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1087. }
  1088. }
  1089. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1090. }
  1091. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1092. {
  1093. if (ac_chargingInfo[count]->Type == _Type_AC)
  1094. {
  1095. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1096. }
  1097. }
  1098. PRINTF_FUNC("Initialization OK \n");
  1099. return PASS;
  1100. }
  1101. bool InitialSystemDefaultConfig()
  1102. {
  1103. bool result = true;
  1104. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1105. InitGPIO();
  1106. InitEthernet();
  1107. GetMacAddress();
  1108. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1109. // sleep(3);
  1110. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1111. // sleep(1);
  1112. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
  1113. return result;
  1114. }
  1115. void DisplaySelfTestFailReason()
  1116. {
  1117. // RB、FB、407、EV 小板中有些板子無回應
  1118. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1119. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; }
  1120. if (ShmFanModuleData->SelfTest_Comp == NO)
  1121. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; }
  1122. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1123. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; }
  1124. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1125. {
  1126. if (chargingInfo[index]->Type == _Type_Chademo)
  1127. {
  1128. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1129. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; }
  1130. }
  1131. else if (chargingInfo[index]->Type == _Type_GB)
  1132. {
  1133. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1134. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; }
  1135. }
  1136. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1137. {
  1138. if (ShmCcsData->CommProtocol == 0x01)
  1139. {
  1140. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1141. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; }
  1142. }
  1143. }
  1144. }
  1145. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1146. {
  1147. // 先借 GBT 顯示
  1148. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1149. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; }
  1150. }
  1151. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1152. {
  1153. // AC Contact 未搭上
  1154. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1155. }
  1156. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1157. {
  1158. // PSU 通訊問題
  1159. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1160. }
  1161. }
  1162. void SelfTestRun()
  1163. {
  1164. bool evInitFlag = false;
  1165. StartSystemTimeoutDet(Timeout_SelftestChk);
  1166. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1167. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1168. {
  1169. ChkPrimaryStatus();
  1170. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1171. {
  1172. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1173. return;
  1174. }
  1175. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1176. {
  1177. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1178. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1179. {
  1180. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1181. return;
  1182. }
  1183. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1184. {
  1185. case _STEST_VERSION:
  1186. {
  1187. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  1188. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  1189. {
  1190. //PRINTF_FUNC("RB pass \n");
  1191. ShmRelayModuleData->SelfTest_Comp = YES;
  1192. }
  1193. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  1194. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  1195. {
  1196. //PRINTF_FUNC("Fan pass \n");
  1197. ShmFanModuleData->SelfTest_Comp = YES;
  1198. }
  1199. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  1200. ShmPrimaryMcuData->version[0] != '\0')
  1201. {
  1202. //PRINTF_FUNC("407 pass \n");
  1203. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1204. }
  1205. // EV 小板
  1206. if (!evInitFlag)
  1207. {
  1208. evInitFlag = YES;
  1209. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1210. {
  1211. if (chargingInfo[index]->Type == _Type_Chademo)
  1212. {
  1213. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1214. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1215. {
  1216. //PRINTF_FUNC("chademo pass \n");
  1217. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1218. }
  1219. else
  1220. {
  1221. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1222. evInitFlag = NO;
  1223. }
  1224. }
  1225. else if (chargingInfo[index]->Type == _Type_GB)
  1226. {
  1227. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1228. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1229. {
  1230. //PRINTF_FUNC("GBT pass \n");
  1231. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1232. }
  1233. else
  1234. {
  1235. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1236. evInitFlag = NO;
  1237. }
  1238. }
  1239. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1240. {
  1241. if (ShmCcsData->CommProtocol == 0x01)
  1242. {
  1243. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  1244. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  1245. {
  1246. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1247. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1248. }
  1249. else
  1250. {
  1251. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1252. evInitFlag = NO;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1258. {
  1259. if (ac_chargingInfo[index]->Type == _Type_AC)
  1260. {
  1261. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  1262. ac_chargingInfo[index]->version[0] != '\0')
  1263. {
  1264. ac_chargingInfo[index]->SelfTest_Comp = YES;
  1265. }
  1266. else
  1267. {
  1268. evInitFlag = NO;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. if (ShmFanModuleData->SelfTest_Comp &&
  1274. ShmRelayModuleData->SelfTest_Comp &&
  1275. ShmPrimaryMcuData->SelfTest_Comp &&
  1276. evInitFlag)
  1277. {
  1278. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1279. }
  1280. }
  1281. break;
  1282. case _STEST_AC_CONTACTOR:
  1283. {
  1284. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1285. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1286. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1287. {
  1288. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1289. PRINTF_FUNC("Communication board pass. \n");
  1290. }
  1291. }
  1292. break;
  1293. case _STEST_PSU_DETECT:
  1294. {
  1295. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1296. {
  1297. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1298. }
  1299. }
  1300. break;
  1301. case _STEST_PSU_CAP:
  1302. {
  1303. // 此測試是要確認當前總輸出能力
  1304. // 如果沒有 PSU 模組請 bypass
  1305. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1306. {
  1307. sleep(1);
  1308. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1309. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1310. }
  1311. }
  1312. break;
  1313. }
  1314. }
  1315. else
  1316. break;
  1317. usleep(100000);
  1318. }
  1319. }
  1320. int SpawnTask()
  1321. {
  1322. sleep(2);
  1323. system("/root/Module_EventLogging &");
  1324. system("/root/Module_PrimaryComm &");
  1325. system("/root/Module_EvComm &");
  1326. system("/root/Module_LcmControl &");
  1327. system("/root/Module_InternalComm &");
  1328. system("/root/Module_PsuComm &");
  1329. system("/root/Module_ProduceUtils &");
  1330. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1331. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  1332. {
  1333. system("/root/OcppBackend &");
  1334. }
  1335. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1336. {
  1337. system("/root/Module_4g &");
  1338. }
  1339. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1340. {
  1341. system("/root/Module_Wifi &");
  1342. }
  1343. return PASS;
  1344. }
  1345. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1346. {
  1347. int result = PASS;
  1348. int fd,wrd;
  1349. unsigned int i,Chk;
  1350. unsigned char *ptr, *BufTmp;
  1351. Chk=0;
  1352. ptr=(unsigned char *)UsrData;
  1353. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  1354. {
  1355. memset(BufTmp, 0, MtdBlockSize);
  1356. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1357. for(i=0; i<MtdBlockSize-4; i++)
  1358. Chk+=*(ptr+i);
  1359. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  1360. fd = open("/dev/mtdblock10", O_RDWR);
  1361. if (fd > 0)
  1362. {
  1363. wrd=write(fd, BufTmp, MtdBlockSize);
  1364. close(fd);
  1365. if(wrd >= MtdBlockSize)
  1366. {
  1367. fd = open("/dev/mtdblock11", O_RDWR);
  1368. if (fd > 0)
  1369. {
  1370. wrd=write(fd, BufTmp, MtdBlockSize);
  1371. close(fd);
  1372. if(wrd < MtdBlockSize)
  1373. {
  1374. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  1375. result = FAIL;
  1376. }
  1377. }
  1378. else
  1379. {
  1380. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  1381. result = FAIL;
  1382. }
  1383. }
  1384. else
  1385. {
  1386. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  1387. result = FAIL;
  1388. }
  1389. }
  1390. else
  1391. {
  1392. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  1393. result = FAIL;
  1394. }
  1395. }
  1396. else
  1397. {
  1398. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  1399. result = FAIL;
  1400. }
  1401. if(BufTmp != NULL)
  1402. free(BufTmp);
  1403. return result;
  1404. }
  1405. //===============================================
  1406. // Common Detect Chk - Stop Charging ?
  1407. //===============================================
  1408. bool isEvBoardStopChargeFlag(byte gunIndex)
  1409. {
  1410. return chargingInfo[gunIndex]->StopChargeFlag;
  1411. }
  1412. //===============================================
  1413. // 掃描插槍狀況
  1414. //===============================================
  1415. void ClearDetectPluginFlag()
  1416. {
  1417. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1418. }
  1419. void DetectPluginStart()
  1420. {
  1421. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1422. }
  1423. bool isDetectPlugin()
  1424. {
  1425. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1426. return YES;
  1427. return NO;
  1428. }
  1429. //===============================================
  1430. // Common Detect Chk - Chademo
  1431. //===============================================
  1432. bool isEvGunLocked_chademo(byte gunIndex)
  1433. {
  1434. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1435. }
  1436. bool isEvContactorWelding_chademo(byte gunIndex)
  1437. {
  1438. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1439. }
  1440. bool isEvStopReq_chademo(byte gunIndex)
  1441. {
  1442. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1443. }
  1444. bool isEvStopCharging_chademo(byte gunIndex)
  1445. {
  1446. if (isEvGunLocked_chademo(gunIndex) == NO)
  1447. {
  1448. // 無鎖槍 = 停止
  1449. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  1450. return YES;
  1451. }
  1452. return NO;
  1453. }
  1454. byte isPrechargeStatus_chademo(byte gunIndex)
  1455. {
  1456. byte result = 0x00;
  1457. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1458. return result;
  1459. }
  1460. //===============================================
  1461. // Common Detect Chk - GB
  1462. //===============================================
  1463. bool isEvGunLocked_gb(byte gunIndex)
  1464. {
  1465. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1466. }
  1467. bool isEvStopCharging_gb(byte gunIndex)
  1468. {
  1469. if (isEvGunLocked_gb(gunIndex) == NO)
  1470. {
  1471. // 無鎖槍 = 停止
  1472. PRINTF_FUNC("gun locked none. \n");
  1473. return YES;
  1474. }
  1475. return NO;
  1476. }
  1477. byte isPrechargeStatus_gb(byte gunIndex)
  1478. {
  1479. byte result = 0x00;
  1480. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1481. return result;
  1482. }
  1483. //===============================================
  1484. // Common Detect Chk - CCS
  1485. //===============================================
  1486. bool isEvGunLocked_ccs(byte gunIndex)
  1487. {
  1488. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1489. }
  1490. byte isPrechargeStatus_ccs(byte gunIndex)
  1491. {
  1492. byte result = 0x00;
  1493. if (ShmCcsData->CommProtocol == 0x01)
  1494. {
  1495. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1496. }
  1497. return result;
  1498. }
  1499. bool isEvStopCharging_ccs(byte gunIndex)
  1500. {
  1501. if (isEvGunLocked_ccs(gunIndex) == NO)
  1502. {
  1503. // 無鎖槍 = 停止
  1504. PRINTF_FUNC("gun locked none. \n");
  1505. return YES;
  1506. }
  1507. return NO;
  1508. }
  1509. //===============================================
  1510. // Callback
  1511. //===============================================
  1512. void DisplayChargingInfo()
  1513. {
  1514. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  1515. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1516. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  1517. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  1518. {
  1519. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1520. }
  1521. else
  1522. {
  1523. for (byte i = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount - 1; i >= 0; i--)
  1524. {
  1525. if (chargingInfo[i]->SystemStatus != S_IDLE)
  1526. {
  1527. ChangeGunSelectByIndex(i);
  1528. break;
  1529. }
  1530. }
  1531. }
  1532. usleep(50000);
  1533. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1534. }
  1535. void _AutoReturnTimeout()
  1536. {
  1537. PRINTF_FUNC("*********** _AutoReturnTimeout *********** \n");
  1538. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1539. {
  1540. ClearDetectPluginFlag();
  1541. }
  1542. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1543. {
  1544. DetectPluginStart();
  1545. }
  1546. usleep(50000);
  1547. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1548. }
  1549. void _SelfTestTimeout()
  1550. {
  1551. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1552. {
  1553. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  1554. {
  1555. setChargerMode(gun_index, MODE_ALARM);
  1556. }
  1557. }
  1558. ShmPsuData->Work_Step = _NO_WORKING;
  1559. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1560. PRINTF_FUNC("Self test timeout. \n");
  1561. }
  1562. void _AuthorizedTimeout()
  1563. {
  1564. if(IsAuthorizingMode())
  1565. {
  1566. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  1567. isCardScan = false;
  1568. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1569. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1570. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1571. ClearAuthorizedFlag();
  1572. }
  1573. }
  1574. void _DetectPlugInTimeout()
  1575. {
  1576. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  1577. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1578. ClearDetectPluginFlag();
  1579. usleep(50000);
  1580. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1581. }
  1582. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1583. {
  1584. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1585. {
  1586. if(!isEvGunLocked_chademo(gunIndex))
  1587. {
  1588. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  1589. }
  1590. }
  1591. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1592. {
  1593. if(!isEvGunLocked_ccs(gunIndex))
  1594. {
  1595. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  1596. }
  1597. }
  1598. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1599. {
  1600. if(!isEvGunLocked_ccs(gunIndex))
  1601. {
  1602. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  1603. }
  1604. }
  1605. ChargingTerminalProcess(gunIndex);
  1606. _AutoReturnTimeout();
  1607. }
  1608. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1609. {
  1610. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  1611. setChargerMode(gunIndex, MODE_IDLE);
  1612. _AutoReturnTimeout();
  1613. }
  1614. void _PrepareTimeout(byte gunIndex)
  1615. {
  1616. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  1617. setChargerMode(gunIndex, MODE_IDLE);
  1618. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  1619. _AutoReturnTimeout();
  1620. }
  1621. void _CcsPrechargeTimeout(byte gunIndex)
  1622. {
  1623. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  1624. setChargerMode(gunIndex, MODE_IDLE);
  1625. }
  1626. //===============================================
  1627. // 取得卡號與卡號驗證
  1628. //===============================================
  1629. bool canStartCharging()
  1630. {
  1631. char buf2[16] = "";
  1632. memset(buf2, 0, ARRAY_SIZE(buf2));
  1633. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1634. {
  1635. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1636. }
  1637. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1638. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1639. if(strcmp(buf2, "Accepted") == EQUAL)
  1640. return true;
  1641. else
  1642. {
  1643. //NULL
  1644. }
  1645. return false;
  1646. }
  1647. void AuthorizingStart()
  1648. {
  1649. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1650. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1651. }
  1652. void ClearAuthorizedFlag()
  1653. {
  1654. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1655. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1656. }
  1657. bool isAuthorizedComplete()
  1658. {
  1659. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1660. return false;
  1661. return true;
  1662. }
  1663. bool IsAuthorizingMode()
  1664. {
  1665. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  1666. return false;
  1667. return true;
  1668. }
  1669. //===============================================
  1670. // 紀錄 Alarm Code
  1671. //===============================================
  1672. void RecordAlarmCode(byte gunIndex, char *code)
  1673. {
  1674. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  1675. if (strcmp(code, "012234") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  1676. if (strcmp(code, "012235") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  1677. if (strcmp(code, "012236") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  1678. if (strcmp(code, "012288") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  1679. if (strcmp(code, "012289") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  1680. if (strcmp(code, "012290") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  1681. }
  1682. void RecordWarningCode(byte gunIndex, char *code)
  1683. {
  1684. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, code, 6);
  1685. if (strcmp(code, "012296") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  1686. if (strcmp(code, "012297") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  1687. if (strcmp(code, "012298") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  1688. }
  1689. void ReleaseAlarmCode(byte gunIndex)
  1690. {
  1691. bool isCleanCheck = false;
  1692. char code[7];
  1693. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1694. {
  1695. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL &&
  1696. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1697. {
  1698. memcpy(code, "012234", 6);
  1699. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1700. isCleanCheck = true;
  1701. }
  1702. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL &&
  1703. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES)
  1704. {
  1705. memcpy(code, "012289", 6);
  1706. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1707. isCleanCheck = true;
  1708. }
  1709. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1710. {
  1711. ReleaseWarningCodeByString(gunIndex, "012296");
  1712. }
  1713. }
  1714. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1715. {
  1716. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL &&
  1717. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  1718. {
  1719. memcpy(code, "012236", 6);
  1720. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1721. isCleanCheck = true;
  1722. }
  1723. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL &&
  1724. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail == YES)
  1725. {
  1726. memcpy(code, "012290", 6);
  1727. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1728. isCleanCheck = true;
  1729. }
  1730. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1731. {
  1732. ReleaseWarningCodeByString(gunIndex, "012298");
  1733. }
  1734. }
  1735. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1736. {
  1737. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL &&
  1738. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1739. {
  1740. memcpy(code, "012235", 6);
  1741. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1742. isCleanCheck = true;
  1743. }
  1744. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL &&
  1745. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES)
  1746. {
  1747. memcpy(code, "012288", 6);
  1748. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1749. isCleanCheck = true;
  1750. }
  1751. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1752. {
  1753. ReleaseWarningCodeByString(gunIndex, "012297");
  1754. }
  1755. }
  1756. if (isCleanCheck)
  1757. {
  1758. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1759. {
  1760. if (index == gunIndex)
  1761. continue;
  1762. else
  1763. {
  1764. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, code, 6) != EQUAL)
  1765. {
  1766. if (strncmp(code, "012234", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  1767. if (strncmp(code, "012289", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  1768. if (strncmp(code, "012236", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  1769. if (strncmp(code, "012290", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  1770. if (strncmp(code, "012235", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  1771. if (strncmp(code, "012288", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  1772. }
  1773. }
  1774. }
  1775. }
  1776. }
  1777. void ReleaseWarningCodeByString(byte gunIndex, char *code)
  1778. {
  1779. bool isCleanCheck = false;
  1780. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1781. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1782. {
  1783. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1784. isCleanCheck = true;
  1785. }
  1786. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1787. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1788. {
  1789. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1790. isCleanCheck = true;
  1791. }
  1792. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1793. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1794. {
  1795. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1796. isCleanCheck = true;
  1797. }
  1798. if (isCleanCheck)
  1799. {
  1800. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1801. {
  1802. if (index == gunIndex)
  1803. continue;
  1804. else
  1805. {
  1806. if (strncmp((char *)chargingInfo[index]->ConnectorWarningCode, code, 6) != EQUAL)
  1807. {
  1808. if (strncmp(code, "012296", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  1809. if (strncmp(code, "012297", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  1810. if (strncmp(code, "012298", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  1811. }
  1812. }
  1813. }
  1814. }
  1815. }
  1816. //===============================================
  1817. // EmergencyStop and Charging Stop
  1818. //===============================================
  1819. void ChargingTerminalProcess(byte gunIndex)
  1820. {
  1821. setChargerMode(gunIndex, MODE_TERMINATING);
  1822. }
  1823. void AcChargingTerminalProcess()
  1824. {
  1825. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  1826. }
  1827. void StopChargingProcessByString(byte level)
  1828. {
  1829. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  1830. {
  1831. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  1832. }
  1833. }
  1834. void ReleaseChargingProcessByString(byte level)
  1835. {
  1836. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  1837. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  1838. }
  1839. // 一般錯誤停止充電處理函式
  1840. void BoardErrOccurByString(byte index, char *code)
  1841. {
  1842. byte level = 1;
  1843. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1844. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1845. {
  1846. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  1847. {
  1848. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1849. }
  1850. ChargingTerminalProcess(index);
  1851. }
  1852. StopChargingProcessByString(level);
  1853. }
  1854. // 急停狀況的停止充電處理函式
  1855. void EmcOccureByString(char *code)
  1856. {
  1857. byte level = 2;
  1858. // 嚴重的急停有四種 : EMC 按鈕、Mainbreak、Dooropen、SPD Trip
  1859. // 其錯誤等級為 2
  1860. if (strncmp(code, "012251", 6) == EQUAL || strncmp(code, "012252", 6) == EQUAL ||
  1861. strncmp(code, "012237", 6) == EQUAL || strncmp(code, "012238", 6) == EQUAL)
  1862. {
  1863. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  1864. {
  1865. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1866. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1867. {
  1868. ChargingTerminalProcess(gun);
  1869. }
  1870. StopChargingProcessByString(level);
  1871. }
  1872. }
  1873. }
  1874. void ReleaseBoardErrOccurByString(byte index, char *code)
  1875. {
  1876. bool isTrigger = false;
  1877. byte level = 1;
  1878. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1879. {
  1880. isTrigger = true;
  1881. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1882. }
  1883. if (isTrigger)
  1884. {
  1885. ReleaseChargingProcessByString(level);
  1886. }
  1887. }
  1888. void ReleaseEmsOccureByString(byte index, char *code)
  1889. {
  1890. bool isTrigger = false;
  1891. byte level = 2;
  1892. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1893. {
  1894. isTrigger = true;
  1895. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1896. }
  1897. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  1898. {
  1899. isTrigger = true;
  1900. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  1901. }
  1902. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  1903. {
  1904. isTrigger = true;
  1905. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  1906. }
  1907. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  1908. {
  1909. isTrigger = true;
  1910. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1911. }
  1912. if (isTrigger)
  1913. {
  1914. ReleaseChargingProcessByString(level);
  1915. }
  1916. }
  1917. //===============================================
  1918. // 確認硬體 (按鈕) 狀態
  1919. //===============================================
  1920. bool leftBtnPush = false;
  1921. bool rightBtnPush = false;
  1922. void ChkPrimaryStatus()
  1923. {
  1924. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  1925. {
  1926. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  1927. EmcOccureByString("012251");
  1928. }
  1929. else
  1930. ReleaseEmsOccureByString(0, "012251");
  1931. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  1932. {
  1933. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  1934. EmcOccureByString("012238");
  1935. }
  1936. else
  1937. ReleaseEmsOccureByString(0, "012238");
  1938. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  1939. {
  1940. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  1941. EmcOccureByString("012237");
  1942. }
  1943. else
  1944. ReleaseEmsOccureByString(0, "012237");
  1945. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  1946. {
  1947. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  1948. EmcOccureByString("012252");
  1949. }
  1950. else
  1951. ReleaseEmsOccureByString(0, "012252");
  1952. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  1953. {
  1954. if(!leftBtnPush)
  1955. {
  1956. leftBtnPush = true;
  1957. PRINTF_FUNC("left btn down............................... \n");
  1958. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1959. {
  1960. case S_IDLE:
  1961. {
  1962. if(isDetectPlugin())
  1963. {
  1964. _DetectPlugInTimeout();
  1965. StopSystemTimeoutDet();
  1966. }
  1967. }
  1968. break;
  1969. case S_REASSIGN_CHECK:
  1970. case S_REASSIGN:
  1971. case S_PREPARNING:
  1972. case S_PREPARING_FOR_EV:
  1973. case S_PREPARING_FOR_EVSE:
  1974. case S_CCS_PRECHARGE_ST0:
  1975. case S_CCS_PRECHARGE_ST1:
  1976. {
  1977. // 取消充電
  1978. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1979. }
  1980. break;
  1981. case S_CHARGING:
  1982. {
  1983. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  1984. {
  1985. // 停止充電
  1986. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1987. }
  1988. }
  1989. break;
  1990. case S_COMPLETE:
  1991. {
  1992. // 回 IDLE
  1993. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  1994. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  1995. }
  1996. break;
  1997. }
  1998. }
  1999. }
  2000. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2001. {
  2002. if(leftBtnPush)
  2003. {
  2004. leftBtnPush = false;
  2005. PRINTF_FUNC("left btn up............................... \n");
  2006. }
  2007. }
  2008. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2009. {
  2010. if(!rightBtnPush)
  2011. {
  2012. rightBtnPush = true;
  2013. PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2014. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2015. {
  2016. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2017. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2018. }
  2019. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2020. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  2021. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2022. else
  2023. {
  2024. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2025. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2026. }
  2027. }
  2028. }
  2029. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2030. {
  2031. if(rightBtnPush)
  2032. {
  2033. rightBtnPush = false;
  2034. PRINTF_FUNC("right btn up............................... \n");
  2035. }
  2036. }
  2037. }
  2038. //===============================================
  2039. // 確認各小板偵測的錯誤狀況
  2040. //===============================================
  2041. void CheckErrorOccurStatus(byte index)
  2042. {
  2043. // 小板
  2044. if (chargingInfo[index]->Type == _Type_Chademo)
  2045. {
  2046. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  2047. BoardErrOccurByString(index, "011012");
  2048. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  2049. BoardErrOccurByString(index, "012234");
  2050. }
  2051. else if (chargingInfo[index]->Type == _Type_GB)
  2052. {
  2053. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES)
  2054. BoardErrOccurByString(index, "011016");
  2055. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  2056. BoardErrOccurByString(index, "012236");
  2057. }
  2058. else if (chargingInfo[index]->Type == _Type_CCS_2)
  2059. {
  2060. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  2061. BoardErrOccurByString(index, "011014");
  2062. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  2063. BoardErrOccurByString(index, "012235");
  2064. }
  2065. // RB
  2066. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2067. {
  2068. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2069. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2070. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2071. {
  2072. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2073. {
  2074. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2075. StopChargingProcessByString(2);
  2076. }
  2077. }
  2078. else
  2079. {
  2080. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2081. {
  2082. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2083. ReleaseChargingProcessByString(2);
  2084. }
  2085. }
  2086. }
  2087. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2088. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2089. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2090. {
  2091. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2092. {
  2093. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2094. StopChargingProcessByString(2);
  2095. }
  2096. }
  2097. else
  2098. {
  2099. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2100. {
  2101. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2102. ReleaseChargingProcessByString(2);
  2103. }
  2104. }
  2105. }
  2106. //===============================================
  2107. // 確認 GPIO 狀態
  2108. //===============================================
  2109. void gpio_set_value(unsigned int gpio, unsigned int value)
  2110. {
  2111. int fd;
  2112. char buf[MAX_BUF];
  2113. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2114. fd = open(buf, O_WRONLY);
  2115. if (fd < 0)
  2116. {
  2117. perror("gpio/set-value");
  2118. return;
  2119. }
  2120. if (value)
  2121. write(fd, "1", 2);
  2122. else
  2123. write(fd, "0", 2);
  2124. close(fd);
  2125. }
  2126. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2127. {
  2128. int fd;
  2129. char buf[MAX_BUF];
  2130. char ch;
  2131. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2132. fd = open(buf, O_RDONLY);
  2133. if (fd < 0){
  2134. perror("gpio/get-value");
  2135. return fd;
  2136. }
  2137. read(fd, &ch, 1);
  2138. if (ch != '0') {
  2139. *value = 1;
  2140. } else {
  2141. *value = 0;
  2142. }
  2143. close(fd);
  2144. return 0;
  2145. }
  2146. void CheckGunTypeFromHw()
  2147. {
  2148. int pinIn[4] = { 22, 23, 44, 45 };
  2149. unsigned int gpioValue = 0;
  2150. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2151. {
  2152. gpio_get_value(pinIn[i], &gpioValue);
  2153. switch (pinIn[i])
  2154. {
  2155. case 22:
  2156. bd1_1_status = gpioValue;
  2157. break;
  2158. case 23:
  2159. bd1_2_status = gpioValue;
  2160. break;
  2161. case 44:
  2162. bd0_1_status = gpioValue;
  2163. break;
  2164. case 45:
  2165. bd0_2_status = gpioValue;
  2166. break;
  2167. }
  2168. }
  2169. }
  2170. void CheckGpioInStatus()
  2171. {
  2172. int pinIn[2] = { 27, 47 };
  2173. unsigned int gpioValue = 0;
  2174. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2175. {
  2176. gpio_get_value(pinIn[i], &gpioValue);
  2177. if (gpioValue == 0x01)
  2178. {
  2179. switch(pinIn[i])
  2180. {
  2181. // 小板緊急停止
  2182. case 47:
  2183. {
  2184. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2185. {
  2186. if (chargingInfo[i]->slotsIndex == 1)
  2187. {
  2188. if (chargingInfo[i]->Type == _Type_Chademo)
  2189. BoardErrOccurByString(i, "023730");
  2190. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2191. BoardErrOccurByString(i, "013627");
  2192. break;
  2193. }
  2194. }
  2195. }
  2196. break;
  2197. case 27:
  2198. {
  2199. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2200. {
  2201. if (chargingInfo[i]->slotsIndex == 3)
  2202. {
  2203. if (chargingInfo[i]->Type == _Type_Chademo)
  2204. BoardErrOccurByString(i, "023730");
  2205. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2206. BoardErrOccurByString(i, "013627");
  2207. break;
  2208. }
  2209. }
  2210. }
  2211. break;
  2212. }
  2213. }
  2214. else
  2215. {
  2216. switch (pinIn[i])
  2217. {
  2218. // 小板解除緊急停止
  2219. case 47:
  2220. {
  2221. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2222. {
  2223. if (chargingInfo[i]->slotsIndex == 1)
  2224. {
  2225. if (chargingInfo[i]->Type == _Type_Chademo)
  2226. ReleaseBoardErrOccurByString(i, "023730");
  2227. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2228. ReleaseBoardErrOccurByString(i, "013627");
  2229. break;
  2230. }
  2231. }
  2232. }
  2233. break;
  2234. case 27:
  2235. {
  2236. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2237. {
  2238. if (chargingInfo[i]->slotsIndex == 3)
  2239. {
  2240. if (chargingInfo[i]->Type == _Type_Chademo)
  2241. ReleaseBoardErrOccurByString(i, "023730");
  2242. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2243. ReleaseBoardErrOccurByString(i, "013627");
  2244. break;
  2245. }
  2246. }
  2247. }
  2248. break;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. //===============================================
  2254. // Main process
  2255. //===============================================
  2256. // 檢查 Byte 中某個 Bit 的值
  2257. // _byte : 欲改變的 byte
  2258. // _bit : 該 byte 的第幾個 bit
  2259. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2260. {
  2261. return ( _byte & mask_table[_bit] ) != 0x00;
  2262. }
  2263. // 設定 Byte 中某個 Bit的值
  2264. // _byte : 欲改變的 byte
  2265. // _bit : 該 byte 的第幾個 bit
  2266. // value : 修改的值為 0 or 1
  2267. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2268. {
  2269. if(value == 1)
  2270. *_byte |= (1 << _bit);
  2271. else if (value == 0)
  2272. *_byte ^= (1 << _bit);
  2273. }
  2274. void UserScanFunction()
  2275. {
  2276. bool idleReq = false;
  2277. unsigned char stopReq = 255;
  2278. // 當前非驗證的狀態
  2279. if(!IsAuthorizingMode())
  2280. {
  2281. // 先判斷現在是否可以提供刷卡
  2282. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2283. // 2. 停止充電
  2284. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX)
  2285. {
  2286. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2287. return;
  2288. }
  2289. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2290. {
  2291. if (chargingInfo[i]->SystemStatus == S_CHARGING)
  2292. {
  2293. stopReq = i;
  2294. }
  2295. if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable) == YES ||
  2296. (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable))
  2297. {
  2298. idleReq = true;
  2299. }
  2300. }
  2301. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2302. ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  2303. {
  2304. stopReq = DEFAULT_AC_INDEX;
  2305. }
  2306. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  2307. {
  2308. if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2309. {
  2310. char value[32];
  2311. PRINTF_FUNC("ac stop charging \n");
  2312. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2313. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2314. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2315. ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2316. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2317. {
  2318. AcChargingTerminalProcess();
  2319. }
  2320. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2321. }
  2322. else if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2323. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2324. (_acgunIndex <= 0 || (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)))
  2325. {
  2326. char value[32];
  2327. PRINTF_FUNC("stop charging \n");
  2328. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2329. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2330. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2331. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2332. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2333. {
  2334. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2335. }
  2336. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2337. }
  2338. else if (idleReq)
  2339. {
  2340. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  2341. stopReq != 255 &&
  2342. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2343. {
  2344. idleReq = false;
  2345. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2346. }
  2347. else if ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  2348. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE)
  2349. {
  2350. PRINTF_FUNC("// LCM => Authorizing \n");
  2351. // LCM => Authorizing
  2352. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2353. // 進入確認卡號狀態
  2354. AuthorizingStart();
  2355. }
  2356. else
  2357. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2358. }
  2359. else
  2360. {
  2361. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2362. }
  2363. }
  2364. }
  2365. }
  2366. unsigned char isModeChange(unsigned char gun_index)
  2367. {
  2368. unsigned char result = NO;
  2369. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  2370. {
  2371. result = YES;
  2372. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2373. }
  2374. return result;
  2375. }
  2376. void ScannerCardProcess()
  2377. {
  2378. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2379. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2380. {
  2381. isCardScan = true;
  2382. // 處理刷卡及驗證卡號的動作
  2383. UserScanFunction();
  2384. }
  2385. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  2386. {
  2387. StartSystemTimeoutDet(Timeout_Authorizing);
  2388. // 確認驗證卡號完成沒
  2389. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2390. {
  2391. StopSystemTimeoutDet();
  2392. // 判斷後台回覆狀態
  2393. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2394. {
  2395. // LCM => Authorize complete
  2396. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2397. }
  2398. else
  2399. {
  2400. // LCM => Authorize fail
  2401. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2402. }
  2403. ClearAuthorizedFlag();
  2404. }
  2405. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2406. {
  2407. // 白名單驗證
  2408. for (int i = 0; i < 10; i++)
  2409. {
  2410. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2411. {
  2412. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2413. {
  2414. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2415. ClearAuthorizedFlag();
  2416. break;
  2417. }
  2418. }
  2419. }
  2420. }
  2421. }
  2422. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2423. {
  2424. StartSystemTimeoutDet(Timeout_VerifyFail);
  2425. isCardScan = false;
  2426. }
  2427. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2428. {
  2429. StartSystemTimeoutDet(Timeout_VerifyComp);
  2430. }
  2431. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2432. {
  2433. StartSystemTimeoutDet(Timeout_WaitPlug);
  2434. }
  2435. else
  2436. isCardScan = false;
  2437. }
  2438. bool AddGunInfoByConnector(byte typeValue, byte slots)
  2439. {
  2440. bool result = true;
  2441. switch (typeValue)
  2442. {
  2443. case '0': // none
  2444. break;
  2445. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  2446. break;
  2447. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  2448. break;
  2449. case '3': // IEC 62196-2 Type 2 Plug
  2450. case '4': // IEC 62196-2 Type 2 Socket
  2451. if (AC_QUANTITY > _ac_Index)
  2452. {
  2453. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  2454. // AC 固定 index
  2455. ac_chargingInfo[_acgunIndex]->Index = 1;
  2456. ac_chargingInfo[_acgunIndex]->SystemStatus = S_BOOTING;
  2457. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  2458. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  2459. _ac_Index++;
  2460. _acgunIndex++;
  2461. }
  2462. else
  2463. result = false;
  2464. break;
  2465. case '5': // GB/T AC Plug
  2466. break;
  2467. case '6': // GB/T AC Socket
  2468. break;
  2469. case 'J': // CHAdeMO
  2470. {
  2471. if (CHAdeMO_QUANTITY > _chademoIndex)
  2472. {
  2473. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  2474. chargingInfo[_gunIndex]->Index = _gunIndex;
  2475. chargingInfo[_gunIndex]->slotsIndex = slots;
  2476. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2477. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  2478. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  2479. chargingInfo[_gunIndex]->IsAvailable = YES;
  2480. _chademoIndex++;
  2481. _gunIndex++;
  2482. }
  2483. else
  2484. result = false;
  2485. }
  2486. break;
  2487. case 'U': // CCS1 combo
  2488. case 'E': // CCS2 combo
  2489. {
  2490. if (CCS_QUANTITY > _ccsIndex)
  2491. {
  2492. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  2493. chargingInfo[_gunIndex]->Index = _gunIndex;
  2494. chargingInfo[_gunIndex]->slotsIndex = slots;
  2495. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2496. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  2497. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  2498. chargingInfo[_gunIndex]->IsAvailable = YES;
  2499. // 現階段預設為走 DIN70121
  2500. ShmCcsData->CommProtocol = 0x01;
  2501. _ccsIndex++;
  2502. _gunIndex++;
  2503. }
  2504. else
  2505. result = false;
  2506. }
  2507. break;
  2508. case 'G': // GBT DC
  2509. {
  2510. if (GB_QUANTITY > _gb_Index)
  2511. {
  2512. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  2513. chargingInfo[_gunIndex]->Index = _gunIndex;
  2514. chargingInfo[_gunIndex]->slotsIndex = slots;
  2515. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2516. chargingInfo[_gunIndex]->Type = _Type_GB;
  2517. chargingInfo[_gunIndex]->type_index = _gb_Index;
  2518. chargingInfo[_gunIndex]->IsAvailable = YES;
  2519. _gb_Index++;
  2520. _gunIndex++;
  2521. }
  2522. else
  2523. result = false;
  2524. }
  2525. break;
  2526. case 'D': // GBT DC x 2
  2527. break;
  2528. }
  2529. return result;
  2530. }
  2531. bool CheckConnectorTypeStatus()
  2532. {
  2533. bool result = true;
  2534. PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  2535. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  2536. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  2537. {
  2538. byte slots = 1;
  2539. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  2540. {
  2541. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  2542. {
  2543. return false;
  2544. }
  2545. slots++;
  2546. }
  2547. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  2548. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  2549. PRINTF_FUNC("DC _gunCount = %d, AC _gunCount = %d \n",
  2550. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  2551. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  2552. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  2553. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  2554. result = false;
  2555. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2556. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2557. {
  2558. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  2559. {
  2560. // 與硬體相同 type : Chademo
  2561. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2562. {
  2563. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2564. }
  2565. }
  2566. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  2567. {
  2568. // 與硬體相同 type : CCS
  2569. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2570. {
  2571. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2572. }
  2573. }
  2574. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  2575. {
  2576. // 與硬體相同 type : GB
  2577. if (chargingInfo[gunIndex]->Type == _Type_GB)
  2578. {
  2579. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2580. }
  2581. }
  2582. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  2583. {
  2584. // 與硬體相同 type : Chademo
  2585. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2586. {
  2587. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2588. }
  2589. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2590. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2591. }
  2592. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  2593. {
  2594. // 與硬體相同 type : CCS
  2595. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2596. {
  2597. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2598. }
  2599. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2600. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2601. }
  2602. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  2603. {
  2604. // 與硬體相同 type : GB
  2605. if (chargingInfo[gunIndex]->Type == _Type_GB)
  2606. {
  2607. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2608. }
  2609. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2610. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2611. }
  2612. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  2613. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  2614. result = false;
  2615. }
  2616. }
  2617. else
  2618. {
  2619. // Module Name 不正確 - 告警
  2620. result = false;
  2621. }
  2622. return result;
  2623. }
  2624. void KillTask()
  2625. {
  2626. ChangeLcmByIndex(_LCM_FIX);
  2627. system("killall Module_EventLogging");
  2628. system("killall Module_PrimaryComm");
  2629. system("killall Module_EvComm");
  2630. system("killall Module_LcmControl");
  2631. system("killall Module_InternalComm");
  2632. system("killall Module_PsuComm");
  2633. system("killall OcppBackend &");
  2634. system("killall Module_4g &");
  2635. system("killall Module_Wifi &");
  2636. }
  2637. char CheckUpdateProcess()
  2638. {
  2639. DIR *d;
  2640. struct dirent *dir;
  2641. d = opendir("/mnt/");
  2642. if (d)
  2643. {
  2644. long int MaxLen=48*1024*1024, ImageLen = 0;
  2645. while ((dir = readdir(d)) != NULL)
  2646. {
  2647. char *new_str;
  2648. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  2649. new_str[0] = '\0';
  2650. strcat(new_str, "/mnt/");
  2651. strcat(new_str, dir->d_name);
  2652. int fd = open(new_str, O_RDONLY);
  2653. if (fd < 0)
  2654. {
  2655. return FAIL;
  2656. }
  2657. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2658. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2659. //get the image length
  2660. ImageLen = read(fd, ptr, MaxLen);
  2661. if (ImageLen > 20)
  2662. {
  2663. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  2664. PRINTF_FUNC("Typed...%x \r\n", Type);
  2665. switch (Type)
  2666. {
  2667. case CSU_BOOTLOADER:
  2668. case CSU_KERNEL_CONFIGURATION:
  2669. case CSU_KERNEL_IMAGE:
  2670. case CSU_ROOT_FILE_SYSTEM:
  2671. case CSU_USER_CONFIGURATION:
  2672. {
  2673. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2674. return PASS;
  2675. else
  2676. return FAIL;
  2677. }
  2678. break;
  2679. case CCS_BOARD_BOOTLOADER:
  2680. case CCS_BOARD_KERNEL_CONFIGURATION:
  2681. case CCS_BOARD_KERNEL_IMAGE:
  2682. case CCS_BOARD_FILE_SYSTEM:
  2683. {
  2684. bool isPass = true;
  2685. int CanFd = InitCanBus();
  2686. if (CanFd > 0)
  2687. {
  2688. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2689. {
  2690. if (!isPass)
  2691. break;
  2692. if (chargingInfo[index]->Type == _Type_CCS_2)
  2693. {
  2694. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  2695. {
  2696. isPass = false;
  2697. }
  2698. }
  2699. }
  2700. }
  2701. else
  2702. {
  2703. printf("Upgrade CCS open CAN FD fail.\n");
  2704. isPass = false;
  2705. }
  2706. return isPass;
  2707. }
  2708. break;
  2709. case CSU_PRIMARY_CONTROLLER:
  2710. case RELAY_CONTROL_BOARD:
  2711. case FAN_CONTROL_BOARD:
  2712. case AC_WALLMOUNT_CONTROLLER:
  2713. {
  2714. byte target = 0x00;
  2715. if (Type == CSU_PRIMARY_CONTROLLER)
  2716. target = UPGRADE_PRI;
  2717. else if (Type == RELAY_CONTROL_BOARD)
  2718. target = UPGRADE_RB;
  2719. else if (Type == FAN_CONTROL_BOARD)
  2720. target = UPGRADE_FAN;
  2721. else if (Type == AC_WALLMOUNT_CONTROLLER)
  2722. target = UPGRADE_AC;
  2723. int fd = InitComPort(target);
  2724. if (Upgrade_UART(fd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2725. return PASS;
  2726. else
  2727. return FAIL;
  2728. close(fd);
  2729. }
  2730. break;
  2731. case CHAdeMO_BOARD:
  2732. case GB_BOARD:
  2733. {
  2734. bool isPass = true;
  2735. int CanFd = InitCanBus();
  2736. if (CanFd > 0)
  2737. {
  2738. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2739. {
  2740. if (!isPass)
  2741. break;
  2742. if ((Type == CHAdeMO_BOARD && chargingInfo[index]->Type == _Type_Chademo) ||
  2743. (Type == GB_BOARD && chargingInfo[index]->Type == _Type_GB))
  2744. {
  2745. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2746. return PASS;
  2747. else
  2748. return FAIL;
  2749. }
  2750. }
  2751. }
  2752. else
  2753. {
  2754. PRINTF_FUNC("Upgrad FD fail. \n");
  2755. isPass = false;
  2756. }
  2757. return isPass;
  2758. break;
  2759. }
  2760. }
  2761. }
  2762. free(new_str);
  2763. free(ptr);
  2764. }
  2765. }
  2766. free(dir);
  2767. closedir(d);
  2768. return FAIL;
  2769. }
  2770. void CreateRfidFork()
  2771. {
  2772. pid_t rfidRecPid;
  2773. rfidRecPid = fork();
  2774. if (rfidRecPid == 0)
  2775. {
  2776. while(true)
  2777. {
  2778. // 刷卡判斷
  2779. RFID rfid;
  2780. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  2781. !ShmSysConfigAndInfo->SysConfig.isRFID)
  2782. {}
  2783. else if(getRequestCardSN(rfidFd, 0, &rfid))
  2784. {
  2785. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2786. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  2787. {
  2788. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  2789. {
  2790. switch (rfid.snType)
  2791. {
  2792. case RFID_SN_TYPE_6BYTE:
  2793. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2794. "%02X%02X%02X%02X%02X%02X",
  2795. rfid.currentCard[0], rfid.currentCard[1],
  2796. rfid.currentCard[2], rfid.currentCard[3],
  2797. rfid.currentCard[4], rfid.currentCard[5]);
  2798. break;
  2799. case RFID_SN_TYPE_7BYTE:
  2800. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2801. "%02X%02X%02X%02X%02X%02X%02X",
  2802. rfid.currentCard[0], rfid.currentCard[1],
  2803. rfid.currentCard[2], rfid.currentCard[3],
  2804. rfid.currentCard[4], rfid.currentCard[5],
  2805. rfid.currentCard[6]);
  2806. break;
  2807. case RFID_SN_TYPE_10BYTE:
  2808. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2809. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2810. rfid.currentCard[0], rfid.currentCard[1],
  2811. rfid.currentCard[2], rfid.currentCard[3],
  2812. rfid.currentCard[4], rfid.currentCard[5],
  2813. rfid.currentCard[6], rfid.currentCard[7],
  2814. rfid.currentCard[8], rfid.currentCard[9]);
  2815. break;
  2816. case RFID_SN_TYPE_4BYTE:
  2817. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2818. "%02X%02X%02X%02X",
  2819. rfid.currentCard[0], rfid.currentCard[1],
  2820. rfid.currentCard[2], rfid.currentCard[3]);
  2821. break;
  2822. }
  2823. }
  2824. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  2825. {
  2826. switch (rfid.snType)
  2827. {
  2828. case RFID_SN_TYPE_6BYTE:
  2829. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2830. "%02X%02X%02X%02X%02X%02X",
  2831. rfid.currentCard[5], rfid.currentCard[4],
  2832. rfid.currentCard[3], rfid.currentCard[2],
  2833. rfid.currentCard[1], rfid.currentCard[0]);
  2834. break;
  2835. case RFID_SN_TYPE_7BYTE:
  2836. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2837. "%02X%02X%02X%02X%02X%02X%02X",
  2838. rfid.currentCard[6], rfid.currentCard[5],
  2839. rfid.currentCard[4], rfid.currentCard[3],
  2840. rfid.currentCard[2], rfid.currentCard[1],
  2841. rfid.currentCard[0]);
  2842. break;
  2843. case RFID_SN_TYPE_10BYTE:
  2844. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2845. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2846. rfid.currentCard[9], rfid.currentCard[8],
  2847. rfid.currentCard[7], rfid.currentCard[6],
  2848. rfid.currentCard[5], rfid.currentCard[4],
  2849. rfid.currentCard[3], rfid.currentCard[2],
  2850. rfid.currentCard[1], rfid.currentCard[0]);
  2851. break;
  2852. case RFID_SN_TYPE_4BYTE:
  2853. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2854. "%02X%02X%02X%02X",
  2855. rfid.currentCard[3], rfid.currentCard[2],
  2856. rfid.currentCard[1], rfid.currentCard[0]);
  2857. break;
  2858. }
  2859. }
  2860. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2861. }
  2862. }
  2863. sleep(1);
  2864. }
  2865. }
  2866. }
  2867. void StartSystemTimeoutDet(unsigned char flag)
  2868. {
  2869. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  2870. {
  2871. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2872. }
  2873. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  2874. }
  2875. void StopSystemTimeoutDet()
  2876. {
  2877. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2878. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  2879. }
  2880. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  2881. {
  2882. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2883. {
  2884. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  2885. {
  2886. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  2887. }
  2888. chargingInfo[gunIndex]->TimeoutFlag = flag;
  2889. }
  2890. }
  2891. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  2892. {
  2893. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2894. {
  2895. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  2896. }
  2897. }
  2898. void CreateTimeoutFork()
  2899. {
  2900. pid_t timeoutPid;
  2901. timeoutPid = fork();
  2902. if (timeoutPid > 0)
  2903. {
  2904. while(true)
  2905. {
  2906. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  2907. // 系統
  2908. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  2909. {
  2910. case Timeout_SelftestChk:
  2911. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2912. {
  2913. _SelfTestTimeout();
  2914. StopSystemTimeoutDet();
  2915. }
  2916. break;
  2917. case Timeout_Authorizing:
  2918. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 30000000)
  2919. {
  2920. _AuthorizedTimeout();
  2921. StopSystemTimeoutDet();
  2922. }
  2923. break;
  2924. case Timeout_VerifyFail:
  2925. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2926. {
  2927. _AutoReturnTimeout();
  2928. StopSystemTimeoutDet();
  2929. }
  2930. break;
  2931. case Timeout_VerifyComp:
  2932. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2933. {
  2934. _AutoReturnTimeout();
  2935. StopSystemTimeoutDet();
  2936. }
  2937. break;
  2938. case Timeout_WaitPlug:
  2939. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2940. {
  2941. _DetectPlugInTimeout();
  2942. StopSystemTimeoutDet();
  2943. }
  2944. break;
  2945. case Timeout_ReturnToChargingGunDet:
  2946. {
  2947. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 30000000)
  2948. {
  2949. DisplayChargingInfo();
  2950. StopSystemTimeoutDet();
  2951. }
  2952. }
  2953. break;
  2954. }
  2955. // 各槍
  2956. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2957. {
  2958. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  2959. switch(chargingInfo[gun_index]->TimeoutFlag)
  2960. {
  2961. case Timeout_Preparing:
  2962. {
  2963. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 30000000)
  2964. {
  2965. _PrepareTimeout(gun_index);
  2966. StopGunInfoTimeoutDet(gun_index);
  2967. }
  2968. }
  2969. break;
  2970. case Timeout_EvChargingDet:
  2971. {
  2972. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
  2973. {
  2974. _DetectEvChargingEnableTimeout(gun_index);
  2975. StopGunInfoTimeoutDet(gun_index);
  2976. }
  2977. }
  2978. break;
  2979. case Timeout_EvseChargingDet:
  2980. {
  2981. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2982. {
  2983. _DetectEvseChargingEnableTimeout(gun_index);
  2984. StopGunInfoTimeoutDet(gun_index);
  2985. }
  2986. }
  2987. break;
  2988. case Timeout_EvseCompleteDet:
  2989. {
  2990. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  2991. {
  2992. StopGunInfoTimeoutDet(gun_index);
  2993. }
  2994. }
  2995. break;
  2996. case Timeout_ForCcsPrechargeDet:
  2997. {
  2998. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2999. {
  3000. _CcsPrechargeTimeout(gun_index);
  3001. StopGunInfoTimeoutDet(gun_index);
  3002. }
  3003. }
  3004. break;
  3005. }
  3006. }
  3007. sleep(1);
  3008. }
  3009. }
  3010. }
  3011. void GetSystemTime()
  3012. {
  3013. struct timeb csuTime;
  3014. struct tm *tmCSU;
  3015. ftime(&csuTime);
  3016. tmCSU = localtime(&csuTime.time);
  3017. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3018. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3019. tmCSU->tm_sec);
  3020. // byte date[14];
  3021. //
  3022. //
  3023. // //sprintf(&date, "%d", );
  3024. //
  3025. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  3026. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  3027. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  3028. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  3029. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  3030. //
  3031. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  3032. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  3033. //
  3034. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  3035. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  3036. //
  3037. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  3038. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  3039. //
  3040. // date[10] = (tmCSU->tm_min) / 10 % 10;
  3041. // date[11] = (tmCSU->tm_min) / 1 % 10;
  3042. //
  3043. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  3044. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  3045. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  3046. // date[4], date[5], date[6], date[7],
  3047. // date[8], date[9], date[10], date[11],
  3048. // date[12], date[13]);
  3049. }
  3050. void CheckFactoryConfigFunction()
  3051. {
  3052. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3053. {
  3054. system("cd /root;./FactoryConfig -m");
  3055. system("sync");
  3056. sleep(5);
  3057. system("reboot -f");
  3058. sleep(5);
  3059. system("reboot -f");
  3060. }
  3061. }
  3062. void CheckFwUpdateFunction()
  3063. {
  3064. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  3065. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  3066. {
  3067. DEBUG_INFO_MSG("ftp : update start. \n");
  3068. KillTask();
  3069. if (CheckUpdateProcess() == PASS)
  3070. DEBUG_INFO_MSG("ftp : update complete. \n");
  3071. else
  3072. DEBUG_INFO_MSG("ftp : update fail. \n");
  3073. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3074. sleep(5);
  3075. system("reboot -f");
  3076. }
  3077. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  3078. {
  3079. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  3080. }
  3081. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  3082. {
  3083. DEBUG_INFO_MSG("Backend : update start. \n");
  3084. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3085. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3086. KillTask();
  3087. if (CheckUpdateProcess() == PASS)
  3088. {
  3089. DEBUG_INFO_MSG("Backend : update complete. \n");
  3090. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3091. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3092. }
  3093. else
  3094. {
  3095. DEBUG_INFO_MSG("Backend : update fail. \n");
  3096. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3097. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3098. }
  3099. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3100. sleep(5);
  3101. system("reboot -f");
  3102. }
  3103. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == EQUAL)
  3104. {
  3105. DEBUG_ERROR_MSG("Backend to download file fail. \n");
  3106. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3107. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3108. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3109. }
  3110. }
  3111. //===============================================
  3112. // Check reservation date is expired
  3113. //===============================================
  3114. int isReservationExpired(unsigned char gun_index)
  3115. {
  3116. int result = NO;
  3117. struct tm expiredDate;
  3118. struct timeb expiredTime;
  3119. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  3120. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  3121. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  3122. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  3123. {
  3124. expiredDate.tm_year -= 1900;
  3125. expiredDate.tm_mon -= 1;
  3126. expiredTime.time = mktime(&expiredDate);
  3127. if (!CheckTimeOut(expiredTime))
  3128. {
  3129. result = YES;
  3130. }
  3131. }
  3132. return result;
  3133. }
  3134. //===============================================
  3135. // OCPP
  3136. //===============================================
  3137. void CheckOcppStatus()
  3138. {
  3139. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  3140. {
  3141. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = YES;
  3142. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  3143. }
  3144. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  3145. {
  3146. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  3147. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  3148. {
  3149. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  3150. sleep(3);
  3151. system("reboot -f");
  3152. }
  3153. else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  3154. {
  3155. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  3156. sleep(3);
  3157. KillTask();
  3158. system("/usr/bin/run_evse_restart.sh");
  3159. }
  3160. }
  3161. }
  3162. void OcppStartTransation(byte gunIndex)
  3163. {
  3164. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3165. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  3166. else
  3167. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3168. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  3169. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionReq = YES;
  3170. }
  3171. void OcppStopTransation(byte gunIndex)
  3172. {
  3173. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3174. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  3175. else
  3176. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3177. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  3178. ShmOCPP16Data->CpMsg.bits[gunIndex].StopTransactionReq = YES;
  3179. }
  3180. bool OcppRemoteStop(byte gunIndex)
  3181. {
  3182. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  3183. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  3184. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  3185. return result;
  3186. }
  3187. void OcppRemoteStartChk()
  3188. {
  3189. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3190. {}
  3191. else if(!isDetectPlugin())
  3192. {
  3193. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3194. {
  3195. if ((chargingInfo[gun_index]->SystemStatus == S_IDLE || chargingInfo[gun_index]->SystemStatus == S_RESERVATION)&&
  3196. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES)
  3197. {
  3198. ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  3199. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = NO;
  3200. DetectPluginStart();
  3201. break;
  3202. }
  3203. }
  3204. }
  3205. }
  3206. void ChkOcppStatus(byte gunIndex)
  3207. {
  3208. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  3209. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  3210. {
  3211. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  3212. if (isReservationExpired(gunIndex))
  3213. {
  3214. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3215. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3216. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  3217. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  3218. }
  3219. }
  3220. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  3221. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  3222. {
  3223. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  3224. if (isReservationExpired(gunIndex))
  3225. {
  3226. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3227. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3228. chargingInfo[gunIndex]->ReservationId = 0;
  3229. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  3230. }
  3231. }
  3232. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  3233. {
  3234. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3235. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3236. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  3237. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  3238. chargingInfo[gunIndex]->IsAvailable = YES;
  3239. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  3240. chargingInfo[gunIndex]->IsAvailable = NO;
  3241. }
  3242. }
  3243. bool CheckBackendChargingTimeout(byte gunIndex)
  3244. {
  3245. bool result = false;
  3246. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3247. {
  3248. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  3249. {
  3250. if (chargingInfo[gunIndex]->RemainChargingDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  3251. result = true;
  3252. }
  3253. }
  3254. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3255. {
  3256. // 隨插即充電的要看 offline
  3257. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  3258. {
  3259. if (chargingInfo[gunIndex]->RemainChargingDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  3260. result = true;
  3261. }
  3262. }
  3263. return result;
  3264. }
  3265. bool CheckBackendChargingEnergy(byte gunIndex)
  3266. {
  3267. bool result = false;
  3268. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3269. {
  3270. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  3271. {
  3272. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  3273. result = true;
  3274. }
  3275. }
  3276. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3277. {
  3278. // 隨插即充電的要看 offline
  3279. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  3280. {
  3281. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  3282. result = true;
  3283. }
  3284. }
  3285. return result;
  3286. }
  3287. //===============================================
  3288. // Config process
  3289. //===============================================
  3290. void AddPlugInTimes(byte gunIndex)
  3291. {
  3292. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3293. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  3294. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3295. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  3296. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  3297. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  3298. }
  3299. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  3300. {
  3301. char cmdBuf[32];
  3302. struct timeb csuTime;
  3303. struct tm *tmCSU;
  3304. ftime(&csuTime);
  3305. tmCSU = localtime(&csuTime.time);
  3306. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  3307. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3308. tmCSU->tm_sec);
  3309. if (isStart)
  3310. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  3311. else
  3312. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  3313. }
  3314. void zipLogFiles()
  3315. {
  3316. const char* logPath = "/Storage/SystemLog";
  3317. // 獲取目錄
  3318. DIR* pDir = opendir(logPath);
  3319. if (pDir != NULL)
  3320. {
  3321. struct timeb csuTime;
  3322. struct tm *tmCSU;
  3323. ftime(&csuTime);
  3324. tmCSU = localtime(&csuTime.time);
  3325. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3326. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3327. // tmCSU->tm_sec);
  3328. // Read items inside the folder
  3329. struct dirent* pEntry = NULL;
  3330. while ((pEntry = readdir(pDir)) != NULL)
  3331. {
  3332. if (strcmp(pEntry->d_name, ".") != 0 &&
  3333. strcmp(pEntry->d_name, "..") != 0 &&
  3334. strncmp(pEntry->d_name, "[", 1) == 0 &&
  3335. strstr(pEntry->d_name, "tar") < 0)
  3336. {
  3337. char yearC[5];
  3338. unsigned short year = 0;
  3339. char monthC[3];
  3340. unsigned short month = 0;
  3341. yearC[4] = '\0';
  3342. strncpy(yearC, pEntry->d_name + 1, 4);
  3343. monthC[2] = '\0';
  3344. strncpy(monthC, pEntry->d_name + 6, 2);
  3345. year = atoi(yearC);
  3346. month = atoi(monthC);
  3347. if (year != 0)
  3348. {
  3349. if (year < tmCSU->tm_year + 1900 ||
  3350. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  3351. {
  3352. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  3353. char file[256];
  3354. memset(file, 0x00, sizeof(file));
  3355. strcat(file, "tar zcvf ");
  3356. strcat(file, logPath);
  3357. strncat(file, "/", 1);
  3358. strcat(file, pEntry->d_name);
  3359. strcat(file, ".tar");
  3360. strncat(file, " ", 1);
  3361. strcat(file, logPath);
  3362. strncat(file, "/", 1);
  3363. strcat(file, pEntry->d_name);
  3364. PRINTF_FUNC("zip = %s \n", file);
  3365. system(file);
  3366. }
  3367. }
  3368. }
  3369. }
  3370. }
  3371. // Close folder
  3372. closedir(pDir);
  3373. }
  3374. void ChangeGunSelectByIndex(byte sel)
  3375. {
  3376. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  3377. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  3378. }
  3379. void CheckIsAlternatvieByModelName()
  3380. {
  3381. // 黑白機 ?
  3382. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UE1PH") == EQUAL ||
  3383. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UW1PH") == EQUAL ||
  3384. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  3385. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0EE1PH") == EQUAL ||
  3386. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0EW1PH") == EQUAL ||
  3387. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL ||
  3388. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UE1PH") == EQUAL ||
  3389. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UW1PH") == EQUAL ||
  3390. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UT1PH") == EQUAL)
  3391. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  3392. else
  3393. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  3394. }
  3395. void StopProcessingLoop()
  3396. {
  3397. for (;;)
  3398. {
  3399. CheckFactoryConfigFunction();
  3400. CheckFwUpdateFunction();
  3401. sleep(1);
  3402. }
  3403. }
  3404. void CreateWatchdog()
  3405. {
  3406. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  3407. {
  3408. wtdFd = InitWatchDog();
  3409. if (wtdFd < 0)
  3410. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  3411. }
  3412. }
  3413. bool IsConnectorWholeIdle()
  3414. {
  3415. bool result = true;
  3416. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  3417. {
  3418. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  3419. chargingInfo[count]->SystemStatus != S_RESERVATION)
  3420. {
  3421. result = false;
  3422. break;
  3423. }
  3424. }
  3425. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  3426. {
  3427. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  3428. ac_chargingInfo[count]->IsErrorOccur == NO)
  3429. {
  3430. result = false;
  3431. break;
  3432. }
  3433. }
  3434. return result;
  3435. }
  3436. void ClearAlarmCodeWhenAcOff()
  3437. {
  3438. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  3439. {
  3440. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  3441. }
  3442. }
  3443. int main(void)
  3444. {
  3445. if(CreateShareMemory() == 0)
  3446. {
  3447. #ifdef SystemLogMessage
  3448. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  3449. #endif
  3450. if(ShmStatusCodeData!=NULL)
  3451. {
  3452. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  3453. }
  3454. return 0;
  3455. sleep(5);
  3456. system("reboot -f");
  3457. sleep(5);
  3458. system("reboot -f");
  3459. }
  3460. PRINTF_FUNC("Chademo = %d, CCS = %d, GB = %d, AC = %d \n",
  3461. CHAdeMO_QUANTITY, CCS_QUANTITY, GB_QUANTITY, AC_QUANTITY);
  3462. PRINTF_FUNC("\n"); PRINTF_FUNC("Initial SystemConfig and Info.......\n");
  3463. if (!InitialSystemDefaultConfig())
  3464. {
  3465. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  3466. StopProcessingLoop();
  3467. }
  3468. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  3469. CheckGunTypeFromHw();
  3470. PRINTF_FUNC("CheckIsAlternatvieByModelName. \n");
  3471. CheckIsAlternatvieByModelName();
  3472. PRINTF_FUNC("ModelName = %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  3473. PRINTF_FUNC("InitialShareMemoryInfo \n");
  3474. InitialShareMemoryInfo();
  3475. PRINTF_FUNC("ChargerType (IEC or UL) = %d \n", ShmSysConfigAndInfo->SysInfo.ChargerType);
  3476. ChangeLcmByIndex(_LCM_INIT);
  3477. if (!CheckConnectorTypeStatus())
  3478. isModelNameMatch = false;
  3479. Initialization();
  3480. PRINTF_FUNC("Spawn all Task. \n");
  3481. SpawnTask();
  3482. if (!isModelNameMatch)
  3483. {
  3484. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3485. ChangeLcmByIndex(_LCM_FIX);
  3486. // Module Name 與硬體對應不正確
  3487. PRINTF_FUNC("Module Name & HW info none match. \n");
  3488. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  3489. sleep(3);
  3490. KillTask();
  3491. StopProcessingLoop();
  3492. }
  3493. PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
  3494. CreateTimeoutFork();
  3495. PRINTF_FUNC("Self test. \n");
  3496. SelfTestRun();
  3497. StopSystemTimeoutDet();
  3498. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  3499. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  3500. ShmPsuData->Work_Step == _NO_WORKING)
  3501. {
  3502. DisplaySelfTestFailReason();
  3503. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3504. {
  3505. setChargerMode(gun_index, MODE_ALARM);
  3506. }
  3507. ChangeLcmByIndex(_LCM_FIX);
  3508. sleep(3);
  3509. KillTask();
  3510. StopProcessingLoop();
  3511. }
  3512. else
  3513. {
  3514. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3515. {
  3516. setChargerMode(gun_index, MODE_IDLE);
  3517. }
  3518. }
  3519. ChangeLcmByIndex(_LCM_IDLE);
  3520. sleep(1);
  3521. //***** 須新增的偵測 *****//
  3522. // 1. Thernal - 控制風扇轉速
  3523. // 2. ouput fuse - 控制風扇轉速
  3524. CreateRfidFork();
  3525. // Create Watchdog
  3526. //CreateWatchdog();
  3527. // Main loop
  3528. PRINTF_FUNC("Main Loop. \n");
  3529. for (;;)
  3530. {
  3531. CheckOcppStatus();
  3532. ChkPrimaryStatus();
  3533. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  3534. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3535. {
  3536. CheckFactoryConfigFunction();
  3537. CheckFwUpdateFunction();
  3538. }
  3539. // OCPP 邏輯
  3540. OcppRemoteStartChk();
  3541. // 讀卡邏輯
  3542. ScannerCardProcess();
  3543. // 當 AC 沒有搭上時,清除一些錯誤碼
  3544. ClearAlarmCodeWhenAcOff();
  3545. if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan)
  3546. {
  3547. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3548. }
  3549. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3550. {
  3551. CheckGpioInStatus();
  3552. CheckErrorOccurStatus(gun_index);
  3553. ChkOcppStatus(gun_index);
  3554. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  3555. switch(chargingInfo[gun_index]->SystemStatus)
  3556. {
  3557. case S_IDLE:
  3558. case S_RESERVATION:
  3559. {
  3560. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  3561. isModeChange(gun_index))
  3562. {
  3563. PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
  3564. chargingInfo[gun_index]->RemainChargingDuration = 0;
  3565. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  3566. }
  3567. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  3568. isModeChange(gun_index))
  3569. {
  3570. PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
  3571. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  3572. }
  3573. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  3574. {
  3575. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  3576. {
  3577. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  3578. }
  3579. ClearDetectPluginFlag();
  3580. }
  3581. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3582. {/* 不給充電 */}
  3583. else
  3584. {
  3585. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
  3586. {
  3587. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3588. }
  3589. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3590. {
  3591. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  3592. {
  3593. // 均充 -> 最大充
  3594. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3595. {
  3596. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  3597. {
  3598. PRINTF_FUNC("=============Smart Charging============= Step 11 \n");
  3599. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3600. }
  3601. }
  3602. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  3603. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  3604. {
  3605. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  3606. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  3607. }
  3608. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  3609. {
  3610. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3611. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3612. }
  3613. }
  3614. else
  3615. {
  3616. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3617. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3618. }
  3619. }
  3620. else
  3621. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3622. { // Idle 正常程序起點
  3623. // 判斷是否有啟用檢查插槍
  3624. if(isDetectPlugin())
  3625. {
  3626. // 卡號驗證成功後,等待充電槍插入充電車
  3627. if ((ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL &&
  3628. ShmSysConfigAndInfo->SysInfo.OrderCharging < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount))
  3629. {
  3630. if (chargingInfo[(unsigned char)ShmSysConfigAndInfo->SysInfo.OrderCharging]->ConnectorPlugIn == YES &&
  3631. chargingInfo[gun_index]->IsAvailable)
  3632. {
  3633. PRINTF_FUNC("-----------------1----------------- %d \n", ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3634. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3635. AddPlugInTimes(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3636. setChargerMode(ShmSysConfigAndInfo->SysInfo.OrderCharging, MODE_REASSIGN_CHECK);
  3637. ClearDetectPluginFlag();
  3638. continue;
  3639. }
  3640. }
  3641. else
  3642. {
  3643. if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
  3644. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  3645. {
  3646. PRINTF_FUNC("-----------------2----------------- \n");
  3647. ChangeGunSelectByIndex(gun_index);
  3648. AddPlugInTimes(gun_index);
  3649. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  3650. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  3651. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3652. // 當前操作的槍號,進入 Preparing
  3653. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3654. ClearDetectPluginFlag();
  3655. continue;
  3656. }
  3657. }
  3658. if (!isCardScan)
  3659. {
  3660. // LCM => Waiting for plugging
  3661. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3662. }
  3663. }
  3664. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  3665. {
  3666. if (!isReservationExpired(gun_index))
  3667. {
  3668. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  3669. }
  3670. }
  3671. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  3672. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  3673. {
  3674. PRINTF_FUNC("-----------------3----------------- \n");
  3675. ChangeGunSelectByIndex(gun_index);
  3676. AddPlugInTimes(gun_index);
  3677. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3678. ClearDetectPluginFlag();
  3679. continue;
  3680. }
  3681. else
  3682. {
  3683. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3684. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  3685. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  3686. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  3687. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  3688. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  3689. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  3690. {
  3691. bool isReturnTimeout = false;
  3692. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  3693. {
  3694. if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  3695. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  3696. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX))
  3697. {
  3698. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  3699. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  3700. {
  3701. isReturnTimeout = true;
  3702. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3703. }
  3704. }
  3705. }
  3706. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  3707. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  3708. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  3709. {
  3710. isReturnTimeout = true;
  3711. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3712. }
  3713. if (!isReturnTimeout)
  3714. StopSystemTimeoutDet();
  3715. }
  3716. }
  3717. } // Idle 正常程序終點
  3718. }
  3719. ReleaseAlarmCode(gun_index);
  3720. }
  3721. break;
  3722. case S_REASSIGN_CHECK:
  3723. {
  3724. if (isModeChange(gun_index))
  3725. {
  3726. PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
  3727. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3728. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL)
  3729. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  3730. StopSystemTimeoutDet();
  3731. }
  3732. bool isRessign = false;
  3733. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  3734. {
  3735. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  3736. {
  3737. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3738. {
  3739. // 有其他槍已經分配好 psu 模塊
  3740. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  3741. chargingInfo[index]->SystemStatus >= S_PREPARNING)
  3742. {
  3743. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  3744. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  3745. isRessign = true;
  3746. break;
  3747. }
  3748. }
  3749. }
  3750. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  3751. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  3752. {
  3753. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  3754. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  3755. {
  3756. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  3757. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  3758. {
  3759. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  3760. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  3761. }
  3762. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  3763. {
  3764. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3765. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3766. }
  3767. }
  3768. continue;
  3769. }
  3770. }
  3771. if (isRessign)
  3772. setChargerMode(gun_index, MODE_REASSIGN);
  3773. else
  3774. setChargerMode(gun_index, MODE_PRECHARGE);
  3775. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3776. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3777. }
  3778. break;
  3779. case S_REASSIGN:
  3780. {
  3781. if (isModeChange(gun_index))
  3782. {
  3783. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  3784. }
  3785. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  3786. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  3787. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  3788. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3789. {
  3790. setChargerMode(gun_index, MODE_PRECHARGE);
  3791. }
  3792. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  3793. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3794. {
  3795. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  3796. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  3797. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3798. }
  3799. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  3800. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3801. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3802. }
  3803. break;
  3804. case S_PREPARNING:
  3805. {
  3806. if (isModeChange(gun_index))
  3807. {
  3808. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  3809. StopGunInfoTimeoutDet(gun_index);
  3810. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  3811. }
  3812. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10)
  3813. {
  3814. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  3815. }
  3816. if (isEvBoardStopChargeFlag(gun_index) == YES || OcppRemoteStop(gun_index) == YES)
  3817. {
  3818. // 板端或後臺要求停止
  3819. ChargingTerminalProcess(gun_index);
  3820. }
  3821. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3822. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3823. }
  3824. break;
  3825. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  3826. {
  3827. if (isModeChange(gun_index))
  3828. {
  3829. PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  3830. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3831. StopGunInfoTimeoutDet(gun_index);
  3832. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  3833. }
  3834. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3835. {
  3836. // 檢查車端的槍鎖是否為鎖上
  3837. if (isEvGunLocked_chademo(gun_index) == YES)
  3838. {
  3839. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3840. }
  3841. }
  3842. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3843. {
  3844. // 檢查車端的 charging enable 是否為 1
  3845. if (isEvGunLocked_gb(gun_index) == YES)
  3846. {
  3847. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3848. }
  3849. }
  3850. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3851. {
  3852. // 檢查車端的 charging enable 是否為 1
  3853. if (isEvGunLocked_ccs(gun_index) == YES)
  3854. {
  3855. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3856. }
  3857. }
  3858. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3859. OcppRemoteStop(gun_index) == YES)
  3860. {
  3861. // 板端或後臺要求停止
  3862. ChargingTerminalProcess(gun_index);
  3863. }
  3864. // LCM => Pre-charging
  3865. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3866. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3867. }
  3868. break;
  3869. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  3870. {
  3871. if (isModeChange(gun_index))
  3872. {
  3873. PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  3874. StopGunInfoTimeoutDet(gun_index);
  3875. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  3876. }
  3877. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3878. {
  3879. // 檢查樁端的 GFD 結果
  3880. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  3881. {
  3882. // 當前操作的槍號,進入 Charging
  3883. setChargerMode(gun_index, MODE_CHARGING);
  3884. }
  3885. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3886. {
  3887. // GFD 錯誤停止
  3888. RecordAlarmCode(gun_index, "012234");
  3889. }
  3890. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3891. {
  3892. // GFD 警告
  3893. RecordWarningCode(gun_index, "012296");
  3894. }
  3895. }
  3896. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3897. {
  3898. // 檢查樁端的 GFD 結果
  3899. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9)
  3900. {
  3901. setChargerMode(gun_index, MODE_CHARGING);
  3902. }
  3903. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3904. {
  3905. // GFD 錯誤停止
  3906. RecordAlarmCode(gun_index, "012236");
  3907. }
  3908. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3909. {
  3910. // GFD 警告
  3911. RecordWarningCode(gun_index, "012298");
  3912. }
  3913. }
  3914. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3915. {
  3916. // 檢查樁端的 GFD 結果
  3917. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  3918. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3919. {
  3920. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  3921. }
  3922. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3923. {
  3924. // GFD 錯誤停止
  3925. RecordAlarmCode(gun_index, "012235");
  3926. }
  3927. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3928. {
  3929. // GFD 警告
  3930. RecordWarningCode(gun_index, "012297");
  3931. }
  3932. }
  3933. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3934. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  3935. OcppRemoteStop(gun_index) == YES)
  3936. {
  3937. // 板端或後臺要求停止
  3938. ChargingTerminalProcess(gun_index);
  3939. }
  3940. // LCM => Pre-charging
  3941. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3942. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3943. }
  3944. break;
  3945. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  3946. {
  3947. if (isModeChange(gun_index))
  3948. {
  3949. PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
  3950. StopGunInfoTimeoutDet(gun_index);
  3951. ftime(&startChargingTime[gun_index]);
  3952. ChangeStartOrStopDateTime(YES, gun_index);
  3953. OcppStartTransation(gun_index);
  3954. }
  3955. ftime(&endChargingTime[gun_index]);
  3956. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  3957. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3958. {
  3959. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3960. {
  3961. // GFD 錯誤停止
  3962. RecordAlarmCode(gun_index, "012234");
  3963. }
  3964. else if ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3965. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  3966. {
  3967. // UVP
  3968. RecordAlarmCode(gun_index, "012289");
  3969. ChargingTerminalProcess(gun_index);
  3970. }
  3971. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3972. {
  3973. // GFD 警告
  3974. RecordWarningCode(gun_index, "012296");
  3975. }
  3976. else
  3977. ReleaseWarningCodeByString(gun_index, "012296");
  3978. }
  3979. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3980. {
  3981. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3982. {
  3983. // GFD 錯誤停止
  3984. RecordAlarmCode(gun_index, "012236");
  3985. }
  3986. else if (isPrechargeStatus_gb(gun_index) == 10 &&
  3987. ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3988. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)))
  3989. {
  3990. // UVP
  3991. RecordAlarmCode(gun_index, "012290");
  3992. ChargingTerminalProcess(gun_index);
  3993. }
  3994. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3995. {
  3996. // GFD 警告
  3997. RecordWarningCode(gun_index, "012298");
  3998. }
  3999. else
  4000. ReleaseWarningCodeByString(gun_index, "012298");
  4001. }
  4002. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  4003. {
  4004. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4005. {
  4006. // GFD 錯誤停止
  4007. RecordAlarmCode(gun_index, "012235");
  4008. }
  4009. else if ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  4010. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  4011. {
  4012. // UVP
  4013. RecordAlarmCode(gun_index, "012288");
  4014. ChargingTerminalProcess(gun_index);
  4015. }
  4016. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  4017. {
  4018. // GFD 警告
  4019. RecordWarningCode(gun_index, "012297");
  4020. }
  4021. else
  4022. ReleaseWarningCodeByString(gun_index, "012297");
  4023. }
  4024. if (isEvBoardStopChargeFlag(gun_index) ||
  4025. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  4026. OcppRemoteStop(gun_index) ||
  4027. CheckBackendChargingTimeout(gun_index) ||
  4028. CheckBackendChargingEnergy(gun_index))
  4029. {
  4030. PRINTF_FUNC("S_CHARGING=======Stop=========================== %f \n", chargingInfo[gun_index]->EvBatterytargetVoltage);
  4031. // 板端或後臺要求停止
  4032. ChargingTerminalProcess(gun_index);
  4033. }
  4034. // LCM => Charging
  4035. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  4036. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  4037. }
  4038. break;
  4039. case S_TERMINATING:
  4040. {
  4041. if (isModeChange(gun_index))
  4042. {
  4043. PRINTF_FUNC ("terminating......................... %x \n", gun_index);
  4044. StopGunInfoTimeoutDet(gun_index);
  4045. }
  4046. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  4047. {
  4048. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4049. if (isEvStopCharging_chademo(gun_index) == YES ||
  4050. isPrechargeStatus_chademo(gun_index) <= 0)
  4051. {
  4052. setChargerMode(gun_index, MODE_COMPLETE);
  4053. }
  4054. }
  4055. else if (chargingInfo[gun_index]->Type == _Type_GB)
  4056. {
  4057. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4058. if (isEvStopCharging_gb(gun_index) == YES ||
  4059. isPrechargeStatus_gb(gun_index) <= 0)
  4060. {
  4061. setChargerMode(gun_index, MODE_COMPLETE);
  4062. }
  4063. }
  4064. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  4065. {
  4066. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  4067. if (isEvStopCharging_ccs(gun_index) == YES &&
  4068. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  4069. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  4070. {
  4071. setChargerMode(gun_index, MODE_COMPLETE);
  4072. }
  4073. }
  4074. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  4075. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  4076. }
  4077. break;
  4078. case S_COMPLETE:
  4079. {
  4080. if (isModeChange(gun_index))
  4081. {
  4082. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  4083. OcppStopTransation(gun_index);
  4084. ftime(&endChargingTime[gun_index]);
  4085. if (chargingInfo[gun_index]->RemainChargingDuration != 0)
  4086. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  4087. StopGunInfoTimeoutDet(gun_index);
  4088. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  4089. ChangeStartOrStopDateTime(NO, gun_index);
  4090. }
  4091. if(chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  4092. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  4093. {
  4094. setChargerMode(gun_index, MODE_IDLE);
  4095. }
  4096. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  4097. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  4098. }
  4099. break;
  4100. case S_CCS_PRECHARGE_ST0:
  4101. {
  4102. if (isModeChange(gun_index))
  4103. {
  4104. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  4105. StopGunInfoTimeoutDet(gun_index);
  4106. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  4107. }
  4108. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4109. {
  4110. // GFD 錯誤停止
  4111. RecordAlarmCode(gun_index, "012235");
  4112. }
  4113. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  4114. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4115. {
  4116. // 板端要求停止
  4117. ChargingTerminalProcess(gun_index);
  4118. }
  4119. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  4120. // 切換 D+ Relay to Precharge Relay
  4121. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  4122. {
  4123. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  4124. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  4125. {
  4126. PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  4127. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  4128. }
  4129. }
  4130. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  4131. {
  4132. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  4133. }
  4134. break;
  4135. }
  4136. case S_CCS_PRECHARGE_ST1:
  4137. {
  4138. if (isModeChange(gun_index))
  4139. {
  4140. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  4141. }
  4142. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4143. {
  4144. // GFD 錯誤停止
  4145. RecordAlarmCode(gun_index, "012235");
  4146. }
  4147. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  4148. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  4149. {
  4150. // 板端要求停止
  4151. ChargingTerminalProcess(gun_index);
  4152. }
  4153. // 等待小板通知進入充電
  4154. // 切換 D+ Relay to Precharge Relay
  4155. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  4156. {
  4157. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  4158. setChargerMode(gun_index, MODE_CHARGING);
  4159. }
  4160. break;
  4161. }
  4162. }
  4163. }
  4164. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  4165. {
  4166. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  4167. }
  4168. else
  4169. {
  4170. bool dcPageRun = false;
  4171. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  4172. {
  4173. if (ac_chargingInfo[0]->SystemStatus == S_IDLE)
  4174. ChangeLcmByIndex(_LCM_IDLE);
  4175. else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING)
  4176. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  4177. else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  4178. ChangeLcmByIndex(_LCM_CHARGING);
  4179. else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  4180. ac_chargingInfo[0]->SystemStatus == S_COMPLETE)
  4181. ChangeLcmByIndex(_LCM_COMPLETE);
  4182. else
  4183. dcPageRun = true;
  4184. }
  4185. else
  4186. dcPageRun = true;
  4187. if (dcPageRun)
  4188. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  4189. }
  4190. write(wtdFd, "a", 1);
  4191. usleep(whileLoopTime);
  4192. }
  4193. return FAIL;
  4194. }