main.c 129 KB

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