main.c 134 KB

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