main.c 79 KB

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