main.c 115 KB

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