main.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415
  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.10.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. if(ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1044. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow)
  1045. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1046. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower / 30) * 100;
  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("*********** _PrechargeTimeout ***********\n");
  1614. setChargerMode(gunIndex, MODE_IDLE);
  1615. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  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.AuthorizeReq = NO;
  1651. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1652. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1653. }
  1654. bool isAuthorizedComplete()
  1655. {
  1656. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1657. return false;
  1658. return true;
  1659. }
  1660. bool IsAuthorizingMode()
  1661. {
  1662. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  1663. return false;
  1664. return true;
  1665. }
  1666. //===============================================
  1667. // 紀錄 Alarm Code
  1668. //===============================================
  1669. void RecordAlarmCode(char *code)
  1670. {
  1671. if (strcmp(code, "012234") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  1672. if (strcmp(code, "012235") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  1673. if (strcmp(code, "012236") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  1674. if (strcmp(code, "012288") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  1675. if (strcmp(code, "012289") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  1676. if (strcmp(code, "012290") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  1677. if (strcmp(code, "012296") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  1678. if (strcmp(code, "012297") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  1679. if (strcmp(code, "012298") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  1680. }
  1681. void ReleaseAlarmCode(byte gunIndex)
  1682. {
  1683. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1684. {
  1685. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  1686. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  1687. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  1688. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  1689. }
  1690. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1691. {
  1692. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  1693. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  1694. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  1695. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  1696. }
  1697. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1698. {
  1699. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  1700. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  1701. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  1702. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  1703. }
  1704. }
  1705. void CleanAlarmCodeByString(char *code)
  1706. {
  1707. if (strcmp(code, "012296") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  1708. if (strcmp(code, "012297") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  1709. if (strcmp(code, "012298") == 0) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  1710. }
  1711. //===============================================
  1712. // EmergencyStop and Charging Stop
  1713. //===============================================
  1714. void ChargingTerminalProcess(byte gunIndex)
  1715. {
  1716. setChargerMode(gunIndex, MODE_TERMINATING);
  1717. }
  1718. void AcChargingTerminalProcess()
  1719. {
  1720. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  1721. }
  1722. void StopChargingProcessByString(byte level)
  1723. {
  1724. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  1725. {
  1726. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  1727. }
  1728. }
  1729. void ReleaseChargingProcessByString(byte level)
  1730. {
  1731. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  1732. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  1733. }
  1734. // 一般錯誤停止充電處理函式
  1735. void BoardErrOccurByString(byte index, char *code)
  1736. {
  1737. byte level = 1;
  1738. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1739. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1740. {
  1741. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  1742. {
  1743. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1744. }
  1745. ChargingTerminalProcess(index);
  1746. }
  1747. StopChargingProcessByString(level);
  1748. }
  1749. // 急停狀況的停止充電處理函式
  1750. void EmcOccureByString(char *code)
  1751. {
  1752. byte level = 2;
  1753. // 嚴重的急停有四種 : EMC 按鈕、Mainbreak、Dooropen、SPD Trip
  1754. // 其錯誤等級為 2
  1755. if (strncmp(code, "012251", 6) == EQUAL || strncmp(code, "012252", 6) == EQUAL ||
  1756. strncmp(code, "012237", 6) == EQUAL || strncmp(code, "012238", 6) == EQUAL)
  1757. {
  1758. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  1759. {
  1760. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1761. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1762. {
  1763. ChargingTerminalProcess(gun);
  1764. }
  1765. StopChargingProcessByString(level);
  1766. }
  1767. }
  1768. }
  1769. void ReleaseBoardErrOccurByString(byte index, char *code)
  1770. {
  1771. bool isTrigger = false;
  1772. byte level = 1;
  1773. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1774. {
  1775. isTrigger = true;
  1776. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1777. }
  1778. if (isTrigger)
  1779. {
  1780. ReleaseChargingProcessByString(level);
  1781. }
  1782. }
  1783. void ReleaseEmsOccureByString(byte index, char *code)
  1784. {
  1785. bool isTrigger = false;
  1786. byte level = 2;
  1787. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1788. {
  1789. isTrigger = true;
  1790. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1791. }
  1792. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  1793. {
  1794. isTrigger = true;
  1795. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  1796. }
  1797. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  1798. {
  1799. isTrigger = true;
  1800. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  1801. }
  1802. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  1803. {
  1804. isTrigger = true;
  1805. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1806. }
  1807. if (isTrigger)
  1808. {
  1809. ReleaseChargingProcessByString(level);
  1810. }
  1811. }
  1812. //===============================================
  1813. // 確認硬體 (按鈕) 狀態
  1814. //===============================================
  1815. bool leftBtnPush = false;
  1816. bool rightBtnPush = false;
  1817. void ChkPrimaryStatus()
  1818. {
  1819. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  1820. {
  1821. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  1822. EmcOccureByString("012251");
  1823. }
  1824. else
  1825. ReleaseEmsOccureByString(0, "012251");
  1826. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  1827. {
  1828. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  1829. EmcOccureByString("012238");
  1830. }
  1831. else
  1832. ReleaseEmsOccureByString(0, "012238");
  1833. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  1834. {
  1835. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  1836. EmcOccureByString("012237");
  1837. }
  1838. else
  1839. ReleaseEmsOccureByString(0, "012237");
  1840. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  1841. {
  1842. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  1843. EmcOccureByString("012252");
  1844. }
  1845. else
  1846. ReleaseEmsOccureByString(0, "012252");
  1847. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  1848. {
  1849. if(!leftBtnPush)
  1850. {
  1851. leftBtnPush = true;
  1852. PRINTF_FUNC("left btn down............................... \n");
  1853. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1854. {
  1855. case S_IDLE:
  1856. {
  1857. if(isDetectPlugin())
  1858. {
  1859. _DetectPlugInTimeout();
  1860. StopSystemTimeoutDet();
  1861. }
  1862. }
  1863. break;
  1864. case S_PREPARNING:
  1865. case S_PREPARING_FOR_EV:
  1866. case S_PREPARING_FOR_EVSE:
  1867. case S_CCS_PRECHARGE_ST0:
  1868. case S_CCS_PRECHARGE_ST1:
  1869. {
  1870. // 取消充電
  1871. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1872. }
  1873. break;
  1874. case S_CHARGING:
  1875. {
  1876. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  1877. {
  1878. // 停止充電
  1879. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1880. }
  1881. }
  1882. break;
  1883. case S_COMPLETE:
  1884. {
  1885. // 回 IDLE
  1886. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  1887. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  1888. }
  1889. break;
  1890. }
  1891. }
  1892. }
  1893. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  1894. {
  1895. if(leftBtnPush)
  1896. {
  1897. leftBtnPush = false;
  1898. PRINTF_FUNC("left btn up............................... \n");
  1899. }
  1900. }
  1901. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  1902. {
  1903. if(!rightBtnPush)
  1904. {
  1905. rightBtnPush = true;
  1906. PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1907. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  1908. {
  1909. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  1910. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1911. }
  1912. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1913. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  1914. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1915. else
  1916. {
  1917. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1918. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1919. }
  1920. }
  1921. }
  1922. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  1923. {
  1924. if(rightBtnPush)
  1925. {
  1926. rightBtnPush = false;
  1927. PRINTF_FUNC("right btn up............................... \n");
  1928. }
  1929. }
  1930. }
  1931. //===============================================
  1932. // 確認各小板偵測的錯誤狀況
  1933. //===============================================
  1934. void CheckErrorOccurStatus(byte index)
  1935. {
  1936. // 小板
  1937. if (chargingInfo[index]->Type == _Type_Chademo)
  1938. {
  1939. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  1940. BoardErrOccurByString(index, "011012");
  1941. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1942. BoardErrOccurByString(index, "012234");
  1943. }
  1944. else if (chargingInfo[index]->Type == _Type_GB)
  1945. {
  1946. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES)
  1947. BoardErrOccurByString(index, "011016");
  1948. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  1949. BoardErrOccurByString(index, "012236");
  1950. }
  1951. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1952. {
  1953. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  1954. BoardErrOccurByString(index, "011014");
  1955. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1956. BoardErrOccurByString(index, "012235");
  1957. }
  1958. // RB
  1959. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  1960. {
  1961. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  1962. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  1963. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  1964. {
  1965. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  1966. {
  1967. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  1968. StopChargingProcessByString(2);
  1969. }
  1970. }
  1971. else
  1972. {
  1973. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  1974. {
  1975. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1976. ReleaseChargingProcessByString(2);
  1977. }
  1978. }
  1979. }
  1980. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  1981. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  1982. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  1983. {
  1984. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  1985. {
  1986. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  1987. StopChargingProcessByString(2);
  1988. }
  1989. }
  1990. else
  1991. {
  1992. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  1993. {
  1994. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1995. ReleaseChargingProcessByString(2);
  1996. }
  1997. }
  1998. }
  1999. //===============================================
  2000. // 確認 GPIO 狀態
  2001. //===============================================
  2002. void gpio_set_value(unsigned int gpio, unsigned int value)
  2003. {
  2004. int fd;
  2005. char buf[MAX_BUF];
  2006. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2007. fd = open(buf, O_WRONLY);
  2008. if (fd < 0)
  2009. {
  2010. perror("gpio/set-value");
  2011. return;
  2012. }
  2013. if (value)
  2014. write(fd, "1", 2);
  2015. else
  2016. write(fd, "0", 2);
  2017. close(fd);
  2018. }
  2019. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2020. {
  2021. int fd;
  2022. char buf[MAX_BUF];
  2023. char ch;
  2024. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2025. fd = open(buf, O_RDONLY);
  2026. if (fd < 0) {
  2027. perror("gpio/get-value");
  2028. return fd;
  2029. }
  2030. read(fd, &ch, 1);
  2031. if (ch != '0') {
  2032. *value = 1;
  2033. } else {
  2034. *value = 0;
  2035. }
  2036. close(fd);
  2037. return 0;
  2038. }
  2039. void CheckGunTypeFromHw()
  2040. {
  2041. int pinIn[4] = { 22, 23, 44, 45 };
  2042. unsigned int gpioValue = 0;
  2043. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2044. gpio_get_value(pinIn[i], &gpioValue);
  2045. {
  2046. switch (pinIn[i])
  2047. {
  2048. case 22:
  2049. bd1_1_status = gpioValue;
  2050. break;
  2051. case 23:
  2052. bd1_2_status = gpioValue;
  2053. break;
  2054. case 44:
  2055. bd0_1_status = gpioValue;
  2056. break;
  2057. case 45:
  2058. bd0_2_status = gpioValue;
  2059. break;
  2060. }
  2061. }
  2062. }
  2063. }
  2064. void CheckGpioInStatus()
  2065. {
  2066. int pinIn[2] = { 27, 47 };
  2067. unsigned int gpioValue = 0;
  2068. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2069. {
  2070. gpio_get_value(pinIn[i], &gpioValue);
  2071. if (gpioValue == 0x01)
  2072. {
  2073. switch(pinIn[i])
  2074. {
  2075. // 小板緊急停止
  2076. case 47:
  2077. {
  2078. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2079. {
  2080. if (chargingInfo[i]->slotsIndex == 1)
  2081. {
  2082. if (chargingInfo[i]->Type == _Type_Chademo)
  2083. BoardErrOccurByString(i, "023730");
  2084. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2085. BoardErrOccurByString(i, "013627");
  2086. break;
  2087. }
  2088. }
  2089. }
  2090. break;
  2091. case 27:
  2092. {
  2093. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2094. {
  2095. if (chargingInfo[i]->slotsIndex == 3)
  2096. {
  2097. if (chargingInfo[i]->Type == _Type_Chademo)
  2098. BoardErrOccurByString(i, "023730");
  2099. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2100. BoardErrOccurByString(i, "013627");
  2101. break;
  2102. }
  2103. }
  2104. }
  2105. break;
  2106. }
  2107. }
  2108. else
  2109. {
  2110. switch (pinIn[i])
  2111. {
  2112. // 小板解除緊急停止
  2113. case 47:
  2114. {
  2115. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2116. {
  2117. if (chargingInfo[i]->slotsIndex == 1)
  2118. {
  2119. if (chargingInfo[i]->Type == _Type_Chademo)
  2120. ReleaseBoardErrOccurByString(i, "023730");
  2121. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2122. ReleaseBoardErrOccurByString(i, "013627");
  2123. break;
  2124. }
  2125. }
  2126. }
  2127. break;
  2128. case 27:
  2129. {
  2130. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2131. {
  2132. if (chargingInfo[i]->slotsIndex == 3)
  2133. {
  2134. if (chargingInfo[i]->Type == _Type_Chademo)
  2135. ReleaseBoardErrOccurByString(i, "023730");
  2136. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2137. ReleaseBoardErrOccurByString(i, "013627");
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. break;
  2143. }
  2144. }
  2145. }
  2146. }
  2147. //===============================================
  2148. // Main process
  2149. //===============================================
  2150. // 檢查 Byte 中某個 Bit 的值
  2151. // _byte : 欲改變的 byte
  2152. // _bit : 該 byte 的第幾個 bit
  2153. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2154. {
  2155. return ( _byte & mask_table[_bit] ) != 0x00;
  2156. }
  2157. // 設定 Byte 中某個 Bit的值
  2158. // _byte : 欲改變的 byte
  2159. // _bit : 該 byte 的第幾個 bit
  2160. // value : 修改的值為 0 or 1
  2161. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2162. {
  2163. if(value == 1)
  2164. *_byte |= (1 << _bit);
  2165. else if (value == 0)
  2166. *_byte ^= (1 << _bit);
  2167. }
  2168. void UserScanFunction()
  2169. {
  2170. bool idleReq = false;
  2171. unsigned char stopReq = 255;
  2172. // 當前非驗證的狀態
  2173. if(!IsAuthorizingMode())
  2174. {
  2175. // 先判斷現在是否可以提供刷卡
  2176. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2177. // 2. 停止充電
  2178. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2179. {
  2180. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX ||
  2181. (chargingInfo[i]->SystemStatus >= S_AUTHORIZING && chargingInfo[i]->SystemStatus <= S_PREPARNING) ||
  2182. (chargingInfo[i]->SystemStatus >= S_TERMINATING && chargingInfo[i]->SystemStatus < S_COMPLETE))
  2183. {
  2184. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2185. return;
  2186. }
  2187. if (chargingInfo[i]->SystemStatus == S_CHARGING)
  2188. {
  2189. stopReq = i;
  2190. }
  2191. if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable) == YES ||
  2192. (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable))
  2193. {
  2194. idleReq = true;
  2195. }
  2196. }
  2197. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2198. ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  2199. {
  2200. stopReq = DEFAULT_AC_INDEX;
  2201. }
  2202. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  2203. {
  2204. if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2205. {
  2206. char value[32];
  2207. PRINTF_FUNC("ac stop charging \n");
  2208. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2209. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2210. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2211. ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2212. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2213. {
  2214. AcChargingTerminalProcess();
  2215. }
  2216. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2217. }
  2218. else if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2219. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2220. (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))
  2221. {
  2222. char value[32];
  2223. PRINTF_FUNC("stop charging \n");
  2224. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2225. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2226. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2227. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2228. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2229. {
  2230. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2231. }
  2232. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2233. }
  2234. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_COMPLETE)
  2235. {
  2236. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2237. }
  2238. else if (idleReq)
  2239. {
  2240. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && stopReq != 255 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2241. {
  2242. idleReq = false;
  2243. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2244. }
  2245. else
  2246. {
  2247. PRINTF_FUNC("// LCM => Authorizing \n");
  2248. // LCM => Authorizing
  2249. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2250. // 進入確認卡號狀態
  2251. AuthorizingStart();
  2252. }
  2253. }
  2254. else
  2255. {
  2256. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2257. }
  2258. }
  2259. }
  2260. }
  2261. unsigned char isModeChange(unsigned char gun_index)
  2262. {
  2263. unsigned char result = NO;
  2264. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  2265. {
  2266. result = YES;
  2267. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2268. }
  2269. return result;
  2270. }
  2271. void ScannerCardProcess()
  2272. {
  2273. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2274. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2275. {
  2276. isCardScan = true;
  2277. // 處理刷卡及驗證卡號的動作
  2278. UserScanFunction();
  2279. }
  2280. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  2281. {
  2282. StartSystemTimeoutDet(Timeout_Authorizing);
  2283. // 確認驗證卡號完成沒
  2284. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2285. {
  2286. StopSystemTimeoutDet();
  2287. // 判斷後台回覆狀態
  2288. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2289. {
  2290. // LCM => Authorize complete
  2291. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2292. }
  2293. else
  2294. {
  2295. // LCM => Authorize fail
  2296. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2297. }
  2298. ClearAuthorizedFlag();
  2299. }
  2300. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2301. {
  2302. // 白名單驗證
  2303. for (int i = 0; i < 10; i++)
  2304. {
  2305. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2306. {
  2307. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2308. {
  2309. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2310. ClearAuthorizedFlag();
  2311. break;
  2312. }
  2313. }
  2314. }
  2315. }
  2316. }
  2317. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2318. {
  2319. StartSystemTimeoutDet(Timeout_VerifyFail);
  2320. isCardScan = false;
  2321. }
  2322. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2323. {
  2324. StartSystemTimeoutDet(Timeout_VerifyComp);
  2325. }
  2326. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2327. {
  2328. StartSystemTimeoutDet(Timeout_WaitPlug);
  2329. }
  2330. else
  2331. isCardScan = false;
  2332. }
  2333. bool AddGunInfoByConnector(byte typeValue, byte slots)
  2334. {
  2335. bool result = true;
  2336. switch (typeValue)
  2337. {
  2338. case '0': // none
  2339. break;
  2340. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  2341. break;
  2342. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  2343. break;
  2344. case '3': // IEC 62196-2 Type 2 Plug
  2345. break;
  2346. case '4': // IEC 62196-2 Type 2 Socket
  2347. if (AC_QUANTITY > _ac_Index)
  2348. {
  2349. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  2350. // AC 固定 index
  2351. ac_chargingInfo[_acgunIndex]->Index = 1;
  2352. ac_chargingInfo[_acgunIndex]->SystemStatus = S_BOOTING;
  2353. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  2354. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  2355. _ac_Index++;
  2356. _acgunIndex++;
  2357. }
  2358. else
  2359. result = false;
  2360. break;
  2361. case '5': // GB/T AC Plug
  2362. break;
  2363. case '6': // GB/T AC Socket
  2364. break;
  2365. case 'J': // CHAdeMO
  2366. {
  2367. if (CHAdeMO_QUANTITY > _chademoIndex)
  2368. {
  2369. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  2370. chargingInfo[_gunIndex]->Index = _gunIndex;
  2371. chargingInfo[_gunIndex]->slotsIndex = slots;
  2372. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2373. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  2374. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  2375. chargingInfo[_gunIndex]->IsAvailable = YES;
  2376. _chademoIndex++;
  2377. _gunIndex++;
  2378. }
  2379. else
  2380. result = false;
  2381. }
  2382. break;
  2383. case 'U': // CCS1 combo
  2384. case 'E': // CCS2 combo
  2385. {
  2386. if (CCS_QUANTITY > _ccsIndex)
  2387. {
  2388. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  2389. chargingInfo[_gunIndex]->Index = _gunIndex;
  2390. chargingInfo[_gunIndex]->slotsIndex = slots;
  2391. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2392. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  2393. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  2394. chargingInfo[_gunIndex]->IsAvailable = YES;
  2395. // 現階段預設為走 DIN70121
  2396. ShmCcsData->CommProtocol = 0x01;
  2397. _ccsIndex++;
  2398. _gunIndex++;
  2399. }
  2400. else
  2401. result = false;
  2402. }
  2403. break;
  2404. case 'G': // GBT DC
  2405. {
  2406. if (GB_QUANTITY > _gb_Index)
  2407. {
  2408. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  2409. chargingInfo[_gunIndex]->Index = _gunIndex;
  2410. chargingInfo[_gunIndex]->slotsIndex = slots;
  2411. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2412. chargingInfo[_gunIndex]->Type = _Type_GB;
  2413. chargingInfo[_gunIndex]->type_index = _gb_Index;
  2414. chargingInfo[_gunIndex]->IsAvailable = YES;
  2415. _gb_Index++;
  2416. _gunIndex++;
  2417. }
  2418. else
  2419. result = false;
  2420. }
  2421. break;
  2422. case 'D': // GBT DC x 2
  2423. break;
  2424. }
  2425. return result;
  2426. }
  2427. bool CheckConnectorTypeStatus()
  2428. {
  2429. bool result = true;
  2430. PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  2431. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  2432. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  2433. {
  2434. byte slots = 1;
  2435. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  2436. {
  2437. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  2438. {
  2439. return false;
  2440. }
  2441. slots++;
  2442. }
  2443. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  2444. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  2445. PRINTF_FUNC("DC _gunCount = %d, AC _gunCount = %d \n",
  2446. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  2447. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  2448. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  2449. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  2450. result = false;
  2451. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2452. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2453. {
  2454. if (bd0_1_status == 0 && bd0_2_status == 1)
  2455. {
  2456. // 與硬體相同 type : Chademo
  2457. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2458. {
  2459. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2460. }
  2461. }
  2462. else if (bd0_1_status == 1 && bd0_2_status == 0)
  2463. {
  2464. // 與硬體相同 type : CCS
  2465. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2466. {
  2467. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2468. }
  2469. }
  2470. else if (bd0_1_status == 1 && bd0_2_status == 1)
  2471. {
  2472. // 與硬體相同 type : GB
  2473. if (chargingInfo[gunIndex]->Type == _Type_GB)
  2474. {
  2475. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2476. }
  2477. }
  2478. if (bd1_1_status == 0 && bd1_2_status == 1)
  2479. {
  2480. // 與硬體相同 type : Chademo
  2481. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2482. {
  2483. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2484. }
  2485. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2486. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2487. }
  2488. else if (bd1_1_status == 1 && bd1_2_status == 0)
  2489. {
  2490. // 與硬體相同 type : CCS
  2491. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  2492. {
  2493. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2494. }
  2495. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2496. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2497. }
  2498. else if (bd1_1_status == 1 && bd1_2_status == 1)
  2499. {
  2500. // 與硬體相同 type : GB
  2501. if (chargingInfo[gunIndex]->Type == _Type_GB)
  2502. {
  2503. chargingInfo[gunIndex]->Evboard_id = 0x02;
  2504. }
  2505. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2506. chargingInfo[gunIndex]->Evboard_id = 0x01;
  2507. }
  2508. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  2509. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  2510. result = false;
  2511. }
  2512. }
  2513. else
  2514. {
  2515. // Module Name 不正確 - 告警
  2516. result = false;
  2517. }
  2518. return result;
  2519. }
  2520. void KillTask()
  2521. {
  2522. ChangeLcmByIndex(_LCM_FIX);
  2523. system("killall Module_EventLogging");
  2524. system("killall Module_PrimaryComm");
  2525. system("killall Module_EvComm");
  2526. system("killall Module_LcmControl");
  2527. system("killall Module_InternalComm");
  2528. system("killall Module_PsuComm");
  2529. system("killall OcppBackend &");
  2530. system("killall Module_4g &");
  2531. system("killall Module_Wifi &");
  2532. }
  2533. char CheckUpdateProcess()
  2534. {
  2535. DIR *d;
  2536. struct dirent *dir;
  2537. d = opendir("/mnt/");
  2538. if (d)
  2539. {
  2540. long int MaxLen=48*1024*1024, ImageLen = 0;
  2541. while ((dir = readdir(d)) != NULL)
  2542. {
  2543. char *new_str;
  2544. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  2545. new_str[0] = '\0';
  2546. strcat(new_str, "/mnt/");
  2547. strcat(new_str, dir->d_name);
  2548. int fd = open(new_str, O_RDONLY);
  2549. if (fd < 0)
  2550. {
  2551. return FAIL;
  2552. }
  2553. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2554. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2555. //get the image length
  2556. ImageLen = read(fd, ptr, MaxLen);
  2557. if (ImageLen > 20)
  2558. {
  2559. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  2560. PRINTF_FUNC("Typed...%x \r\n", Type);
  2561. switch (Type)
  2562. {
  2563. case 0x10000001:
  2564. case 0x10000002:
  2565. case 0x10000003:
  2566. case 0x10000004:
  2567. case 0x10000005:
  2568. {
  2569. if (Upgrade_Flash(Type, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2570. return PASS;
  2571. else
  2572. return FAIL;
  2573. }
  2574. break;
  2575. case 0x10000007:
  2576. case 0x10000008:
  2577. case 0x10000009:
  2578. case 0x1000000A:
  2579. {
  2580. }
  2581. break;
  2582. case 0x10000006:
  2583. case 0x1000000D:
  2584. case 0x1000000E:
  2585. case 0x20000002:
  2586. {
  2587. // CSU_PRIMARY_CONTROLLER : 0x10000006
  2588. byte target = 0x00;
  2589. if (Type == 0x10000006)
  2590. target = UPGRADE_PRI;
  2591. else if (Type == 0x1000000D)
  2592. target = UPGRADE_RB;
  2593. else if (Type == 0x1000000E)
  2594. target = UPGRADE_FAN;
  2595. else if (Type == 0x20000002)
  2596. target = UPGRADE_AC;
  2597. int fd = InitComPort(target);
  2598. if (Upgrade_UART(fd, Type, target, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2599. return PASS;
  2600. else
  2601. return FAIL;
  2602. close(fd);
  2603. }
  2604. break;
  2605. case 0x1000000B:
  2606. case 0x1000000C:
  2607. {
  2608. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  2609. bool isPass = true;
  2610. int CanFd = InitCanBus();
  2611. if (CanFd > 0)
  2612. {
  2613. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2614. {
  2615. if (!isPass)
  2616. break;
  2617. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  2618. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB))
  2619. {
  2620. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2621. return PASS;
  2622. else
  2623. return FAIL;
  2624. }
  2625. }
  2626. }
  2627. else
  2628. {
  2629. PRINTF_FUNC("Upgrad FD fail. \n");
  2630. isPass = false;
  2631. }
  2632. return isPass;
  2633. break;
  2634. }
  2635. }
  2636. }
  2637. free(new_str);
  2638. free(ptr);
  2639. }
  2640. }
  2641. free(dir);
  2642. closedir(d);
  2643. return FAIL;
  2644. }
  2645. void CreateRfidFork()
  2646. {
  2647. pid_t rfidRecPid;
  2648. rfidRecPid = fork();
  2649. if (rfidRecPid == 0)
  2650. {
  2651. while(true)
  2652. {
  2653. // 刷卡判斷
  2654. RFID rfid;
  2655. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  2656. !ShmSysConfigAndInfo->SysConfig.isRFID)
  2657. {}
  2658. else if(getRequestCardSN(rfidFd, 0, &rfid))
  2659. {
  2660. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2661. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  2662. {
  2663. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  2664. {
  2665. switch (rfid.snType)
  2666. {
  2667. case RFID_SN_TYPE_6BYTE:
  2668. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2669. "%02X%02X%02X%02X%02X%02X",
  2670. rfid.currentCard[0], rfid.currentCard[1],
  2671. rfid.currentCard[2], rfid.currentCard[3],
  2672. rfid.currentCard[4], rfid.currentCard[5]);
  2673. break;
  2674. case RFID_SN_TYPE_7BYTE:
  2675. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2676. "%02X%02X%02X%02X%02X%02X%02X",
  2677. rfid.currentCard[0], rfid.currentCard[1],
  2678. rfid.currentCard[2], rfid.currentCard[3],
  2679. rfid.currentCard[4], rfid.currentCard[5],
  2680. rfid.currentCard[6]);
  2681. break;
  2682. case RFID_SN_TYPE_10BYTE:
  2683. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2684. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2685. rfid.currentCard[0], rfid.currentCard[1],
  2686. rfid.currentCard[2], rfid.currentCard[3],
  2687. rfid.currentCard[4], rfid.currentCard[5],
  2688. rfid.currentCard[6], rfid.currentCard[7],
  2689. rfid.currentCard[8], rfid.currentCard[9]);
  2690. break;
  2691. case RFID_SN_TYPE_4BYTE:
  2692. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2693. "%02X%02X%02X%02X",
  2694. rfid.currentCard[0], rfid.currentCard[1],
  2695. rfid.currentCard[2], rfid.currentCard[3]);
  2696. break;
  2697. }
  2698. }
  2699. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  2700. {
  2701. switch (rfid.snType)
  2702. {
  2703. case RFID_SN_TYPE_6BYTE:
  2704. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2705. "%02X%02X%02X%02X%02X%02X",
  2706. rfid.currentCard[5], rfid.currentCard[4],
  2707. rfid.currentCard[3], rfid.currentCard[2],
  2708. rfid.currentCard[1], rfid.currentCard[0]);
  2709. break;
  2710. case RFID_SN_TYPE_7BYTE:
  2711. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2712. "%02X%02X%02X%02X%02X%02X%02X",
  2713. rfid.currentCard[6], rfid.currentCard[5],
  2714. rfid.currentCard[4], rfid.currentCard[3],
  2715. rfid.currentCard[2], rfid.currentCard[1],
  2716. rfid.currentCard[0]);
  2717. break;
  2718. case RFID_SN_TYPE_10BYTE:
  2719. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2720. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  2721. rfid.currentCard[9], rfid.currentCard[8],
  2722. rfid.currentCard[7], rfid.currentCard[6],
  2723. rfid.currentCard[5], rfid.currentCard[4],
  2724. rfid.currentCard[3], rfid.currentCard[2],
  2725. rfid.currentCard[1], rfid.currentCard[0]);
  2726. break;
  2727. case RFID_SN_TYPE_4BYTE:
  2728. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  2729. "%02X%02X%02X%02X",
  2730. rfid.currentCard[3], rfid.currentCard[2],
  2731. rfid.currentCard[1], rfid.currentCard[0]);
  2732. break;
  2733. }
  2734. }
  2735. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2736. }
  2737. }
  2738. sleep(1);
  2739. }
  2740. }
  2741. }
  2742. void StartSystemTimeoutDet(unsigned char flag)
  2743. {
  2744. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  2745. {
  2746. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2747. }
  2748. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  2749. }
  2750. void StopSystemTimeoutDet()
  2751. {
  2752. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2753. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  2754. }
  2755. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  2756. {
  2757. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2758. {
  2759. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  2760. {
  2761. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  2762. }
  2763. chargingInfo[gunIndex]->TimeoutFlag = flag;
  2764. }
  2765. }
  2766. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  2767. {
  2768. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2769. {
  2770. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  2771. }
  2772. }
  2773. void CreateTimeoutFork()
  2774. {
  2775. pid_t timeoutPid;
  2776. timeoutPid = fork();
  2777. if (timeoutPid > 0)
  2778. {
  2779. while(true)
  2780. {
  2781. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  2782. // 系統
  2783. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  2784. {
  2785. case Timeout_SelftestChk:
  2786. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2787. {
  2788. _SelfTestTimeout();
  2789. StopSystemTimeoutDet();
  2790. }
  2791. break;
  2792. case Timeout_Authorizing:
  2793. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 30000000)
  2794. {
  2795. _AuthorizedTimeout();
  2796. StopSystemTimeoutDet();
  2797. }
  2798. break;
  2799. case Timeout_VerifyFail:
  2800. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2801. {
  2802. _AutoReturnTimeout();
  2803. StopSystemTimeoutDet();
  2804. }
  2805. break;
  2806. case Timeout_VerifyComp:
  2807. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2808. {
  2809. _AutoReturnTimeout();
  2810. StopSystemTimeoutDet();
  2811. }
  2812. break;
  2813. case Timeout_WaitPlug:
  2814. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2815. {
  2816. _DetectPlugInTimeout();
  2817. StopSystemTimeoutDet();
  2818. }
  2819. break;
  2820. case Timeout_ReturnToChargingGunDet:
  2821. {
  2822. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 30000000)
  2823. {
  2824. DisplayChargingInfo();
  2825. StopSystemTimeoutDet();
  2826. }
  2827. }
  2828. break;
  2829. }
  2830. // 各槍
  2831. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2832. {
  2833. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  2834. switch(chargingInfo[gun_index]->TimeoutFlag)
  2835. {
  2836. case Timeout_Preparing:
  2837. {
  2838. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 30000000)
  2839. {
  2840. _PrepareTimeout(gun_index);
  2841. StopGunInfoTimeoutDet(gun_index);
  2842. }
  2843. }
  2844. break;
  2845. case Timeout_EvChargingDet:
  2846. {
  2847. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
  2848. {
  2849. _DetectEvChargingEnableTimeout(gun_index);
  2850. StopGunInfoTimeoutDet(gun_index);
  2851. }
  2852. }
  2853. break;
  2854. case Timeout_EvseChargingDet:
  2855. {
  2856. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2857. {
  2858. _DetectEvseChargingEnableTimeout(gun_index);
  2859. StopGunInfoTimeoutDet(gun_index);
  2860. }
  2861. }
  2862. break;
  2863. case Timeout_EvseCompleteDet:
  2864. {
  2865. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  2866. {
  2867. StopGunInfoTimeoutDet(gun_index);
  2868. }
  2869. }
  2870. break;
  2871. case Timeout_ForCcsPrechargeDet:
  2872. {
  2873. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2874. {
  2875. _CcsPrechargeTimeout(gun_index);
  2876. StopGunInfoTimeoutDet(gun_index);
  2877. }
  2878. }
  2879. break;
  2880. }
  2881. }
  2882. sleep(1);
  2883. }
  2884. }
  2885. }
  2886. void GetSystemTime()
  2887. {
  2888. struct timeb csuTime;
  2889. struct tm *tmCSU;
  2890. ftime(&csuTime);
  2891. tmCSU = localtime(&csuTime.time);
  2892. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  2893. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2894. tmCSU->tm_sec);
  2895. // byte date[14];
  2896. //
  2897. //
  2898. // //sprintf(&date, "%d", );
  2899. //
  2900. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  2901. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  2902. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  2903. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  2904. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  2905. //
  2906. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  2907. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  2908. //
  2909. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  2910. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  2911. //
  2912. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  2913. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  2914. //
  2915. // date[10] = (tmCSU->tm_min) / 10 % 10;
  2916. // date[11] = (tmCSU->tm_min) / 1 % 10;
  2917. //
  2918. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  2919. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  2920. // 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],
  2921. // date[4], date[5], date[6], date[7],
  2922. // date[8], date[9], date[10], date[11],
  2923. // date[12], date[13]);
  2924. }
  2925. void CheckFactoryConfigFunction()
  2926. {
  2927. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  2928. {
  2929. system("cd /root;./FactoryConfig -m");
  2930. system("sync");
  2931. sleep(5);
  2932. system("reboot -f");
  2933. sleep(5);
  2934. system("reboot -f");
  2935. }
  2936. }
  2937. void CheckFwUpdateFunction()
  2938. {
  2939. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  2940. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  2941. {
  2942. DEBUG_INFO_MSG("ftp : update start. \n");
  2943. KillTask();
  2944. if (CheckUpdateProcess() == PASS)
  2945. DEBUG_INFO_MSG("ftp : update complete. \n");
  2946. else
  2947. DEBUG_INFO_MSG("ftp : update fail. \n");
  2948. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  2949. sleep(5);
  2950. system("reboot -f");
  2951. }
  2952. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  2953. {
  2954. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  2955. }
  2956. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  2957. {
  2958. DEBUG_INFO_MSG("Backend : update start. \n");
  2959. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2960. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  2961. KillTask();
  2962. if (CheckUpdateProcess() == PASS)
  2963. {
  2964. DEBUG_INFO_MSG("Backend : update complete. \n");
  2965. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2966. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  2967. }
  2968. else
  2969. {
  2970. DEBUG_INFO_MSG("Backend : update fail. \n");
  2971. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2972. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  2973. }
  2974. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  2975. sleep(5);
  2976. system("reboot -f");
  2977. }
  2978. else if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == EQUAL)
  2979. {
  2980. DEBUG_ERROR_MSG("Backend to download file fail. \n");
  2981. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  2982. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  2983. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  2984. }
  2985. }
  2986. //===============================================
  2987. // Check reservation date is expired
  2988. //===============================================
  2989. int isReservationExpired(unsigned char gun_index)
  2990. {
  2991. int result = NO;
  2992. struct tm expiredDate;
  2993. struct timeb expiredTime;
  2994. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  2995. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2996. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2997. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  2998. {
  2999. expiredDate.tm_year -= 1900;
  3000. expiredDate.tm_mon -= 1;
  3001. expiredTime.time = mktime(&expiredDate);
  3002. if (!CheckTimeOut(expiredTime))
  3003. {
  3004. result = YES;
  3005. }
  3006. }
  3007. return result;
  3008. }
  3009. //===============================================
  3010. // OCPP
  3011. //===============================================
  3012. void CheckOcppStatus()
  3013. {
  3014. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  3015. {
  3016. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = YES;
  3017. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  3018. }
  3019. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  3020. {
  3021. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  3022. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  3023. {
  3024. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  3025. sleep(3);
  3026. system("reboot -f");
  3027. }
  3028. else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  3029. {
  3030. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  3031. sleep(3);
  3032. KillTask();
  3033. system("/usr/bin/run_evse_restart.sh");
  3034. }
  3035. }
  3036. }
  3037. void OcppStartTransation(byte gunIndex)
  3038. {
  3039. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3040. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  3041. else
  3042. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3043. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  3044. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionReq = YES;
  3045. }
  3046. void OcppStopTransation(byte gunIndex)
  3047. {
  3048. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3049. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  3050. else
  3051. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3052. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  3053. ShmOCPP16Data->CpMsg.bits[gunIndex].StopTransactionReq = YES;
  3054. }
  3055. bool OcppRemoteStop(byte gunIndex)
  3056. {
  3057. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  3058. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  3059. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  3060. return result;
  3061. }
  3062. void OcppRemoteStartChk()
  3063. {
  3064. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3065. {}
  3066. else if(!isDetectPlugin())
  3067. {
  3068. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3069. {
  3070. if ((chargingInfo[gun_index]->SystemStatus == S_IDLE || chargingInfo[gun_index]->SystemStatus == S_RESERVATION)&&
  3071. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES)
  3072. {
  3073. ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  3074. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = NO;
  3075. DetectPluginStart();
  3076. break;
  3077. }
  3078. }
  3079. }
  3080. }
  3081. void ChkOcppStatus(byte gunIndex)
  3082. {
  3083. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  3084. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  3085. {
  3086. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  3087. if (isReservationExpired(gunIndex))
  3088. {
  3089. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3090. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3091. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  3092. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  3093. }
  3094. }
  3095. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  3096. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  3097. {
  3098. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  3099. if (isReservationExpired(gunIndex))
  3100. {
  3101. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3102. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3103. chargingInfo[gunIndex]->ReservationId = 0;
  3104. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  3105. }
  3106. }
  3107. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  3108. {
  3109. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3110. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3111. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  3112. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  3113. chargingInfo[gunIndex]->IsAvailable = YES;
  3114. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  3115. chargingInfo[gunIndex]->IsAvailable = NO;
  3116. }
  3117. }
  3118. bool CheckBackendChargingTimeout(byte gunIndex)
  3119. {
  3120. bool result = false;
  3121. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3122. {
  3123. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  3124. {
  3125. if (chargingInfo[gunIndex]->RemainChargingDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  3126. result = true;
  3127. }
  3128. }
  3129. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3130. {
  3131. // 隨插即充電的要看 offline
  3132. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  3133. {
  3134. if (chargingInfo[gunIndex]->RemainChargingDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  3135. result = true;
  3136. }
  3137. }
  3138. return result;
  3139. }
  3140. bool CheckBackendChargingEnergy(byte gunIndex)
  3141. {
  3142. bool result = false;
  3143. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3144. {
  3145. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  3146. {
  3147. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  3148. result = true;
  3149. }
  3150. }
  3151. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3152. {
  3153. // 隨插即充電的要看 offline
  3154. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  3155. {
  3156. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  3157. result = true;
  3158. }
  3159. }
  3160. return result;
  3161. }
  3162. //===============================================
  3163. // Config process
  3164. //===============================================
  3165. void AddPlugInTimes(byte gunIndex)
  3166. {
  3167. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3168. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  3169. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3170. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  3171. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  3172. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  3173. }
  3174. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  3175. {
  3176. char cmdBuf[32];
  3177. struct timeb csuTime;
  3178. struct tm *tmCSU;
  3179. ftime(&csuTime);
  3180. tmCSU = localtime(&csuTime.time);
  3181. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  3182. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3183. tmCSU->tm_sec);
  3184. if (isStart)
  3185. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  3186. else
  3187. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  3188. }
  3189. void zipLogFiles()
  3190. {
  3191. const char* logPath = "/Storage/SystemLog";
  3192. // 獲取目錄
  3193. DIR* pDir = opendir(logPath);
  3194. if (pDir != NULL)
  3195. {
  3196. struct timeb csuTime;
  3197. struct tm *tmCSU;
  3198. ftime(&csuTime);
  3199. tmCSU = localtime(&csuTime.time);
  3200. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3201. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3202. // tmCSU->tm_sec);
  3203. // Read items inside the folder
  3204. struct dirent* pEntry = NULL;
  3205. while ((pEntry = readdir(pDir)) != NULL)
  3206. {
  3207. if (strcmp(pEntry->d_name, ".") != 0 &&
  3208. strcmp(pEntry->d_name, "..") != 0 &&
  3209. strncmp(pEntry->d_name, "[", 1) == 0 &&
  3210. strstr(pEntry->d_name, "tar") < 0)
  3211. {
  3212. char yearC[5];
  3213. unsigned short year = 0;
  3214. char monthC[3];
  3215. unsigned short month = 0;
  3216. yearC[4] = '\0';
  3217. strncpy(yearC, pEntry->d_name + 1, 4);
  3218. monthC[2] = '\0';
  3219. strncpy(monthC, pEntry->d_name + 6, 2);
  3220. year = atoi(yearC);
  3221. month = atoi(monthC);
  3222. if (year != 0)
  3223. {
  3224. if (year < tmCSU->tm_year + 1900 ||
  3225. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  3226. {
  3227. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  3228. char file[256];
  3229. memset(file, 0x00, sizeof(file));
  3230. strcat(file, "tar zcvf ");
  3231. strcat(file, logPath);
  3232. strncat(file, "/", 1);
  3233. strcat(file, pEntry->d_name);
  3234. strcat(file, ".tar");
  3235. strncat(file, " ", 1);
  3236. strcat(file, logPath);
  3237. strncat(file, "/", 1);
  3238. strcat(file, pEntry->d_name);
  3239. PRINTF_FUNC("zip = %s \n", file);
  3240. system(file);
  3241. }
  3242. }
  3243. }
  3244. }
  3245. }
  3246. // Close folder
  3247. closedir(pDir);
  3248. }
  3249. void ChangeGunSelectByIndex(byte sel)
  3250. {
  3251. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  3252. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  3253. }
  3254. void CheckIsAlternatvieByModelName()
  3255. {
  3256. // 黑白機 ?
  3257. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  3258. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL)
  3259. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  3260. else
  3261. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  3262. }
  3263. void StopProcessingLoop()
  3264. {
  3265. for (;;)
  3266. {
  3267. CheckFactoryConfigFunction();
  3268. CheckFwUpdateFunction();
  3269. sleep(1);
  3270. }
  3271. }
  3272. void CreateWatchdog()
  3273. {
  3274. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  3275. {
  3276. wtdFd = InitWatchDog();
  3277. if (wtdFd < 0)
  3278. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  3279. }
  3280. }
  3281. bool IsConnectorWholeIdle()
  3282. {
  3283. bool result = true;
  3284. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  3285. {
  3286. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  3287. chargingInfo[count]->SystemStatus != S_RESERVATION)
  3288. {
  3289. result = false;
  3290. break;
  3291. }
  3292. }
  3293. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  3294. {
  3295. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  3296. ac_chargingInfo[count]->IsErrorOccur == NO)
  3297. {
  3298. result = false;
  3299. break;
  3300. }
  3301. }
  3302. return result;
  3303. }
  3304. int main(void)
  3305. {
  3306. if(CreateShareMemory() == 0)
  3307. {
  3308. #ifdef SystemLogMessage
  3309. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  3310. #endif
  3311. if(ShmStatusCodeData!=NULL)
  3312. {
  3313. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  3314. }
  3315. return 0;
  3316. sleep(5);
  3317. system("reboot -f");
  3318. sleep(5);
  3319. system("reboot -f");
  3320. }
  3321. PRINTF_FUNC("Chademo = %d, CCS = %d, GB = %d, AC = %d \n",
  3322. CHAdeMO_QUANTITY, CCS_QUANTITY, GB_QUANTITY, AC_QUANTITY);
  3323. PRINTF_FUNC("\n"); PRINTF_FUNC("Initial SystemConfig and Info.......\n");
  3324. if (!InitialSystemDefaultConfig())
  3325. {
  3326. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  3327. StopProcessingLoop();
  3328. }
  3329. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  3330. CheckGunTypeFromHw();
  3331. PRINTF_FUNC("CheckIsAlternatvieByModelName. \n");
  3332. CheckIsAlternatvieByModelName();
  3333. PRINTF_FUNC("ModelName = %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  3334. PRINTF_FUNC("InitialShareMemoryInfo \n");
  3335. InitialShareMemoryInfo();
  3336. PRINTF_FUNC("ChargerType (IEC or UL) = %d \n", ShmSysConfigAndInfo->SysInfo.ChargerType);
  3337. ChangeLcmByIndex(_LCM_INIT);
  3338. if (!CheckConnectorTypeStatus())
  3339. isModelNameMatch = false;
  3340. Initialization();
  3341. PRINTF_FUNC("Spawn all Task. \n");
  3342. SpawnTask();
  3343. if (!isModelNameMatch)
  3344. {
  3345. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3346. ChangeLcmByIndex(_LCM_FIX);
  3347. // Module Name 與硬體對應不正確
  3348. PRINTF_FUNC("Module Name & HW info none match. \n");
  3349. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  3350. sleep(3);
  3351. KillTask();
  3352. StopProcessingLoop();
  3353. }
  3354. PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
  3355. CreateTimeoutFork();
  3356. PRINTF_FUNC("Self test. \n");
  3357. SelfTestRun();
  3358. StopSystemTimeoutDet();
  3359. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  3360. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  3361. ShmPsuData->Work_Step == _NO_WORKING)
  3362. {
  3363. DisplaySelfTestFailReason();
  3364. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3365. {
  3366. setChargerMode(gun_index, MODE_ALARM);
  3367. }
  3368. ChangeLcmByIndex(_LCM_FIX);
  3369. sleep(3);
  3370. KillTask();
  3371. StopProcessingLoop();
  3372. }
  3373. else
  3374. {
  3375. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3376. {
  3377. setChargerMode(gun_index, MODE_IDLE);
  3378. }
  3379. }
  3380. ChangeLcmByIndex(_LCM_IDLE);
  3381. sleep(1);
  3382. //***** 須新增的偵測 *****//
  3383. // 1. Thernal - 控制風扇轉速
  3384. // 2. ouput fuse - 控制風扇轉速
  3385. CreateRfidFork();
  3386. // Create Watchdog
  3387. //CreateWatchdog();
  3388. // Main loop
  3389. PRINTF_FUNC("Main Loop. \n");
  3390. for (;;)
  3391. {
  3392. CheckOcppStatus();
  3393. ChkPrimaryStatus();
  3394. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  3395. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3396. {
  3397. CheckFactoryConfigFunction();
  3398. CheckFwUpdateFunction();
  3399. }
  3400. // OCPP 邏輯
  3401. OcppRemoteStartChk();
  3402. // 讀卡邏輯
  3403. ScannerCardProcess();
  3404. if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan)
  3405. {
  3406. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3407. }
  3408. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3409. {
  3410. CheckGpioInStatus();
  3411. CheckErrorOccurStatus(gun_index);
  3412. ChkOcppStatus(gun_index);
  3413. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  3414. switch(chargingInfo[gun_index]->SystemStatus)
  3415. {
  3416. case S_IDLE:
  3417. case S_RESERVATION:
  3418. {
  3419. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  3420. isModeChange(gun_index))
  3421. {
  3422. PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
  3423. chargingInfo[gun_index]->RemainChargingDuration = 0;
  3424. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  3425. }
  3426. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  3427. isModeChange(gun_index))
  3428. {
  3429. PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
  3430. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  3431. }
  3432. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  3433. {
  3434. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  3435. {
  3436. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  3437. }
  3438. ClearDetectPluginFlag();
  3439. }
  3440. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3441. {/* 不給充電 */}
  3442. else
  3443. {
  3444. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
  3445. {
  3446. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3447. }
  3448. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3449. {
  3450. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  3451. {
  3452. // 均充 -> 最大充
  3453. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3454. {
  3455. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  3456. {
  3457. PRINTF_FUNC("=============Smart Charging============= Step 11 \n");
  3458. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3459. }
  3460. }
  3461. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  3462. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  3463. {
  3464. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  3465. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  3466. }
  3467. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  3468. {
  3469. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3470. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3471. }
  3472. }
  3473. else
  3474. {
  3475. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3476. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3477. }
  3478. }
  3479. else
  3480. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3481. { // Idle 正常程序起點
  3482. // 判斷是否有啟用檢查插槍
  3483. if(isDetectPlugin())
  3484. {
  3485. // 卡號驗證成功後,等待充電槍插入充電車
  3486. if ((ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL &&
  3487. ShmSysConfigAndInfo->SysInfo.OrderCharging < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount))
  3488. {
  3489. if (chargingInfo[(unsigned char)ShmSysConfigAndInfo->SysInfo.OrderCharging]->ConnectorPlugIn == YES &&
  3490. chargingInfo[gun_index]->IsAvailable)
  3491. {
  3492. PRINTF_FUNC("-----------------1----------------- %d \n", ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3493. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3494. AddPlugInTimes(ShmSysConfigAndInfo->SysInfo.OrderCharging);
  3495. setChargerMode(ShmSysConfigAndInfo->SysInfo.OrderCharging, MODE_REASSIGN_CHECK);
  3496. ClearDetectPluginFlag();
  3497. continue;
  3498. }
  3499. }
  3500. else
  3501. {
  3502. if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
  3503. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  3504. {
  3505. PRINTF_FUNC("-----------------2----------------- \n");
  3506. ChangeGunSelectByIndex(gun_index);
  3507. AddPlugInTimes(gun_index);
  3508. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  3509. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  3510. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3511. // 當前操作的槍號,進入 Preparing
  3512. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3513. ClearDetectPluginFlag();
  3514. continue;
  3515. }
  3516. }
  3517. if (!isCardScan)
  3518. {
  3519. // LCM => Waiting for plugging
  3520. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  3521. }
  3522. }
  3523. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  3524. {
  3525. if (!isReservationExpired(gun_index))
  3526. {
  3527. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  3528. }
  3529. }
  3530. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  3531. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  3532. {
  3533. PRINTF_FUNC("-----------------3----------------- \n");
  3534. ChangeGunSelectByIndex(gun_index);
  3535. AddPlugInTimes(gun_index);
  3536. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  3537. ClearDetectPluginFlag();
  3538. continue;
  3539. }
  3540. else
  3541. {
  3542. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3543. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  3544. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  3545. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  3546. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  3547. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  3548. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  3549. {
  3550. bool isReturnTimeout = false;
  3551. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  3552. {
  3553. if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  3554. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  3555. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX))
  3556. {
  3557. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  3558. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  3559. {
  3560. isReturnTimeout = true;
  3561. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3562. }
  3563. }
  3564. }
  3565. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  3566. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  3567. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  3568. {
  3569. isReturnTimeout = true;
  3570. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  3571. }
  3572. if (!isReturnTimeout)
  3573. StopSystemTimeoutDet();
  3574. }
  3575. }
  3576. } // Idle 正常程序終點
  3577. }
  3578. ReleaseAlarmCode(gun_index);
  3579. }
  3580. break;
  3581. case S_REASSIGN_CHECK:
  3582. {
  3583. if (isModeChange(gun_index))
  3584. {
  3585. PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
  3586. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3587. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != FAIL)
  3588. ShmSysConfigAndInfo->SysInfo.OrderCharging = FAIL;
  3589. StopSystemTimeoutDet();
  3590. }
  3591. bool isRessign = false;
  3592. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  3593. {
  3594. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  3595. {
  3596. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3597. {
  3598. // 有其他槍已經分配好 psu 模塊
  3599. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  3600. chargingInfo[index]->SystemStatus >= S_PREPARNING)
  3601. {
  3602. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  3603. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  3604. isRessign = true;
  3605. break;
  3606. }
  3607. }
  3608. }
  3609. }
  3610. if (isRessign)
  3611. setChargerMode(gun_index, MODE_REASSIGN);
  3612. else
  3613. setChargerMode(gun_index, MODE_PRECHARGE);
  3614. }
  3615. break;
  3616. case S_REASSIGN:
  3617. {
  3618. if (isModeChange(gun_index))
  3619. {
  3620. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  3621. }
  3622. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  3623. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  3624. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  3625. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  3626. {
  3627. setChargerMode(gun_index, MODE_PRECHARGE);
  3628. }
  3629. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  3630. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  3631. {
  3632. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  3633. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  3634. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  3635. }
  3636. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  3637. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3638. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3639. }
  3640. break;
  3641. case S_PREPARNING:
  3642. {
  3643. if (isModeChange(gun_index))
  3644. {
  3645. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  3646. StopGunInfoTimeoutDet(gun_index);
  3647. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  3648. }
  3649. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10)
  3650. {
  3651. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  3652. }
  3653. if (isEvBoardStopChargeFlag(gun_index) == YES || OcppRemoteStop(gun_index) == YES)
  3654. {
  3655. // 板端或後臺要求停止
  3656. ChargingTerminalProcess(gun_index);
  3657. }
  3658. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3659. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3660. }
  3661. break;
  3662. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  3663. {
  3664. if (isModeChange(gun_index))
  3665. {
  3666. PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  3667. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3668. StopGunInfoTimeoutDet(gun_index);
  3669. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  3670. }
  3671. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3672. {
  3673. // 檢查車端的槍鎖是否為鎖上
  3674. if (isEvGunLocked_chademo(gun_index) == YES)
  3675. {
  3676. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3677. }
  3678. }
  3679. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3680. {
  3681. // 檢查車端的 charging enable 是否為 1
  3682. if (isEvGunLocked_gb(gun_index) == YES)
  3683. {
  3684. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3685. }
  3686. }
  3687. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3688. {
  3689. // 檢查車端的 charging enable 是否為 1
  3690. if (isEvGunLocked_ccs(gun_index) == YES)
  3691. {
  3692. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  3693. }
  3694. }
  3695. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3696. OcppRemoteStop(gun_index) == YES)
  3697. {
  3698. // 板端或後臺要求停止
  3699. ChargingTerminalProcess(gun_index);
  3700. }
  3701. // LCM => Pre-charging
  3702. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3703. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3704. }
  3705. break;
  3706. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  3707. {
  3708. if (isModeChange(gun_index))
  3709. {
  3710. PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  3711. StopGunInfoTimeoutDet(gun_index);
  3712. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  3713. }
  3714. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3715. {
  3716. // 檢查樁端的 GFD 結果
  3717. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  3718. {
  3719. // 當前操作的槍號,進入 Charging
  3720. setChargerMode(gun_index, MODE_CHARGING);
  3721. }
  3722. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3723. {
  3724. // GFD 錯誤停止
  3725. RecordAlarmCode("012234");
  3726. }
  3727. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3728. {
  3729. // GFD 警告
  3730. RecordAlarmCode("012296");
  3731. }
  3732. }
  3733. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3734. {
  3735. // 檢查樁端的 GFD 結果
  3736. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9)
  3737. {
  3738. setChargerMode(gun_index, MODE_CHARGING);
  3739. }
  3740. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3741. {
  3742. // GFD 錯誤停止
  3743. RecordAlarmCode("012236");
  3744. }
  3745. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3746. {
  3747. // GFD 警告
  3748. RecordAlarmCode("012298");
  3749. }
  3750. }
  3751. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3752. {
  3753. // 檢查樁端的 GFD 結果
  3754. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  3755. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3756. {
  3757. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  3758. }
  3759. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3760. {
  3761. // GFD 錯誤停止
  3762. RecordAlarmCode("012235");
  3763. }
  3764. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3765. {
  3766. // GFD 警告
  3767. RecordAlarmCode("012297");
  3768. }
  3769. }
  3770. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3771. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  3772. OcppRemoteStop(gun_index) == YES)
  3773. {
  3774. // 板端或後臺要求停止
  3775. ChargingTerminalProcess(gun_index);
  3776. }
  3777. // LCM => Pre-charging
  3778. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3779. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  3780. }
  3781. break;
  3782. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  3783. {
  3784. if (isModeChange(gun_index))
  3785. {
  3786. PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
  3787. StopGunInfoTimeoutDet(gun_index);
  3788. ftime(&startChargingTime[gun_index]);
  3789. ChangeStartOrStopDateTime(YES, gun_index);
  3790. OcppStartTransation(gun_index);
  3791. }
  3792. ftime(&endChargingTime[gun_index]);
  3793. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  3794. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3795. {
  3796. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3797. {
  3798. // GFD 錯誤停止
  3799. RecordAlarmCode("012234");
  3800. }
  3801. else if ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3802. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  3803. {
  3804. // UVP
  3805. RecordAlarmCode("012289");
  3806. ChargingTerminalProcess(gun_index);
  3807. }
  3808. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3809. {
  3810. // GFD 警告
  3811. RecordAlarmCode("012296");
  3812. }
  3813. else
  3814. CleanAlarmCodeByString("012296");
  3815. }
  3816. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3817. {
  3818. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3819. {
  3820. // GFD 錯誤停止
  3821. RecordAlarmCode("012236");
  3822. }
  3823. else if (isPrechargeStatus_gb(gun_index) == 10 &&
  3824. ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3825. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)))
  3826. {
  3827. // UVP
  3828. RecordAlarmCode("012290");
  3829. ChargingTerminalProcess(gun_index);
  3830. }
  3831. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3832. {
  3833. // GFD 警告
  3834. RecordAlarmCode("012298");
  3835. }
  3836. else
  3837. CleanAlarmCodeByString("012298");
  3838. }
  3839. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3840. {
  3841. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3842. {
  3843. // GFD 錯誤停止
  3844. RecordAlarmCode("012235");
  3845. }
  3846. else if ((chargingInfo[gun_index]->EvBatterytargetVoltage > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  3847. (chargingInfo[gun_index]->RemainChargingDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  3848. {
  3849. // UVP
  3850. RecordAlarmCode("012288");
  3851. ChargingTerminalProcess(gun_index);
  3852. }
  3853. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  3854. {
  3855. // GFD 警告
  3856. RecordAlarmCode("012297");
  3857. }
  3858. else
  3859. CleanAlarmCodeByString("012297");
  3860. }
  3861. if (isEvBoardStopChargeFlag(gun_index) ||
  3862. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL ||
  3863. OcppRemoteStop(gun_index) ||
  3864. CheckBackendChargingTimeout(gun_index) ||
  3865. CheckBackendChargingEnergy(gun_index))
  3866. {
  3867. PRINTF_FUNC("S_CHARGING=======Stop=========================== %f \n", chargingInfo[gun_index]->EvBatterytargetVoltage);
  3868. // 板端或後臺要求停止
  3869. ChargingTerminalProcess(gun_index);
  3870. }
  3871. // LCM => Charging
  3872. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3873. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  3874. }
  3875. break;
  3876. case S_TERMINATING:
  3877. {
  3878. if (isModeChange(gun_index))
  3879. {
  3880. PRINTF_FUNC ("terminating......................... %x \n", gun_index);
  3881. StopGunInfoTimeoutDet(gun_index);
  3882. }
  3883. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  3884. {
  3885. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  3886. if (isEvStopCharging_chademo(gun_index) == YES ||
  3887. isPrechargeStatus_chademo(gun_index) <= 0)
  3888. {
  3889. setChargerMode(gun_index, MODE_COMPLETE);
  3890. }
  3891. }
  3892. else if (chargingInfo[gun_index]->Type == _Type_GB)
  3893. {
  3894. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  3895. if (isEvStopCharging_gb(gun_index) == YES ||
  3896. isPrechargeStatus_gb(gun_index) <= 0)
  3897. {
  3898. setChargerMode(gun_index, MODE_COMPLETE);
  3899. }
  3900. }
  3901. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  3902. {
  3903. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  3904. if (isEvStopCharging_ccs(gun_index) == YES &&
  3905. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  3906. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  3907. {
  3908. setChargerMode(gun_index, MODE_COMPLETE);
  3909. }
  3910. }
  3911. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3912. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  3913. }
  3914. break;
  3915. case S_COMPLETE:
  3916. {
  3917. if (isModeChange(gun_index))
  3918. {
  3919. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  3920. OcppStopTransation(gun_index);
  3921. ftime(&endChargingTime[gun_index]);
  3922. if (chargingInfo[gun_index]->RemainChargingDuration != 0)
  3923. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  3924. StopGunInfoTimeoutDet(gun_index);
  3925. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  3926. ChangeStartOrStopDateTime(NO, gun_index);
  3927. }
  3928. if(chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  3929. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  3930. {
  3931. setChargerMode(gun_index, MODE_IDLE);
  3932. }
  3933. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  3934. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  3935. }
  3936. break;
  3937. case S_CCS_PRECHARGE_ST0:
  3938. {
  3939. if (isModeChange(gun_index))
  3940. {
  3941. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  3942. StopGunInfoTimeoutDet(gun_index);
  3943. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  3944. }
  3945. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3946. {
  3947. // GFD 錯誤停止
  3948. RecordAlarmCode("012235");
  3949. }
  3950. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3951. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3952. {
  3953. // 板端要求停止
  3954. ChargingTerminalProcess(gun_index);
  3955. }
  3956. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  3957. // 切換 D+ Relay to Precharge Relay
  3958. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  3959. {
  3960. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  3961. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  3962. {
  3963. PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  3964. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  3965. }
  3966. }
  3967. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  3968. {
  3969. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  3970. }
  3971. break;
  3972. }
  3973. case S_CCS_PRECHARGE_ST1:
  3974. {
  3975. if (isModeChange(gun_index))
  3976. {
  3977. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  3978. }
  3979. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3980. {
  3981. // GFD 錯誤停止
  3982. RecordAlarmCode("012235");
  3983. }
  3984. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  3985. chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  3986. {
  3987. // 板端要求停止
  3988. ChargingTerminalProcess(gun_index);
  3989. }
  3990. // 等待小板通知進入充電
  3991. // 切換 D+ Relay to Precharge Relay
  3992. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  3993. {
  3994. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  3995. setChargerMode(gun_index, MODE_CHARGING);
  3996. }
  3997. break;
  3998. }
  3999. }
  4000. }
  4001. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  4002. {
  4003. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  4004. }
  4005. else
  4006. {
  4007. bool dcPageRun = false;
  4008. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  4009. {
  4010. if (ac_chargingInfo[0]->SystemStatus == S_IDLE)
  4011. ChangeLcmByIndex(_LCM_IDLE);
  4012. else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING)
  4013. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  4014. else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  4015. ChangeLcmByIndex(_LCM_CHARGING);
  4016. else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  4017. ac_chargingInfo[0]->SystemStatus == S_COMPLETE)
  4018. ChangeLcmByIndex(_LCM_COMPLETE);
  4019. else
  4020. dcPageRun = true;
  4021. }
  4022. else
  4023. dcPageRun = true;
  4024. if (dcPageRun)
  4025. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  4026. }
  4027. write(wtdFd, "a", 1);
  4028. usleep(whileLoopTime);
  4029. }
  4030. return FAIL;
  4031. }