main.c 189 KB

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