main.c 160 KB

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