main.c 191 KB

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