main.c 195 KB

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