main.c 171 KB

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