main.c 196 KB

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