main.c 203 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474
  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. int presentChargedEnergyClear(unsigned char gun_index)
  1823. {
  1824. int result = FAIL;
  1825. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  1826. memset(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  1827. result = PASS;
  1828. return result;
  1829. }
  1830. float presentChargedEnergyTotal(unsigned char gun_index)
  1831. {
  1832. float result = 0.0f;
  1833. for(int idx=0;idx<ARRAY_SIZE(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod);idx++)
  1834. {
  1835. result += ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[idx];
  1836. }
  1837. return result;
  1838. }
  1839. int presentChargedEnergyUpdate(unsigned char gun_index)
  1840. {
  1841. int result = FAIL;
  1842. time_t CurrentTime;
  1843. struct tm *tm;
  1844. CurrentTime = time(NULL);
  1845. tm=localtime(&CurrentTime);
  1846. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  1847. {
  1848. // Resolution: 0.01 kwh
  1849. ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += (((float)(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start))/10.0) - presentChargedEnergyTotal(gun_index);
  1850. }
  1851. else
  1852. {
  1853. // Resolution: 0.01 kwh
  1854. ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += ((((float)(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start))/10.0) +
  1855. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/10.0) +
  1856. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/10.0))
  1857. - presentChargedEnergyTotal(gun_index);
  1858. }
  1859. // Resolution: 0.1 kwh
  1860. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = presentChargedEnergyTotal(gun_index)/10.0;
  1861. return result;
  1862. }
  1863. //===============================================
  1864. // Get firmware version
  1865. //===============================================
  1866. void get_firmware_version(unsigned char gun_index)
  1867. {
  1868. FILE *fp;
  1869. char cmd[512];
  1870. char buf[512];
  1871. // Get CSU hardware version
  1872. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  1873. // Get CSU boot loader version
  1874. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1875. // Get CSU kernel version
  1876. sprintf(cmd, "/bin/uname -r");
  1877. fp = popen(cmd, "r");
  1878. if(fp == NULL)
  1879. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1880. else
  1881. {
  1882. while(fgets(buf, sizeof(buf), fp) != NULL)
  1883. {
  1884. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  1885. }
  1886. }
  1887. // Get MCU firmware version
  1888. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1889. // Get CSU root file system version
  1890. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.49.00.0000.00");
  1891. // Get AC connector type from model name
  1892. for(uint8_t idx=0;idx<3;idx++)
  1893. {
  1894. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1895. {
  1896. case '1':
  1897. // J1772 Plug
  1898. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1899. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1900. break;
  1901. case '2':
  1902. // J1772 Socket
  1903. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1904. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1905. break;
  1906. case '3':
  1907. // CE Plug
  1908. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1909. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1910. break;
  1911. case '4':
  1912. // CE Socket
  1913. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1914. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1915. break;
  1916. case '5':
  1917. // GB Plug
  1918. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1919. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1920. break;
  1921. case '6':
  1922. // GB Socket
  1923. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1924. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1925. break;
  1926. }
  1927. }
  1928. // Get network option from model name
  1929. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1930. {
  1931. case 'B':
  1932. case 'U':
  1933. //Blue tooth
  1934. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1935. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1936. break;
  1937. case 'W':
  1938. // WIFI
  1939. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1940. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1941. break;
  1942. case 'T':
  1943. // 3G/4G
  1944. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1945. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1946. break;
  1947. default:
  1948. // LAN
  1949. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1950. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1951. break;
  1952. }
  1953. // Get rating power from model name
  1954. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1955. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1956. // Get vender code from model name
  1957. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1958. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1959. DEBUG_INFO("========================================\n");
  1960. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1961. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  1962. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  1963. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  1964. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1965. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1966. DEBUG_INFO("========================================\n");
  1967. }
  1968. //===============================================
  1969. // Upgrade firmware
  1970. //===============================================
  1971. int upgrade_check()
  1972. {
  1973. int result = PASS;
  1974. int fd;
  1975. DIR *dir;
  1976. struct dirent *file;
  1977. char cmd[512];
  1978. long int MaxLen=48*1024*1024;
  1979. if ((dir = opendir ("/mnt")) != NULL)
  1980. {
  1981. /* print all the files and directories within directory */
  1982. while ((file = readdir (dir)) != NULL)
  1983. {
  1984. if((strlen(file->d_name)>2))
  1985. {
  1986. // Wait for MCU upgrade finish.
  1987. while(ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (AC_QUANTITY>1?ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq:FALSE))sleep(1);
  1988. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1989. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1990. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1991. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1992. {
  1993. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1994. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1995. read(fd, ptr, MaxLen);
  1996. close(fd);
  1997. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1998. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1999. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2000. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2001. if((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0]) &&
  2002. (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1]) &&
  2003. (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7]) &&
  2004. (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8]) &&
  2005. (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9]) &&
  2006. (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11]) &&
  2007. (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12]) &&
  2008. (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13]) &&
  2009. (ShmCharger->fwUpgradeInfo.fwType>0))
  2010. {
  2011. switch(ShmCharger->fwUpgradeInfo.fwType)
  2012. {
  2013. case CSU_BOOTLOADER:
  2014. case CSU_KERNEL_CONFIGURATION:
  2015. case CSU_KERNEL_IMAGE:
  2016. case CSU_ROOT_FILE_SYSTEM:
  2017. case CSU_USER_CONFIGURATION:
  2018. case CSU_PRIMARY_CONTROLLER:
  2019. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2020. {
  2021. result = FAIL;
  2022. }
  2023. else
  2024. {
  2025. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2026. {
  2027. DEBUG_INFO("Restore model name & serial number.\n");
  2028. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2029. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2030. {
  2031. DEBUG_INFO("Re-load configuration fail.\n");
  2032. result = FAIL;
  2033. }
  2034. else
  2035. {
  2036. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2037. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2038. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2039. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2040. {
  2041. DEBUG_INFO("Re-write configuration fail.\n");
  2042. result = FAIL;
  2043. }
  2044. else
  2045. DEBUG_INFO("Re-write configuration OK.\n");
  2046. }
  2047. }
  2048. }
  2049. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2050. system(cmd);
  2051. break;
  2052. case AC_WALLMOUNT_CONTROLLER:
  2053. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2054. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2055. break;
  2056. default:
  2057. result = FAIL;
  2058. DEBUG_WARN("Image file is unknown type.\n");
  2059. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2060. system(cmd);
  2061. break;
  2062. }
  2063. }
  2064. else
  2065. {
  2066. result = FAIL;
  2067. DEBUG_ERROR("Model name and Firmware type error.\n");
  2068. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2069. system(cmd);
  2070. }
  2071. free(ptr);
  2072. }
  2073. else
  2074. {
  2075. result = FAIL;
  2076. DEBUG_ERROR("New firmware open error.\n");
  2077. }
  2078. }
  2079. else
  2080. {
  2081. if(strlen(file->d_name) >= 3)
  2082. {
  2083. result = FAIL;
  2084. DEBUG_ERROR("File name error.\n");
  2085. }
  2086. else
  2087. {
  2088. DEBUG_ERROR("Searching file.\n");
  2089. }
  2090. }
  2091. }
  2092. closedir (dir);
  2093. }
  2094. else
  2095. {
  2096. result = FAIL;
  2097. DEBUG_ERROR("/mnt does not valid.\n");
  2098. }
  2099. return result;
  2100. }
  2101. //===============================================
  2102. // Check RFID is match with start user
  2103. //===============================================
  2104. int isMatchStartUser(unsigned char gun_index)
  2105. {
  2106. uint8_t tmpUser[32];
  2107. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2108. {
  2109. // Big endian
  2110. switch(rfid.snType)
  2111. {
  2112. case RFID_SN_TYPE_6BYTE:
  2113. 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]);
  2114. break;
  2115. case RFID_SN_TYPE_7BYTE:
  2116. 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]);
  2117. break;
  2118. case RFID_SN_TYPE_10BYTE:
  2119. 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]);
  2120. break;
  2121. case RFID_SN_TYPE_4BYTE:
  2122. default:
  2123. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2124. break;
  2125. }
  2126. }
  2127. else
  2128. {
  2129. // Little endian
  2130. switch(rfid.snType)
  2131. {
  2132. case RFID_SN_TYPE_6BYTE:
  2133. 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]);
  2134. break;
  2135. case RFID_SN_TYPE_7BYTE:
  2136. 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]);
  2137. break;
  2138. case RFID_SN_TYPE_10BYTE:
  2139. 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]);
  2140. break;
  2141. case RFID_SN_TYPE_4BYTE:
  2142. default:
  2143. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2144. break;
  2145. }
  2146. }
  2147. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2148. }
  2149. //===============================================
  2150. // Read RFID Serial Number
  2151. //===============================================
  2152. int GetCardSerialNumber()
  2153. {
  2154. int isSuccess = FAIL;
  2155. int module_type = MODULE_EWT;
  2156. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2157. {
  2158. if(rfid.cardType == ISO14443A)
  2159. {
  2160. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2161. {
  2162. isSuccess = PASS;
  2163. }
  2164. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2165. {
  2166. isSuccess = PASS;
  2167. }
  2168. sethaltCard(rfidFd,module_type);
  2169. }
  2170. else if(rfid.cardType == IS014443B)
  2171. {
  2172. isSuccess = PASS;
  2173. }
  2174. else if(rfid.cardType == FELICA)
  2175. {
  2176. isSuccess = PASS;
  2177. }
  2178. else
  2179. {}
  2180. }
  2181. else
  2182. {}
  2183. return isSuccess;
  2184. }
  2185. //===============================================
  2186. // Set led motion
  2187. //===============================================
  2188. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2189. {
  2190. switch(led_mode)
  2191. {
  2192. case LED_ACTION_INIT:
  2193. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2194. break;
  2195. case LED_ACTION_IDLE:
  2196. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2197. break;
  2198. case LED_ACTION_AUTHED:
  2199. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2200. break;
  2201. case LED_ACTION_CONNECTED:
  2202. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2203. break;
  2204. case LED_ACTION_CHARGING:
  2205. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2206. break;
  2207. case LED_ACTION_STOP:
  2208. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2209. break;
  2210. case LED_ACTION_ALARM:
  2211. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2212. break;
  2213. case LED_ACTION_MAINTAIN:
  2214. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2215. break;
  2216. case LED_ACTION_RFID_PASS:
  2217. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2218. break;
  2219. case LED_ACTION_RFID_FAIL:
  2220. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2221. break;
  2222. case LED_ACTION_BLE_CONNECT:
  2223. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2224. break;
  2225. case LED_ACTION_BLE_DISABLE:
  2226. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2227. break;
  2228. case LED_ACTION_DEBUG:
  2229. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2230. break;
  2231. case LED_ACTION_ALL_OFF:
  2232. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2233. break;
  2234. case LED_RELAY_ON:
  2235. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2236. break;
  2237. case LED_RELAY_OFF:
  2238. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2239. break;
  2240. case LED_ACTION_HANDSHAKE_FAIL:
  2241. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2242. break;
  2243. case LED_ACTION_INTERNET_DISCONNECT:
  2244. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2245. break;
  2246. }
  2247. }
  2248. //===============================================
  2249. // Request on/off set
  2250. //===============================================
  2251. void setRequest(unsigned char gun_index,unsigned char isOn)
  2252. {
  2253. if(isOn == ON)
  2254. {
  2255. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2256. {
  2257. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2258. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  2259. }
  2260. }
  2261. else
  2262. {
  2263. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2264. {
  2265. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2266. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  2267. }
  2268. }
  2269. }
  2270. //===============================================
  2271. // Request on/off get
  2272. //===============================================
  2273. int getRequest(unsigned char gun_index)
  2274. {
  2275. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2276. }
  2277. //===============================================
  2278. // Relay on/off set
  2279. //===============================================
  2280. void setRelay(unsigned char gun_index,unsigned char isOn)
  2281. {
  2282. if(isOn == ON)
  2283. {
  2284. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  2285. {
  2286. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  2287. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  2288. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  2289. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  2290. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  2291. }
  2292. }
  2293. else
  2294. {
  2295. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  2296. {
  2297. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  2298. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  2299. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  2300. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  2301. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  2302. }
  2303. }
  2304. }
  2305. //===============================================
  2306. // Relay on/off get
  2307. //===============================================
  2308. int getRelay(unsigned char gun_index)
  2309. {
  2310. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  2311. }
  2312. //===============================================
  2313. // Set speaker on/off request
  2314. //===============================================
  2315. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2316. {
  2317. if(isOn == ON)
  2318. {
  2319. ShmCharger->isSpeakerOn = ON;
  2320. ShmCharger->speaker_type = speaker_mode;
  2321. }
  2322. }
  2323. //===============================================
  2324. // Initialization RFID communication port
  2325. //===============================================
  2326. int InitRfidPort()
  2327. {
  2328. int uartO2 = open(rfidPortName, O_RDWR);
  2329. struct termios tios;
  2330. if (uartO2 != FAIL)
  2331. {
  2332. ioctl (uartO2, TCGETS, &tios);
  2333. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2334. tios.c_lflag = 0;
  2335. tios.c_iflag = 0;
  2336. tios.c_oflag = 0;
  2337. tios.c_cc[VMIN] = 0;
  2338. tios.c_cc[VTIME] = (unsigned char) 1;
  2339. tios.c_lflag = 0;
  2340. tcflush(uartO2, TCIFLUSH);
  2341. ioctl(uartO2, TCSETS, &tios);
  2342. }
  2343. if (uartO2 < 0)
  2344. {
  2345. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2346. }
  2347. return uartO2;
  2348. }
  2349. //===============================================
  2350. // Check internet access status
  2351. //===============================================
  2352. int isReachableInternet()
  2353. {
  2354. int result = FAIL;
  2355. FILE *fp;
  2356. char cmd[256];
  2357. char buf[512];
  2358. char tmp[512];
  2359. // Get ip address & net mask
  2360. strcpy(cmd, "ifconfig eth0");
  2361. fp = popen(cmd, "r");
  2362. if (fp != NULL)
  2363. {
  2364. while(fgets(buf, sizeof(buf), fp) != NULL)
  2365. {
  2366. if (strstr(buf, "inet addr:") > 0)
  2367. {
  2368. sscanf(buf, "%*s%s", tmp);
  2369. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2370. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  2371. {
  2372. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  2373. }
  2374. sscanf(buf, "%*s%*s%*s%s", tmp);
  2375. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2376. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  2377. {
  2378. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  2379. }
  2380. }
  2381. }
  2382. }
  2383. pclose(fp);
  2384. memset(buf, 0x00, sizeof(buf));
  2385. // Get gateway
  2386. fp = popen("ip route", "r");
  2387. if(fp == NULL)
  2388. result = FAIL;
  2389. else
  2390. {
  2391. while(fgets(buf, sizeof(buf), fp) != NULL)
  2392. {
  2393. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2394. break;
  2395. }
  2396. if(strstr(buf, "default") != NULL)
  2397. {
  2398. sscanf(buf, "%*s%*s%s", tmp);
  2399. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2400. }
  2401. }
  2402. pclose(fp);
  2403. memset(buf, 0x00, sizeof(buf));
  2404. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2405. {
  2406. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2407. fp = popen(cmd, "r");
  2408. if(fp != NULL)
  2409. {
  2410. while(fgets(buf, sizeof(buf), fp) != NULL)
  2411. {
  2412. if(strstr(buf, "transmitted") > 0)
  2413. {
  2414. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2415. if(strstr(buf,"100%") != NULL)
  2416. {
  2417. }
  2418. else
  2419. {
  2420. result = PASS;
  2421. }
  2422. //DEBUG_INFO("%s",buf);
  2423. //DEBUG_INFO("%s\n",tmp);
  2424. }
  2425. }
  2426. }
  2427. pclose(fp);
  2428. }
  2429. return result;
  2430. }
  2431. int isRouteFail()
  2432. {
  2433. int result = YES;
  2434. FILE *fp;
  2435. char buf[512];
  2436. fp = popen("route -n", "r");
  2437. if(fp != NULL)
  2438. {
  2439. while(fgets(buf, sizeof(buf), fp) != NULL)
  2440. {
  2441. if(strstr(buf, "eth0") != NULL)
  2442. result = NO;
  2443. }
  2444. }
  2445. pclose(fp);
  2446. return result;
  2447. }
  2448. //===============================================
  2449. // Check reservation date is expired
  2450. //===============================================
  2451. int isReservationExpired(unsigned char gun_index)
  2452. {
  2453. int result = NO;
  2454. struct tm expiredDate;
  2455. struct timeb expiredTime;
  2456. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2457. {
  2458. 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)
  2459. {
  2460. expiredDate.tm_year -= 1900;
  2461. expiredDate.tm_mon -= 1;
  2462. expiredTime.time = mktime(&expiredDate);
  2463. if(DiffTimebWithNow(expiredTime)>=0)
  2464. {
  2465. result = YES;
  2466. }
  2467. }
  2468. else
  2469. {
  2470. DEBUG_WARN("Expired date parsing error.\n");
  2471. DEBUG_INFO("Date expired.\n");
  2472. result = YES;
  2473. }
  2474. }
  2475. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2476. {
  2477. 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)
  2478. {
  2479. expiredDate.tm_year -= 1900;
  2480. expiredDate.tm_mon -= 1;
  2481. expiredTime.time = mktime(&expiredDate);
  2482. if(DiffTimebWithNow(expiredTime)>=0)
  2483. {
  2484. result = YES;
  2485. }
  2486. }
  2487. else
  2488. {
  2489. DEBUG_WARN("Expired date parsing error.\n");
  2490. DEBUG_INFO("Date expired.\n");
  2491. result = YES;
  2492. }
  2493. }
  2494. return result;
  2495. }
  2496. //===============================================
  2497. // Check charging profile related date routine
  2498. //===============================================
  2499. int isProfileValid(uint8_t gun_index)
  2500. {
  2501. int result = NO;
  2502. struct tm tmFrom, tmTo;
  2503. struct timeb tbFrom, tbTo;
  2504. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2505. {
  2506. 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) &&
  2507. (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))
  2508. {
  2509. tmFrom.tm_year -= 1900;
  2510. tmFrom.tm_mon -= 1;
  2511. tbFrom.time = mktime(&tmFrom);
  2512. tmTo.tm_year -= 1900;
  2513. tmTo.tm_mon -= 1;
  2514. tbTo.time = mktime(&tmTo);
  2515. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2516. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2517. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2518. {
  2519. result = YES;
  2520. }
  2521. }
  2522. else
  2523. {
  2524. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2525. }
  2526. }
  2527. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2528. {
  2529. 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) &&
  2530. (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))
  2531. {
  2532. tmFrom.tm_year -= 1900;
  2533. tmFrom.tm_mon -= 1;
  2534. tbFrom.time = mktime(&tmFrom);
  2535. tmTo.tm_year -= 1900;
  2536. tmTo.tm_mon -= 1;
  2537. tbTo.time = mktime(&tmTo);
  2538. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  2539. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  2540. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  2541. {
  2542. result = YES;
  2543. }
  2544. }
  2545. else
  2546. {
  2547. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  2548. }
  2549. }
  2550. return result;
  2551. }
  2552. int getScheduleStart(int gun_index)
  2553. {
  2554. int result = -1;
  2555. struct tm tmScheduleStart;;
  2556. struct timeb tbScheduleStart;
  2557. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2558. {
  2559. 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))
  2560. {
  2561. tmScheduleStart.tm_year -= 1900;
  2562. tmScheduleStart.tm_mon -= 1;
  2563. tbScheduleStart.time = mktime(&tmScheduleStart);
  2564. tbScheduleStart.millitm = 0;
  2565. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2566. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2567. }
  2568. else
  2569. {
  2570. DEBUG_WARN("Schedule start date parsing error.\n");
  2571. }
  2572. }
  2573. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2574. {
  2575. 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))
  2576. {
  2577. tmScheduleStart.tm_year -= 1900;
  2578. tmScheduleStart.tm_mon -= 1;
  2579. tbScheduleStart.time = mktime(&tmScheduleStart);
  2580. tbScheduleStart.millitm = 0;
  2581. result = DiffTimebWithNow(tbScheduleStart)/1000;
  2582. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  2583. }
  2584. else
  2585. {
  2586. DEBUG_WARN("Schedule start date parsing error.\n");
  2587. }
  2588. }
  2589. return result;
  2590. }
  2591. int getStartSinceToday()
  2592. {
  2593. int result = -1;
  2594. time_t t;
  2595. struct tm *tmStartToday;
  2596. struct timeb tbStartToday;
  2597. t=time(NULL);
  2598. tmStartToday=localtime(&t);
  2599. tmStartToday->tm_hour = 0;
  2600. tmStartToday->tm_min = 0;
  2601. tmStartToday->tm_sec = 0;
  2602. tbStartToday.time = mktime(tmStartToday);
  2603. result = DiffTimebWithNow(tbStartToday)/1000;
  2604. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  2605. return result;
  2606. }
  2607. int getStartSinceWeek()
  2608. {
  2609. int result = -1;
  2610. time_t t;
  2611. struct tm *tmStartWeek;
  2612. struct timeb tbStartWeek;
  2613. t=time(NULL);
  2614. tmStartWeek=localtime(&t);
  2615. t-=86400*tmStartWeek->tm_wday;
  2616. tmStartWeek=localtime(&t);
  2617. tmStartWeek->tm_hour = 0;
  2618. tmStartWeek->tm_min = 0;
  2619. tmStartWeek->tm_sec = 0;
  2620. tbStartWeek.time = mktime(tmStartWeek);
  2621. result = DiffTimebWithNow(tbStartWeek)/1000;
  2622. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  2623. return result;
  2624. }
  2625. //===============================================
  2626. // Valid from local white list
  2627. //===============================================
  2628. int isValidLocalWhiteCard()
  2629. {
  2630. uint8_t result = FAIL;
  2631. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  2632. {
  2633. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  2634. {
  2635. result = PASS;
  2636. }
  2637. }
  2638. return result;
  2639. }
  2640. //==========================================
  2641. // Check routine
  2642. //==========================================
  2643. void checkTask()
  2644. {
  2645. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2646. {
  2647. if(system("pidof -s Module_4g > /dev/null") != 0)
  2648. {
  2649. DEBUG_INFO("Module_4g not running, restart it.\n");
  2650. system("/root/Module_4g &");
  2651. }
  2652. }
  2653. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2654. {
  2655. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  2656. {
  2657. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  2658. system("/root/Module_Wifi &");
  2659. }
  2660. }
  2661. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  2662. {
  2663. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  2664. system("/root/Module_EventLogging &");
  2665. }
  2666. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2667. {
  2668. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2669. {
  2670. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  2671. {
  2672. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  2673. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2674. system("pkill OcppBackend");
  2675. }
  2676. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2677. {
  2678. DEBUG_INFO("OcppBackend not running, restart it.\n");
  2679. ocpp_process_start();
  2680. }
  2681. }
  2682. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2683. {
  2684. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  2685. {
  2686. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  2687. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2688. system("pkill OcppBackend20");
  2689. }
  2690. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  2691. {
  2692. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  2693. ocpp_process_start();
  2694. }
  2695. }
  2696. }
  2697. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  2698. {
  2699. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  2700. system("/root/Module_AlarmDetect &");
  2701. }
  2702. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  2703. {
  2704. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  2705. system("/root/Module_InternalComm &");
  2706. }
  2707. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  2708. {
  2709. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  2710. system("/root/Module_Speaker &");
  2711. }
  2712. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  2713. {
  2714. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  2715. system ("/root/Module_ProduceUtils &");
  2716. }
  2717. if(system("pidof -s Module_LcmControl > /dev/null") != 0)
  2718. {
  2719. DEBUG_INFO("Module_LcmControl not running, restart it.\n");
  2720. system ("/root/Module_LcmControl &");
  2721. }
  2722. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  2723. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  2724. {
  2725. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  2726. system ("/root/Module_PowerSharing &");
  2727. }
  2728. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  2729. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  2730. {
  2731. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  2732. system ("/root/Module_InitUpgrade &");
  2733. }
  2734. }
  2735. void checkConnectionTimeout()
  2736. {
  2737. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  2738. {
  2739. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  2740. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  2741. }
  2742. else
  2743. {
  2744. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  2745. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  2746. {
  2747. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10000);
  2748. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  2749. }
  2750. else
  2751. {
  2752. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  2753. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  2754. }
  2755. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  2756. }
  2757. }
  2758. void checkReset()
  2759. {
  2760. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2761. {
  2762. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2763. {
  2764. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2765. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2766. {
  2767. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2768. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2769. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2770. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2771. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2772. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2773. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2774. {
  2775. system("sync");
  2776. sleep(5);
  2777. system("reboot -f");
  2778. sleep(5);
  2779. system("reboot -f");
  2780. }
  2781. else
  2782. {
  2783. sleep(5);
  2784. close(wtdFd);
  2785. system("/usr/bin/run_evse_restart.sh");
  2786. }
  2787. }
  2788. }
  2789. }
  2790. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2791. {
  2792. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  2793. {
  2794. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2795. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  2796. {
  2797. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  2798. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  2799. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  2800. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  2801. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2802. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2803. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  2804. {
  2805. system("sync");
  2806. sleep(5);
  2807. system("reboot -f");
  2808. sleep(5);
  2809. system("reboot -f");
  2810. }
  2811. else
  2812. {
  2813. sleep(5);
  2814. close(wtdFd);
  2815. system("/usr/bin/run_evse_restart.sh");
  2816. }
  2817. }
  2818. }
  2819. }
  2820. }
  2821. void checkReservation(uint8_t gun_index)
  2822. {
  2823. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2824. {
  2825. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2826. {
  2827. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2828. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2829. {
  2830. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2831. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2832. }
  2833. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2834. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2835. }
  2836. }
  2837. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2838. {
  2839. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  2840. {
  2841. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2842. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2843. {
  2844. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  2845. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2846. }
  2847. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2848. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2849. }
  2850. }
  2851. }
  2852. void checkUnlocker(uint8_t gun_index)
  2853. {
  2854. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2855. {
  2856. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2857. {
  2858. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2859. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2860. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2861. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2862. }
  2863. }
  2864. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2865. {
  2866. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2867. {
  2868. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2869. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "NotSupported");
  2870. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  2871. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  2872. }
  2873. }
  2874. }
  2875. void checkAvailability(uint8_t gun_index)
  2876. {
  2877. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2878. {
  2879. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2880. {
  2881. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2882. {
  2883. DB_Update_Operactive(localDb, gun_index, true);
  2884. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2885. }
  2886. else
  2887. {
  2888. DB_Update_Operactive(localDb, gun_index, false);
  2889. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2890. }
  2891. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2892. }
  2893. }
  2894. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2895. {
  2896. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2897. {
  2898. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  2899. {
  2900. DB_Update_Operactive(localDb, gun_index, true);
  2901. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2902. }
  2903. else
  2904. {
  2905. DB_Update_Operactive(localDb, gun_index, false);
  2906. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2907. }
  2908. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2909. }
  2910. }
  2911. }
  2912. void checkChargingProfileLimit(uint8_t gun_index)
  2913. {
  2914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2915. {
  2916. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2917. (ocpp_get_profile_req(gun_index) != ON) &&
  2918. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2919. {
  2920. // Debug information
  2921. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2922. {
  2923. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2924. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2925. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2926. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2927. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2928. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2929. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2930. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2931. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2932. DEBUG_INFO("===============================================================\n");
  2933. }
  2934. // Checking profile kind
  2935. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2936. {
  2937. // Absolute profile
  2938. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2939. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2940. {
  2941. // Checking limitation
  2942. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2943. {
  2944. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2945. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  2946. )
  2947. {
  2948. 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);
  2949. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2950. }
  2951. else
  2952. break;
  2953. }
  2954. }
  2955. }
  2956. }
  2957. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2958. {
  2959. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2960. }
  2961. }
  2962. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2963. {
  2964. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  2965. (ocpp_get_profile_req(gun_index) != ON) &&
  2966. (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
  2967. {
  2968. // Debug information
  2969. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2970. {
  2971. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].id);
  2972. DEBUG_INFO("Valid from: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom);
  2973. DEBUG_INFO("Valid to: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].validTo);
  2974. DEBUG_INFO("Start schedule: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  2975. DEBUG_INFO("Profile kind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind);
  2976. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].recurrencyKind);
  2977. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose);
  2978. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId);
  2979. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit);
  2980. DEBUG_INFO("===============================================================\n");
  2981. }
  2982. // Checking profile kind
  2983. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  2984. {
  2985. // Absolute profile
  2986. if(((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId == ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId)) ||
  2987. (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == FAIL))
  2988. {
  2989. // Checking limitation
  2990. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  2991. {
  2992. if((getScheduleStart(gun_index) > ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  2993. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  2994. )
  2995. {
  2996. 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);
  2997. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2998. }
  2999. else
  3000. break;
  3001. }
  3002. }
  3003. }
  3004. }
  3005. else if((ShmOCPP20Data->SmartChargingProfile[gun_index].id == 0) && (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  3006. {
  3007. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3008. }
  3009. }
  3010. }
  3011. void checkStopReason(uint8_t gun_index)
  3012. {
  3013. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3014. {
  3015. if(!ocpp_get_starttransaction_result(gun_index))
  3016. {
  3017. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  3018. }
  3019. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3020. {
  3021. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3022. }
  3023. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3024. {
  3025. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3026. }
  3027. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3028. {
  3029. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3030. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3031. else
  3032. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3033. }
  3034. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3035. {
  3036. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3037. }
  3038. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3039. {
  3040. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3041. }
  3042. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3043. {
  3044. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3045. }
  3046. else
  3047. {
  3048. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3049. }
  3050. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3051. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3052. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3053. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3054. }
  3055. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3056. {
  3057. if(!ocpp_get_starttransaction_result(gun_index))
  3058. {
  3059. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  3060. }
  3061. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3062. {
  3063. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  3064. }
  3065. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3066. {
  3067. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  3068. }
  3069. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  3070. {
  3071. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  3072. }
  3073. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3074. {
  3075. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  3076. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  3077. else
  3078. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  3079. }
  3080. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3081. {
  3082. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  3083. }
  3084. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT) ||
  3085. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT) ||
  3086. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  3087. {
  3088. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  3089. }
  3090. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  3091. {
  3092. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  3093. }
  3094. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  3095. {
  3096. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  3097. }
  3098. else
  3099. {
  3100. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  3101. }
  3102. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3103. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3104. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3105. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  3106. }
  3107. }
  3108. void checkRemoteUpgradeStatus()
  3109. {
  3110. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3111. {
  3112. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3113. {
  3114. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3115. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3116. ShmCharger->isUpdateSuccess = NO;
  3117. }
  3118. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3119. {
  3120. DEBUG_INFO("Firmware remote upgrading...\n");
  3121. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3122. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3123. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3124. int result = upgrade_check();
  3125. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3126. if(result == PASS)
  3127. {
  3128. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3129. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3130. ShmCharger->isUpdateSuccess = YES;
  3131. DEBUG_INFO("Remote update success...\n");
  3132. }
  3133. else
  3134. {
  3135. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3136. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3137. ShmCharger->isUpdateSuccess = NO;
  3138. DEBUG_INFO("Remote update unsuccess...\n");
  3139. }
  3140. }
  3141. else
  3142. {}
  3143. }
  3144. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3145. {
  3146. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3147. {
  3148. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3149. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3150. ShmCharger->isUpdateSuccess = NO;
  3151. }
  3152. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  3153. {
  3154. DEBUG_INFO("Firmware remote upgrading...\n");
  3155. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3156. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3157. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3158. int result = upgrade_check();
  3159. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3160. if(result == PASS)
  3161. {
  3162. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3163. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3164. ShmCharger->isUpdateSuccess = YES;
  3165. DEBUG_INFO("Remote update success...\n");
  3166. }
  3167. else
  3168. {
  3169. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3170. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3171. ShmCharger->isUpdateSuccess = NO;
  3172. DEBUG_INFO("Remote update unsuccess...\n");
  3173. }
  3174. }
  3175. else
  3176. {}
  3177. }
  3178. }
  3179. //======================================================
  3180. // Main process
  3181. //======================================================
  3182. int main(void)
  3183. {
  3184. //==================================================
  3185. // Create all share memory
  3186. //==================================================
  3187. if(CreatShareMemory()==0)
  3188. {
  3189. DEBUG_ERROR("CreatShareMemory NG\n");
  3190. if(ShmStatusCodeData!=NULL)
  3191. {
  3192. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3193. }
  3194. sleep(5);
  3195. system("reboot -f");
  3196. sleep(5);
  3197. system("reboot -f");
  3198. }
  3199. else
  3200. {
  3201. DEBUG_INFO("CreatShareMemory OK\n");
  3202. }
  3203. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3204. {
  3205. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  3206. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  3207. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  3208. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  3209. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  3210. }
  3211. //==================================================
  3212. // Main loop
  3213. //==================================================
  3214. for(;;)
  3215. {
  3216. //==============================================
  3217. // Synchronize share memory from OCPP struct
  3218. //==============================================
  3219. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  3220. //==============================================
  3221. // Check task processing
  3222. //==============================================
  3223. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3224. checkTask();
  3225. //==============================================
  3226. // Check connection timeout specification
  3227. //==============================================
  3228. checkConnectionTimeout();
  3229. //==============================================
  3230. // Something need run in Idle mode
  3231. //==============================================
  3232. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  3233. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  3234. {
  3235. // Check restore factory setting request
  3236. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3237. {
  3238. // Set led to initial
  3239. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3240. {
  3241. setLedMotion(gun_index,LED_ACTION_INIT);
  3242. }
  3243. system("cd /root;./Module_FactoryConfig -m");
  3244. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3245. system("sync");
  3246. sleep(5);
  3247. system("reboot -f");
  3248. sleep(5);
  3249. system("reboot -f");
  3250. }
  3251. // Check upgrade firmware request
  3252. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  3253. ocpp_get_update_firmware_req())
  3254. {
  3255. // If available memory too small, free memory cache first
  3256. if(getAvailableMemory() < (200*1024*1024))
  3257. {
  3258. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  3259. system("echo 3 > /proc/sys/vm/drop_caches");
  3260. }
  3261. ShmCharger->isUpdateSuccess = NO;
  3262. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3263. {
  3264. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3265. setChargerMode(gun_index, SYS_MODE_UPDATE);
  3266. }
  3267. }
  3268. }
  3269. //==============================================
  3270. // Check remote reset request
  3271. //==============================================
  3272. checkReset();
  3273. //==============================================
  3274. // Check RFID
  3275. //==============================================
  3276. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  3277. {
  3278. if(GetCardSerialNumber()!= FAIL)
  3279. {
  3280. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  3281. }
  3282. }
  3283. //==============================================
  3284. // Connector loop
  3285. //==============================================
  3286. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3287. {
  3288. //==========================================
  3289. // Power saving logic
  3290. //==========================================
  3291. /*
  3292. * TODO:
  3293. * 1. Power saving logic
  3294. */
  3295. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3296. {
  3297. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  3298. }
  3299. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
  3300. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING))
  3301. {
  3302. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3303. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3304. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3305. }
  3306. else
  3307. {
  3308. if((ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
  3309. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING)) &&
  3310. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) &&
  3311. (DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10000)))
  3312. {
  3313. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3314. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3315. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3316. }
  3317. }
  3318. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  3319. {
  3320. if(ShmCharger->isLcdOn == ON)
  3321. {
  3322. //if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) < (TIMEOUT_SPEC_POWERSAVING_LCD+600))
  3323. DEBUG_INFO("LCD into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  3324. ShmCharger->isLcdOn = OFF;
  3325. }
  3326. }
  3327. else
  3328. {
  3329. if(ShmCharger->isLcdOn == OFF)
  3330. {
  3331. //if((0 < DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD])) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) < 600))
  3332. DEBUG_INFO("LCD exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  3333. ShmCharger->isLcdOn = ON;
  3334. }
  3335. }
  3336. if(DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  3337. {
  3338. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  3339. {
  3340. DEBUG_INFO("RFID into power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  3341. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  3342. }
  3343. }
  3344. else
  3345. {
  3346. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  3347. {
  3348. DEBUG_INFO("RFID exit power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  3349. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  3350. }
  3351. }
  3352. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  3353. {
  3354. if(ShmCharger->gun_info[gun_index].isMeterOn )
  3355. {
  3356. DEBUG_INFO("Meter into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  3357. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  3358. }
  3359. }
  3360. else
  3361. {
  3362. if(!ShmCharger->gun_info[gun_index].isMeterOn )
  3363. {
  3364. DEBUG_INFO("Meter exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  3365. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  3366. }
  3367. }
  3368. //==========================================
  3369. // Synchronize current rating value from MCU
  3370. //==========================================
  3371. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3372. //==========================================
  3373. // Assign connector location index for OCPP
  3374. //==========================================
  3375. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  3376. //==========================================
  3377. // Synchronize present charging power
  3378. //==========================================
  3379. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  3380. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  3381. else
  3382. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  3383. //==========================================
  3384. // Check initialization "PASS" or "FAIL"
  3385. //==========================================
  3386. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  3387. {
  3388. // Alarm event check
  3389. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  3390. {
  3391. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  3392. {
  3393. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  3394. {
  3395. setChargerMode(gun_index, SYS_MODE_ALARM);
  3396. }
  3397. }
  3398. }
  3399. }
  3400. //==========================================
  3401. // Reservation request check
  3402. //==========================================
  3403. checkReservation(gun_index);
  3404. //==========================================
  3405. // Change availability check
  3406. //==========================================
  3407. checkAvailability(gun_index);
  3408. if(ShmCharger->gun_info[gun_index].isOperactive)
  3409. {
  3410. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  3411. {
  3412. setChargerMode(gun_index, SYS_MODE_IDLE);
  3413. }
  3414. }
  3415. else
  3416. {
  3417. if(isMode(gun_index, SYS_MODE_IDLE))
  3418. {
  3419. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  3420. }
  3421. }
  3422. //==========================================
  3423. // Unlock Connector signal check
  3424. //==========================================
  3425. checkUnlocker(gun_index);
  3426. //==========================================
  3427. // Connector process
  3428. //==========================================
  3429. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  3430. {
  3431. case SYS_MODE_BOOTING:
  3432. if(isModeChange(gun_index))
  3433. {
  3434. setLedMotion(gun_index,LED_ACTION_INIT);
  3435. //CSU Initialization & task spawn
  3436. if((Initialization() != PASS) ||
  3437. (SpawnTask() != PASS))
  3438. {
  3439. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  3440. }
  3441. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  3442. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  3443. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  3444. ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  3445. }
  3446. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  3447. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  3448. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  3449. {
  3450. ShmCharger->gun_selectd = 0;
  3451. // Firmware version
  3452. get_firmware_version(gun_index);
  3453. // OCPP BootNotification info set
  3454. DEBUG_INFO("==========================================\n");
  3455. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  3456. DEBUG_INFO("==========================================\n");
  3457. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  3458. ocpp_boot_info_sync();
  3459. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  3460. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  3461. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  3462. DEBUG_INFO("==========================================\n");
  3463. DEBUG_INFO("========== Set Wifi information ==========\n");
  3464. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  3465. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  3466. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  3467. DEBUG_INFO("==========================================\n");
  3468. // Set max current to rating current
  3469. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3470. // Default Ethernet / Wifi / 4G to 1:disconnected
  3471. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  3472. {
  3473. case 'E':
  3474. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3475. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  3476. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  3477. break;
  3478. case 'W':
  3479. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3480. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3481. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  3482. break;
  3483. case 'T':
  3484. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3485. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  3486. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3487. break;
  3488. case 'D':
  3489. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  3490. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  3491. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  3492. break;
  3493. }
  3494. // Default LCM brightness to 100
  3495. ShmCharger->isLcdOn = ON;
  3496. // If Web Server OPCC URL is empty kill Module_OcppBackend
  3497. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  3498. {
  3499. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  3500. system ("pkill OcppBackend");
  3501. }
  3502. // If rotate switch equal zero, the system needs to change Debug mode
  3503. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  3504. setChargerMode(gun_index, SYS_MODE_DEBUG);
  3505. else
  3506. setChargerMode(gun_index, SYS_MODE_IDLE);
  3507. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  3508. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  3509. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  3510. ?3:1);
  3511. }
  3512. break;
  3513. case SYS_MODE_IDLE:
  3514. if(isModeChange(gun_index))
  3515. {
  3516. setLedMotion(gun_index,LED_ACTION_IDLE);
  3517. setRelay(gun_index, OFF);
  3518. setRequest(gun_index,OFF);
  3519. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3520. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3521. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3522. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3523. ocpp_set_remotestart(gun_index, OFF);
  3524. ocpp_set_remotestop(gun_index, OFF);
  3525. ocpp_set_auth_conf(OFF);
  3526. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  3527. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  3528. presentChargedEnergyClear(gun_index);
  3529. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  3530. ocpp_set_unlocker_req(gun_index, OFF);
  3531. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  3532. // Response StopTransactionConf
  3533. ocpp_set_stoptransaction_conf(gun_index, OFF);
  3534. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  3535. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3536. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3537. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3538. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  3539. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  3540. if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  3541. }
  3542. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  3543. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  3544. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3545. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  3546. (ocpp_get_remotestart(gun_index) == ON) ||
  3547. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  3548. {
  3549. // Clean User id & Card Number
  3550. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3551. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3552. {
  3553. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3554. DEBUG_INFO("Start Method : RFID...\n");
  3555. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3556. {
  3557. // Big endian
  3558. switch(rfid.snType)
  3559. {
  3560. case RFID_SN_TYPE_6BYTE:
  3561. 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]);
  3562. break;
  3563. case RFID_SN_TYPE_7BYTE:
  3564. 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]);
  3565. break;
  3566. case RFID_SN_TYPE_10BYTE:
  3567. 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]);
  3568. break;
  3569. case RFID_SN_TYPE_4BYTE:
  3570. default:
  3571. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3572. break;
  3573. }
  3574. }
  3575. else
  3576. {
  3577. // Little endian
  3578. switch(rfid.snType)
  3579. {
  3580. case RFID_SN_TYPE_6BYTE:
  3581. 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]);
  3582. break;
  3583. case RFID_SN_TYPE_7BYTE:
  3584. 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]);
  3585. break;
  3586. case RFID_SN_TYPE_10BYTE:
  3587. 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]);
  3588. break;
  3589. case RFID_SN_TYPE_4BYTE:
  3590. default:
  3591. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3592. break;
  3593. }
  3594. }
  3595. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3596. }
  3597. else if(ocpp_get_remotestart(gun_index))
  3598. {
  3599. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3600. ocpp_copy_userid_from_remotestart(gun_index);
  3601. DEBUG_INFO("Start Method : BACKEND...\n");
  3602. }
  3603. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  3604. {
  3605. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  3606. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  3607. DEBUG_INFO("Start Method : BLE...\n");
  3608. }
  3609. else
  3610. {
  3611. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  3612. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  3613. DEBUG_INFO("Start Method : FREE...\n");
  3614. }
  3615. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3616. ocpp_set_remotestart(gun_index, OFF);
  3617. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  3618. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  3619. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  3620. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3621. }
  3622. else
  3623. {}
  3624. break;
  3625. case SYS_MODE_AUTHORIZING:
  3626. if(isModeChange(gun_index))
  3627. {
  3628. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3629. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3630. {
  3631. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3632. {
  3633. case AUTH_MODE_ENABLE:
  3634. if(ocpp_get_connection_status())
  3635. {
  3636. // On line
  3637. ocpp_set_auth_req(ON, "ISO14443");
  3638. setLedMotion(gun_index,LED_ACTION_AUTHED);
  3639. }
  3640. else
  3641. {
  3642. // Off line
  3643. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3644. {
  3645. case OFF_POLICY_LOCALLIST:
  3646. ocpp_set_auth_req(ON, "ISO14443");
  3647. break;
  3648. case OFF_POLICY_PH_RFID:
  3649. break;
  3650. case OFF_POLICY_FREE:
  3651. break;
  3652. case OFF_POLICY_NOCHARGE:
  3653. default:
  3654. break;
  3655. }
  3656. }
  3657. break;
  3658. case AUTH_MODE_DISABLE:
  3659. default:
  3660. break;
  3661. }
  3662. }
  3663. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3664. if(ShmCharger->isCcsEnable)system("/root/Module_CCS &");
  3665. }
  3666. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  3667. {
  3668. // Authorization timeout process.
  3669. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3670. setChargerMode(gun_index, SYS_MODE_IDLE);
  3671. DEBUG_WARN("Authorize timeout !!!\n");
  3672. }
  3673. else
  3674. {
  3675. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  3676. {
  3677. case START_METHOD_RFID:
  3678. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  3679. if((ocpp_get_auth_conf()) ||
  3680. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  3681. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0))
  3682. )
  3683. {
  3684. if(ocpp_get_auth_result() ||
  3685. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  3686. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  3687. {
  3688. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3689. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  3690. DEBUG_INFO("Authorize pass.\n");
  3691. setSpeaker(ON,SPEAKER_SHORT);
  3692. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3693. sleep(3);
  3694. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3695. }
  3696. else
  3697. {
  3698. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  3699. DEBUG_INFO("Authorize fail.\n");
  3700. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3701. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3702. sleep(3);
  3703. setChargerMode(gun_index, SYS_MODE_IDLE);
  3704. }
  3705. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3706. ocpp_set_auth_conf(OFF);
  3707. }
  3708. break;
  3709. case START_METHOD_BACKEND:
  3710. case START_METHOD_BLE:
  3711. case START_METHOD_FREE:
  3712. default:
  3713. setSpeaker(ON,SPEAKER_SHORT);
  3714. setChargerMode(gun_index, SYS_MODE_PREPARING);
  3715. break;
  3716. }
  3717. }
  3718. break;
  3719. case SYS_MODE_PREPARING:
  3720. if(isModeChange(gun_index))
  3721. {
  3722. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3723. setLedMotion(gun_index,LED_ACTION_AUTHED);
  3724. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  3725. if(ShmCharger->isCcsEnable)
  3726. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  3727. else
  3728. {
  3729. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  3730. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3731. setRequest(gun_index, ON);
  3732. }
  3733. }
  3734. // If control pilot detect Bx, skip watch dog time out.
  3735. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  3736. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  3737. {
  3738. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  3739. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  3740. {
  3741. case HANDSHAKE_DUTY_5:
  3742. if(!getRequest(gun_index))
  3743. {
  3744. setRequest(gun_index, ON);
  3745. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  3746. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3747. DEBUG_INFO("Set Request On.\n");
  3748. //Let CCS task start to negotiate
  3749. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  3750. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  3751. }
  3752. // Set CCS 5% PWM duty
  3753. if(ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)
  3754. {
  3755. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  3756. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  3757. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3758. }
  3759. break;
  3760. case HANDSHAKE_DUTY_5_CHECK:
  3761. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
  3762. {
  3763. //2 secs timeout
  3764. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  3765. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  3766. }
  3767. break;
  3768. case HANDSHAKE_CCS:
  3769. //CCS handshake timeout
  3770. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3771. {
  3772. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3773. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  3774. DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", (TIMEOUT_SPEC_BS_HLC_HANDSHAKE/1000));
  3775. }
  3776. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  3777. {
  3778. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  3779. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  3780. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3781. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  3782. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3783. }
  3784. //CCS status check
  3785. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  3786. {
  3787. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  3788. DEBUG_INFO("Enter HLC Mode charging.\n");
  3789. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  3790. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3791. }
  3792. break;
  3793. case HANDSHAKE_CP_STATE_E:
  3794. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3795. {
  3796. if(ShmCharger->isCcsEnable)
  3797. {
  3798. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  3799. //CP STATE E for 4 secs
  3800. sleep(4);
  3801. }
  3802. //restore normal CP PWM duty
  3803. // Determine max charging current to MCU
  3804. DEBUG_INFO("Determine max charging current to MCU.\n");
  3805. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3806. {
  3807. 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);
  3808. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3809. {
  3810. 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);
  3811. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3812. }
  3813. }
  3814. else
  3815. {
  3816. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3817. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3818. {
  3819. 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);
  3820. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3821. }
  3822. }
  3823. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3824. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  3825. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  3826. }
  3827. break;
  3828. case HANDSHAKE_SET_MAX_CURRENT:
  3829. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3830. {
  3831. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  3832. DEBUG_INFO("Enter BS Mode charging.\n");
  3833. //for EV READY 30 secs didn't start charging to STATE E
  3834. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  3835. }
  3836. break;
  3837. case HANDSHAKE_BS_MODE:
  3838. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  3839. setRelay(gun_index,ON);
  3840. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  3841. {
  3842. ocpp_set_unlocker_req(gun_index, OFF);
  3843. presentChargedEnergyClear(gun_index);
  3844. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3845. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  3846. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  3847. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  3848. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  3849. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  3850. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3851. else
  3852. {
  3853. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100.0);
  3854. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100.0);
  3855. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100.0);
  3856. }
  3857. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3858. ocpp_copy_userid_to_starttransaction(gun_index);
  3859. ocpp_set_starttransaction_req(gun_index, ON);
  3860. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3861. }
  3862. //EV READY CHECK
  3863. /*
  3864. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  3865. {
  3866. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  3867. {
  3868. DEBUG_INFO("EV READY STATE E 4sec.\n");
  3869. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  3870. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  3871. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3872. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  3873. }
  3874. }
  3875. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  3876. {
  3877. switch(ShmCharger->gun_info[gun_index].evReadyState)
  3878. {
  3879. case EV_READY_STAT_E:
  3880. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3881. {
  3882. sleep(4);
  3883. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3884. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3885. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  3886. }
  3887. break;
  3888. case EV_READY_STAT_C:
  3889. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3890. {
  3891. usleep(500000);
  3892. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  3893. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3894. {
  3895. 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);
  3896. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3897. {
  3898. 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);
  3899. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3900. }
  3901. }
  3902. else
  3903. {
  3904. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3905. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3906. {
  3907. 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);
  3908. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3909. }
  3910. }
  3911. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3912. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  3913. }
  3914. break;
  3915. case EV_READY_SET_MAX_CURRENT:
  3916. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  3917. {
  3918. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  3919. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  3920. }
  3921. break;
  3922. default:
  3923. break;
  3924. }
  3925. }*/
  3926. break;
  3927. case HANDSHAKE_HLC_MODE:
  3928. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)
  3929. {
  3930. setRelay(gun_index,ON);
  3931. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  3932. }
  3933. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  3934. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  3935. {
  3936. ocpp_set_unlocker_req(gun_index, OFF);
  3937. presentChargedEnergyClear(gun_index);
  3938. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  3939. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  3940. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  3941. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  3942. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  3943. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  3944. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  3945. else
  3946. {
  3947. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  3948. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  3949. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  3950. }
  3951. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3952. ocpp_copy_userid_to_starttransaction(gun_index);
  3953. ocpp_set_starttransaction_req(gun_index, ON);
  3954. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  3955. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3956. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  3957. }
  3958. //120 sec timeout
  3959. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  3960. {
  3961. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  3962. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5000)
  3963. {
  3964. DEBUG_INFO("CCS 120 secs handshake 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_100)
  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. default:
  3980. break;
  3981. }
  3982. }
  3983. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  3984. {
  3985. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  3986. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  3987. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == CP_STATE_C)
  3988. setRelay(gun_index,ON);
  3989. ocpp_copy_userid_to_starttransaction(gun_index);
  3990. ocpp_set_starttransaction_req(gun_index, ON);
  3991. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3992. }
  3993. // Unplug charging gun to Idle mode
  3994. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  3995. {
  3996. DEBUG_INFO("Charging gun is plugged before.\n");
  3997. //setChargerMode(gun_index, SYS_MODE_IDLE);
  3998. //Cancel CCS task negotiating
  3999. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4000. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4001. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4002. }
  4003. // Use RFID card to stop handshaking
  4004. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  4005. {
  4006. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  4007. setSpeaker(ON,SPEAKER_SHORT);
  4008. //setChargerMode(gun_index, SYS_MODE_IDLE);
  4009. //Cancel CCS task negotiating
  4010. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4011. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4012. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4013. }
  4014. else
  4015. {
  4016. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4017. }
  4018. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  4019. {
  4020. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4021. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  4022. {
  4023. DEBUG_INFO("Handshaking timeout...\n");
  4024. //Cancel CCS task negotiating
  4025. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4026. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4027. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4028. }
  4029. }
  4030. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  4031. {
  4032. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4033. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4034. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  4035. setChargerMode(gun_index, SYS_MODE_IDLE);
  4036. }
  4037. break;
  4038. case SYS_MODE_CHARGING:
  4039. if(isModeChange(gun_index))
  4040. {
  4041. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4042. ftime(&startChargingTime[gun_index]);
  4043. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4044. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4045. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4046. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4047. ocpp_set_auth_req(OFF);
  4048. ocpp_reset_smartcharging_profileId(gun_index);
  4049. ocpp_set_profile_req(gun_index, ON);
  4050. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4051. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4052. }
  4053. //if time up, clear CCS MSG count
  4054. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  4055. (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  4056. {
  4057. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  4058. if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  4059. {
  4060. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  4061. }
  4062. else
  4063. {
  4064. DEBUG_INFO("CCS could not get MSG from car.\n");
  4065. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4066. //setRelay(gun_index, OFF);
  4067. }
  4068. }
  4069. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4070. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4071. ocpp_get_remotestop(gun_index) ||
  4072. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4073. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  4074. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  4075. ocpp_get_reset_req() ||
  4076. ocpp_get_unlocker_req(gun_index) ||
  4077. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  4078. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  4079. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  4080. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  4081. {
  4082. if(ShmCharger->gun_info[gun_index].rfidReq)
  4083. {
  4084. // If RFID SN different with start user, it need to authorize ID
  4085. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4086. {
  4087. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4088. {
  4089. // Request authorization
  4090. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4091. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4092. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4093. {
  4094. // Big endian
  4095. switch(rfid.snType)
  4096. {
  4097. case RFID_SN_TYPE_6BYTE:
  4098. 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]);
  4099. break;
  4100. case RFID_SN_TYPE_7BYTE:
  4101. 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]);
  4102. break;
  4103. case RFID_SN_TYPE_10BYTE:
  4104. 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]);
  4105. break;
  4106. case RFID_SN_TYPE_4BYTE:
  4107. default:
  4108. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4109. break;
  4110. }
  4111. }
  4112. else
  4113. {
  4114. // Little endian
  4115. switch(rfid.snType)
  4116. {
  4117. case RFID_SN_TYPE_6BYTE:
  4118. 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]);
  4119. break;
  4120. case RFID_SN_TYPE_7BYTE:
  4121. 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]);
  4122. break;
  4123. case RFID_SN_TYPE_10BYTE:
  4124. 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]);
  4125. break;
  4126. case RFID_SN_TYPE_4BYTE:
  4127. default:
  4128. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4129. break;
  4130. }
  4131. }
  4132. DEBUG_INFO("End request User Id: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4133. DEBUG_INFO("Start method: %d...\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  4134. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4135. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4136. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4137. {
  4138. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4139. {
  4140. case AUTH_MODE_ENABLE:
  4141. if(ocpp_get_connection_status())
  4142. {
  4143. // On line
  4144. ocpp_set_auth_req(ON, "ISO14443");
  4145. }
  4146. else
  4147. {
  4148. // Off line
  4149. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4150. {
  4151. case OFF_POLICY_LOCALLIST:
  4152. ocpp_set_auth_req(ON, "ISO14443");
  4153. break;
  4154. case OFF_POLICY_PH_RFID:
  4155. break;
  4156. case OFF_POLICY_FREE:
  4157. break;
  4158. case OFF_POLICY_NOCHARGE:
  4159. default:
  4160. break;
  4161. }
  4162. }
  4163. break;
  4164. case AUTH_MODE_DISABLE:
  4165. default:
  4166. break;
  4167. }
  4168. }
  4169. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4170. {
  4171. DEBUG_INFO("Should be remote stop charger... \n");
  4172. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4173. }
  4174. }
  4175. else
  4176. {
  4177. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4178. if(ocpp_get_auth_conf() ||
  4179. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4180. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  4181. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4182. {
  4183. if(ocpp_get_auth_result() ||
  4184. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4185. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4186. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4187. {
  4188. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4189. }
  4190. else
  4191. {
  4192. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4193. ocpp_set_auth_req(OFF);
  4194. DEBUG_INFO("Authorize fail.\n");
  4195. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4196. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4197. sleep(3);
  4198. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4199. }
  4200. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4201. ocpp_set_auth_conf(OFF);
  4202. }
  4203. }
  4204. }
  4205. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  4206. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  4207. {
  4208. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4209. setRelay(gun_index, OFF);
  4210. }
  4211. }
  4212. else
  4213. {
  4214. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4215. setRelay(gun_index, OFF);
  4216. }
  4217. }
  4218. else
  4219. {
  4220. // Charging session info calculation
  4221. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4222. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4223. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100.0);
  4224. else
  4225. {
  4226. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  4227. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  4228. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  4229. }
  4230. ftime(&endChargingTime[gun_index]);
  4231. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4232. presentChargedEnergyUpdate(gun_index);
  4233. // Response StartTransactionConf
  4234. ocpp_set_starttransaction_conf(gun_index, OFF);
  4235. // Charging profile preparation
  4236. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  4237. {
  4238. if(!ocpp_get_profile_conf(gun_index))
  4239. {
  4240. ocpp_set_profile_req(gun_index, ON);
  4241. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4242. }
  4243. else
  4244. {
  4245. ocpp_set_profile_conf(gun_index, OFF);
  4246. }
  4247. }
  4248. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  4249. checkChargingProfileLimit(gun_index);
  4250. // Charging session target current check if OCPP disconnect and power sharing server connected
  4251. if(!ocpp_get_connection_status() && ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4252. {
  4253. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent;
  4254. }
  4255. // Determine max charging current to MCU
  4256. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4257. {
  4258. 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);
  4259. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4260. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4261. {
  4262. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4263. {
  4264. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4265. {
  4266. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4267. {
  4268. 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);
  4269. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4270. }
  4271. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4272. }
  4273. }
  4274. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4275. {
  4276. 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);
  4277. }
  4278. }
  4279. }
  4280. else
  4281. {
  4282. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4283. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4284. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4285. {
  4286. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4287. {
  4288. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4289. {
  4290. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4291. {
  4292. 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);
  4293. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4294. }
  4295. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4296. }
  4297. }
  4298. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4299. {
  4300. 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);
  4301. }
  4302. }
  4303. }
  4304. // Charging session local limit condition check
  4305. if(ocpp_get_connection_status())
  4306. {
  4307. // On-line max condition check
  4308. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  4309. {
  4310. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4311. {
  4312. setRelay(gun_index, OFF);
  4313. setRequest(gun_index, OFF);
  4314. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4315. }
  4316. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4317. {
  4318. setRelay(gun_index, OFF);
  4319. }
  4320. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4321. 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));
  4322. }
  4323. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  4324. {
  4325. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4326. {
  4327. setRelay(gun_index, OFF);
  4328. setRequest(gun_index, OFF);
  4329. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4330. }
  4331. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4332. {
  4333. setRelay(gun_index, OFF);
  4334. }
  4335. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4336. 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));
  4337. }
  4338. else
  4339. {
  4340. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4341. {
  4342. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4343. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4344. setRelay(gun_index, ON);
  4345. else
  4346. setRelay(gun_index, OFF);
  4347. }
  4348. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4349. {
  4350. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4351. setRelay(gun_index, ON);
  4352. }
  4353. }
  4354. }
  4355. else
  4356. {
  4357. // Off-line max condition check
  4358. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4359. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  4360. {
  4361. 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))))
  4362. {
  4363. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4364. {
  4365. setRelay(gun_index, OFF);
  4366. setRequest(gun_index, OFF);
  4367. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4368. }
  4369. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4370. {
  4371. setRelay(gun_index, OFF);
  4372. }
  4373. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4374. 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));
  4375. }
  4376. 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))))
  4377. {
  4378. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4379. {
  4380. setRelay(gun_index, OFF);
  4381. setRequest(gun_index, OFF);
  4382. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4383. }
  4384. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4385. {
  4386. setRelay(gun_index, OFF);
  4387. }
  4388. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4389. 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));
  4390. }
  4391. else
  4392. {
  4393. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4394. {
  4395. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4396. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  4397. setRelay(gun_index, ON);
  4398. else
  4399. setRelay(gun_index, OFF);
  4400. }
  4401. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4402. {
  4403. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4404. setRelay(gun_index, ON);
  4405. }
  4406. }
  4407. }
  4408. else
  4409. {
  4410. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4411. {
  4412. setRequest(gun_index, OFF);
  4413. }
  4414. setRelay(gun_index, OFF);
  4415. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  4416. }
  4417. }
  4418. // Debug information
  4419. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  4420. {
  4421. DEBUG_INFO("===============================================================\n");
  4422. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4423. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  4424. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4425. DEBUG_INFO("===============================================================\n");
  4426. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4427. }
  4428. }
  4429. break;
  4430. case SYS_MODE_TERMINATING:
  4431. if(isModeChange(gun_index))
  4432. {
  4433. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  4434. {
  4435. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  4436. }
  4437. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4438. {
  4439. setLedMotion(gun_index, LED_ACTION_STOP);
  4440. }
  4441. else
  4442. {}
  4443. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  4444. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4445. }
  4446. // End authorize pass
  4447. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4448. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  4449. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4450. ocpp_get_remotestop(gun_index) ||
  4451. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4452. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  4453. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  4454. ocpp_get_reset_req() ||
  4455. ocpp_get_unlocker_req(gun_index) ||
  4456. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  4457. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  4458. {
  4459. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  4460. {
  4461. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4462. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  4463. {
  4464. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4465. DEBUG_INFO("Authorize pass.\n");
  4466. setSpeaker(ON,SPEAKER_SHORT);
  4467. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4468. sleep(3);
  4469. }
  4470. }
  4471. else
  4472. {}
  4473. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4474. setRequest(gun_index, OFF);
  4475. setRelay(gun_index, OFF);
  4476. setLedMotion(gun_index, LED_ACTION_STOP);
  4477. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4478. {
  4479. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4480. {
  4481. checkStopReason(gun_index);
  4482. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4483. }
  4484. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4485. {
  4486. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4487. //Cancel CCS task negotiating
  4488. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  4489. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  4490. setRelay(gun_index,OFF);
  4491. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  4492. }
  4493. }
  4494. }
  4495. else
  4496. {
  4497. // If the charger stops charging, the led should be stoped blink
  4498. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4499. {
  4500. setLedMotion(gun_index, LED_ACTION_STOP);
  4501. }
  4502. if(ShmCharger->gun_info[gun_index].rfidReq)
  4503. {
  4504. DEBUG_INFO("Certified in terminating mode... \n");
  4505. // If RFID SN different with start user, it need to authorize ID
  4506. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4507. {
  4508. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4509. {
  4510. // Request authorization
  4511. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  4512. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4513. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4514. {
  4515. // Big endian
  4516. switch(rfid.snType)
  4517. {
  4518. case RFID_SN_TYPE_6BYTE:
  4519. 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]);
  4520. break;
  4521. case RFID_SN_TYPE_7BYTE:
  4522. 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]);
  4523. break;
  4524. case RFID_SN_TYPE_10BYTE:
  4525. 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]);
  4526. break;
  4527. case RFID_SN_TYPE_4BYTE:
  4528. default:
  4529. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4530. break;
  4531. }
  4532. }
  4533. else
  4534. {
  4535. // Little endian
  4536. switch(rfid.snType)
  4537. {
  4538. case RFID_SN_TYPE_6BYTE:
  4539. 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]);
  4540. break;
  4541. case RFID_SN_TYPE_7BYTE:
  4542. 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]);
  4543. break;
  4544. case RFID_SN_TYPE_10BYTE:
  4545. 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]);
  4546. break;
  4547. case RFID_SN_TYPE_4BYTE:
  4548. default:
  4549. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4550. break;
  4551. }
  4552. }
  4553. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4554. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  4555. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4556. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4557. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4558. {
  4559. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4560. {
  4561. case AUTH_MODE_ENABLE:
  4562. if(ocpp_get_connection_status())
  4563. {
  4564. // On line
  4565. ocpp_set_auth_req(ON, "ISO14443");
  4566. }
  4567. else
  4568. {
  4569. // Off line
  4570. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4571. {
  4572. case OFF_POLICY_LOCALLIST:
  4573. ocpp_set_auth_req(ON, "ISO14443");
  4574. break;
  4575. case OFF_POLICY_PH_RFID:
  4576. break;
  4577. case OFF_POLICY_FREE:
  4578. break;
  4579. case OFF_POLICY_NOCHARGE:
  4580. default:
  4581. break;
  4582. }
  4583. }
  4584. break;
  4585. case AUTH_MODE_DISABLE:
  4586. default:
  4587. break;
  4588. }
  4589. }
  4590. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4591. {
  4592. DEBUG_INFO("Should be remote stop charger... \n");
  4593. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4594. }
  4595. }
  4596. else
  4597. {
  4598. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4599. if(ocpp_get_auth_conf() ||
  4600. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4601. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4602. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4603. {
  4604. if(ocpp_get_auth_result() ||
  4605. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4606. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  4607. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4608. {
  4609. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4610. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4611. }
  4612. else
  4613. {
  4614. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4615. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4616. ocpp_set_auth_req(OFF);
  4617. DEBUG_INFO("Authorize fail... \n");
  4618. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4619. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4620. sleep(3);
  4621. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4622. }
  4623. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  4624. ocpp_set_auth_conf(OFF);
  4625. }
  4626. }
  4627. }
  4628. }
  4629. 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)) &&
  4630. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  4631. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  4632. !ocpp_get_remotestop(gun_index) &&
  4633. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  4634. !ocpp_get_reset_req() &&
  4635. ocpp_get_starttransaction_result(gun_index) &&
  4636. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  4637. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  4638. !(!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)))) &&
  4639. !(!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)))) &&
  4640. (((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))
  4641. )
  4642. {
  4643. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4644. }
  4645. }
  4646. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  4647. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  4648. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  4649. {
  4650. DEBUG_INFO("Set PWM duty 100% go to SYS_MODE_TERMINATING.\n");
  4651. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4652. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4653. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  4654. checkStopReason(gun_index);
  4655. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4656. }
  4657. break;
  4658. case SYS_MODE_COMPLETE:
  4659. if(isModeChange(gun_index))
  4660. {
  4661. setLedMotion(gun_index, LED_ACTION_STOP);
  4662. setRelay(gun_index, OFF);
  4663. setRequest(gun_index, OFF);
  4664. sleep(5);
  4665. }
  4666. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4667. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4668. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4669. ocpp_set_remotestop(gun_index, OFF);
  4670. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  4671. DB_Insert_Record(localDb, gun_index);
  4672. setChargerMode(gun_index, SYS_MODE_IDLE);
  4673. break;
  4674. case SYS_MODE_ALARM:
  4675. setLedMotion(gun_index,LED_ACTION_ALARM);
  4676. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4677. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4678. {}
  4679. else
  4680. {
  4681. setRelay(gun_index, OFF);
  4682. setRequest(gun_index, OFF);
  4683. }
  4684. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  4685. {
  4686. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  4687. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  4688. {
  4689. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  4690. }
  4691. else
  4692. {
  4693. setChargerMode(gun_index, SYS_MODE_IDLE);
  4694. }
  4695. }
  4696. break;
  4697. case SYS_MODE_FAULT:
  4698. if(isModeChange(gun_index))
  4699. {}
  4700. break;
  4701. case SYS_MODE_MAINTAIN:
  4702. if(isModeChange(gun_index))
  4703. {
  4704. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4705. }
  4706. break;
  4707. case SYS_MODE_UPDATE:
  4708. if(isModeChange(gun_index))
  4709. {
  4710. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4711. }
  4712. //======================================
  4713. // Check local upgrade firmware request
  4714. //======================================
  4715. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  4716. {
  4717. DEBUG_INFO("Firmware local upgrading...\n");
  4718. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  4719. int value = upgrade_check();
  4720. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  4721. if(value == PASS)
  4722. {
  4723. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4724. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4725. ShmCharger->isUpdateSuccess = YES;
  4726. DEBUG_INFO("Local update success...\n");
  4727. }
  4728. else
  4729. {
  4730. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4731. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4732. ShmCharger->isUpdateSuccess = NO;
  4733. DEBUG_INFO("Local update unsuccess...\n");
  4734. }
  4735. }
  4736. else if(ocpp_get_update_firmware_req())
  4737. {
  4738. //======================================
  4739. // Check remote upgrade firmware request
  4740. //======================================
  4741. checkRemoteUpgradeStatus();
  4742. }
  4743. else
  4744. {
  4745. //======================================
  4746. // Upgrade complete reboot system
  4747. //======================================
  4748. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  4749. {
  4750. if(ShmCharger->isUpdateSuccess == YES)
  4751. {
  4752. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4753. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4754. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  4755. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4756. DEBUG_WARN("Firmware upgrade success.\n");
  4757. sleep(5);
  4758. DEBUG_INFO("Firmware upgraded, reboot...\n");
  4759. system("reboot -f");
  4760. sleep(5);
  4761. system("reboot -f");
  4762. }
  4763. else
  4764. {
  4765. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4766. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4767. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  4768. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4769. DEBUG_WARN("Firmware upgrade fail.\n");
  4770. close(wtdFd);
  4771. system("/usr/bin/run_evse_restart.sh");
  4772. }
  4773. }
  4774. }
  4775. break;
  4776. case SYS_MODE_RESERVATION:
  4777. if(isModeChange(gun_index))
  4778. {
  4779. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4780. }
  4781. if(isReservationExpired(gun_index))
  4782. {
  4783. DEBUG_INFO("Reservation: Time's up...\n");
  4784. setChargerMode(gun_index, SYS_MODE_IDLE);
  4785. }
  4786. else if(ocpp_get_cancelreservation_req(gun_index))
  4787. {
  4788. DEBUG_INFO("Reservation: Cancel reservation...\n");
  4789. setChargerMode(gun_index, SYS_MODE_IDLE);
  4790. ocpp_set_cancelreservation_req(gun_index, OFF);
  4791. }
  4792. else
  4793. {
  4794. // Check is there RFID or back end request start
  4795. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4796. ocpp_get_remotestart(gun_index))
  4797. {
  4798. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4799. {
  4800. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4801. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4802. {
  4803. // Big endian
  4804. switch(rfid.snType)
  4805. {
  4806. case RFID_SN_TYPE_6BYTE:
  4807. 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]);
  4808. break;
  4809. case RFID_SN_TYPE_7BYTE:
  4810. 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]);
  4811. break;
  4812. case RFID_SN_TYPE_10BYTE:
  4813. 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]);
  4814. break;
  4815. case RFID_SN_TYPE_4BYTE:
  4816. default:
  4817. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4818. break;
  4819. }
  4820. }
  4821. else
  4822. {
  4823. // Little endian
  4824. switch(rfid.snType)
  4825. {
  4826. case RFID_SN_TYPE_6BYTE:
  4827. 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]);
  4828. break;
  4829. case RFID_SN_TYPE_7BYTE:
  4830. 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]);
  4831. break;
  4832. case RFID_SN_TYPE_10BYTE:
  4833. 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]);
  4834. break;
  4835. case RFID_SN_TYPE_4BYTE:
  4836. default:
  4837. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4838. break;
  4839. }
  4840. }
  4841. if(ocpp_compare_reserve_id_with_user(gun_index))
  4842. {
  4843. DEBUG_INFO("Start Method in reservation : RFID...\n");
  4844. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4845. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4846. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4847. }
  4848. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4849. }
  4850. else if(ocpp_get_remotestart(gun_index))
  4851. {
  4852. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  4853. {
  4854. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  4855. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4856. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4857. }
  4858. ocpp_set_remotestop(gun_index, OFF);
  4859. }
  4860. }
  4861. }
  4862. break;
  4863. case SYS_MODE_BOOKING:
  4864. if(isModeChange(gun_index))
  4865. {}
  4866. break;
  4867. case SYS_MODE_DEBUG:
  4868. if(isModeChange(gun_index))
  4869. {
  4870. setLedMotion(gun_index,LED_ACTION_DEBUG);
  4871. }
  4872. break;
  4873. }
  4874. }
  4875. // System watch dog reset
  4876. write(wtdFd, "a", 1);
  4877. usleep(50000);
  4878. }
  4879. return FAIL;
  4880. }