main.c 105 KB

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