main.c 163 KB

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