main.c 91 KB

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