main.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769
  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 <stdint.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "Config.h"
  31. #include <stdbool.h>
  32. #include <dirent.h>
  33. #include "timeout.h"
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define BUFFER_SIZE 128
  38. #define YES 1
  39. #define NO 0
  40. #define NORMAL 0
  41. #define ABNORMAL 1
  42. #define EQUAL 0
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define 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. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  52. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  53. int whileLoopTime = 10000; // 10 ms
  54. int wtdFd = -1;
  55. bool IsAuthorizingMode();
  56. void ClearAuthorizedFlag();
  57. bool isDetectPlugin();
  58. void ClearDetectPluginFlag();
  59. int mystrcmp(unsigned char *p1, unsigned char *p2);
  60. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  61. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  62. void ChargingTerminalProcess(byte gunIndex);
  63. void ChkPrimaryStatus();
  64. void StartSystemTimeoutDet(unsigned char flag);
  65. void StopSystemTimeoutDet();
  66. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  67. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  68. int StoreLogMsg_1(const char *fmt, ...);
  69. unsigned long GetTimeoutValue(struct timeval _sour_time);
  70. void gpio_set_value(unsigned int gpio, unsigned int value);
  71. void PRINTF_FUNC(char *string, ...);
  72. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  73. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  74. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  75. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  76. struct StatusCodeData *ShmStatusCodeData;
  77. struct PsuData *ShmPsuData;
  78. struct CHAdeMOData *ShmCHAdeMOData;
  79. struct CcsData *ShmCcsData;
  80. struct PrimaryMcuData *ShmPrimaryMcuData;
  81. struct FanModuleData *ShmFanModuleData;
  82. struct RelayModuleData *ShmRelayModuleData;
  83. struct OCPP16Data *ShmOCPP16Data;
  84. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  85. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  86. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  87. byte _gunCount = 0;
  88. // for initial index to check EV board type is correct
  89. byte _gunIndex = 0;
  90. byte _chademoIndex = 0;
  91. byte _ccsIndex = 0;
  92. byte _gb_Index = 0;
  93. byte bd0_1_status = 0;
  94. byte bd0_2_status = 0;
  95. byte bd1_1_status = 0;
  96. byte bd1_2_status = 0;
  97. bool isCardScan = false;
  98. int rfidFd = -1;
  99. char* rfidPortName = "/dev/ttyS2";
  100. //================================================
  101. // initial can-bus
  102. //================================================
  103. int InitCanBus()
  104. {
  105. int s0,nbytes;
  106. struct timeval tv;
  107. struct ifreq ifr0;
  108. struct sockaddr_can addr0;
  109. system("/sbin/ip link set can0 down");
  110. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  111. system("/sbin/ip link set can0 up");
  112. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  113. tv.tv_sec = 0;
  114. tv.tv_usec = 10000;
  115. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  116. {
  117. #ifdef SystemLogMessage
  118. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  119. #endif
  120. }
  121. nbytes=40960;
  122. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  123. {
  124. #ifdef SystemLogMessage
  125. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  126. #endif
  127. }
  128. nbytes=40960;
  129. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  130. {
  131. #ifdef SystemLogMessage
  132. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  133. #endif
  134. }
  135. strcpy(ifr0.ifr_name, "can0" );
  136. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  137. addr0.can_family = AF_CAN;
  138. addr0.can_ifindex = ifr0.ifr_ifindex;
  139. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  140. return s0;
  141. }
  142. //================================================
  143. // initial uart port
  144. //================================================
  145. char *_priPortName = "/dev/ttyS1";
  146. char *_485PortName = "/dev/ttyS5";
  147. int InitComPort(byte target)
  148. {
  149. int fd;
  150. struct termios tios;
  151. if(target == UPGRADE_PRI)
  152. fd = open(_priPortName, O_RDWR);
  153. else if (target == UPGRADE_FAN || target == UPGRADE_RB)
  154. fd = open(_485PortName, O_RDWR);
  155. if(fd<=0)
  156. {
  157. #ifdef SystemLogMessage
  158. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  159. #endif
  160. return -1;
  161. }
  162. ioctl (fd, TCGETS, &tios);
  163. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  164. tios.c_lflag = 0;
  165. tios.c_iflag = 0;
  166. tios.c_oflag = 0;
  167. tios.c_cc[VMIN]=0;
  168. tios.c_cc[VTIME]=(unsigned char)1;
  169. tios.c_lflag=0;
  170. tcflush(fd, TCIFLUSH);
  171. ioctl (fd, TCSETS, &tios);
  172. return fd;
  173. }
  174. //=================================
  175. // Common routine
  176. //=================================
  177. int InitWatchDog()
  178. {
  179. int fd;
  180. system("/usr/bin/fuser -k /dev/watchdog");
  181. sleep(1);
  182. system("echo V > /dev/watchdog");
  183. sleep(1);
  184. fd=open("/dev/watchdog", O_RDWR);
  185. if(fd<=0)
  186. {
  187. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  188. }
  189. return fd;
  190. }
  191. int StoreLogMsg_1(const char *fmt, ...)
  192. {
  193. char Buf[4096+256];
  194. char buffer[4096];
  195. time_t CurrentTime;
  196. struct tm *tm;
  197. va_list args;
  198. va_start(args, fmt);
  199. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  200. va_end(args);
  201. memset(Buf,0,sizeof(Buf));
  202. CurrentTime = time(NULL);
  203. tm=localtime(&CurrentTime);
  204. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  205. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  206. buffer,
  207. tm->tm_year+1900,tm->tm_mon+1);
  208. system(Buf);
  209. return rc;
  210. }
  211. unsigned long GetTimeoutValue(struct timeval _sour_time)
  212. {
  213. struct timeval _end_time;
  214. gettimeofday(&_end_time, NULL);
  215. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  216. }
  217. int mystrcmp(unsigned char *p1, unsigned char *p2)
  218. {
  219. while(*p1==*p2)
  220. {
  221. if(*p1=='\0' || *p2=='\0')
  222. break;
  223. p1++;
  224. p2++;
  225. }
  226. if(*p1=='\0' && *p2=='\0')
  227. return(PASS);
  228. else
  229. return(FAIL);
  230. }
  231. int DiffTimeb(struct timeb ST, struct timeb ET)
  232. {
  233. //return milli-second
  234. unsigned int StartTime,StopTime;
  235. StartTime=(unsigned int)ST.time;
  236. StopTime=(unsigned int)ET.time;
  237. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  238. return (StopTime-StartTime);
  239. }
  240. bool CheckTimeOut(struct timeb ST)
  241. {
  242. struct timeb ET;
  243. unsigned int StartTime, StopTime;
  244. ftime(&ET);
  245. StartTime = (unsigned int) ST.time;
  246. StopTime = (unsigned int) ET.time;
  247. return (StopTime > StartTime)? YES : NO;
  248. }
  249. void setChargerMode(byte gun_index, byte mode)
  250. {
  251. chargingInfo[gun_index]->SystemStatus = mode;
  252. }
  253. void PRINTF_FUNC(char *string, ...)
  254. {
  255. if (DEBUG)
  256. {
  257. va_list args;
  258. char buffer[4096];
  259. va_start(args, string);
  260. vsnprintf(buffer, sizeof(buffer), string, args);
  261. va_end(args);
  262. printf("%s \n", buffer);
  263. }
  264. }
  265. //==========================================
  266. // Check interface status
  267. //==========================================
  268. int isInterfaceUp(const char *interface)
  269. {
  270. int result = FAIL;
  271. FILE *fp;
  272. char cmd[256];
  273. char buf[512];
  274. strcpy(cmd, "ifconfig");
  275. fp = popen(cmd, "r");
  276. if(fp != NULL)
  277. {
  278. while(fgets(buf, sizeof(buf), fp) != NULL)
  279. {
  280. if(strstr(buf, interface) > 0)
  281. {
  282. result = PASS;
  283. }
  284. }
  285. }
  286. pclose(fp);
  287. return result;
  288. }
  289. //=================================
  290. // Create all share memory
  291. //=================================
  292. int CreateShareMemory()
  293. {
  294. int MeterSMId;
  295. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  296. {
  297. #ifdef SystemLogMessage
  298. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  299. #endif
  300. return 0;
  301. }
  302. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  303. {
  304. #ifdef SystemLogMessage
  305. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  306. #endif
  307. return 0;
  308. }
  309. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  310. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  311. {
  312. #ifdef SystemLogMessage
  313. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  314. #endif
  315. return 0;
  316. }
  317. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  318. {
  319. #ifdef SystemLogMessage
  320. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  321. #endif
  322. return 0;
  323. }
  324. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  325. //creat ShmPsuData
  326. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  327. {
  328. #ifdef SystemLogMessage
  329. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  330. #endif
  331. return 0;
  332. }
  333. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  334. {
  335. #ifdef SystemLogMessage
  336. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  337. #endif
  338. return 0;
  339. }
  340. memset(ShmPsuData, 0, sizeof(struct PsuData));
  341. if(CHAdeMO_QUANTITY > 0)
  342. {
  343. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  344. {
  345. #ifdef SystemLogMessage
  346. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  347. #endif
  348. return 0;
  349. }
  350. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  351. {
  352. #ifdef SystemLogMessage
  353. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  354. #endif
  355. return 0;
  356. }
  357. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  358. }
  359. //creat ShmCcsData
  360. if(CCS_QUANTITY > 0)
  361. {
  362. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  363. {
  364. #ifdef SystemLogMessage
  365. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  366. #endif
  367. return 0;
  368. }
  369. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  370. {
  371. #ifdef SystemLogMessage
  372. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  373. #endif
  374. return 0;
  375. }
  376. memset(ShmCcsData, 0, sizeof(struct CcsData));
  377. }
  378. //creat ShmPrimaryMcuData
  379. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  380. {
  381. #ifdef SystemLogMessage
  382. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  383. #endif
  384. return 0;
  385. }
  386. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  387. {
  388. #ifdef SystemLogMessage
  389. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  390. #endif
  391. return 0;
  392. }
  393. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  394. //creat ShmFanModuleData
  395. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  396. {
  397. #ifdef SystemLogMessage
  398. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  399. #endif
  400. return 0;
  401. }
  402. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  403. {
  404. #ifdef SystemLogMessage
  405. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  406. #endif
  407. return 0;
  408. }
  409. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  410. //creat ShmRelayModuleData
  411. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  412. {
  413. #ifdef SystemLogMessage
  414. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  415. #endif
  416. return 0;
  417. }
  418. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  419. {
  420. #ifdef SystemLogMessage
  421. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  422. #endif
  423. return 0;
  424. }
  425. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  426. //creat ShmOCPP16Data
  427. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  428. {
  429. #ifdef SystemLogMessage
  430. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  431. #endif
  432. return 0;
  433. }
  434. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  435. {
  436. #ifdef SystemLogMessage
  437. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  438. #endif
  439. return 0;
  440. }
  441. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  442. return 1;
  443. }
  444. //=================================
  445. // LCM Page
  446. //=================================
  447. void ChangeLcmByIndex(byte page_index)
  448. {
  449. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  450. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  451. {
  452. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  453. }
  454. }
  455. //======================================================
  456. // Peripheral initial
  457. //======================================================
  458. void InitGPIO()
  459. {
  460. /*****************0~3, 4 bank, bank x 32+ num*********************/
  461. /***************************************************************/
  462. /*************** GPIO 0 ***************************************/
  463. /***************************************************************/
  464. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  465. system("echo 22 > /sys/class/gpio/export");
  466. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  467. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  468. system("echo 23 > /sys/class/gpio/export");
  469. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  470. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  471. system("echo 26 > /sys/class/gpio/export");
  472. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  473. system("echo 1 > /sys/class/gpio/gpio26/value");
  474. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  475. system("echo 27 > /sys/class/gpio/export");
  476. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  477. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  478. system("echo 29 > /sys/class/gpio/export");
  479. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  480. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  481. system("echo 19 > /sys/class/gpio/export");
  482. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  483. system("echo 1 > /sys/class/gpio/gpio19/value");
  484. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  485. system("echo 20 > /sys/class/gpio/export");
  486. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  487. /***************************************************************/
  488. /*************** GPIO 1 ***************************************/
  489. /***************************************************************/
  490. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  491. system("echo 44 > /sys/class/gpio/export");
  492. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  493. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  494. system("echo 45 > /sys/class/gpio/export");
  495. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  496. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  497. system("echo 46 > /sys/class/gpio/export");
  498. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  499. system("echo 0 > /sys/class/gpio/gpio46/value");
  500. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  501. system("echo 47 > /sys/class/gpio/export");
  502. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  503. /***************************************************************/
  504. /*************** GPIO 2 ***************************************/
  505. /***************************************************************/
  506. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  507. system("echo 89 > /sys/class/gpio/export");
  508. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  509. system("echo 1 > /sys/class/gpio/gpio89/value");
  510. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  511. system("echo 87 > /sys/class/gpio/export");
  512. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  513. system("echo 0 > /sys/class/gpio/gpio87/value");
  514. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  515. system("echo 88 > /sys/class/gpio/export");
  516. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  517. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  518. system("echo 86 > /sys/class/gpio/export");
  519. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  520. /***************************************************************/
  521. /*************** GPIO 3 ***************************************/
  522. /***************************************************************/
  523. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  524. system("echo 111 > /sys/class/gpio/export");
  525. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  526. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  527. system("echo 114 > /sys/class/gpio/export");
  528. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  529. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  530. system("echo 113 > /sys/class/gpio/export");
  531. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  532. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  533. system("echo 110 > /sys/class/gpio/export");
  534. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  535. system("echo 0 > /sys/class/gpio/gpio110/value");
  536. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  537. system("echo 115 > /sys/class/gpio/export");
  538. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  539. system("echo 0 > /sys/class/gpio/gpio115/value");
  540. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  541. system("echo 112 > /sys/class/gpio/export");
  542. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  543. system("echo 1 > /sys/class/gpio/gpio112/value");
  544. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  545. system("echo 116 > /sys/class/gpio/export");
  546. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  547. system("echo 0 > /sys/class/gpio/gpio116/value");
  548. #ifdef SystemLogMessage
  549. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  550. #endif
  551. }
  552. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  553. {
  554. int fd,wrd;
  555. unsigned char *buf;
  556. unsigned int ChkSum,ChkSumOrg;
  557. if((buf=malloc(MtdBlockSize))==NULL)
  558. {
  559. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  560. if(ShmStatusCodeData!=NULL)
  561. {
  562. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  563. }
  564. sleep(5);
  565. system("reboot -f");
  566. sleep(5);
  567. system("reboot -f");
  568. }
  569. memset(buf, 0, MtdBlockSize);
  570. //================================================
  571. // Load configuration from mtdblock10
  572. //================================================
  573. fd = open("/dev/mtdblock10", O_RDWR);
  574. if (fd < 0)
  575. {
  576. free(buf);
  577. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  578. if(ShmStatusCodeData!=NULL)
  579. {
  580. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  581. }
  582. sleep(5);
  583. system("reboot -f");
  584. sleep(5);
  585. system("reboot -f");
  586. }
  587. wrd=read(fd, buf, MtdBlockSize);
  588. close(fd);
  589. if(wrd<MtdBlockSize)
  590. {
  591. free(buf);
  592. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  593. if(ShmStatusCodeData!=NULL)
  594. {
  595. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  596. }
  597. sleep(5);
  598. system("reboot -f");
  599. sleep(5);
  600. system("reboot -f");
  601. }
  602. ChkSum=0;
  603. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  604. {
  605. ChkSum+=buf[wrd];
  606. }
  607. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  608. //================================================
  609. // Load configuration from mtdblock11
  610. //================================================
  611. if(ChkSum!=ChkSumOrg)
  612. {
  613. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  614. fd = open("/dev/mtdblock11", O_RDWR);
  615. if (fd < 0)
  616. {
  617. free(buf);
  618. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  619. if(ShmStatusCodeData!=NULL)
  620. {
  621. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  622. }
  623. sleep(5);
  624. system("reboot -f");
  625. sleep(5);
  626. system("reboot -f");
  627. }
  628. memset(buf, 0, MtdBlockSize);
  629. wrd=read(fd, buf,MtdBlockSize);
  630. close(fd);
  631. if(wrd<MtdBlockSize)
  632. {
  633. free(buf);
  634. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  635. if(ShmStatusCodeData!=NULL)
  636. {
  637. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  638. }
  639. sleep(5);
  640. system("reboot -f");
  641. sleep(5);
  642. system("reboot -f");
  643. }
  644. ChkSum=0;
  645. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  646. {
  647. ChkSum+=buf[wrd];
  648. }
  649. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  650. //================================================
  651. // Load configuration from mtdblock12 (Factory default)
  652. //================================================
  653. if(ChkSum!=ChkSumOrg)
  654. {
  655. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  656. fd = open("/dev/mtdblock12", O_RDWR);
  657. if (fd < 0)
  658. {
  659. free(buf);
  660. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  661. if(ShmStatusCodeData!=NULL)
  662. {
  663. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  664. }
  665. sleep(5);
  666. system("reboot -f");
  667. sleep(5);
  668. system("reboot -f");
  669. }
  670. memset(buf, 0, MtdBlockSize);
  671. wrd=read(fd, buf,MtdBlockSize);
  672. close(fd);
  673. if(wrd<MtdBlockSize)
  674. {
  675. free(buf);
  676. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  677. if(ShmStatusCodeData!=NULL)
  678. {
  679. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  680. }
  681. sleep(5);
  682. system("reboot -f");
  683. sleep(5);
  684. system("reboot -f");
  685. }
  686. ChkSum=0;
  687. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  688. {
  689. ChkSum+=buf[wrd];
  690. }
  691. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  692. if(ChkSum!=ChkSumOrg)
  693. {
  694. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  695. free(buf);
  696. system("cd /root;./FactoryConfig -m");
  697. system("sync");
  698. sleep(5);
  699. system("reboot -f");
  700. sleep(5);
  701. system("reboot -f");
  702. return FAIL;
  703. }
  704. }
  705. }
  706. //load OK
  707. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  708. free(buf);
  709. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  710. return PASS;
  711. }
  712. int isReachableInternet()
  713. {
  714. int result = FAIL;
  715. FILE *fp;
  716. char cmd[256];
  717. char buf[512];
  718. //char tmp[512];
  719. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  720. {
  721. strcpy(cmd, "ping -c 1 -w 3 ");
  722. strcat(cmd, valid_Internet[idx]);
  723. fp = popen(cmd, "r");
  724. if(fp != NULL)
  725. {
  726. while(fgets(buf, sizeof(buf), fp) != NULL)
  727. {
  728. if(strstr(buf, "transmitted") > 0)
  729. {
  730. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  731. if(strstr(buf,"100%") != NULL)
  732. {
  733. }
  734. else
  735. {
  736. result = PASS;
  737. }
  738. //DEBUG_INFO("%s",buf);
  739. //DEBUG_INFO("%s\n",tmp);
  740. }
  741. }
  742. }
  743. pclose(fp);
  744. }
  745. return result;
  746. }
  747. void InitEthernet()
  748. {
  749. char tmpbuf[256];
  750. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  751. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  752. sleep(2);
  753. //Init Eth0 for internet
  754. memset(tmpbuf,0,256);
  755. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  756. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  757. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  758. system(tmpbuf);
  759. memset(tmpbuf,0,256);
  760. sprintf(tmpbuf,"route add default gw %s eth0 ",
  761. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  762. system(tmpbuf);
  763. //Init Eth1 for administrator tool
  764. memset(tmpbuf,0,256);
  765. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  766. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  767. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  768. system(tmpbuf);
  769. //Run DHCP client if enabled
  770. system("killall udhcpc");
  771. system("rm -rf /etc/resolv.conf");
  772. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  773. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  774. if(ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient == 0)
  775. system("/sbin/udhcpc -i eth0 -s /root/dhcp_script/eth0.script > /dev/null &");
  776. pid_t pid = fork();
  777. uint8_t cnt_pingDNS_Fail;
  778. if(pid == 0)
  779. {
  780. for(;;)
  781. {
  782. if(isReachableInternet() == PASS)
  783. {
  784. ShmSysConfigAndInfo->SysInfo.InternetConn = YES;
  785. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= NO;
  786. cnt_pingDNS_Fail = 0;
  787. }
  788. else
  789. {
  790. if(++cnt_pingDNS_Fail > 3)
  791. {
  792. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  793. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= YES;
  794. }
  795. }
  796. sleep(5);
  797. }
  798. }
  799. #ifdef SystemLogMessage
  800. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK");
  801. #endif
  802. }
  803. int InitialRfidPort()
  804. {
  805. int uartO2 = open(rfidPortName, O_RDWR);
  806. struct termios tios;
  807. if (uartO2 != FAIL)
  808. {
  809. ioctl (uartO2, TCGETS, &tios);
  810. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  811. tios.c_lflag = 0;
  812. tios.c_iflag = 0;
  813. tios.c_oflag = 0;
  814. tios.c_cc[VMIN] = 0;
  815. tios.c_cc[VTIME] = (unsigned char) 1;
  816. tios.c_lflag = 0;
  817. tcflush(uartO2, TCIFLUSH);
  818. ioctl(uartO2, TCSETS, &tios);
  819. }
  820. if (uartO2 < 0)
  821. {
  822. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  823. }
  824. return uartO2;
  825. }
  826. void InitialShareMemoryInfo()
  827. {
  828. FILE *fp;
  829. char cmd[512];
  830. char buf[512];
  831. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.01.04.20.02.00");
  832. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  833. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  834. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  835. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  836. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  837. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  838. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  839. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  840. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  841. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  842. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  843. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  844. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  845. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  846. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  847. sprintf(cmd, "/bin/uname -r");
  848. fp = popen(cmd, "r");
  849. if(fp == NULL)
  850. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  851. else
  852. {
  853. while(fgets(buf, sizeof(buf), fp) != NULL)
  854. {
  855. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  856. }
  857. }
  858. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.01.44.0030");
  859. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  860. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  861. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  862. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  863. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  864. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  865. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  866. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  867. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  868. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  869. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  870. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  871. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  872. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  873. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  874. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  875. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  876. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  877. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  878. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  879. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  880. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  881. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  882. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  883. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  884. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  885. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  886. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  887. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  888. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  889. ShmPrimaryMcuData->SelfTest_Comp = NO;
  890. ShmRelayModuleData->SelfTest_Comp = NO;
  891. ShmFanModuleData->SelfTest_Comp = NO;
  892. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  893. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  894. ShmFanModuleData->TestFanSpeed = 0;
  895. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  896. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  897. }
  898. int Initialization()
  899. {
  900. // 初始化卡號驗證的 Flag
  901. ClearAuthorizedFlag();
  902. // 初始化插槍驗證的 Flag
  903. ClearDetectPluginFlag();
  904. // UART 2 for Rfid
  905. rfidFd = InitialRfidPort();
  906. int pinOut[2] = { 115, 116 };
  907. for (byte count = 0; count < _gunCount; count++)
  908. {
  909. if (chargingInfo[count]->Type == _Type_Chademo)
  910. {
  911. gpio_set_value(pinOut[count], 0x01);
  912. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  913. }
  914. else if (chargingInfo[count]->Type == _Type_CCS_2)
  915. {
  916. if (ShmCcsData->CommProtocol == 0x01)
  917. {
  918. if (_gunCount == 1)
  919. gpio_set_value(pinOut[1], 0x00);
  920. else
  921. gpio_set_value(pinOut[count], 0x00);
  922. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  923. }
  924. }
  925. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  926. }
  927. PRINTF_FUNC("Initialization OK \n");
  928. return PASS;
  929. }
  930. bool InitialSystemDefaultConfig()
  931. {
  932. bool result = true;
  933. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  934. InitGPIO();
  935. //InitEthernet();
  936. if (DEBUG == NO)
  937. {
  938. wtdFd = InitWatchDog();
  939. if (wtdFd < 0)
  940. result = false;
  941. }
  942. system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  943. sleep(3);
  944. system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  945. sleep(1);
  946. system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
  947. return result;
  948. }
  949. void DisplaySelfTestFailReason()
  950. {
  951. // RB、FB、407、EV 小板中有些板子無回應
  952. if (ShmRelayModuleData->SelfTest_Comp == NO)
  953. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; }
  954. if (ShmFanModuleData->SelfTest_Comp == NO)
  955. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; }
  956. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  957. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; }
  958. for (byte index = 0; index < _gunCount; index++)
  959. {
  960. if (chargingInfo[index]->Type == _Type_Chademo)
  961. {
  962. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  963. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; }
  964. }
  965. else if (chargingInfo[index]->Type == _Type_CCS_2)
  966. {
  967. if (ShmCcsData->CommProtocol == 0x01)
  968. {
  969. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  970. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; }
  971. }
  972. }
  973. }
  974. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  975. {
  976. // AC Contact 未搭上
  977. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  978. }
  979. else
  980. {
  981. // PSU 通訊問題
  982. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  983. }
  984. }
  985. void SelfTestRun()
  986. {
  987. bool evInitFlag = false;
  988. StartSystemTimeoutDet(Timeout_SelftestChk);
  989. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  990. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  991. {
  992. ChkPrimaryStatus();
  993. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  994. {
  995. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  996. return;
  997. }
  998. if (_gunCount > 0)
  999. {
  1000. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1001. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1002. {
  1003. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1004. return;
  1005. }
  1006. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1007. {
  1008. case _STEST_VERSION:
  1009. {
  1010. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  1011. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  1012. {
  1013. //PRINTF_FUNC("RB pass \n");
  1014. ShmRelayModuleData->SelfTest_Comp = YES;
  1015. }
  1016. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  1017. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  1018. {
  1019. //PRINTF_FUNC("Fan pass \n");
  1020. ShmFanModuleData->SelfTest_Comp = YES;
  1021. }
  1022. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  1023. ShmPrimaryMcuData->version[0] != '\0')
  1024. {
  1025. //PRINTF_FUNC("407 pass \n");
  1026. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1027. }
  1028. // EV 小板
  1029. if (!evInitFlag)
  1030. {
  1031. evInitFlag = YES;
  1032. for (byte index = 0; index < _gunCount; index++)
  1033. {
  1034. if (chargingInfo[index]->Type == _Type_Chademo)
  1035. {
  1036. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1037. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1038. {
  1039. //PRINTF_FUNC("chademo pass \n");
  1040. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1041. }
  1042. else
  1043. {
  1044. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1045. evInitFlag = NO;
  1046. }
  1047. }
  1048. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1049. {
  1050. if (ShmCcsData->CommProtocol == 0x01)
  1051. {
  1052. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  1053. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  1054. {
  1055. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1056. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1057. }
  1058. else
  1059. {
  1060. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1061. evInitFlag = NO;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. }
  1067. if (ShmFanModuleData->SelfTest_Comp &&
  1068. ShmRelayModuleData->SelfTest_Comp &&
  1069. ShmPrimaryMcuData->SelfTest_Comp &&
  1070. evInitFlag)
  1071. {
  1072. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1073. }
  1074. }
  1075. break;
  1076. case _STEST_AC_CONTACTOR:
  1077. {
  1078. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1079. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1080. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1081. {
  1082. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1083. PRINTF_FUNC("Communication board pass. \n");
  1084. }
  1085. }
  1086. break;
  1087. case _STEST_PSU_DETECT:
  1088. {
  1089. // 此測試主要測試 PSU 對應是否為正確的火線上電壓
  1090. // 如果沒有 PSU 模組請 bypass
  1091. if (ShmPsuData->Work_Step == _TEST_POWER_STEP || ShmPsuData->Work_Step == _TEST_COMPLETE)
  1092. {
  1093. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1094. }
  1095. }
  1096. break;
  1097. case _STEST_PSU_CAP:
  1098. {
  1099. // 此測試是要確認當前總輸出能力
  1100. // 如果沒有 PSU 模組請 bypass
  1101. if (ShmPsuData->Work_Step == _TEST_COMPLETE)
  1102. {
  1103. sleep(1);
  1104. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1105. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1106. }
  1107. }
  1108. break;
  1109. }
  1110. }
  1111. else
  1112. break;
  1113. usleep(100000);
  1114. }
  1115. }
  1116. int SpawnTask()
  1117. {
  1118. system("/root/Module_EventLogging &");
  1119. system("/root/Module_PrimaryComm &");
  1120. system("/root/Module_EvComm &");
  1121. system("/root/Module_LcmControl &");
  1122. system("/root/Module_InternalComm &");
  1123. system("/root/Module_PsuComm &");
  1124. system("/root/OcppBackend &");
  1125. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1126. {
  1127. system("/root/Module_4g &");
  1128. }
  1129. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1130. {
  1131. system("/root/Module_Wifi &");
  1132. }
  1133. // 加入參數
  1134. // char str[64];
  1135. // memset(str, '\0', sizeof(65));
  1136. // sprintf(str, "/root/Module_EvComm %x &", (CHAdeMO_QUANTITY + CCS_QUANTITY));
  1137. // PRINTF_FUNC("%s \n", str);
  1138. // system(str);
  1139. return PASS;
  1140. }
  1141. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1142. {
  1143. int result = PASS;
  1144. int fd,wrd;
  1145. unsigned int i,Chk;
  1146. unsigned char *ptr, *BufTmp;
  1147. Chk=0;
  1148. ptr=(unsigned char *)UsrData;
  1149. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  1150. {
  1151. memset(BufTmp, 0, MtdBlockSize);
  1152. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1153. for(i=0; i<MtdBlockSize-4; i++)
  1154. Chk+=*(ptr+i);
  1155. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  1156. fd = open("/dev/mtdblock10", O_RDWR);
  1157. if (fd > 0)
  1158. {
  1159. wrd=write(fd, BufTmp, MtdBlockSize);
  1160. close(fd);
  1161. if(wrd >= MtdBlockSize)
  1162. {
  1163. fd = open("/dev/mtdblock11", O_RDWR);
  1164. if (fd > 0)
  1165. {
  1166. wrd=write(fd, BufTmp, MtdBlockSize);
  1167. close(fd);
  1168. if(wrd < MtdBlockSize)
  1169. {
  1170. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  1171. result = FAIL;
  1172. }
  1173. }
  1174. else
  1175. {
  1176. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  1177. result = FAIL;
  1178. }
  1179. }
  1180. else
  1181. {
  1182. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  1183. result = FAIL;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  1189. result = FAIL;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  1195. result = FAIL;
  1196. }
  1197. if(BufTmp != NULL)
  1198. free(BufTmp);
  1199. return result;
  1200. }
  1201. //===============================================
  1202. // Common Detect Chk - Stop Charging ?
  1203. //===============================================
  1204. bool isEvBoardStopChargeFlag(byte gunIndex)
  1205. {
  1206. return chargingInfo[gunIndex]->StopChargeFlag;
  1207. }
  1208. //===============================================
  1209. // 掃描插槍狀況
  1210. //===============================================
  1211. void ClearDetectPluginFlag()
  1212. {
  1213. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1214. }
  1215. void DetectPluginStart()
  1216. {
  1217. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1218. }
  1219. bool isDetectPlugin()
  1220. {
  1221. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1222. return YES;
  1223. return NO;
  1224. }
  1225. //===============================================
  1226. // Common Detect Chk - Chademo
  1227. //===============================================
  1228. bool isEvGunLocked_chademo(byte gunIndex)
  1229. {
  1230. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1231. }
  1232. bool isEvContactorWelding_chademo(byte gunIndex)
  1233. {
  1234. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1235. }
  1236. bool isEvStopReq_chademo(byte gunIndex)
  1237. {
  1238. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1239. }
  1240. bool isEvStopCharging_chademo(byte gunIndex)
  1241. {
  1242. if (isEvGunLocked_chademo(gunIndex) == NO)
  1243. {
  1244. // 無鎖槍 = 停止
  1245. PRINTF_FUNC("gun locked none. \n");
  1246. return YES;
  1247. }
  1248. return NO;
  1249. }
  1250. byte isPrechargeStatus_chademo(byte gunIndex)
  1251. {
  1252. byte result = 0x00;
  1253. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1254. return result;
  1255. }
  1256. //===============================================
  1257. // Common Detect Chk - CCS
  1258. //===============================================
  1259. bool isEvGunLocked_ccs(byte gunIndex)
  1260. {
  1261. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1262. }
  1263. byte isPrechargeStatus_ccs(byte gunIndex)
  1264. {
  1265. byte result = 0x00;
  1266. if (ShmCcsData->CommProtocol == 0x01)
  1267. {
  1268. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1269. }
  1270. return result;
  1271. }
  1272. bool isEvStopCharging_ccs(byte gunIndex)
  1273. {
  1274. if (isEvGunLocked_ccs(gunIndex) == NO)
  1275. {
  1276. // 無鎖槍 = 停止
  1277. PRINTF_FUNC("gun locked none. \n");
  1278. return YES;
  1279. }
  1280. return NO;
  1281. }
  1282. //===============================================
  1283. // Callback
  1284. //===============================================
  1285. void DisplayChargingInfo()
  1286. {
  1287. for (byte i = _gunCount - 1; i >= 0; i--)
  1288. {
  1289. if (chargingInfo[i]->SystemStatus != S_IDLE)
  1290. {
  1291. ShmSysConfigAndInfo->SysInfo.CurGunSelected = i;
  1292. break;
  1293. }
  1294. }
  1295. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1296. }
  1297. void _AutoReturnTimeout()
  1298. {
  1299. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1300. {
  1301. ClearDetectPluginFlag();
  1302. }
  1303. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1304. {
  1305. DetectPluginStart();
  1306. }
  1307. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1308. //
  1309. // for (byte i = 0; i < CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY; i++)
  1310. // {
  1311. // if (chargingInfo[i]->SystemStatus > S_IDLE)
  1312. // {
  1313. // switch(chargingInfo[i]->SystemStatus)
  1314. // {
  1315. // case S_PREPARNING:
  1316. // case S_PREPARING_FOR_EV:
  1317. // case S_PREPARING_FOR_EVSE:
  1318. // {
  1319. // ChangeLcmByIndex(i, _LCM_PRE_CHARGE);
  1320. // }
  1321. // break;
  1322. // case S_CHARGING:
  1323. // {
  1324. // ChangeLcmByIndex(i, _LCM_CHARGING);
  1325. // }
  1326. // break;
  1327. // case S_TERMINATING:
  1328. // {
  1329. // ChangeLcmByIndex(i, _LCM_COMPLETE);
  1330. // }
  1331. // break;
  1332. // }
  1333. // return;
  1334. // }
  1335. // }
  1336. // if (!IsAuthorizingMode())
  1337. // ChangeLcmByIndex(255, _LCM_IDLE);
  1338. }
  1339. void _SelfTestTimeout()
  1340. {
  1341. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1342. {
  1343. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  1344. {
  1345. setChargerMode(gun_index, MODE_ALARM);
  1346. }
  1347. }
  1348. ShmPsuData->Work_Step = _NO_WORKING;
  1349. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1350. PRINTF_FUNC("Self test timeout. \n");
  1351. }
  1352. void _AuthorizedTimeout()
  1353. {
  1354. if(IsAuthorizingMode())
  1355. {
  1356. PRINTF_FUNC("_AuthorizedTimeout \n");
  1357. isCardScan = false;
  1358. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1359. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1360. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1361. ClearAuthorizedFlag();
  1362. }
  1363. }
  1364. void _DetectPlugInTimeout()
  1365. {
  1366. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1367. PRINTF_FUNC("_DetectPlugInTimeout \n");
  1368. ClearDetectPluginFlag();
  1369. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1370. }
  1371. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1372. {
  1373. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1374. {
  1375. if(!isEvGunLocked_chademo(gunIndex))
  1376. {
  1377. PRINTF_FUNC("_DetectEvChargingEnableTimeout (chademo) \n");
  1378. ChargingTerminalProcess(gunIndex);
  1379. _AutoReturnTimeout();
  1380. }
  1381. }
  1382. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1383. {
  1384. if(!isEvGunLocked_ccs(gunIndex))
  1385. {
  1386. PRINTF_FUNC("_DetectEvChargingEnableTimeout (ccs) \n");
  1387. ChargingTerminalProcess(gunIndex);
  1388. _AutoReturnTimeout();
  1389. }
  1390. }
  1391. }
  1392. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1393. {
  1394. PRINTF_FUNC("_DetectEvseChargingEnableTimeout (GFD timeout) \n");
  1395. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1396. {
  1397. setChargerMode(gunIndex, MODE_IDLE);
  1398. _AutoReturnTimeout();
  1399. }
  1400. }
  1401. void _PrepareTimeout(byte gunIndex)
  1402. {
  1403. PRINTF_FUNC("_PrechargeTimeout \n");
  1404. setChargerMode(gunIndex, MODE_IDLE);
  1405. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1406. _AutoReturnTimeout();
  1407. }
  1408. void _CompleteTimeout(byte gunIndex)
  1409. {
  1410. PRINTF_FUNC("_CompleteTimeout ====> %d \n", gunIndex);
  1411. setChargerMode(gunIndex, MODE_IDLE);
  1412. }
  1413. void _CcsPrechargeTimeout(byte gunIndex)
  1414. {
  1415. PRINTF_FUNC("_CcsPrechargeTimeout \n");
  1416. setChargerMode(gunIndex, MODE_IDLE);
  1417. }
  1418. //===============================================
  1419. // 取得卡號與卡號驗證
  1420. //===============================================
  1421. bool canStartCharging()
  1422. {
  1423. char buf2[16] = "";
  1424. memset(buf2, 0, ARRAY_SIZE(buf2));
  1425. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1426. {
  1427. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1428. }
  1429. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1430. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1431. if(strcmp(buf2, "Accepted") == EQUAL)
  1432. return true;
  1433. else
  1434. {
  1435. }
  1436. return false;
  1437. }
  1438. void AuthorizingStart()
  1439. {
  1440. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1441. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1442. }
  1443. void ClearAuthorizedFlag()
  1444. {
  1445. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1446. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1447. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1448. }
  1449. bool isAuthorizedComplete()
  1450. {
  1451. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1452. return false;
  1453. return true;
  1454. }
  1455. bool IsAuthorizingMode()
  1456. {
  1457. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  1458. return false;
  1459. return true;
  1460. }
  1461. //===============================================
  1462. // 紀錄 Alarm Code
  1463. //===============================================
  1464. void RecordAlarmCode(char *code)
  1465. {
  1466. if (strcmp(code, "012234") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  1467. if (strcmp(code, "012235") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  1468. }
  1469. void ReleaseAlarmCode(byte gunIndex)
  1470. {
  1471. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1472. {
  1473. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  1474. }
  1475. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1476. {
  1477. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  1478. }
  1479. }
  1480. //===============================================
  1481. // EmergencyStop and Charging Stop
  1482. //===============================================
  1483. void ChargingTerminalProcess(byte gunIndex)
  1484. {
  1485. setChargerMode(gunIndex, MODE_TERMINATING);
  1486. }
  1487. void StopChargingProcessByString(byte level, byte gun_index, char *string)
  1488. {
  1489. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  1490. {
  1491. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  1492. }
  1493. }
  1494. void ReleaseChargingProcessByString(byte level, char *code)
  1495. {
  1496. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  1497. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  1498. }
  1499. // 一般錯誤停止充電處理函式
  1500. void BoardErrOccurByString(byte index, char *code)
  1501. {
  1502. byte level = 1;
  1503. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1504. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1505. {
  1506. ChargingTerminalProcess(index);
  1507. }
  1508. StopChargingProcessByString(level, index, code);
  1509. }
  1510. // 急停狀況的停止充電處理函式
  1511. void EmcOccureByString(char *code)
  1512. {
  1513. byte level = 2;
  1514. // 嚴重的急停有四種 : EMC 按鈕、Mainbreak、Dooropen、SPD Trip
  1515. // 其錯誤等級為 2
  1516. if (strncmp(code, "012251", 6) == 0 || strncmp(code, "012252", 6) == 0 ||
  1517. strncmp(code, "012237", 6) == 0 || strncmp(code, "012238", 6) == 0)
  1518. {
  1519. for (byte gun = 0; gun < _gunCount; gun++)
  1520. {
  1521. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1522. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1523. {
  1524. ChargingTerminalProcess(gun);
  1525. }
  1526. StopChargingProcessByString(level, gun, code);
  1527. }
  1528. }
  1529. }
  1530. void ReleaseBoardErrOccurByString(byte index, char *code)
  1531. {
  1532. bool isTrigger = false;
  1533. byte level = 1;
  1534. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1535. {
  1536. isTrigger = true;
  1537. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1538. }
  1539. if (isTrigger)
  1540. {
  1541. ReleaseChargingProcessByString(level, code);
  1542. }
  1543. }
  1544. void ReleaseEmsOccureByString(byte index, char *code)
  1545. {
  1546. bool isTrigger = false;
  1547. byte level = 2;
  1548. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1549. {
  1550. isTrigger = true;
  1551. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1552. }
  1553. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  1554. {
  1555. isTrigger = true;
  1556. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  1557. }
  1558. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  1559. {
  1560. isTrigger = true;
  1561. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  1562. }
  1563. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  1564. {
  1565. isTrigger = true;
  1566. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1567. }
  1568. if (isTrigger)
  1569. {
  1570. ReleaseChargingProcessByString(level, code);
  1571. }
  1572. }
  1573. //===============================================
  1574. // 確認硬體 (按鈕) 狀態
  1575. //===============================================
  1576. bool leftBtnPush = false;
  1577. bool rightBtnPush = false;
  1578. void ChkPrimaryStatus()
  1579. {
  1580. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  1581. {
  1582. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  1583. EmcOccureByString("012251");
  1584. }
  1585. else
  1586. ReleaseEmsOccureByString(0, "012251");
  1587. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  1588. {
  1589. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  1590. EmcOccureByString("012238");
  1591. }
  1592. else
  1593. ReleaseEmsOccureByString(0, "012238");
  1594. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  1595. {
  1596. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  1597. EmcOccureByString("012237");
  1598. }
  1599. else
  1600. ReleaseEmsOccureByString(0, "012237");
  1601. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  1602. {
  1603. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  1604. EmcOccureByString("012252");
  1605. }
  1606. else
  1607. ReleaseEmsOccureByString(0, "012252");
  1608. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  1609. {
  1610. if(!leftBtnPush)
  1611. {
  1612. leftBtnPush = true;
  1613. PRINTF_FUNC("left btn down............................... \n");
  1614. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < _gunCount)
  1615. ShmSysConfigAndInfo->SysInfo.CurGunSelected += 1;
  1616. else
  1617. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1618. }
  1619. // 左邊的選槍按鈕,只有在雙槍都在充電時候才有用 : 30KW 以下該按鈕無作用
  1620. }
  1621. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  1622. {
  1623. if(leftBtnPush)
  1624. {
  1625. leftBtnPush = false;
  1626. PRINTF_FUNC("left btn up............................... \n");
  1627. }
  1628. }
  1629. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  1630. {
  1631. if(!rightBtnPush)
  1632. {
  1633. rightBtnPush = true;
  1634. PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1635. if (_gunCount > 1)
  1636. {
  1637. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1638. {
  1639. case S_IDLE:
  1640. {
  1641. //ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  1642. }
  1643. break;
  1644. case S_PREPARNING:
  1645. case S_PREPARING_FOR_EV:
  1646. case S_PREPARING_FOR_EVSE:
  1647. case S_CCS_PRECHARGE_ST0:
  1648. case S_CCS_PRECHARGE_ST1:
  1649. case S_CHARGING:
  1650. {
  1651. // 停止充電
  1652. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1653. }
  1654. break;
  1655. case S_COMPLETE:
  1656. {
  1657. // 回 IDLE
  1658. PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  1659. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  1660. }
  1661. break;
  1662. }
  1663. }
  1664. }
  1665. // 右邊的按鈕,只作用在當前頁面,當前頁面如果在可以回 Home 與 停止充電的狀態為可用
  1666. // switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1667. // {
  1668. // case S_IDLE: {}break;
  1669. // case S_AUTHORIZING: {}break;
  1670. // case S_PREPARING_FOR_EV: {}break;
  1671. // case S_PREPARING_FOR_EVSE: {}break;
  1672. // case S_CCS_PRECHARGE_ST0: {}break;
  1673. // case S_CCS_PRECHARGE_ST1: {}break;
  1674. // case S_CHARGING:
  1675. // {
  1676. // // 停止充電
  1677. // PRINTF_FUNC("Stop --------------------------------------------------- \n");
  1678. // //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1679. // }
  1680. // break;
  1681. // case S_COMPLETE:
  1682. // {
  1683. // //setChargerMode(ShmSysConfigAndInfo->SysInfo.CurGunSelected, MODE_IDLE);
  1684. // }
  1685. // break;
  1686. // }
  1687. }
  1688. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  1689. {
  1690. if(rightBtnPush)
  1691. {
  1692. rightBtnPush = false;
  1693. PRINTF_FUNC("right btn up............................... \n");
  1694. }
  1695. }
  1696. }
  1697. //===============================================
  1698. // 確認各小板偵測的錯誤狀況
  1699. //===============================================
  1700. void CheckErrorOccurStatus(byte index)
  1701. {
  1702. // 小板
  1703. if (chargingInfo[index]->Type == _Type_Chademo)
  1704. {
  1705. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  1706. BoardErrOccurByString(index, "011012");
  1707. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1708. BoardErrOccurByString(index, "012234");
  1709. }
  1710. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1711. {
  1712. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  1713. BoardErrOccurByString(index, "011014");
  1714. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1715. BoardErrOccurByString(index, "012235");
  1716. }
  1717. }
  1718. //===============================================
  1719. // 確認 GPIO 狀態
  1720. //===============================================
  1721. void gpio_set_value(unsigned int gpio, unsigned int value)
  1722. {
  1723. int fd;
  1724. char buf[MAX_BUF];
  1725. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1726. fd = open(buf, O_WRONLY);
  1727. if (fd < 0)
  1728. {
  1729. perror("gpio/set-value");
  1730. return;
  1731. }
  1732. if (value)
  1733. write(fd, "1", 2);
  1734. else
  1735. write(fd, "0", 2);
  1736. close(fd);
  1737. }
  1738. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1739. {
  1740. int fd;
  1741. char buf[MAX_BUF];
  1742. char ch;
  1743. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1744. fd = open(buf, O_RDONLY);
  1745. if (fd < 0) {
  1746. perror("gpio/get-value");
  1747. return fd;
  1748. }
  1749. read(fd, &ch, 1);
  1750. if (ch != '0') {
  1751. *value = 1;
  1752. } else {
  1753. *value = 0;
  1754. }
  1755. close(fd);
  1756. return 0;
  1757. }
  1758. void CheckGunTypeFromHw()
  1759. {
  1760. int pinIn[4] = { 22, 23, 44, 45 };
  1761. unsigned int gpioValue = 0;
  1762. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1763. gpio_get_value(pinIn[i], &gpioValue);
  1764. {
  1765. switch (pinIn[i])
  1766. {
  1767. case 22:
  1768. bd1_1_status = gpioValue;
  1769. break;
  1770. case 23:
  1771. bd1_2_status = gpioValue;
  1772. break;
  1773. case 44:
  1774. bd0_1_status = gpioValue;
  1775. break;
  1776. case 45:
  1777. bd0_2_status = gpioValue;
  1778. break;
  1779. }
  1780. }
  1781. }
  1782. }
  1783. void CheckGpioInStatus()
  1784. {
  1785. int pinIn[2] = { 27, 47 };
  1786. unsigned int gpioValue = 0;
  1787. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  1788. {
  1789. gpio_get_value(pinIn[i], &gpioValue);
  1790. if (gpioValue == 0x01)
  1791. {
  1792. switch(pinIn[i])
  1793. {
  1794. // 小板緊急停止
  1795. case 47:
  1796. {
  1797. for(int i = 0; i < _gunCount; i++)
  1798. {
  1799. if (chargingInfo[i]->slotsIndex == 1)
  1800. {
  1801. if (chargingInfo[i]->Type == _Type_Chademo)
  1802. BoardErrOccurByString(i, "023730");
  1803. else if (chargingInfo[i]->Type == _Type_CCS_2)
  1804. BoardErrOccurByString(i, "013627");
  1805. break;
  1806. }
  1807. }
  1808. }
  1809. break;
  1810. case 27:
  1811. {
  1812. for(int i = 0; i < _gunCount; i++)
  1813. {
  1814. if (chargingInfo[i]->slotsIndex == 3)
  1815. {
  1816. if (chargingInfo[i]->Type == _Type_Chademo)
  1817. BoardErrOccurByString(i, "023730");
  1818. else if (chargingInfo[i]->Type == _Type_CCS_2)
  1819. BoardErrOccurByString(i, "013627");
  1820. break;
  1821. }
  1822. }
  1823. }
  1824. break;
  1825. }
  1826. }
  1827. else
  1828. {
  1829. switch (pinIn[i])
  1830. {
  1831. // 小板解除緊急停止
  1832. case 47:
  1833. {
  1834. for(int i = 0; i < _gunCount; i++)
  1835. {
  1836. if (chargingInfo[i]->slotsIndex == 1)
  1837. {
  1838. if (chargingInfo[i]->Type == _Type_Chademo)
  1839. ReleaseEmsOccureByString(i, "023730");
  1840. else if (chargingInfo[i]->Type == _Type_CCS_2)
  1841. ReleaseEmsOccureByString(i, "013627");
  1842. break;
  1843. }
  1844. }
  1845. }
  1846. break;
  1847. case 27:
  1848. {
  1849. for (int i = 0; i < _gunCount; i++)
  1850. {
  1851. if (chargingInfo[i]->slotsIndex == 3)
  1852. {
  1853. if (chargingInfo[i]->Type == _Type_Chademo)
  1854. ReleaseEmsOccureByString(i, "023730");
  1855. else if (chargingInfo[i]->Type == _Type_CCS_2)
  1856. ReleaseEmsOccureByString(i, "013627");
  1857. break;
  1858. }
  1859. }
  1860. }
  1861. break;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. //===============================================
  1867. // Main process
  1868. //===============================================
  1869. // 檢查 Byte 中某個 Bit 的值
  1870. // _byte : 欲改變的 byte
  1871. // _bit : 該 byte 的第幾個 bit
  1872. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  1873. {
  1874. return ( _byte & mask_table[_bit] ) != 0x00;
  1875. }
  1876. // 設定 Byte 中某個 Bit的值
  1877. // _byte : 欲改變的 byte
  1878. // _bit : 該 byte 的第幾個 bit
  1879. // value : 修改的值為 0 or 1
  1880. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  1881. {
  1882. if(value == 1)
  1883. *_byte |= (1 << _bit);
  1884. else if (value == 0)
  1885. *_byte ^= (1 << _bit);
  1886. }
  1887. void UserScanFunction()
  1888. {
  1889. bool idleReq = false;
  1890. unsigned char stopReq = 255;
  1891. // 當前非驗證的狀態
  1892. if(!IsAuthorizingMode())
  1893. {
  1894. // 先判斷現在是否可以提供刷卡
  1895. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  1896. // 2. 停止充電
  1897. for (byte i = 0; i < _gunCount; i++)
  1898. {
  1899. // 二擇一
  1900. if ((chargingInfo[i]->SystemStatus >= S_AUTHORIZING && chargingInfo[i]->SystemStatus <= S_PREPARNING) ||
  1901. (chargingInfo[i]->SystemStatus >= S_TERMINATING && chargingInfo[i]->SystemStatus < S_COMPLETE))
  1902. {
  1903. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1904. return;
  1905. }
  1906. if (chargingInfo[i]->SystemStatus == S_CHARGING)
  1907. {
  1908. stopReq = i;
  1909. }
  1910. else if (chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable == YES)
  1911. {
  1912. idleReq = true;
  1913. }
  1914. }
  1915. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  1916. {
  1917. if (stopReq < _gunCount &&
  1918. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING)
  1919. {
  1920. char value[32];
  1921. PRINTF_FUNC("stop charging \n");
  1922. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  1923. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  1924. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  1925. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  1926. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  1927. {
  1928. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1929. }
  1930. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1931. }
  1932. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_COMPLETE)
  1933. {
  1934. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1935. }
  1936. else if (idleReq)
  1937. {
  1938. if (_gunCount > 1 && stopReq != 255 && ONE_CONNECTOR_USE == YES)
  1939. {
  1940. idleReq = false;
  1941. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1942. }
  1943. else
  1944. {
  1945. PRINTF_FUNC("// LCM => Authorizing \n");
  1946. // LCM => Authorizing
  1947. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  1948. // 進入確認卡號狀態
  1949. AuthorizingStart();
  1950. }
  1951. }
  1952. else
  1953. {
  1954. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1955. }
  1956. }
  1957. }
  1958. }
  1959. unsigned char isModeChange(unsigned char gun_index)
  1960. {
  1961. unsigned char result = NO;
  1962. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  1963. {
  1964. result = YES;
  1965. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  1966. }
  1967. return result;
  1968. }
  1969. void ScannerCardProcess()
  1970. {
  1971. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level == 0 &&
  1972. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_OCPP)
  1973. {
  1974. isCardScan = true;
  1975. // 處理刷卡及驗證卡號的動作
  1976. UserScanFunction();
  1977. }
  1978. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  1979. {
  1980. StartSystemTimeoutDet(Timeout_Authorizing);
  1981. // 確認驗證卡號完成沒
  1982. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  1983. {
  1984. StopSystemTimeoutDet();
  1985. // 判斷後台回覆狀態
  1986. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  1987. {
  1988. // LCM => Authorize complete
  1989. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  1990. }
  1991. else
  1992. {
  1993. // LCM => Authorize fail
  1994. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1995. }
  1996. ClearAuthorizedFlag();
  1997. }
  1998. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  1999. {
  2000. // 白名單驗證
  2001. for (int i = 0; i < 10; i++)
  2002. {
  2003. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2004. {
  2005. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2006. {
  2007. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2008. ClearAuthorizedFlag();
  2009. break;
  2010. }
  2011. }
  2012. }
  2013. }
  2014. }
  2015. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2016. {
  2017. StartSystemTimeoutDet(Timeout_VerifyFail);
  2018. isCardScan = false;
  2019. }
  2020. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2021. {
  2022. StartSystemTimeoutDet(Timeout_VerifyComp);
  2023. }
  2024. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2025. {
  2026. StartSystemTimeoutDet(Timeout_WaitPlug);
  2027. }
  2028. else
  2029. isCardScan = false;
  2030. }
  2031. void AddGunInfoByConnector(byte typeValue, byte slots)
  2032. {
  2033. switch (typeValue)
  2034. {
  2035. case '0': // none
  2036. break;
  2037. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  2038. break;
  2039. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  2040. break;
  2041. case '3': // IEC 62196-2 Type 2 Plug
  2042. break;
  2043. case '4': // IEC 62196-2 Type 2 Socket
  2044. break;
  2045. case '5': // GB/T AC Plug
  2046. break;
  2047. case '6': // GB/T AC Socket
  2048. break;
  2049. case 'J': // CHAdeMO
  2050. {
  2051. if (CHAdeMO_QUANTITY > _chademoIndex)
  2052. {
  2053. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  2054. chargingInfo[_gunIndex]->Index = _gunIndex;
  2055. chargingInfo[_gunIndex]->slotsIndex = slots;
  2056. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2057. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  2058. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  2059. chargingInfo[_gunIndex]->IsAvailable = YES;
  2060. _chademoIndex++;
  2061. _gunIndex++;
  2062. }
  2063. }
  2064. break;
  2065. case 'U': // CCS1 combo
  2066. break;
  2067. case 'E': // CCS2 combo
  2068. {
  2069. if (CCS_QUANTITY > _ccsIndex)
  2070. {
  2071. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  2072. chargingInfo[_gunIndex]->Index = _gunIndex;
  2073. chargingInfo[_gunIndex]->slotsIndex = slots;
  2074. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2075. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  2076. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  2077. chargingInfo[_gunIndex]->IsAvailable = YES;
  2078. // 現階段預設為走 DIN70121
  2079. ShmCcsData->CommProtocol = 0x01;
  2080. _ccsIndex++;
  2081. _gunIndex++;
  2082. }
  2083. }
  2084. break;
  2085. case 'G': // GBT DC
  2086. break;
  2087. case 'D': // GBT DC x 2
  2088. break;
  2089. }
  2090. }
  2091. bool CheckConnectorTypeStatus()
  2092. {
  2093. bool result = true;
  2094. PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  2095. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  2096. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  2097. {
  2098. byte slots = 1;
  2099. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  2100. {
  2101. AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots);
  2102. slots++;
  2103. }
  2104. _gunCount = _gunIndex;
  2105. PRINTF_FUNC("_gunCount = %d \n", _gunCount);
  2106. if (_gunCount == 0)
  2107. result = false;
  2108. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2109. for (byte gunIndex = 0; gunIndex < _gunCount; gunIndex++)
  2110. {
  2111. if (bd0_1_status == 0 && bd0_2_status == 1)
  2112. {
  2113. // 與硬體相同 type : Chademo
  2114. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2115. {
  2116. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2117. }
  2118. }
  2119. else if (bd0_1_status == 1 && bd0_2_status == 0)
  2120. {
  2121. // 與硬體相同 type : CCS
  2122. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2123. {
  2124. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2125. }
  2126. }
  2127. if (bd1_1_status == 0 && bd1_2_status == 1)
  2128. {
  2129. // 與硬體相同 type : Chademo
  2130. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2131. {
  2132. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2133. }
  2134. if (_gunCount == 1)
  2135. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2136. }
  2137. else if (bd1_1_status == 1 && bd1_2_status == 0)
  2138. {
  2139. // 與硬體相同 type : CCS
  2140. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2141. {
  2142. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2143. }
  2144. if (_gunCount == 1)
  2145. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2146. }
  2147. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  2148. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  2149. result = false;
  2150. }
  2151. }
  2152. else
  2153. {
  2154. // Module Name 不正確 - 告警
  2155. result = false;
  2156. }
  2157. return result;
  2158. }
  2159. void KillTask()
  2160. {
  2161. ChangeLcmByIndex(_LCM_FIX);
  2162. system("killall Module_EventLogging");
  2163. system("killall Module_PrimaryComm");
  2164. system("killall Module_EvComm");
  2165. system("killall Module_LcmControl");
  2166. system("killall Module_InternalComm");
  2167. system("killall Module_PsuComm");
  2168. }
  2169. char CheckUpdateProcess()
  2170. {
  2171. DIR *d;
  2172. struct dirent *dir;
  2173. d = opendir("/mnt/");
  2174. if (d)
  2175. {
  2176. long int MaxLen=48*1024*1024, ImageLen = 0;
  2177. while ((dir = readdir(d)) != NULL)
  2178. {
  2179. char *new_str;
  2180. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  2181. new_str[0] = '\0';
  2182. strcat(new_str, "/mnt/");
  2183. strcat(new_str, dir->d_name);
  2184. int fd = open(new_str, O_RDONLY);
  2185. if (fd < 0)
  2186. {
  2187. return FAIL;
  2188. }
  2189. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2190. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2191. //get the image length
  2192. ImageLen = read(fd, ptr, MaxLen);
  2193. if (ImageLen > 20)
  2194. {
  2195. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  2196. PRINTF_FUNC("Typed...%x \r\n", Type);
  2197. switch (Type)
  2198. {
  2199. case 0x10000001:
  2200. case 0x10000002:
  2201. case 0x10000003:
  2202. case 0x10000004:
  2203. case 0x10000005:
  2204. {
  2205. if (Upgrade_Flash(Type, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2206. return PASS;
  2207. else
  2208. return FAIL;
  2209. }
  2210. break;
  2211. case 0x10000006:
  2212. case 0x1000000D:
  2213. case 0x1000000E:
  2214. {
  2215. // CSU_PRIMARY_CONTROLLER : 0x10000006
  2216. byte target = 0x00;
  2217. if (Type == 0x10000006)
  2218. target = UPGRADE_PRI;
  2219. else if (Type == 0x1000000D)
  2220. target = UPGRADE_RB;
  2221. else if (Type == 0x1000000E)
  2222. target = UPGRADE_FAN;
  2223. int fd = InitComPort(target);
  2224. if (Upgrade_UART(fd, Type, target, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2225. return PASS;
  2226. else
  2227. return FAIL;
  2228. close(fd);
  2229. }
  2230. break;
  2231. case 0x10000007:
  2232. case 0x10000008:
  2233. case 0x10000009:
  2234. case 0x1000000A:
  2235. case 0x1000000B:
  2236. case 0x1000000C:
  2237. {
  2238. // CHAdeMO_BOARD : 0x1000000B
  2239. bool isPass = true;
  2240. int CanFd = InitCanBus();
  2241. if (CanFd > 0)
  2242. {
  2243. for(byte index = 0; index < _gunCount; index++)
  2244. {
  2245. if (!isPass)
  2246. break;
  2247. if (Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo)
  2248. {
  2249. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) != PASS)
  2250. {
  2251. PRINTF_FUNC("Upgrad Fail. \n");
  2252. isPass = false;
  2253. }
  2254. }
  2255. else if ((Type == 0x10000007 || Type == 0x10000008 || Type == 0x10000009 || Type == 0x1000000A) &&
  2256. chargingInfo[index]->Type == _Type_CCS_2)
  2257. {
  2258. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) != PASS)
  2259. {
  2260. PRINTF_FUNC("Upgrad Fail. \n");
  2261. isPass = false;
  2262. }
  2263. }
  2264. }
  2265. }
  2266. else
  2267. {
  2268. PRINTF_FUNC("Upgrad FD fail. \n");
  2269. isPass = false;
  2270. }
  2271. return isPass;
  2272. break;
  2273. }
  2274. }
  2275. }
  2276. free(new_str);
  2277. free(ptr);
  2278. }
  2279. }
  2280. free(dir);
  2281. closedir(d);
  2282. return FAIL;
  2283. }
  2284. void CreateRfidFork()
  2285. {
  2286. pid_t rfidRecPid;
  2287. rfidRecPid = fork();
  2288. if (rfidRecPid == 0)
  2289. {
  2290. while(true)
  2291. {
  2292. // 刷卡判斷
  2293. RFID rfid;
  2294. if(getRequestCardSN(rfidFd, 0, &rfid))
  2295. {
  2296. PRINTF_FUNC("Get Card.. \n");
  2297. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  2298. {
  2299. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  2300. {
  2301. switch (rfid.snType)
  2302. {
  2303. case RFID_SN_TYPE_6BYTE:
  2304. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2305. "%02X%02X%02X%02X%02X%02X",
  2306. rfid.currentCard[0], rfid.currentCard[1],
  2307. rfid.currentCard[2], rfid.currentCard[3],
  2308. rfid.currentCard[4], rfid.currentCard[5]);
  2309. break;
  2310. case RFID_SN_TYPE_7BYTE:
  2311. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2312. "%02X%02X%02X%02X%02X%02X%02X",
  2313. rfid.currentCard[0], rfid.currentCard[1],
  2314. rfid.currentCard[2], rfid.currentCard[3],
  2315. rfid.currentCard[4], rfid.currentCard[5],
  2316. rfid.currentCard[6]);
  2317. break;
  2318. case RFID_SN_TYPE_10BYTE:
  2319. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2320. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2321. rfid.currentCard[0], rfid.currentCard[1],
  2322. rfid.currentCard[2], rfid.currentCard[3],
  2323. rfid.currentCard[4], rfid.currentCard[5],
  2324. rfid.currentCard[6], rfid.currentCard[7],
  2325. rfid.currentCard[8], rfid.currentCard[9]);
  2326. break;
  2327. case RFID_SN_TYPE_4BYTE:
  2328. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2329. "%02X%02X%02X%02X",
  2330. rfid.currentCard[0], rfid.currentCard[1],
  2331. rfid.currentCard[2], rfid.currentCard[3]);
  2332. break;
  2333. }
  2334. }
  2335. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  2336. {
  2337. switch (rfid.snType)
  2338. {
  2339. case RFID_SN_TYPE_6BYTE:
  2340. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2341. "%02X%02X%02X%02X%02X%02X",
  2342. rfid.currentCard[5], rfid.currentCard[4],
  2343. rfid.currentCard[3], rfid.currentCard[2],
  2344. rfid.currentCard[1], rfid.currentCard[0]);
  2345. break;
  2346. case RFID_SN_TYPE_7BYTE:
  2347. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2348. "%02X%02X%02X%02X%02X%02X%02X",
  2349. rfid.currentCard[6], rfid.currentCard[5],
  2350. rfid.currentCard[4], rfid.currentCard[3],
  2351. rfid.currentCard[2], rfid.currentCard[1],
  2352. rfid.currentCard[0]);
  2353. break;
  2354. case RFID_SN_TYPE_10BYTE:
  2355. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2356. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2357. rfid.currentCard[9], rfid.currentCard[8],
  2358. rfid.currentCard[7], rfid.currentCard[6],
  2359. rfid.currentCard[5], rfid.currentCard[4],
  2360. rfid.currentCard[3], rfid.currentCard[2],
  2361. rfid.currentCard[1], rfid.currentCard[0]);
  2362. break;
  2363. case RFID_SN_TYPE_4BYTE:
  2364. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2365. "%02X%02X%02X%02X",
  2366. rfid.currentCard[3], rfid.currentCard[2],
  2367. rfid.currentCard[1], rfid.currentCard[0]);
  2368. break;
  2369. }
  2370. }
  2371. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2372. }
  2373. }
  2374. sleep(1);
  2375. }
  2376. }
  2377. }
  2378. void StartSystemTimeoutDet(unsigned char flag)
  2379. {
  2380. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  2381. {
  2382. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2383. }
  2384. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  2385. }
  2386. void StopSystemTimeoutDet()
  2387. {
  2388. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2389. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  2390. }
  2391. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  2392. {
  2393. if (gunIndex < _gunCount)
  2394. {
  2395. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  2396. {
  2397. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  2398. }
  2399. chargingInfo[gunIndex]->TimeoutFlag = flag;
  2400. }
  2401. }
  2402. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  2403. {
  2404. if (gunIndex < _gunCount)
  2405. {
  2406. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  2407. }
  2408. }
  2409. void CreateTimeoutFork()
  2410. {
  2411. pid_t timeoutPid;
  2412. timeoutPid = fork();
  2413. if (timeoutPid == 0)
  2414. {
  2415. while(true)
  2416. {
  2417. // 系統
  2418. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  2419. {
  2420. case Timeout_SelftestChk:
  2421. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2422. {
  2423. _SelfTestTimeout();
  2424. StopSystemTimeoutDet();
  2425. }
  2426. break;
  2427. case Timeout_Authorizing:
  2428. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 50000000)
  2429. {
  2430. _AuthorizedTimeout();
  2431. StopSystemTimeoutDet();
  2432. }
  2433. break;
  2434. case Timeout_VerifyFail:
  2435. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2436. {
  2437. _AutoReturnTimeout();
  2438. StopSystemTimeoutDet();
  2439. }
  2440. break;
  2441. case Timeout_VerifyComp:
  2442. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2443. {
  2444. _AutoReturnTimeout();
  2445. StopSystemTimeoutDet();
  2446. }
  2447. break;
  2448. case Timeout_WaitPlug:
  2449. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2450. {
  2451. _DetectPlugInTimeout();
  2452. StopSystemTimeoutDet();
  2453. }
  2454. break;
  2455. case Timeout_ReturnToChargingGunDet:
  2456. {
  2457. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 10000000)
  2458. {
  2459. DisplayChargingInfo();
  2460. StopSystemTimeoutDet();
  2461. }
  2462. }
  2463. break;
  2464. }
  2465. // 各槍
  2466. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2467. {
  2468. switch(chargingInfo[gun_index]->TimeoutFlag)
  2469. {
  2470. case Timeout_Preparing:
  2471. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 40000000)
  2472. {
  2473. _PrepareTimeout(gun_index);
  2474. StopGunInfoTimeoutDet(gun_index);
  2475. }
  2476. break;
  2477. case Timeout_EvChargingDet:
  2478. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
  2479. {
  2480. _DetectEvChargingEnableTimeout(gun_index);
  2481. StopGunInfoTimeoutDet(gun_index);
  2482. }
  2483. break;
  2484. case Timeout_EvseChargingDet:
  2485. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2486. {
  2487. _DetectEvseChargingEnableTimeout(gun_index);
  2488. StopGunInfoTimeoutDet(gun_index);
  2489. }
  2490. break;
  2491. case Timeout_WaitforCompleteDet:
  2492. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  2493. {
  2494. _CompleteTimeout(gun_index);
  2495. StopGunInfoTimeoutDet(gun_index);
  2496. }
  2497. break;
  2498. case Timeout_ForCcsPrechargeDet:
  2499. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2500. {
  2501. _CcsPrechargeTimeout(gun_index);
  2502. StopGunInfoTimeoutDet(gun_index);
  2503. }
  2504. break;
  2505. }
  2506. }
  2507. usleep(100000);
  2508. }
  2509. }
  2510. }
  2511. void GetSystemTime()
  2512. {
  2513. struct timeb csuTime;
  2514. struct tm *tmCSU;
  2515. ftime(&csuTime);
  2516. tmCSU = localtime(&csuTime.time);
  2517. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  2518. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2519. tmCSU->tm_sec);
  2520. // byte date[14];
  2521. //
  2522. //
  2523. // //sprintf(&date, "%d", );
  2524. //
  2525. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  2526. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  2527. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  2528. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  2529. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  2530. //
  2531. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  2532. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  2533. //
  2534. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  2535. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  2536. //
  2537. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  2538. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  2539. //
  2540. // date[10] = (tmCSU->tm_min) / 10 % 10;
  2541. // date[11] = (tmCSU->tm_min) / 1 % 10;
  2542. //
  2543. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  2544. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  2545. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  2546. // date[4], date[5], date[6], date[7],
  2547. // date[8], date[9], date[10], date[11],
  2548. // date[12], date[13]);
  2549. }
  2550. void CheckFactoryConfigFunction()
  2551. {
  2552. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  2553. {
  2554. system("cd /root;./FactoryConfig -m");
  2555. system("sync");
  2556. sleep(5);
  2557. system("reboot -f");
  2558. sleep(5);
  2559. system("reboot -f");
  2560. }
  2561. }
  2562. void CheckFwUpdateFunction()
  2563. {
  2564. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  2565. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  2566. {
  2567. DEBUG_INFO_MSG("ftp : update start. \n");
  2568. KillTask();
  2569. if (CheckUpdateProcess() == PASS)
  2570. DEBUG_INFO_MSG("ftp : update complete. \n");
  2571. else
  2572. DEBUG_INFO_MSG("ftp : update fail. \n");
  2573. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  2574. sleep(5);
  2575. system("reboot -f");
  2576. }
  2577. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  2578. {
  2579. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  2580. }
  2581. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  2582. {
  2583. DEBUG_INFO_MSG("Backend : update start. \n");
  2584. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2585. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  2586. KillTask();
  2587. if (CheckUpdateProcess() == PASS)
  2588. {
  2589. DEBUG_INFO_MSG("Backend : update complete. \n");
  2590. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2591. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  2592. }
  2593. else
  2594. {
  2595. DEBUG_INFO_MSG("Backend : update fail. \n");
  2596. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2597. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  2598. }
  2599. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  2600. sleep(5);
  2601. system("reboot -f");
  2602. }
  2603. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == EQUAL)
  2604. {
  2605. DEBUG_ERROR_MSG("Backend to download file fail. \n");
  2606. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2607. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  2608. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  2609. }
  2610. }
  2611. //===============================================
  2612. // Check reservation date is expired
  2613. //===============================================
  2614. int isReservationExpired(unsigned char gun_index)
  2615. {
  2616. int result = NO;
  2617. struct tm expiredDate;
  2618. struct timeb expiredTime;
  2619. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  2620. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2621. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2622. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  2623. {
  2624. expiredDate.tm_year -= 1900;
  2625. expiredDate.tm_mon -= 1;
  2626. expiredTime.time = mktime(&expiredDate);
  2627. if (!CheckTimeOut(expiredTime))
  2628. {
  2629. result = YES;
  2630. }
  2631. }
  2632. return result;
  2633. }
  2634. //===============================================
  2635. // OCPP
  2636. //===============================================
  2637. void CheckOcppStatus()
  2638. {
  2639. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  2640. {
  2641. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = YES;
  2642. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  2643. }
  2644. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  2645. {
  2646. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  2647. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  2648. {
  2649. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  2650. sleep(3);
  2651. system("reboot -f");
  2652. }
  2653. else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  2654. {
  2655. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  2656. sleep(3);
  2657. KillTask();
  2658. system("/usr/bin/run_evse_restart.sh");
  2659. }
  2660. }
  2661. }
  2662. void OcppStartTransation(byte gunIndex)
  2663. {
  2664. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  2665. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  2666. else
  2667. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  2668. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  2669. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionReq = YES;
  2670. }
  2671. void OcppStopTransation(byte gunIndex)
  2672. {
  2673. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  2674. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  2675. else
  2676. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  2677. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  2678. ShmOCPP16Data->CpMsg.bits[gunIndex].StopTransactionReq = YES;
  2679. }
  2680. bool OcppRemoteStop(byte gunIndex)
  2681. {
  2682. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  2683. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  2684. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  2685. return result;
  2686. }
  2687. void OcppRemoteStartChk()
  2688. {
  2689. if(!isDetectPlugin())
  2690. {
  2691. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2692. {
  2693. if ((chargingInfo[gun_index]->SystemStatus == S_IDLE || chargingInfo[gun_index]->SystemStatus == S_RESERVATION)&&
  2694. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES)
  2695. {
  2696. ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  2697. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = NO;
  2698. DetectPluginStart();
  2699. break;
  2700. }
  2701. }
  2702. }
  2703. }
  2704. void ChkOcppStatus(byte gunIndex)
  2705. {
  2706. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  2707. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  2708. {
  2709. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  2710. if (isReservationExpired(gunIndex))
  2711. {
  2712. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  2713. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  2714. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  2715. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  2716. }
  2717. }
  2718. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  2719. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  2720. {
  2721. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  2722. if (isReservationExpired(gunIndex))
  2723. {
  2724. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  2725. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  2726. chargingInfo[gunIndex]->ReservationId = 0;
  2727. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  2728. }
  2729. }
  2730. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  2731. {
  2732. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  2733. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  2734. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  2735. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  2736. chargingInfo[gunIndex]->IsAvailable = YES;
  2737. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  2738. chargingInfo[gunIndex]->IsAvailable = NO;
  2739. }
  2740. }
  2741. bool CheckBackendChargingTimeout(byte gunIndex)
  2742. {
  2743. bool result = false;
  2744. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_OCPP)
  2745. {
  2746. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  2747. {
  2748. if (chargingInfo[gunIndex]->RemainChargingDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  2749. result = true;
  2750. }
  2751. }
  2752. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_FREE)
  2753. {
  2754. // 隨插即充電的要看 offline
  2755. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  2756. {
  2757. if (chargingInfo[gunIndex]->RemainChargingDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  2758. result = true;
  2759. }
  2760. }
  2761. return result;
  2762. }
  2763. bool CheckBackendChargingEnergy(byte gunIndex)
  2764. {
  2765. bool result = false;
  2766. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_OCPP)
  2767. {
  2768. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  2769. {
  2770. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  2771. result = true;
  2772. }
  2773. }
  2774. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_FREE)
  2775. {
  2776. // 隨插即充電的要看 offline
  2777. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  2778. {
  2779. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  2780. result = true;
  2781. }
  2782. }
  2783. return result;
  2784. }
  2785. //===============================================
  2786. // Config process
  2787. //===============================================
  2788. void AddPlugInTimes(byte gunIndex)
  2789. {
  2790. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2791. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  2792. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2793. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  2794. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  2795. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  2796. }
  2797. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  2798. {
  2799. char cmdBuf[32];
  2800. struct timeb csuTime;
  2801. struct tm *tmCSU;
  2802. ftime(&csuTime);
  2803. tmCSU = localtime(&csuTime.time);
  2804. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2805. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2806. tmCSU->tm_sec);
  2807. if (isStart)
  2808. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  2809. else
  2810. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  2811. }
  2812. void zipLogFiles()
  2813. {
  2814. const char* logPath = "/Storage/SystemLog";
  2815. // 獲取目錄
  2816. DIR* pDir = opendir(logPath);
  2817. if (pDir != NULL)
  2818. {
  2819. struct timeb csuTime;
  2820. struct tm *tmCSU;
  2821. ftime(&csuTime);
  2822. tmCSU = localtime(&csuTime.time);
  2823. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  2824. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2825. // tmCSU->tm_sec);
  2826. // Read items inside the folder
  2827. struct dirent* pEntry = NULL;
  2828. while ((pEntry = readdir(pDir)) != NULL)
  2829. {
  2830. if (strcmp(pEntry->d_name, ".") != 0 &&
  2831. strcmp(pEntry->d_name, "..") != 0 &&
  2832. strncmp(pEntry->d_name, "[", 1) == 0 &&
  2833. strstr(pEntry->d_name, "tar") < 0)
  2834. {
  2835. char yearC[5];
  2836. unsigned short year = 0;
  2837. char monthC[3];
  2838. unsigned short month = 0;
  2839. yearC[4] = '\0';
  2840. strncpy(yearC, pEntry->d_name + 1, 4);
  2841. monthC[2] = '\0';
  2842. strncpy(monthC, pEntry->d_name + 6, 2);
  2843. year = atoi(yearC);
  2844. month = atoi(monthC);
  2845. if (year != 0)
  2846. {
  2847. if (year < tmCSU->tm_year + 1900 ||
  2848. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  2849. {
  2850. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  2851. char file[256];
  2852. memset(file, 0x00, sizeof(file));
  2853. strcat(file, "tar zcvf ");
  2854. strcat(file, logPath);
  2855. strncat(file, "/", 1);
  2856. strcat(file, pEntry->d_name);
  2857. strcat(file, ".tar");
  2858. strncat(file, " ", 1);
  2859. strcat(file, logPath);
  2860. strncat(file, "/", 1);
  2861. strcat(file, pEntry->d_name);
  2862. PRINTF_FUNC("zip = %s \n", file);
  2863. system(file);
  2864. }
  2865. }
  2866. }
  2867. }
  2868. }
  2869. // Close folder
  2870. closedir(pDir);
  2871. }
  2872. void StopProcessing()
  2873. {
  2874. for (;;)
  2875. sleep(5);
  2876. }
  2877. int main(void)
  2878. {
  2879. if(CreateShareMemory() == 0)
  2880. {
  2881. #ifdef SystemLogMessage
  2882. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  2883. #endif
  2884. if(ShmStatusCodeData!=NULL)
  2885. {
  2886. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  2887. }
  2888. return 0;
  2889. sleep(5);
  2890. system("reboot -f");
  2891. sleep(5);
  2892. system("reboot -f");
  2893. }
  2894. PRINTF_FUNC("\n"); PRINTF_FUNC("Initial SystemConfig and Info.......\n");
  2895. if (!InitialSystemDefaultConfig())
  2896. {
  2897. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  2898. StopProcessing();
  2899. }
  2900. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  2901. CheckGunTypeFromHw();
  2902. PRINTF_FUNC("InitialShareMemoryInfo & Initialization. \n");
  2903. InitialShareMemoryInfo();
  2904. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  2905. ChangeLcmByIndex(_LCM_INIT);
  2906. if (!CheckConnectorTypeStatus())
  2907. {
  2908. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  2909. ChangeLcmByIndex(_LCM_FIX);
  2910. // Module Name 與硬體對應不正確
  2911. PRINTF_FUNC("Module Name & HW info none match. \n");
  2912. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  2913. sleep(3);
  2914. KillTask();
  2915. StopProcessing();
  2916. }
  2917. Initialization();
  2918. ShmSysConfigAndInfo->SysInfo.InternetConn = 1;
  2919. PRINTF_FUNC("Spawn all Task. \n");
  2920. SpawnTask();
  2921. PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
  2922. CreateTimeoutFork();
  2923. PRINTF_FUNC("Self test. \n");
  2924. SelfTestRun();
  2925. StopSystemTimeoutDet();
  2926. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  2927. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  2928. ShmPsuData->Work_Step == _NO_WORKING)
  2929. {
  2930. DisplaySelfTestFailReason();
  2931. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2932. {
  2933. setChargerMode(gun_index, MODE_ALARM);
  2934. }
  2935. ChangeLcmByIndex(_LCM_FIX);
  2936. StopProcessing();
  2937. }
  2938. else
  2939. {
  2940. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2941. {
  2942. setChargerMode(gun_index, MODE_IDLE);
  2943. }
  2944. }
  2945. ChangeLcmByIndex(_LCM_IDLE);
  2946. sleep(1);
  2947. //***** 須新增的偵測 *****//
  2948. // 1. Thernal - 控制風扇轉速
  2949. // 2. ouput fuse - 控制風扇轉速
  2950. CreateRfidFork();
  2951. // Main loop
  2952. PRINTF_FUNC("Main Loop. \n");
  2953. for (;;)
  2954. {
  2955. CheckOcppStatus();
  2956. ChkPrimaryStatus();
  2957. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_IDLE &&
  2958. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  2959. {
  2960. CheckFactoryConfigFunction();
  2961. CheckFwUpdateFunction();
  2962. }
  2963. // OCPP 邏輯
  2964. OcppRemoteStartChk();
  2965. // 讀卡邏輯
  2966. ScannerCardProcess();
  2967. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2968. {
  2969. CheckGpioInStatus();
  2970. CheckErrorOccurStatus(gun_index);
  2971. ChkOcppStatus(gun_index);
  2972. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  2973. switch(chargingInfo[gun_index]->SystemStatus)
  2974. {
  2975. case S_IDLE:
  2976. case S_RESERVATION:
  2977. {
  2978. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  2979. isModeChange(gun_index))
  2980. {
  2981. PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
  2982. chargingInfo[gun_index]->RemainChargingDuration = 0;
  2983. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  2984. }
  2985. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  2986. isModeChange(gun_index))
  2987. {
  2988. PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
  2989. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  2990. }
  2991. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  2992. {
  2993. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  2994. {
  2995. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  2996. }
  2997. ClearDetectPluginFlag();
  2998. }
  2999. else
  3000. {
  3001. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
  3002. {
  3003. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3004. }
  3005. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3006. {
  3007. // 均充 -> 最大充
  3008. }
  3009. else
  3010. {
  3011. // 判斷是否有啟用檢查插槍
  3012. if(isDetectPlugin())
  3013. {
  3014. // 卡號驗證成功後,等待充電槍插入充電車
  3015. if ((ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL &&
  3016. ShmSysConfigAndInfo->SysInfo.OrderCharging < _gunCount))
  3017. {
  3018. if (chargingInfo[(unsigned char)ShmSysConfigAndInfo->SysInfo.OrderCharging]->ConnectorPlugIn == YES &&
  3019. chargingInfo[gun_index]->IsAvailable)
  3020. {
  3021. PRINTF_FUNC("-----------------1----------------- %d \n", ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3022. ShmSysConfigAndInfo->SysInfo.CurGunSelected = ShmSysConfigAndInfo->SysInfo.OrderCharging;
  3023. AddPlugInTimes(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3024. setChargerMode(ShmSysConfigAndInfo->SysInfo.OrderCharging, MODE_REASSIGN_CHECK);
  3025. ClearDetectPluginFlag();
  3026. continue;
  3027. }
  3028. }
  3029. else
  3030. {
  3031. if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
  3032. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  3033. {
  3034. PRINTF_FUNC("-----------------2----------------- \n");
  3035. ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
  3036. AddPlugInTimes(gun_index);
  3037. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  3038. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  3039. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3040. // 當前操作的槍號,進入 Preparing
  3041. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3042. ClearDetectPluginFlag();
  3043. continue;
  3044. }
  3045. }
  3046. if (!isCardScan)
  3047. {
  3048. // LCM => Waiting for plugging
  3049. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3050. }
  3051. }
  3052. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  3053. {
  3054. if (!isReservationExpired(gun_index))
  3055. {
  3056. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  3057. }
  3058. }
  3059. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == _SYS_AUTHORIZE_FREE &&
  3060. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  3061. {
  3062. PRINTF_FUNC("-----------------3----------------- \n");
  3063. ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
  3064. AddPlugInTimes(gun_index);
  3065. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3066. ClearDetectPluginFlag();
  3067. continue;
  3068. }
  3069. else
  3070. {
  3071. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3072. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  3073. if (_gunCount > 1 &&
  3074. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  3075. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  3076. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  3077. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  3078. {
  3079. for (byte count = 0; count < _gunCount; count++)
  3080. {
  3081. if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected &&
  3082. ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  3083. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1)))
  3084. {
  3085. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3086. }
  3087. }
  3088. }
  3089. }
  3090. }
  3091. }
  3092. ReleaseAlarmCode(gun_index);
  3093. }
  3094. break;
  3095. case S_REASSIGN_CHECK:
  3096. {
  3097. if (isModeChange(gun_index))
  3098. {
  3099. PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
  3100. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3101. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL)
  3102. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  3103. StopSystemTimeoutDet();
  3104. }
  3105. bool isRessign = false;
  3106. if (_gunCount > 1)
  3107. {
  3108. for (byte index = 0; index < _gunCount; index++)
  3109. {
  3110. // 有其他槍已經分配好 psu 模塊
  3111. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  3112. chargingInfo[index]->SystemStatus >= S_PREPARNING)
  3113. {
  3114. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  3115. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE;
  3116. isRessign = true;
  3117. break;
  3118. }
  3119. }
  3120. }
  3121. if (isRessign)
  3122. setChargerMode(gun_index, MODE_REASSIGN);
  3123. else
  3124. setChargerMode(gun_index, MODE_PRECHARGE);
  3125. }
  3126. break;
  3127. case S_REASSIGN:
  3128. {
  3129. if (isModeChange(gun_index))
  3130. {
  3131. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  3132. }
  3133. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  3134. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  3135. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  3136. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  3137. setChargerMode(gun_index, MODE_PRECHARGE);
  3138. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY &&
  3139. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3140. {
  3141. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  3142. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  3143. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  3144. }
  3145. PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  3146. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3147. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3148. }
  3149. break;
  3150. case S_PREPARNING:
  3151. {
  3152. if (isModeChange(gun_index))
  3153. {
  3154. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  3155. StopGunInfoTimeoutDet(gun_index);
  3156. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  3157. }
  3158. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  3159. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3160. // Precharge 三個流程 : 1 Precharge, 2 Preparing for ev, 3 Preparing for evse
  3161. // Precharge : AC Contactor <Relay board>, Relay k1 k2 <Relay board>, PSU AddressAssignment, PSU GroupAvailablePower
  3162. // Preparing for ev : 車端通訊流程
  3163. // Preparing for evse : PSU (output 500V, 2A), GFD Test <Relay board>
  3164. //ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES &&
  3165. if (((ShmPsuData->SystemPresentPsuQuantity > 0 &&
  3166. ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 &&
  3167. ShmPsuData->PsuGroup[gun_index].GroupAvailablePower > 10) &&
  3168. chargingInfo[gun_index]->AvailableChargingPower > 10))
  3169. {
  3170. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  3171. }
  3172. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3173. OcppRemoteStop(gun_index) == YES)
  3174. {
  3175. // 板端或後臺要求停止
  3176. ChargingTerminalProcess(gun_index);
  3177. }
  3178. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3179. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3180. }
  3181. break;
  3182. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  3183. {
  3184. if (isModeChange(gun_index))
  3185. {
  3186. PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  3187. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3188. StopGunInfoTimeoutDet(gun_index);
  3189. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  3190. }
  3191. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3192. {
  3193. // 檢查車端的槍鎖是否為鎖上
  3194. if (isEvGunLocked_chademo(gun_index) == YES)
  3195. {
  3196. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3197. }
  3198. }
  3199. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3200. {
  3201. // 檢查車端的 charging enable 是否為 1
  3202. if (isEvGunLocked_ccs(gun_index) == YES)
  3203. {
  3204. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3205. }
  3206. }
  3207. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3208. OcppRemoteStop(gun_index) == YES)
  3209. {
  3210. // 板端或後臺要求停止
  3211. ChargingTerminalProcess(gun_index);
  3212. }
  3213. // LCM => Pre-charging
  3214. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3215. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3216. }
  3217. break;
  3218. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  3219. {
  3220. if (isModeChange(gun_index))
  3221. {
  3222. PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  3223. StopGunInfoTimeoutDet(gun_index);
  3224. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  3225. }
  3226. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3227. {
  3228. // 檢查樁端的 GFD 結果
  3229. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  3230. {
  3231. // 當前操作的槍號,進入 Charging
  3232. setChargerMode(gun_index, MODE_CHARGING);
  3233. }
  3234. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3235. {
  3236. // GFD 錯誤停止
  3237. RecordAlarmCode("012234");
  3238. }
  3239. }
  3240. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3241. {
  3242. // 檢查樁端的 GFD 結果
  3243. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS)
  3244. {
  3245. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  3246. }
  3247. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3248. {
  3249. // GFD 錯誤停止
  3250. RecordAlarmCode("012235");
  3251. }
  3252. }
  3253. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3254. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  3255. OcppRemoteStop(gun_index) == YES)
  3256. {
  3257. // 板端或後臺要求停止
  3258. ChargingTerminalProcess(gun_index);
  3259. }
  3260. // LCM => Pre-charging
  3261. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3262. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3263. }
  3264. break;
  3265. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  3266. {
  3267. if (isModeChange(gun_index))
  3268. {
  3269. PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
  3270. StopGunInfoTimeoutDet(gun_index);
  3271. ftime(&startChargingTime[gun_index]);
  3272. ChangeStartOrStopDateTime(YES, gun_index);
  3273. OcppStartTransation(gun_index);
  3274. }
  3275. ftime(&endChargingTime[gun_index]);
  3276. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  3277. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3278. {
  3279. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3280. {
  3281. // GFD 錯誤停止
  3282. RecordAlarmCode("012234");
  3283. }
  3284. }
  3285. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3286. {
  3287. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3288. {
  3289. // GFD 錯誤停止
  3290. RecordAlarmCode("012235");
  3291. }
  3292. }
  3293. if (isEvBoardStopChargeFlag(gun_index) ||
  3294. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  3295. OcppRemoteStop(gun_index) ||
  3296. CheckBackendChargingTimeout(gun_index) ||
  3297. CheckBackendChargingEnergy(gun_index))
  3298. {
  3299. // 板端或後臺要求停止
  3300. ChargingTerminalProcess(gun_index);
  3301. }
  3302. // LCM => Charging
  3303. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3304. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  3305. }
  3306. break;
  3307. case S_TERMINATING:
  3308. {
  3309. if (isModeChange(gun_index))
  3310. {
  3311. PRINTF_FUNC ("terminating......................... %x \n", gun_index);
  3312. StopGunInfoTimeoutDet(gun_index);
  3313. }
  3314. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3315. {
  3316. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  3317. if (isEvStopCharging_chademo(gun_index) == YES ||
  3318. isPrechargeStatus_chademo(gun_index) <= 0)
  3319. {
  3320. setChargerMode(gun_index, MODE_COMPLETE);
  3321. }
  3322. }
  3323. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3324. {
  3325. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  3326. if (isEvStopCharging_ccs(gun_index) == YES)
  3327. {
  3328. setChargerMode(gun_index, MODE_COMPLETE);
  3329. }
  3330. }
  3331. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3332. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  3333. }
  3334. break;
  3335. case S_COMPLETE:
  3336. {
  3337. if (isModeChange(gun_index))
  3338. {
  3339. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  3340. OcppStopTransation(gun_index);
  3341. ftime(&endChargingTime[gun_index]);
  3342. if (chargingInfo[gun_index]->RemainChargingDuration != 0)
  3343. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  3344. //strcpy((char *)chargingInfo[gun_index]->CardNumber, "");
  3345. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3346. StopGunInfoTimeoutDet(gun_index);
  3347. ChangeStartOrStopDateTime(NO, gun_index);
  3348. // 單槍或者雙槍單充自動回 idle
  3349. if (_gunCount == 1 || (_gunCount > 1 && ONE_CONNECTOR_USE == YES))
  3350. StartGunInfoTimeoutDet(gun_index, Timeout_WaitforCompleteDet);
  3351. }
  3352. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3353. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  3354. }
  3355. break;
  3356. case S_CCS_PRECHARGE_ST0:
  3357. {
  3358. if (isModeChange(gun_index))
  3359. {
  3360. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  3361. StopGunInfoTimeoutDet(gun_index);
  3362. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  3363. }
  3364. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3365. {
  3366. // GFD 錯誤停止
  3367. RecordAlarmCode("012235");
  3368. }
  3369. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3370. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3371. {
  3372. // 板端要求停止
  3373. ChargingTerminalProcess(gun_index);
  3374. }
  3375. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  3376. // 切換 D+ Relay to Precharge Relay
  3377. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  3378. {
  3379. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  3380. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  3381. {
  3382. PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  3383. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  3384. }
  3385. }
  3386. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  3387. {
  3388. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  3389. }
  3390. break;
  3391. }
  3392. case S_CCS_PRECHARGE_ST1:
  3393. {
  3394. if (isModeChange(gun_index))
  3395. {
  3396. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  3397. }
  3398. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3399. {
  3400. // GFD 錯誤停止
  3401. RecordAlarmCode("012235");
  3402. }
  3403. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3404. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3405. {
  3406. // 板端要求停止
  3407. ChargingTerminalProcess(gun_index);
  3408. }
  3409. // 等待小板通知進入充電
  3410. // 切換 D+ Relay to Precharge Relay
  3411. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  3412. {
  3413. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  3414. setChargerMode(gun_index, MODE_CHARGING);
  3415. }
  3416. break;
  3417. }
  3418. }
  3419. }
  3420. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  3421. {
  3422. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  3423. }
  3424. else
  3425. {
  3426. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  3427. }
  3428. write(wtdFd, "a", 1);
  3429. usleep(whileLoopTime);
  3430. }
  3431. return FAIL;
  3432. }