main.c 196 KB

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