main.c 114 KB

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