main.c 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751
  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 0x600000
  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=0;wrd<MtdBlockSize-4;wrd++)
  1264. {
  1265. ChkSum+=buf[wrd];
  1266. }
  1267. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1268. //================================================
  1269. // Load configuration from mtdblock11
  1270. //================================================
  1271. if(ChkSum!=ChkSumOrg)
  1272. {
  1273. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1274. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1275. fd = open("/mnt/EvseConfig", O_RDWR);
  1276. if (fd < 0)
  1277. {
  1278. free(buf);
  1279. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1280. if(ShmStatusCodeData!=NULL)
  1281. {
  1282. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1283. }
  1284. sleep(5);
  1285. system("reboot -f");
  1286. sleep(5);
  1287. system("reboot -f");
  1288. }
  1289. memset(buf, 0, MtdBlockSize);
  1290. wrd=read(fd, buf,MtdBlockSize);
  1291. close(fd);
  1292. if(wrd<MtdBlockSize)
  1293. {
  1294. free(buf);
  1295. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1296. if(ShmStatusCodeData!=NULL)
  1297. {
  1298. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1299. }
  1300. sleep(5);
  1301. system("reboot -f");
  1302. sleep(5);
  1303. system("reboot -f");
  1304. }
  1305. ChkSum=0;
  1306. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1307. {
  1308. ChkSum+=buf[wrd];
  1309. }
  1310. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1311. //================================================
  1312. // Load configuration from mtdblock12 (Factory default)
  1313. //================================================
  1314. if(ChkSum!=ChkSumOrg)
  1315. {
  1316. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  1317. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1318. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1319. if (fd < 0)
  1320. {
  1321. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  1322. free(buf);
  1323. if(ShmStatusCodeData!=NULL)
  1324. {
  1325. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1326. }
  1327. sleep(5);
  1328. system("reboot -f");
  1329. sleep(5);
  1330. system("reboot -f");
  1331. }
  1332. memset(buf, 0, MtdBlockSize);
  1333. wrd=read(fd, buf,MtdBlockSize);
  1334. close(fd);
  1335. if(wrd<MtdBlockSize)
  1336. {
  1337. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  1338. free(buf);
  1339. if(ShmStatusCodeData!=NULL)
  1340. {
  1341. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1342. }
  1343. sleep(5);
  1344. system("reboot -f");
  1345. sleep(5);
  1346. system("reboot -f");
  1347. }
  1348. ChkSum=0;
  1349. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1350. {
  1351. ChkSum+=buf[wrd];
  1352. }
  1353. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1354. if(ChkSum!=ChkSumOrg)
  1355. {
  1356. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  1357. free(buf);
  1358. system("cd /root;./Module_FactoryConfig -m");
  1359. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1360. system("sync");
  1361. sleep(5);
  1362. system("reboot -f");
  1363. sleep(5);
  1364. system("reboot -f");
  1365. return FAIL;
  1366. }
  1367. }
  1368. }
  1369. //load OK
  1370. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1371. free(buf);
  1372. system("rm -f /mnt/EvseConfig.bin");
  1373. // SysConfig in flash is empty (0xffffffff)
  1374. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1375. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)))
  1376. {
  1377. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1378. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1379. DEBUG_INFO("Model name or serial number over length.\n");
  1380. system("cd /root;./Module_FactoryConfig -m");
  1381. sleep(3);
  1382. system("/usr/bin/run_evse_restart.sh");
  1383. }
  1384. DEBUG_INFO("Load SysConfigData OK\n");
  1385. return PASS;
  1386. }
  1387. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1388. {
  1389. int result = PASS;
  1390. int fd,wrd;
  1391. unsigned int i,Chk;
  1392. unsigned char *ptr, *BufTmp;
  1393. Chk=0;
  1394. ptr=(unsigned char *)UsrData;
  1395. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1396. {
  1397. memset(BufTmp,0,MtdBlockSize);
  1398. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1399. for(i=0;i<MtdBlockSize-4;i++)
  1400. Chk+=*(BufTmp+i);
  1401. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  1402. // Output configuration to file.
  1403. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  1404. if (fd < 0)
  1405. {
  1406. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  1407. free(BufTmp);
  1408. return 0;
  1409. }
  1410. wrd=write(fd, BufTmp, MtdBlockSize);
  1411. close(fd);
  1412. if(wrd<MtdBlockSize)
  1413. {
  1414. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  1415. free(BufTmp);
  1416. return 0;
  1417. }
  1418. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  1419. DEBUG_INFO("Erase /dev/mtd10.\n");
  1420. runShellCmd("flash_erase /dev/mtd10 0 12");
  1421. DEBUG_INFO("Write /dev/mtd10.\n");
  1422. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  1423. DEBUG_INFO("Erase /dev/mtd11.\n");
  1424. runShellCmd("flash_erase /dev/mtd11 0 12");
  1425. DEBUG_INFO("Write /dev/mtd11.\n");
  1426. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  1427. system("rm -f /mnt/EvseConfig.bin");
  1428. DEBUG_INFO("EvseConfig write to flash OK\n");
  1429. }
  1430. else
  1431. {
  1432. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1433. result = FAIL;
  1434. }
  1435. if(BufTmp!=NULL)
  1436. free(BufTmp);
  1437. return result;
  1438. }
  1439. void InitEthernet()
  1440. {
  1441. pid_t pid;
  1442. uint8_t cnt_pingDNS_Fail;
  1443. char tmpbuf[256];
  1444. //unsigned int address;
  1445. //Init Eth0 for internet
  1446. memset(tmpbuf,0,256);
  1447. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1448. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1449. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1450. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1451. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1452. system(tmpbuf);
  1453. memset(tmpbuf,0,256);
  1454. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1455. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1456. system(tmpbuf);
  1457. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  1458. system("ifconfig lo up &");
  1459. system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  1460. if(isInterfaceUp("eth1")==PASS)
  1461. {
  1462. //Init Eth1 for administrator tool
  1463. memset(tmpbuf,0,256);
  1464. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up &",
  1465. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1466. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1467. system(tmpbuf);
  1468. }
  1469. //Run DHCP client if enabled
  1470. system("killall udhcpc");
  1471. system("rm -rf /etc/resolv.conf");
  1472. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  1473. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1474. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  1475. {
  1476. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1477. system(tmpbuf);
  1478. }
  1479. // Upgrade system id to /etc/hostname
  1480. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1481. system(tmpbuf);
  1482. // Ethernet MAC address
  1483. getEth0MacAddress();
  1484. //check internet status
  1485. pid = fork();
  1486. if(pid == 0)
  1487. {
  1488. for(;;)
  1489. {
  1490. if(isRouteFail())
  1491. {
  1492. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  1493. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1494. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1495. {
  1496. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1497. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1498. system(tmpbuf);
  1499. }
  1500. else
  1501. {
  1502. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1503. memset(tmpbuf,0,256);
  1504. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1505. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1506. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1507. system(tmpbuf);
  1508. memset(tmpbuf,0,256);
  1509. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1510. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1511. system(tmpbuf);
  1512. }
  1513. }
  1514. if(isReachableInternet() == PASS)
  1515. {
  1516. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  1517. cnt_pingDNS_Fail = 0;
  1518. }
  1519. else
  1520. {
  1521. if(cnt_pingDNS_Fail >= 3)
  1522. {
  1523. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  1524. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1525. {
  1526. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1527. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1528. system(tmpbuf);
  1529. }
  1530. else
  1531. {
  1532. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1533. memset(tmpbuf,0,256);
  1534. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1535. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1536. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1537. system(tmpbuf);
  1538. memset(tmpbuf,0,256);
  1539. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1540. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1541. system(tmpbuf);
  1542. }
  1543. cnt_pingDNS_Fail = 0;
  1544. }
  1545. else
  1546. {
  1547. cnt_pingDNS_Fail++;
  1548. }
  1549. }
  1550. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  1551. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  1552. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  1553. (ShmOCPP16Data->OcppConnStatus != PASS))
  1554. {
  1555. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1556. }
  1557. else
  1558. {
  1559. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  1560. }
  1561. //============================================================
  1562. // Priority for internet 0 : First / 1 : Second / 2: Third
  1563. //============================================================
  1564. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  1565. {
  1566. system("/sbin/ifmetric eth0 0");
  1567. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1568. {
  1569. system("/sbin/ifmetric mlan0 1");
  1570. }
  1571. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1572. {
  1573. system("/sbin/ifmetric ppp0 2");
  1574. }
  1575. }
  1576. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  1577. {
  1578. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1579. {
  1580. system("/sbin/ifmetric eth0 1");
  1581. system("/sbin/ifmetric mlan0 0");
  1582. }
  1583. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1584. {
  1585. system("/sbin/ifmetric ppp0 2");
  1586. }
  1587. }
  1588. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  1589. {
  1590. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1591. {
  1592. system("/sbin/ifmetric mlan0 2");
  1593. }
  1594. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1595. {
  1596. system("/sbin/ifmetric eth0 1");
  1597. system("/sbin/ifmetric ppp0 0");
  1598. }
  1599. }
  1600. sleep(5);
  1601. }
  1602. }
  1603. DEBUG_INFO("Initial Ethernet OK\n");
  1604. }
  1605. int SpawnTask()
  1606. {
  1607. system ("pkill Module_");
  1608. system ("pkill OcppBackend");
  1609. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1610. {
  1611. system("/root/Module_4g &");
  1612. }
  1613. else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1614. {
  1615. system("/root/Module_Wifi &");
  1616. }
  1617. system("/root/Module_EventLogging &");
  1618. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1619. {
  1620. ocpp_process_start();
  1621. }
  1622. else
  1623. {
  1624. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  1625. ocpp_process_start();
  1626. }
  1627. system ("/root/Module_AlarmDetect &");
  1628. system ("/root/Module_InternalComm &");
  1629. system ("/root/Module_Speaker &");
  1630. system ("/root/Module_ProduceUtils &");
  1631. return PASS;
  1632. }
  1633. int Initialization()
  1634. {
  1635. int result = PASS;
  1636. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1637. InitGPIO();
  1638. InitEthernet();
  1639. if(DB_Open(localDb) != PASS)
  1640. {
  1641. DEBUG_ERROR("Local database initial fail.\n");
  1642. result = FAIL;
  1643. }
  1644. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  1645. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1646. if((rfidFd = InitRfidPort()) == FAIL)
  1647. {
  1648. DEBUG_ERROR("RFID port initial fail.\n");
  1649. result = FAIL;
  1650. }
  1651. if((wtdFd = InitWatchDog()) == FAIL)
  1652. {
  1653. DEBUG_ERROR("Watchdog initial fail.\n");
  1654. result = FAIL;
  1655. }
  1656. if(result == PASS)
  1657. DEBUG_INFO("Initialization OK.\n");
  1658. else
  1659. DEBUG_INFO("Initialization Fail.\n");
  1660. return result;
  1661. }
  1662. //=====================================================
  1663. // Common routine
  1664. //=====================================================
  1665. char* getSystemModeName(unsigned char mode)
  1666. {
  1667. char* result;
  1668. switch(mode)
  1669. {
  1670. case SYS_MODE_BOOTING:
  1671. result = "booting";
  1672. break;
  1673. case SYS_MODE_IDLE:
  1674. result = "idle";
  1675. break;
  1676. case SYS_MODE_AUTHORIZING:
  1677. result = "authorizing";
  1678. break;
  1679. case SYS_MODE_PREPARING:
  1680. result = "preparing";
  1681. break;
  1682. case SYS_MODE_CHARGING:
  1683. result = "charging";
  1684. break;
  1685. case SYS_MODE_TERMINATING:
  1686. result = "terminating";
  1687. break;
  1688. case SYS_MODE_COMPLETE:
  1689. result = "complete";
  1690. break;
  1691. case SYS_MODE_ALARM:
  1692. result = "alarm";
  1693. break;
  1694. case SYS_MODE_FAULT:
  1695. result = "fault";
  1696. break;
  1697. case SYS_MODE_MAINTAIN:
  1698. result = "maintain";
  1699. break;
  1700. case SYS_MODE_RESERVATION:
  1701. result = "reservation";
  1702. break;
  1703. case SYS_MODE_BOOKING:
  1704. result = "booking";
  1705. break;
  1706. case SYS_MODE_DEBUG:
  1707. result = "debug";
  1708. break;
  1709. case SYS_MODE_UPDATE:
  1710. result = "upgrade";
  1711. break;
  1712. default:
  1713. result = "unknown";
  1714. break;
  1715. }
  1716. return result;
  1717. }
  1718. void setChargerMode(unsigned char gun_index, unsigned char mode)
  1719. {
  1720. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1721. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  1722. 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));
  1723. }
  1724. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  1725. {
  1726. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  1727. }
  1728. unsigned char isModeChange(unsigned char gun_index)
  1729. {
  1730. unsigned char result = NO;
  1731. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  1732. {
  1733. result = YES;
  1734. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1735. }
  1736. return result;
  1737. }
  1738. void gpio_set_value(unsigned int gpio, unsigned int value)
  1739. {
  1740. int fd;
  1741. char buf[256];
  1742. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1743. fd = open(buf, O_WRONLY);
  1744. if (fd < 0)
  1745. {
  1746. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  1747. return;
  1748. }
  1749. if (value)
  1750. write(fd, "1", 2);
  1751. else
  1752. write(fd, "0", 2);
  1753. close(fd);
  1754. }
  1755. int gpio_get_value(unsigned int gpio)
  1756. {
  1757. int fd;
  1758. char buf[256];
  1759. char ch;
  1760. int8_t result = FAIL;
  1761. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  1762. fd = open(buf, O_RDONLY);
  1763. if (fd < 0)
  1764. {
  1765. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  1766. return result;
  1767. }
  1768. read(fd, &ch, 1);
  1769. if (ch != '0')
  1770. result = 1;
  1771. else
  1772. result = 0;
  1773. close(fd);
  1774. return result;
  1775. }
  1776. //===============================================
  1777. // Get firmware version
  1778. //===============================================
  1779. void get_firmware_version(unsigned char gun_index)
  1780. {
  1781. FILE *fp;
  1782. char cmd[512];
  1783. char buf[512];
  1784. // Get CSU hardware version
  1785. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  1786. // Get CSU boot loader version
  1787. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1788. // Get CSU kernel version
  1789. sprintf(cmd, "/bin/uname -r");
  1790. fp = popen(cmd, "r");
  1791. if(fp == NULL)
  1792. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1793. else
  1794. {
  1795. while(fgets(buf, sizeof(buf), fp) != NULL)
  1796. {
  1797. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  1798. }
  1799. }
  1800. // Get MCU firmware version
  1801. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1802. // Get CSU root file system version
  1803. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.69.00.0000.00");
  1804. // Get AC connector type from model name
  1805. for(uint8_t idx=0;idx<3;idx++)
  1806. {
  1807. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1808. {
  1809. case '1':
  1810. // J1772 Plug
  1811. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1812. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1813. break;
  1814. case '2':
  1815. // J1772 Socket
  1816. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1817. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1818. break;
  1819. case '3':
  1820. // CE Plug
  1821. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1822. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1823. break;
  1824. case '4':
  1825. // CE Socket
  1826. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1827. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1828. break;
  1829. case '5':
  1830. // GB Plug
  1831. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1832. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1833. break;
  1834. case '6':
  1835. // GB Socket
  1836. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1837. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1838. break;
  1839. }
  1840. }
  1841. // Get network option from model name
  1842. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1843. {
  1844. case 'B':
  1845. case 'U':
  1846. //Blue tooth
  1847. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1848. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1849. break;
  1850. case 'W':
  1851. // WIFI
  1852. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1853. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1854. break;
  1855. case 'T':
  1856. // 3G/4G
  1857. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1858. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1859. break;
  1860. case 'D':
  1861. // WIFI + 4G
  1862. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1863. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  1864. break;
  1865. default:
  1866. // LAN
  1867. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1868. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1869. break;
  1870. }
  1871. // Get rating power from model name
  1872. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1873. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1874. // Get vender code from model name
  1875. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1876. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1877. DEBUG_INFO("========================================\n");
  1878. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1879. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  1880. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  1881. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  1882. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1883. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1884. DEBUG_INFO("========================================\n");
  1885. }
  1886. //===============================================
  1887. // Upgrade firmware
  1888. //===============================================
  1889. int upgrade_check()
  1890. {
  1891. int result = PASS;
  1892. int fd;
  1893. DIR *dir;
  1894. struct dirent *file;
  1895. char cmd[512];
  1896. long int MaxLen=48*1024*1024;
  1897. if ((dir = opendir ("/mnt")) != NULL)
  1898. {
  1899. /* print all the files and directories within directory */
  1900. while ((file = readdir (dir)) != NULL)
  1901. {
  1902. if(strlen(file->d_name)>2)
  1903. {
  1904. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1905. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1906. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1907. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1908. {
  1909. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1910. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1911. read(fd, ptr, MaxLen);
  1912. close(fd);
  1913. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1914. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1915. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  1916. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1917. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1918. {
  1919. switch(ShmCharger->fwUpgradeInfo.fwType)
  1920. {
  1921. case CSU_BOOTLOADER:
  1922. case CSU_KERNEL_CONFIGURATION:
  1923. case CSU_KERNEL_IMAGE:
  1924. case CSU_ROOT_FILE_SYSTEM:
  1925. case CSU_USER_CONFIGURATION:
  1926. case CSU_PRIMARY_CONTROLLER:
  1927. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1928. {
  1929. result = FAIL;
  1930. }
  1931. else
  1932. {
  1933. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  1934. {
  1935. DEBUG_INFO("Restore model name & serial number.\n");
  1936. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1937. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1938. {
  1939. DEBUG_INFO("Re-load configuration fail.\n");
  1940. result = FAIL;
  1941. }
  1942. else
  1943. {
  1944. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  1945. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  1946. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  1947. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1948. {
  1949. DEBUG_INFO("Re-write configuration fail.\n");
  1950. result = FAIL;
  1951. }
  1952. else
  1953. DEBUG_INFO("Re-write configuration OK.\n");
  1954. }
  1955. }
  1956. }
  1957. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1958. system(cmd);
  1959. break;
  1960. case AC_WALLMOUNT_CONTROLLER:
  1961. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1962. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1963. break;
  1964. default:
  1965. result = FAIL;
  1966. DEBUG_WARN("Image file is unknown type.\n");
  1967. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1968. system(cmd);
  1969. break;
  1970. }
  1971. }
  1972. else
  1973. {
  1974. result = FAIL;
  1975. DEBUG_ERROR("Model name and Firmware type error.\n");
  1976. }
  1977. free(ptr);
  1978. }
  1979. else
  1980. {
  1981. result = FAIL;
  1982. DEBUG_ERROR("New firmware open error.\n");
  1983. }
  1984. }
  1985. else
  1986. {
  1987. if(strlen(file->d_name) >= 3)
  1988. {
  1989. result = FAIL;
  1990. DEBUG_ERROR("File name error.\n");
  1991. }
  1992. else
  1993. {
  1994. DEBUG_ERROR("Searching file.\n");
  1995. }
  1996. }
  1997. }
  1998. closedir (dir);
  1999. }
  2000. else
  2001. {
  2002. result = FAIL;
  2003. DEBUG_ERROR("/mnt does not valid.\n");
  2004. }
  2005. return result;
  2006. }
  2007. //===============================================
  2008. // Check RFID is match with start user
  2009. //===============================================
  2010. int isMatchStartUser(unsigned char gun_index)
  2011. {
  2012. uint8_t tmpUser[32];
  2013. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2014. {
  2015. // Big endian
  2016. switch(rfid.snType)
  2017. {
  2018. case RFID_SN_TYPE_6BYTE:
  2019. 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]);
  2020. break;
  2021. case RFID_SN_TYPE_7BYTE:
  2022. 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]);
  2023. break;
  2024. case RFID_SN_TYPE_10BYTE:
  2025. 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]);
  2026. break;
  2027. case RFID_SN_TYPE_4BYTE:
  2028. default:
  2029. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2030. break;
  2031. }
  2032. }
  2033. else
  2034. {
  2035. // Little endian
  2036. switch(rfid.snType)
  2037. {
  2038. case RFID_SN_TYPE_6BYTE:
  2039. 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]);
  2040. break;
  2041. case RFID_SN_TYPE_7BYTE:
  2042. 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]);
  2043. break;
  2044. case RFID_SN_TYPE_10BYTE:
  2045. 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]);
  2046. break;
  2047. case RFID_SN_TYPE_4BYTE:
  2048. default:
  2049. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2050. break;
  2051. }
  2052. }
  2053. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2054. }
  2055. //===============================================
  2056. // Read RFID Serial Number
  2057. //===============================================
  2058. int GetCardSerialNumber()
  2059. {
  2060. int isSuccess = FAIL;
  2061. int module_type = MODULE_EWT;
  2062. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2063. {
  2064. if(rfid.cardType == ISO14443A)
  2065. {
  2066. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2067. {
  2068. isSuccess = PASS;
  2069. }
  2070. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2071. {
  2072. isSuccess = PASS;
  2073. }
  2074. sethaltCard(rfidFd,module_type);
  2075. }
  2076. else if(rfid.cardType == IS014443B)
  2077. {
  2078. isSuccess = PASS;
  2079. }
  2080. else if(rfid.cardType == FELICA)
  2081. {
  2082. isSuccess = PASS;
  2083. }
  2084. else
  2085. {}
  2086. }
  2087. else
  2088. {}
  2089. return isSuccess;
  2090. }
  2091. //===============================================
  2092. // Set led motion
  2093. //===============================================
  2094. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2095. {
  2096. switch(led_mode)
  2097. {
  2098. case LED_ACTION_INIT:
  2099. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2100. break;
  2101. case LED_ACTION_IDLE:
  2102. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2103. break;
  2104. case LED_ACTION_AUTHED:
  2105. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2106. break;
  2107. case LED_ACTION_CONNECTED:
  2108. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2109. break;
  2110. case LED_ACTION_CHARGING:
  2111. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2112. break;
  2113. case LED_ACTION_STOP:
  2114. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2115. break;
  2116. case LED_ACTION_ALARM:
  2117. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2118. break;
  2119. case LED_ACTION_MAINTAIN:
  2120. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2121. break;
  2122. case LED_ACTION_RFID_PASS:
  2123. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2124. break;
  2125. case LED_ACTION_RFID_FAIL:
  2126. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2127. break;
  2128. case LED_ACTION_BLE_CONNECT:
  2129. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2130. break;
  2131. case LED_ACTION_BLE_DISABLE:
  2132. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2133. break;
  2134. case LED_ACTION_DEBUG:
  2135. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2136. break;
  2137. case LED_ACTION_ALL_OFF:
  2138. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2139. break;
  2140. case LED_RELAY_ON:
  2141. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2142. break;
  2143. case LED_RELAY_OFF:
  2144. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2145. break;
  2146. case LED_ACTION_HANDSHAKE_FAIL:
  2147. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2148. break;
  2149. case LED_ACTION_INTERNET_DISCONNECT:
  2150. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2151. break;
  2152. }
  2153. }
  2154. //===============================================
  2155. // Relay on/off request set
  2156. //===============================================
  2157. void setRelay(unsigned char gun_index,unsigned char isOn)
  2158. {
  2159. if(isOn == ON)
  2160. {
  2161. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2162. {
  2163. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2164. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  2165. }
  2166. }
  2167. else
  2168. {
  2169. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2170. {
  2171. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2172. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  2173. }
  2174. }
  2175. }
  2176. //===============================================
  2177. // Relay on/off request get
  2178. //===============================================
  2179. int getRelay(unsigned char gun_index)
  2180. {
  2181. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2182. }
  2183. //===============================================
  2184. // Set speaker on/off request
  2185. //===============================================
  2186. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2187. {
  2188. if(isOn == ON)
  2189. {
  2190. ShmCharger->isSpeakerOn = ON;
  2191. ShmCharger->speaker_type = speaker_mode;
  2192. }
  2193. }
  2194. //===============================================
  2195. // Initialization RFID communication port
  2196. //===============================================
  2197. int InitRfidPort()
  2198. {
  2199. int uartO2 = open(rfidPortName, O_RDWR);
  2200. struct termios tios;
  2201. if (uartO2 != FAIL)
  2202. {
  2203. ioctl (uartO2, TCGETS, &tios);
  2204. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2205. tios.c_lflag = 0;
  2206. tios.c_iflag = 0;
  2207. tios.c_oflag = 0;
  2208. tios.c_cc[VMIN] = 0;
  2209. tios.c_cc[VTIME] = (unsigned char) 1;
  2210. tios.c_lflag = 0;
  2211. tcflush(uartO2, TCIFLUSH);
  2212. ioctl(uartO2, TCSETS, &tios);
  2213. }
  2214. if (uartO2 < 0)
  2215. {
  2216. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2217. }
  2218. return uartO2;
  2219. }
  2220. //===============================================
  2221. // Check internet access status
  2222. //===============================================
  2223. int isReachableInternet()
  2224. {
  2225. int result = FAIL;
  2226. FILE *fp;
  2227. char cmd[256];
  2228. char buf[512];
  2229. char tmp[512];
  2230. strcpy(cmd, "ifconfig eth0");
  2231. fp = popen(cmd, "r");
  2232. if(fp != NULL)
  2233. {
  2234. while(fgets(buf, sizeof(buf), fp) != NULL)
  2235. {
  2236. if (strstr(buf, "inet addr:") > 0)
  2237. {
  2238. sscanf(buf, "%*s%s", tmp);
  2239. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2240. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  2241. {
  2242. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  2243. }
  2244. sscanf(buf, "%*s%*s%*s%s", tmp);
  2245. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2246. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  2247. {
  2248. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  2249. }
  2250. }
  2251. }
  2252. }
  2253. pclose(fp);
  2254. memset(buf, 0x00, sizeof(buf));
  2255. // Get gateway
  2256. fp = popen("ip route", "r");
  2257. if(fp == NULL)
  2258. result = FAIL;
  2259. else
  2260. {
  2261. while(fgets(buf, sizeof(buf), fp) != NULL)
  2262. {
  2263. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2264. break;
  2265. }
  2266. if(strstr(buf, "default") != NULL)
  2267. {
  2268. sscanf(buf, "%*s%*s%s", tmp);
  2269. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2270. }
  2271. }
  2272. pclose(fp);
  2273. memset(buf, 0x00, sizeof(buf));
  2274. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2275. {
  2276. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2277. fp = popen(cmd, "r");
  2278. if(fp != NULL)
  2279. {
  2280. while(fgets(buf, sizeof(buf), fp) != NULL)
  2281. {
  2282. if(strstr(buf, "transmitted") > 0)
  2283. {
  2284. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2285. if(strstr(buf,"100%") != NULL)
  2286. {
  2287. }
  2288. else
  2289. {
  2290. result = PASS;
  2291. }
  2292. //DEBUG_INFO("%s",buf);
  2293. //DEBUG_INFO("%s\n",tmp);
  2294. }
  2295. }
  2296. }
  2297. pclose(fp);
  2298. }
  2299. return result;
  2300. }
  2301. //===============================================
  2302. // Check route for eth0
  2303. //===============================================
  2304. int isRouteFail()
  2305. {
  2306. int result = YES;
  2307. FILE *fp;
  2308. char buf[512];
  2309. fp = popen("route -n", "r");
  2310. if(fp != NULL)
  2311. {
  2312. while(fgets(buf, sizeof(buf), fp) != NULL)
  2313. {
  2314. if(strstr(buf, "eth0") != NULL)
  2315. result = NO;
  2316. }
  2317. }
  2318. pclose(fp);
  2319. return result;
  2320. }
  2321. //===============================================
  2322. // Check reservation date is expired
  2323. //===============================================
  2324. int isReservationExpired(unsigned char gun_index)
  2325. {
  2326. int result = NO;
  2327. struct tm expiredDate;
  2328. struct timeb expiredTime;
  2329. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2330. {
  2331. 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)
  2332. {
  2333. expiredDate.tm_year -= 1900;
  2334. expiredDate.tm_mon -= 1;
  2335. expiredTime.time = mktime(&expiredDate);
  2336. if(DiffTimebWithNow(expiredTime)>=0)
  2337. {
  2338. result = YES;
  2339. }
  2340. }
  2341. else
  2342. {
  2343. DEBUG_WARN("Expired date parsing error.\n");
  2344. DEBUG_INFO("Date expired.\n");
  2345. result = YES;
  2346. }
  2347. }
  2348. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2349. {
  2350. 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)
  2351. {
  2352. expiredDate.tm_year -= 1900;
  2353. expiredDate.tm_mon -= 1;
  2354. expiredTime.time = mktime(&expiredDate);
  2355. if(DiffTimebWithNow(expiredTime)>=0)
  2356. {
  2357. result = YES;
  2358. }
  2359. }
  2360. else
  2361. {
  2362. DEBUG_WARN("Expired date parsing error.\n");
  2363. DEBUG_INFO("Date expired.\n");
  2364. result = YES;
  2365. }
  2366. }
  2367. return result;
  2368. }
  2369. //===============================================
  2370. // Check charging profile related date routine
  2371. //===============================================
  2372. int isProfileValid(uint8_t gun_index)
  2373. {
  2374. int result = NO;
  2375. struct tm tmFrom, tmTo;
  2376. struct timeb tbFrom, tbTo;
  2377. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2378. {
  2379. 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) &&
  2380. (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))
  2381. {
  2382. tmFrom.tm_year -= 1900;
  2383. tmFrom.tm_mon -= 1;
  2384. tbFrom.time = mktime(&tmFrom);
  2385. tmTo.tm_year -= 1900;
  2386. tmTo.tm_mon -= 1;
  2387. tbTo.time = mktime(&tmTo);
  2388. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2389. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2390. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2391. {
  2392. result = YES;
  2393. }
  2394. }
  2395. else
  2396. {
  2397. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2398. }
  2399. }
  2400. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2401. {
  2402. 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) &&
  2403. (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))
  2404. {
  2405. tmFrom.tm_year -= 1900;
  2406. tmFrom.tm_mon -= 1;
  2407. tbFrom.time = mktime(&tmFrom);
  2408. tmTo.tm_year -= 1900;
  2409. tmTo.tm_mon -= 1;
  2410. tbTo.time = mktime(&tmTo);
  2411. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2412. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2413. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2414. {
  2415. result = YES;
  2416. }
  2417. }
  2418. else
  2419. {
  2420. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2421. }
  2422. }
  2423. return result;
  2424. }
  2425. int getScheduleStart(int gun_index)
  2426. {
  2427. int result = -1;
  2428. struct tm tmScheduleStart;;
  2429. struct timeb tbScheduleStart;
  2430. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2431. {
  2432. 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))
  2433. {
  2434. tmScheduleStart.tm_year -= 1900;
  2435. tmScheduleStart.tm_mon -= 1;
  2436. tbScheduleStart.time = mktime(&tmScheduleStart);
  2437. tbScheduleStart.millitm = 0;
  2438. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2439. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2440. }
  2441. else
  2442. {
  2443. DEBUG_WARN("Schedule start date parsing error.\n");
  2444. }
  2445. }
  2446. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2447. {
  2448. 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))
  2449. {
  2450. tmScheduleStart.tm_year -= 1900;
  2451. tmScheduleStart.tm_mon -= 1;
  2452. tbScheduleStart.time = mktime(&tmScheduleStart);
  2453. tbScheduleStart.millitm = 0;
  2454. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2455. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2456. }
  2457. else
  2458. {
  2459. DEBUG_WARN("Schedule start date parsing error.\n");
  2460. }
  2461. }
  2462. return result;
  2463. }
  2464. int getStartSinceToday()
  2465. {
  2466. int result = -1;
  2467. time_t t;
  2468. struct tm *tmStartToday;
  2469. struct timeb tbStartToday;
  2470. t=time(NULL);
  2471. tmStartToday=localtime(&t);
  2472. tmStartToday->tm_hour = 0;
  2473. tmStartToday->tm_min = 0;
  2474. tmStartToday->tm_sec = 0;
  2475. tbStartToday.time = mktime(tmStartToday);
  2476. result = DiffTimebWithNow(tbStartToday)/1000;
  2477. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  2478. return result;
  2479. }
  2480. int getStartSinceWeek()
  2481. {
  2482. int result = -1;
  2483. time_t t;
  2484. struct tm *tmStartWeek;
  2485. struct timeb tbStartWeek;
  2486. t=time(NULL);
  2487. tmStartWeek=localtime(&t);
  2488. t-=86400*tmStartWeek->tm_wday;
  2489. tmStartWeek=localtime(&t);
  2490. tmStartWeek->tm_hour = 0;
  2491. tmStartWeek->tm_min = 0;
  2492. tmStartWeek->tm_sec = 0;
  2493. tbStartWeek.time = mktime(tmStartWeek);
  2494. result = DiffTimebWithNow(tbStartWeek)/1000;
  2495. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  2496. return result;
  2497. }
  2498. //===============================================
  2499. // Valid from local white list
  2500. //===============================================
  2501. int isValidLocalWhiteCard()
  2502. {
  2503. uint8_t result = FAIL;
  2504. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  2505. {
  2506. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  2507. {
  2508. result = PASS;
  2509. }
  2510. }
  2511. return result;
  2512. }
  2513. //==========================================
  2514. // Check routine
  2515. //==========================================
  2516. void checkTask()
  2517. {
  2518. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2519. {
  2520. if(system("pidof -s Module_4g > /dev/null") != 0)
  2521. {
  2522. DEBUG_INFO("Module_4g not running, restart it.\n");
  2523. system("/root/Module_4g &");
  2524. }
  2525. }
  2526. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2527. {
  2528. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  2529. {
  2530. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  2531. system("/root/Module_Wifi &");
  2532. }
  2533. }
  2534. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  2535. {
  2536. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  2537. system("/root/Module_EventLogging &");
  2538. }
  2539. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2540. {
  2541. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2542. {
  2543. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  2544. {
  2545. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  2546. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2547. system("pkill OcppBackend");
  2548. }
  2549. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2550. {
  2551. DEBUG_INFO("OcppBackend not running, restart it.\n");
  2552. ocpp_process_start();
  2553. }
  2554. }
  2555. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2556. {
  2557. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  2558. {
  2559. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  2560. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2561. system("pkill OcppBackend20");
  2562. }
  2563. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  2564. {
  2565. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  2566. ocpp_process_start();
  2567. }
  2568. }
  2569. }
  2570. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  2571. {
  2572. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  2573. system("/root/Module_AlarmDetect &");
  2574. }
  2575. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  2576. {
  2577. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  2578. system("/root/Module_InternalComm &");
  2579. }
  2580. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  2581. {
  2582. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  2583. system("/root/Module_Speaker &");
  2584. }
  2585. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  2586. {
  2587. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  2588. system ("/root/Module_ProduceUtils &");
  2589. }
  2590. /*
  2591. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  2592. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  2593. {
  2594. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  2595. system ("/root/Module_InitUpgrade &");
  2596. }*/
  2597. }
  2598. void checkConnectionTimeout()
  2599. {
  2600. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  2601. {
  2602. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2603. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  2604. }
  2605. else
  2606. {
  2607. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  2608. {
  2609. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2610. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  2611. {
  2612. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2613. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  2614. }
  2615. else
  2616. {
  2617. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  2618. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  2619. }
  2620. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2621. }
  2622. else
  2623. {
  2624. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2625. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\n.");
  2626. }
  2627. //DEBUG_INFO("Present timeout spec : %d \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  2628. }
  2629. }
  2630. void checkReset()
  2631. {
  2632. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2633. {
  2634. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2635. {
  2636. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2637. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2638. {
  2639. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2640. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2641. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2642. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2643. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2644. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2645. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2646. {
  2647. system("sync");
  2648. sleep(5);
  2649. system("reboot -f");
  2650. sleep(5);
  2651. system("reboot -f");
  2652. }
  2653. else
  2654. {
  2655. sleep(5);
  2656. close(wtdFd);
  2657. system("/usr/bin/run_evse_restart.sh");
  2658. }
  2659. }
  2660. }
  2661. }
  2662. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2663. {
  2664. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2665. {
  2666. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2667. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2668. {
  2669. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  2670. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  2671. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  2672. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  2673. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2674. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2675. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  2676. {
  2677. system("sync");
  2678. sleep(5);
  2679. system("reboot -f");
  2680. sleep(5);
  2681. system("reboot -f");
  2682. }
  2683. else
  2684. {
  2685. sleep(5);
  2686. close(wtdFd);
  2687. system("/usr/bin/run_evse_restart.sh");
  2688. }
  2689. }
  2690. }
  2691. }
  2692. }
  2693. void checkReservation(uint8_t gun_index)
  2694. {
  2695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2696. {
  2697. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2698. {
  2699. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2700. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2701. {
  2702. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2703. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2704. }
  2705. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2706. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2707. }
  2708. }
  2709. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2710. {
  2711. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  2712. {
  2713. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2714. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2715. {
  2716. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  2717. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2718. }
  2719. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2720. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2721. }
  2722. }
  2723. }
  2724. void checkUnlocker(uint8_t gun_index)
  2725. {
  2726. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2727. {
  2728. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2729. {
  2730. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2731. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2732. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2733. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2734. }
  2735. }
  2736. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2737. {
  2738. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2739. {
  2740. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2741. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "NotSupported");
  2742. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  2743. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  2744. }
  2745. }
  2746. }
  2747. void checkAvailability(uint8_t gun_index)
  2748. {
  2749. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2750. {
  2751. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2752. {
  2753. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2754. {
  2755. DB_Update_Operactive(localDb, gun_index, true);
  2756. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2757. }
  2758. else
  2759. {
  2760. DB_Update_Operactive(localDb, gun_index, false);
  2761. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2762. }
  2763. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2764. }
  2765. }
  2766. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2767. {
  2768. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2769. {
  2770. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  2771. {
  2772. DB_Update_Operactive(localDb, gun_index, true);
  2773. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2774. }
  2775. else
  2776. {
  2777. DB_Update_Operactive(localDb, gun_index, false);
  2778. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2779. }
  2780. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2781. }
  2782. }
  2783. }
  2784. void checkChargingProfileLimit(uint8_t gun_index)
  2785. {
  2786. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2787. {
  2788. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2789. (ocpp_get_profile_req(gun_index) != ON) &&
  2790. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2791. {
  2792. // Debug information
  2793. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2794. {
  2795. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2796. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2797. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2798. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2799. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2800. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2801. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2802. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2803. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2804. DEBUG_INFO("========================================\n");
  2805. }
  2806. // Checking profile kind
  2807. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2808. {
  2809. // Absolute profile
  2810. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2811. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2812. {
  2813. // Checking limitation
  2814. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2815. {
  2816. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2817. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  2818. )
  2819. {
  2820. 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);
  2821. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2822. }
  2823. else
  2824. break;
  2825. }
  2826. }
  2827. }
  2828. }
  2829. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2830. {
  2831. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2832. }
  2833. }
  2834. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2835. {
  2836. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2837. (ocpp_get_profile_req(gun_index) != ON) &&
  2838. (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
  2839. {
  2840. // Debug information
  2841. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2842. {
  2843. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].id);
  2844. DEBUG_INFO("Valid from: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom);
  2845. DEBUG_INFO("Valid to: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validTo);
  2846. DEBUG_INFO("Start schedule: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  2847. DEBUG_INFO("Profile kind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind);
  2848. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].recurrencyKind);
  2849. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose);
  2850. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId);
  2851. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit);
  2852. DEBUG_INFO("=======================================\n");
  2853. }
  2854. // Checking profile kind
  2855. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  2856. {
  2857. // Absolute profile
  2858. if(((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId == ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId)) ||
  2859. (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == FAIL))
  2860. {
  2861. // Checking limitation
  2862. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  2863. {
  2864. if((getScheduleStart(gun_index) > ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  2865. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  2866. )
  2867. {
  2868. 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);
  2869. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2870. }
  2871. else
  2872. break;
  2873. }
  2874. }
  2875. }
  2876. }
  2877. else if((ShmOCPP20Data->SmartChargingProfile[gun_index].id == 0) && (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2878. {
  2879. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2880. }
  2881. }
  2882. }
  2883. void checkStopReason(uint8_t gun_index)
  2884. {
  2885. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2886. {
  2887. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2888. if(!ocpp_get_starttransaction_result(gun_index))
  2889. {
  2890. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  2891. }
  2892. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  2893. {
  2894. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  2895. }
  2896. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2897. {
  2898. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  2899. }
  2900. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2901. {
  2902. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  2903. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  2904. else
  2905. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  2906. }
  2907. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2908. {
  2909. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  2910. if(!isMatchStartUser(gun_index))
  2911. {
  2912. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2913. }
  2914. else
  2915. {
  2916. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2917. }
  2918. DEBUG_INFO("Gun-%d [ IdTag ] : %s \n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  2919. }
  2920. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  2921. {
  2922. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  2923. }
  2924. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  2925. {
  2926. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  2927. }
  2928. else
  2929. {
  2930. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  2931. }
  2932. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  2933. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  2934. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  2935. }
  2936. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2937. {
  2938. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  2939. if(!ocpp_get_starttransaction_result(gun_index))
  2940. {
  2941. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  2942. }
  2943. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  2944. {
  2945. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  2946. }
  2947. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2948. {
  2949. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  2950. }
  2951. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  2952. {
  2953. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  2954. }
  2955. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2956. {
  2957. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  2958. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  2959. else
  2960. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  2961. }
  2962. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2963. {
  2964. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  2965. if(!isMatchStartUser(gun_index))
  2966. {
  2967. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  2968. }
  2969. else
  2970. {
  2971. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  2972. }
  2973. DEBUG_INFO("Gun-%d [ idToken ] : %s \n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  2974. }
  2975. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_OVER_CURRENT))
  2976. {
  2977. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  2978. }
  2979. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  2980. {
  2981. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  2982. }
  2983. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  2984. {
  2985. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  2986. }
  2987. else
  2988. {
  2989. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  2990. }
  2991. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  2992. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  2993. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  2994. }
  2995. }
  2996. void checkRemoteUpgradeStatus()
  2997. {
  2998. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2999. {
  3000. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3001. {
  3002. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3003. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3004. ShmCharger->isUpdateSuccess = NO;
  3005. }
  3006. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3007. {
  3008. DEBUG_INFO("Firmware remote upgrading...\n");
  3009. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3010. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3011. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3012. int result = upgrade_check();
  3013. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3014. if(result == PASS)
  3015. {
  3016. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3017. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3018. ShmCharger->isUpdateSuccess = YES;
  3019. DEBUG_INFO("Remote update success...\n");
  3020. }
  3021. else
  3022. {
  3023. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3024. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3025. ShmCharger->isUpdateSuccess = NO;
  3026. DEBUG_INFO("Remote update unsuccess...\n");
  3027. }
  3028. }
  3029. else
  3030. {}
  3031. }
  3032. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3033. {
  3034. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3035. {
  3036. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3037. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3038. ShmCharger->isUpdateSuccess = NO;
  3039. }
  3040. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  3041. {
  3042. DEBUG_INFO("Firmware remote upgrading...\n");
  3043. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3044. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3045. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3046. int result = upgrade_check();
  3047. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3048. if(result == PASS)
  3049. {
  3050. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3051. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3052. ShmCharger->isUpdateSuccess = YES;
  3053. DEBUG_INFO("Remote update success...\n");
  3054. }
  3055. else
  3056. {
  3057. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3058. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3059. ShmCharger->isUpdateSuccess = NO;
  3060. DEBUG_INFO("Remote update unsuccess...\n");
  3061. }
  3062. }
  3063. else
  3064. {}
  3065. }
  3066. }
  3067. //===============================================
  3068. // Main process
  3069. //===============================================
  3070. int main(void)
  3071. {
  3072. //Create all share memory
  3073. if(CreatShareMemory()==0)
  3074. {
  3075. DEBUG_ERROR("CreatShareMemory NG\n");
  3076. if(ShmStatusCodeData!=NULL)
  3077. {
  3078. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3079. }
  3080. sleep(5);
  3081. system("reboot -f");
  3082. sleep(5);
  3083. system("reboot -f");
  3084. }
  3085. else
  3086. {
  3087. DEBUG_INFO("CreatShareMemory OK\n");
  3088. }
  3089. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3090. {
  3091. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  3092. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  3093. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  3094. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  3095. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  3096. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  3097. }
  3098. // Main loop
  3099. for(;;)
  3100. {
  3101. //==========================================
  3102. // Synchronize share memory from OCPP struct
  3103. //==========================================
  3104. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  3105. //==========================================
  3106. // Check task processing
  3107. //==========================================
  3108. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3109. checkTask();
  3110. //==========================================
  3111. // Check connection timeout specification
  3112. //==========================================
  3113. checkConnectionTimeout();
  3114. //==========================================
  3115. // Something need run in Idle mode
  3116. //==========================================
  3117. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  3118. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  3119. {
  3120. //======================================
  3121. // Check restore factory setting request
  3122. //======================================
  3123. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3124. {
  3125. // Set led to initial
  3126. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3127. {
  3128. setLedMotion(gun_index,LED_ACTION_INIT);
  3129. }
  3130. system("cd /root;./Module_FactoryConfig -m");
  3131. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3132. system("sync");
  3133. sleep(5);
  3134. system("reboot -f");
  3135. sleep(5);
  3136. system("reboot -f");
  3137. }
  3138. //======================================
  3139. // Check upgrade firmware request
  3140. //======================================
  3141. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3142. ocpp_get_update_firmware_req())
  3143. {
  3144. // If available memory too small, free memory cache first
  3145. if(getAvailableMemory() < (200*1024*1024))
  3146. {
  3147. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3148. system("echo 3 > /proc/sys/vm/drop_caches");
  3149. }
  3150. ShmCharger->isUpdateSuccess = NO;
  3151. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3152. {
  3153. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3154. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3155. }
  3156. }
  3157. }
  3158. //==========================================
  3159. // Check remote reset request
  3160. //==========================================
  3161. checkReset();
  3162. //==========================================
  3163. // Check RFID
  3164. //==========================================
  3165. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  3166. {
  3167. if(GetCardSerialNumber()!= FAIL)
  3168. {
  3169. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3170. }
  3171. }
  3172. //==========================================
  3173. // Connector loop
  3174. //==========================================
  3175. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3176. {
  3177. // Synchronize current rating value from MCU
  3178. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3179. // Assign connector location index for OCPP
  3180. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  3181. // Synchronize present charging power
  3182. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3183. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  3184. else
  3185. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3186. // Check initialization "PASS" or "FAIL"
  3187. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3188. {
  3189. // Alarm event check
  3190. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  3191. {
  3192. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  3193. {
  3194. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  3195. {
  3196. setChargerMode(gun_index, SYS_MODE_ALARM);
  3197. }
  3198. }
  3199. }
  3200. }
  3201. // Reservation request check
  3202. checkReservation(gun_index);
  3203. // Change availability check
  3204. checkAvailability(gun_index);
  3205. if(ShmCharger->gun_info[gun_index].isOperactive)
  3206. {
  3207. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  3208. {
  3209. setChargerMode(gun_index, SYS_MODE_IDLE);
  3210. }
  3211. }
  3212. else
  3213. {
  3214. if(isMode(gun_index, SYS_MODE_IDLE))
  3215. {
  3216. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  3217. }
  3218. }
  3219. // Unlock Connector signal check
  3220. checkUnlocker(gun_index);
  3221. // Connector process
  3222. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  3223. {
  3224. case SYS_MODE_BOOTING:
  3225. if(isModeChange(gun_index))
  3226. {
  3227. setLedMotion(gun_index,LED_ACTION_INIT);
  3228. //CSU Initialization & task spawn
  3229. if((Initialization() != PASS) ||
  3230. (SpawnTask() != PASS))
  3231. {
  3232. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  3233. }
  3234. }
  3235. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  3236. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  3237. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  3238. {
  3239. ShmCharger->gun_selectd = 0;
  3240. // Firmware version
  3241. get_firmware_version(gun_index);
  3242. // OCPP BootNotification info set
  3243. DEBUG_INFO("==========================================\n");
  3244. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  3245. DEBUG_INFO("==========================================\n");
  3246. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  3247. ocpp_boot_info_sync();
  3248. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  3249. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  3250. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  3251. DEBUG_INFO("==========================================\n");
  3252. DEBUG_INFO("========== Set Wifi information ==========\n");
  3253. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  3254. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3255. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3256. DEBUG_INFO("==========================================\n");
  3257. // Set max current to rating current
  3258. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3259. // Default Ethernet / Wifi / 4G to 1:disconnected
  3260. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3261. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3262. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3263. // If Web Server OPCC URL is empty kill Module_OcppBackend
  3264. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  3265. {
  3266. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  3267. system ("pkill OcppBackend");
  3268. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  3269. }
  3270. // If rotate switch equal zero, the system needs to change Debug mode
  3271. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  3272. setChargerMode(gun_index, SYS_MODE_DEBUG);
  3273. else
  3274. setChargerMode(gun_index, SYS_MODE_IDLE);
  3275. }
  3276. break;
  3277. case SYS_MODE_IDLE:
  3278. if(isModeChange(gun_index))
  3279. {
  3280. setLedMotion(gun_index,LED_ACTION_IDLE);
  3281. setRelay(gun_index,OFF);
  3282. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3283. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3284. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3285. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3286. ocpp_set_remotestart(gun_index, OFF);
  3287. ocpp_set_remotestop(gun_index, OFF);
  3288. ocpp_set_auth_conf(OFF);
  3289. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  3290. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  3291. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3292. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  3293. 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;
  3294. ocpp_set_unlocker_req(gun_index, OFF);
  3295. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  3296. // Response StopTransactionConf
  3297. ocpp_set_stoptransaction_conf(gun_index, OFF);
  3298. }
  3299. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  3300. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  3301. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3302. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  3303. (ocpp_get_remotestart(gun_index) == ON) ||
  3304. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  3305. {
  3306. // Clean User id & Card Number
  3307. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3308. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3309. {
  3310. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3311. DEBUG_INFO("Start Method : RFID...\n");
  3312. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3313. {
  3314. // Big endian
  3315. switch(rfid.snType)
  3316. {
  3317. case RFID_SN_TYPE_6BYTE:
  3318. 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]);
  3319. break;
  3320. case RFID_SN_TYPE_7BYTE:
  3321. 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]);
  3322. break;
  3323. case RFID_SN_TYPE_10BYTE:
  3324. 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]);
  3325. break;
  3326. case RFID_SN_TYPE_4BYTE:
  3327. default:
  3328. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3329. break;
  3330. }
  3331. }
  3332. else
  3333. {
  3334. // Little endian
  3335. switch(rfid.snType)
  3336. {
  3337. case RFID_SN_TYPE_6BYTE:
  3338. 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]);
  3339. break;
  3340. case RFID_SN_TYPE_7BYTE:
  3341. 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]);
  3342. break;
  3343. case RFID_SN_TYPE_10BYTE:
  3344. 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]);
  3345. break;
  3346. case RFID_SN_TYPE_4BYTE:
  3347. default:
  3348. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3349. break;
  3350. }
  3351. }
  3352. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3353. }
  3354. else if(ocpp_get_remotestart(gun_index))
  3355. {
  3356. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3357. ocpp_copy_userid_from_remotestart(gun_index);
  3358. DEBUG_INFO("Start Method : BACKEND...\n");
  3359. }
  3360. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  3361. {
  3362. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  3363. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  3364. DEBUG_INFO("Start Method : BLE...\n");
  3365. }
  3366. else
  3367. {
  3368. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  3369. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  3370. DEBUG_INFO("Start Method : FREE...\n");
  3371. }
  3372. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3373. ocpp_set_remotestart(gun_index, OFF);
  3374. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3375. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3376. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  3377. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3378. }
  3379. else
  3380. {}
  3381. break;
  3382. case SYS_MODE_AUTHORIZING:
  3383. if(isModeChange(gun_index))
  3384. {
  3385. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3386. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3387. {
  3388. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3389. {
  3390. case AUTH_MODE_ENABLE:
  3391. if(ocpp_get_connection_status())
  3392. {
  3393. // On line
  3394. ocpp_set_auth_req(ON, "ISO14443");
  3395. }
  3396. else
  3397. {
  3398. // Off line
  3399. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3400. {
  3401. case OFF_POLICY_LOCALLIST:
  3402. ocpp_set_auth_req(ON, "ISO14443");
  3403. break;
  3404. case OFF_POLICY_PH_RFID:
  3405. break;
  3406. case OFF_POLICY_FREE:
  3407. break;
  3408. case OFF_POLICY_NOCHARGE:
  3409. default:
  3410. break;
  3411. }
  3412. }
  3413. break;
  3414. case AUTH_MODE_DISABLE:
  3415. default:
  3416. break;
  3417. }
  3418. }
  3419. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3420. }
  3421. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  3422. {
  3423. // Authorization timeout process.
  3424. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3425. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3426. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3427. sleep(3);
  3428. setChargerMode(gun_index, SYS_MODE_IDLE);
  3429. DEBUG_WARN("Authorize timeout !!!\n");
  3430. }
  3431. else
  3432. {
  3433. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  3434. {
  3435. case START_METHOD_RFID:
  3436. if((ocpp_get_auth_conf()) ||
  3437. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  3438. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)))
  3439. {
  3440. if(ocpp_get_auth_result(gun_index) ||
  3441. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  3442. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  3443. {
  3444. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3445. DEBUG_INFO("Authorize pass.\n");
  3446. setSpeaker(ON,SPEAKER_SHORT);
  3447. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3448. sleep(3);
  3449. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3450. }
  3451. else
  3452. {
  3453. DEBUG_INFO("Authorize fail.\n");
  3454. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3455. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3456. sleep(3);
  3457. setChargerMode(gun_index, SYS_MODE_IDLE);
  3458. }
  3459. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3460. ocpp_set_auth_conf(OFF);
  3461. }
  3462. break;
  3463. case START_METHOD_BACKEND:
  3464. case START_METHOD_BLE:
  3465. case START_METHOD_FREE:
  3466. default:
  3467. setSpeaker(ON,SPEAKER_SHORT);
  3468. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3469. break;
  3470. }
  3471. }
  3472. break;
  3473. case SYS_MODE_PREPARING:
  3474. if(isModeChange(gun_index))
  3475. {
  3476. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3477. setRelay(gun_index, ON);
  3478. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3479. }
  3480. // If control pilot detect Bx, skip watch dog time out.
  3481. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  3482. {
  3483. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3484. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3485. }
  3486. // Unplug charging gun to Idle mode
  3487. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  3488. {
  3489. DEBUG_INFO("Charging gun is plugged before.\n");
  3490. setChargerMode(gun_index, SYS_MODE_IDLE);
  3491. }
  3492. // Use RFID card to stop handshaking
  3493. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  3494. {
  3495. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  3496. setSpeaker(ON,SPEAKER_SHORT);
  3497. setChargerMode(gun_index, SYS_MODE_IDLE);
  3498. }
  3499. else
  3500. {
  3501. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3502. }
  3503. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  3504. {
  3505. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  3506. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  3507. {
  3508. DEBUG_INFO("Handshaking timeout...\n");
  3509. setChargerMode(gun_index, SYS_MODE_IDLE);
  3510. }
  3511. }
  3512. else if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  3513. {
  3514. ocpp_set_unlocker_req(gun_index, OFF);
  3515. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  3516. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3517. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  3518. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3519. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3520. ocpp_copy_userid_to_starttransaction(gun_index);
  3521. ocpp_set_starttransaction_req(gun_index, ON);
  3522. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3523. }
  3524. break;
  3525. case SYS_MODE_CHARGING:
  3526. if(isModeChange(gun_index))
  3527. {
  3528. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3529. ftime(&startChargingTime[gun_index]);
  3530. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3531. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3532. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3533. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3534. ocpp_set_auth_req(OFF);
  3535. ocpp_reset_smartcharging_profileId(gun_index);
  3536. ocpp_set_profile_req(gun_index, ON);
  3537. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3538. }
  3539. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3540. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3541. ocpp_get_remotestop(gun_index) ||
  3542. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  3543. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  3544. ocpp_get_reset_req() ||
  3545. ocpp_get_unlocker_req(gun_index) ||
  3546. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  3547. {
  3548. if(ShmCharger->gun_info[gun_index].rfidReq)
  3549. {
  3550. DEBUG_INFO("Certified in charging mode... \n");
  3551. // If RFID SN different with start user, it need to authorize ID
  3552. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3553. {
  3554. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  3555. {
  3556. // Request authorization
  3557. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3558. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3559. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3560. {
  3561. // Big endian
  3562. switch(rfid.snType)
  3563. {
  3564. case RFID_SN_TYPE_6BYTE:
  3565. 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]);
  3566. break;
  3567. case RFID_SN_TYPE_7BYTE:
  3568. 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]);
  3569. break;
  3570. case RFID_SN_TYPE_10BYTE:
  3571. 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]);
  3572. break;
  3573. case RFID_SN_TYPE_4BYTE:
  3574. default:
  3575. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3576. break;
  3577. }
  3578. }
  3579. else
  3580. {
  3581. // Little endian
  3582. switch(rfid.snType)
  3583. {
  3584. case RFID_SN_TYPE_6BYTE:
  3585. 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]);
  3586. break;
  3587. case RFID_SN_TYPE_7BYTE:
  3588. 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]);
  3589. break;
  3590. case RFID_SN_TYPE_10BYTE:
  3591. 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]);
  3592. break;
  3593. case RFID_SN_TYPE_4BYTE:
  3594. default:
  3595. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3596. break;
  3597. }
  3598. }
  3599. DEBUG_INFO("End request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3600. DEBUG_INFO("Start method : %d\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3601. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3602. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3603. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3604. {
  3605. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3606. {
  3607. case AUTH_MODE_ENABLE:
  3608. if(ocpp_get_connection_status())
  3609. {
  3610. // On line
  3611. ocpp_set_auth_req(ON, "ISO14443");
  3612. }
  3613. else
  3614. {
  3615. // Off line
  3616. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3617. {
  3618. case OFF_POLICY_LOCALLIST:
  3619. ocpp_set_auth_req(ON, "ISO14443");
  3620. break;
  3621. case OFF_POLICY_PH_RFID:
  3622. break;
  3623. case OFF_POLICY_FREE:
  3624. break;
  3625. case OFF_POLICY_NOCHARGE:
  3626. default:
  3627. break;
  3628. }
  3629. }
  3630. break;
  3631. case AUTH_MODE_DISABLE:
  3632. default:
  3633. break;
  3634. }
  3635. }
  3636. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3637. {
  3638. DEBUG_INFO("Should be remote stop charger... \n");
  3639. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3640. }
  3641. }
  3642. else
  3643. {
  3644. if(ocpp_get_auth_conf() ||
  3645. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3646. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  3647. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3648. {
  3649. if(ocpp_get_auth_result(gun_index) ||
  3650. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3651. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3652. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3653. {
  3654. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3655. }
  3656. else
  3657. {
  3658. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3659. ocpp_set_auth_req(OFF);
  3660. DEBUG_INFO("Authorize fail.\n");
  3661. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3662. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3663. sleep(3);
  3664. }
  3665. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3666. ocpp_set_auth_conf(OFF);
  3667. }
  3668. }
  3669. }
  3670. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  3671. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  3672. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3673. }
  3674. else
  3675. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3676. }
  3677. else
  3678. {
  3679. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3680. ftime(&endChargingTime[gun_index]);
  3681. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  3682. 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;
  3683. // Response StartTransactionConf
  3684. ocpp_set_starttransaction_conf(gun_index, OFF);
  3685. // Charging profile preparation
  3686. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  3687. {
  3688. if(!ocpp_get_profile_conf(gun_index))
  3689. {
  3690. ocpp_set_profile_req(gun_index, ON);
  3691. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  3692. }
  3693. else
  3694. {
  3695. ocpp_set_profile_conf(gun_index, OFF);
  3696. }
  3697. }
  3698. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  3699. checkChargingProfileLimit(gun_index);
  3700. // Determine max charging current to MCU
  3701. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3702. {
  3703. 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);
  3704. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3705. {
  3706. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3707. {
  3708. 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);
  3709. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3710. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3711. }
  3712. else
  3713. {}
  3714. }
  3715. }
  3716. else
  3717. {
  3718. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3719. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3720. {
  3721. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3722. {
  3723. 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);
  3724. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3725. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3726. }
  3727. else
  3728. {}
  3729. }
  3730. }
  3731. // Debug information
  3732. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  3733. {
  3734. DEBUG_INFO("=============================================================\n");
  3735. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  3736. 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);
  3737. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  3738. DEBUG_INFO("=============================================================\n");
  3739. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  3740. }
  3741. if(ocpp_get_connection_status())
  3742. {
  3743. // On-line max condition check
  3744. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  3745. {
  3746. setRelay(gun_index, OFF);
  3747. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3748. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3749. 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));
  3750. }
  3751. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  3752. {
  3753. setRelay(gun_index, OFF);
  3754. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3755. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3756. 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));
  3757. }
  3758. else
  3759. {
  3760. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3761. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  3762. {
  3763. setLedMotion(gun_index,LED_ACTION_CHARGING);
  3764. setRelay(gun_index, ON);
  3765. }
  3766. else
  3767. {
  3768. setLedMotion(gun_index, LED_ACTION_STOP);
  3769. setRelay(gun_index, OFF);
  3770. }
  3771. }
  3772. }
  3773. else
  3774. {
  3775. // Off-line max condition check
  3776. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3777. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  3778. {
  3779. 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))))
  3780. {
  3781. setRelay(gun_index, OFF);
  3782. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3783. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3784. 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));
  3785. }
  3786. 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))))
  3787. {
  3788. setRelay(gun_index, OFF);
  3789. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  3790. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  3791. 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));
  3792. }
  3793. else
  3794. {
  3795. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  3796. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  3797. {
  3798. setLedMotion(gun_index,LED_ACTION_CHARGING);
  3799. setRelay(gun_index, ON);
  3800. }
  3801. else
  3802. {
  3803. setLedMotion(gun_index, LED_ACTION_STOP);
  3804. setRelay(gun_index, OFF);
  3805. }
  3806. }
  3807. }
  3808. else
  3809. {
  3810. setRelay(gun_index, OFF);
  3811. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  3812. }
  3813. }
  3814. }
  3815. break;
  3816. case SYS_MODE_TERMINATING:
  3817. if(isModeChange(gun_index))
  3818. {
  3819. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  3820. {
  3821. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  3822. }
  3823. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  3824. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3825. sleep(3);
  3826. }
  3827. // End authorize pass
  3828. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3829. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  3830. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3831. ocpp_get_remotestop(gun_index) ||
  3832. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  3833. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  3834. ocpp_get_reset_req() ||
  3835. ocpp_get_unlocker_req(gun_index) ||
  3836. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  3837. {
  3838. if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
  3839. {
  3840. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3841. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  3842. {
  3843. DEBUG_INFO("Authorize pass.\n");
  3844. setSpeaker(ON,SPEAKER_SHORT);
  3845. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3846. sleep(3);
  3847. }
  3848. }
  3849. else
  3850. {}
  3851. setRelay(gun_index, OFF);
  3852. setLedMotion(gun_index, LED_ACTION_STOP);
  3853. // If relay is off, the system should change to complete mode
  3854. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3855. {
  3856. checkStopReason(gun_index);
  3857. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3858. }
  3859. }
  3860. else
  3861. {
  3862. // If the charger stops charging, the led should be stoped blink
  3863. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3864. {
  3865. setLedMotion(gun_index, LED_ACTION_STOP);
  3866. }
  3867. if(ShmCharger->gun_info[gun_index].rfidReq)
  3868. {
  3869. DEBUG_INFO("Certified in terminating mode... \n");
  3870. // If RFID SN different with start user, it need to authorize ID
  3871. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3872. {
  3873. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  3874. {
  3875. // Request authorization
  3876. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3877. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3878. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3879. {
  3880. // Big endian
  3881. switch(rfid.snType)
  3882. {
  3883. case RFID_SN_TYPE_6BYTE:
  3884. 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]);
  3885. break;
  3886. case RFID_SN_TYPE_7BYTE:
  3887. 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]);
  3888. break;
  3889. case RFID_SN_TYPE_10BYTE:
  3890. 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]);
  3891. break;
  3892. case RFID_SN_TYPE_4BYTE:
  3893. default:
  3894. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3895. break;
  3896. }
  3897. }
  3898. else
  3899. {
  3900. // Little endian
  3901. switch(rfid.snType)
  3902. {
  3903. case RFID_SN_TYPE_6BYTE:
  3904. 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]);
  3905. break;
  3906. case RFID_SN_TYPE_7BYTE:
  3907. 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]);
  3908. break;
  3909. case RFID_SN_TYPE_10BYTE:
  3910. 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]);
  3911. break;
  3912. case RFID_SN_TYPE_4BYTE:
  3913. default:
  3914. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3915. break;
  3916. }
  3917. }
  3918. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3919. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3920. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3921. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3922. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3923. {
  3924. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3925. {
  3926. case AUTH_MODE_ENABLE:
  3927. if(ocpp_get_connection_status())
  3928. {
  3929. // On line
  3930. ocpp_set_auth_req(ON, "ISO14443");
  3931. }
  3932. else
  3933. {
  3934. // Off line
  3935. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3936. {
  3937. case OFF_POLICY_LOCALLIST:
  3938. ocpp_set_auth_req(ON, "ISO14443");
  3939. break;
  3940. case OFF_POLICY_PH_RFID:
  3941. break;
  3942. case OFF_POLICY_FREE:
  3943. break;
  3944. case OFF_POLICY_NOCHARGE:
  3945. default:
  3946. break;
  3947. }
  3948. }
  3949. break;
  3950. case AUTH_MODE_DISABLE:
  3951. default:
  3952. break;
  3953. }
  3954. }
  3955. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3956. {
  3957. DEBUG_INFO("Should be remote stop charger... \n");
  3958. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3959. }
  3960. }
  3961. else
  3962. {
  3963. if(ocpp_get_auth_conf() ||
  3964. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3965. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  3966. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3967. {
  3968. if(ocpp_get_auth_result(gun_index) ||
  3969. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3970. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3971. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3972. {
  3973. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3974. }
  3975. else
  3976. {
  3977. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3978. ocpp_set_auth_req(OFF);
  3979. DEBUG_INFO("Authorize fail... \n");
  3980. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3981. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3982. sleep(3);
  3983. }
  3984. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3985. ocpp_set_auth_conf(OFF);
  3986. }
  3987. }
  3988. }
  3989. }
  3990. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  3991. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  3992. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  3993. !ocpp_get_remotestop(gun_index) &&
  3994. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  3995. !ocpp_get_reset_req() &&
  3996. ocpp_get_starttransaction_result(gun_index) &&
  3997. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  3998. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  3999. !(!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)))) &&
  4000. !(!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)))))
  4001. {
  4002. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4003. }
  4004. }
  4005. break;
  4006. case SYS_MODE_COMPLETE:
  4007. if(isModeChange(gun_index))
  4008. {
  4009. setLedMotion(gun_index, LED_ACTION_STOP);
  4010. sleep(3);
  4011. }
  4012. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4013. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4014. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4015. ocpp_set_remotestop(gun_index, OFF);
  4016. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4017. DB_Insert_Record(localDb, gun_index);
  4018. setChargerMode(gun_index, SYS_MODE_IDLE);
  4019. break;
  4020. case SYS_MODE_ALARM:
  4021. setLedMotion(gun_index,LED_ACTION_ALARM);
  4022. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4023. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4024. {
  4025. }
  4026. else
  4027. {
  4028. setRelay(gun_index, OFF);
  4029. }
  4030. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4031. {
  4032. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4033. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4034. {
  4035. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4036. }
  4037. else
  4038. {
  4039. setChargerMode(gun_index, SYS_MODE_IDLE);
  4040. }
  4041. }
  4042. break;
  4043. case SYS_MODE_FAULT:
  4044. if(isModeChange(gun_index))
  4045. {}
  4046. break;
  4047. case SYS_MODE_MAINTAIN:
  4048. if(isModeChange(gun_index))
  4049. {
  4050. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4051. }
  4052. break;
  4053. case SYS_MODE_UPDATE:
  4054. if(isModeChange(gun_index))
  4055. {
  4056. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4057. }
  4058. //======================================
  4059. // Check local upgrade firmware request
  4060. //======================================
  4061. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  4062. {
  4063. DEBUG_INFO("Firmware local upgrading...\n");
  4064. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  4065. int value = upgrade_check();
  4066. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  4067. if(value == PASS)
  4068. {
  4069. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4070. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4071. ShmCharger->isUpdateSuccess = YES;
  4072. DEBUG_INFO("Local update success...\n");
  4073. }
  4074. else
  4075. {
  4076. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4077. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4078. ShmCharger->isUpdateSuccess = NO;
  4079. DEBUG_INFO("Local update unsuccess...\n");
  4080. }
  4081. }
  4082. else if(ocpp_get_update_firmware_req())
  4083. {
  4084. //======================================
  4085. // Check remote upgrade firmware request
  4086. //======================================
  4087. checkRemoteUpgradeStatus();
  4088. }
  4089. else
  4090. {
  4091. //======================================
  4092. // Upgrade complete reboot system
  4093. //======================================
  4094. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  4095. {
  4096. if(ShmCharger->isUpdateSuccess == YES)
  4097. {
  4098. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4099. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4100. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  4101. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4102. DEBUG_WARN("Firmware upgrade success.\n");
  4103. sleep(5);
  4104. DEBUG_INFO("Firmware upgraded, reboot...\n");
  4105. system("reboot -f");
  4106. sleep(5);
  4107. system("reboot -f");
  4108. }
  4109. else
  4110. {
  4111. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4112. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4113. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  4114. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4115. DEBUG_WARN("Firmware upgrade fail.\n");
  4116. sleep(5);
  4117. system("rm -rvf /mnt/* ");
  4118. close(wtdFd);
  4119. system("/usr/bin/run_evse_restart.sh");
  4120. }
  4121. }
  4122. }
  4123. break;
  4124. case SYS_MODE_RESERVATION:
  4125. if(isModeChange(gun_index))
  4126. {
  4127. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4128. }
  4129. if(isReservationExpired(gun_index))
  4130. {
  4131. DEBUG_INFO("Reservation: Time's up...\n");
  4132. setChargerMode(gun_index, SYS_MODE_IDLE);
  4133. }
  4134. else if(ocpp_get_cancelreservation_req(gun_index))
  4135. {
  4136. DEBUG_INFO("Reservation: Cancel reservation...\n");
  4137. setChargerMode(gun_index, SYS_MODE_IDLE);
  4138. ocpp_set_cancelreservation_req(gun_index, OFF);
  4139. }
  4140. else
  4141. {
  4142. // Check is there RFID or back end request start
  4143. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4144. ocpp_get_remotestart(gun_index))
  4145. {
  4146. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4147. {
  4148. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4149. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4150. {
  4151. // Big endian
  4152. switch(rfid.snType)
  4153. {
  4154. case RFID_SN_TYPE_6BYTE:
  4155. 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]);
  4156. break;
  4157. case RFID_SN_TYPE_7BYTE:
  4158. 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]);
  4159. break;
  4160. case RFID_SN_TYPE_10BYTE:
  4161. 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]);
  4162. break;
  4163. case RFID_SN_TYPE_4BYTE:
  4164. default:
  4165. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4166. break;
  4167. }
  4168. }
  4169. else
  4170. {
  4171. // Little endian
  4172. switch(rfid.snType)
  4173. {
  4174. case RFID_SN_TYPE_6BYTE:
  4175. 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]);
  4176. break;
  4177. case RFID_SN_TYPE_7BYTE:
  4178. 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]);
  4179. break;
  4180. case RFID_SN_TYPE_10BYTE:
  4181. 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]);
  4182. break;
  4183. case RFID_SN_TYPE_4BYTE:
  4184. default:
  4185. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4186. break;
  4187. }
  4188. }
  4189. if(ocpp_compare_reserve_id_with_user(gun_index))
  4190. {
  4191. DEBUG_INFO("Start Method in reservation : RFID...\n");
  4192. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4193. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4194. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4195. }
  4196. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4197. }
  4198. else if(ocpp_get_remotestart(gun_index))
  4199. {
  4200. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  4201. {
  4202. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  4203. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4204. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4205. }
  4206. ocpp_set_remotestop(gun_index, OFF);
  4207. }
  4208. }
  4209. }
  4210. break;
  4211. case SYS_MODE_BOOKING:
  4212. if(isModeChange(gun_index))
  4213. {}
  4214. break;
  4215. case SYS_MODE_DEBUG:
  4216. if(isModeChange(gun_index))
  4217. {
  4218. setLedMotion(gun_index,LED_ACTION_DEBUG);
  4219. }
  4220. break;
  4221. }
  4222. }
  4223. // System watch dog reset
  4224. write(wtdFd, "a", 1);
  4225. usleep(50000);
  4226. }
  4227. return FAIL;
  4228. }