main.c 105 KB

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