main.c 206 KB

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