main.c 188 KB

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