main.c 202 KB

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