main.c 207 KB

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