main.c 203 KB

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