main.c 192 KB

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