main.c 195 KB

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