main.c 155 KB

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