main.c 198 KB

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