main.c 114 KB

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