main.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include <ifaddrs.h>
  27. #include <math.h>
  28. #include "../../define.h"
  29. #include "Config.h"
  30. #include <stdbool.h>
  31. #include <dirent.h>
  32. #include "Module_Upgrade.h"
  33. #include "timeout.h"
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define BUFFER_SIZE 128
  38. #define YES 1
  39. #define NO 0
  40. #define NORMAL 0
  41. #define ABNORMAL 1
  42. #define EQUAL 0
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  47. bool IsAuthorizingMode();
  48. void ClearAuthorizedFlag();
  49. bool isDetectPlugin();
  50. void ClearDetectPluginFlag();
  51. int mystrcmp(unsigned char *p1, unsigned char *p2);
  52. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  53. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  54. void ChargingTerminalProcess(byte gunIndex);
  55. void ChkPrimaryStatus();
  56. void StartSystemTimeoutDet(unsigned char flag);
  57. void StopSystemTimeoutDet();
  58. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  59. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  60. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  61. struct StatusCodeData *ShmStatusCodeData;
  62. struct PsuData *ShmPsuData;
  63. struct CHAdeMOData *ShmCHAdeMOData;
  64. struct CcsData *ShmCcsData;
  65. struct PrimaryMcuData *ShmPrimaryMcuData;
  66. struct FanModuleData *ShmFanModuleData;
  67. struct RelayModuleData *ShmRelayModuleData;
  68. struct OCPP16Data *ShmOCPP16Data;
  69. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  70. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  71. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  72. byte _gunCount = 0;
  73. // for initial index to check EV board type is correct
  74. byte _gunIndex = 0;
  75. byte _chademoIndex = 0;
  76. byte _ccsIndex = 0;
  77. byte _gb_Index = 0;
  78. byte bd0_1_status = 0;
  79. byte bd0_2_status = 0;
  80. byte bd1_1_status = 0;
  81. byte bd1_2_status = 0;
  82. bool isCardScan = false;
  83. int rfidFd = -1;
  84. char* rfidPortName = "/dev/ttyS2";
  85. byte autoReturnTimeoutFlag = 0x00;
  86. int whileLoopTime = 10000; // 10 ms
  87. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  88. int StoreLogMsg(const char *fmt, ...);
  89. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  90. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  91. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  92. //================================================
  93. // initial can-bus
  94. //================================================
  95. int InitCanBus()
  96. {
  97. int s0,nbytes;
  98. struct timeval tv;
  99. struct ifreq ifr0;
  100. struct sockaddr_can addr0;
  101. system("/sbin/ip link set can0 down");
  102. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  103. system("/sbin/ip link set can0 up");
  104. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  105. tv.tv_sec = 0;
  106. tv.tv_usec = 10000;
  107. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  108. {
  109. #ifdef SystemLogMessage
  110. DEBUG_ERROR("Set SO_RCVTIMEO NG");
  111. #endif
  112. }
  113. nbytes=40960;
  114. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  115. {
  116. #ifdef SystemLogMessage
  117. DEBUG_ERROR("Set SO_RCVBUF NG");
  118. #endif
  119. }
  120. nbytes=40960;
  121. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  122. {
  123. #ifdef SystemLogMessage
  124. DEBUG_ERROR("Set SO_SNDBUF NG");
  125. #endif
  126. }
  127. strcpy(ifr0.ifr_name, "can0" );
  128. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  129. addr0.can_family = AF_CAN;
  130. addr0.can_ifindex = ifr0.ifr_ifindex;
  131. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  132. return s0;
  133. }
  134. //================================================
  135. // initial uart port
  136. //================================================
  137. char *priPortName = "/dev/ttyS1";
  138. int InitComPort(byte id)
  139. {
  140. int fd;
  141. struct termios tios;
  142. if (id == 0x04)
  143. fd = open(priPortName, O_RDWR);
  144. if(fd<=0)
  145. {
  146. #ifdef SystemLogMessage
  147. DEBUG_ERROR("open 407 Communication port NG \n");
  148. #endif
  149. return -1;
  150. }
  151. ioctl (fd, TCGETS, &tios);
  152. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  153. tios.c_lflag = 0;
  154. tios.c_iflag = 0;
  155. tios.c_oflag = 0;
  156. tios.c_cc[VMIN]=0;
  157. tios.c_cc[VTIME]=(unsigned char)1;
  158. tios.c_lflag=0;
  159. tcflush(fd, TCIFLUSH);
  160. ioctl (fd, TCSETS, &tios);
  161. return fd;
  162. }
  163. //=================================
  164. // Common routine
  165. //=================================
  166. int mystrcmp(unsigned char *p1, unsigned char *p2)
  167. {
  168. while(*p1==*p2)
  169. {
  170. if(*p1=='\0' || *p2=='\0')
  171. break;
  172. p1++;
  173. p2++;
  174. }
  175. if(*p1=='\0' && *p2=='\0')
  176. return(PASS);
  177. else
  178. return(FAIL);
  179. }
  180. int DiffTimeb(struct timeb ST, struct timeb ET)
  181. {
  182. //return milli-second
  183. unsigned int StartTime,StopTime;
  184. StartTime=(unsigned int)ST.time;
  185. StopTime=(unsigned int)ET.time;
  186. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  187. return (StopTime-StartTime);
  188. }
  189. int StoreLogMsg(const char *fmt, ...)
  190. {
  191. char Buf[4096+256];
  192. char buffer[4096];
  193. time_t CurrentTime;
  194. struct tm *tm;
  195. va_list args;
  196. va_start(args, fmt);
  197. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  198. va_end(args);
  199. memset(Buf,0,sizeof(Buf));
  200. CurrentTime = time(NULL);
  201. tm=localtime(&CurrentTime);
  202. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  203. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  204. buffer,
  205. tm->tm_year+1900,tm->tm_mon+1);
  206. system(Buf);
  207. return rc;
  208. }
  209. unsigned long GetTimeoutValue(struct timeval _sour_time)
  210. {
  211. struct timeval _end_time;
  212. gettimeofday(&_end_time, NULL);
  213. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  214. }
  215. void setChargerMode(byte gun_index, byte mode)
  216. {
  217. chargingInfo[gun_index]->SystemStatus = mode;
  218. }
  219. //==========================================
  220. // Check interface status
  221. //==========================================
  222. int isInterfaceUp(const char *interface)
  223. {
  224. int result = FAIL;
  225. FILE *fp;
  226. char cmd[256];
  227. char buf[512];
  228. strcpy(cmd, "ifconfig");
  229. fp = popen(cmd, "r");
  230. if(fp != NULL)
  231. {
  232. while(fgets(buf, sizeof(buf), fp) != NULL)
  233. {
  234. if(strstr(buf, interface) > 0)
  235. {
  236. result = PASS;
  237. }
  238. }
  239. }
  240. pclose(fp);
  241. return result;
  242. }
  243. //=================================
  244. // Create all share memory
  245. //=================================
  246. int CreateShareMemory()
  247. {
  248. int MeterSMId;
  249. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  250. {
  251. #ifdef SystemLogMessage
  252. StoreLogMsg("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  253. #endif
  254. return 0;
  255. }
  256. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  257. {
  258. #ifdef SystemLogMessage
  259. StoreLogMsg("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  260. #endif
  261. return 0;
  262. }
  263. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  264. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  265. {
  266. #ifdef SystemLogMessage
  267. StoreLogMsg("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  268. #endif
  269. return 0;
  270. }
  271. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  272. {
  273. #ifdef SystemLogMessage
  274. StoreLogMsg("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  275. #endif
  276. return 0;
  277. }
  278. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  279. //creat ShmPsuData
  280. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  281. {
  282. #ifdef SystemLogMessage
  283. StoreLogMsg("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  284. #endif
  285. return 0;
  286. }
  287. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  288. {
  289. #ifdef SystemLogMessage
  290. StoreLogMsg("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  291. #endif
  292. return 0;
  293. }
  294. memset(ShmPsuData, 0, sizeof(struct PsuData));
  295. if(CHAdeMO_QUANTITY > 0)
  296. {
  297. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  298. {
  299. #ifdef SystemLogMessage
  300. StoreLogMsg("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  301. #endif
  302. return 0;
  303. }
  304. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  305. {
  306. #ifdef SystemLogMessage
  307. StoreLogMsg("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  308. #endif
  309. return 0;
  310. }
  311. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  312. }
  313. //creat ShmCcsData
  314. if(CCS_QUANTITY > 0)
  315. {
  316. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  317. {
  318. #ifdef SystemLogMessage
  319. StoreLogMsg("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  320. #endif
  321. return 0;
  322. }
  323. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  324. {
  325. #ifdef SystemLogMessage
  326. StoreLogMsg("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  327. #endif
  328. return 0;
  329. }
  330. memset(ShmCcsData, 0, sizeof(struct CcsData));
  331. }
  332. //creat ShmPrimaryMcuData
  333. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  334. {
  335. #ifdef SystemLogMessage
  336. StoreLogMsg("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  337. #endif
  338. return 0;
  339. }
  340. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  341. {
  342. #ifdef SystemLogMessage
  343. StoreLogMsg("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  344. #endif
  345. return 0;
  346. }
  347. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  348. //creat ShmFanModuleData
  349. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  350. {
  351. #ifdef SystemLogMessage
  352. StoreLogMsg("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  353. #endif
  354. return 0;
  355. }
  356. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  357. {
  358. #ifdef SystemLogMessage
  359. StoreLogMsg("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  360. #endif
  361. return 0;
  362. }
  363. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  364. //creat ShmRelayModuleData
  365. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  366. {
  367. #ifdef SystemLogMessage
  368. StoreLogMsg("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  369. #endif
  370. return 0;
  371. }
  372. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  373. {
  374. #ifdef SystemLogMessage
  375. StoreLogMsg("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  376. #endif
  377. return 0;
  378. }
  379. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  380. //creat ShmOCPP16Data
  381. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  382. {
  383. #ifdef SystemLogMessage
  384. StoreLogMsg("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  385. #endif
  386. return 0;
  387. }
  388. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  389. {
  390. #ifdef SystemLogMessage
  391. StoreLogMsg("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  392. #endif
  393. return 0;
  394. }
  395. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  396. return 1;
  397. }
  398. //=================================
  399. // LCM Page
  400. //=================================
  401. void ChangeLcmByIndex(byte page_index)
  402. {
  403. if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level != 2 ||
  404. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  405. {
  406. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  407. }
  408. }
  409. //======================================================
  410. // Peripheral initial
  411. //======================================================
  412. void InitGPIO()
  413. {
  414. /*****************0~3, 4 bank, bank x 32+ num*********************/
  415. /***************************************************************/
  416. /*************** GPIO 0 ***************************************/
  417. /***************************************************************/
  418. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  419. system("echo 22 > /sys/class/gpio/export");
  420. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  421. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  422. system("echo 23 > /sys/class/gpio/export");
  423. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  424. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  425. system("echo 26 > /sys/class/gpio/export");
  426. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  427. system("echo 1 > /sys/class/gpio/gpio26/value");
  428. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  429. system("echo 27 > /sys/class/gpio/export");
  430. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  431. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  432. system("echo 29 > /sys/class/gpio/export");
  433. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  434. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  435. system("echo 19 > /sys/class/gpio/export");
  436. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  437. system("echo 0 > /sys/class/gpio/gpio19/value");
  438. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  439. system("echo 20 > /sys/class/gpio/export");
  440. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  441. /***************************************************************/
  442. /*************** GPIO 1 ***************************************/
  443. /***************************************************************/
  444. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  445. system("echo 44 > /sys/class/gpio/export");
  446. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  447. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  448. system("echo 45 > /sys/class/gpio/export");
  449. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  450. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  451. system("echo 46 > /sys/class/gpio/export");
  452. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  453. system("echo 0 > /sys/class/gpio/gpio46/value");
  454. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  455. system("echo 47 > /sys/class/gpio/export");
  456. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  457. /***************************************************************/
  458. /*************** GPIO 2 ***************************************/
  459. /***************************************************************/
  460. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  461. system("echo 89 > /sys/class/gpio/export");
  462. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  463. system("echo 1 > /sys/class/gpio/gpio89/value");
  464. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  465. system("echo 87 > /sys/class/gpio/export");
  466. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  467. system("echo 0 > /sys/class/gpio/gpio87/value");
  468. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  469. system("echo 88 > /sys/class/gpio/export");
  470. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  471. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  472. system("echo 86 > /sys/class/gpio/export");
  473. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  474. /***************************************************************/
  475. /*************** GPIO 3 ***************************************/
  476. /***************************************************************/
  477. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  478. system("echo 111 > /sys/class/gpio/export");
  479. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  480. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  481. system("echo 114 > /sys/class/gpio/export");
  482. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  483. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  484. system("echo 113 > /sys/class/gpio/export");
  485. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  486. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  487. system("echo 110 > /sys/class/gpio/export");
  488. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  489. system("echo 0 > /sys/class/gpio/gpio110/value");
  490. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1*/
  491. system("echo 115 > /sys/class/gpio/export");
  492. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  493. system("echo 0 > /sys/class/gpio/gpio115/value");
  494. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  495. system("echo 112 > /sys/class/gpio/export");
  496. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  497. system("echo 0 > /sys/class/gpio/gpio112/value");
  498. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2*/
  499. system("echo 116 > /sys/class/gpio/export");
  500. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  501. system("echo 0 > /sys/class/gpio/gpio116/value");
  502. #ifdef SystemLogMessage
  503. StoreLogMsg("[main]InitGPIO: Initial GPIO OK");
  504. #endif
  505. }
  506. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  507. {
  508. int fd,wrd;
  509. struct SysConfigData *buf;
  510. byte *PtrBuf;
  511. unsigned int ChkSum, ChkSumOrg;
  512. if ((buf = malloc(sizeof(struct SysConfigData))) == NULL)
  513. {
  514. #ifdef SystemLogMessage
  515. StoreLogMsg("[main]LoadSysConfigAndInfo:malloc buffer NG,rebooting..");
  516. #endif
  517. if (ShmStatusCodeData != NULL)
  518. {
  519. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  520. }
  521. sleep(5);
  522. system("reboot -f");
  523. sleep(5);
  524. system("reboot -f");
  525. }
  526. memset(buf, 0, sizeof(struct SysConfigData));
  527. fd = open("/dev/mtdblock10", O_RDWR);
  528. if (fd < 0)
  529. {
  530. free(buf);
  531. #ifdef SystemLogMessage
  532. StoreLogMsg("[main]LoadSysConfigAndInfo:open mtdblock10 NG,rebooting..");
  533. #endif
  534. if (ShmStatusCodeData != NULL)
  535. {
  536. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  537. }
  538. sleep(5);
  539. system("reboot -f");
  540. sleep(5);
  541. system("reboot -f");
  542. }
  543. wrd = read(fd, buf, sizeof(struct SysConfigData));
  544. close(fd);
  545. if (wrd != (sizeof(struct SysConfigData)))
  546. {
  547. free(buf);
  548. #ifdef SystemLogMessage
  549. StoreLogMsg("[main]LoadSysConfigAndInfo: read SysConfigData data NG,rebooting..");
  550. #endif
  551. if (ShmStatusCodeData != NULL)
  552. {
  553. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  554. }
  555. sleep(5);
  556. system("reboot -f");
  557. sleep(5);
  558. system("reboot -f");
  559. }
  560. PtrBuf = (byte *) buf;
  561. ChkSum = 0;
  562. for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  563. {
  564. ChkSum += PtrBuf[wrd];
  565. }
  566. ChkSumOrg = buf->Checksum;
  567. if (ChkSum != ChkSumOrg)
  568. {
  569. #ifdef SystemLogMessage
  570. StoreLogMsg("[main]LoadSysConfigAndInfo: Primary SysConfigData checksum NG, read backup");
  571. #endif
  572. fd = open("/dev/mtdblock11", O_RDWR);
  573. if (fd < 0)
  574. {
  575. free(buf);
  576. #ifdef SystemLogMessage
  577. StoreLogMsg("[main]LoadSysConfigAndInfo: open mtdblock11 (backup) NG,rebooting..");
  578. #endif
  579. if (ShmStatusCodeData != NULL)
  580. {
  581. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  582. }
  583. sleep(5);
  584. system("reboot -f");
  585. sleep(5);
  586. system("reboot -f");
  587. }
  588. memset(buf, 0, sizeof(struct SysConfigData));
  589. wrd = read(fd, buf, sizeof(struct SysConfigData));
  590. close(fd);
  591. if (wrd != sizeof(struct SysConfigData))
  592. {
  593. free(buf);
  594. #ifdef SystemLogMessage
  595. StoreLogMsg("[main]LoadSysConfigAndInfo: read backup SysConfigData data NG,rebooting..");
  596. #endif
  597. if (ShmStatusCodeData != NULL)
  598. {
  599. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  600. }
  601. sleep(5);
  602. system("reboot -f");
  603. sleep(5);
  604. system("reboot -f");
  605. }
  606. PtrBuf = (byte *) buf;
  607. ChkSum = 0;
  608. for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  609. {
  610. ChkSum += PtrBuf[wrd];
  611. }
  612. ChkSumOrg = buf->Checksum;
  613. if (ChkSum != ChkSumOrg)
  614. {
  615. #ifdef SystemLogMessage
  616. StoreLogMsg("[main]LoadSysConfigAndInfo: backup SysConfigData checksum NG, read Factory default");
  617. #endif
  618. fd = open("/dev/mtdblock12", O_RDWR);
  619. if (fd < 0)
  620. {
  621. free(buf);
  622. #ifdef SystemLogMessage
  623. StoreLogMsg("[main]LoadSysConfigAndInfo: open mtdblock12 (Factory default) NG,rebooting..");
  624. #endif
  625. if (ShmStatusCodeData != NULL)
  626. {
  627. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  628. }
  629. sleep(5);
  630. system("reboot -f");
  631. sleep(5);
  632. system("reboot -f");
  633. }
  634. memset(buf, 0, sizeof(struct SysConfigData));
  635. wrd = read(fd, buf, sizeof(struct SysConfigData));
  636. close(fd);
  637. if (wrd != sizeof(struct SysConfigData))
  638. {
  639. free(buf);
  640. #ifdef SystemLogMessage
  641. StoreLogMsg("[main]LoadSysConfigAndInfo: read factory default SysConfigData data NG,rebooting..");
  642. #endif
  643. if (ShmStatusCodeData != NULL)
  644. {
  645. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  646. }
  647. sleep(5);
  648. system("reboot -f");
  649. sleep(5);
  650. system("reboot -f");
  651. }
  652. PtrBuf = (byte *) buf;
  653. ChkSum = 0;
  654. for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  655. {
  656. ChkSum += PtrBuf[wrd];
  657. }
  658. ChkSumOrg = buf->Checksum;
  659. if (ChkSum != ChkSumOrg)
  660. {
  661. #ifdef SystemLogMessage
  662. StoreLogMsg("[main]LoadSysConfigAndInfo: factory default SysConfigData checksum NG, restore factory default");
  663. #endif
  664. goto DefaultShm;
  665. }
  666. }
  667. }
  668. //load OK
  669. memcpy((struct SysConfigData *) ptr, (struct SysConfigData *) buf, sizeof(struct SysConfigData));
  670. free(buf);
  671. #ifdef SystemLogMessage
  672. StoreLogMsg("[main]LoadSysConfigAndInfo: Load SysConfigData OK");
  673. #endif
  674. return 1;
  675. DefaultShm: system("cd /root;./FactoryConfig");
  676. system("sync");
  677. sleep(5);
  678. system("reboot -f");
  679. sleep(5);
  680. system("reboot -f");
  681. return FAIL;
  682. }
  683. void InitEthernet()
  684. {
  685. char tmpbuf[256];
  686. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  687. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  688. sleep(2);
  689. //Init Eth0 for internet
  690. return;
  691. memset(tmpbuf,0,256);
  692. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  693. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  694. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  695. system(tmpbuf);
  696. memset(tmpbuf,0,256);
  697. sprintf(tmpbuf,"route add default gw %s eth0 ",
  698. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  699. system(tmpbuf);
  700. //Init Eth1 for administrator tool
  701. memset(tmpbuf,0,256);
  702. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  703. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  704. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  705. system(tmpbuf);
  706. //Run DHCP client if enabled
  707. system("killall udhcpc");
  708. system("rm -rf /etc/resolv.conf");
  709. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  710. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  711. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  712. system("/sbin/udhcpc -i eth0 -s /root/simple.script > /dev/null &");
  713. #ifdef SystemLogMessage
  714. StoreLogMsg("[main]InitEthernet: Initial Ethernet OK");
  715. #endif
  716. }
  717. int InitialRfidPort()
  718. {
  719. int uartO2 = open(rfidPortName, O_RDWR);
  720. struct termios tios;
  721. if (uartO2 != FAIL)
  722. {
  723. ioctl (uartO2, TCGETS, &tios);
  724. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  725. tios.c_lflag = 0;
  726. tios.c_iflag = 0;
  727. tios.c_oflag = 0;
  728. tios.c_cc[VMIN] = 0;
  729. tios.c_cc[VTIME] = (unsigned char) 1;
  730. tios.c_lflag = 0;
  731. tcflush(uartO2, TCIFLUSH);
  732. ioctl(uartO2, TCSETS, &tios);
  733. }
  734. if (uartO2 < 0)
  735. {
  736. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  737. }
  738. return uartO2;
  739. }
  740. int Initialization()
  741. {
  742. //InitGPIO();
  743. //LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  744. //InitEthernet();
  745. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  746. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  747. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  748. // 初始化卡號驗證的 Flag
  749. ClearAuthorizedFlag();
  750. // 初始化插槍驗證的 Flag
  751. ClearDetectPluginFlag();
  752. // UART 2 for Rfid
  753. rfidFd = InitialRfidPort();
  754. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  755. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  756. ShmPrimaryMcuData->SelfTest_Comp = NO;
  757. ShmRelayModuleData->SelfTest_Comp = NO;
  758. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  759. for (byte count = 0; count < _gunCount; count++)
  760. {
  761. if (chargingInfo[count]->Type == _Type_Chademo)
  762. {
  763. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  764. }
  765. else if (chargingInfo[count]->Type == _Type_CCS)
  766. {
  767. if (ShmCcsData->CommProtocol == 0x01)
  768. {
  769. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  770. }
  771. }
  772. }
  773. #ifdef SystemLogMessage
  774. printf("Initialization OK \n");
  775. #endif
  776. return PASS;
  777. }
  778. void SelfTestRun()
  779. {
  780. bool evInitFlag = false;
  781. StartSystemTimeoutDet(Timeout_SelftestChk);
  782. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  783. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  784. {
  785. ChkPrimaryStatus();
  786. if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 2)
  787. {
  788. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  789. return;
  790. }
  791. if (_gunCount > 0)
  792. {
  793. if (ShmPsuData->Work_Step == _NO_WORKING)
  794. {
  795. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  796. break;
  797. }
  798. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  799. {
  800. case _STEST_VERSION:
  801. {
  802. // RB Version
  803. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  804. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  805. {
  806. //printf("RB pass \n");
  807. ShmRelayModuleData->SelfTest_Comp = YES;
  808. }
  809. // 407 Version
  810. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  811. ShmPrimaryMcuData->version[0] != '\0')
  812. {
  813. //printf("407 pass \n");
  814. ShmPrimaryMcuData->SelfTest_Comp = YES;
  815. }
  816. // EV 小板
  817. if (!evInitFlag)
  818. {
  819. evInitFlag = YES;
  820. for (byte index = 0; index < _gunCount; index++)
  821. {
  822. if (chargingInfo[index]->Type == _Type_Chademo)
  823. {
  824. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  825. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  826. {
  827. //printf("chademo pass \n");
  828. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  829. }
  830. else
  831. {
  832. printf("chademo fw lose...... \n");
  833. evInitFlag = NO;
  834. }
  835. }
  836. else if (chargingInfo[index]->Type == _Type_CCS)
  837. {
  838. if (ShmCcsData->CommProtocol == 0x01)
  839. {
  840. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  841. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  842. {
  843. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  844. }
  845. else
  846. {
  847. printf("ccs fw lose \n");
  848. evInitFlag = NO;
  849. }
  850. }
  851. }
  852. }
  853. }
  854. if (ShmRelayModuleData->SelfTest_Comp && ShmPrimaryMcuData->SelfTest_Comp && evInitFlag)
  855. {
  856. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  857. }
  858. }
  859. break;
  860. case _STEST_AC_CONTACTOR:
  861. {
  862. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  863. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  864. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  865. {
  866. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  867. printf("Communication board pass. \n");
  868. }
  869. }
  870. break;
  871. case _STEST_PSU_DETECT:
  872. {
  873. // 此測試主要測試 PSU 對應是否為正確的火線上電壓
  874. // 如果沒有 PSU 模組請 bypass
  875. if (ShmPsuData->Work_Step == _TEST_POWER_STEP || ShmPsuData->Work_Step == _TEST_COMPLETE)
  876. {
  877. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  878. }
  879. }
  880. break;
  881. case _STEST_PSU_CAP:
  882. {
  883. // 此測試是要確認當前總輸出能力
  884. // 如果沒有 PSU 模組請 bypass
  885. if (ShmPsuData->Work_Step == _TEST_COMPLETE)
  886. {
  887. sleep(1);
  888. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  889. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  890. }
  891. }
  892. break;
  893. }
  894. }
  895. else
  896. break;
  897. usleep(100000);
  898. }
  899. }
  900. int SpawnTask()
  901. {
  902. system("/root/Module_EventLogging &");
  903. system("/root/Module_PrimaryComm &");
  904. system("/root/Module_EvComm &");
  905. system("/root/Module_LcmControl &");
  906. system("/root/Module_InternalComm &");
  907. system("/root/Module_PsuComm &");
  908. //system("/root/OcppBackend &");
  909. //system("/root/Module_4g &");
  910. //system("/root/Module_Wifi &");
  911. //system("/root/Module_PsuComm &");
  912. //system("/root/InfyPowerPsu_Comm &");
  913. // 加入參數
  914. // char str[64];
  915. // memset(str, '\0', sizeof(65));
  916. // sprintf(str, "/root/Module_EvComm %x &", (CHAdeMO_QUANTITY + CCS_QUANTITY));
  917. // printf("%s \n", str);
  918. // system(str);
  919. return PASS;
  920. }
  921. int StoreUsrConfigData(struct SysConfigData *UsrData)
  922. {
  923. int fd,wrd;
  924. unsigned int i, Chk;
  925. byte *ptr;
  926. Chk = 0;
  927. ptr = (byte *) UsrData;
  928. for (i = 0; i < sizeof(struct SysConfigData) - 4; i++)
  929. {
  930. Chk += *(ptr + i);
  931. }
  932. UsrData->Checksum = Chk;
  933. fd = open("/dev/mtdblock10", O_RDWR);
  934. if (fd < 0)
  935. {
  936. #ifdef SystemLogMessage
  937. StoreLogMsg("[main]StoreUsrConfigData: open /dev/mtdblock10 NG");
  938. #endif
  939. return 0;
  940. }
  941. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  942. close(fd);
  943. if (wrd != (sizeof(struct SysConfigData)))
  944. {
  945. #ifdef SystemLogMessage
  946. StoreLogMsg("[main]StoreUsrConfigData: write /dev/mtdblock10 NG");
  947. #endif
  948. return 0;
  949. }
  950. fd = open("/dev/mtdblock11", O_RDWR);
  951. if (fd < 0)
  952. {
  953. #ifdef SystemLogMessage
  954. StoreLogMsg("[main]StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
  955. #endif
  956. return 0;
  957. }
  958. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  959. close(fd);
  960. if (wrd != (sizeof(struct SysConfigData)))
  961. {
  962. #ifdef SystemLogMessage
  963. StoreLogMsg("[main]StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
  964. #endif
  965. return 0;
  966. }
  967. return 1;
  968. }
  969. //===============================================
  970. // Common Detect Chk - Stop Charging ?
  971. //===============================================
  972. bool isEvBoardStopChargeFlag(byte gunIndex)
  973. {
  974. return chargingInfo[gunIndex]->StopChargeFlag;
  975. }
  976. //===============================================
  977. // Common Detect Chk - Chademo
  978. //===============================================
  979. bool isEvGunLocked_chademo(byte gunIndex)
  980. {
  981. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  982. }
  983. bool isEvContactorWelding_chademo(byte gunIndex)
  984. {
  985. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  986. }
  987. bool isEvStopReq_chademo(byte gunIndex)
  988. {
  989. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  990. }
  991. bool isEvStopCharging_chademo(byte gunIndex)
  992. {
  993. if (isEvGunLocked_chademo(gunIndex) == NO)
  994. {
  995. // 無鎖槍 = 停止
  996. printf("gun locked none. \n");
  997. return YES;
  998. }
  999. return NO;
  1000. }
  1001. byte isPrechargeStatus_chademo(byte gunIndex)
  1002. {
  1003. byte result = 0x00;
  1004. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1005. return result;
  1006. }
  1007. //===============================================
  1008. // Common Detect Chk - CCS
  1009. //===============================================
  1010. bool isEvGunLocked_ccs(byte gunIndex)
  1011. {
  1012. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1013. }
  1014. byte isPrechargeStatus_ccs(byte gunIndex)
  1015. {
  1016. byte result = 0x00;
  1017. if (ShmCcsData->CommProtocol == 0x01)
  1018. {
  1019. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1020. }
  1021. return result;
  1022. }
  1023. bool isEvStopCharging_ccs(byte gunIndex)
  1024. {
  1025. if (isEvGunLocked_ccs(gunIndex) == NO)
  1026. {
  1027. // 無鎖槍 = 停止
  1028. printf("gun locked none. \n");
  1029. return YES;
  1030. }
  1031. return NO;
  1032. }
  1033. //===============================================
  1034. // Callback
  1035. //===============================================
  1036. void _AutoReturnTimeout()
  1037. {
  1038. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1039. {
  1040. ClearDetectPluginFlag();
  1041. }
  1042. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1043. //
  1044. // for (byte i = 0; i < CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY; i++)
  1045. // {
  1046. // if (chargingInfo[i]->SystemStatus > S_IDLE)
  1047. // {
  1048. // switch(chargingInfo[i]->SystemStatus)
  1049. // {
  1050. // case S_PREPARNING:
  1051. // case S_PREPARING_FOR_EV:
  1052. // case S_PREPARING_FOR_EVSE:
  1053. // {
  1054. // ChangeLcmByIndex(i, _LCM_PRE_CHARGE);
  1055. // }
  1056. // break;
  1057. // case S_CHARGING:
  1058. // {
  1059. // ChangeLcmByIndex(i, _LCM_CHARGING);
  1060. // }
  1061. // break;
  1062. // case S_TERMINATING:
  1063. // {
  1064. // ChangeLcmByIndex(i, _LCM_COMPLETE);
  1065. // }
  1066. // break;
  1067. // }
  1068. // return;
  1069. // }
  1070. // }
  1071. // if (!IsAuthorizingMode())
  1072. // ChangeLcmByIndex(255, _LCM_IDLE);
  1073. }
  1074. void _SelfTestTimeout()
  1075. {
  1076. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1077. {
  1078. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  1079. {
  1080. setChargerMode(gun_index, MODE_ALARM);
  1081. }
  1082. ShmPsuData->Work_Step = _NO_WORKING;
  1083. }
  1084. }
  1085. void _AuthorizedTimeout()
  1086. {
  1087. if(IsAuthorizingMode())
  1088. {
  1089. printf("_AuthorizedTimeout \n");
  1090. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1091. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1092. ClearAuthorizedFlag();
  1093. }
  1094. }
  1095. void _DetectPlugInTimeout()
  1096. {
  1097. if(isDetectPlugin())
  1098. {
  1099. printf("_DetectPlugInTimeout \n");
  1100. ClearDetectPluginFlag();
  1101. }
  1102. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1103. }
  1104. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1105. {
  1106. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1107. {
  1108. if(!isEvGunLocked_chademo(gunIndex))
  1109. {
  1110. printf("_DetectEvChargingEnableTimeout (chademo) \n");
  1111. ChargingTerminalProcess(gunIndex);
  1112. _AutoReturnTimeout();
  1113. }
  1114. }
  1115. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  1116. {
  1117. if(!isEvGunLocked_ccs(gunIndex))
  1118. {
  1119. printf("_DetectEvChargingEnableTimeout (ccs) \n");
  1120. ChargingTerminalProcess(gunIndex);
  1121. _AutoReturnTimeout();
  1122. }
  1123. }
  1124. }
  1125. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1126. {
  1127. printf("_DetectEvseChargingEnableTimeout (GFD timeout) \n");
  1128. if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1129. {
  1130. setChargerMode(gunIndex, MODE_IDLE);
  1131. _AutoReturnTimeout();
  1132. }
  1133. }
  1134. void _PrepareTimeout(byte gunIndex)
  1135. {
  1136. printf("_PrechargeTimeout \n");
  1137. setChargerMode(gunIndex, MODE_IDLE);
  1138. _AutoReturnTimeout();
  1139. }
  1140. void _CompleteTimeout(byte gunIndex)
  1141. {
  1142. printf("_CompleteTimeout ====> %d \n", gunIndex);
  1143. setChargerMode(gunIndex, MODE_IDLE);
  1144. }
  1145. void _CcsPrechargeTimeout(byte gunIndex)
  1146. {
  1147. printf("_CcsPrechargeTimeout \n");
  1148. setChargerMode(gunIndex, MODE_IDLE);
  1149. }
  1150. //===============================================
  1151. // 取得卡號與卡號驗證
  1152. //===============================================
  1153. bool canStartCharging()
  1154. {
  1155. char buf2[16] = "";
  1156. memset(buf2, 0, ARRAY_SIZE(buf2));
  1157. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1158. {
  1159. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1160. }
  1161. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1162. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1163. if(strstr(buf2, "Accepted") != 0)
  1164. return true;
  1165. else
  1166. {
  1167. }
  1168. return false;
  1169. }
  1170. void AuthorizingStart()
  1171. {
  1172. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1173. }
  1174. void ClearAuthorizedFlag()
  1175. {
  1176. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1177. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1178. }
  1179. bool isAuthorizedComplete()
  1180. {
  1181. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1182. return false;
  1183. return true;
  1184. }
  1185. bool IsAuthorizingMode()
  1186. {
  1187. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq == NO)
  1188. return false;
  1189. return true;
  1190. }
  1191. byte GetCardNumber()
  1192. {
  1193. byte card_number[16];
  1194. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  1195. {
  1196. if(getRequestCardSN(rfidFd, 0, card_number))
  1197. {
  1198. //Get Card Number
  1199. byte len = card_number[0];
  1200. char buf2[32] = "";
  1201. memcpy(buf2, (card_number + 1), len);
  1202. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x0, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1203. for (byte index = 0; index < len; index++)
  1204. {
  1205. sprintf((char *)ShmSysConfigAndInfo->SysConfig.UserId + (index * 2), "%02X", buf2[index]);
  1206. }
  1207. printf("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  1208. return PASS;
  1209. }
  1210. }
  1211. return FAIL;
  1212. }
  1213. //===============================================
  1214. // 掃描插槍狀況
  1215. //===============================================
  1216. void ClearDetectPluginFlag()
  1217. {
  1218. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1219. }
  1220. void DetectPluginStart()
  1221. {
  1222. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1223. }
  1224. bool isDetectPlugin()
  1225. {
  1226. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1227. return YES;
  1228. return NO;
  1229. }
  1230. //===============================================
  1231. // EmergencyStop and Charging Stop
  1232. //===============================================
  1233. void ChargingTerminalProcess(byte gunIndex)
  1234. {
  1235. setChargerMode(gunIndex, MODE_TERMINATING);
  1236. }
  1237. void StopChargingProcessByString(byte level, byte gun_index, char *string)
  1238. {
  1239. if (strlen((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index]) == 0 ||
  1240. level > ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level)
  1241. {
  1242. memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], string, 7);
  1243. ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = level;
  1244. }
  1245. }
  1246. void ReleaseChargingProcessByString(byte gun_index, char *code)
  1247. {
  1248. memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], "", 7);
  1249. ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = 0;
  1250. }
  1251. // 各小板的停止充電處理函式
  1252. void EmcOccureByString(byte index, char *code)
  1253. {
  1254. bool isStopCharger = false;
  1255. if (strncmp(code, "012251", 6) == 0 || strncmp(code, "012252", 6) == 0 ||
  1256. strncmp(code, "012237", 6) == 0 || strncmp(code, "012238", 6) == 0)
  1257. {
  1258. isStopCharger = true;
  1259. }
  1260. if (isStopCharger)
  1261. {
  1262. for (byte gun = 0; gun < _gunCount; gun++)
  1263. {
  1264. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1265. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1266. {
  1267. ChargingTerminalProcess(gun);
  1268. }
  1269. StopChargingProcessByString(2, gun, code);
  1270. }
  1271. }
  1272. else
  1273. {
  1274. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1275. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1276. {
  1277. ChargingTerminalProcess(index);
  1278. }
  1279. StopChargingProcessByString(2, index, code);
  1280. }
  1281. }
  1282. void ReleaseEmsOccureByString(byte index, char *code)
  1283. {
  1284. bool isReleaseCharger = false;
  1285. bool isTrigger = false;
  1286. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1287. {
  1288. isTrigger = true;
  1289. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1290. isReleaseCharger = true;
  1291. }
  1292. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  1293. {
  1294. isTrigger = true;
  1295. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  1296. isReleaseCharger = true;
  1297. }
  1298. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  1299. {
  1300. isTrigger = true;
  1301. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  1302. isReleaseCharger = true;
  1303. }
  1304. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  1305. {
  1306. isTrigger = true;
  1307. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1308. isReleaseCharger = true;
  1309. }
  1310. else if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1311. {
  1312. isTrigger = true;
  1313. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1314. }
  1315. if (isTrigger)
  1316. {
  1317. if (isReleaseCharger)
  1318. {
  1319. for (byte gun = 0; gun < _gunCount; gun++)
  1320. {
  1321. ReleaseChargingProcessByString(gun, code);
  1322. }
  1323. }
  1324. else
  1325. {
  1326. ReleaseChargingProcessByString(index, code);
  1327. }
  1328. }
  1329. }
  1330. //===============================================
  1331. // 確認硬體 (按鈕) 狀態
  1332. //===============================================
  1333. bool leftBtnPush = false;
  1334. bool rightBtnPush = false;
  1335. void ChkPrimaryStatus()
  1336. {
  1337. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  1338. {
  1339. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  1340. EmcOccureByString(0, "012251");
  1341. }
  1342. else
  1343. ReleaseEmsOccureByString(0, "012251");
  1344. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  1345. {
  1346. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  1347. EmcOccureByString(0, "012238");
  1348. }
  1349. else
  1350. ReleaseEmsOccureByString(0, "012238");
  1351. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  1352. {
  1353. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  1354. EmcOccureByString(0, "012237");
  1355. }
  1356. else
  1357. ReleaseEmsOccureByString(0, "012237");
  1358. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  1359. {
  1360. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  1361. EmcOccureByString(0, "012252");
  1362. }
  1363. else
  1364. ReleaseEmsOccureByString(0, "012252");
  1365. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  1366. {
  1367. if(!leftBtnPush)
  1368. {
  1369. printf("left btn down............................... \n");
  1370. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1371. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1372. {
  1373. case S_IDLE:
  1374. {
  1375. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  1376. }
  1377. break;
  1378. case S_CHARGING:
  1379. {
  1380. // 停止充電
  1381. printf("To Stop = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1382. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 9;
  1383. //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1384. }
  1385. break;
  1386. case S_COMPLETE:
  1387. {
  1388. // 回 IDLE
  1389. printf("Back to IDLE = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1390. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 1;
  1391. }
  1392. break;
  1393. }
  1394. }
  1395. leftBtnPush = true;
  1396. // 左邊的選槍按鈕,只有在雙槍都在充電時候才有用 : 30KW 以下該按鈕無作用
  1397. }
  1398. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  1399. {
  1400. if(leftBtnPush)
  1401. printf("left btn up............................... \n");
  1402. leftBtnPush = false;
  1403. }
  1404. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  1405. {
  1406. if(!rightBtnPush)
  1407. {
  1408. printf("right btn down............................... \n");
  1409. if (_gunCount > 1)
  1410. {
  1411. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 1;
  1412. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1413. {
  1414. case S_IDLE:
  1415. {
  1416. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  1417. }
  1418. break;
  1419. case S_CHARGING:
  1420. {
  1421. // 停止充電
  1422. printf("To Stop = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1423. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 9;
  1424. //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1425. }
  1426. break;
  1427. case S_COMPLETE:
  1428. {
  1429. // 回 IDLE
  1430. printf("Back to IDLE = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1431. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 1;
  1432. }
  1433. break;
  1434. }
  1435. }
  1436. }
  1437. rightBtnPush = true;
  1438. // 右邊的按鈕,只作用在當前頁面,當前頁面如果在可以回 Home 與 停止充電的狀態為可用
  1439. // switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1440. // {
  1441. // case S_IDLE: {}break;
  1442. // case S_AUTHORIZING: {}break;
  1443. // case S_PREPARING_FOR_EV: {}break;
  1444. // case S_PREPARING_FOR_EVSE: {}break;
  1445. // case S_CCS_PRECHARGE_ST0: {}break;
  1446. // case S_CCS_PRECHARGE_ST1: {}break;
  1447. // case S_CHARGING:
  1448. // {
  1449. // // 停止充電
  1450. // printf("Stop --------------------------------------------------- \n");
  1451. // //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1452. // }
  1453. // break;
  1454. // case S_COMPLETE:
  1455. // {
  1456. // //setChargerMode(ShmSysConfigAndInfo->SysInfo.CurGunSelected, MODE_IDLE);
  1457. // }
  1458. // break;
  1459. // }
  1460. }
  1461. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  1462. {
  1463. if(rightBtnPush)
  1464. printf("right btn up............................... \n");
  1465. rightBtnPush = false;
  1466. }
  1467. }
  1468. //===============================================
  1469. // 確認各小板偵測的錯誤狀況
  1470. //===============================================
  1471. void CheckErrorOccurStatus(byte index)
  1472. {
  1473. // 小板
  1474. if (chargingInfo[index]->Type == _Type_Chademo)
  1475. {
  1476. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1477. EmcOccureByString(index, "023730");
  1478. else if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  1479. EmcOccureByString(index, "011012");
  1480. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1481. EmcOccureByString(index, "012234");
  1482. }
  1483. else if (chargingInfo[index]->Type == _Type_CCS)
  1484. {
  1485. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  1486. EmcOccureByString(index, "011014");
  1487. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1488. EmcOccureByString(index, "012235");
  1489. }
  1490. }
  1491. //===============================================
  1492. // 確認 GPIO 狀態
  1493. //===============================================
  1494. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1495. {
  1496. int fd;
  1497. char buf[MAX_BUF];
  1498. char ch;
  1499. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1500. fd = open(buf, O_RDONLY);
  1501. if (fd < 0) {
  1502. perror("gpio/get-value");
  1503. return fd;
  1504. }
  1505. read(fd, &ch, 1);
  1506. if (ch != '0') {
  1507. *value = 1;
  1508. } else {
  1509. *value = 0;
  1510. }
  1511. close(fd);
  1512. return 0;
  1513. }
  1514. void CheckGunTypeFromHw()
  1515. {
  1516. int pinIn[4] = { 22, 23, 44, 45 };
  1517. unsigned int gpioValue = 0;
  1518. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1519. gpio_get_value(pinIn[i], &gpioValue);
  1520. {
  1521. switch (pinIn[i])
  1522. {
  1523. case 22:
  1524. bd1_1_status = gpioValue;
  1525. break;
  1526. case 23:
  1527. bd1_2_status = gpioValue;
  1528. break;
  1529. case 44:
  1530. bd0_1_status = gpioValue;
  1531. break;
  1532. case 45:
  1533. bd0_2_status = gpioValue;
  1534. break;
  1535. }
  1536. }
  1537. }
  1538. }
  1539. void CheckGpioInStatus()
  1540. {
  1541. int pinIn[2] = { 27, 47 };
  1542. unsigned int gpioValue = 0;
  1543. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  1544. {
  1545. gpio_get_value(pinIn[i], &gpioValue);
  1546. if (gpioValue == 0x01)
  1547. {
  1548. switch(pinIn[i])
  1549. {
  1550. // 小板緊急停止
  1551. case 27:
  1552. {
  1553. for(int i = 0; i < _gunCount; i++)
  1554. {
  1555. if (chargingInfo[i]->slotsIndex == 1)
  1556. {
  1557. if (chargingInfo[i]->Type == _Type_Chademo)
  1558. EmcOccureByString(i, "023730");
  1559. else if (chargingInfo[i]->Type == _Type_CCS)
  1560. EmcOccureByString(i, "013627");
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. break;
  1566. case 47:
  1567. {
  1568. for(int i = 0; i < _gunCount; i++)
  1569. {
  1570. if (chargingInfo[i]->slotsIndex == 3)
  1571. {
  1572. if (chargingInfo[i]->Type == _Type_Chademo)
  1573. EmcOccureByString(i, "023730");
  1574. else if (chargingInfo[i]->Type == _Type_CCS)
  1575. EmcOccureByString(i, "013627");
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. break;
  1581. }
  1582. }
  1583. else
  1584. {
  1585. switch (pinIn[i])
  1586. {
  1587. // 小板解除緊急停止
  1588. case 27:
  1589. {
  1590. for(int i = 0; i < _gunCount; i++)
  1591. {
  1592. if (chargingInfo[i]->slotsIndex == 1)
  1593. {
  1594. if (chargingInfo[i]->Type == _Type_Chademo)
  1595. ReleaseEmsOccureByString(i, "023730");
  1596. else if (chargingInfo[i]->Type == _Type_CCS)
  1597. ReleaseEmsOccureByString(i, "013627");
  1598. break;
  1599. }
  1600. }
  1601. }
  1602. break;
  1603. case 47:
  1604. {
  1605. // 右槍
  1606. for (int i = 0; i < _gunCount; i++)
  1607. {
  1608. if (chargingInfo[i]->slotsIndex == 3)
  1609. {
  1610. if (chargingInfo[i]->Type == _Type_Chademo)
  1611. ReleaseEmsOccureByString(i, "023730");
  1612. else if (chargingInfo[i]->Type == _Type_CCS)
  1613. ReleaseEmsOccureByString(i, "013627");
  1614. break;
  1615. }
  1616. }
  1617. }
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. }
  1623. //===============================================
  1624. // Main process
  1625. //===============================================
  1626. // 檢查 Byte 中某個 Bit 的值
  1627. // _byte : 欲改變的 byte
  1628. // _bit : 該 byte 的第幾個 bit
  1629. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  1630. {
  1631. return ( _byte & mask_table[_bit] ) != 0x00;
  1632. }
  1633. // 設定 Byte 中某個 Bit的值
  1634. // _byte : 欲改變的 byte
  1635. // _bit : 該 byte 的第幾個 bit
  1636. // value : 修改的值為 0 or 1
  1637. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  1638. {
  1639. if(value == 1)
  1640. *_byte |= (1 << _bit);
  1641. else if (value == 0)
  1642. *_byte ^= (1 << _bit);
  1643. }
  1644. void UserScanFunction()
  1645. {
  1646. bool idleReq = false;
  1647. unsigned char stopReq = 255;
  1648. // 當前非驗證的狀態
  1649. if(!IsAuthorizingMode())
  1650. {
  1651. // 先判斷現在是否可以提供刷卡
  1652. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  1653. // 2. 停止充電
  1654. for (byte i = 0; i < _gunCount; i++)
  1655. {
  1656. // 二擇一
  1657. if (chargingInfo[i]->SystemStatus == S_CHARGING)
  1658. {
  1659. stopReq = i;
  1660. }
  1661. else if (chargingInfo[i]->SystemStatus == S_IDLE)
  1662. {
  1663. idleReq = true;
  1664. }
  1665. }
  1666. //printf("idleReq = %x, stopReq = %d \n", idleReq, stopReq);
  1667. // 有閒置的槍號,即可接受刷卡
  1668. if (idleReq || stopReq < _gunCount)
  1669. {
  1670. // 取卡號,假設 : 刷卡過了
  1671. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  1672. {
  1673. if (stopReq < _gunCount)
  1674. {
  1675. char value[32];
  1676. memcpy(value, (unsigned char *)chargingInfo[stopReq]->CardNumber, ARRAY_SIZE(chargingInfo[stopReq]->CardNumber));
  1677. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  1678. {
  1679. ChargingTerminalProcess(stopReq);
  1680. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1681. return;
  1682. }
  1683. }
  1684. if (idleReq)
  1685. {
  1686. // LCM => Authorizing
  1687. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  1688. //ChangeLcmByIndex(_LCM_AUTHORIZING);
  1689. // 進入確認卡號狀態
  1690. AuthorizingStart();
  1691. // authorizing timer
  1692. StartSystemTimeoutDet(Timeout_Authorizing);
  1693. autoReturnTimeoutFlag = NO;
  1694. }
  1695. }
  1696. }
  1697. }
  1698. else
  1699. {
  1700. // 確認驗證卡號完成沒
  1701. if (isAuthorizedComplete() || true)
  1702. {
  1703. StopSystemTimeoutDet();
  1704. // 判斷後台回覆狀態
  1705. if(canStartCharging() || true)
  1706. {
  1707. // LCM => Authorize complete
  1708. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  1709. //ChangeLcmByIndex(_LCM_AUTHORIZ_COMP);
  1710. // 通過認證,開始確認當前要進入充電的槍號
  1711. DetectPluginStart();
  1712. }
  1713. else
  1714. {
  1715. // LCM => Authorize fail
  1716. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1717. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1718. }
  1719. ClearAuthorizedFlag();
  1720. }
  1721. }
  1722. }
  1723. unsigned char isModeChange(unsigned char gun_index)
  1724. {
  1725. unsigned char result = NO;
  1726. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  1727. {
  1728. result = YES;
  1729. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  1730. }
  1731. return result;
  1732. }
  1733. void ScannerCardProcess()
  1734. {
  1735. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 0)
  1736. {
  1737. isCardScan = true;
  1738. // 處理刷卡及驗證卡號的動作
  1739. UserScanFunction();
  1740. }
  1741. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  1742. {
  1743. StartSystemTimeoutDet(Timeout_VerifyFail);
  1744. isCardScan = false;
  1745. }
  1746. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1747. {
  1748. StartSystemTimeoutDet(Timeout_VerifyComp);
  1749. }
  1750. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1751. {
  1752. StartSystemTimeoutDet(Timeout_WaitPlug);
  1753. }
  1754. else
  1755. isCardScan = false;
  1756. }
  1757. void AddGunInfoByConnector(byte typeValue, byte slots)
  1758. {
  1759. switch (typeValue)
  1760. {
  1761. case '0': // none
  1762. break;
  1763. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  1764. break;
  1765. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  1766. break;
  1767. case '3': // IEC 62196-2 Type 2 Plug
  1768. break;
  1769. case '4': // IEC 62196-2 Type 2 Socket
  1770. break;
  1771. case '5': // GB/T AC Plug
  1772. break;
  1773. case '6': // GB/T AC Socket
  1774. break;
  1775. case 'J': // CHAdeMO
  1776. {
  1777. if (CHAdeMO_QUANTITY > _chademoIndex)
  1778. {
  1779. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  1780. chargingInfo[_gunIndex]->Index = _gunIndex;
  1781. chargingInfo[_gunIndex]->slotsIndex = slots;
  1782. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  1783. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  1784. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  1785. _chademoIndex++;
  1786. _gunIndex++;
  1787. }
  1788. }
  1789. break;
  1790. case 'U': // CCS1 combo
  1791. break;
  1792. case 'E': // CCS2 combo
  1793. {
  1794. if (CCS_QUANTITY > _ccsIndex)
  1795. {
  1796. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  1797. chargingInfo[_gunIndex]->Index = _gunIndex;
  1798. chargingInfo[_gunIndex]->slotsIndex = slots;
  1799. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  1800. chargingInfo[_gunIndex]->Type = _Type_CCS;
  1801. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  1802. // 現階段預設為走 DIN70121
  1803. ShmCcsData->CommProtocol = 0x01;
  1804. _ccsIndex++;
  1805. _gunIndex++;
  1806. }
  1807. }
  1808. break;
  1809. case 'G': // GBT DC
  1810. break;
  1811. case 'D': // GBT DC x 2
  1812. break;
  1813. }
  1814. }
  1815. bool CheckConnectorTypeStatus()
  1816. {
  1817. bool result = true;
  1818. printf("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  1819. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  1820. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  1821. {
  1822. byte slots = 1;
  1823. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  1824. {
  1825. AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots);
  1826. slots++;
  1827. }
  1828. _gunCount = _gunIndex;
  1829. printf("_gunCount = %d \n", _gunCount);
  1830. if (_gunCount == 0)
  1831. result = false;
  1832. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  1833. for (byte gunIndex = 0; gunIndex < _gunCount; gunIndex++)
  1834. {
  1835. if (bd0_1_status == 0 && bd0_2_status == 1)
  1836. {
  1837. // 與硬體相同 type : Chademo
  1838. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1839. {
  1840. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1841. }
  1842. }
  1843. else if (bd0_1_status == 1 && bd0_2_status == 0)
  1844. {
  1845. // 與硬體相同 type : CCS
  1846. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  1847. {
  1848. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1849. }
  1850. }
  1851. if (bd1_1_status == 0 && bd1_2_status == 1)
  1852. {
  1853. // 與硬體相同 type : Chademo
  1854. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1855. {
  1856. chargingInfo[gunIndex]->Evboard_id = 0x02;
  1857. }
  1858. if (_gunCount == 1)
  1859. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1860. }
  1861. else if (bd1_1_status == 1 && bd1_2_status == 0)
  1862. {
  1863. // 與硬體相同 type : CCS
  1864. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  1865. {
  1866. chargingInfo[gunIndex]->Evboard_id = 0x02;
  1867. }
  1868. if (_gunCount == 1)
  1869. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1870. }
  1871. printf("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  1872. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  1873. result = false;
  1874. }
  1875. }
  1876. else
  1877. {
  1878. // Module Name 不正確 - 告警
  1879. result = false;
  1880. }
  1881. return result;
  1882. }
  1883. void KillTask()
  1884. {
  1885. ChangeLcmByIndex(_LCM_FIX);
  1886. system("killall Module_EventLogging");
  1887. system("killall Module_PrimaryComm");
  1888. system("killall Module_EvComm");
  1889. system("killall Module_LcmControl");
  1890. system("killall Module_InternalComm");
  1891. system("killall Module_PsuComm");
  1892. }
  1893. char CheckUpdateProcess()
  1894. {
  1895. DIR *d;
  1896. struct dirent *dir;
  1897. d = opendir("/mnt/");
  1898. if (d)
  1899. {
  1900. long int MaxLen=48*1024*1024, ImageLen = 0;
  1901. while ((dir = readdir(d)) != NULL)
  1902. {
  1903. char *new_str;
  1904. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  1905. new_str[0] = '\0';
  1906. strcat(new_str, "/mnt/");
  1907. strcat(new_str, dir->d_name);
  1908. int fd = open(new_str, O_RDONLY);
  1909. if (fd < 0)
  1910. {
  1911. return FAIL;
  1912. }
  1913. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1914. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1915. //get the image length
  1916. ImageLen = read(fd, ptr, MaxLen);
  1917. if (ImageLen > 20)
  1918. {
  1919. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  1920. printf("Typed...%x \r\n", Type);
  1921. switch (Type)
  1922. {
  1923. case 0x10000001:
  1924. case 0x10000002:
  1925. case 0x10000003:
  1926. case 0x10000004:
  1927. case 0x10000005:
  1928. case 0x10000006:
  1929. {
  1930. // CSU_PRIMARY_CONTROLLER : 0x10000006
  1931. int fd = InitComPort(0x04);
  1932. if (Upgrade_UART(fd, Type, 0x04, new_str, ShmSysConfigAndInfo) == PASS)
  1933. return PASS;
  1934. else
  1935. return FAIL;
  1936. close(fd);
  1937. }
  1938. break;
  1939. case 0x1000000B:
  1940. {
  1941. // CHAdeMO_BOARD : 0x1000000B
  1942. for(byte index = 0; index < _gunCount; index++)
  1943. {
  1944. if (chargingInfo[index]->Type == _Type_Chademo)
  1945. {
  1946. int CanFd = InitCanBus();
  1947. if (CanFd > 0)
  1948. {
  1949. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo) == PASS)
  1950. {
  1951. printf("Upgrad OK. \n");
  1952. return PASS;
  1953. }
  1954. else
  1955. {
  1956. printf("Upgrad Fail. \n");
  1957. return FAIL;
  1958. }
  1959. }
  1960. else
  1961. {
  1962. printf("Upgrad FD fail. \n");
  1963. return FAIL;
  1964. }
  1965. }
  1966. }
  1967. break;
  1968. }
  1969. }
  1970. }
  1971. free(new_str);
  1972. free(ptr);
  1973. }
  1974. }
  1975. free(dir);
  1976. closedir(d);
  1977. return FAIL;
  1978. }
  1979. void CreateRfidFork()
  1980. {
  1981. pid_t rfidRecPid;
  1982. \
  1983. rfidRecPid = fork();
  1984. if (rfidRecPid == 0)
  1985. {
  1986. while(true)
  1987. {
  1988. // 刷卡判斷
  1989. GetCardNumber();
  1990. usleep(100000);
  1991. }
  1992. }
  1993. }
  1994. void StartSystemTimeoutDet(unsigned char flag)
  1995. {
  1996. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  1997. {
  1998. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  1999. }
  2000. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  2001. }
  2002. void StopSystemTimeoutDet()
  2003. {
  2004. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  2005. }
  2006. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  2007. {
  2008. if (gunIndex < _gunCount)
  2009. {
  2010. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  2011. {
  2012. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  2013. }
  2014. chargingInfo[gunIndex]->TimeoutFlag = flag;
  2015. }
  2016. }
  2017. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  2018. {
  2019. if (gunIndex < _gunCount)
  2020. {
  2021. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  2022. }
  2023. }
  2024. void CreateTimeoutFork()
  2025. {
  2026. pid_t timeoutPid;
  2027. timeoutPid = fork();
  2028. if (timeoutPid == 0)
  2029. {
  2030. while(true)
  2031. {
  2032. // 系統
  2033. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  2034. {
  2035. case Timeout_SelftestChk:
  2036. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 60000000)
  2037. {
  2038. _SelfTestTimeout();
  2039. StopSystemTimeoutDet();
  2040. }
  2041. break;
  2042. case Timeout_Authorizing:
  2043. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 60000000)
  2044. {
  2045. _AuthorizedTimeout();
  2046. StopSystemTimeoutDet();
  2047. }
  2048. break;
  2049. case Timeout_VerifyFail:
  2050. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2051. {
  2052. _AutoReturnTimeout();
  2053. StopSystemTimeoutDet();
  2054. }
  2055. break;
  2056. case Timeout_VerifyComp:
  2057. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2058. {
  2059. _AutoReturnTimeout();
  2060. StopSystemTimeoutDet();
  2061. }
  2062. break;
  2063. case Timeout_WaitPlug:
  2064. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2065. {
  2066. _DetectPlugInTimeout();
  2067. StopSystemTimeoutDet();
  2068. }
  2069. break;
  2070. }
  2071. // 各槍
  2072. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2073. {
  2074. switch(chargingInfo[gun_index]->TimeoutFlag)
  2075. {
  2076. case Timeout_Preparing:
  2077. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 30000000)
  2078. {
  2079. _PrepareTimeout(gun_index);
  2080. StopGunInfoTimeoutDet(gun_index);
  2081. }
  2082. break;
  2083. case Timeout_EvChargingDet:
  2084. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
  2085. {
  2086. _DetectEvChargingEnableTimeout(gun_index);
  2087. StopGunInfoTimeoutDet(gun_index);
  2088. }
  2089. break;
  2090. case Timeout_EvseChargingDet:
  2091. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2092. {
  2093. _DetectEvseChargingEnableTimeout(gun_index);
  2094. StopGunInfoTimeoutDet(gun_index);
  2095. }
  2096. break;
  2097. case Timeout_WaitforCompleteDet:
  2098. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  2099. {
  2100. _CompleteTimeout(gun_index);
  2101. StopGunInfoTimeoutDet(gun_index);
  2102. }
  2103. break;
  2104. case Timeout_ForCcsPrechargeDet:
  2105. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2106. {
  2107. _CcsPrechargeTimeout(gun_index);
  2108. StopGunInfoTimeoutDet(gun_index);
  2109. }
  2110. break;
  2111. }
  2112. }
  2113. usleep(100000);
  2114. }
  2115. }
  2116. }
  2117. int main(void)
  2118. {
  2119. InitGPIO();
  2120. InitEthernet();
  2121. sleep(1);
  2122. system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  2123. sleep(1);
  2124. system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
  2125. //echo 1 > /sys/class/gpio/gpio110/value
  2126. //return 0;
  2127. if(CreateShareMemory() == 0)
  2128. {
  2129. #ifdef SystemLogMessage
  2130. DEBUG_ERROR("CreatShareMemory NG \n");
  2131. #endif
  2132. if(ShmStatusCodeData!=NULL)
  2133. {
  2134. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  2135. }
  2136. return 0;
  2137. sleep(5);
  2138. system("reboot -f");
  2139. sleep(5);
  2140. system("reboot -f");
  2141. }
  2142. printf("\n");
  2143. printf("CheckGunTypeFromHw....... \n");
  2144. CheckGunTypeFromHw();
  2145. char *moduleName = "DSYE601E00T2PH";
  2146. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, moduleName, strlen(moduleName));
  2147. if (!CheckConnectorTypeStatus())
  2148. {
  2149. // Module Name 與硬體對應不正確
  2150. printf("Module Name & HW info none match. \n");
  2151. DEBUG_ERROR("Module Name & HW info none match. \n");
  2152. sleep(5);
  2153. return 0;
  2154. }
  2155. printf("Module Name & HW info correct. Initialize.......\n");
  2156. Initialization();
  2157. printf("Spawn all Task. \n");
  2158. SpawnTask();
  2159. ChangeLcmByIndex(_LCM_INIT);
  2160. CreateTimeoutFork();
  2161. printf("Self test. \n");
  2162. SelfTestRun();
  2163. StopSystemTimeoutDet();
  2164. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  2165. {
  2166. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2167. {
  2168. setChargerMode(gun_index, MODE_ALARM);
  2169. }
  2170. ChangeLcmByIndex(_LCM_FIX);
  2171. return FAIL;
  2172. }
  2173. else
  2174. {
  2175. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2176. {
  2177. setChargerMode(gun_index, MODE_IDLE);
  2178. }
  2179. }
  2180. ChangeLcmByIndex(_LCM_IDLE);
  2181. sleep(1);
  2182. //***** 須新增的偵測 *****//
  2183. // 1. Thernal - 控制風扇轉速
  2184. // 2. ouput fuse - 控制風扇轉速
  2185. CreateRfidFork();
  2186. // Main loop
  2187. printf("Main Loop. \n");
  2188. for (;;)
  2189. {
  2190. ChkPrimaryStatus();
  2191. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_IDLE)
  2192. {
  2193. //printf("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  2194. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  2195. {
  2196. KillTask();
  2197. if (CheckUpdateProcess() == PASS)
  2198. {
  2199. printf("update complete. \n");
  2200. }
  2201. else
  2202. {
  2203. printf("update fail. \n");
  2204. }
  2205. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  2206. sleep(5);
  2207. system("reboot -f");
  2208. }
  2209. }
  2210. // usleep(whileLoopTime * 100);
  2211. // continue;
  2212. // 讀卡邏輯
  2213. ScannerCardProcess();
  2214. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2215. {
  2216. CheckGpioInStatus();
  2217. CheckErrorOccurStatus(gun_index);
  2218. switch(chargingInfo[gun_index]->SystemStatus)
  2219. {
  2220. case S_IDLE:
  2221. {
  2222. if (isModeChange(gun_index))
  2223. {
  2224. printf("S_IDLE================================== %x \n", gun_index);
  2225. chargingInfo[gun_index]->RemainChargingDuration = 0;
  2226. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  2227. }
  2228. if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 2)
  2229. {
  2230. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  2231. ClearDetectPluginFlag();
  2232. }
  2233. else
  2234. {
  2235. // 判斷是否有啟用檢查插槍
  2236. if(isDetectPlugin())
  2237. {
  2238. // 卡號驗證成功後,等待充電槍插入充電車
  2239. if (chargingInfo[gun_index]->ConnectorPlugIn == YES)
  2240. {
  2241. ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
  2242. strcpy((char *)chargingInfo[gun_index]->CardNumber, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  2243. // 當前操作的槍號,進入 Preparing
  2244. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  2245. ClearDetectPluginFlag();
  2246. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2247. }
  2248. else if (!isCardScan)
  2249. {
  2250. // LCM => Waiting for plugging
  2251. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  2252. }
  2253. }
  2254. else
  2255. {
  2256. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2257. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  2258. }
  2259. }
  2260. }
  2261. break;
  2262. case S_REASSIGN_CHECK:
  2263. {
  2264. if (isModeChange(gun_index))
  2265. {
  2266. printf("S_REASSIGN_CHECK================================== %x \n", gun_index);
  2267. StopSystemTimeoutDet();
  2268. }
  2269. bool isRessign = false;
  2270. if (_gunCount > 1)
  2271. {
  2272. for (byte index = 0; index < _gunCount; index++)
  2273. {
  2274. // 有其他槍已經分配好 psu 模塊
  2275. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  2276. chargingInfo[index]->SystemStatus >= S_PREPARNING)
  2277. {
  2278. printf("=============Smart Charging============= Step 1 \n");
  2279. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE;
  2280. isRessign = true;
  2281. break;
  2282. }
  2283. }
  2284. }
  2285. if (isRessign)
  2286. setChargerMode(gun_index, MODE_REASSIGN);
  2287. else
  2288. setChargerMode(gun_index, MODE_PRECHARGE);
  2289. }
  2290. break;
  2291. case S_REASSIGN:
  2292. {
  2293. if (isModeChange(gun_index))
  2294. {
  2295. printf("S_REASSIGN================================== %x \n", gun_index);
  2296. }
  2297. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  2298. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  2299. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  2300. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_MAIN)
  2301. setChargerMode(gun_index, MODE_PRECHARGE);
  2302. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY &&
  2303. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  2304. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_MAIN;
  2305. }
  2306. break;
  2307. case S_PREPARNING:
  2308. {
  2309. if (isModeChange(gun_index))
  2310. {
  2311. printf("S_PREPARNING================================== %x \n", gun_index);
  2312. StopGunInfoTimeoutDet(gun_index);
  2313. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  2314. }
  2315. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  2316. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2317. // Precharge 三個流程 : 1 Precharge, 2 Preparing for ev, 3 Preparing for evse
  2318. // Precharge : AC Contactor <Relay board>, Relay k1 k2 <Relay board>, PSU AddressAssignment, PSU GroupAvailablePower
  2319. // Preparing for ev : 車端通訊流程
  2320. // Preparing for evse : PSU (output 500V, 2A), GFD Test <Relay board>
  2321. //ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES &&
  2322. if (((ShmPsuData->SystemPresentPsuQuantity > 0 &&
  2323. ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 &&
  2324. ShmPsuData->PsuGroup[gun_index].GroupAvailablePower > 10) &&
  2325. chargingInfo[gun_index]->AvailableChargingPower > 10))
  2326. {
  2327. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  2328. }
  2329. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2330. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2331. }
  2332. break;
  2333. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  2334. {
  2335. if (isModeChange(gun_index))
  2336. {
  2337. printf("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  2338. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2339. StopGunInfoTimeoutDet(gun_index);
  2340. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  2341. }
  2342. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  2343. {
  2344. // 檢查車端的槍鎖是否為鎖上
  2345. if (isEvGunLocked_chademo(gun_index) == YES)
  2346. {
  2347. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  2348. }
  2349. }
  2350. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  2351. {
  2352. // 檢查車端的 charging enable 是否為 1
  2353. if (isEvGunLocked_ccs(gun_index) == YES)
  2354. {
  2355. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  2356. }
  2357. }
  2358. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2359. {
  2360. // 板端要求停止
  2361. ChargingTerminalProcess(gun_index);
  2362. }
  2363. // LCM => Pre-charging
  2364. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2365. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2366. }
  2367. break;
  2368. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  2369. {
  2370. if (isModeChange(gun_index))
  2371. {
  2372. printf("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  2373. StopGunInfoTimeoutDet(gun_index);
  2374. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  2375. }
  2376. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  2377. {
  2378. // 檢查樁端的 GFD 結果
  2379. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  2380. {
  2381. // 當前操作的槍號,進入 Charging
  2382. setChargerMode(gun_index, MODE_CHARGING);
  2383. }
  2384. }
  2385. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  2386. {
  2387. // 檢查樁端的 GFD 結果
  2388. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS)
  2389. {
  2390. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  2391. }
  2392. }
  2393. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2394. {
  2395. // 板端要求停止
  2396. ChargingTerminalProcess(gun_index);
  2397. }
  2398. // LCM => Pre-charging
  2399. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2400. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2401. }
  2402. break;
  2403. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  2404. {
  2405. if (isModeChange(gun_index))
  2406. {
  2407. printf("S_CHARGING================================== %x \n", gun_index);
  2408. StopGunInfoTimeoutDet(gun_index);
  2409. ftime(&startChargingTime[gun_index]);
  2410. }
  2411. ftime(&endChargingTime[gun_index]);
  2412. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  2413. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2414. {
  2415. // 板端要求停止
  2416. ChargingTerminalProcess(gun_index);
  2417. }
  2418. // LCM => Charging
  2419. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2420. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  2421. }
  2422. break;
  2423. case S_TERMINATING:
  2424. {
  2425. if (isModeChange(gun_index))
  2426. {
  2427. printf ("terminating......................... %x \n", gun_index);
  2428. StopGunInfoTimeoutDet(gun_index);
  2429. }
  2430. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  2431. {
  2432. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  2433. if (isEvStopCharging_chademo(gun_index) == YES)
  2434. {
  2435. setChargerMode(gun_index, MODE_COMPLETE);
  2436. }
  2437. }
  2438. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  2439. {
  2440. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  2441. if (isEvStopCharging_ccs(gun_index) == YES)
  2442. {
  2443. setChargerMode(gun_index, MODE_COMPLETE);
  2444. }
  2445. }
  2446. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2447. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  2448. //ChangeLcmByIndex(gun_index, _LCM_COMPLETE);
  2449. }
  2450. break;
  2451. case S_COMPLETE:
  2452. {
  2453. if (isModeChange(gun_index))
  2454. {
  2455. printf ("complete......................... %x \n", gun_index);
  2456. ftime(&endChargingTime[gun_index]);
  2457. if (chargingInfo[gun_index]->RemainChargingDuration != 0)
  2458. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  2459. strcpy((char *)chargingInfo[gun_index]->CardNumber, "");
  2460. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2461. StopGunInfoTimeoutDet(gun_index);
  2462. StartGunInfoTimeoutDet(gun_index, Timeout_WaitforCompleteDet);
  2463. }
  2464. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2465. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  2466. }
  2467. break;
  2468. case S_CCS_PRECHARGE_ST0:
  2469. {
  2470. if (isModeChange(gun_index))
  2471. {
  2472. printf("CCS Precharge Processing 1....................%x \n", gun_index);
  2473. StopGunInfoTimeoutDet(gun_index);
  2474. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  2475. }
  2476. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2477. {
  2478. // 板端要求停止
  2479. ChargingTerminalProcess(gun_index);
  2480. }
  2481. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  2482. // 切換 D+ Relay to Precharge Relay
  2483. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  2484. {
  2485. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  2486. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  2487. {
  2488. printf("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  2489. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  2490. }
  2491. }
  2492. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  2493. {
  2494. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  2495. }
  2496. break;
  2497. }
  2498. case S_CCS_PRECHARGE_ST1:
  2499. {
  2500. if (isModeChange(gun_index))
  2501. {
  2502. printf("CCS Precharge Processing 2....................%x \n", gun_index);
  2503. }
  2504. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2505. {
  2506. // 板端要求停止
  2507. ChargingTerminalProcess(gun_index);
  2508. }
  2509. // 等待小板通知進入充電
  2510. // 切換 D+ Relay to Precharge Relay
  2511. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  2512. {
  2513. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  2514. setChargerMode(gun_index, MODE_CHARGING);
  2515. }
  2516. break;
  2517. }
  2518. }
  2519. }
  2520. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  2521. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  2522. else
  2523. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  2524. usleep(whileLoopTime);
  2525. }
  2526. return FAIL;
  2527. }