main.c 169 KB

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