main.c 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814
  1. #include "define.h"
  2. #include "main.h"
  3. //==========================
  4. // System basic sample constant
  5. //==========================
  6. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  7. #define PASS 1
  8. #define FAIL -1
  9. #define YES 1
  10. #define NO 0
  11. #define ON 1
  12. #define OFF 0
  13. #define BUFFER_SIZE 128
  14. //==========================
  15. // Timer interval
  16. //==========================
  17. #define TMR_IDX_HANDSHAKING 0
  18. #define TMR_IDX_AUTH 1
  19. #define TMR_IDX_LOGPPRINTOUT 2
  20. #define TMR_IDX_PROFILE_PREPARE 3
  21. #define TMR_IDX_PWN_CHANGE 4
  22. #define TMR_IDX_CHECK_TASK 5
  23. #define TMR_IDX_6 6
  24. #define TMR_IDX_7 7
  25. #define TMR_IDX_8 8
  26. #define TMR_IDX_9 9
  27. #define TIMEOUT_SPEC_HANDSHAKING 180000
  28. #define TIMEOUT_SPEC_AUTH 60000
  29. #define TIMEOUT_SPEC_HANDSHAKING_LED 185000
  30. #define TIMEOUT_SPEC_LOGPPRINTOUT 30000
  31. #define TIMEOUT_SPEC_PROFILE_PREPARE 5000
  32. #define TIMEOUT_SPEC_PWN_CHANGE 5000
  33. #define SPEC_TEMPERATURE_WARN 75
  34. #define SPEC_TEMPERATURE_RECOVER 65
  35. #define MtdBlockSize 0x300000
  36. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  37. //==========================
  38. // Declare method
  39. //==========================
  40. void trim(char *s);
  41. int mystrcmp(char *p1,char *p2);
  42. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  43. void split(char **arr, char *str, const char *del);
  44. int isReachableInternet();
  45. int isRouteFail();
  46. int InitRfidPort(void);
  47. int GetCardSerialNumber();
  48. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  49. void setRelay(unsigned char gun_index,unsigned char isOn);
  50. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  51. void setLightIntensity(unsigned char gun_index);
  52. void initialLightIntensity(unsigned char gun_index);
  53. //==========================
  54. // Declare RFID module type
  55. //==========================
  56. #define MODULE_EWT 0
  57. int wtdFd = -1;
  58. int rfidFd = -1;
  59. char* rfidPortName = "/dev/ttyS2";
  60. RFID rfid;
  61. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  62. //==========================
  63. // Declare share memory
  64. //==========================
  65. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  66. struct StatusCodeData *ShmStatusCodeData;
  67. struct PsuData *ShmPsuData;
  68. struct CHAdeMOData *ShmCHAdeMOData;
  69. struct CcsData *ShmCcsData;
  70. struct PrimaryMcuData *ShmPrimaryMcuData;
  71. struct FanModuleData *ShmFanModuleData;
  72. struct RelayModuleData *ShmRelayModuleData;
  73. struct OCPP16Data *ShmOCPP16Data;
  74. struct Charger *ShmCharger;
  75. struct timeb startTime[AC_QUANTITY][10];
  76. struct timeb startChargingTime[AC_QUANTITY];
  77. struct timeb endChargingTime[AC_QUANTITY];
  78. sqlite3 *localDb;
  79. struct SysConfigData SysConfigOrg;
  80. //=================================
  81. // Common routine
  82. //=================================
  83. void trim(char *s)
  84. {
  85. int i=0, j, k, l=0;
  86. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  87. i++;
  88. j = strlen(s)-1;
  89. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  90. j--;
  91. if(i==0 && j==strlen(s)-1) { }
  92. else if(i==0) s[j+1] = '\0';
  93. else {
  94. for(k=i; k<=j; k++) s[l++] = s[k];
  95. s[l] = '\0';
  96. }
  97. }
  98. int mystrcmp(char *p1,char *p2)
  99. {
  100. while(*p1==*p2)
  101. {
  102. if(*p1=='\0' || *p2=='\0')
  103. break;
  104. p1++;
  105. p2++;
  106. }
  107. if(*p1=='\0' && *p2=='\0')
  108. return(PASS);
  109. else
  110. return(FAIL);
  111. }
  112. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  113. {
  114. strncpy(dest, src + start, cnt);
  115. dest[cnt] = 0;
  116. }
  117. void split(char **arr, char *str, const char *del)
  118. {
  119. char *s = strtok(str, del);
  120. while(s != NULL)
  121. {
  122. *arr++ = s;
  123. s = strtok(NULL, del);
  124. }
  125. }
  126. int StoreLogMsg(const char *fmt, ...)
  127. {
  128. char Buf[4096+256];
  129. char buffer[4096];
  130. time_t CurrentTime;
  131. struct tm *tm;
  132. struct timeval tv;
  133. va_list args;
  134. va_start(args, fmt);
  135. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  136. va_end(args);
  137. memset(Buf,0,sizeof(Buf));
  138. CurrentTime = time(NULL);
  139. tm=localtime(&CurrentTime);
  140. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  141. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  142. {
  143. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  144. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  145. buffer,
  146. tm->tm_year+1900,tm->tm_mon+1,
  147. ShmSysConfigAndInfo->SysConfig.ModelName,
  148. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  149. }
  150. else
  151. {
  152. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  153. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  154. buffer,
  155. tm->tm_year+1900,tm->tm_mon+1);
  156. }
  157. #ifdef SystemLogMessage
  158. system(Buf);
  159. #endif
  160. #ifdef ConsloePrintLog
  161. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  162. #endif
  163. return rc;
  164. }
  165. long long DiffTimebWithNow(struct timeb ST)
  166. {
  167. //return milli-second
  168. struct timeb ET;
  169. long long StartTime,StopTime;
  170. ftime(&ET);
  171. StartTime=(long long)ST.time;
  172. StopTime=(long long)ET.time;
  173. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  174. }
  175. long long DiffTimeb(struct timeb ST, struct timeb ET)
  176. {
  177. //return milli-second
  178. long long StartTime,StopTime;
  179. StartTime=(long long)ST.time;
  180. StopTime=(long long)ET.time;
  181. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  182. }
  183. int DiffTimebWithNowSec(struct timeb ST)
  184. {
  185. //return milli-second
  186. struct timeb ET;
  187. unsigned int StartTime,StopTime;
  188. ftime(&ET);
  189. StartTime=(unsigned int)ST.time;
  190. StopTime=(unsigned int)ET.time;
  191. return (StopTime-StartTime);
  192. }
  193. int isOvertNow(uint8_t *start)
  194. {
  195. int result = YES;
  196. struct ParsingResult
  197. {
  198. int result;
  199. int scanedElement;
  200. int year;
  201. int month;
  202. int mday;
  203. int hour;
  204. int min;
  205. int sec;
  206. int tz_hour;
  207. int tz_min;
  208. float minSec;
  209. }parsingResult;
  210. struct tm tmStart;
  211. struct timeb tbStart;
  212. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  213. if(strstr((char*)start, ".") != NULL)
  214. {
  215. // Original data with mini second
  216. if(strstr((char*)start, "Z") != NULL)
  217. {
  218. // Original data with Z
  219. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  220. &parsingResult.year,
  221. &parsingResult.month,
  222. &parsingResult.mday,
  223. &parsingResult.hour,
  224. &parsingResult.min,
  225. &parsingResult.sec,
  226. &parsingResult.minSec);
  227. }
  228. else
  229. {
  230. // Original data without Z
  231. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  232. &parsingResult.year,
  233. &parsingResult.month,
  234. &parsingResult.mday,
  235. &parsingResult.hour,
  236. &parsingResult.min,
  237. &parsingResult.sec,
  238. &parsingResult.minSec,
  239. &parsingResult.tz_hour,
  240. &parsingResult.tz_min);
  241. }
  242. }
  243. else
  244. {
  245. // Original data without mini second
  246. if(strstr((char*)start, "Z") != NULL)
  247. {
  248. // Original data with Z
  249. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  250. &parsingResult.year,
  251. &parsingResult.month,
  252. &parsingResult.mday,
  253. &parsingResult.hour,
  254. &parsingResult.min,
  255. &parsingResult.sec);
  256. }
  257. else
  258. {
  259. // Original data without Z
  260. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  261. &parsingResult.year,
  262. &parsingResult.month,
  263. &parsingResult.mday,
  264. &parsingResult.hour,
  265. &parsingResult.min,
  266. &parsingResult.sec,
  267. &parsingResult.tz_hour,
  268. &parsingResult.tz_min);
  269. }
  270. }
  271. if(parsingResult.scanedElement >= 6)
  272. {
  273. tmStart.tm_year = parsingResult.year - 1900;
  274. tmStart.tm_mon = parsingResult.month - 1;
  275. tmStart.tm_mday = parsingResult.mday;
  276. tmStart.tm_hour = parsingResult.hour;
  277. tmStart.tm_min = parsingResult.min;
  278. tmStart.tm_sec = parsingResult.sec;
  279. tmStart.tm_gmtoff = 0;
  280. tbStart.time = mktime(&tmStart);
  281. tbStart.timezone = 0;
  282. tbStart.millitm = 0;
  283. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  284. if(DiffTimebWithNowSec(tbStart) <= 0)
  285. result = NO;
  286. else
  287. result = YES;
  288. }
  289. else
  290. {
  291. DEBUG_WARN("Start date parsing error.\n");
  292. }
  293. return result;
  294. }
  295. void getDateTimeString(char* result)
  296. {
  297. time_t CurrentTime;
  298. struct tm *tm;
  299. CurrentTime = time(NULL);
  300. tm=localtime(&CurrentTime);
  301. sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  302. }
  303. unsigned long long getAvailableMemory()
  304. {
  305. long pages = sysconf(_SC_AVPHYS_PAGES);
  306. long page_size = sysconf(_SC_PAGE_SIZE);
  307. return pages * page_size;
  308. }
  309. unsigned int isKernelSupportNAT()
  310. {
  311. unsigned int result = NO;
  312. unsigned int version = 0;
  313. FILE *fp;
  314. char cmd[256];
  315. char buf[512];
  316. // Get IP address & net mask
  317. strcpy(cmd, "uname -v");
  318. fp = popen(cmd, "r");
  319. if(fp != NULL)
  320. {
  321. if(fgets(buf, sizeof(buf), fp) != NULL)
  322. {
  323. sscanf(buf, "#%d", &version);
  324. //DEBUG_INFO("Kernel version: %d\n", version);
  325. if(version >= 30)
  326. result = YES;
  327. }
  328. }
  329. pclose(fp);
  330. return result;
  331. }
  332. int getEth0MacAddress()
  333. {
  334. int result = PASS;
  335. FILE *fp;
  336. char cmd[256];
  337. char buf[512];
  338. char tmp[512];
  339. strcpy(cmd, "ifconfig eth0");
  340. fp = popen(cmd, "r");
  341. if(fp != NULL)
  342. {
  343. while(fgets(buf, sizeof(buf), fp) != NULL)
  344. {
  345. if(strstr(buf, "eth0") > 0)
  346. {
  347. result = PASS;
  348. }
  349. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  350. {
  351. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  352. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  353. }
  354. }
  355. }
  356. pclose(fp);
  357. return result;
  358. }
  359. //======================================================
  360. // OCPP routine
  361. //======================================================
  362. void ocpp_process_start()
  363. {
  364. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  365. {
  366. system("/root/OcppBackend &");
  367. }
  368. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  369. {
  370. }
  371. }
  372. uint8_t ocpp_get_connection_status()
  373. {
  374. uint8_t result = OFF;
  375. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  376. {
  377. result = ShmOCPP16Data->OcppConnStatus;
  378. }
  379. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  380. {
  381. }
  382. return result;
  383. }
  384. uint16_t ocpp_get_connection_timeout()
  385. {
  386. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING/1000);
  387. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  388. {
  389. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  390. {
  391. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  392. }
  393. }
  394. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  395. {
  396. }
  397. return result;
  398. }
  399. uint8_t ocpp_get_update_firmware_req()
  400. {
  401. uint8_t result = NO;
  402. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  403. {
  404. result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq;
  405. }
  406. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  407. {
  408. }
  409. return result;
  410. }
  411. uint8_t ocpp_get_reset_req()
  412. {
  413. uint8_t result = NO;
  414. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  415. {
  416. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  417. }
  418. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  419. {
  420. }
  421. return result;
  422. }
  423. void ocpp_boot_info_sync()
  424. {
  425. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  426. {
  427. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  428. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  429. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  430. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  431. switch(ShmSysConfigAndInfo->SysConfig.ModelName[3])
  432. {
  433. case 'M':
  434. case 'Z':
  435. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "MID");
  436. break;
  437. case 'P':
  438. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
  439. break;
  440. case 'I':
  441. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
  442. break;
  443. case 'U':
  444. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "UL");
  445. break;
  446. default:
  447. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
  448. break;
  449. }
  450. }
  451. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  452. {
  453. }
  454. }
  455. void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
  456. {
  457. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  458. {
  459. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
  460. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
  461. }
  462. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  463. {
  464. }
  465. }
  466. void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
  467. {
  468. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  469. {
  470. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
  471. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
  472. }
  473. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  474. {
  475. }
  476. }
  477. uint8_t ocpp_get_remotestart(uint8_t gun_index)
  478. {
  479. uint8_t result = OFF;
  480. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  481. {
  482. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  483. }
  484. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  485. {
  486. }
  487. return result;
  488. }
  489. void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
  490. {
  491. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  492. {
  493. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  494. }
  495. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  496. {
  497. }
  498. }
  499. uint8_t ocpp_get_remotestop(uint8_t gun_index)
  500. {
  501. uint8_t result = OFF;
  502. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  503. {
  504. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
  505. }
  506. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  507. {
  508. }
  509. return result;
  510. }
  511. void ocpp_set_auth_req(uint8_t status, ...)
  512. {
  513. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  514. {
  515. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
  516. {
  517. if(status == ON)
  518. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  519. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
  520. }
  521. }
  522. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  523. {
  524. }
  525. }
  526. uint8_t ocpp_get_auth_req()
  527. {
  528. uint8_t result = OFF;
  529. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  530. {
  531. result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
  532. }
  533. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  534. {
  535. }
  536. return result;
  537. }
  538. void ocpp_set_auth_conf(uint8_t status)
  539. {
  540. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  541. {
  542. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
  543. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
  544. }
  545. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  546. {
  547. }
  548. }
  549. uint8_t ocpp_get_auth_conf()
  550. {
  551. uint8_t result = OFF;
  552. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  553. {
  554. result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  555. }
  556. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  557. {
  558. }
  559. return result;
  560. }
  561. uint8_t ocpp_get_auth_result(uint8_t gun_index)
  562. {
  563. uint8_t result = OFF;
  564. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  565. {
  566. case SYS_MODE_AUTHORIZING:
  567. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  568. {
  569. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0))
  570. result = PASS;
  571. }
  572. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  573. {
  574. }
  575. break;
  576. case SYS_MODE_CHARGING:
  577. case SYS_MODE_TERMINATING:
  578. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  579. {
  580. DEBUG_INFO("==========================================\n");
  581. DEBUG_INFO("=== OCPP GETTING AUTHORIZE RESULT 1.6 ====\n");
  582. DEBUG_INFO("==========================================\n");
  583. DEBUG_INFO("Authorize.ResponseIdTagInfo.ParentIdTag : %s \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  584. DEBUG_INFO("StartTransaction[%d].ResponseIdTagInfo.ParentIdTag : %s \n", gun_index ,ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag);
  585. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  586. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0))
  587. result = PASS;
  588. }
  589. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  590. {
  591. }
  592. break;
  593. default:
  594. break;
  595. }
  596. DEBUG_INFO("Authorize result : %s \n", ((result == PASS)?"Pass":"Fail"));
  597. return result;
  598. }
  599. uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
  600. {
  601. uint8_t result = OFF;
  602. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  603. {
  604. result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor;
  605. }
  606. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  607. {
  608. }
  609. return result;
  610. }
  611. void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
  612. {
  613. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  614. {
  615. if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor != status)
  616. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = status;
  617. }
  618. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  619. {
  620. }
  621. }
  622. void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
  623. {
  624. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  625. {
  626. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
  627. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
  628. }
  629. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  630. {
  631. }
  632. }
  633. void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
  634. {
  635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  636. {
  637. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
  638. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
  639. }
  640. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  641. {
  642. }
  643. }
  644. void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
  645. {
  646. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  647. {
  648. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
  649. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
  650. }
  651. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  652. {
  653. }
  654. }
  655. void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
  656. {
  657. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  658. {
  659. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
  660. {
  661. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
  662. }
  663. }
  664. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  665. {
  666. }
  667. }
  668. void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
  669. {
  670. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  671. {
  672. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  673. }
  674. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  675. {
  676. }
  677. }
  678. uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
  679. {
  680. uint8_t result = PASS;
  681. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  682. {
  683. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  684. {
  685. if((strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked")==0) ||
  686. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired")==0) ||
  687. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid")==0))
  688. result = NO;
  689. }
  690. }
  691. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  692. {
  693. }
  694. return result;
  695. }
  696. uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
  697. {
  698. uint8_t result = 0;
  699. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  700. {
  701. result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
  702. }
  703. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  704. {
  705. }
  706. return result;
  707. }
  708. void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
  709. {
  710. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  711. {
  712. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  713. }
  714. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  715. {
  716. }
  717. }
  718. uint8_t ocpp_get_profile_req(uint8_t gun_index)
  719. {
  720. uint8_t result = OFF;
  721. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  722. {
  723. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  724. }
  725. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  726. {
  727. }
  728. return result;
  729. }
  730. void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
  731. {
  732. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  733. {
  734. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  735. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  736. }
  737. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  738. {
  739. }
  740. }
  741. uint8_t ocpp_get_profile_conf(uint8_t gun_index)
  742. {
  743. uint8_t result = OFF;
  744. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  745. {
  746. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  747. }
  748. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  749. {
  750. }
  751. return result;
  752. }
  753. void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
  754. {
  755. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  756. {
  757. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  758. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  759. }
  760. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  761. {
  762. }
  763. }
  764. uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
  765. {
  766. uint8_t result = OFF;
  767. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  768. {
  769. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
  770. result = ON;
  771. }
  772. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  773. {
  774. }
  775. return result;
  776. }
  777. uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
  778. {
  779. uint8_t result = OFF;
  780. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  781. {
  782. result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
  783. }
  784. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  785. {
  786. }
  787. return result;
  788. }
  789. void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
  790. {
  791. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  792. {
  793. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  794. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  795. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  796. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  797. }
  798. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  799. {
  800. }
  801. }
  802. uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
  803. {
  804. uint8_t result = OFF;
  805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  806. {
  807. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  808. result = ON;
  809. }
  810. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  811. {
  812. }
  813. return result;
  814. }
  815. uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
  816. {
  817. uint8_t result = OFF;
  818. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  819. {
  820. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  821. {
  822. result = ON;
  823. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  824. }
  825. }
  826. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  827. {
  828. }
  829. return result;
  830. }
  831. //======================================================
  832. // Check interface status
  833. //======================================================
  834. int isInterfaceUp(const char *interface)
  835. {
  836. int result = FAIL;
  837. FILE *fp;
  838. char cmd[256];
  839. char buf[512];
  840. strcpy(cmd, "ifconfig");;
  841. fp = popen(cmd, "r");
  842. if(fp != NULL)
  843. {
  844. while(fgets(buf, sizeof(buf), fp) != NULL)
  845. {
  846. if(strstr(buf, interface) > 0)
  847. {
  848. result = PASS;
  849. }
  850. }
  851. }
  852. pclose(fp);
  853. return result;
  854. }
  855. //======================================================
  856. // Create all share memory
  857. //======================================================
  858. int CreatShareMemory()
  859. {
  860. int result = PASS;
  861. int MeterSMId;
  862. //creat ShmSysConfigAndInfo
  863. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  864. {
  865. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  866. result = FAIL;
  867. }
  868. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  869. {
  870. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  871. result = FAIL;
  872. }
  873. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  874. //creat ShmStatusCodeData
  875. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  876. {
  877. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  878. result = FAIL;
  879. }
  880. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  881. {
  882. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  883. result = FAIL;
  884. }
  885. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  886. //creat ShmPsuData
  887. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  888. {
  889. DEBUG_ERROR("shmget ShmPsuData NG\n");
  890. result = FAIL;
  891. }
  892. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  893. {
  894. DEBUG_ERROR("shmat ShmPsuData NG\n");
  895. result = FAIL;
  896. }
  897. memset(ShmPsuData,0,sizeof(struct PsuData));
  898. //creat ShmCHAdeMOData
  899. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  900. {
  901. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\n");
  902. result = FAIL;
  903. }
  904. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  905. {
  906. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\n");
  907. result = FAIL;
  908. }
  909. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  910. //creat ShmCcsData
  911. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  912. {
  913. DEBUG_ERROR("shmget ShmCcsData NG\n");
  914. result = FAIL;
  915. }
  916. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  917. {
  918. DEBUG_ERROR("shmat ShmCcsData NG\n");
  919. result = FAIL;
  920. }
  921. memset(ShmCcsData,0,sizeof(struct CcsData));
  922. //creat ShmPrimaryMcuData
  923. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  924. {
  925. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  926. result = FAIL;
  927. }
  928. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  929. {
  930. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  931. result = FAIL;
  932. }
  933. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  934. /*
  935. //creat ShmFanModuleData
  936. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  937. {
  938. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  939. result = FAIL;
  940. }
  941. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  942. {
  943. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  944. result = FAIL;
  945. }
  946. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  947. //creat ShmRelayModuleData
  948. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  949. {
  950. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  951. result = FAIL;
  952. }
  953. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  954. {
  955. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  956. result = FAIL;
  957. }
  958. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
  959. //creat ShmOCPP16Data
  960. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  961. {
  962. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  963. result = FAIL;
  964. }
  965. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  966. {
  967. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  968. result = FAIL;
  969. }
  970. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  971. //creat ShmCharger
  972. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  973. {
  974. DEBUG_ERROR("shmget ShmCharger NG\n");
  975. result = FAIL;
  976. }
  977. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  978. {
  979. DEBUG_ERROR("shmat ShmCharger NG\n");
  980. result = FAIL;
  981. }
  982. memset(ShmCharger,0,sizeof(struct Charger));
  983. return result;
  984. }
  985. //===============================================
  986. // SQLite3 related routine
  987. //===============================================
  988. int DB_Open(sqlite3 *db)
  989. {
  990. int result = PASS;
  991. char* errMsg = NULL;
  992. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  993. "idx integer primary key AUTOINCREMENT, "
  994. "reservationId text, "
  995. "transactionId text, "
  996. "startMethod text, "
  997. "userId text, "
  998. "dateTimeStart text, "
  999. "dateTimeStop text,"
  1000. "socStart text, "
  1001. "socStop text, "
  1002. "chargeEnergy text, "
  1003. "stopReason text"
  1004. ");";
  1005. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  1006. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  1007. "`item` TEXT NOT NULL, "
  1008. "`connector` INTEGER NOT NULL, "
  1009. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  1010. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1011. if(sqlite3_open(DB_FILE, &db))
  1012. {
  1013. result = FAIL;
  1014. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1015. sqlite3_close(db);
  1016. }
  1017. else
  1018. {
  1019. DEBUG_INFO( "Local charging record database open successfully.\n");
  1020. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  1021. {
  1022. result = FAIL;
  1023. DEBUG_ERROR( "Create local charging record table error message: %s\n", errMsg);
  1024. }
  1025. else
  1026. {
  1027. DEBUG_INFO( "Opened local charging record table successfully\n");
  1028. }
  1029. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  1030. {
  1031. result = FAIL;
  1032. DEBUG_ERROR( "Create local config table error message: %s\n", errMsg);
  1033. }
  1034. else
  1035. {
  1036. DEBUG_INFO( "Opened local config table successfully\n");
  1037. }
  1038. sqlite3_close(db);
  1039. }
  1040. return result;
  1041. }
  1042. int DB_Insert_Record(sqlite3 *db, int gun_index)
  1043. {
  1044. int result = PASS;
  1045. char* errMsg = NULL;
  1046. char sqlStr[1024];
  1047. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1048. {
  1049. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  1050. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  1051. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1052. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1053. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1054. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1055. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1056. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1057. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1058. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1059. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1060. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  1061. }
  1062. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1063. {
  1064. }
  1065. if(sqlite3_open(DB_FILE, &db))
  1066. {
  1067. result = FAIL;
  1068. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1069. sqlite3_close(db);
  1070. }
  1071. else
  1072. {
  1073. DEBUG_INFO( "Local charging record database open successfully.\n");
  1074. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1075. {
  1076. result = FAIL;
  1077. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  1078. }
  1079. else
  1080. {
  1081. DEBUG_INFO( "Insert local charging record successfully\n");
  1082. }
  1083. sprintf(sqlStr, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  1084. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1085. {
  1086. result = FAIL;
  1087. DEBUG_INFO( "delete local charging error message: %s\n", errMsg);
  1088. }
  1089. else
  1090. {
  1091. DEBUG_INFO( "delete local charging record successfully\n");
  1092. }
  1093. sqlite3_close(db);
  1094. }
  1095. return result;
  1096. }
  1097. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  1098. {
  1099. uint8_t result = false;
  1100. char* errMsg = NULL;
  1101. char sqlStr[1024];
  1102. srand(time(NULL));
  1103. if(sqlite3_open(DB_FILE, &db))
  1104. {
  1105. result = FAIL;
  1106. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1107. sqlite3_close(db);
  1108. }
  1109. else
  1110. {
  1111. DEBUG_INFO( "Local charging record database open successfully.\n");
  1112. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
  1113. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1114. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1115. {
  1116. result = FAIL;
  1117. DEBUG_INFO( "update config error message: %s\n", errMsg);
  1118. }
  1119. else
  1120. {
  1121. DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
  1122. }
  1123. sqlite3_close(db);
  1124. }
  1125. return result;
  1126. }
  1127. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  1128. {
  1129. uint8_t result = true;
  1130. char* errMsg = NULL;
  1131. char sqlStr[1024];
  1132. char **rs;
  1133. int rows, cols;
  1134. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  1135. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1136. if(sqlite3_open(DB_FILE, &db))
  1137. {
  1138. result = FAIL;
  1139. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1140. sqlite3_close(db);
  1141. }
  1142. else
  1143. {
  1144. DEBUG_INFO( "Local config query database open successfully.\n");
  1145. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1146. if(rows>0)
  1147. {
  1148. for(int idxRow=1;idxRow<=rows;idxRow++)
  1149. {
  1150. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1151. {
  1152. result = false;
  1153. }
  1154. DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow*cols)+3]);
  1155. }
  1156. }
  1157. else
  1158. {
  1159. DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
  1160. }
  1161. sqlite3_free_table(rs);
  1162. sqlite3_close(db);
  1163. }
  1164. return result;
  1165. }
  1166. //======================================================
  1167. // Peripheral initial
  1168. //======================================================
  1169. int InitWatchDog()
  1170. {
  1171. int fd;
  1172. int timeout = 180;
  1173. system("/usr/bin/fuser -k /dev/watchdog");
  1174. sleep(1);
  1175. system("echo V > /dev/watchdog");
  1176. sleep(1);
  1177. fd=open("/dev/watchdog", O_RDWR);
  1178. if(fd<=0)
  1179. {
  1180. DEBUG_ERROR("System watch dog initial fail.\n");
  1181. }
  1182. ioctl(fd, _IOWR('W', 6, int), &timeout);
  1183. return fd;
  1184. }
  1185. void InitGPIO()
  1186. {
  1187. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1188. /***************************************************************/
  1189. /*************** INPUT PIN ***************************************/
  1190. /***************************************************************/
  1191. /***************************************************************/
  1192. /*************** OUTPUT PIN ************************************/
  1193. /***************************************************************/
  1194. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  1195. system("echo 116 > /sys/class/gpio/export");
  1196. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1197. system("echo 0 > /sys/class/gpio/gpio116/value");
  1198. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  1199. system("echo 19 > /sys/class/gpio/export");
  1200. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1201. system("echo 1 > /sys/class/gpio/gpio19/value");
  1202. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  1203. system("echo 65 > /sys/class/gpio/export");
  1204. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  1205. system("echo 0 > /sys/class/gpio/gpio65/value");
  1206. /*4G/Wifi RST:GPIO3_14 => H:ON; L:OFF*/
  1207. system("echo 110 > /sys/class/gpio/export");
  1208. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1209. system("echo 1 > /sys/class/gpio/gpio110/value");
  1210. sleep(3);
  1211. system("echo 0 > /sys/class/gpio/gpio110/value");
  1212. DEBUG_INFO("Initial GPIO OK\n");
  1213. }
  1214. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1215. {
  1216. int fd,wrd;
  1217. unsigned char *buf;
  1218. unsigned int ChkSum,ChkSumOrg;
  1219. if((buf=malloc(MtdBlockSize))==NULL)
  1220. {
  1221. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  1222. if(ShmStatusCodeData!=NULL)
  1223. {
  1224. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1225. }
  1226. sleep(5);
  1227. system("reboot -f");
  1228. sleep(5);
  1229. system("reboot -f");
  1230. }
  1231. memset(buf, 0, MtdBlockSize);
  1232. //================================================
  1233. // Load configuration from mtdblock10
  1234. //================================================
  1235. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1236. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1237. if (fd < 0)
  1238. {
  1239. free(buf);
  1240. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  1241. if(ShmStatusCodeData!=NULL)
  1242. {
  1243. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1244. }
  1245. sleep(5);
  1246. system("reboot -f");
  1247. sleep(5);
  1248. system("reboot -f");
  1249. }
  1250. wrd=read(fd, buf, MtdBlockSize);
  1251. close(fd);
  1252. if(wrd<MtdBlockSize)
  1253. {
  1254. free(buf);
  1255. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  1256. if(ShmStatusCodeData!=NULL)
  1257. {
  1258. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1259. }
  1260. sleep(5);
  1261. system("reboot -f");
  1262. sleep(5);
  1263. system("reboot -f");
  1264. }
  1265. ChkSum=0;
  1266. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1267. {
  1268. ChkSum+=buf[wrd];
  1269. }
  1270. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1271. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1272. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1273. //================================================
  1274. // Load configuration from mtdblock11
  1275. //================================================
  1276. if(ChkSum!=ChkSumOrg)
  1277. {
  1278. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1279. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1280. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1281. if (fd < 0)
  1282. {
  1283. free(buf);
  1284. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1285. if(ShmStatusCodeData!=NULL)
  1286. {
  1287. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1288. }
  1289. sleep(5);
  1290. system("reboot -f");
  1291. sleep(5);
  1292. system("reboot -f");
  1293. }
  1294. memset(buf, 0, MtdBlockSize);
  1295. wrd=read(fd, buf,MtdBlockSize);
  1296. close(fd);
  1297. if(wrd<MtdBlockSize)
  1298. {
  1299. free(buf);
  1300. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1301. if(ShmStatusCodeData!=NULL)
  1302. {
  1303. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1304. }
  1305. sleep(5);
  1306. system("reboot -f");
  1307. sleep(5);
  1308. system("reboot -f");
  1309. }
  1310. ChkSum=0;
  1311. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1312. {
  1313. ChkSum+=buf[wrd];
  1314. }
  1315. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1316. //================================================
  1317. // Load configuration from mtdblock12 (Factory default)
  1318. //================================================
  1319. if(ChkSum!=ChkSumOrg)
  1320. {
  1321. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  1322. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1323. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1324. if (fd < 0)
  1325. {
  1326. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  1327. free(buf);
  1328. if(ShmStatusCodeData!=NULL)
  1329. {
  1330. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1331. }
  1332. sleep(5);
  1333. system("reboot -f");
  1334. sleep(5);
  1335. system("reboot -f");
  1336. }
  1337. memset(buf, 0, MtdBlockSize);
  1338. wrd=read(fd, buf,MtdBlockSize);
  1339. close(fd);
  1340. if(wrd<MtdBlockSize)
  1341. {
  1342. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  1343. free(buf);
  1344. if(ShmStatusCodeData!=NULL)
  1345. {
  1346. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1347. }
  1348. sleep(5);
  1349. system("reboot -f");
  1350. sleep(5);
  1351. system("reboot -f");
  1352. }
  1353. ChkSum=0;
  1354. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1355. {
  1356. ChkSum+=buf[wrd];
  1357. }
  1358. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1359. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1360. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1361. if(ChkSum!=ChkSumOrg)
  1362. {
  1363. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  1364. free(buf);
  1365. system("cd /root;./Module_FactoryConfig -m");
  1366. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1367. system("sync");
  1368. sleep(5);
  1369. system("reboot -f");
  1370. sleep(5);
  1371. system("reboot -f");
  1372. return FAIL;
  1373. }
  1374. }
  1375. }
  1376. //load OK
  1377. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1378. free(buf);
  1379. system("rm -f /mnt/EvseConfig.bin");
  1380. // SysConfig in flash is empty (0xffffffff)
  1381. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1382. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1383. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1384. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1385. {
  1386. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1387. {
  1388. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1389. }
  1390. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1391. {
  1392. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1393. }
  1394. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1395. {
  1396. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1397. }
  1398. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1399. {
  1400. DEBUG_INFO("Ethernet dhcp config is null.\n");
  1401. }
  1402. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  1403. {
  1404. DEBUG_INFO("Model name over length.\n");
  1405. }
  1406. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  1407. {
  1408. DEBUG_INFO("Model serial number over length.\n");
  1409. }
  1410. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  1411. {
  1412. DEBUG_INFO("System id over length.\n");
  1413. }
  1414. system("cd /root;./Module_FactoryConfig -m");
  1415. sleep(3);
  1416. system("/usr/bin/run_evse_restart.sh");
  1417. }
  1418. DEBUG_INFO("Load SysConfigData OK\n");
  1419. return PASS;
  1420. }
  1421. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1422. {
  1423. int result = PASS;
  1424. int fd,wrd;
  1425. unsigned int i,Chk;
  1426. unsigned char *ptr, *BufTmp;
  1427. Chk=0;
  1428. ptr=(unsigned char *)UsrData;
  1429. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1430. {
  1431. memset(BufTmp,0,MtdBlockSize);
  1432. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1433. for(i=0;i<MtdBlockSize-4;i++)
  1434. Chk+=*(BufTmp+i);
  1435. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  1436. // Output configuration to file.
  1437. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  1438. if (fd < 0)
  1439. {
  1440. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  1441. free(BufTmp);
  1442. return 0;
  1443. }
  1444. wrd=write(fd, BufTmp, MtdBlockSize);
  1445. close(fd);
  1446. if(wrd<MtdBlockSize)
  1447. {
  1448. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  1449. free(BufTmp);
  1450. return 0;
  1451. }
  1452. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  1453. DEBUG_INFO("Erase /dev/mtd10.\n");
  1454. runShellCmd("flash_erase /dev/mtd10 0 12");
  1455. DEBUG_INFO("Write /dev/mtd10.\n");
  1456. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  1457. DEBUG_INFO("Erase /dev/mtd11.\n");
  1458. runShellCmd("flash_erase /dev/mtd11 0 12");
  1459. DEBUG_INFO("Write /dev/mtd11.\n");
  1460. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  1461. system("rm -f /mnt/EvseConfig.bin");
  1462. DEBUG_INFO("EvseConfig write to flash OK\n");
  1463. }
  1464. else
  1465. {
  1466. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1467. result = FAIL;
  1468. }
  1469. if(BufTmp!=NULL)
  1470. free(BufTmp);
  1471. return result;
  1472. }
  1473. void InitEthernet()
  1474. {
  1475. pid_t pid;
  1476. uint8_t cnt_pingDNS_Fail;
  1477. char tmpbuf[256];
  1478. //unsigned int address;
  1479. //Init Eth0 for internet
  1480. memset(tmpbuf,0,256);
  1481. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1482. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1483. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1484. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1485. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1486. system(tmpbuf);
  1487. memset(tmpbuf,0,256);
  1488. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1489. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1490. system(tmpbuf);
  1491. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  1492. system("ifconfig lo up &");
  1493. system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  1494. if(isInterfaceUp("eth1")==PASS)
  1495. {
  1496. //Init Eth1 for administrator tool
  1497. memset(tmpbuf,0,256);
  1498. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up &",
  1499. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1500. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1501. system(tmpbuf);
  1502. }
  1503. //Run DHCP client if enabled
  1504. system("killall udhcpc");
  1505. system("rm -rf /etc/resolv.conf");
  1506. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  1507. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1508. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  1509. {
  1510. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1511. system(tmpbuf);
  1512. }
  1513. // Upgrade system id to /etc/hostname
  1514. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1515. system(tmpbuf);
  1516. // Ethernet MAC address
  1517. getEth0MacAddress();
  1518. //check internet status
  1519. pid = fork();
  1520. if(pid == 0)
  1521. {
  1522. for(;;)
  1523. {
  1524. if(isRouteFail())
  1525. {
  1526. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  1527. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1528. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1529. {
  1530. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1531. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1532. system(tmpbuf);
  1533. }
  1534. else
  1535. {
  1536. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1537. memset(tmpbuf,0,256);
  1538. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1539. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1540. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1541. system(tmpbuf);
  1542. memset(tmpbuf,0,256);
  1543. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1544. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1545. system(tmpbuf);
  1546. }
  1547. }
  1548. if(isReachableInternet() == PASS)
  1549. {
  1550. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  1551. cnt_pingDNS_Fail = 0;
  1552. }
  1553. else
  1554. {
  1555. if(cnt_pingDNS_Fail >= 3)
  1556. {
  1557. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  1558. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1559. {
  1560. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1561. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1562. system(tmpbuf);
  1563. }
  1564. else
  1565. {
  1566. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1567. memset(tmpbuf,0,256);
  1568. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1569. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1570. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1571. system(tmpbuf);
  1572. memset(tmpbuf,0,256);
  1573. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1574. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1575. system(tmpbuf);
  1576. }
  1577. cnt_pingDNS_Fail = 0;
  1578. }
  1579. else
  1580. {
  1581. cnt_pingDNS_Fail++;
  1582. }
  1583. }
  1584. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  1585. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  1586. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  1587. (ShmOCPP16Data->OcppConnStatus != PASS))
  1588. {
  1589. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1590. }
  1591. else
  1592. {
  1593. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  1594. }
  1595. //============================================================
  1596. // Priority for internet 0 : First / 1 : Second / 2: Third
  1597. //============================================================
  1598. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  1599. {
  1600. system("/sbin/ifmetric eth0 0");
  1601. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1602. {
  1603. system("/sbin/ifmetric mlan0 1");
  1604. }
  1605. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1606. {
  1607. system("/sbin/ifmetric ppp0 2");
  1608. }
  1609. }
  1610. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  1611. {
  1612. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1613. {
  1614. system("/sbin/ifmetric eth0 1");
  1615. system("/sbin/ifmetric mlan0 0");
  1616. }
  1617. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1618. {
  1619. system("/sbin/ifmetric ppp0 2");
  1620. }
  1621. }
  1622. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  1623. {
  1624. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1625. {
  1626. system("/sbin/ifmetric mlan0 2");
  1627. }
  1628. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1629. {
  1630. system("/sbin/ifmetric eth0 1");
  1631. system("/sbin/ifmetric ppp0 0");
  1632. }
  1633. }
  1634. sleep(5);
  1635. }
  1636. }
  1637. DEBUG_INFO("Initial Ethernet OK\n");
  1638. }
  1639. int SpawnTask()
  1640. {
  1641. system ("pkill Module_");
  1642. system ("pkill OcppBackend");
  1643. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1644. {
  1645. system("/root/Module_4g &");
  1646. }
  1647. else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1648. {
  1649. system("/root/Module_Wifi &");
  1650. }
  1651. system("/root/Module_EventLogging &");
  1652. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1653. {
  1654. ocpp_process_start();
  1655. }
  1656. else
  1657. {
  1658. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  1659. ocpp_process_start();
  1660. }
  1661. system ("/root/Module_AlarmDetect &");
  1662. system ("/root/Module_InternalComm &");
  1663. system ("/root/Module_Speaker &");
  1664. system ("/root/Module_ProduceUtils &");
  1665. return PASS;
  1666. }
  1667. int Initialization()
  1668. {
  1669. int result = PASS;
  1670. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1671. InitGPIO();
  1672. InitEthernet();
  1673. if(DB_Open(localDb) != PASS)
  1674. {
  1675. DEBUG_ERROR("Local database initial fail.\n");
  1676. result = FAIL;
  1677. }
  1678. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  1679. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1680. if((rfidFd = InitRfidPort()) == FAIL)
  1681. {
  1682. DEBUG_ERROR("RFID port initial fail.\n");
  1683. result = FAIL;
  1684. }
  1685. if((wtdFd = InitWatchDog()) == FAIL)
  1686. {
  1687. DEBUG_ERROR("Watchdog initial fail.\n");
  1688. result = FAIL;
  1689. }
  1690. if(result == PASS)
  1691. DEBUG_INFO("Initialization OK.\n");
  1692. else
  1693. DEBUG_INFO("Initialization Fail.\n");
  1694. return result;
  1695. }
  1696. //=====================================================
  1697. // Common routine
  1698. //=====================================================
  1699. char* getSystemModeName(unsigned char mode)
  1700. {
  1701. char* result;
  1702. switch(mode)
  1703. {
  1704. case SYS_MODE_BOOTING:
  1705. result = "booting";
  1706. break;
  1707. case SYS_MODE_IDLE:
  1708. result = "idle";
  1709. break;
  1710. case SYS_MODE_AUTHORIZING:
  1711. result = "authorizing";
  1712. break;
  1713. case SYS_MODE_PREPARING:
  1714. result = "preparing";
  1715. break;
  1716. case SYS_MODE_CHARGING:
  1717. result = "charging";
  1718. break;
  1719. case SYS_MODE_TERMINATING:
  1720. result = "terminating";
  1721. break;
  1722. case SYS_MODE_COMPLETE:
  1723. result = "complete";
  1724. break;
  1725. case SYS_MODE_ALARM:
  1726. result = "alarm";
  1727. break;
  1728. case SYS_MODE_FAULT:
  1729. result = "fault";
  1730. break;
  1731. case SYS_MODE_MAINTAIN:
  1732. result = "maintain";
  1733. break;
  1734. case SYS_MODE_RESERVATION:
  1735. result = "reservation";
  1736. break;
  1737. case SYS_MODE_BOOKING:
  1738. result = "booking";
  1739. break;
  1740. case SYS_MODE_DEBUG:
  1741. result = "debug";
  1742. break;
  1743. case SYS_MODE_UPDATE:
  1744. result = "upgrade";
  1745. break;
  1746. default:
  1747. result = "unknown";
  1748. break;
  1749. }
  1750. return result;
  1751. }
  1752. void setChargerMode(unsigned char gun_index, unsigned char mode)
  1753. {
  1754. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1755. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  1756. DEBUG_INFO("Gun-%02d mode switch from %s to %s\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
  1757. }
  1758. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  1759. {
  1760. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  1761. }
  1762. unsigned char isModeChange(unsigned char gun_index)
  1763. {
  1764. unsigned char result = NO;
  1765. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  1766. {
  1767. result = YES;
  1768. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1769. }
  1770. return result;
  1771. }
  1772. void gpio_set_value(unsigned int gpio, unsigned int value)
  1773. {
  1774. int fd;
  1775. char buf[256];
  1776. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1777. fd = open(buf, O_WRONLY);
  1778. if (fd < 0)
  1779. {
  1780. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  1781. return;
  1782. }
  1783. if (value)
  1784. write(fd, "1", 2);
  1785. else
  1786. write(fd, "0", 2);
  1787. close(fd);
  1788. }
  1789. int gpio_get_value(unsigned int gpio)
  1790. {
  1791. int fd;
  1792. char buf[256];
  1793. char ch;
  1794. int8_t result = FAIL;
  1795. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1796. fd = open(buf, O_RDONLY);
  1797. if (fd < 0)
  1798. {
  1799. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  1800. return result;
  1801. }
  1802. read(fd, &ch, 1);
  1803. if (ch != '0')
  1804. result = 1;
  1805. else
  1806. result = 0;
  1807. close(fd);
  1808. return result;
  1809. }
  1810. //===============================================
  1811. // Get firmware version
  1812. //===============================================
  1813. void get_firmware_version(unsigned char gun_index)
  1814. {
  1815. FILE *fp;
  1816. char cmd[512];
  1817. char buf[512];
  1818. // Get CSU hardware version
  1819. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  1820. // Get CSU boot loader version
  1821. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1822. // Get CSU kernel version
  1823. sprintf(cmd, "/bin/uname -r");
  1824. fp = popen(cmd, "r");
  1825. if(fp == NULL)
  1826. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1827. else
  1828. {
  1829. while(fgets(buf, sizeof(buf), fp) != NULL)
  1830. {
  1831. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  1832. }
  1833. }
  1834. // Get MCU firmware version
  1835. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1836. // Get CSU root file system version
  1837. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.53.00.0000.00");
  1838. // Get AC connector type from model name
  1839. for(uint8_t idx=0;idx<3;idx++)
  1840. {
  1841. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1842. {
  1843. case '1':
  1844. // J1772 Plug
  1845. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1846. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1847. break;
  1848. case '2':
  1849. // J1772 Socket
  1850. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1851. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1852. break;
  1853. case '3':
  1854. // CE Plug
  1855. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1856. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1857. break;
  1858. case '4':
  1859. // CE Socket
  1860. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1861. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1862. break;
  1863. case '5':
  1864. // GB Plug
  1865. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1866. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1867. break;
  1868. case '6':
  1869. // GB Socket
  1870. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1871. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1872. break;
  1873. }
  1874. }
  1875. // Get network option from model name
  1876. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1877. {
  1878. case 'B':
  1879. case 'U':
  1880. //Blue tooth
  1881. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1882. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1883. break;
  1884. case 'W':
  1885. // WIFI
  1886. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1887. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1888. break;
  1889. case 'T':
  1890. // 3G/4G
  1891. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1892. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1893. break;
  1894. case 'D':
  1895. // WIFI + 4G
  1896. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1897. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  1898. break;
  1899. default:
  1900. // LAN
  1901. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1902. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1903. break;
  1904. }
  1905. // Get rating power from model name
  1906. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1907. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1908. // Get vender code from model name
  1909. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1910. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1911. DEBUG_INFO("========================================\n");
  1912. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1913. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  1914. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  1915. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  1916. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1917. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1918. DEBUG_INFO("========================================\n");
  1919. }
  1920. //===============================================
  1921. // Upgrade firmware
  1922. //===============================================
  1923. int upgrade_check()
  1924. {
  1925. int result = PASS;
  1926. int fd;
  1927. DIR *dir;
  1928. struct dirent *file;
  1929. char cmd[512];
  1930. long int MaxLen=48*1024*1024;
  1931. if ((dir = opendir ("/mnt")) != NULL)
  1932. {
  1933. /* print all the files and directories within directory */
  1934. while ((file = readdir (dir)) != NULL)
  1935. {
  1936. if(strlen(file->d_name)>2)
  1937. {
  1938. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1939. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1940. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1941. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1942. {
  1943. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1944. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1945. read(fd, ptr, MaxLen);
  1946. close(fd);
  1947. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1948. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1949. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  1950. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1951. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1952. {
  1953. switch(ShmCharger->fwUpgradeInfo.fwType)
  1954. {
  1955. case CSU_BOOTLOADER:
  1956. case CSU_KERNEL_CONFIGURATION:
  1957. case CSU_KERNEL_IMAGE:
  1958. case CSU_ROOT_FILE_SYSTEM:
  1959. case CSU_USER_CONFIGURATION:
  1960. case CSU_PRIMARY_CONTROLLER:
  1961. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1962. {
  1963. result = FAIL;
  1964. }
  1965. else
  1966. {
  1967. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  1968. {
  1969. DEBUG_INFO("Restore model name & serial number.\n");
  1970. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1971. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1972. {
  1973. DEBUG_INFO("Re-load configuration fail.\n");
  1974. result = FAIL;
  1975. }
  1976. else
  1977. {
  1978. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  1979. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  1980. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  1981. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1982. {
  1983. DEBUG_INFO("Re-write configuration fail.\n");
  1984. result = FAIL;
  1985. }
  1986. else
  1987. DEBUG_INFO("Re-write configuration OK.\n");
  1988. }
  1989. }
  1990. }
  1991. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1992. system(cmd);
  1993. break;
  1994. case AC_WALLMOUNT_CONTROLLER:
  1995. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1996. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1997. break;
  1998. default:
  1999. result = FAIL;
  2000. DEBUG_WARN("Image file is unknown type.\n");
  2001. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2002. system(cmd);
  2003. break;
  2004. }
  2005. }
  2006. else
  2007. {
  2008. result = FAIL;
  2009. DEBUG_ERROR("Model name and Firmware type error.\n");
  2010. }
  2011. free(ptr);
  2012. }
  2013. else
  2014. {
  2015. result = FAIL;
  2016. DEBUG_ERROR("New firmware open error.\n");
  2017. }
  2018. }
  2019. else
  2020. {
  2021. if(strlen(file->d_name) >= 3)
  2022. {
  2023. result = FAIL;
  2024. DEBUG_ERROR("File name error.\n");
  2025. }
  2026. else
  2027. {
  2028. DEBUG_ERROR("Searching file.\n");
  2029. }
  2030. }
  2031. }
  2032. closedir (dir);
  2033. }
  2034. else
  2035. {
  2036. result = FAIL;
  2037. DEBUG_ERROR("/mnt does not valid.\n");
  2038. }
  2039. return result;
  2040. }
  2041. //===============================================
  2042. // Check RFID is match with start user
  2043. //===============================================
  2044. int isMatchStartUser(unsigned char gun_index)
  2045. {
  2046. uint8_t tmpUser[32];
  2047. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2048. {
  2049. // Big endian
  2050. switch(rfid.snType)
  2051. {
  2052. case RFID_SN_TYPE_6BYTE:
  2053. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2054. break;
  2055. case RFID_SN_TYPE_7BYTE:
  2056. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2057. break;
  2058. case RFID_SN_TYPE_10BYTE:
  2059. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2060. break;
  2061. case RFID_SN_TYPE_4BYTE:
  2062. default:
  2063. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2064. break;
  2065. }
  2066. }
  2067. else
  2068. {
  2069. // Little endian
  2070. switch(rfid.snType)
  2071. {
  2072. case RFID_SN_TYPE_6BYTE:
  2073. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2074. break;
  2075. case RFID_SN_TYPE_7BYTE:
  2076. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2077. break;
  2078. case RFID_SN_TYPE_10BYTE:
  2079. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2080. break;
  2081. case RFID_SN_TYPE_4BYTE:
  2082. default:
  2083. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2084. break;
  2085. }
  2086. }
  2087. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2088. }
  2089. //===============================================
  2090. // Read RFID Serial Number
  2091. //===============================================
  2092. int GetCardSerialNumber()
  2093. {
  2094. int isSuccess = FAIL;
  2095. int module_type = MODULE_EWT;
  2096. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2097. {
  2098. if(rfid.cardType == ISO14443A)
  2099. {
  2100. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2101. {
  2102. isSuccess = PASS;
  2103. }
  2104. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2105. {
  2106. isSuccess = PASS;
  2107. }
  2108. sethaltCard(rfidFd,module_type);
  2109. }
  2110. else if(rfid.cardType == IS014443B)
  2111. {
  2112. isSuccess = PASS;
  2113. }
  2114. else if(rfid.cardType == FELICA)
  2115. {
  2116. isSuccess = PASS;
  2117. }
  2118. else
  2119. {}
  2120. }
  2121. else
  2122. {}
  2123. return isSuccess;
  2124. }
  2125. //===============================================
  2126. // Set led motion
  2127. //===============================================
  2128. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2129. {
  2130. switch(led_mode)
  2131. {
  2132. case LED_ACTION_INIT:
  2133. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2134. break;
  2135. case LED_ACTION_IDLE:
  2136. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2137. break;
  2138. case LED_ACTION_AUTHED:
  2139. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2140. break;
  2141. case LED_ACTION_CONNECTED:
  2142. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2143. break;
  2144. case LED_ACTION_CHARGING:
  2145. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2146. break;
  2147. case LED_ACTION_STOP:
  2148. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2149. break;
  2150. case LED_ACTION_ALARM:
  2151. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2152. break;
  2153. case LED_ACTION_MAINTAIN:
  2154. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2155. break;
  2156. case LED_ACTION_RFID_PASS:
  2157. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2158. break;
  2159. case LED_ACTION_RFID_FAIL:
  2160. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2161. break;
  2162. case LED_ACTION_BLE_CONNECT:
  2163. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2164. break;
  2165. case LED_ACTION_BLE_DISABLE:
  2166. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2167. break;
  2168. case LED_ACTION_DEBUG:
  2169. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2170. break;
  2171. case LED_ACTION_ALL_OFF:
  2172. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2173. break;
  2174. case LED_RELAY_ON:
  2175. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2176. break;
  2177. case LED_RELAY_OFF:
  2178. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2179. break;
  2180. case LED_ACTION_HANDSHAKE_FAIL:
  2181. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2182. break;
  2183. case LED_ACTION_INTERNET_DISCONNECT:
  2184. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2185. break;
  2186. }
  2187. }
  2188. //===============================================
  2189. // Relay on/off request set
  2190. //===============================================
  2191. void setRelay(unsigned char gun_index,unsigned char isOn)
  2192. {
  2193. if(isOn == ON)
  2194. {
  2195. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2196. {
  2197. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2198. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  2199. }
  2200. }
  2201. else
  2202. {
  2203. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2204. {
  2205. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2206. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  2207. }
  2208. }
  2209. }
  2210. //===============================================
  2211. // Relay on/off request get
  2212. //===============================================
  2213. int getRelay(unsigned char gun_index)
  2214. {
  2215. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2216. }
  2217. //===============================================
  2218. // Set speaker on/off request
  2219. //===============================================
  2220. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2221. {
  2222. if(isOn == ON)
  2223. {
  2224. ShmCharger->isSpeakerOn = ON;
  2225. ShmCharger->speaker_type = speaker_mode;
  2226. }
  2227. }
  2228. //===============================================
  2229. // Initialization RFID communication port
  2230. //===============================================
  2231. int InitRfidPort()
  2232. {
  2233. int uartO2 = open(rfidPortName, O_RDWR);
  2234. struct termios tios;
  2235. if (uartO2 != FAIL)
  2236. {
  2237. ioctl (uartO2, TCGETS, &tios);
  2238. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2239. tios.c_lflag = 0;
  2240. tios.c_iflag = 0;
  2241. tios.c_oflag = 0;
  2242. tios.c_cc[VMIN] = 0;
  2243. tios.c_cc[VTIME] = (unsigned char) 1;
  2244. tios.c_lflag = 0;
  2245. tcflush(uartO2, TCIFLUSH);
  2246. ioctl(uartO2, TCSETS, &tios);
  2247. }
  2248. if (uartO2 < 0)
  2249. {
  2250. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2251. }
  2252. return uartO2;
  2253. }
  2254. //===============================================
  2255. // Check internet access status
  2256. //===============================================
  2257. int isReachableInternet()
  2258. {
  2259. int result = FAIL;
  2260. FILE *fp;
  2261. char cmd[256];
  2262. char buf[512];
  2263. char tmp[512];
  2264. strcpy(cmd, "ifconfig eth0");
  2265. fp = popen(cmd, "r");
  2266. if(fp != NULL)
  2267. {
  2268. while(fgets(buf, sizeof(buf), fp) != NULL)
  2269. {
  2270. if (strstr(buf, "inet addr:") > 0)
  2271. {
  2272. sscanf(buf, "%*s%s", tmp);
  2273. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2274. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  2275. {
  2276. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  2277. }
  2278. sscanf(buf, "%*s%*s%*s%s", tmp);
  2279. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2280. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  2281. {
  2282. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  2283. }
  2284. }
  2285. }
  2286. }
  2287. pclose(fp);
  2288. memset(buf, 0x00, sizeof(buf));
  2289. // Get gateway
  2290. fp = popen("ip route", "r");
  2291. if(fp == NULL)
  2292. result = FAIL;
  2293. else
  2294. {
  2295. while(fgets(buf, sizeof(buf), fp) != NULL)
  2296. {
  2297. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2298. break;
  2299. }
  2300. if(strstr(buf, "default") != NULL)
  2301. {
  2302. sscanf(buf, "%*s%*s%s", tmp);
  2303. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2304. }
  2305. }
  2306. pclose(fp);
  2307. memset(buf, 0x00, sizeof(buf));
  2308. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2309. {
  2310. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2311. fp = popen(cmd, "r");
  2312. if(fp != NULL)
  2313. {
  2314. while(fgets(buf, sizeof(buf), fp) != NULL)
  2315. {
  2316. if(strstr(buf, "transmitted") > 0)
  2317. {
  2318. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2319. if(strstr(buf,"100%") != NULL)
  2320. {
  2321. }
  2322. else
  2323. {
  2324. result = PASS;
  2325. }
  2326. //DEBUG_INFO("%s",buf);
  2327. //DEBUG_INFO("%s\n",tmp);
  2328. }
  2329. }
  2330. }
  2331. pclose(fp);
  2332. }
  2333. return result;
  2334. }
  2335. //===============================================
  2336. // Check route for eth0
  2337. //===============================================
  2338. int isRouteFail()
  2339. {
  2340. int result = YES;
  2341. FILE *fp;
  2342. char buf[512];
  2343. fp = popen("route -n", "r");
  2344. if(fp != NULL)
  2345. {
  2346. while(fgets(buf, sizeof(buf), fp) != NULL)
  2347. {
  2348. if(strstr(buf, "eth0") != NULL)
  2349. result = NO;
  2350. }
  2351. }
  2352. pclose(fp);
  2353. return result;
  2354. }
  2355. //===============================================
  2356. // Check reservation date is expired
  2357. //===============================================
  2358. int isReservationExpired(unsigned char gun_index)
  2359. {
  2360. int result = NO;
  2361. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  2362. result = YES;
  2363. return result;
  2364. }
  2365. //===============================================
  2366. // Check charging profile related date routine
  2367. //===============================================
  2368. int isProfileValid(uint8_t gun_index)
  2369. {
  2370. int result = NO;
  2371. struct tm tmFrom, tmTo;
  2372. struct timeb tbFrom, tbTo;
  2373. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2374. {
  2375. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  2376. (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  2377. {
  2378. tmFrom.tm_year -= 1900;
  2379. tmFrom.tm_mon -= 1;
  2380. tbFrom.time = mktime(&tmFrom);
  2381. tmTo.tm_year -= 1900;
  2382. tmTo.tm_mon -= 1;
  2383. tbTo.time = mktime(&tmTo);
  2384. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2385. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2386. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2387. {
  2388. result = YES;
  2389. }
  2390. }
  2391. else
  2392. {
  2393. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2394. }
  2395. }
  2396. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2397. {
  2398. }
  2399. return result;
  2400. }
  2401. int getScheduleStart(int gun_index)
  2402. {
  2403. int result = -1;
  2404. struct tm tmScheduleStart;;
  2405. struct timeb tbScheduleStart;
  2406. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2407. {
  2408. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%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))
  2409. {
  2410. tmScheduleStart.tm_year -= 1900;
  2411. tmScheduleStart.tm_mon -= 1;
  2412. tbScheduleStart.time = mktime(&tmScheduleStart);
  2413. tbScheduleStart.millitm = 0;
  2414. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2415. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2416. }
  2417. else
  2418. {
  2419. DEBUG_WARN("Schedule start date parsing error.\n");
  2420. }
  2421. }
  2422. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2423. {
  2424. }
  2425. return result;
  2426. }
  2427. int getStartSinceToday()
  2428. {
  2429. int result = -1;
  2430. time_t t;
  2431. struct tm *tmStartToday;
  2432. struct timeb tbStartToday;
  2433. t=time(NULL);
  2434. tmStartToday=localtime(&t);
  2435. tmStartToday->tm_hour = 0;
  2436. tmStartToday->tm_min = 0;
  2437. tmStartToday->tm_sec = 0;
  2438. tbStartToday.time = mktime(tmStartToday);
  2439. result = DiffTimebWithNow(tbStartToday)/1000;
  2440. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  2441. return result;
  2442. }
  2443. int getStartSinceWeek()
  2444. {
  2445. int result = -1;
  2446. time_t t;
  2447. struct tm *tmStartWeek;
  2448. struct timeb tbStartWeek;
  2449. t=time(NULL);
  2450. tmStartWeek=localtime(&t);
  2451. t-=86400*tmStartWeek->tm_wday;
  2452. tmStartWeek=localtime(&t);
  2453. tmStartWeek->tm_hour = 0;
  2454. tmStartWeek->tm_min = 0;
  2455. tmStartWeek->tm_sec = 0;
  2456. tbStartWeek.time = mktime(tmStartWeek);
  2457. result = DiffTimebWithNow(tbStartWeek)/1000;
  2458. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  2459. return result;
  2460. }
  2461. //===============================================
  2462. // Valid from local white list
  2463. //===============================================
  2464. int isValidLocalWhiteCard()
  2465. {
  2466. uint8_t result = FAIL;
  2467. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  2468. {
  2469. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  2470. {
  2471. result = PASS;
  2472. }
  2473. }
  2474. return result;
  2475. }
  2476. //==========================================
  2477. // Check routine
  2478. //==========================================
  2479. void checkTask()
  2480. {
  2481. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2482. {
  2483. if(system("pidof -s Module_4g > /dev/null") != 0)
  2484. {
  2485. DEBUG_INFO("Module_4g not running, restart it.\n");
  2486. system("/root/Module_4g &");
  2487. }
  2488. }
  2489. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2490. {
  2491. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  2492. {
  2493. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  2494. system("/root/Module_Wifi &");
  2495. }
  2496. }
  2497. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  2498. {
  2499. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  2500. system("/root/Module_EventLogging &");
  2501. }
  2502. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2503. {
  2504. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2505. {
  2506. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  2507. {
  2508. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  2509. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2510. system("pkill OcppBackend");
  2511. sleep(3);
  2512. ocpp_process_start();
  2513. }
  2514. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2515. {
  2516. DEBUG_INFO("OcppBackend not running, restart it.\n");
  2517. ocpp_process_start();
  2518. }
  2519. }
  2520. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2521. {
  2522. }
  2523. }
  2524. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  2525. {
  2526. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  2527. system("/root/Module_AlarmDetect &");
  2528. }
  2529. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  2530. {
  2531. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  2532. system("/root/Module_InternalComm &");
  2533. }
  2534. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  2535. {
  2536. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  2537. system("/root/Module_Speaker &");
  2538. }
  2539. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  2540. {
  2541. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  2542. system ("/root/Module_ProduceUtils &");
  2543. }
  2544. }
  2545. void checkConnectionTimeout()
  2546. {
  2547. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  2548. {
  2549. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2550. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  2551. }
  2552. else
  2553. {
  2554. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  2555. {
  2556. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2557. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  2558. {
  2559. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2560. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  2561. }
  2562. else
  2563. {
  2564. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  2565. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  2566. }
  2567. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2568. }
  2569. else
  2570. {
  2571. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2572. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\n.");
  2573. }
  2574. //DEBUG_INFO("Present timeout spec : %d \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  2575. }
  2576. }
  2577. void checkReset()
  2578. {
  2579. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2580. {
  2581. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2582. {
  2583. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2584. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2585. {
  2586. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2587. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2588. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2589. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2590. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2591. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2592. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2593. {
  2594. system("sync");
  2595. sleep(5);
  2596. system("reboot -f");
  2597. sleep(5);
  2598. system("reboot -f");
  2599. }
  2600. else
  2601. {
  2602. sleep(5);
  2603. close(wtdFd);
  2604. system("/usr/bin/run_evse_restart.sh");
  2605. }
  2606. }
  2607. }
  2608. }
  2609. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2610. {
  2611. }
  2612. }
  2613. void checkReservation(uint8_t gun_index)
  2614. {
  2615. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2616. {
  2617. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2618. {
  2619. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2620. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2621. {
  2622. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2623. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2624. }
  2625. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2626. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2627. }
  2628. }
  2629. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2630. {
  2631. }
  2632. }
  2633. void checkUnlocker(uint8_t gun_index)
  2634. {
  2635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2636. {
  2637. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2638. {
  2639. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2640. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2641. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2642. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2643. }
  2644. }
  2645. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2646. {
  2647. }
  2648. }
  2649. void checkAvailability(uint8_t gun_index)
  2650. {
  2651. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2652. {
  2653. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2654. {
  2655. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2656. {
  2657. DB_Update_Operactive(localDb, gun_index, true);
  2658. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2659. }
  2660. else
  2661. {
  2662. DB_Update_Operactive(localDb, gun_index, false);
  2663. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2664. }
  2665. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2666. }
  2667. }
  2668. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2669. {
  2670. }
  2671. }
  2672. void checkChargingProfileLimit(uint8_t gun_index)
  2673. {
  2674. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2675. {
  2676. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2677. (ocpp_get_profile_req(gun_index) != ON) &&
  2678. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2679. {
  2680. // Debug information
  2681. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2682. {
  2683. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2684. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2685. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2686. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2687. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2688. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2689. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2690. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2691. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2692. DEBUG_INFO("========================================\n");
  2693. }
  2694. // Checking profile kind
  2695. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2696. {
  2697. // Absolute profile
  2698. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2699. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2700. {
  2701. // Checking limitation
  2702. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2703. {
  2704. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2705. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  2706. )
  2707. {
  2708. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  2709. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2710. }
  2711. else
  2712. break;
  2713. }
  2714. }
  2715. }
  2716. }
  2717. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2718. {
  2719. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2720. }
  2721. }
  2722. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2723. {
  2724. }
  2725. }
  2726. //===============================================
  2727. // Set Light Internsity from ocpp
  2728. //===============================================
  2729. void setLightIntensity(unsigned char gun_index)
  2730. {
  2731. switch(atoi((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[LightIntensity].ItemData))
  2732. {
  2733. case 0 ... 15:
  2734. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x00)
  2735. {
  2736. // 12 sector change to level 0
  2737. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x00;
  2738. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x00;
  2739. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x00;
  2740. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x00;
  2741. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x00;
  2742. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x00;
  2743. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x00;
  2744. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x00;
  2745. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x00;
  2746. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x00;
  2747. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x00;
  2748. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x00;
  2749. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x00;
  2750. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2751. DEBUG_INFO("Light intensity level : [%X] == Level 0.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2752. }
  2753. break;
  2754. case 16 ... 35:
  2755. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x11)
  2756. {
  2757. // 12 sector change to level 1
  2758. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x11;
  2759. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x11;
  2760. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x11;
  2761. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x11;
  2762. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x11;
  2763. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x11;
  2764. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x11;
  2765. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x11;
  2766. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x11;
  2767. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x11;
  2768. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x11;
  2769. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x11;
  2770. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x11;
  2771. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2772. DEBUG_INFO("Light intensity level : [%X] == Level 1.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2773. }
  2774. break;
  2775. case 36 ... 75:
  2776. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x22)
  2777. {
  2778. // 12 sector change to level 2
  2779. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x22;
  2780. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x22;
  2781. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x22;
  2782. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x22;
  2783. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x22;
  2784. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x22;
  2785. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x22;
  2786. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x22;
  2787. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x22;
  2788. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x22;
  2789. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x22;
  2790. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x22;
  2791. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x22;
  2792. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2793. DEBUG_INFO("Light intensity level : [%X] == Level 2.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2794. }
  2795. break;
  2796. case 76 ... 100:
  2797. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x33)
  2798. {
  2799. // 12 sector change to level 3
  2800. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x33;
  2801. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x33;
  2802. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x33;
  2803. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x33;
  2804. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x33;
  2805. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x33;
  2806. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x33;
  2807. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x33;
  2808. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x33;
  2809. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x33;
  2810. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x33;
  2811. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x33;
  2812. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x33;
  2813. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2814. DEBUG_INFO("Light intensity level : [%X] == Level 3.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2815. }
  2816. break;
  2817. default:
  2818. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 2)
  2819. {
  2820. // 12 sector change to level 2
  2821. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x22;
  2822. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x22;
  2823. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x22;
  2824. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x22;
  2825. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x22;
  2826. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x22;
  2827. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x22;
  2828. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x22;
  2829. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x22;
  2830. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x22;
  2831. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x22;
  2832. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x22;
  2833. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x22;
  2834. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2835. DEBUG_INFO("Default light intensity level : [%X] == Level 2.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2836. }
  2837. break;
  2838. }
  2839. }
  2840. void initialLightIntensity(unsigned char gun_index)
  2841. {
  2842. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0xff;
  2843. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0xff;
  2844. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0xff;
  2845. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0xff;
  2846. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0xff;
  2847. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0xff;
  2848. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0xff;
  2849. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0xff;
  2850. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0xff;
  2851. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0xff;
  2852. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0xff;
  2853. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0xff;
  2854. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0xff;
  2855. DEBUG_INFO("Initial light intensity gun_index %d to : [%X] .\r\n", gun_index, ShmCharger->gun_info[0].LightIntensityLevel);
  2856. }
  2857. void checkStopReason(uint8_t gun_index)
  2858. {
  2859. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2860. {
  2861. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2862. if(!ocpp_get_starttransaction_result(gun_index))
  2863. {
  2864. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  2865. }
  2866. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  2867. {
  2868. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  2869. }
  2870. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2871. {
  2872. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  2873. }
  2874. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2875. {
  2876. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  2877. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  2878. else
  2879. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  2880. }
  2881. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2882. {
  2883. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  2884. if(!isMatchStartUser(gun_index))
  2885. {
  2886. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2887. }
  2888. else
  2889. {
  2890. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2891. }
  2892. DEBUG_INFO("Gun-%d [ IdTag ] : %s \n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  2893. }
  2894. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  2895. {
  2896. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  2897. }
  2898. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  2899. {
  2900. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  2901. }
  2902. else
  2903. {
  2904. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  2905. }
  2906. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  2907. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  2908. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  2909. }
  2910. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2911. {
  2912. }
  2913. }
  2914. void checkRemoteUpgradeStatus()
  2915. {
  2916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2917. {
  2918. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  2919. {
  2920. DEBUG_INFO("Firmware remote upgraded fail...\n");
  2921. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2922. ShmCharger->isUpdateSuccess = NO;
  2923. }
  2924. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  2925. {
  2926. DEBUG_INFO("Firmware remote upgrading...\n");
  2927. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  2928. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2929. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2930. int result = upgrade_check();
  2931. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  2932. if(result == PASS)
  2933. {
  2934. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  2935. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  2936. ShmCharger->isUpdateSuccess = YES;
  2937. DEBUG_INFO("Remote update success...\n");
  2938. }
  2939. else
  2940. {
  2941. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  2942. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  2943. ShmCharger->isUpdateSuccess = NO;
  2944. DEBUG_INFO("Remote update unsuccess...\n");
  2945. }
  2946. }
  2947. else
  2948. {}
  2949. }
  2950. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2951. {
  2952. }
  2953. }
  2954. //===============================================
  2955. // Main process
  2956. //===============================================
  2957. int main(void)
  2958. {
  2959. //Create all share memory
  2960. if(CreatShareMemory()==0)
  2961. {
  2962. DEBUG_ERROR("CreatShareMemory NG\n");
  2963. if(ShmStatusCodeData!=NULL)
  2964. {
  2965. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  2966. }
  2967. sleep(5);
  2968. system("reboot -f");
  2969. sleep(5);
  2970. system("reboot -f");
  2971. }
  2972. else
  2973. {
  2974. DEBUG_INFO("CreatShareMemory OK\n");
  2975. }
  2976. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2977. {
  2978. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  2979. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  2980. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  2981. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  2982. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  2983. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  2984. initialLightIntensity(gun_index);
  2985. }
  2986. // Main loop
  2987. for(;;)
  2988. {
  2989. //==========================================
  2990. // Synchronize share memory from OCPP struct
  2991. //==========================================
  2992. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  2993. //==============================================
  2994. // Period check for 10 seconds
  2995. //==============================================
  2996. if((DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10000) || (DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  2997. {
  2998. //==============================================
  2999. // Check task processing
  3000. //==============================================
  3001. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3002. checkTask();
  3003. //==============================================
  3004. // Check connection timeout specification
  3005. //==============================================
  3006. checkConnectionTimeout();
  3007. ftime(&startTime[0][TMR_IDX_CHECK_TASK]);
  3008. }
  3009. //==========================================
  3010. // Something need run in Idle mode
  3011. //==========================================
  3012. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  3013. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  3014. {
  3015. //======================================
  3016. // Check restore factory setting request
  3017. //======================================
  3018. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3019. {
  3020. // Set led to initial
  3021. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3022. {
  3023. setLedMotion(gun_index,LED_ACTION_INIT);
  3024. }
  3025. sleep(5);
  3026. system("cd /root;./Module_FactoryConfig -m");
  3027. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3028. system("sync");
  3029. sleep(5);
  3030. system("reboot -f");
  3031. sleep(5);
  3032. system("reboot -f");
  3033. }
  3034. //======================================
  3035. // Check upgrade firmware request
  3036. //======================================
  3037. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3038. ocpp_get_update_firmware_req())
  3039. {
  3040. // If available memory too small, free memory cache first
  3041. if(getAvailableMemory() < (200*1024*1024))
  3042. {
  3043. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3044. system("echo 3 > /proc/sys/vm/drop_caches");
  3045. }
  3046. ShmCharger->isUpdateSuccess = NO;
  3047. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3048. {
  3049. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3050. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3051. }
  3052. }
  3053. }
  3054. //==========================================
  3055. // Check remote reset request
  3056. //==========================================
  3057. checkReset();
  3058. //==========================================
  3059. // Check RFID
  3060. //==========================================
  3061. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  3062. {
  3063. if(GetCardSerialNumber()!= FAIL)
  3064. {
  3065. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3066. }
  3067. }
  3068. //==========================================
  3069. // Connector loop
  3070. //==========================================
  3071. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3072. {
  3073. // Synchronize current rating value from MCU
  3074. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3075. // Assign connector location index for OCPP
  3076. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  3077. // Synchronize present charging power
  3078. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3079. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  3080. else
  3081. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3082. // The system switch to Booting mode
  3083. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == 0XFF)
  3084. {
  3085. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3086. }
  3087. // Set light intensity
  3088. setLightIntensity(gun_index);
  3089. // Check initialization "PASS" or "FAIL"
  3090. if(ShmCharger->gun_info[gun_index].isInitialPass == YES)
  3091. {
  3092. // Alarm event check
  3093. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  3094. {
  3095. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  3096. {
  3097. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE)
  3098. {
  3099. }
  3100. else
  3101. {
  3102. setChargerMode(gun_index, SYS_MODE_ALARM);
  3103. }
  3104. }
  3105. }
  3106. else
  3107. {
  3108. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM)
  3109. {
  3110. }
  3111. }
  3112. }
  3113. // Reservation request check
  3114. checkReservation(gun_index);
  3115. // Change availability check
  3116. checkAvailability(gun_index);
  3117. if(ShmCharger->gun_info[gun_index].isOperactive)
  3118. {
  3119. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  3120. {
  3121. setChargerMode(gun_index, SYS_MODE_IDLE);
  3122. }
  3123. }
  3124. else
  3125. {
  3126. if(isMode(gun_index, SYS_MODE_IDLE))
  3127. {
  3128. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  3129. }
  3130. }
  3131. // Unlock Connector signal check
  3132. checkUnlocker(gun_index);
  3133. // Connector process
  3134. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  3135. {
  3136. case SYS_MODE_BOOTING:
  3137. if(isModeChange(gun_index))
  3138. {
  3139. setLedMotion(gun_index,LED_ACTION_INIT);
  3140. //CSU Initialization & task spawn
  3141. if((Initialization() != PASS) ||
  3142. (SpawnTask() != PASS))
  3143. {
  3144. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  3145. }
  3146. }
  3147. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  3148. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  3149. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  3150. {
  3151. ShmCharger->gun_selectd = 0;
  3152. // Firmware version
  3153. get_firmware_version(gun_index);
  3154. // OCPP BootNotification info set
  3155. DEBUG_INFO("==========================================\n");
  3156. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  3157. DEBUG_INFO("==========================================\n");
  3158. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  3159. ocpp_boot_info_sync();
  3160. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  3161. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  3162. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  3163. DEBUG_INFO("CpFwVersion: %s\n",ShmOCPP16Data->BootNotification.CpFwVersion);
  3164. DEBUG_INFO("CpMeterSerialNumber: %s\n",ShmOCPP16Data->BootNotification.CpMeterSerialNumber);
  3165. DEBUG_INFO("CpMeterType: %s\n",ShmOCPP16Data->BootNotification.CpMeterType);
  3166. DEBUG_INFO("==========================================\n");
  3167. DEBUG_INFO("========== Set Wifi information ==========\n");
  3168. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  3169. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3170. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3171. DEBUG_INFO("==========================================\n");
  3172. // Flag for initialization system success
  3173. ShmCharger->gun_info[gun_index].isInitialPass = YES;
  3174. // Set max current to rating current
  3175. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3176. // Default Ethernet / Wifi / 4G to 1:disconnected
  3177. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3178. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3179. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3180. // If Web Server OPCC URL is empty kill Module_OcppBackend
  3181. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  3182. {
  3183. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  3184. system ("pkill OcppBackend");
  3185. }
  3186. // If rotate switch equal zero, the system needs to change Debug mode
  3187. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  3188. setChargerMode(gun_index, SYS_MODE_DEBUG);
  3189. else
  3190. setChargerMode(gun_index, SYS_MODE_IDLE);
  3191. }
  3192. break;
  3193. case SYS_MODE_IDLE:
  3194. if(isModeChange(gun_index))
  3195. {
  3196. setLedMotion(gun_index,LED_ACTION_IDLE);
  3197. setRelay(gun_index,OFF);
  3198. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3199. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3200. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3201. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3202. ocpp_set_remotestart(gun_index, OFF);
  3203. ocpp_set_remotestop(gun_index, OFF);
  3204. ocpp_set_auth_conf(OFF);
  3205. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  3206. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  3207. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3208. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  3209. ocpp_set_unlocker_req(gun_index, OFF);
  3210. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  3211. // Response StopTransactionConf
  3212. ocpp_set_stoptransaction_conf(gun_index, OFF);
  3213. }
  3214. if(ocpp_get_connection_status() == ON)
  3215. {
  3216. setLedMotion(gun_index,LED_ACTION_IDLE);
  3217. }
  3218. else
  3219. {
  3220. setLedMotion(gun_index,LED_ACTION_INTERNET_DISCONNECT);
  3221. }
  3222. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  3223. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  3224. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3225. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  3226. (ocpp_get_remotestart(gun_index) == ON) ||
  3227. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  3228. {
  3229. // Clean User id & Card Number
  3230. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3231. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3232. {
  3233. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3234. DEBUG_INFO("Start Method : RFID...\n");
  3235. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3236. {
  3237. // Big endian
  3238. switch(rfid.snType)
  3239. {
  3240. case RFID_SN_TYPE_6BYTE:
  3241. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  3242. break;
  3243. case RFID_SN_TYPE_7BYTE:
  3244. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  3245. break;
  3246. case RFID_SN_TYPE_10BYTE:
  3247. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  3248. break;
  3249. case RFID_SN_TYPE_4BYTE:
  3250. default:
  3251. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3252. break;
  3253. }
  3254. }
  3255. else
  3256. {
  3257. // Little endian
  3258. switch(rfid.snType)
  3259. {
  3260. case RFID_SN_TYPE_6BYTE:
  3261. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3262. break;
  3263. case RFID_SN_TYPE_7BYTE:
  3264. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3265. break;
  3266. case RFID_SN_TYPE_10BYTE:
  3267. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3268. break;
  3269. case RFID_SN_TYPE_4BYTE:
  3270. default:
  3271. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3272. break;
  3273. }
  3274. }
  3275. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3276. }
  3277. else if(ocpp_get_remotestart(gun_index))
  3278. {
  3279. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3280. ocpp_copy_userid_from_remotestart(gun_index);
  3281. DEBUG_INFO("Start Method : BACKEND...\n");
  3282. }
  3283. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  3284. {
  3285. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  3286. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  3287. DEBUG_INFO("Start Method : BLE...\n");
  3288. }
  3289. else
  3290. {
  3291. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  3292. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  3293. DEBUG_INFO("Start Method : FREE...\n");
  3294. }
  3295. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3296. ocpp_set_remotestart(gun_index, OFF);
  3297. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3298. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3299. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  3300. // Get target current
  3301. ocpp_set_profile_req(gun_index, ON);
  3302. sleep(1);
  3303. checkChargingProfileLimit(gun_index);
  3304. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3305. {
  3306. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3307. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3308. }
  3309. else
  3310. {
  3311. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3312. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3313. }
  3314. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3315. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3316. }
  3317. else
  3318. {}
  3319. break;
  3320. case SYS_MODE_AUTHORIZING:
  3321. if(isModeChange(gun_index))
  3322. {
  3323. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3324. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3325. {
  3326. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3327. {
  3328. case AUTH_MODE_ENABLE:
  3329. if(ocpp_get_connection_status())
  3330. {
  3331. // On line
  3332. ocpp_set_auth_req(ON, "ISO14443");
  3333. }
  3334. else
  3335. {
  3336. // Off line
  3337. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3338. {
  3339. case OFF_POLICY_LOCALLIST:
  3340. ocpp_set_auth_req(ON, "ISO14443");
  3341. break;
  3342. case OFF_POLICY_PH_RFID:
  3343. break;
  3344. case OFF_POLICY_FREE:
  3345. break;
  3346. case OFF_POLICY_NOCHARGE:
  3347. default:
  3348. break;
  3349. }
  3350. }
  3351. break;
  3352. case AUTH_MODE_DISABLE:
  3353. default:
  3354. break;
  3355. }
  3356. }
  3357. }
  3358. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  3359. {
  3360. // Authorization timeout process.
  3361. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3362. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3363. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3364. sleep(3);
  3365. setChargerMode(gun_index, SYS_MODE_IDLE);
  3366. DEBUG_WARN("Authorize timeout !!!\n");
  3367. }
  3368. else
  3369. {
  3370. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  3371. {
  3372. case START_METHOD_RFID:
  3373. if((ocpp_get_auth_conf()) ||
  3374. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  3375. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)))
  3376. {
  3377. DEBUG_INFO("ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status: %s \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  3378. if(ocpp_get_auth_result(gun_index) ||
  3379. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  3380. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  3381. {
  3382. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3383. DEBUG_INFO("Authorize pass.\n");
  3384. setSpeaker(ON,SPEAKER_SHORT);
  3385. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3386. sleep(3);
  3387. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3388. }
  3389. else
  3390. {
  3391. DEBUG_INFO("Authorize fail.\n");
  3392. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3393. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3394. sleep(3);
  3395. setChargerMode(gun_index, SYS_MODE_IDLE);
  3396. }
  3397. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3398. ocpp_set_auth_conf(OFF);
  3399. }
  3400. break;
  3401. case START_METHOD_BACKEND:
  3402. case START_METHOD_BLE:
  3403. case START_METHOD_FREE:
  3404. default:
  3405. setSpeaker(ON,SPEAKER_SHORT);
  3406. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3407. break;
  3408. }
  3409. }
  3410. break;
  3411. case SYS_MODE_PREPARING:
  3412. if(isModeChange(gun_index))
  3413. {
  3414. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3415. setRelay(gun_index, ON);
  3416. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3417. }
  3418. // If control pilot detect Bx, skip watch dog time out.
  3419. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  3420. {
  3421. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3422. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3423. }
  3424. // Unplug charging gun to Idle mode
  3425. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  3426. {
  3427. DEBUG_INFO("Charging gun is plugged before.\n");
  3428. setChargerMode(gun_index, SYS_MODE_IDLE);
  3429. }
  3430. // Use RFID card to stop handshaking
  3431. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  3432. {
  3433. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  3434. setSpeaker(ON,SPEAKER_SHORT);
  3435. setChargerMode(gun_index, SYS_MODE_IDLE);
  3436. }
  3437. else
  3438. {
  3439. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3440. }
  3441. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  3442. {
  3443. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  3444. setSpeaker(ON,SPEAKER_INTERVAL_LONG);
  3445. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+3000))
  3446. {
  3447. DEBUG_INFO("Handshaking timeout...\n");
  3448. setChargerMode(gun_index, SYS_MODE_IDLE);
  3449. }
  3450. }
  3451. else if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  3452. {
  3453. setSpeaker(ON,SPEAKER_SHORT);
  3454. ocpp_set_unlocker_req(gun_index, OFF);
  3455. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3456. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3457. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  3458. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3459. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3460. ocpp_copy_userid_to_starttransaction(gun_index);
  3461. ocpp_set_starttransaction_req(gun_index, ON);
  3462. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3463. }
  3464. break;
  3465. case SYS_MODE_CHARGING:
  3466. if(isModeChange(gun_index))
  3467. {
  3468. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3469. ftime(&startChargingTime[gun_index]);
  3470. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3471. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3472. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3473. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3474. ocpp_set_auth_req(OFF);
  3475. ocpp_reset_smartcharging_profileId(gun_index);
  3476. ocpp_set_profile_req(gun_index, ON);
  3477. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3478. }
  3479. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3480. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3481. ocpp_get_remotestop(gun_index) ||
  3482. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  3483. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  3484. ocpp_get_reset_req() ||
  3485. ocpp_get_unlocker_req(gun_index) ||
  3486. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  3487. {
  3488. if(ShmCharger->gun_info[gun_index].rfidReq)
  3489. {
  3490. DEBUG_INFO("Certified in charging mode... \n");
  3491. // If RFID SN different with start user, it need to authorize ID
  3492. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3493. {
  3494. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  3495. {
  3496. // Request authorization
  3497. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3498. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3499. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3500. {
  3501. // Big endian
  3502. switch(rfid.snType)
  3503. {
  3504. case RFID_SN_TYPE_6BYTE:
  3505. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  3506. break;
  3507. case RFID_SN_TYPE_7BYTE:
  3508. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  3509. break;
  3510. case RFID_SN_TYPE_10BYTE:
  3511. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  3512. break;
  3513. case RFID_SN_TYPE_4BYTE:
  3514. default:
  3515. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3516. break;
  3517. }
  3518. }
  3519. else
  3520. {
  3521. // Little endian
  3522. switch(rfid.snType)
  3523. {
  3524. case RFID_SN_TYPE_6BYTE:
  3525. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3526. break;
  3527. case RFID_SN_TYPE_7BYTE:
  3528. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3529. break;
  3530. case RFID_SN_TYPE_10BYTE:
  3531. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3532. break;
  3533. case RFID_SN_TYPE_4BYTE:
  3534. default:
  3535. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3536. break;
  3537. }
  3538. }
  3539. DEBUG_INFO("End request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3540. DEBUG_INFO("Start method : %d\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3541. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3542. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3543. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3544. {
  3545. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3546. {
  3547. case AUTH_MODE_ENABLE:
  3548. if(ocpp_get_connection_status())
  3549. {
  3550. // On line
  3551. ocpp_set_auth_req(ON, "ISO14443");
  3552. }
  3553. else
  3554. {
  3555. // Off line
  3556. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3557. {
  3558. case OFF_POLICY_LOCALLIST:
  3559. ocpp_set_auth_req(ON, "ISO14443");
  3560. break;
  3561. case OFF_POLICY_PH_RFID:
  3562. break;
  3563. case OFF_POLICY_FREE:
  3564. break;
  3565. case OFF_POLICY_NOCHARGE:
  3566. default:
  3567. break;
  3568. }
  3569. }
  3570. break;
  3571. case AUTH_MODE_DISABLE:
  3572. default:
  3573. break;
  3574. }
  3575. }
  3576. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3577. {
  3578. DEBUG_INFO("Should be remote stop charger... \n");
  3579. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3580. }
  3581. }
  3582. else
  3583. {
  3584. if(ocpp_get_auth_conf() ||
  3585. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3586. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
  3587. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3588. {
  3589. if(ocpp_get_auth_result(gun_index) ||
  3590. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3591. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3592. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3593. {
  3594. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3595. }
  3596. else
  3597. {
  3598. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3599. ocpp_set_auth_req(OFF);
  3600. DEBUG_INFO("Authorize fail.\n");
  3601. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3602. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3603. sleep(3);
  3604. }
  3605. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3606. ocpp_set_auth_conf(OFF);
  3607. }
  3608. }
  3609. }
  3610. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  3611. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  3612. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3613. }
  3614. else
  3615. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3616. }
  3617. else
  3618. {
  3619. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3620. ftime(&endChargingTime[gun_index]);
  3621. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  3622. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start)/100.0;
  3623. // Response StartTransactionConf
  3624. ocpp_set_starttransaction_conf(gun_index, OFF);
  3625. // Charging profile preparation
  3626. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  3627. {
  3628. if(!ocpp_get_profile_conf(gun_index))
  3629. {
  3630. ocpp_set_profile_req(gun_index, ON);
  3631. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3632. }
  3633. else
  3634. {
  3635. ocpp_set_profile_conf(gun_index, OFF);
  3636. }
  3637. }
  3638. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  3639. checkChargingProfileLimit(gun_index);
  3640. // If temperature warning derating PWM duty 20%
  3641. if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp >= SPEC_TEMPERATURE_WARN)
  3642. {
  3643. ShmCharger->gun_info[gun_index].tempCoefficient = 0.8;
  3644. }
  3645. else if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp <= SPEC_TEMPERATURE_RECOVER)
  3646. {
  3647. ShmCharger->gun_info[gun_index].tempCoefficient = 1.0;
  3648. }
  3649. ShmCharger->gun_info[gun_index].targetCurrent = (ShmCharger->gun_info[gun_index].targetCurrent==0?ShmCharger->gun_info[gun_index].targetCurrent:(((ShmCharger->gun_info[gun_index].targetCurrent*ShmCharger->gun_info[gun_index].tempCoefficient)>=6) ? (ShmCharger->gun_info[gun_index].targetCurrent*ShmCharger->gun_info[gun_index].tempCoefficient):6));
  3650. // Determine max charging current to MCU
  3651. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3652. {
  3653. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3654. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3655. {
  3656. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3657. {
  3658. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3659. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3660. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3661. }
  3662. else
  3663. {}
  3664. }
  3665. }
  3666. else
  3667. {
  3668. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3669. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3670. {
  3671. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3672. {
  3673. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3674. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3675. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3676. }
  3677. else
  3678. {}
  3679. }
  3680. }
  3681. // Debug information
  3682. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  3683. {
  3684. DEBUG_INFO("=============================================================\n");
  3685. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  3686. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  3687. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  3688. DEBUG_INFO("=============================================================\n");
  3689. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3690. }
  3691. if(ocpp_get_connection_status())
  3692. {
  3693. // On-line max condition check
  3694. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  3695. {
  3696. setRelay(gun_index, OFF);
  3697. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3698. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3699. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  3700. }
  3701. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  3702. {
  3703. setRelay(gun_index, OFF);
  3704. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3705. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3706. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  3707. }
  3708. else
  3709. {
  3710. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3711. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  3712. {
  3713. setLedMotion(gun_index,LED_ACTION_CHARGING);
  3714. setRelay(gun_index, ON);
  3715. }
  3716. else
  3717. {
  3718. setLedMotion(gun_index, LED_ACTION_STOP);
  3719. setRelay(gun_index, OFF);
  3720. }
  3721. }
  3722. }
  3723. else
  3724. {
  3725. // Off-line max condition check
  3726. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3727. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  3728. {
  3729. if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
  3730. {
  3731. setRelay(gun_index, OFF);
  3732. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3733. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3734. DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  3735. }
  3736. else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  3737. {
  3738. setRelay(gun_index, OFF);
  3739. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3740. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3741. DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  3742. }
  3743. else
  3744. {
  3745. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3746. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  3747. {
  3748. setLedMotion(gun_index,LED_ACTION_CHARGING);
  3749. setRelay(gun_index, ON);
  3750. }
  3751. else
  3752. {
  3753. setLedMotion(gun_index, LED_ACTION_STOP);
  3754. setRelay(gun_index, OFF);
  3755. }
  3756. }
  3757. }
  3758. else
  3759. {
  3760. setRelay(gun_index, OFF);
  3761. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  3762. }
  3763. }
  3764. }
  3765. break;
  3766. case SYS_MODE_TERMINATING:
  3767. if(isModeChange(gun_index))
  3768. {
  3769. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  3770. {
  3771. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  3772. }
  3773. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  3774. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3775. sleep(3);
  3776. }
  3777. // End authorize pass
  3778. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3779. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  3780. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3781. ocpp_get_remotestop(gun_index) ||
  3782. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  3783. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  3784. ocpp_get_reset_req() ||
  3785. ocpp_get_unlocker_req(gun_index) ||
  3786. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  3787. {
  3788. if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
  3789. {
  3790. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3791. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  3792. {
  3793. DEBUG_INFO("Authorize pass.\n");
  3794. setSpeaker(ON,SPEAKER_SHORT);
  3795. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3796. sleep(3);
  3797. }
  3798. else
  3799. {
  3800. /*
  3801. // If stop reason is "Bluetooth" / "RemoteStop" / "Reset" / "Unplug" should be set speaker on
  3802. if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
  3803. DEBUG_INFO("Bluetooth to stop charging... \n");
  3804. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)
  3805. DEBUG_INFO("Remote to stop charging... \n");
  3806. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0))
  3807. DEBUG_INFO("Unplug to stop charging... \n");
  3808. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3809. DEBUG_INFO("Reset request to stop charging... \n");
  3810. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3811. DEBUG_INFO("Unlock connector request to stop charging... \n");
  3812. setSpeaker(ON,SPEAKER_SHORT);
  3813. */
  3814. }
  3815. }
  3816. else
  3817. {}
  3818. setRelay(gun_index, OFF);
  3819. setLedMotion(gun_index, LED_ACTION_STOP);
  3820. // If relay is off, the system should change to complete mode
  3821. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3822. {
  3823. checkStopReason(gun_index);
  3824. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3825. }
  3826. }
  3827. else
  3828. {
  3829. // If the charger stops charging, the led should be stoped blink
  3830. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3831. {
  3832. setLedMotion(gun_index, LED_ACTION_STOP);
  3833. }
  3834. if(ShmCharger->gun_info[gun_index].rfidReq)
  3835. {
  3836. DEBUG_INFO("Certified in terminating mode... \n");
  3837. // If RFID SN different with start user, it need to authorize ID
  3838. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3839. {
  3840. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  3841. {
  3842. // Request authorization
  3843. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3844. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3845. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3846. {
  3847. // Big endian
  3848. switch(rfid.snType)
  3849. {
  3850. case RFID_SN_TYPE_6BYTE:
  3851. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  3852. break;
  3853. case RFID_SN_TYPE_7BYTE:
  3854. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  3855. break;
  3856. case RFID_SN_TYPE_10BYTE:
  3857. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  3858. break;
  3859. case RFID_SN_TYPE_4BYTE:
  3860. default:
  3861. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3862. break;
  3863. }
  3864. }
  3865. else
  3866. {
  3867. // Little endian
  3868. switch(rfid.snType)
  3869. {
  3870. case RFID_SN_TYPE_6BYTE:
  3871. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3872. break;
  3873. case RFID_SN_TYPE_7BYTE:
  3874. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3875. break;
  3876. case RFID_SN_TYPE_10BYTE:
  3877. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3878. break;
  3879. case RFID_SN_TYPE_4BYTE:
  3880. default:
  3881. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3882. break;
  3883. }
  3884. }
  3885. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3886. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3887. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3888. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3889. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3890. {
  3891. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3892. {
  3893. case AUTH_MODE_ENABLE:
  3894. if(ocpp_get_connection_status())
  3895. {
  3896. // On line
  3897. ocpp_set_auth_req(ON, "ISO14443");
  3898. }
  3899. else
  3900. {
  3901. // Off line
  3902. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3903. {
  3904. case OFF_POLICY_LOCALLIST:
  3905. ocpp_set_auth_req(ON, "ISO14443");
  3906. break;
  3907. case OFF_POLICY_PH_RFID:
  3908. break;
  3909. case OFF_POLICY_FREE:
  3910. break;
  3911. case OFF_POLICY_NOCHARGE:
  3912. default:
  3913. break;
  3914. }
  3915. }
  3916. break;
  3917. case AUTH_MODE_DISABLE:
  3918. default:
  3919. break;
  3920. }
  3921. }
  3922. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3923. {
  3924. DEBUG_INFO("Should be remote stop charger... \n");
  3925. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3926. }
  3927. }
  3928. else
  3929. {
  3930. if(ocpp_get_auth_conf() ||
  3931. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3932. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > 2000)) ||
  3933. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3934. {
  3935. if(ocpp_get_auth_result(gun_index) ||
  3936. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3937. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3938. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3939. {
  3940. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3941. }
  3942. else
  3943. {
  3944. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3945. ocpp_set_auth_req(OFF);
  3946. DEBUG_INFO("Authorize fail... \n");
  3947. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3948. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3949. sleep(3);
  3950. }
  3951. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3952. ocpp_set_auth_conf(OFF);
  3953. }
  3954. }
  3955. }
  3956. }
  3957. // Charging profile preparation
  3958. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  3959. {
  3960. if(!ocpp_get_profile_conf(gun_index))
  3961. {
  3962. ocpp_set_profile_req(gun_index, ON);
  3963. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3964. }
  3965. else
  3966. {
  3967. ocpp_set_profile_conf(gun_index, OFF);
  3968. }
  3969. }
  3970. // Check target current if charging profile limit > 0
  3971. checkChargingProfileLimit(gun_index);
  3972. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  3973. {
  3974. setRelay(gun_index, ON);
  3975. }
  3976. // Debug information
  3977. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  3978. {
  3979. DEBUG_INFO("=============================================================\n");
  3980. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  3981. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  3982. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  3983. DEBUG_INFO("=============================================================\n");
  3984. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3985. }
  3986. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  3987. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  3988. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  3989. !ocpp_get_remotestop(gun_index) &&
  3990. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  3991. !ocpp_get_reset_req() &&
  3992. ocpp_get_starttransaction_result(gun_index) &&
  3993. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  3994. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  3995. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
  3996. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))))
  3997. {
  3998. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3999. }
  4000. }
  4001. break;
  4002. case SYS_MODE_COMPLETE:
  4003. if(isModeChange(gun_index))
  4004. {
  4005. setLedMotion(gun_index, LED_ACTION_STOP);
  4006. sleep(3);
  4007. }
  4008. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || ocpp_get_reset_req())
  4009. {
  4010. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4011. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4012. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4013. ocpp_set_remotestop(gun_index, OFF);
  4014. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4015. DB_Insert_Record(localDb, gun_index);
  4016. setChargerMode(gun_index, SYS_MODE_IDLE);
  4017. }
  4018. break;
  4019. case SYS_MODE_ALARM:
  4020. setLedMotion(gun_index,LED_ACTION_ALARM);
  4021. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4022. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4023. {
  4024. }
  4025. else
  4026. {
  4027. setRelay(gun_index, OFF);
  4028. }
  4029. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4030. {
  4031. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4032. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4033. {
  4034. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4035. }
  4036. else
  4037. {
  4038. setChargerMode(gun_index, SYS_MODE_IDLE);
  4039. }
  4040. }
  4041. break;
  4042. case SYS_MODE_FAULT:
  4043. if(isModeChange(gun_index))
  4044. {}
  4045. break;
  4046. case SYS_MODE_MAINTAIN:
  4047. if(isModeChange(gun_index))
  4048. {
  4049. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4050. }
  4051. break;
  4052. case SYS_MODE_UPDATE:
  4053. if(isModeChange(gun_index))
  4054. {
  4055. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4056. }
  4057. //======================================
  4058. // Check local upgrade firmware request
  4059. //======================================
  4060. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  4061. {
  4062. DEBUG_INFO("Firmware local upgrading...\n");
  4063. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  4064. int value = upgrade_check();
  4065. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  4066. if(value == PASS)
  4067. {
  4068. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4069. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4070. ShmCharger->isUpdateSuccess = YES;
  4071. DEBUG_INFO("Local update success...\n");
  4072. }
  4073. else
  4074. {
  4075. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4076. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4077. ShmCharger->isUpdateSuccess = NO;
  4078. DEBUG_INFO("Local update unsuccess...\n");
  4079. }
  4080. }
  4081. else if(ocpp_get_update_firmware_req())
  4082. {
  4083. //======================================
  4084. // Check remote upgrade firmware request
  4085. //======================================
  4086. checkRemoteUpgradeStatus();
  4087. }
  4088. else
  4089. {
  4090. //======================================
  4091. // Upgrade complete reboot system
  4092. //======================================
  4093. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  4094. {
  4095. if(ShmCharger->isUpdateSuccess == YES)
  4096. {
  4097. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4098. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4099. DEBUG_WARN("Firmware upgrade success.\n");
  4100. sleep(5);
  4101. DEBUG_INFO("Firmware upgraded, reboot...\n");
  4102. system("reboot -f");
  4103. sleep(5);
  4104. system("reboot -f");
  4105. }
  4106. else
  4107. {
  4108. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4109. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4110. DEBUG_WARN("Firmware upgrade fail.\n");
  4111. sleep(5);
  4112. system("rm -rvf /mnt/* ");
  4113. close(wtdFd);
  4114. system("/usr/bin/run_evse_restart.sh");
  4115. }
  4116. }
  4117. }
  4118. break;
  4119. case SYS_MODE_RESERVATION:
  4120. if(isModeChange(gun_index))
  4121. {
  4122. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4123. }
  4124. if(isReservationExpired(gun_index))
  4125. {
  4126. DEBUG_INFO("Reservation: Time's up...\n");
  4127. setChargerMode(gun_index, SYS_MODE_IDLE);
  4128. }
  4129. else if(ocpp_get_cancelreservation_req(gun_index))
  4130. {
  4131. DEBUG_INFO("Reservation: Cancel reservation...\n");
  4132. setChargerMode(gun_index, SYS_MODE_IDLE);
  4133. ocpp_set_cancelreservation_req(gun_index, OFF);
  4134. }
  4135. else
  4136. {
  4137. // Check is there RFID or back end request start
  4138. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4139. ocpp_get_remotestart(gun_index))
  4140. {
  4141. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4142. {
  4143. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4144. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4145. {
  4146. // Big endian
  4147. switch(rfid.snType)
  4148. {
  4149. case RFID_SN_TYPE_6BYTE:
  4150. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  4151. break;
  4152. case RFID_SN_TYPE_7BYTE:
  4153. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  4154. break;
  4155. case RFID_SN_TYPE_10BYTE:
  4156. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  4157. break;
  4158. case RFID_SN_TYPE_4BYTE:
  4159. default:
  4160. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4161. break;
  4162. }
  4163. }
  4164. else
  4165. {
  4166. // Little endian
  4167. switch(rfid.snType)
  4168. {
  4169. case RFID_SN_TYPE_6BYTE:
  4170. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4171. break;
  4172. case RFID_SN_TYPE_7BYTE:
  4173. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4174. break;
  4175. case RFID_SN_TYPE_10BYTE:
  4176. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4177. break;
  4178. case RFID_SN_TYPE_4BYTE:
  4179. default:
  4180. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4181. break;
  4182. }
  4183. }
  4184. if(ocpp_compare_reserve_id_with_user(gun_index))
  4185. {
  4186. DEBUG_INFO("Start Method in reservation : RFID...\n");
  4187. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4188. DEBUG_INFO("Reservation User Id : %s\n", ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4189. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4190. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4191. }
  4192. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4193. }
  4194. else if(ocpp_get_remotestart(gun_index))
  4195. {
  4196. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  4197. {
  4198. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  4199. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4200. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4201. }
  4202. ocpp_set_remotestop(gun_index, OFF);
  4203. }
  4204. }
  4205. }
  4206. break;
  4207. case SYS_MODE_BOOKING:
  4208. if(isModeChange(gun_index))
  4209. {}
  4210. break;
  4211. case SYS_MODE_DEBUG:
  4212. if(isModeChange(gun_index))
  4213. {
  4214. setLedMotion(gun_index,LED_ACTION_DEBUG);
  4215. }
  4216. break;
  4217. }
  4218. }
  4219. // System watch dog reset
  4220. write(wtdFd, "a", 1);
  4221. usleep(50000);
  4222. }
  4223. return FAIL;
  4224. }