main.c 107 KB

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