main.c 133 KB

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